| 138 | |
| 139 | |
| 140 | static int64_t genRespPack(uint32_t funcid, uint32_t retCode, uint64_t requestId, uint8_t *data, int dataLen, |
| 141 | uint8_t **outPack, uint32_t *outLen) { |
| 142 | |
| 143 | if (funcid > 65535) { |
| 144 | perr("genRespPack funcid should less than 65535, funid:%d", funcid); |
| 145 | return -2; |
| 146 | } |
| 147 | |
| 148 | if (HEADER_VERSION < HEADER_PROTOCAL_VERSION) { |
| 149 | perr("genRespPack HEADER_VERSION err:%d", HEADER_VERSION); |
| 150 | return -1; |
| 151 | } |
| 152 | |
| 153 | if (HEADER_VERSION >= HEADER_JSON_VERSION) {//json |
| 154 | uint32_t totalLen = dataLen + sizeof(AMCRespHeaderV2); |
| 155 | uint8_t *buf = new uint8_t[totalLen]; |
| 156 | |
| 157 | AMCRespHeaderV2 *p = reinterpret_cast<AMCRespHeaderV2 *>(buf); |
| 158 | p->retCode = retCode; |
| 159 | p->requestid = requestId; |
| 160 | p->funcid = funcid; |
| 161 | p->bodylen = dataLen; |
| 162 | struct timespec ts = {0, 0}; |
| 163 | clock_gettime(CLOCK_BOOTTIME_ALARM, &ts); |
| 164 | p->timestamp = (static_cast<int64_t>(ts.tv_sec) * 1000 * 1000 * 1000) + ts.tv_nsec; |
| 165 | pdbg("genRespPack json funcid:%d timestamp:%d ms requestId:%lld", funcid, TOINT(p->timestamp/1000000L), requestId); |
| 166 | p->begin = HEADER_BEGIN; |
| 167 | p->version = HEADER_VERSION; |
| 168 | |
| 169 | p->headerlen = sizeof(AMCRespHeaderV2); |
| 170 | p->bodyformat = HEADER_BODYFORMAT_JSON; |
| 171 | |
| 172 | if (dataLen) { |
| 173 | memcpy(buf + sizeof(AMCRespHeaderV2), data, dataLen); |
| 174 | } |
| 175 | *outPack = buf; |
| 176 | *outLen = totalLen; |
| 177 | |
| 178 | } else {//proto |
| 179 | uint32_t totalLen = dataLen + sizeof(AMCRespHeader); |
| 180 | uint8_t *buf = new uint8_t[totalLen]; |
| 181 | AMCRespHeader *p = reinterpret_cast<AMCRespHeader *>(buf); |
| 182 | p->retCode = retCode; |
| 183 | p->requestid = requestId; |
| 184 | p->funcid = funcid; |
| 185 | p->bodylen = dataLen; |
| 186 | struct timespec ts = {0, 0}; |
| 187 | clock_gettime(CLOCK_BOOTTIME_ALARM, &ts); |
| 188 | p->timestamp = (static_cast<int64_t>(ts.tv_sec) * 1000 * 1000 * 1000) + ts.tv_nsec; |
| 189 | pdbg("genRespPack proto funcid:%d timestamp:%d ms requestId:%lld", funcid, TOINT(p->timestamp/1000000L), requestId); |
| 190 | p->begin = HEADER_BEGIN; |
| 191 | p->version = HEADER_VERSION; |
| 192 | |
| 193 | if (dataLen) { |
| 194 | memcpy(buf + sizeof(AMCRespHeader), data, dataLen); |
| 195 | } |
| 196 | *outPack = buf; |
| 197 | *outLen = totalLen; |
no outgoing calls
no test coverage detected