int fdfs_recv_header(ConnectionInfo *pTrackerServer, int64_t *in_bytes) { TrackerHeader resp; int result; if ((result=tcprecvdata_nb(pTrackerServer->sock, &resp, \ sizeof(resp), g_fdfs_network_timeout)) != 0) { logError("file: "__FILE__", line: %d, " \ "server: %s:%d, recv data fail, " \ "errno: %d, error info: %s", \ __LINE__, pTrackerServer->ip_addr, \ pTrackerServer->port, \ result, STRERROR(result)); *in_bytes = 0; return result; } if (resp.status != 0) { logError("file: "__FILE__", line: %d, " \ "server: %s:%d, response status %d != 0", \ __LINE__, pTrackerServer->ip_addr, \ pTrackerServer->port, resp.status); *in_bytes = 0; return resp.status; } *in_bytes = buff2long(resp.pkg_len); if (*in_bytes < 0) { logError("file: "__FILE__", line: %d, " \ "server: %s:%d, recv package size " \ INT64_PRINTF_FORMAT" is not correct", \ __LINE__, pTrackerServer->ip_addr, \ pTrackerServer->port, *in_bytes); *in_bytes = 0; return EINVAL; } return resp.status; }
int fdht_recv_header(FDHTServerInfo *pServer, fdht_pkg_size_t *in_bytes) { FDHTProtoHeader resp; int result; if ((result=tcprecvdata_nb(pServer->sock, &resp, \ sizeof(resp), g_fdht_network_timeout)) != 0) { logError("file: "__FILE__", line: %d, " \ "server: %s:%d, recv data fail, " \ "errno: %d, error info: %s", \ __LINE__, pServer->ip_addr, \ pServer->port, \ result, STRERROR(result)); *in_bytes = 0; return result; } if (resp.status != 0) { *in_bytes = 0; return resp.status; } *in_bytes = buff2int(resp.pkg_len); if (*in_bytes < 0) { logError("file: "__FILE__", line: %d, " \ "server: %s:%d, recv package size %d " \ "is not correct", \ __LINE__, pServer->ip_addr, \ pServer->port, *in_bytes); *in_bytes = 0; return EINVAL; } return resp.status; }
UINT32 TrackerMgr::GetStorageStat(ConnectionInfo *pTrackerServer, TCHAR *pszGroupName, FDFSStorageStat *pStat, UINT32 nLen, UINT32 *pnStatCount) { UINT32 nRet; UINT64 nInBytes; StorageStat rawStat[FDFS_MAX_GROUPS]; BYTE byOut[sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN]; memset(byOut, 0, sizeof(byOut)); memset(pStat, 0, sizeof(FDFSStorageStat) * nLen); UINT32 nNameLen = strlen(pszGroupName); if (nNameLen > FDFS_GROUP_NAME_MAX_LEN) { nNameLen = FDFS_GROUP_NAME_MAX_LEN; } TrackerHeader *pHeader = (TrackerHeader*)byOut; pHeader->byCmd = TRACKER_PROTO_CMD_SERVER_LIST_STORAGE; memcpy(byOut + sizeof(TrackerHeader), pszGroupName, nNameLen); long642buff(FDFS_GROUP_NAME_MAX_LEN, (char*)pHeader->byPkgLen); EnterCriticalSection(&pTrackerServer->csSend); if ((nRet = tcpsenddata_nb(pTrackerServer->sock, byOut, sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN, DEFAULT_NETWORK_TIMEOUT)) != 0) { closesocket(pTrackerServer->sock); pTrackerServer->sock = INVALID_SOCKET; LeaveCriticalSection(&pTrackerServer->csSend); WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::GetStorageStat tcpsenddata_nb Header Failed, Error Code:%d"), nRet); return enumNetworkError_FDFS; } TrackerHeader resp; int nCount = 0; EnterCriticalSection(&pTrackerServer->csRecv); if ((nRet = tcprecvdata_nb(pTrackerServer->sock, &resp, sizeof(resp), DEFAULT_NETWORK_TIMEOUT, &nCount)) != 0) { closesocket(pTrackerServer->sock); pTrackerServer->sock = INVALID_SOCKET; LeaveCriticalSection(&pTrackerServer->csRecv); LeaveCriticalSection(&pTrackerServer->csSend); WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::GetStorageStat tcprecvdata_nb Header Failed, Error Code:%d"), nRet); return enumNetworkError_FDFS; } if (resp.byStatus != 0) { closesocket(pTrackerServer->sock); pTrackerServer->sock = INVALID_SOCKET; LeaveCriticalSection(&pTrackerServer->csRecv); LeaveCriticalSection(&pTrackerServer->csSend); WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::GetStorageStat Status Error, %d"), resp.byStatus); return enumFailure_FDFS; } nInBytes = buff2long64((const char*)(resp.byPkgLen)); if (nInBytes < 0 || (nInBytes % sizeof(StorageStat)) != 0 || (nInBytes / sizeof(StorageStat)) > FDFS_MAX_SERVERS_EACH_GROUP) { closesocket(pTrackerServer->sock); pTrackerServer->sock = INVALID_SOCKET; LeaveCriticalSection(&pTrackerServer->csRecv); LeaveCriticalSection(&pTrackerServer->csSend); WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::GetStorageStat Packet Len Error")); return enumFailure_FDFS; } *pnStatCount = nInBytes / sizeof(StorageStat); //包头接收完毕,接收Body if ((nRet = tcprecvdata_nb(pTrackerServer->sock, rawStat, nInBytes, DEFAULT_NETWORK_TIMEOUT, &nCount)) != 0) { closesocket(pTrackerServer->sock); pTrackerServer->sock = INVALID_SOCKET; LeaveCriticalSection(&pTrackerServer->csRecv); LeaveCriticalSection(&pTrackerServer->csSend); WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::GetStorageStat tcprecvdata_nb Body Failed, Error Code:%d"), nRet); return enumNetworkError_FDFS; } LeaveCriticalSection(&pTrackerServer->csRecv); LeaveCriticalSection(&pTrackerServer->csSend); //数据接收完毕,开始处理 if(nLen < *pnStatCount) { WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::GetStorageStat Storages Is Too Many")); return enumFailure_FDFS; } StorageStatBuff *pStatBuff = NULL; StorageStat *pSrc = NULL; FDFSStorageInfo *pStorageInfo = NULL; FDFSStorageStat *pDest = NULL; for(int i = 0; i < *pnStatCount; i++) { pDest = &pStat[i]; pSrc = &rawStat[i]; pStatBuff = &(pSrc->statBuff); pStorageInfo = &(pDest->stat); pDest->byStatus = pSrc->byStatus; //保留最后一个\0 memcpy(pDest->szID, pSrc->byID, FDFS_STORAGE_ID_MAX_SIZE - 1); memcpy(pDest->szIpAddr, pSrc->byIP, IP_ADDRESS_SIZE - 1); memcpy(pDest->szSrcId, pSrc->bySrcID, FDFS_STORAGE_ID_MAX_SIZE - 1); strcpy(pDest->szDomainName, (const char*)pSrc->byDomainName); strcpy(pDest->szVersion, (const char*)pSrc->byVersion); pDest->joinTime = buff2long64((const char*)pSrc->byJoinTime); pDest->upTime = buff2long64((const char*)pSrc->byUpTime); pDest->nTotalMb = buff2long64((const char*)pSrc->byTotalMB); pDest->nFreeMb = buff2long64((const char*)pSrc->byFreeMB); pDest->nUploadPriority = buff2long64((const char*)pSrc->byUploadPriority); pDest->nStorePathCount = buff2long64((const char*)pSrc->byStorePathCount); pDest->nSubdirCountPerPath = buff2long64((const char*)pSrc->bySubdirCountPerPath); pDest->nStoragePort = buff2long64((const char*)pSrc->byStoragePort); pDest->nStorageHttpPort = buff2long64((const char*)pSrc->byStorageHttpPort); pDest->nCurrentWritePath = buff2long64((const char*)pSrc->byCurrentWritePath); pStorageInfo->connection.nAllocCount = buff2int((const char*)pStatBuff->connection.byAllocCount); pStorageInfo->connection.nCurrentCount = buff2int((const char*)pStatBuff->connection.byCurrentCount); pStorageInfo->connection.nMaxCount = buff2int((const char*)pStatBuff->connection.byMaxCount); pStorageInfo->nTotalUploadCount = buff2long64((const char*)pStatBuff->byTotalUploadCount); pStorageInfo->nSuccessUploadCount = buff2long64((const char*)pStatBuff->bySuccessUploadCount); pStorageInfo->nTotalAppendCount = buff2long64((const char*)pStatBuff->byTotalAppendCount); pStorageInfo->nSuccessAppendCount = buff2long64((const char*)pStatBuff->bySuccessAppendCount); pStorageInfo->nTotalModifyCount = buff2long64((const char*)pStatBuff->byTotalModifyCount); pStorageInfo->nSuccessModifyCount = buff2long64((const char*)pStatBuff->bySuccessModifyCount); pStorageInfo->nTotalTruncateCount = buff2long64((const char*)pStatBuff->byTotalTruncateCount); pStorageInfo->nSuccessTruncateCount = buff2long64((const char*)pStatBuff->bySuccessTruncateCount); pStorageInfo->nTotalSetMetaCount = buff2long64((const char*)pStatBuff->byTotalSetMetaCount); pStorageInfo->nSuccessSetMetaCount = buff2long64((const char*)pStatBuff->bySuccessSetMetaCount); pStorageInfo->nTotalDeleteCount = buff2long64((const char*)pStatBuff->byTotalDeleteCount); pStorageInfo->nSuccessDeleteCount = buff2long64((const char*)pStatBuff->bySuccessDeleteCount); pStorageInfo->nTotalDownloadCount = buff2long64((const char*)pStatBuff->byTotalDownloadCount); pStorageInfo->nSuccessDownloadCount = buff2long64((const char*)pStatBuff->bySuccessDownloadCount); pStorageInfo->nTotalGetMetaCount = buff2long64((const char*)pStatBuff->byTotalGetMetaCount); pStorageInfo->nSuccessGetMetaCount = buff2long64((const char*)pStatBuff->bySuccessGetMetaCount); pStorageInfo->lastSourceUpdate = buff2long64((const char*)pStatBuff->byLastSourceUpdate); pStorageInfo->lastSyncUpdate = buff2long64((const char*)pStatBuff->byLastSyncUpdate); pStorageInfo->lastSyncedTimestamp = buff2long64((const char*)pStatBuff->byLastSyncedTimestamp); pStorageInfo->nTotalCreateLinkCount = buff2long64((const char*)pStatBuff->byTotalCreateLinkCount); pStorageInfo->nSuccessCreateLinkCount = buff2long64((const char*)pStatBuff->bySuccessCreateLinkCount); pStorageInfo->nTotalDeleteLinkCount = buff2long64((const char*)pStatBuff->byTotalDeleteLinkCount); pStorageInfo->nSuccessDeleteLinkCount = buff2long64((const char*)pStatBuff->bySuccessDeleteLinkCount); pStorageInfo->nTotalUploadBytes = buff2long64((const char*)pStatBuff->byTotalUploadBytes); pStorageInfo->nSuccessUploadBytes = buff2long64((const char*)pStatBuff->bySuccessUploadBytes); pStorageInfo->nTotalAppendBytes = buff2long64((const char*)pStatBuff->byTotalAppendBytes); pStorageInfo->nSuccessAppendBytes = buff2long64((const char*)pStatBuff->bySuccessAppendBytes); pStorageInfo->nTotalModifyBytes = buff2long64((const char*)pStatBuff->byTotalModifyBytes); pStorageInfo->nSuccessModifyBytes = buff2long64((const char*)pStatBuff->bySuccessModifyBytes); pStorageInfo->nTotalDownloadBytes = buff2long64((const char*)pStatBuff->byTotalDownloadBytes); pStorageInfo->nSuccessDownloadBytes = buff2long64((const char*)pStatBuff->bySuccessDownloadBytes); pStorageInfo->nTotalSyncInBytes = buff2long64((const char*)pStatBuff->byTotalSyncInBytes); pStorageInfo->nSuccessSyncInBytes = buff2long64((const char*)pStatBuff->bySuccessSyncInBytes); pStorageInfo->nTotalSyncOutBytes = buff2long64((const char*)pStatBuff->byTotalSyncOutBytes); pStorageInfo->nSuccessSyncOutBytes = buff2long64((const char*)pStatBuff->bySuccessSyncOutBytes); pStorageInfo->nTotalFileOpenCount = buff2long64((const char*)pStatBuff->byTotalFileOpenCount); pStorageInfo->nSuccessFileOpenCount = buff2long64((const char*)pStatBuff->bySuccessFileOpenCount); pStorageInfo->nTotalFileReadCount = buff2long64((const char*)pStatBuff->byTotalFileReadCount); pStorageInfo->nSuccessFileReadCount = buff2long64((const char*)pStatBuff->bySuccessFileReadCount); pStorageInfo->nTotalFileWriteCount = buff2long64((const char*)pStatBuff->byTotalFileWriteCount); pStorageInfo->nSuccessFileWriteCount = buff2long64((const char*)pStatBuff->bySuccessFileWriteCount); pStorageInfo->lastHeartBeatTime = buff2long64((const char*)pStatBuff->byLastHeartBeatTime); pDest->bIfTrunkServer = pSrc->byIfTrunkServer; } return enumSuccess_FDFS; }
UINT32 TrackerMgr::GetGroupStat(ConnectionInfo *pTrackerServer, FDFSGroupStat *pStat, UINT32 nLen, UINT32 *pnStatCount) { UINT32 nRet; UINT64 nInBytes; memset(pStat, 0, sizeof(FDFSGroupStat) * nLen); GroupStat rawStat[FDFS_MAX_GROUPS]; TrackerHeader header; memset(&header, 0, sizeof(header)); header.byCmd = TRACKER_PROTO_CMD_SERVER_LIST_ALL_GROUPS; EnterCriticalSection(&pTrackerServer->csSend); if ((nRet = tcpsenddata_nb(pTrackerServer->sock, &header, sizeof(header), DEFAULT_NETWORK_TIMEOUT)) != 0) { closesocket(pTrackerServer->sock); pTrackerServer->sock = INVALID_SOCKET; LeaveCriticalSection(&pTrackerServer->csSend); WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::GetGroupStat tcpsenddata_nb Header Failed, Error Code:%d"), nRet); return enumNetworkError_FDFS; } TrackerHeader resp; int nCount = 0; EnterCriticalSection(&pTrackerServer->csRecv); if ((nRet = tcprecvdata_nb(pTrackerServer->sock, &resp, sizeof(resp), DEFAULT_NETWORK_TIMEOUT, &nCount)) != 0) { closesocket(pTrackerServer->sock); pTrackerServer->sock = INVALID_SOCKET; LeaveCriticalSection(&pTrackerServer->csRecv); LeaveCriticalSection(&pTrackerServer->csSend); WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::GetGroupStat tcprecvdata_nb Header Failed, Error Code:%d"), nRet); return enumNetworkError_FDFS; } if (resp.byStatus != 0) { closesocket(pTrackerServer->sock); pTrackerServer->sock = INVALID_SOCKET; LeaveCriticalSection(&pTrackerServer->csRecv); LeaveCriticalSection(&pTrackerServer->csSend); WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::GetGroupStat Status Error, %d"), resp.byStatus); return enumFailure_FDFS; } nInBytes = buff2long64((const char*)(resp.byPkgLen)); if (nInBytes < 0 || (nInBytes % sizeof(GroupStat)) != 0 || (nInBytes / sizeof(GroupStat)) > FDFS_MAX_GROUPS) { closesocket(pTrackerServer->sock); pTrackerServer->sock = INVALID_SOCKET; LeaveCriticalSection(&pTrackerServer->csRecv); LeaveCriticalSection(&pTrackerServer->csSend); WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::GetGroupStat Packet Len Error")); return enumFailure_FDFS; } *pnStatCount = nInBytes / sizeof(GroupStat); //包头接收完毕,接收Body if ((nRet = tcprecvdata_nb(pTrackerServer->sock, rawStat, nInBytes, DEFAULT_NETWORK_TIMEOUT, &nCount)) != 0) { closesocket(pTrackerServer->sock); pTrackerServer->sock = INVALID_SOCKET; LeaveCriticalSection(&pTrackerServer->csRecv); LeaveCriticalSection(&pTrackerServer->csSend); WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::GetGroupStat tcprecvdata_nb Body Failed, Error Code:%d"), nRet); return enumNetworkError_FDFS; } LeaveCriticalSection(&pTrackerServer->csRecv); LeaveCriticalSection(&pTrackerServer->csSend); //数据接收完毕,开始处理 if(nLen < *pnStatCount) { WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::GetGroupStat Groups Is Too Many")); return enumFailure_FDFS; } for(int i = 0; i < *pnStatCount; i++) { memcpy(pStat[i].szGroupName, rawStat[i].szGroupName, FDFS_GROUP_NAME_MAX_LEN); pStat[i].nTotalMB = buff2long64((const char*)rawStat[i].byTotalMB); pStat[i].nFreeMB = buff2long64((const char*)rawStat[i].byFreeMB); pStat[i].nTrunkFreeMB = buff2long64((const char*)rawStat[i].byTrunkFreeMB); pStat[i].nCount = buff2long64((const char*)rawStat[i].byCount); pStat[i].nStoragePort = buff2long64((const char*)rawStat[i].byStoragePort); pStat[i].nStorageHttpPort = buff2long64((const char*)rawStat[i].byStorageHttpPort); pStat[i].nActiveCount = buff2long64((const char*)rawStat[i].byActiveCount); pStat[i].nCurrentWriteServer = buff2long64((const char*)rawStat[i].byCurrentWriteServer); pStat[i].nStorePathCount = buff2long64((const char*)rawStat[i].byStorePathCount); pStat[i].nSubdirCountPerPath = buff2long64((const char*)rawStat[i].bySubdirCountPerPath); pStat[i].nCurrentTrunkFileID = buff2long64((const char*)rawStat[i].byCurrentTrunkFileID); } return enumSuccess_FDFS; }
UINT32 TrackerMgr::QueryStorageFetch(ConnectionInfo *pTrackerServer, const BYTE *szGroupName, const BYTE *szRemoteFileName, ServerAddress *pStorageServer) { UINT32 nRemoteFileNameLen; UINT32 nGroupNameLen; UINT32 nOutBytes = 0, nInBytes = 0; UINT32 nRet; BYTE *p = NULL; BYTE byOutBuff[sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN + FDFS_REMOTE_FILE_NAME_MAX_LEN]; TrackerHeader *pHeader; memset(byOutBuff, 0, sizeof(byOutBuff)); pHeader = (TrackerHeader *)byOutBuff; p = byOutBuff + sizeof(TrackerHeader); nGroupNameLen = strlen((const char*)szGroupName); memcpy_s(p, sizeof(byOutBuff) - (p - byOutBuff), szGroupName, nGroupNameLen); p += FDFS_GROUP_NAME_MAX_LEN; nRemoteFileNameLen = strlen((const char*)szRemoteFileName); memcpy_s(p, sizeof(byOutBuff) - (p - byOutBuff), szRemoteFileName, nRemoteFileNameLen); p += nRemoteFileNameLen; nOutBytes = p - byOutBuff; long642buff(nOutBytes - sizeof(TrackerHeader), (char*)pHeader->byPkgLen); pHeader->byCmd = TRACKER_PROTO_CMD_SERVICE_QUERY_FETCH_ONE; EnterCriticalSection(&pTrackerServer->csSend); if ((nRet = tcpsenddata_nb(pTrackerServer->sock, byOutBuff, nOutBytes, DEFAULT_NETWORK_TIMEOUT)) != 0) { closesocket(pTrackerServer->sock); pTrackerServer->sock = INVALID_SOCKET; LeaveCriticalSection(&pTrackerServer->csSend); WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryStorageFetch tcpsenddata_nb Header Failed, Error Code:%d"), nRet); return enumNetworkError_FDFS; } //发包完毕,准备接收数据 TrackerHeader resp; int nCount = 0; EnterCriticalSection(&pTrackerServer->csRecv); if ((nRet = tcprecvdata_nb(pTrackerServer->sock, &resp, sizeof(resp), DEFAULT_NETWORK_TIMEOUT, &nCount)) != 0) { closesocket(pTrackerServer->sock); pTrackerServer->sock = INVALID_SOCKET; LeaveCriticalSection(&pTrackerServer->csRecv); LeaveCriticalSection(&pTrackerServer->csSend); WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryStorageFetch tcprecvdata_nb Header Failed, Error Code:%d"), nRet); return enumNetworkError_FDFS; } if (resp.byStatus != 0) { closesocket(pTrackerServer->sock); pTrackerServer->sock = INVALID_SOCKET; LeaveCriticalSection(&pTrackerServer->csRecv); LeaveCriticalSection(&pTrackerServer->csSend); WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryStorageFetch Status Error, %d"), resp.byStatus); return enumFailure_FDFS; } nInBytes = buff2long64((const char*)(resp.byPkgLen)); if (nInBytes < 0) { closesocket(pTrackerServer->sock); pTrackerServer->sock = INVALID_SOCKET; LeaveCriticalSection(&pTrackerServer->csRecv); LeaveCriticalSection(&pTrackerServer->csSend); WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryStorageFetch Packet Len Error")); return enumFailure_FDFS; } //包头接收完毕,接收Body BYTE *pbyBuff = NULL; pbyBuff = new BYTE[nInBytes]; if(pbyBuff == NULL) { closesocket(pTrackerServer->sock); pTrackerServer->sock = INVALID_SOCKET; LeaveCriticalSection(&pTrackerServer->csRecv); LeaveCriticalSection(&pTrackerServer->csSend); WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryStorageFetch New Error")); delete[] pbyBuff; return enumFailure_FDFS; } if ((nRet = tcprecvdata_nb(pTrackerServer->sock, pbyBuff, nInBytes, DEFAULT_NETWORK_TIMEOUT, &nCount)) != 0) { closesocket(pTrackerServer->sock); pTrackerServer->sock = INVALID_SOCKET; LeaveCriticalSection(&pTrackerServer->csRecv); LeaveCriticalSection(&pTrackerServer->csSend); WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryStorageFetch tcprecvdata_nb Body Failed, Error Code:%d"), nRet); delete[] pbyBuff; return enumNetworkError_FDFS; } LeaveCriticalSection(&pTrackerServer->csRecv); LeaveCriticalSection(&pTrackerServer->csSend); //数据接收完毕,开始解析 //前16字节为Group Name,用不着 //memcpy(szGroupName, pbyBuff, FDFS_GROUP_NAME_MAX_LEN); //*(szGroupName + FDFS_GROUP_NAME_MAX_LEN) = '\0'; memcpy(pStorageServer->szIP, pbyBuff + FDFS_GROUP_NAME_MAX_LEN, IP_ADDRESS_SIZE - 1); pStorageServer->nPort = buff2long64((const char*)(pbyBuff + FDFS_GROUP_NAME_MAX_LEN + IP_ADDRESS_SIZE - 1)); delete[] pbyBuff; return enumSuccess_FDFS; }
UINT32 TrackerMgr::QueryUpdateStorageStore(ConnectionInfo *pTrackerServer, const BYTE *pbyMasterGroupName, const BYTE *pbyMasterFileName, ServerAddress *pStorageServer, UINT32 *nStorePathIndex) { UINT32 nRet; UINT64 nInBytes; BYTE byOutBuff[sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN + FDFS_REMOTE_FILE_NAME_MAX_LEN]; memset(byOutBuff, 0, sizeof(byOutBuff)); TrackerHeader *pHeader = (TrackerHeader*)byOutBuff; memcpy(byOutBuff + sizeof(TrackerHeader), pbyMasterGroupName, FDFS_GROUP_NAME_MAX_LEN); UINT32 nMasterFileNameLen = strlen((const char*)pbyMasterFileName); memcpy(byOutBuff + sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN, pbyMasterFileName, nMasterFileNameLen); long642buff(FDFS_GROUP_NAME_MAX_LEN + nMasterFileNameLen, (char*)pHeader->byPkgLen); pHeader->byCmd = TRACKER_PROTO_CMD_SERVICE_QUERY_UPDATE; EnterCriticalSection(&pTrackerServer->csSend); if ((nRet = tcpsenddata_nb(pTrackerServer->sock, &byOutBuff, sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN + nMasterFileNameLen, DEFAULT_NETWORK_TIMEOUT)) != 0) { closesocket(pTrackerServer->sock); pTrackerServer->sock = INVALID_SOCKET; LeaveCriticalSection(&pTrackerServer->csSend); WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryUpdateStorageStore tcpsenddata_nb Header Failed, Error Code:%d"), nRet); return enumNetworkError_FDFS; } TrackerHeader resp; int nCount = 0; EnterCriticalSection(&pTrackerServer->csRecv); if ((nRet = tcprecvdata_nb(pTrackerServer->sock, &resp, sizeof(resp), DEFAULT_NETWORK_TIMEOUT, &nCount)) != 0) { closesocket(pTrackerServer->sock); pTrackerServer->sock = INVALID_SOCKET; LeaveCriticalSection(&pTrackerServer->csRecv); LeaveCriticalSection(&pTrackerServer->csSend); WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryUpdateStorageStore tcprecvdata_nb Header Failed, Error Code:%d"), nRet); return enumNetworkError_FDFS; } if (resp.byStatus != 0) { closesocket(pTrackerServer->sock); pTrackerServer->sock = INVALID_SOCKET; LeaveCriticalSection(&pTrackerServer->csRecv); LeaveCriticalSection(&pTrackerServer->csSend); WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryUpdateStorageStore Status Error, %d"), resp.byStatus); return enumFailure_FDFS; } nInBytes = buff2long64((const char*)(resp.byPkgLen)); if (nInBytes < 0) { closesocket(pTrackerServer->sock); pTrackerServer->sock = INVALID_SOCKET; LeaveCriticalSection(&pTrackerServer->csRecv); LeaveCriticalSection(&pTrackerServer->csSend); WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryUpdateStorageStore Packet Len Error")); return enumFailure_FDFS; } //包头接收完毕,接收Body BYTE *pbyBuff = NULL; pbyBuff = new BYTE[nInBytes]; if(pbyBuff == NULL) { closesocket(pTrackerServer->sock); pTrackerServer->sock = INVALID_SOCKET; LeaveCriticalSection(&pTrackerServer->csRecv); LeaveCriticalSection(&pTrackerServer->csSend); WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryUpdateStorageStore New Error")); delete[] pbyBuff; return enumFailure_FDFS; } if ((nRet = tcprecvdata_nb(pTrackerServer->sock, pbyBuff, nInBytes, DEFAULT_NETWORK_TIMEOUT, &nCount)) != 0) { closesocket(pTrackerServer->sock); pTrackerServer->sock = INVALID_SOCKET; LeaveCriticalSection(&pTrackerServer->csRecv); LeaveCriticalSection(&pTrackerServer->csSend); WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryUpdateStorageStore tcprecvdata_nb Body Failed, Error Code:%d"), nRet); delete[] pbyBuff; return enumNetworkError_FDFS; } LeaveCriticalSection(&pTrackerServer->csRecv); LeaveCriticalSection(&pTrackerServer->csSend); //数据接收完毕,开始解析 memcpy(pStorageServer->szIP, pbyBuff + FDFS_GROUP_NAME_MAX_LEN, IP_ADDRESS_SIZE - 1); pStorageServer->nPort = buff2long64((const char*)(pbyBuff + FDFS_GROUP_NAME_MAX_LEN + IP_ADDRESS_SIZE - 1)); delete[] pbyBuff; return enumSuccess_FDFS; }
//recv不做重连 UINT32 TrackerMgr::QueryStorageStore(ConnectionInfo *pTrackerServer, ServerAddress *pStorageServer, TCHAR *szGroupName, UINT32 *nStorePathIndex) { UINT32 nRet; UINT64 nInBytes; TrackerHeader header; memset(&header, 0, sizeof(header)); header.byCmd = TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ONE; EnterCriticalSection(&pTrackerServer->csSend); if ((nRet = tcpsenddata_nb(pTrackerServer->sock, &header, sizeof(header), DEFAULT_NETWORK_TIMEOUT)) != 0) { closesocket(pTrackerServer->sock); pTrackerServer->sock = INVALID_SOCKET; LeaveCriticalSection(&pTrackerServer->csSend); WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryStorageStore tcpsenddata_nb Header Failed, Error Code:%d"), nRet); return enumNetworkError_FDFS; } TrackerHeader resp; int nCount = 0; EnterCriticalSection(&pTrackerServer->csRecv); if ((nRet = tcprecvdata_nb(pTrackerServer->sock, &resp, sizeof(resp), DEFAULT_NETWORK_TIMEOUT, &nCount)) != 0) { closesocket(pTrackerServer->sock); pTrackerServer->sock = INVALID_SOCKET; LeaveCriticalSection(&pTrackerServer->csRecv); LeaveCriticalSection(&pTrackerServer->csSend); WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryStorageStore tcprecvdata_nb Header Failed, Error Code:%d"), nRet); return enumNetworkError_FDFS; } if (resp.byStatus != 0) { closesocket(pTrackerServer->sock); pTrackerServer->sock = INVALID_SOCKET; LeaveCriticalSection(&pTrackerServer->csRecv); LeaveCriticalSection(&pTrackerServer->csSend); WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryStorageStore Status Error, %d"), resp.byStatus); if(resp.byStatus == 28) return enumNoEnoughSpace_FDFS; return enumFailure_FDFS; } nInBytes = buff2long64((const char*)(resp.byPkgLen)); if (nInBytes < 0) { closesocket(pTrackerServer->sock); pTrackerServer->sock = INVALID_SOCKET; LeaveCriticalSection(&pTrackerServer->csRecv); LeaveCriticalSection(&pTrackerServer->csSend); WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryStorageStore Packet Len Error")); return enumFailure_FDFS; } //包头接收完毕,接收Body BYTE *pbyBuff = NULL; pbyBuff = new BYTE[nInBytes]; if(pbyBuff == NULL) { closesocket(pTrackerServer->sock); pTrackerServer->sock = INVALID_SOCKET; LeaveCriticalSection(&pTrackerServer->csRecv); LeaveCriticalSection(&pTrackerServer->csSend); WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryStorageStore New Error")); delete[] pbyBuff; return enumFailure_FDFS; } if ((nRet = tcprecvdata_nb(pTrackerServer->sock, pbyBuff, nInBytes, DEFAULT_NETWORK_TIMEOUT, &nCount)) != 0) { closesocket(pTrackerServer->sock); pTrackerServer->sock = INVALID_SOCKET; LeaveCriticalSection(&pTrackerServer->csRecv); LeaveCriticalSection(&pTrackerServer->csSend); WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryStorageStore tcprecvdata_nb Body Failed, Error Code:%d"), nRet); delete[] pbyBuff; return enumNetworkError_FDFS; } LeaveCriticalSection(&pTrackerServer->csRecv); LeaveCriticalSection(&pTrackerServer->csSend); //数据接收完毕,开始解析 memcpy(szGroupName, pbyBuff, FDFS_GROUP_NAME_MAX_LEN); *(szGroupName + FDFS_GROUP_NAME_MAX_LEN) = '\0'; memcpy(pStorageServer->szIP, pbyBuff + FDFS_GROUP_NAME_MAX_LEN, IP_ADDRESS_SIZE - 1); pStorageServer->nPort = buff2long64((const char*)(pbyBuff + FDFS_GROUP_NAME_MAX_LEN + IP_ADDRESS_SIZE - 1)); *nStorePathIndex = *(pbyBuff + FDFS_GROUP_NAME_MAX_LEN + IP_ADDRESS_SIZE - 1 + FDFS_PROTO_PKG_LEN_SIZE); delete[] pbyBuff; return enumSuccess_FDFS; }
int fdht_recv_response(FDHTServerInfo *pServer, \ char **buff, const int buff_size, \ fdht_pkg_size_t *in_bytes) { int result; bool bMalloced; result = fdht_recv_header(pServer, in_bytes); if (result != 0) { return result; } if (*in_bytes == 0) { return 0; } if (*buff == NULL) { *buff = (char *)malloc((*in_bytes) + 1); if (*buff == NULL) { *in_bytes = 0; logError("file: "__FILE__", line: %d, " \ "malloc %d bytes fail", \ __LINE__, (*in_bytes) + 1); return errno != 0 ? errno : ENOMEM; } bMalloced = true; } else { if (*in_bytes > buff_size) { logError("file: "__FILE__", line: %d, " \ "server: %s:%d, recv body bytes: %d" \ " exceed max: %d", \ __LINE__, pServer->ip_addr, \ pServer->port, *in_bytes, buff_size); *in_bytes = 0; return ENOSPC; } bMalloced = false; } if ((result=tcprecvdata_nb(pServer->sock, *buff, \ *in_bytes, g_fdht_network_timeout)) != 0) { logError("file: "__FILE__", line: %d, " \ "server: %s:%d, recv data fail, " \ "errno: %d, error info: %s", \ __LINE__, pServer->ip_addr, \ pServer->port, \ result, STRERROR(result)); *in_bytes = 0; if (bMalloced) { free(*buff); *buff = NULL; } return result; } return 0; }