-
Notifications
You must be signed in to change notification settings - Fork 5
/
msg_buffer.cpp
181 lines (145 loc) · 3.14 KB
/
msg_buffer.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "msg_buffer.h"
Msg_buffer::Msg_buffer(int sock_fd, int queue_size, int n_byte_of_msg_len)
{
_sock_fd = sock_fd;
_queue_size = queue_size;
_n_byte_of_msg_len = n_byte_of_msg_len;
_read_queue = (char*) malloc(queue_size);
_r_len = queue_size;
_r_head = 0;
_r_tail = 0;
_write_queue= (char*) malloc(queue_size);
_w_len = queue_size;
_w_head = 0;
_w_tail = 0;
pthread_mutex_init(& _w_mutex, NULL);
}
Msg_buffer::~Msg_buffer()
{
pthread_mutex_destroy(& _w_mutex);
}
int Msg_buffer::read_all()
{
int n_read;
bool read_flag = false;
while (1)
{
if (_r_tail >= _r_len)
{
_read_queue = (char*) realloc(_read_queue, _r_len*2);
_r_len *= 2;
}
if ( (n_read = read(_sock_fd, _read_queue+_r_tail, _r_len-_r_tail)) < 0)
{
if (errno == EAGAIN || errno == EWOULDBLOCK)
break;
if (errno == EINTR)
continue;
return -1;
}
else if (n_read == 0)
{
if (!read_flag)
return 0;
else
break;
}
else
{
_r_tail += n_read;
read_flag = true;
}
}
return 1;
}
int Msg_buffer::pop_a_msg(char* msg_buff)
{
/* get message length */
int msg_len;
if ( (msg_len = get_msg_len()) < 0)
return -1;
/* copy the message to msg_buff */
int n_byte_rest = _r_tail - (_r_head+_n_byte_of_msg_len);
if (msg_len > n_byte_rest)
return -1;
memmove(msg_buff, _read_queue+_r_head+_n_byte_of_msg_len, msg_len);
_r_head += _n_byte_of_msg_len + msg_len;
reset_queue(_read_queue, _r_head, _r_tail);
return msg_len;
}
int Msg_buffer::push_a_msg(char* msg_buff, int msg_len)
{
pthread_mutex_lock(& _w_mutex);
if (msg_len < 0)
{
pthread_mutex_unlock(& _w_mutex);
return -1;
}
if (_w_tail+_n_byte_of_msg_len+msg_len > _w_len)
{
_write_queue = (char*) realloc(_write_queue, _w_len*2);
_w_len *= 2;
}
/* set message length */
set_msg_len(msg_len);
/* copy message to write queue */
memmove(_write_queue+_w_tail+_n_byte_of_msg_len, msg_buff, msg_len);
_w_tail += _n_byte_of_msg_len + msg_len;
pthread_mutex_unlock(& _w_mutex);
return 0;
}
int Msg_buffer::write_all()
{
pthread_mutex_lock(& _w_mutex);
int n_write;
while (_w_tail-_w_head > 0)
{
n_write = write(_sock_fd, _write_queue+_w_head, _w_tail-_w_head);
if (n_write < 0)
{
if (errno == EAGAIN || errno == EWOULDBLOCK)
{
reset_queue(_write_queue, _w_head, _w_tail);
pthread_mutex_unlock(& _w_mutex);
return 0;
}
if (errno == EINTR)
continue;
pthread_mutex_unlock(& _w_mutex);
return -1;
}
else
{
_w_head += n_write;
}
}
_w_head = _w_tail = 0;
pthread_mutex_unlock(& _w_mutex);
return 1;
}
/*
* private
*/
int Msg_buffer::get_msg_len()
{
/* assume n_byte is equal or less than 4 */
if (_n_byte_of_msg_len > _r_tail-_r_head)
return -1;
int msg_len = 0;
char* p = (char*) & msg_len;
for (int i = 0; i < _n_byte_of_msg_len; ++ i)
p[i] = *(_read_queue+_r_head+i);
return msg_len;
}
void Msg_buffer::set_msg_len(int msg_len)
{
char* p = (char*) & msg_len;
for (int i = 0; i < _n_byte_of_msg_len; ++ i)
*(_write_queue+_w_tail+i) = p[i];
}
void Msg_buffer::reset_queue(char* q, int & head, int & tail)
{
memmove(q, q+head, tail-head);
tail = tail - head;
head = 0;
}