static void Server_newTCPConnection( uv_stream_t *server, int status ) { if ( status < 0 ) { log_warn( "Connection error: %s", uv_err_name( status ) ); return; } dbg_info( "Connection received." ); ClientConnection *client = Client_new( ); if ( !client ) goto badClient; client->handle.tcpHandle = malloc( sizeof(*client->handle.tcpHandle) ); if ( !client->handle.tcpHandle ) goto badHandle; uv_tcp_init( server->loop, client->handle.tcpHandle ); client->handle.tcpHandle->data = client; if ( uv_accept( server, client->handle.stream ) == 0 ) { client->server = server->data; #if defined(CLIENTTIMEINFO) client->startTime = uv_now( server->loop ); #endif Client_handleConnection( client ); } else { Client_terminate( client ); } return; badHandle: Client_free( client ); badClient: return; }
int remove_client(int socket) { // Close the socket close(socket); // Remove registered socket from poll list struct pollfd tempPollfd; tempPollfd.fd = socket; pollVector_remove(pollList, &tempPollfd, &equals_pollfd); // Remove registered Client from client list Client tempClient; tempClient.socket = socket; int i; for (i = 0 ; i < clientList->size; ++i) { if (equals_Client_Socket(&tempClient, clientVector_get(clientList, i)) == 0) { clientVector_removeAt(clientList,i , &tempClient); break; } } StringBuffer *msg = StringBuffer_construct(); StringBuffer_concat(msg, tempClient.name); StringBuffer_concat(msg, " ist offline."); broadcast(msg); Client_free(&tempClient); StringBuffer_free(msg); return EXIT_SUCCESS; }
static void Client_cleanup( uv_handle_t *handle ) { ClientConnection *client = handle->data; checktime( client, "Close connection." ); HttpParser_free( client->parserInfo ); Client_free( client ); // this is the client tcp handle. free( handle ); }