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 b226f14
Show file tree
Hide file tree
Showing 13 changed files with 212 additions and 103 deletions.
24 changes: 21 additions & 3 deletions .cirrus.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
cirrus-ci_task:
bazel-release_task:
container:
image: toxchat/toktok-stack:0.0.31-third_party
image: toxchat/toktok-stack:0.0.31-release
cpu: 2
memory: 2G
configure_script:
Expand All @@ -17,9 +17,27 @@ cirrus-ci_task:
//c-toxcore/...
-//c-toxcore/auto_tests:tcp_relay_test # TODO(robinlinden): Why does this pass locally but not in Cirrus?

bazel-debug_task:
container:
image: toxchat/toktok-stack:0.0.31-debug
cpu: 2
memory: 2G
configure_script:
- /src/workspace/tools/inject-repo c-toxcore
test_all_script:
- cd /src/workspace && bazel test -k
--remote_http_cache=http://$CIRRUS_HTTP_CACHE_HOST
--build_tag_filters=-haskell
--test_tag_filters=-haskell
--remote_download_minimal
--config=debug
--
//c-toxcore/...
-//c-toxcore/auto_tests:tcp_relay_test # TODO(robinlinden): Why does this pass locally but not in Cirrus?

cimple_task:
container:
image: toxchat/toktok-stack:0.0.31-third_party
image: toxchat/toktok-stack:0.0.31-release
cpu: 2
memory: 4G
configure_script:
Expand Down
1 change: 1 addition & 0 deletions .travis/flags-clang.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ add_flag -Wextra
add_flag -Weverything

# Disable specific warning flags for both C and C++.
add_flag -Wno-c11-extensions

# Very verbose, not very useful. This warns about things like int -> uint
# conversions that change sign without a cast and narrowing conversions.
Expand Down
2 changes: 1 addition & 1 deletion .travis/flags.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ add_flag -O3 -march=native

# Warn on non-ISO C.
add_c_flag -pedantic
add_c_flag -std=c99
add_c_flag -std=c11
add_cxx_flag -std=c++11

add_flag -g3
Expand Down
89 changes: 55 additions & 34 deletions auto_tests/tox_loop_test.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#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
#define TCP_RELAY_PORT 33449
/* The Travis-CI container responds poorly to ::1 as a localhost address
* You're encouraged to -D FORCE_TESTS_IPV6 on a local test */
#ifdef FORCE_TESTS_IPV6
Expand All @@ -17,85 +17,106 @@
#endif

typedef struct Loop_Test {
int start_count;
int stop_count;
volatile int start_count;
volatile int stop_count;
pthread_mutex_t mutex;
Tox *tox;
} Loop_Test;

static void tox_loop_cb_start(Tox *tox, void *user_data)
static void tox_loop_cb_start(Tox *tox, void *data)
{
Loop_Test *userdata = (Loop_Test *)user_data;
Loop_Test *userdata = (Loop_Test *)data;
pthread_mutex_lock(&userdata->mutex);
userdata->start_count++;
fprintf(stderr, "br1: %p (%d)\n", (volatile void *)&userdata->start_count, userdata->start_count);
fputs("br2\n", stderr);
++userdata->start_count;
}

static void tox_loop_cb_stop(Tox *tox, void *user_data)
static void tox_loop_cb_stop(Tox *tox, void *data)
{
Loop_Test *userdata = (Loop_Test *) user_data;
userdata->stop_count++;
Loop_Test *userdata = (Loop_Test *)data;
++userdata->stop_count;
pthread_mutex_unlock(&userdata->mutex);
}

static void *tox_loop_worker(void *data)
{
Loop_Test *userdata = (Loop_Test *) data;
tox_loop(userdata->tox, data, nullptr);
Loop_Test *userdata = (Loop_Test *)data;
Tox_Err_Loop err;
tox_loop(userdata->tox, userdata, &err);
ck_assert_msg(err == TOX_ERR_LOOP_OK, "tox_loop error: %d", err);
return nullptr;
}

