Skip to content

Commit

Permalink
slight refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Dec 29, 2021
1 parent 9d57d32 commit 00e9def
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 25 deletions.
4 changes: 2 additions & 2 deletions auto_tests/tox_loop_test.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include <pthread.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#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
Expand Down Expand Up @@ -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);
Expand Down
26 changes: 26 additions & 0 deletions toxcore/TCP_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions toxcore/TCP_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
#include "TCP_server.h"
#include "crypto_core.h"

#ifdef HAVE_LIBEV
#include <ev.h>
#endif

#define TCP_CONNECTION_TIMEOUT 10

typedef enum TCP_Proxy_Type {
Expand Down Expand Up @@ -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);
Expand Down
27 changes: 24 additions & 3 deletions toxcore/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
*/

Expand Down Expand Up @@ -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);
}
Expand Down
12 changes: 12 additions & 0 deletions toxcore/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
#include <stddef.h> // size_t
#include <stdint.h> // uint*_t

#ifdef HAVE_LIBEV
#include <ev.h> // uint*_t
#endif

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -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
Expand Down
33 changes: 13 additions & 20 deletions toxcore/tox.c
Original file line number Diff line number Diff line change
Expand Up @@ -864,20 +864,19 @@ 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));

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);
}
}

Expand All @@ -893,37 +892,31 @@ 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);
}

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));

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
Expand Down

0 comments on commit 00e9def

Please sign in to comment.