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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
| #include <winsock2.h> #include <iostream> #include <assert.h> #include <vector> #include <process.h> #include "iocp.h"
using namespace std;
const u_short kPort = 10001; const std::string kSYN = "(SYN) hello server, I'm client. Can you hear me?"; const std::string kSYN_ACK = "(SYN+ACK) hello client, I'm server. I can hear you, can you hear me?"; const std::string kACK = "(ACK) hello server, I'm client. I can hear you!";
#pragma comment(lib, "Ws2_32.lib")
HANDLE g_IOCP = INVALID_HANDLE_VALUE; HANDLE g_exit = NULL;
int g_work_thread_num = 0; HANDLE *g_work_threads = NULL;
IOCP::PER_SOCKET_CONTEXT *g_listen_ctx = NULL;
CRITICAL_SECTION g_cs_socket_ctx_array; std::vector<IOCP::PER_SOCKET_CONTEXT*> g_socket_ctx_array;
LPFN_ACCEPTEX g_AcceptExFn = NULL; LPFN_GETACCEPTEXSOCKADDRS g_AcceptExSockAddrsFn = NULL;
void AddSocketContext(IOCP::PER_SOCKET_CONTEXT *socket_ctx) { EnterCriticalSection(&g_cs_socket_ctx_array); g_socket_ctx_array.push_back(socket_ctx); LeaveCriticalSection(&g_cs_socket_ctx_array); }
void RemoveSocketContext(IOCP::PER_SOCKET_CONTEXT *socket_ctx) { EnterCriticalSection(&g_cs_socket_ctx_array); for (std::vector<IOCP::PER_SOCKET_CONTEXT*>::iterator it = g_socket_ctx_array.begin(); it != g_socket_ctx_array.end(); it++) { if (*it == socket_ctx) { delete *it; g_socket_ctx_array.erase(it); break; } } LeaveCriticalSection(&g_cs_socket_ctx_array); }
void ClearSocketContextArray() { EnterCriticalSection(&g_cs_socket_ctx_array); for (std::vector<IOCP::PER_SOCKET_CONTEXT*>::iterator it = g_socket_ctx_array.begin(); it != g_socket_ctx_array.end(); it++) { closesocket((*it)->socket); delete *it; } g_socket_ctx_array.clear(); LeaveCriticalSection(&g_cs_socket_ctx_array); }
bool PostAccept(IOCP::PER_IO_CONTEXT* io_ctx) { if (io_ctx == NULL) return false;
io_ctx->operation_type = IOCP::ACCEPT_POSTED; io_ctx->ResetBuffer(); io_ctx->socket = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
if (io_ctx->socket == INVALID_SOCKET) { printf("WSASocket failed with code: %d\n", WSAGetLastError()); return false; }
DWORD bytes = 0; if (g_AcceptExFn(g_listen_ctx->socket, io_ctx->socket, io_ctx->wsa_buffer.buf, 0, sizeof(SOCKADDR_IN) + 16, sizeof(SOCKADDR_IN) + 16, &bytes, &io_ctx->overlapped) == FALSE) { int gle = WSAGetLastError(); if (gle != WSA_IO_PENDING) { printf("AcceptEx failed with code: %d\n", gle); return false; } }
return true; }
bool PostRecv(IOCP::PER_IO_CONTEXT* io_ctx) { if (io_ctx == NULL) return false;
io_ctx->operation_type = IOCP::RECV_POSTED; io_ctx->ResetBuffer();
DWORD recv_bytes = 0; DWORD flags = 0; int ret = WSARecv(io_ctx->socket, &io_ctx->wsa_buffer, 1, &recv_bytes, &flags, &io_ctx->overlapped, NULL); if (ret == SOCKET_ERROR && WSAGetLastError() != WSA_IO_PENDING) { return false; }
return true; }
bool PostSend(IOCP::PER_IO_CONTEXT* io_ctx, const char* msg, int msg_len) { if (io_ctx == NULL) return false;
io_ctx->operation_type = IOCP::SEND_POSTED; memcpy(io_ctx->wsa_buffer.buf, msg, msg_len); io_ctx->wsa_buffer.len = msg_len;
DWORD sent_bytes = 0; int ret = WSASend(io_ctx->socket, &io_ctx->wsa_buffer, 1, &sent_bytes, 0, &io_ctx->overlapped, NULL); if (ret == SOCKET_ERROR && WSAGetLastError() != WSA_IO_PENDING) { return false; }
return true; }
bool DoAccept(IOCP::PER_SOCKET_CONTEXT *socket_ctx, IOCP::PER_IO_CONTEXT *io_ctx) { SOCKADDR_IN* ClientAddr = NULL; SOCKADDR_IN* LocalAddr = NULL; int remoteLen = sizeof(SOCKADDR_IN); int localLen = sizeof(SOCKADDR_IN);
g_AcceptExSockAddrsFn(io_ctx->wsa_buffer.buf, io_ctx->wsa_buffer.len - ((sizeof(SOCKADDR_IN) + 16) * 2), sizeof(SOCKADDR_IN) + 16, sizeof(SOCKADDR_IN) + 16, (LPSOCKADDR*)&LocalAddr, &localLen, (LPSOCKADDR*)&ClientAddr, &remoteLen);
printf("* new connection(%s:%d): %s\n", inet_ntoa(ClientAddr->sin_addr), ntohs(ClientAddr->sin_port), io_ctx->wsa_buffer.buf);
IOCP::PER_SOCKET_CONTEXT *new_socket_ctx = new IOCP::PER_SOCKET_CONTEXT(); new_socket_ctx->socket = io_ctx->socket;
if (!IOCP::AssociateDeviceWithCompletionPort(g_IOCP, (HANDLE)new_socket_ctx->socket, (DWORD)new_socket_ctx)) { printf("AssociateDeviceWithCompletionPort failed\n"); delete new_socket_ctx; new_socket_ctx = NULL; return false; }
AddSocketContext(new_socket_ctx);
IOCP::PER_IO_CONTEXT *new_io_ctx = new_socket_ctx->GetNewIoContext(); new_io_ctx->socket = new_socket_ctx->socket; if (!PostRecv(new_io_ctx)) { printf("PostRecv failed\n"); return false; }
if (!PostAccept(io_ctx)) { printf("PostAccept failed\n"); return false; }
return true; }
bool DoRecv(IOCP::PER_SOCKET_CONTEXT *socket_ctx, IOCP::PER_IO_CONTEXT *io_ctx) { printf("recv: %s\n", io_ctx->wsa_buffer.buf);
if (strcmp(io_ctx->wsa_buffer.buf, kSYN.c_str()) == 0) { IOCP::PER_IO_CONTEXT * new_io_ctx = socket_ctx->GetNewIoContext(); new_io_ctx->socket = socket_ctx->socket;
if (!PostSend(new_io_ctx, kSYN_ACK.c_str(), kSYN_ACK.length())) { printf("PostSend failed\n"); return false; } }
if (!PostRecv(io_ctx)) { printf("PostRecv failed\n"); return false; }
return true; }
bool DoSend(IOCP::PER_SOCKET_CONTEXT *socket_ctx, IOCP::PER_IO_CONTEXT *io_ctx) { printf("send: %s\n", io_ctx->wsa_buffer.buf); return true; }
unsigned int __stdcall WorkThreadProc(void *arg) { DWORD transferred_bytes = 0; IOCP::PER_SOCKET_CONTEXT *socket_ctx = NULL; OVERLAPPED *overlapped = NULL; DWORD gle;
while (WaitForSingleObject(g_exit, 0) != WAIT_OBJECT_0) { BOOL ret = GetQueuedCompletionStatus(g_IOCP, &transferred_bytes, (PULONG_PTR)&socket_ctx, &overlapped, INFINITE); gle = GetLastError();
if (socket_ctx == EXIT_CODE) { break; }
if (ret == FALSE) { if (gle == WAIT_TIMEOUT) { continue; } else if (gle == ERROR_NETNAME_DELETED) { printf("client exit\n");
RemoveSocketContext(socket_ctx);
continue; } else { RemoveSocketContext(socket_ctx); break; } } else { IOCP::PER_IO_CONTEXT *io_ctx = CONTAINING_RECORD(overlapped, IOCP::PER_IO_CONTEXT, overlapped);
if ((transferred_bytes == 0) && (io_ctx->operation_type == IOCP::RECV_POSTED || io_ctx->operation_type == IOCP::SEND_POSTED)) { printf("client disconnect\n"); RemoveSocketContext(socket_ctx); continue; }
switch (io_ctx->operation_type) { case IOCP::ACCEPT_POSTED: DoAccept(socket_ctx, io_ctx);
break; case IOCP::RECV_POSTED: DoRecv(socket_ctx, io_ctx);
break; case IOCP::SEND_POSTED: DoSend(socket_ctx, io_ctx);
break; default: assert(false); } } } return 0; }
int main() { WSADATA wsaData; WORD wVersionRequested = MAKEWORD(2, 2); WSAStartup(wVersionRequested, &wsaData);
do { InitializeCriticalSection(&g_cs_socket_ctx_array); g_IOCP = IOCP::CreateNewCompletionPort(); g_exit = CreateEvent(NULL, FALSE, FALSE, NULL);
g_work_thread_num = IOCP::GetNumberOfProcesser() * 2;
g_work_threads = new HANDLE[g_work_thread_num]; for (int i = 0; i < g_work_thread_num; i++) { g_work_threads[i] = (HANDLE)_beginthreadex(NULL, 0, WorkThreadProc, NULL, 0, NULL); }
g_listen_ctx = new IOCP::PER_SOCKET_CONTEXT; g_listen_ctx->socket = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED);
if (!IOCP::AssociateDeviceWithCompletionPort(g_IOCP, (HANDLE)g_listen_ctx->socket, (DWORD)g_listen_ctx)) { printf("AssociateDeviceWithCompletionPort failed with code: %d\n", GetLastError()); break; }
struct sockaddr_in addr = { 0 }; addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(kPort); if (bind(g_listen_ctx->socket, reinterpret_cast<const sockaddr*>(&addr), sizeof(addr)) == SOCKET_ERROR) { printf("bind failed with code: %d\n", WSAGetLastError()); break; }
if (listen(g_listen_ctx->socket, SOMAXCONN) == SOCKET_ERROR) { printf("listen failed with code: %d\n", WSAGetLastError()); break; }
g_AcceptExFn = IOCP::GetAcceptExFnPointer(g_listen_ctx->socket); if (g_AcceptExFn == NULL) { printf("GetAcceptExFnPointer failed\n"); break; }
g_AcceptExSockAddrsFn = IOCP::GetAcceptExSockAddrsFnPointer(g_listen_ctx->socket); if (g_AcceptExSockAddrsFn == NULL) { printf("GetAcceptExSockAddrsFnPointer failed\n"); break; }
int i = 0; for (; i < 10; i++) { IOCP::PER_IO_CONTEXT *io_ctx = g_listen_ctx->GetNewIoContext(); if (PostAccept(io_ctx) == FALSE) { break; } } if(i != 10) break;
} while (FALSE);
printf("\npress any ket to stop server...\n"); getchar();
SetEvent(g_exit); for (int i = 0; i < g_work_thread_num; i++) { PostQueuedCompletionStatus(g_IOCP, 0, (DWORD)EXIT_CODE, NULL); } WaitForMultipleObjects(g_work_thread_num, g_work_threads, TRUE, INFINITE);
ClearSocketContextArray();
printf("\npress any ket to exit...\n"); getchar();
DeleteCriticalSection(&g_cs_socket_ctx_array); WSACleanup(); return 0; }
|