📄 databaseserver.cpp
字号:
#include "Database.h"#include <stdio.h>HANDLE g_hStopDBSLoopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);// Function name : DatabaseServer::DatabaseServer// Description : // Return type : DatabaseServer::DatabaseServer(){ // Start the Winsock dll WSADATA wsaData; int err = WSAStartup( MAKEWORD( 2, 0 ), &wsaData ); if (err != 0) printf("Winsock2 dll not initialized\n"); m_hServerThread = NULL; m_nPort = 0; strcpy(m_pszHost, "127.0.0.1"); gethostname(m_pszHost, 100); m_pList = NULL; m_hMutex = CreateMutex(NULL, FALSE, NULL);}// Function name : DeleteValueList// Description : // Return type : void // Argument : DatabaseServer::ValueNode *pListvoid DeleteValueList(DatabaseServer::ValueNode *pList){ if (pList == NULL) return; DeleteValueList(pList->pNext); if (pList->pData != NULL) delete pList->pData; delete pList;}// Function name : DeleteKeyList// Description : // Return type : void // Argument : DatabaseServer::KeyNode *pListvoid DeleteKeyList(DatabaseServer::KeyNode *pList){ if (pList == NULL) return; DeleteKeyList(pList->pNext); DeleteValueList(pList->pValueList); if (pList->pszKey != NULL) delete pList->pszKey; delete pList;}// Function name : DeleteIDList// Description : // Return type : void // Argument : DatabaseServer::IDNode *pListvoid DeleteIDList(DatabaseServer::IDNode *pList){ if (pList == NULL) return; DeleteIDList(pList->pNext); DeleteKeyList(pList->pKeyList); delete pList;}// Function name : DatabaseServer::~DatabaseServer// Description : // Return type : DatabaseServer::~DatabaseServer(){ if (m_hServerThread != NULL) { //TerminateThread(m_hServerThread, 0); //m_hServerThread = NULL; SetEvent(g_hStopDBSLoopEvent); while (m_hServerThread) Sleep(200); } CloseHandle(g_hStopDBSLoopEvent); g_hStopDBSLoopEvent = NULL; WaitForSingleObject(m_hMutex, 5000); DeleteIDList(m_pList); m_pList = NULL; ReleaseMutex(m_hMutex); CloseHandle(m_hMutex); m_hMutex = NULL;}// Function name : DatabaseServer::Start// Description : // Return type : bool bool DatabaseServer::Start(){ if (m_hServerThread == NULL) { DWORD dwThreadID; m_hServerThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)DatabaseServerThread, this, 0, &dwThreadID); if (m_hServerThread == NULL) { return false; } } return true;}// Function name : DatabaseServer::GetHost// Description : // Return type : bool // Argument : char *pszHost// Argument : int lengthbool DatabaseServer::GetHost(char *pszHost, int length){ if (strlen(m_pszHost) >= (unsigned int)length) return false; strcpy(pszHost, m_pszHost); return true;}// Function name : DatabaseServer::SetPort// Description : // Return type : bool // Argument : int nPortbool DatabaseServer::SetPort(int nPort){ if (m_hServerThread != NULL) return false; m_nPort = nPort; return true;}// Function name : DatabaseServer::GetPort// Description : // Return type : int int DatabaseServer::GetPort(){ if (m_hServerThread != NULL) { while (m_nPort == 0) Sleep(200); return m_nPort; } return -1;}// Function name : DatabaseServer::Stop// Description : // Return type : bool bool DatabaseServer::Stop(){ if (m_hServerThread != NULL) { //TerminateThread(m_hServerThread, 0); //m_hServerThread = NULL; SetEvent(g_hStopDBSLoopEvent); while (m_hServerThread) Sleep(200); } return true;}// Function name : DatabaseServer::Delete// Description : // Return type : int // Argument : char *pszIDint DatabaseServer::Delete(char *pszID){ IDNode *pNode, *pTrailer; if (WaitForSingleObject(m_hMutex, DATABASE_TIMEOUT) != WAIT_OBJECT_0) return MPI_DBS_FAIL; pNode = pTrailer = m_pList; while (pNode != NULL) { if (strcmp(pNode->pszID, pszID) == 0) { if (m_pList == pNode) m_pList = pNode->pNext; else pTrailer->pNext = pNode->pNext; pNode->pNext = NULL; DeleteIDList(pNode); ReleaseMutex(m_hMutex); return MPI_DBS_SUCCESS; } if (pTrailer != pNode) pTrailer = pTrailer->pNext; pNode = pNode->pNext; } ReleaseMutex(m_hMutex); return MPI_DBS_SUCCESS;}// Function name : DatabaseServer::Delete// Description : // Return type : int // Argument : char *pszID// Argument : char *pszKeyint DatabaseServer::Delete(char *pszID, char *pszKey){ IDNode *pNode; if (WaitForSingleObject(m_hMutex, DATABASE_TIMEOUT) != WAIT_OBJECT_0) return MPI_DBS_FAIL; pNode = m_pList; while (pNode != NULL) { if (strcmp(pNode->pszID, pszID) == 0) { KeyNode *pKey, *pTrailer; pKey = pTrailer = pNode->pKeyList; while (pKey != NULL) { if (strcmp(pKey->pszKey, pszKey) == 0) { if (pNode->pKeyList == pKey) pNode->pKeyList = pNode->pKeyList->pNext; else pTrailer->pNext = pKey->pNext; DeleteValueList(pKey->pValueList); delete pKey; ReleaseMutex(m_hMutex); return MPI_DBS_SUCCESS; } if (pTrailer != pKey) pTrailer = pTrailer->pNext; pKey = pKey->pNext; } } pNode = pNode->pNext; } ReleaseMutex(m_hMutex); return MPI_DBS_SUCCESS;}// Function name : DatabaseServer::Get// Description : // Return type : int // Argument : char *pszID// Argument : char *pszKey// Argument : void *&pValueData// Argument : int *lengthint DatabaseServer::Get(char *pszID, char *pszKey, void *&pValueData, int *length){ IDNode *pNode; KeyNode *pKey; ValueNode *pValue; while (true) { if (WaitForSingleObject(m_hMutex, DATABASE_TIMEOUT) != WAIT_OBJECT_0) return MPI_DBS_FAIL; pNode = m_pList; while (pNode != NULL) { if (strcmp(pNode->pszID, pszID) == 0) { pKey = pNode->pKeyList; while (pKey != NULL) { if (strcmp(pKey->pszKey, pszKey) == 0) { if (pKey->pValueList != NULL) { pValue = pKey->pValueList; if (pValue->length > *length) { *length = pValue->length; ReleaseMutex(m_hMutex); return MPI_DBS_FAIL; } if (pKey->bPersistent) { // make a copy of the data pValueData = new char[pValue->length]; memcpy(pValueData, pValue->pData, pValue->length); *length = pValue->length; } else { // consume the data pValueData = pValue->pData; *length = pValue->length; // put the next entry as the new head of the list pKey->pValueList = pValue->pNext; delete pValue; } ReleaseMutex(m_hMutex); return MPI_DBS_SUCCESS; } } pKey = pKey->pNext; } } pNode = pNode->pNext; } ReleaseMutex(m_hMutex); Sleep(100); } return MPI_DBS_SUCCESS;}// Function name : DatabaseServer::Put// Description : // Return type : int // Argument : char *pszID// Argument : char *pszKey// Argument : void *pValueData// Argument : int length// Argument : bool bPersistentint DatabaseServer::Put(char *pszID, char *pszKey, void *pValueData, int length, bool bPersistent){ IDNode *pIter, *pLast; if (WaitForSingleObject(m_hMutex, DATABASE_TIMEOUT) != WAIT_OBJECT_0) return MPI_DBS_FAIL; if (m_pList == NULL) { // The list is empty KeyNode *pKey = new KeyNode; ValueNode *pValue = new ValueNode; IDNode *pNode = new IDNode; pNode->pKeyList = NULL; pNode->pNext = NULL; pNode->pKeyList = pKey; strcpy(pNode->pszID, pszID); pKey->bPersistent = bPersistent; pKey->pNext = NULL; pKey->pValueList = pValue; pKey->pszKey = pszKey;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -