forked from sogou/workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.cc
More file actions
129 lines (103 loc) · 2.44 KB
/
message.cc
File metadata and controls
129 lines (103 loc) · 2.44 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <arpa/inet.h>
#include <utility>
#include "message.h"
namespace protocol
{
int TutorialMessage::encode(struct iovec vectors[], int max/*max==8192*/)
{
uint32_t n = htonl(this->body_size);
memcpy(this->head, &n, 4);
vectors[0].iov_base = this->head;
vectors[0].iov_len = 4;
vectors[1].iov_base = this->body;
vectors[1].iov_len = this->body_size;
return 2; /* return the number of vectors used, no more then max. */
}
int TutorialMessage::append(const void *buf, size_t size)
{
if (this->head_received < 4)
{
size_t head_left;
void *p;
p = &this->head[head_received];
head_left = 4 - this->head_received;
if (size < 4 - this->head_received)
{
memcpy(p, buf, size);
this->head_received += size;
return 0;
}
this->head_received += head_left;
memcpy(p, buf, head_left);
size -= head_left;
buf = (const char *)buf + head_left;
p = this->head;
this->body_size = ntohl(*(uint32_t *)p);
if (this->body_size > this->size_limit)
{
errno = EMSGSIZE;
return -1;
}
this->body = (char *)malloc(this->body_size);
if (!this->body)
return -1;
this->body_received = 0;
}
size_t body_left = this->body_size - this->body_received;
if (size > body_left)
{
errno = EBADMSG;
return -1;
}
memcpy(this->body + this->body_received, buf, size);
this->body_received += size;
if (size < body_left)
return 0;
return 1;
}
int TutorialMessage::set_message_body(const void *body, size_t size)
{
void *p = malloc(size);
if (!p)
return -1;
memcpy(p, body, size);
free(this->body);
this->body = (char *)p;
this->body_size = size;
this->head_received = 4;
this->body_received = size;
return 0;
}
TutorialMessage::TutorialMessage(TutorialMessage&& msg) :
ProtocolMessage(std::move(msg))
{
memcpy(this->head, msg.head, 4);
this->head_received = msg.head_received;
this->body = msg.body;
this->body_received = msg.body_received;
this->body_size = msg.body_size;
msg.head_received = 0;
msg.body = NULL;
msg.body_size = 0;
}
TutorialMessage& TutorialMessage::operator = (TutorialMessage&& msg)
{
if (&msg != this)
{
*(ProtocolMessage *)this = std::move(msg);
memcpy(this->head, msg.head, 4);
this->head_received = msg.head_received;
this->body = msg.body;
this->body_received = msg.body_received;
this->body_size = msg.body_size;
msg.head_received = 0;
msg.body = NULL;
msg.body_size = 0;
}
return *this;
}
}