📄 databaseserver.cpp
字号:
pValue->pNext = NULL; pValue->pData = pValueData; pValue->length = length; m_pList = pNode; ReleaseMutex(m_hMutex); return MPI_DBS_SUCCESS; } pIter = m_pList; while (pIter != NULL) { if (strcmp(pIter->pszID, pszID) == 0) { // We've matched the id string // Now search the key nodes KeyNode *pKeyIter, *pKeyLast = NULL; pKeyIter = pIter->pKeyList; while (pKeyIter != NULL) { if (strcmp(pKeyIter->pszKey, pszKey) == 0) { delete pszKey; // We've matched the key string ValueNode *pValue = new ValueNode; pValue->length = length; pValue->pData = pValueData; pValue->pNext = NULL; //if (pKeyIter->bPersistent) // <----- This method retains the option specified on the first put call pKeyIter->bPersistent = bPersistent; // <---- This method sets the state to match the current call if (bPersistent) { DeleteValueList( pKeyIter->pValueList ); pKeyIter->pValueList = pValue; } else { if (pKeyIter->pValueList == NULL) pKeyIter->pValueList = pValue; else { ValueNode *p = pKeyIter->pValueList; while (p->pNext != NULL) p = p->pNext; p->pNext = pValue; } } ReleaseMutex(m_hMutex); return MPI_DBS_SUCCESS; } if (pKeyIter->pNext == NULL) pKeyLast = pKeyIter; pKeyIter = pKeyIter->pNext; } // We've reached the end of the list without matching the key strings KeyNode *pKey = new KeyNode; pKey->pNext = NULL; pKey->bPersistent = bPersistent; pKey->pszKey = pszKey; pKey->pValueList = new ValueNode; pKey->pValueList->length = length; pKey->pValueList->pData = pValueData; pKey->pValueList->pNext = NULL; if (pIter->pKeyList == NULL) pIter->pKeyList = pKey; else pKeyLast->pNext = pKey; ReleaseMutex(m_hMutex); return MPI_DBS_SUCCESS; } if (pIter->pNext == NULL) pLast = pIter; pIter = pIter->pNext; } // We've reached the end of the list without matching the id string 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; pValue->pNext = NULL; pValue->pData = pValueData; pValue->length = length; pLast->pNext = pNode; ReleaseMutex(m_hMutex); return MPI_DBS_SUCCESS;}// Function name : DatabaseServer::PrintState// Description : // Return type : void void DatabaseServer::PrintState(){ IDNode *pNode = m_pList; KeyNode *pKey; ValueNode *pValue; if (WaitForSingleObject(m_hMutex, DATABASE_TIMEOUT) != WAIT_OBJECT_0) return; printf("DATABASE:\n{\n"); while (pNode) { printf(" NODE \"%s\"\n", pNode->pszID); pKey = pNode->pKeyList; while (pKey) { if (pKey->bPersistent) printf(" KEY \"%s\" persistent\n", pKey->pszKey); else printf(" KEY \"%s\" consumable\n", pKey->pszKey); pValue = pKey->pValueList; while (pValue) { printf(" VALUE: "); fwrite(pValue->pData, pValue->length, 1, stdout); printf("\n"); pValue = pValue->pNext; } pKey = pKey->pNext; } pNode = pNode->pNext; } printf("}\n"); ReleaseMutex(m_hMutex);}// Function name : DatabaseServer::PrintStateToBuffer// Description : This function will incorrectly handle binary data. The values in the database must be strings// in order for this function to work properly.// Return type : void // Argument : char *pszBuffer// Argument : int *pnLengthvoid DatabaseServer::PrintStateToBuffer(char *pszBuffer, int *pnLength){ IDNode *pNode = m_pList; KeyNode *pKey; ValueNode *pValue; char pLocalBuffer[4096]; int nLength = 0; if (WaitForSingleObject(m_hMutex, DATABASE_TIMEOUT) != WAIT_OBJECT_0) return; sprintf(pLocalBuffer, "DATABASE:\n{\n"); if ((int)strlen(pLocalBuffer) < (*pnLength - 1)) { strcpy(pszBuffer, pLocalBuffer); nLength = strlen(pszBuffer); } else { *pnLength = 0; ReleaseMutex(m_hMutex); return; } while (pNode) { sprintf(pLocalBuffer, " NODE \"%s\"\n", pNode->pszID); if ((int)strlen(pLocalBuffer) < (*pnLength - nLength)) { strcat(pszBuffer, pLocalBuffer); nLength = strlen(pszBuffer); } else { *pnLength = nLength; ReleaseMutex(m_hMutex); return; } pKey = pNode->pKeyList; while (pKey) { if (pKey->bPersistent) sprintf(pLocalBuffer, " KEY \"%s\" persistent\n", pKey->pszKey); else sprintf(pLocalBuffer, " KEY \"%s\" consumable\n", pKey->pszKey); if ((int)strlen(pLocalBuffer) < (*pnLength - nLength)) { strcat(pszBuffer, pLocalBuffer); nLength = strlen(pszBuffer); } else { *pnLength = nLength; ReleaseMutex(m_hMutex); return; } pValue = pKey->pValueList; while (pValue) { sprintf(pLocalBuffer, " VALUE: "); //fwrite(pValue->pData, pValue->length, 1, stdout); strcat(pLocalBuffer, (const char *)pValue->pData); //printf("\n"); strcat(pLocalBuffer, "\n"); if ((int)strlen(pLocalBuffer) < (*pnLength - nLength)) { strcat(pszBuffer, pLocalBuffer); nLength = strlen(pszBuffer); } else { *pnLength = nLength; ReleaseMutex(m_hMutex); return; } pValue = pValue->pNext; } pKey = pKey->pNext; } pNode = pNode->pNext; } sprintf(pLocalBuffer, "}\n"); if ((int)strlen(pLocalBuffer) < (*pnLength - nLength)) { strcat(pszBuffer, pLocalBuffer); nLength = strlen(pszBuffer); } *pnLength = nLength; ReleaseMutex(m_hMutex);}// Function name : StrCatGrow// Description : // Return type : void // Argument : char *&pBase// Argument : char *&pCur// Argument : int &length// Argument : char *pCat// Argument : int catlenvoid StrCatGrow(char *&pBase, char *&pCur, int &length, char *pCat, int catlen){ if (pCur - pBase + catlen > length) { char *pBuf = new char[2*length+catlen]; memcpy(pBuf, pBase, length); length = 2*length + catlen; pCur = pBuf + (pCur - pBase); delete pBase; pBase = pBuf; } memcpy(pCur, pCat, catlen); pCur = pCur + catlen;}// Function name : StrCatGrow// Description : // Return type : void // Argument : char *&pBase// Argument : char *&pCur// Argument : int &length// Argument : char *pCatvoid StrCatGrow(char *&pBase, char *&pCur, int &length, char *pCat){ int catlen = strlen(pCat)+1; StrCatGrow(pBase, pCur, length, pCat, catlen); pCur--; // Backup to the '\0' character}// Function name : Database::GetState// Description : // Return type : void // Argument : char *pszOutput// Argument : int *lengthint DatabaseServer::GetState(char *pszOutput, int *length){ IDNode *pNode = m_pList; KeyNode *pKey; ValueNode *pValue; char pBuffer[1024]; int len = 1024; char *pBase = new char[1024]; char *pCur = pBase; pBase[0] = '\0'; if (WaitForSingleObject(m_hMutex, DATABASE_TIMEOUT) != WAIT_OBJECT_0) return MPI_DBS_FAIL; StrCatGrow(pBase, pCur, len, "DATABASE:\n{\n"); while (pNode) { sprintf(pBuffer, " NODE \"%s\"\n", pNode->pszID); StrCatGrow(pBase, pCur, len, pBuffer); pKey = pNode->pKeyList; while (pKey) { if (pKey->bPersistent) { sprintf(pBuffer, " KEY \"%s\" persistent\n", pKey->pszKey); StrCatGrow(pBase, pCur, len, pBuffer); } else { sprintf(pBuffer, " KEY \"%s\" consumable\n", pKey->pszKey); StrCatGrow(pBase, pCur, len, pBuffer); } pValue = pKey->pValueList; while (pValue) { StrCatGrow(pBase, pCur, len, " VALUE: "); StrCatGrow(pBase, pCur, len, (char*)pValue->pData, pValue->length); StrCatGrow(pBase, pCur, len, "\n"); pValue = pValue->pNext; } pKey = pKey->pNext; } pNode = pNode->pNext; } StrCatGrow(pBase, pCur, len, "}\n"); int difference = pCur - pBase + 1; if (difference <= *length) { memcpy(pszOutput, pBase, difference); *length = difference; ReleaseMutex(m_hMutex); return MPI_DBS_SUCCESS; } *length = difference; ReleaseMutex(m_hMutex); return MPI_DBS_FAIL;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -