diff --git a/src/tcp_server.h b/src/tcp_server.h index fa50657..4dc8231 100644 --- a/src/tcp_server.h +++ b/src/tcp_server.h @@ -127,6 +127,7 @@ private: static void loop(void* data); static void on_new_connection(uv_stream_t* server, int status); static void on_connection_close(uv_handle_t* handle); + static void on_connection_error(uv_handle_t* handle); static void on_connect(uv_connect_t* req, int status); void on_new_client(uv_stream_t* server); void on_new_client_nolock(uv_stream_t* server, Client* client); diff --git a/src/tcp_server.inl b/src/tcp_server.inl index 79a6c10..a2c730d 100644 --- a/src/tcp_server.inl +++ b/src/tcp_server.inl @@ -377,7 +377,7 @@ bool TCPServer::connect_to_peer_nolock(Client* cl err = uv_tcp_nodelay(&client->m_socket, 1); if (err) { LOGERR(1, "failed to set tcp_nodelay on tcp client handle, error " << uv_err_name(err)); - m_preallocatedClients.push_back(client); + uv_close(reinterpret_cast(&client->m_socket), on_connection_error); return false; } @@ -385,7 +385,7 @@ bool TCPServer::connect_to_peer_nolock(Client* cl if (!m_pendingConnections.insert(client->m_addr).second) { LOGINFO(6, "there is already a pending connection to this IP, not connecting to " << log::Gray() << static_cast(client->m_addrString)); - m_preallocatedClients.push_back(client); + uv_close(reinterpret_cast(&client->m_socket), on_connection_error); return false; } @@ -397,7 +397,7 @@ bool TCPServer::connect_to_peer_nolock(Client* cl if (err) { LOGERR(1, "failed to initiate tcp connection, error " << uv_err_name(err)); m_pendingConnections.erase(client->m_addr); - m_preallocatedClients.push_back(client); + uv_close(reinterpret_cast(&client->m_socket), on_connection_error); return false; } else { @@ -676,6 +676,16 @@ void TCPServer::on_connection_close(uv_handle_t* } } +template +void TCPServer::on_connection_error(uv_handle_t* handle) +{ + Client* client = reinterpret_cast(handle->data); + TCPServer* server = client->m_owner; + + MutexLock lock(server->m_clientsListLock); + server->m_preallocatedClients.push_back(client); +} + template void TCPServer::on_connect(uv_connect_t* req, int status) { @@ -701,11 +711,7 @@ void TCPServer::on_connect(uv_connect_t* req, int LOGWARN(5, "failed to connect to " << static_cast(client->m_addrString) << ", error " << uv_err_name(status)); } server->on_connect_failed(client->m_isV6, client->m_addr, client->m_port); - uv_handle_t* h = reinterpret_cast(&client->m_socket); - if (!uv_is_closing(h)) { - uv_close(h, nullptr); - } - server->m_preallocatedClients.push_back(client); + uv_close(reinterpret_cast(&client->m_socket), on_connection_error); return; } @@ -744,14 +750,14 @@ void TCPServer::on_new_client(uv_stream_t* server err = uv_tcp_nodelay(&client->m_socket, 1); if (err) { LOGERR(1, "failed to set tcp_nodelay on tcp client handle, error " << uv_err_name(err)); - m_preallocatedClients.push_back(client); + uv_close(reinterpret_cast(&client->m_socket), on_connection_error); return; } err = uv_accept(server, reinterpret_cast(&client->m_socket)); if (err) { LOGERR(1, "failed to accept client connection, error " << uv_err_name(err)); - m_preallocatedClients.push_back(client); + uv_close(reinterpret_cast(&client->m_socket), on_connection_error); return; }