static void test_tox_loop(void)
{
pthread_t worker, worker_tcp;
struct Tox_Options *opts = tox_options_new(nullptr);
Loop_Test userdata;
Tox_Err_Options_New err_opts;
struct Tox_Options *opts = tox_options_new(&err_opts);
ck_assert_msg(err_opts == TOX_ERR_OPTIONS_NEW_OK, "tox_options_new: %d\n", err_opts);
tox_options_set_experimental_thread_safety(opts, true);

Loop_Test *userdata = calloc(1, sizeof(Loop_Test));
ck_assert(userdata != nullptr);
uint8_t dpk[TOX_PUBLIC_KEY_SIZE];
int retval;

userdata.start_count = 0;
userdata.stop_count = 0;
pthread_mutex_init(&userdata.mutex, nullptr);
userdata->start_count = 0;
userdata->stop_count = 0;
pthread_mutex_init(&userdata->mutex, nullptr);

tox_options_set_tcp_port(opts, TCP_RELAY_PORT);
userdata.tox = tox_new(opts, nullptr);
tox_callback_loop_begin(userdata.tox, tox_loop_cb_start);
tox_callback_loop_end(userdata.tox, tox_loop_cb_stop);
pthread_create(&worker, nullptr, tox_loop_worker, &userdata);
Tox_Err_New err_new;
userdata->tox = tox_new(opts, &err_new);
ck_assert_msg(err_new == TOX_ERR_NEW_OK, "tox_new: %d\n", err_new);
tox_callback_loop_begin(userdata->tox, tox_loop_cb_start);
tox_callback_loop_end(userdata->tox, tox_loop_cb_stop);
pthread_create(&worker, nullptr, tox_loop_worker, userdata);

tox_self_get_dht_id(userdata.tox, dpk);
fprintf(stderr, "br0: udp %p\n", (volatile void *)&userdata->start_count);
tox_self_get_dht_id(userdata->tox, dpk);

tox_options_default(opts);
tox_options_set_experimental_thread_safety(opts, true);
Loop_Test userdata_tcp;
userdata_tcp.start_count = 0;
userdata_tcp.stop_count = 0;
pthread_mutex_init(&userdata_tcp.mutex, nullptr);
userdata_tcp.tox = tox_new(opts, nullptr);
userdata_tcp.tox = tox_new(opts, &err_new);
ck_assert_msg(err_new == TOX_ERR_NEW_OK, "tox_new: %d\n", err_new);
tox_callback_loop_begin(userdata_tcp.tox, tox_loop_cb_start);
tox_callback_loop_end(userdata_tcp.tox, tox_loop_cb_stop);
pthread_create(&worker_tcp, nullptr, tox_loop_worker, &userdata_tcp);

pthread_mutex_lock(&userdata_tcp.mutex);
TOX_ERR_BOOTSTRAP error;
Tox_Err_Bootstrap error;
ck_assert_msg(tox_add_tcp_relay(userdata_tcp.tox, TOX_LOCALHOST, TCP_RELAY_PORT, dpk, &error), "Add relay error, %i",
error);
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(1000);

tox_loop_stop(userdata.tox);
pthread_join(worker, (void **)(void *)&retval);
ck_assert_msg(retval == 0, "tox_loop didn't return 0");
tox_loop_stop(userdata->tox);
void *retval = nullptr;
pthread_join(worker, &retval);
ck_assert_msg((uintptr_t)retval == 0, "tox_loop didn't return 0");

tox_kill(userdata.tox);
ck_assert_msg(userdata.start_count == userdata.stop_count, "start and stop must match");
tox_kill(userdata->tox);
fprintf(stderr, "br3: udp %p (%d)\n", (volatile void *)&userdata->start_count, userdata->start_count);
ck_assert_msg(userdata->start_count == userdata->stop_count, "start and stop must match (start = %d, stop = %d)",
userdata->start_count, userdata->stop_count);

tox_loop_stop(userdata_tcp.tox);
pthread_join(worker_tcp, (void **)(void *)&retval);
ck_assert_msg(retval == 0, "tox_loop didn't return 0");
pthread_join(worker_tcp, &retval);
ck_assert_msg((uintptr_t)retval == 0, "tox_loop didn't return 0");

tox_kill(userdata_tcp.tox);
ck_assert_msg(userdata_tcp.start_count == userdata_tcp.stop_count, "start and stop must match");
fprintf(stderr, "br4: tcp %p (%d)\n", (volatile void *)&userdata_tcp.start_count, userdata_tcp.start_count);
ck_assert_msg(userdata_tcp.start_count == userdata_tcp.stop_count, "start and stop must match (start = %d, stop = %d)",
userdata_tcp.start_count, userdata_tcp.stop_count);

tox_options_free(opts);
free(userdata);
}

int main(int argc, char *argv[])
Expand Down
13 changes: 8 additions & 5 deletions toxcore/LAN_discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
* behaviour. Consider storing the data in per-instance variables instead. */
//!TOKSTYLE-
// No global mutable state in Tokstyle.
static int broadcast_count = -1;
static IP_Port broadcast_ip_ports[MAX_INTERFACES];
static _Thread_local int broadcast_count = -1;
static _Thread_local IP_Port broadcast_ip_ports[MAX_INTERFACES];
//!TOKSTYLE+

#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
Expand Down Expand Up @@ -209,7 +209,8 @@ static void fetch_broadcast_info(uint16_t port)

#endif

/* Send packet to all IPv4 broadcast addresses
/**
* Send packet to all IPv4 broadcast addresses
*
* return 1 if sent to at least one broadcast target.
* return 0 on failure to find any valid broadcast target.
Expand All @@ -233,7 +234,7 @@ static uint32_t send_broadcasts(Networking_Core *net, uint16_t port, const uint8
return 1;
}

/* Return the broadcast ip. */
/** Return the broadcast ip. */
static IP broadcast_ip(Family family_socket, Family family_broadcast)
{
IP ip;
Expand Down Expand Up @@ -266,7 +267,9 @@ static bool ip4_is_local(IP4 ip4)
return ip4.uint8[0] == 127;
}

/* Is IP a local ip or not. */
/**
* Is IP a local ip or not.
*/
bool ip_is_local(IP ip)
{
if (net_family_is_ipv4(ip.family)) {
Expand Down
2 changes: 1 addition & 1 deletion toxcore/LAN_discovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#define LAN_DISCOVERY_INTERVAL 10

/**
* Send a LAN discovery pcaket to the broadcast address with port port.
* Send a LAN discovery packet to the broadcast address with port port.
*/
int32_t lan_discovery_send(uint16_t port, DHT *dht);

Expand Down
5 changes: 0 additions & 5 deletions toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -1895,11 +1895,6 @@ Messenger *new_messenger(Mono_Time *mono_time, Messenger_Options *options, unsig

#ifdef HAVE_LIBEV
m->dispatcher = ev_loop_new(0);
#else
m->loop_run = false;
#endif // HAVE_LIBEV

#if defined(HAVE_LIBEV)

if (m->dispatcher == nullptr) {
free(m);
Expand Down
2 changes: 0 additions & 2 deletions toxcore/Messenger.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,6 @@ struct Messenger {
#ifdef HAVE_LIBEV
struct ev_loop *dispatcher;
ev_async stop_loop;
#else
bool loop_run;
#endif

Messenger_Options options;
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
Loading

0 comments on commit b226f14

Please sign in to comment.