-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib_tcp_server.c
267 lines (232 loc) · 8.68 KB
/
lib_tcp_server.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
264
265
266
267
#include <stdlib.h>
#include <netdb.h>
#include <fcntl.h>
#include "buffer.h"
#include "lib_gutil.h"
#include "channel.h"
socket_acceptor_struct* socket_acceptor_init(int port) {
int listenfd;
int status;
int yes = 1;
char port_str[6];
struct addrinfo hints, * res, * rp;
socket_acceptor_struct* acceptor = malloc(sizeof(socket_acceptor_struct));
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
snprintf(port_str, sizeof(port_str), "%d", port);
if ((status = getaddrinfo(NULL, port_str, &hints, &res) != 0)) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
exit(EXIT_FAILURE);
}
for (rp = res;rp != NULL;rp = rp->ai_next) {
if ((listenfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol)) == -1) {
continue;
}
fcntl(listenfd, F_SETFL, O_NONBLOCK);
if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
perror("setsockopt");
exit(1);
}
if (bind(listenfd, rp->ai_addr, rp->ai_addrlen) == 0) {
break;
}
//fprintf(stderr,"%s\n",strerror(errno));
close(listenfd);
}
freeaddrinfo(res);
if (rp == NULL) {
perror("failed to bind");
exit(EXIT_FAILURE);
}
if (listen(listenfd, 10) != 0) {
perror("failed to listen");
exit(EXIT_FAILURE);
}
acceptor->listen_fd = listenfd;
acceptor->listen_port = port;
return acceptor;
}
/******************** tcp connection **************************/
int handle_connection_closed(tcp_connection_struc* tcp_connection)
{
event_loop_struc* event_loop = tcp_connection->event_loop;
channel_struc* channel = tcp_connection->channel;
event_loop_remove_channel_event(event_loop, channel->fd, channel);
if (tcp_connection->on_connection_closed != NULL) {
tcp_connection->on_connection_closed(tcp_connection);
}
return 0;
}
int handle_read(void* data)
{
tcp_connection_struc* tcp_connection = (tcp_connection_struc*)data;
buffer_struc* input_buffer = tcp_connection->input_buffer;
channel_struc* channel = tcp_connection->channel;
if (buffer_socket_read(input_buffer, channel->fd) > 0) {
if (tcp_connection->on_message != NULL) {
tcp_connection->on_message(input_buffer, tcp_connection);
}
}
else {
handle_connection_closed(tcp_connection);
}
return 0;
}
int handle_write(void* data)
{
tcp_connection_struc* tcp_connection = (tcp_connection_struc*)data;
event_loop_struc* event_loop = tcp_connection->event_loop;
buffer_struc* output_buffer = tcp_connection->out_buffer;
channel_struc* channel = tcp_connection->channel;
ssize_t n_writed = write(channel->fd, output_buffer->data + output_buffer->read_index,
buffer_readable_size(output_buffer));
if (n_writed > 0) {
output_buffer->read_index += n_writed;
if (buffer_readable_size(output_buffer) == 0) {
channel_write_event_disable(channel);
}
if (tcp_connection->on_write_completed != NULL) {
tcp_connection->on_write_completed(tcp_connection);
}
}
else {
net_msgx("tcp connection write %s", tcp_connection->name);
}
return 0;
}
tcp_connection_struc* tcp_connection_init(int connected_fd, event_loop_struc* event_loop,
connection_completed_callback_f on_connection_completed,
connection_closed_callback_f on_connection_closed,
message_callback_f on_message,
write_completed_callback_f on_write_completed)
{
tcp_connection_struc* tcp_connection = malloc(sizeof(tcp_connection_struc));
tcp_connection->on_write_completed = on_write_completed;
tcp_connection->on_message = on_message;
tcp_connection->on_connection_completed = on_connection_completed;
tcp_connection->on_connection_closed = on_connection_closed;
tcp_connection->event_loop = event_loop;
tcp_connection->input_buffer = buffer_new();
tcp_connection->out_buffer = buffer_new();
char buf[16] = { 0 };
sprintf(buf, "connection-%d%c", connected_fd,'\0');
tcp_connection->name = buf;
channel_struc* channel = channel_init(connected_fd, EVENT_READ,
handle_read, handle_write, tcp_connection);
tcp_connection->channel = channel;
if (tcp_connection->on_connection_completed != NULL) {
tcp_connection->on_connection_completed(tcp_connection);
}
event_loop_add_channel_event(tcp_connection->event_loop, connected_fd,
tcp_connection->channel);
return tcp_connection;
}
int tcp_connection_send_data(tcp_connection_struc* tcp_connection, void* data, int size)
{
ssize_t n_writed = 0;
ssize_t n_left = size;
int fault = 0;
channel_struc* channel = tcp_connection->channel;
buffer_struc* output_buffer = tcp_connection->out_buffer;
if (!channel_write_event_is_enabled(channel)
&& buffer_readable_size(output_buffer) == 0) {
n_writed = write(channel->fd, data, size);
if (n_writed >= 0) {
n_left = n_left - n_writed;
}
else {
n_writed = 0;
if (errno != EWOULDBLOCK) {
if (errno == EPIPE || errno == ECONNRESET) {
fault = 1;
}
}
}
}
if (!fault && n_left > 0) {
buffer_append(output_buffer, data + n_writed, n_left);
if (!channel_write_event_is_enabled(channel)) {
channel_write_event_enable(channel);
}
}
return n_writed;
}
int tcp_connection_send_buffer(tcp_connection_struc* tcp_connection, buffer_struc* buffer)
{
int size = buffer_readable_size(buffer);
int result = tcp_connection_send_data(tcp_connection, buffer->data + buffer->read_index, size);
buffer->read_index += size;
return result;
}
void tcp_connection_shutdown(tcp_connection_struc* tcp_connection)
{
if (shutdown(tcp_connection->channel->fd, SHUT_WR) < 0) {
net_msgx("failed to shutdown tcp connect on socket=%d", tcp_connection->channel->fd);
}
}
/************************* TCP server *************************************/
tcp_server_struc* tcp_server_init(event_loop_struc* event_loop, socket_acceptor_struct* acceptor,
connection_completed_callback_f connection_callback,
message_callback_f message_callback,
write_completed_callback_f write_completed_callback,
connection_closed_callback_f connection_closed_callback,
int thread_num)
{
tcp_server_struc* tcp_server = malloc(sizeof(tcp_server_struc));
tcp_server->event_loop = event_loop;
tcp_server->acceptor = acceptor;
tcp_server->connection_completed_callback = connection_callback;
tcp_server->message_callback = message_callback;
tcp_server->write_completed_callback = write_completed_callback;
tcp_server->connection_closed_callback = connection_closed_callback;
tcp_server->thread_num = thread_num;
tcp_server->thread_pool = thread_pool_init(event_loop, thread_num);
tcp_server->data = NULL;
return tcp_server;
}
void set_nonblocking(int fd)
{
fcntl(fd, F_SETFL, O_NONBLOCK);
}
int handle_connection_established(void* data)
{
tcp_server_struc* tcp_server = (tcp_server_struc*)data;
socket_acceptor_struct* acceptor = tcp_server->acceptor;
int listen_fd = acceptor->listen_fd;
struct sockaddr_storage cli_addr;
socklen_t addr_len = sizeof(cli_addr);
int connected_fd = accept(listen_fd, (struct sockaddr*)&cli_addr, &addr_len);
set_nonblocking(connected_fd);
net_msgx("new connection established on socket %d", connected_fd);
event_loop_struc* event_loop = thread_pool_get_loop(tcp_server->thread_pool);
//create a new tcp connection
tcp_connection_struc* tcp_connection = tcp_connection_init(connected_fd,
event_loop, tcp_server->connection_completed_callback,
tcp_server->connection_closed_callback,
tcp_server->message_callback,
tcp_server->write_completed_callback);
if(tcp_server->data != NULL){
tcp_connection->data = tcp_server->data;
}
return 0;
}
void tcp_server_start(tcp_server_struc* tcp_server)
{
socket_acceptor_struct* acceptor = tcp_server->acceptor;
event_loop_struc* event_loop = tcp_server->event_loop;
//start thread pool
thread_pool_start(tcp_server->thread_pool);
// main thread with acceptor
channel_struc* channel = channel_init(acceptor->listen_fd, EVENT_READ,
handle_connection_established, NULL, tcp_server);
event_loop_add_channel_event(event_loop, channel->fd, channel);
return;
}
void tcp_server_set_data(tcp_server_struc* tcp_server, void* data)
{
if (data != NULL) {
tcp_server->data = data;
}
}