| 371 | } |
| 372 | |
| 373 | void NetworkServer::ConnectionThreadFunction(int socket_idx) |
| 374 | { |
| 375 | //This thread handles client connections |
| 376 | |
| 377 | printf("Network connection thread started on port %hu\n", GetPort()); |
| 378 | while(server_online == true) |
| 379 | { |
| 380 | /*-------------------------------------------------*\ |
| 381 | | Create new socket for client connection | |
| 382 | \*-------------------------------------------------*/ |
| 383 | NetworkClientInfo * client_info = new NetworkClientInfo(); |
| 384 | |
| 385 | /*-------------------------------------------------*\ |
| 386 | | Listen for incoming client connection on the | |
| 387 | | server socket. This call blocks until a | |
| 388 | | connection is established | |
| 389 | \*-------------------------------------------------*/ |
| 390 | if(listen(server_sock[socket_idx], 10) < 0) |
| 391 | { |
| 392 | printf("Connection thread closed\r\n"); |
| 393 | server_online = false; |
| 394 | |
| 395 | return; |
| 396 | } |
| 397 | |
| 398 | server_listening = true; |
| 399 | ServerListeningChanged(); |
| 400 | |
| 401 | /*-------------------------------------------------*\ |
| 402 | | Accept the client connection | |
| 403 | \*-------------------------------------------------*/ |
| 404 | client_info->client_sock = accept_select(server_sock[socket_idx]); |
| 405 | |
| 406 | if(client_info->client_sock < 0) |
| 407 | { |
| 408 | printf("Connection thread closed\r\n"); |
| 409 | server_online = false; |
| 410 | |
| 411 | server_listening = false; |
| 412 | ServerListeningChanged(); |
| 413 | |
| 414 | return; |
| 415 | } |
| 416 | |
| 417 | /*-------------------------------------------------*\ |
| 418 | | Get the new client socket and store it in the | |
| 419 | | clients vector | |
| 420 | \*-------------------------------------------------*/ |
| 421 | u_long arg = 0; |
| 422 | ioctlsocket(client_info->client_sock, FIONBIO, &arg); |
| 423 | setsockopt(client_info->client_sock, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)); |
| 424 | |
| 425 | /*-------------------------------------------------*\ |
| 426 | | Discover the remote hosts IP | |
| 427 | \*-------------------------------------------------*/ |
| 428 | struct sockaddr_storage tmp_addr; |
| 429 | char ipstr[INET6_ADDRSTRLEN]; |
| 430 | socklen_t len; |