diff --git a/auto_tests/tox_loop_test.c b/auto_tests/tox_loop_test.c index 66dc716bdcd..66546b98393 100644 --- a/auto_tests/tox_loop_test.c +++ b/auto_tests/tox_loop_test.c @@ -1,11 +1,11 @@ #include #include #include -#include #include "../toxcore/tox.h" #include "check_compat.h" +#include "../testing/misc_tools.h" #define TCP_RELAY_PORT 33448 /* The Travis-CI container responds poorly to ::1 as a localhost address @@ -81,7 +81,7 @@ static void test_tox_loop(void) ck_assert_msg(tox_bootstrap(userdata_tcp.tox, TOX_LOCALHOST, 33445, dpk, &error), "Bootstrap error, %i", error); pthread_mutex_unlock(&userdata_tcp.mutex); - sleep(10); + c_sleep(10000); tox_loop_stop(userdata.tox); pthread_join(worker, (void **)(void *)&retval); diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index e0a20b6b03f..257a92b7902 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c @@ -102,6 +102,32 @@ TCP_Client_Status tcp_con_status(const TCP_Client_Connection *con) { return con->status; } + +#ifdef HAVE_LIBEV +bool tcp_con_ev_is_active(TCP_Client_Connection *con) +{ + return ev_is_active(&con->sock_listener.listener) + || ev_is_pending(&con->sock_listener.listener); +} + +void tcp_con_ev_listen(TCP_Client_Connection *con, struct ev_loop *dispatcher, tcp_con_ev_listen_cb *callback, + void *data) +{ + con->sock_listener.dispatcher = dispatcher; + con->sock_listener.listener.data = data; + + ev_io_init(&con->sock_listener.listener, callback, con->sock.socket, EV_READ); + ev_io_start(dispatcher, &con->sock_listener.listener); +} +#endif + +void tcp_con_ev_stop(TCP_Client_Connection *con) +{ +#ifdef HAVE_LIBEV + ev_io_stop(con->sock_listener.dispatcher, &con->sock_listener.listener); +#endif +} + void *tcp_con_custom_object(const TCP_Client_Connection *con) { return con->custom_object; diff --git a/toxcore/TCP_client.h b/toxcore/TCP_client.h index c221daf5b8d..73a213388e1 100644 --- a/toxcore/TCP_client.h +++ b/toxcore/TCP_client.h @@ -12,6 +12,10 @@ #include "TCP_server.h" #include "crypto_core.h" +#ifdef HAVE_LIBEV +#include +#endif + #define TCP_CONNECTION_TIMEOUT 10 typedef enum TCP_Proxy_Type { @@ -43,6 +47,15 @@ IP_Port tcp_con_ip_port(const TCP_Client_Connection *con); Socket tcp_con_sock(const TCP_Client_Connection *con); TCP_Client_Status tcp_con_status(const TCP_Client_Connection *con); +#ifdef HAVE_LIBEV +bool tcp_con_ev_is_active(TCP_Client_Connection *con); + +typedef void tcp_con_ev_listen_cb(struct ev_loop *dispatcher, ev_io *sock_listener, int events); +void tcp_con_ev_listen(TCP_Client_Connection *con, struct ev_loop *dispatcher, tcp_con_ev_listen_cb *callback, + void *data); +#endif +void tcp_con_ev_stop(TCP_Client_Connection *con); + void *tcp_con_custom_object(const TCP_Client_Connection *con); uint32_t tcp_con_custom_uint(const TCP_Client_Connection *con); void tcp_con_set_custom_object(TCP_Client_Connection *con, void *object); diff --git a/toxcore/network.c b/toxcore/network.c index 6b345680645..e8c129f51ce 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -521,6 +521,29 @@ Socket net_sock(const Networking_Core *net) return net->sock; } +#ifdef HAVE_LIBEV +bool net_ev_is_active(Networking_Core *net) +{ + return ev_is_active(&net->sock_listener.listener) || ev_is_pending(&net->sock_listener.listener); +} + +void net_ev_listen(Networking_Core *net, struct ev_loop *dispatcher, net_ev_listen_cb *callback, void *data) +{ + net->sock_listener.dispatcher = dispatcher; + net->sock_listener.listener.data = data; + + ev_io_init(&net->sock_listener.listener, callback, net->sock.socket, EV_READ); + ev_io_start(dispatcher, &net->sock_listener.listener); +} +#endif + +void net_ev_stop(Networking_Core *net) +{ +#ifdef HAVE_LIBEV + ev_io_stop(net->sock_listener.dispatcher, &net->sock_listener.listener); +#endif +} + /* Basic network functions: */ @@ -1023,9 +1046,7 @@ void kill_networking(Networking_Core *net) } -#ifdef HAVE_LIBEV - ev_io_stop(net->sock_listener.dispatcher, &net->sock_listener.listener); -#endif + net_ev_stop(net); free(net); } diff --git a/toxcore/network.h b/toxcore/network.h index 378e863ebbb..82db948ccc7 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -15,6 +15,10 @@ #include // size_t #include // uint*_t +#ifdef HAVE_LIBEV +#include // uint*_t +#endif + #ifdef __cplusplus extern "C" { #endif @@ -324,6 +328,14 @@ Family net_family(const Networking_Core *net); uint16_t net_port(const Networking_Core *net); Socket net_sock(const Networking_Core *net); +#ifdef HAVE_LIBEV +bool net_ev_is_active(Networking_Core *net); + +typedef void net_ev_listen_cb(struct ev_loop *dispatcher, ev_io *sock_listener, int events); +void net_ev_listen(Networking_Core *net, struct ev_loop *dispatcher, net_ev_listen_cb *callback, void *data); +#endif +void net_ev_stop(Networking_Core *net); + /** Run this before creating sockets. * * return 0 on success diff --git a/toxcore/tox.c b/toxcore/tox.c index bd432bf56d5..b71062e9396 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -864,10 +864,10 @@ static void tox_stop_loop_async(struct ev_loop *dispatcher, ev_async *listener, } Event_Arg *tmp = (Event_Arg *) listener->data; - Messenger *m = tmp->tox; + Messenger *m = tmp->tox->m; - if (ev_is_active(&m->net->sock_listener.listener) || ev_is_pending(&m->net->sock_listener.listener)) { - ev_io_stop(dispatcher, &m->net->sock_listener.listener); + if (net_ev_is_active(m->net)) { + net_ev_stop(m->net); } uint32_t len = tcp_connections_length(nc_get_tcp_c(m->net_crypto)); @@ -875,9 +875,8 @@ static void tox_stop_loop_async(struct ev_loop *dispatcher, ev_async *listener, for (uint32_t i = 0; i < len; ++i) { const TCP_con *conn = tcp_connections_connection_at(nc_get_tcp_c(m->net_crypto), i); - if (ev_is_active(&conn->connection->sock_listener.listener) - || ev_is_pending(&conn->connection->sock_listener.listener)) { - ev_io_stop(dispatcher, &conn->connection->sock_listener.listener); + if (tcp_con_ev_is_active(conn->connection)) { + tcp_con_ev_stop(conn->connection); } } @@ -893,7 +892,8 @@ static void tox_do_iterate(struct ev_loop *dispatcher, ev_io *sock_listener, int } Event_Arg *tmp = (Event_Arg *)sock_listener->data; - Messenger *m = tmp->tox->m; + Tox *tox = tmp->tox; + Messenger *m = tox->m; if (tmp->tox->loop_begin_callback) { tmp->tox->loop_begin_callback(tmp->tox, tmp->user_data); @@ -901,11 +901,8 @@ static void tox_do_iterate(struct ev_loop *dispatcher, ev_io *sock_listener, int tox_iterate(tmp->tox, tmp->user_data); - if (!ev_is_active(&m->net->sock_listener.listener) && !ev_is_pending(&m->net->sock_listener.listener)) { - m->net->sock_listener.dispatcher = dispatcher; - ev_io_init(&m->net->sock_listener.listener, tox_do_iterate, net_sock(m->net), EV_READ); - m->net->sock_listener.listener.data = sock_listener->data; - ev_io_start(dispatcher, &m->net->sock_listener.listener); + if (!net_ev_is_active(m->net)) { + net_ev_listen(m->net, dispatcher, tox_do_iterate, tmp); } uint32_t len = tcp_connections_length(nc_get_tcp_c(m->net_crypto)); @@ -913,17 +910,13 @@ static void tox_do_iterate(struct ev_loop *dispatcher, ev_io *sock_listener, int for (uint32_t i = 0; i < len; ++i) { const TCP_con *conn = tcp_connections_connection_at(nc_get_tcp_c(m->net_crypto), i); - if (!ev_is_active(&conn->connection->sock_listener.listener) - && !ev_is_pending(&conn->connection->sock_listener.listener)) { - conn->connection->sock_listener.dispatcher = dispatcher; - ev_io_init(&conn->connection->sock_listener.listener, tox_do_iterate, tcp_con_sock(conn->connection), EV_READ); - conn->connection->sock_listener.listener.data = sock_listener->data; - ev_io_start(m->dispatcher, &conn->connection->sock_listener.listener); + if (!tcp_con_ev_is_active(conn->connection)) { + tcp_con_ev_listen(conn->connection, dispatcher, tox_do_iterate, tmp); } } - if (m->loop_end_callback) { - m->loop_end_callback(m, tmp->user_data); + if (tox->loop_end_callback) { + tox->loop_end_callback(tox, tmp->user_data); } } #else