-
Notifications
You must be signed in to change notification settings - Fork 1
/
pd_tcp.c
executable file
·264 lines (252 loc) · 6.38 KB
/
pd_tcp.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <string.h>
#include <errno.h>
#include "pd_tcp.h"
#include "pd_ringbuffer.h"
#include "pd_socket.h"
#include "pd_log.h"
#include "pd_ioc.h"
#include "pd_transport.h"
struct PdTcpIOComponent
{
struct PdIOComponent base_ioc;
struct PdRingBuffer recv_buf;
struct PdRingBuffer post_buf;
};
static int pd_tcp_read_stream(struct PdTcpIOComponent *tcp_ioc)
{
int ret = 0;
while (1)
{
char *buffer = pd_ringbuffer_get_producer_buffer(&tcp_ioc->recv_buf);
int length = pd_ringbuffer_get_producer_length(&tcp_ioc->recv_buf);
if (NULL == buffer
|| 0 >= length)
{
break;
}
int readlen = read(pd_socket_get_fd(tcp_ioc->base_ioc.sock), buffer, length);
if (-1 == readlen)
{
if (EINTR == errno)
{
continue;
}
else
{
break;
}
}
if (0 < readlen)
{
pd_ringbuffer_produce(&tcp_ioc->recv_buf, readlen);
PD_LOG(DEBUG, "read from peer=[%s], readlen=%d producer_length=%d",
pd_socket_str_peer(tcp_ioc->base_ioc.sock),
readlen,
pd_ringbuffer_get_producer_length(&tcp_ioc->recv_buf));
}
if (0 == readlen)
{
PD_LOG(DEBUG, "peer close tcp, peer=[%s]", pd_socket_str_peer(tcp_ioc->base_ioc.sock));
pd_tcp_on_error((struct PdIOComponent*)tcp_ioc);
ret = PD_ERR_IOC_DESTROY;
break;
}
}
return ret;
}
static int pd_tcp_write_stream(struct PdTcpIOComponent *tcp_ioc)
{
int ret = 0;
while (1)
{
char *buffer = pd_ringbuffer_get_consumer_buffer(&tcp_ioc->post_buf);
int length = pd_ringbuffer_get_consumer_length(&tcp_ioc->post_buf);
if (NULL == buffer
|| 0 >= length)
{
pd_transport_set_ioc(tcp_ioc->base_ioc.ts, (struct PdIOComponent*)tcp_ioc, 1, 0);
break;
}
int writelen = write(pd_socket_get_fd(tcp_ioc->base_ioc.sock), buffer, length);
if (-1 == writelen)
{
if (EINTR == errno)
{
continue;
}
else if (EPIPE == errno)
{
PD_LOG(DEBUG, "peer close tcp, peer=[%s]", pd_socket_str_peer(tcp_ioc->base_ioc.sock));
pd_tcp_on_error((struct PdIOComponent*)tcp_ioc);
ret = PD_ERR_IOC_DESTROY;
break;
}
else
{
break;
}
}
if (0 < writelen)
{
pd_ringbuffer_consume(&tcp_ioc->post_buf, writelen);
PD_LOG(DEBUG, "write to peer=[%s], writelen=%d consumer_length=%d",
pd_socket_str_peer(tcp_ioc->base_ioc.sock),
writelen,
pd_ringbuffer_get_producer_length(&tcp_ioc->post_buf));
}
}
return ret;
}
static void pd_tcp_handle_packet(struct PdTcpIOComponent *tcp_ioc)
{
while (1)
{
char *buffer = pd_ringbuffer_get_consumer_buffer(&tcp_ioc->recv_buf);
int length = pd_ringbuffer_get_consumer_length(&tcp_ioc->recv_buf);
if (NULL == buffer
|| 0 >= length)
{
break;
}
int handlelen = pd_transport_handle_packet(tcp_ioc->base_ioc.ts, tcp_ioc, buffer, length);
if (0 >= handlelen)
{
break;
}
pd_ringbuffer_consume(&tcp_ioc->recv_buf, handlelen);
}
}
int pd_tcp_on_readable(struct PdIOComponent *ioc)
{
int ret = 0;
if (NULL == ioc)
{
PD_LOG(WARN, "ioc null pointer");
}
else if (NULL == ioc->sock)
{
PD_LOG(WARN, "sock null pointer");
}
else
{
ret = pd_tcp_read_stream((struct PdTcpIOComponent*)ioc);
if (0 == ret)
{
pd_tcp_handle_packet((struct PdTcpIOComponent*)ioc);
}
}
return ret;
}
int pd_tcp_on_writeable(struct PdIOComponent *ioc)
{
int ret = 0;
if (NULL == ioc)
{
PD_LOG(WARN, "ioc null pointer");
}
else if (NULL == ioc->sock)
{
PD_LOG(WARN, "sock null pointer");
}
else
{
ret = pd_tcp_write_stream((struct PdTcpIOComponent*)ioc);
}
return ret;
}
void pd_tcp_on_error(struct PdIOComponent *ioc)
{
if (NULL == ioc)
{
PD_LOG(WARN, "ioc null pointer");
}
else if (NULL == ioc->sock)
{
PD_LOG(WARN, "sock null pointer");
}
else if (NULL == ioc->ts)
{
PD_LOG(WARN, "ts null pointer");
}
else if (0 != pd_transport_remove_ioc(ioc->ts, ioc))
{
PD_LOG(WARN, "pd_transport_remove_ioc fail, ioc=%p", ioc);
}
else
{
PD_LOG(DEBUG, "tcp fd error, will close, ioc=%p sock=%p", ioc, ioc->sock);
pd_socket_destroy(ioc->sock);
pd_tcp_ioc_free(ioc);
}
}
struct PdIOComponent *pd_tcp_ioc_alloc()
{
struct PdTcpIOComponent *tcp_ioc = (struct PdTcpIOComponent*)malloc(sizeof(struct PdTcpIOComponent));
if (NULL != tcp_ioc)
{
memset((void*)tcp_ioc, 0, sizeof(struct PdTcpIOComponent));
if (0 != pd_ringbuffer_init(&tcp_ioc->recv_buf)
|| 0 != pd_ringbuffer_init(&tcp_ioc->post_buf))
{
pd_tcp_ioc_free((struct PdIOComponent*)tcp_ioc);
tcp_ioc = NULL;
}
}
return (struct PdIOComponent*)tcp_ioc;
}
void pd_tcp_ioc_free(struct PdIOComponent *ioc)
{
struct PdTcpIOComponent *tcp_ioc = (struct PdTcpIOComponent*)ioc;
if (NULL != tcp_ioc)
{
pd_ringbuffer_destroy(&tcp_ioc->recv_buf);
pd_ringbuffer_destroy(&tcp_ioc->post_buf);
free(tcp_ioc);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
int pd_tcp_post_packet(struct PdTcpIOComponent *tcp_ioc, const char *buffer, const int length)
{
int ret = 0;
if (NULL == tcp_ioc)
{
PD_LOG(WARN, "ioc null pointer");
ret = -1;
}
else if (NULL == buffer
|| 0 >= length)
{
PD_LOG(WARN, "invalid param, buffer=%p length=%d", buffer, length);
ret = -1;
}
else if (pd_ringbuffer_get_free(&tcp_ioc->post_buf) < length)
{
PD_LOG(WARN, "buffer not enough, length=%d free=%d", length, pd_ringbuffer_get_free(&tcp_ioc->post_buf));
ret = -1;
}
else
{
int64_t pos = 0;
while (pos < length)
{
char *post_buffer = pd_ringbuffer_get_producer_buffer(&tcp_ioc->post_buf);
int post_length = pd_ringbuffer_get_producer_length(&tcp_ioc->post_buf);
int copy_length = (post_length > (length - pos)) ? (length - pos) : post_length;
if (NULL == post_buffer
|| 0 >= post_length)
{
PD_LOG(WARN, "buffer null, post_buffer=%p post_length=%d", post_buffer, post_length);
ret = -1;
break;
}
memcpy(post_buffer, buffer + pos, copy_length);
pd_ringbuffer_produce(&tcp_ioc->post_buf, copy_length);
pos += copy_length;
}
if (0 == ret)
{
pd_transport_set_ioc(tcp_ioc->base_ioc.ts, (struct PdIOComponent*)tcp_ioc, 1, 1);
}
}
return ret;
}