-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
protorpc.c
65 lines (60 loc) · 1.88 KB
/
protorpc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "protorpc.h"
#include <string.h> // import memcpy
int protorpc_pack(const protorpc_message* msg, void* buf, int len) {
if (!msg || !buf || !len) return -1;
const protorpc_head* head = &(msg->head);
unsigned int packlen = protorpc_package_length(head);
// Check is buffer enough
if (len < packlen) {
return -2;
}
unsigned char* p = (unsigned char*)buf;
*p++ = head->protocol[0];
*p++ = head->protocol[1];
*p++ = head->protocol[2];
*p++ = head->protocol[3];
*p++ = head->version;
*p++ = head->flags;
*p++ = head->reserved[0];
*p++ = head->reserved[1];
// hton length
unsigned int length = head->length;
*p++ = (length >> 24) & 0xFF;
*p++ = (length >> 16) & 0xFF;
*p++ = (length >> 8) & 0xFF;
*p++ = length & 0xFF;
// memcpy body
if (msg->body && head->length) {
memcpy(p, msg->body, head->length);
}
return packlen;
}
int protorpc_unpack(protorpc_message* msg, const void* buf, int len) {
if (!msg || !buf || !len) return -1;
if (len < PROTORPC_HEAD_LENGTH) return -2;
protorpc_head* head = &(msg->head);
const unsigned char* p = (const unsigned char*)buf;
head->protocol[0] = *p++;
head->protocol[1] = *p++;
head->protocol[2] = *p++;
head->protocol[3] = *p++;
head->version = *p++;
head->flags = *p++;
head->reserved[0] = *p++;
head->reserved[1] = *p++;
// ntoh length
head->length = ((unsigned int)*p++) << 24;
head->length |= ((unsigned int)*p++) << 16;
head->length |= ((unsigned int)*p++) << 8;
head->length |= *p++;
// Check is buffer enough
unsigned int packlen = protorpc_package_length(head);
if (len < packlen) {
return -3;
}
// NOTE: just shadow copy
if (len > PROTORPC_HEAD_LENGTH) {
msg->body = (const char*)buf + PROTORPC_HEAD_LENGTH;
}
return packlen;
}