| 41 | std::map<int, RecvCache *> mapRecvCache; |
| 42 | |
| 43 | int recvEvent(Event event, int fd, int pid, uid_t uid, const char *path, uint8_t *recvData, int recvDataLen) { |
| 44 | if (event == EVENT_ERROR) { |
| 45 | perr("LocalSocketServer EVENT_ERROR: uid:%d path:%s", uid, path); |
| 46 | return 0; |
| 47 | } |
| 48 | if (event == EVENT_CONNECT) { |
| 49 | pwrn("Connect By uid:%d fd:%d" , uid, fd); |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | if (event == EVENT_DATA) { |
| 54 | |
| 55 | uint8_t *readPtr = recvData; |
| 56 | while (const uint32_t readLeft = (recvDataLen - (readPtr - recvData))) { |
| 57 | if (!mapRecvCache.count(fd)) { |
| 58 | mapRecvCache[fd] = new RecvCache(); |
| 59 | } |
| 60 | RecvCache * const recvCache = mapRecvCache[fd]; |
| 61 | |
| 62 | if (recvCache->reqHeaderOffset < sizeof(AMCReqHeader)) { // fill header |
| 63 | |
| 64 | const int shouldRead = MIN(sizeof(AMCReqHeader) - recvCache->reqHeaderOffset, readLeft ); |
| 65 | memcpy(recvCache->reqHeaderBuf + recvCache->reqHeaderOffset, readPtr, shouldRead); |
| 66 | |
| 67 | readPtr += shouldRead; |
| 68 | recvCache->reqHeaderOffset += shouldRead; |
| 69 | |
| 70 | if (recvCache->reqHeaderOffset < sizeof(AMCReqHeader)) { // wait next event |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | AMCReqHeader *pHeader = (AMCReqHeader *)(recvCache->reqHeaderBuf); |
| 75 | if (pHeader->begin != HEADER_BEGIN) { |
| 76 | perr("Check Header BEGIN failed:0x%x server:0x%x", pHeader->begin, HEADER_BEGIN); |
| 77 | delete recvCache; |
| 78 | mapRecvCache.erase(fd); |
| 79 | return -1; |
| 80 | } |
| 81 | |
| 82 | if (recvCache->reqPayloadOffset < pHeader->bodylen) { |
| 83 | if (recvCache->reqPayload == NULL) { |
| 84 | recvCache->reqPayload = new uint8_t[pHeader->bodylen]; |
| 85 | } |
| 86 | |
| 87 | const int shouldRead = std::min(pHeader->bodylen - recvCache->reqPayloadOffset, readLeft ); |
| 88 | memcpy(recvCache->reqPayload + recvCache->reqPayloadOffset, readPtr, shouldRead); |
| 89 | |
| 90 | readPtr += shouldRead; |
| 91 | recvCache->reqPayloadOffset += shouldRead; |
| 92 | |
| 93 | if (recvCache->reqPayloadOffset < pHeader->bodylen) { // wait next event |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | ServerDataWorker<int> *pp = new ServerDataWorker<int>(); |
| 99 | int64_t requestId = pp->processReceive(uid, &fd, pHeader, recvCache->reqPayload, pHeader->bodylen, inst); |
| 100 | if (requestId < 0) { |
nothing calls this directly
no test coverage detected