diff --git a/src/core/hub.c b/src/core/hub.c index 2128fbc..dbf92e6 100644 --- a/src/core/hub.c +++ b/src/core/hub.c @@ -1030,10 +1030,7 @@ void hub_disconnect_user(struct hub_info* hub, struct hub_user* user, int reason /* stop reading from user */ net_shutdown_r(user->connection->sd); - if (net_con_close(user->connection)) - { - hub_free(user->connection); - } + net_con_close(user->connection); user->connection = 0; LOG_TRACE("hub_disconnect_user(), user=%p, reason=%d, state=%d", user, reason, user->state); diff --git a/src/core/netevent.c b/src/core/netevent.c index 4aa2736..3631e27 100644 --- a/src/core/netevent.c +++ b/src/core/netevent.c @@ -171,17 +171,6 @@ void net_event(struct net_connection* con, int event, void *arg) struct hub_user* user = (struct hub_user*) arg; int flag_close = 0; - if (event == NET_EVENT_DESTROYED) - { - LOG_PROTO("NET_EVENT_DESTROYED: con=%p, user=%p\n", con, user); - if (user) - { - user->connection = 0; - } - hub_free(con); - return; - } - #ifdef DEBUG_SENDQ LOG_TRACE("net_event() : fd=%d, ev=%d, arg=%p", fd, (int) event, arg); #endif diff --git a/src/core/probe.c b/src/core/probe.c index 20988a1..06b2e39 100644 --- a/src/core/probe.c +++ b/src/core/probe.c @@ -27,16 +27,6 @@ static void probe_net_event(struct net_connection* con, int events, void *arg) { struct hub_probe* probe = (struct hub_probe*) con->ptr; - if (events == NET_EVENT_DESTROYED) - { - if (probe) - { - probe->connection = 0; - } - hub_free(con); - return; - } - if (events == NET_EVENT_SOCKERROR || events == NET_EVENT_CLOSED || events == NET_EVENT_TIMEOUT) { probe_destroy(probe); @@ -123,10 +113,7 @@ void probe_destroy(struct hub_probe* probe) { if (probe->connection) { - if (net_con_close(probe->connection)) - { - hub_free(probe->connection); - } + net_con_close(probe->connection); probe->connection = 0; } hub_free(probe); diff --git a/src/network/connection.c b/src/network/connection.c index 08adc0f..f3e89ac 100644 --- a/src/network/connection.c +++ b/src/network/connection.c @@ -96,17 +96,38 @@ void net_con_set(struct net_connection* con) if (CON->callback) \ CON->callback(con, EVENTS, CON->ptr); +static void net_con_after_close(struct net_connection* con) +{ + if (net_con_flag_get(con, NET_INITIALIZED)) + { + LOG_MEMORY("DEL: close: CON={ %p, %p, %d, %d}", con, &con->event, con->sd, -1); + uhub_assert(net_con_flag_get(con, NET_EVENT_SET) != 0); + net_con_flag_unset(con, NET_EVENT_SET); + + event_del(&con->event); + net_con_flag_unset(con, NET_INITIALIZED); + } + + net_con_clear_timeout(con); + net_close(con->sd); + con->sd = -1; + + hub_free(con); +} + static void net_con_event(int fd, short ev, void *arg) { struct net_connection* con = (struct net_connection*) arg; int events = net_con_convert_from_libevent_mask(ev); - if (!con->flags) + if (!net_con_flag_get(con, NET_INITIALIZED)) + { return; + } if (net_con_flag_get(con, NET_CLEANUP)) { - hub_free(con); + net_con_after_close(con); return; } @@ -164,9 +185,7 @@ static void net_con_event(int fd, short ev, void *arg) if (net_con_flag_get(con, NET_CLEANUP)) { - net_con_clear_timeout(con); - net_con_flag_unset(con, NET_INITIALIZED); - CALLBACK(con, NET_EVENT_DESTROYED); + net_con_after_close(con); } else { @@ -241,34 +260,18 @@ int net_con_close(struct net_connection* con) { uhub_assert(con); + con->ptr = 0; + if (net_con_flag_get(con, NET_CLEANUP)) - { - LOG_PROTO("Running net_con_close, but we already have closed..."); return 0; - } if (net_con_flag_get(con, NET_PROCESSING_BUSY)) { - LOG_PROTO("Trying to close socket while processing it"); net_con_flag_set(con, NET_CLEANUP); return 0; } - if (net_con_flag_get(con, NET_INITIALIZED)) - { - LOG_MEMORY("DEL: close: CON={ %p, %p, %d, %d}", con, &con->event, con->sd, -1); - uhub_assert(net_con_flag_get(con, NET_EVENT_SET) != 0); - net_con_flag_unset(con, NET_EVENT_SET); - - event_del(&con->event); - net_con_flag_unset(con, NET_INITIALIZED); - } - - net_con_clear_timeout(con); - net_close(con->sd); - con->sd = -1; - - net_con_flag_set(con, NET_CLEANUP); + net_con_after_close(con); return 1; } diff --git a/src/network/connection.h b/src/network/connection.h index 402c6a2..bf02a69 100644 --- a/src/network/connection.h +++ b/src/network/connection.h @@ -27,7 +27,6 @@ #define NET_EVENT_WRITE 0x0004 #define NET_EVENT_SOCKERROR 0x1000 /* Socket error, closed */ #define NET_EVENT_CLOSED 0x2000 /* Socket closed */ -#define NET_EVENT_DESTROYED 0x4000 /* Struct is invalid and can be cleaned up */ struct net_connection; struct net_timer; diff --git a/src/tools/adcclient.c b/src/tools/adcclient.c index e6b2a6a..ae3ad93 100644 --- a/src/tools/adcclient.c +++ b/src/tools/adcclient.c @@ -81,17 +81,6 @@ static void event_callback(struct net_connection* con, int events, void *arg) { struct ADC_client* client = (struct ADC_client*) con->ptr; - if (events == NET_EVENT_DESTROYED) - { - printf("NET_EVENT_DESTROYED\n"); - if (client) - { - client->con = 0; - } - hub_free(con); - return; - } - if (events == NET_EVENT_SOCKERROR || events == NET_EVENT_CLOSED) { printf("NET_EVENT_SOCKERROR || NET_EVENT_CLOSED\n"); @@ -423,6 +412,7 @@ static void ADC_client_on_connected(struct ADC_client* client) static void ADC_client_on_disconnected(struct ADC_client* client) { net_con_close(client->con); + client->con = 0; ADC_client_set_state(client, ps_none); } @@ -436,10 +426,7 @@ void ADC_client_disconnect(struct ADC_client* client) { if (client->con && client->con->sd != -1) { - if (net_con_close(client->con)) - { - hub_free(client->con); - } + net_con_close(client->con); client->con = 0; } }