-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathecho_server_stackfull.c
280 lines (225 loc) · 7.12 KB
/
echo_server_stackfull.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
268
269
270
271
272
273
274
275
276
277
278
279
280
// iocp_echo_server.c
// -----------------------------------------------------------------------------
#include "universal_fiber.h"
#include <stdio.h>
#include <stdlib.h>
#ifdef _WIN32
LPFN_ACCEPTEX _AcceptEx = NULL;
LPFN_DISCONNECTEX DisconnectEx = NULL;
#define AcceptEx _AcceptEx
#endif
// -----------------------------------------------------------------------------
enum // configuration
{
MAX_BUF = 1024,
SERVER_ADDRESS = INADDR_ANY,
SERVICE_PORT = 50001
};
// prototypes - main functions
static void init(void);
// prototypes - helper functions
static void bind_listening_socket(SOCKET);
static SOCKET create_accepting_socket(void);
static HANDLE create_io_completion_port(void);
static SOCKET create_listening_socket(HANDLE cpl_port);
static BOOL get_completion_status(DWORD*, ULONG_PTR*, OVERLAPPED**);
static void init_winsock(void);
static void prepare_endpoint(struct sockaddr_in*, u_long, u_short);
static void start_listening(SOCKET);
// -----------------------------------------------------------------------------
static void bind_listening_socket(SOCKET listener)
{
struct sockaddr_in sin;
prepare_endpoint(&sin, SERVER_ADDRESS, SERVICE_PORT);
int v = 1;
setsockopt(SOCKET_get_fd(listener), SOL_SOCKET, SO_REUSEADDR, (char*)&v, sizeof v);
if (bind(SOCKET_get_fd(listener), (struct sockaddr*) &sin, sizeof(sin)) == SOCKET_ERROR)
{
printf("* error in bind!\n");
exit(1);
}
}
// -----------------------------------------------------------------------------
static SOCKET create_accepting_socket(void)
{
SOCKET acceptor = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, 0, 0, WSA_FLAG_OVERLAPPED);
if (acceptor == INVALID_SOCKET)
{
printf("* error creating accept socket!\n");
exit(1);
}
return acceptor;
}
// -----------------------------------------------------------------------------
static HANDLE create_io_completion_port(void)
{
HANDLE cpl_port = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
if (!cpl_port)
{
int err = WSAGetLastError();
printf("* error %d in line %d CreateIoCompletionPort\n", err, __LINE__);
exit(1);
}
return cpl_port;
}
// -----------------------------------------------------------------------------
static SOCKET create_listening_socket(HANDLE cpl_port)
{
SOCKET listener = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, 0 , 0, WSA_FLAG_OVERLAPPED);
if (listener == INVALID_SOCKET)
{
printf("* error creating listening socket!\n");
exit(1);
}
if (CreateIoCompletionPort((HANDLE)listener, cpl_port, 0, 0) != cpl_port)
{
int err = WSAGetLastError();
printf("* error %d in listener\n", err);
exit(1);
}
return listener;
}
static void echo_sever_client_session(void* param)
{
SOCKET client_sock = (SOCKET) param;
char buf1[1024];
WSABUF wsabuf1 = {
.len = 1024,
.buf = buf1
};
FiberOVERLAPPED ov;
memset(&ov, 0, sizeof(ov));
DWORD recv_bytes = 0, flag = 0;
if (WSARecv(client_sock, &wsabuf1, 1, &recv_bytes, &flag, &ov.ov, NULL) == SOCKET_ERROR)
{
int err = WSAGetLastError();
if (err != WSA_IO_PENDING)
{
printf("*error %d in WSARecv\n", err);
closesocket(client_sock);
return;
}
}
recv_bytes = get_overlapped_result(&ov);
printf("* read operation completed\n");
char buf2[1024];
int content_length_line_len = snprintf(buf2, 80, "Content-Length: %d\r\n\r\n", ov.byte_transfered);
WSABUF wsabuf[3] = {
{ .len = 17, .buf = (char*) "HTTP/1.1 200 OK\r\n" },
{ .len = (size_t) content_length_line_len, .buf = buf2 },
{ ov.byte_transfered, buf1 }
};
memset(&ov, 0, sizeof(ov));
if (WSASend(client_sock, wsabuf, 3, NULL, 0, &ov.ov, NULL) == SOCKET_ERROR)
{
int err = WSAGetLastError();
if (err != WSA_IO_PENDING)
{
printf("*error %d in WSASend\n", err);
closesocket(client_sock);
return;
}
}
get_overlapped_result(&ov);
printf("* write operation completed\n");
closesocket(client_sock);
printf("client_coroutine exit\n");
return;
}
struct accept_coro_param_pack
{
SOCKET listener;
HANDLE iocp_handle;
};
static void accept_coroutine(void* param)
{
char addr_buf[1024];
SOCKET listener = ((struct accept_coro_param_pack*)param)->listener;
HANDLE iocp_handle = ((struct accept_coro_param_pack*)param)->iocp_handle;
for (;;)
{
SOCKET client_socket = WSASocket(AF_INET, SOCK_STREAM, 0, 0, 0, WSA_FLAG_OVERLAPPED);
FiberOVERLAPPED ov = { {0}, 0 };
DWORD ignore = 0;
BOOL result = AcceptEx(listener, client_socket, addr_buf, 0, sizeof (SOCKADDR_IN)+16, sizeof (SOCKADDR_IN)+16, &ignore, &ov.ov);
ov.last_error = WSAGetLastError();
if (!(!result && ov.last_error != WSA_IO_PENDING))
{
get_overlapped_result(&ov);
if (ov.last_error)
{
closesocket(client_socket);
continue;
}
HANDLE read_port = CreateIoCompletionPort((HANDLE)(client_socket), iocp_handle, 0, 0);
// 开新协程处理连接.
create_detached_coroutine(echo_sever_client_session, (LPVOID)client_socket);
}
else
{
closesocket(client_socket);
}
}
}
// -----------------------------------------------------------------------------
static void init_winsock(void)
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2), &wsaData))
{
printf("* error in WSAStartup!\n");
exit(1);
}
#ifdef _WIN32
GUID disconnectex = WSAID_DISCONNECTEX;
GUID acceptex = WSAID_ACCEPTEX;
DWORD BytesReturned;
SOCKET dummySocket = WSASocket(AF_INET6, SOCK_STREAM, IPPROTO_TCP, 0, 0, WSA_FLAG_OVERLAPPED);
WSAIoctl(dummySocket, SIO_GET_EXTENSION_FUNCTION_POINTER,
&disconnectex, sizeof(GUID), &DisconnectEx, sizeof(DisconnectEx),
&BytesReturned, 0, 0);
WSAIoctl(dummySocket, SIO_GET_EXTENSION_FUNCTION_POINTER,
&acceptex, sizeof(GUID), &_AcceptEx, sizeof(_AcceptEx),
&BytesReturned, 0, 0);
closesocket(dummySocket);
#endif
}
// -----------------------------------------------------------------------------
static void prepare_endpoint(struct sockaddr_in* sin, u_long address,
u_short port)
{
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = htonl(address);
sin->sin_port = htons(port);
}
static void start_listening(SOCKET listener)
{
if (listen(SOCKET_get_fd(listener), 100) == SOCKET_ERROR)
{
printf("* error in listen!\n");
exit(1);
}
printf("* started listening for connection requests...\n");
}
// -----------------------------------------------------------------------------
int main(void)
{
init_winsock();
HANDLE cpl_port = create_io_completion_port();
SOCKET listener = create_listening_socket(cpl_port);
bind_listening_socket(listener);
start_listening(listener);
#ifdef _WIN32
ConvertThreadToFiber(0);
#endif
struct accept_coro_param_pack param;
param.listener = listener;
param.iocp_handle = cpl_port;
// 并发投递 64 个 accept 操作。加快 accept 速度.
for (int i=0; i < 64; i++)
create_detached_coroutine(accept_coroutine, ¶m);
run_event_loop(cpl_port);
return 0;
}
// -----------------------------------------------------------------------------
// the end