| 43 | |
| 44 | |
| 45 | static int64_t genReqPack(uint32_t funcid, uint8_t *data, int dataLen, uint8_t **outPack, uint32_t *outLen, uint32_t callertid, int64_t timestamp) { |
| 46 | pdbg("getReqPack funcid:%d, dataLen:%d callertid:%d timestamp:%d", funcid, dataLen, callertid, TOINT(timestamp/1000000L)); |
| 47 | |
| 48 | if (dataLen > 0 && data == NULL) { |
| 49 | perr("getReqPack input data len %d data:%d", dataLen, TOINT(data)); |
| 50 | return -1; |
| 51 | } |
| 52 | |
| 53 | if (funcid > 65535) { |
| 54 | perr("getReqPack funcid should less than 65535, funid:%d", funcid); |
| 55 | return -2; |
| 56 | } |
| 57 | |
| 58 | if (HEADER_VERSION < HEADER_PROTOCAL_VERSION) { |
| 59 | perr("getReqPack HEADER_VERSION err:%d", HEADER_VERSION); |
| 60 | return -1; |
| 61 | } |
| 62 | |
| 63 | if (HEADER_VERSION >= HEADER_JSON_VERSION) {//json |
| 64 | uint32_t totalLen = dataLen + sizeof(AMCReqHeaderV2); |
| 65 | uint8_t *buf = new uint8_t[totalLen]; |
| 66 | |
| 67 | AMCReqHeaderV2 *header = reinterpret_cast<AMCReqHeaderV2 *>(buf); |
| 68 | header->begin = HEADER_BEGIN; |
| 69 | header->version = HEADER_VERSION; |
| 70 | header->funcid = funcid; |
| 71 | header->bodylen = dataLen; |
| 72 | header->callertid = callertid; |
| 73 | header->timestamp = timestamp; |
| 74 | |
| 75 | header->headerlen = sizeof(AMCReqHeaderV2); |
| 76 | header->bodyformat = HEADER_BODYFORMAT_JSON; |
| 77 | struct timeval now; |
| 78 | gettimeofday(&now, NULL); |
| 79 | header->requestid = (static_cast<int64_t>(now.tv_sec) * 1000 * 1000) + now.tv_usec; |
| 80 | if (dataLen) { |
| 81 | memcpy(buf + sizeof(AMCReqHeaderV2), data, dataLen); |
| 82 | } |
| 83 | |
| 84 | *outPack = buf; |
| 85 | *outLen = totalLen; |
| 86 | |
| 87 | pdbg("genReqPack json header len:%d, requestid:%lld", header->headerlen, header->requestid); |
| 88 | |
| 89 | return header->requestid; |
| 90 | } else {//proto |
| 91 | uint32_t totalLen = dataLen + sizeof(AMCReqHeader); |
| 92 | uint8_t *buf = new uint8_t[totalLen]; |
| 93 | |
| 94 | AMCReqHeader *header = reinterpret_cast<AMCReqHeader *>(buf); |
| 95 | header->begin = HEADER_BEGIN; |
| 96 | header->version = HEADER_VERSION; |
| 97 | header->funcid = funcid; |
| 98 | header->bodylen = dataLen; |
| 99 | header->callertid = callertid; |
| 100 | header->timestamp = timestamp; |
| 101 | struct timeval now; |
| 102 | gettimeofday(&now, NULL); |