Skip to content

Commit ee78824

Browse files
committed
feat: Add an option to wait for events in tox_iterate.
1 parent b14943b commit ee78824

82 files changed

Lines changed: 3843 additions & 551 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.clang-tidy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ CheckOptions:
4242
- key: concurrency-mt-unsafe.FunctionSet
4343
value: posix
4444
- key: readability-function-cognitive-complexity.Threshold
45-
value: 159 # TODO(iphydf): Decrease. tox_new_system is the highest at the moment.
45+
value: 167 # TODO(iphydf): Decrease. tox_new_system is the highest at the moment.
4646
- key: cppcoreguidelines-avoid-do-while.IgnoreMacros
4747
value: true
4848
- key: readability-simplify-boolean-expr.SimplifyDeMorgan

.github/workflows/docker.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ jobs:
138138
- name: Build and store to local Docker daemon
139139
uses: docker/build-push-action@v5
140140
with:
141-
context: other/docker/windows
141+
file: other/docker/windows/windows.Dockerfile
142142
load: true
143143
tags: toxchat/windows:win${{ matrix.bits }}
144144
cache-from: type=registry,ref=toxchat/windows:win${{ matrix.bits }}
@@ -150,7 +150,7 @@ jobs:
150150
if: ${{ github.event_name == 'push' }}
151151
uses: docker/build-push-action@v5
152152
with:
153-
context: other/docker/windows
153+
file: other/docker/windows/windows.Dockerfile
154154
push: ${{ github.event_name == 'push' }}
155155
tags: toxchat/windows:win${{ matrix.bits }}
156156
build-args: |

CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ endif()
2929

3030
set_source_files_properties(
3131
toxcore/mono_time.c
32-
toxcore/network.c
32+
toxcore/os_event.c
33+
toxcore/os_network.c
3334
toxcore/tox.c
3435
toxcore/util.c
3536
PROPERTIES SKIP_UNITY_BUILD_INCLUSION TRUE)
@@ -233,6 +234,8 @@ set(toxcore_SOURCES
233234
toxcore/crypto_core_pack.h
234235
toxcore/DHT.c
235236
toxcore/DHT.h
237+
toxcore/ev.c
238+
toxcore/ev.h
236239
toxcore/events/conference_connected.c
237240
toxcore/events/conference_invite.c
238241
toxcore/events/conference_message.c
@@ -324,6 +327,8 @@ set(toxcore_SOURCES
324327
toxcore/onion_client.c
325328
toxcore/onion_client.h
326329
toxcore/onion.h
330+
toxcore/os_event.c
331+
toxcore/os_event.h
327332
toxcore/os_memory.c
328333
toxcore/os_memory.h
329334
toxcore/os_network.c
@@ -571,6 +576,9 @@ function(unit_test subdir target)
571576
elseif(TARGET Threads::Threads)
572577
target_link_libraries(unit_${target}_test PRIVATE Threads::Threads)
573578
endif()
579+
if(WIN32)
580+
target_link_libraries(unit_${target}_test PRIVATE ws2_32)
581+
endif()
574582
target_link_libraries(unit_${target}_test PRIVATE GTest::gtest GTest::gtest_main GTest::gmock)
575583
set_target_properties(unit_${target}_test PROPERTIES COMPILE_FLAGS "${TEST_CXX_FLAGS}")
576584
add_test(NAME ${target} COMMAND unit_${target}_test)
@@ -604,6 +612,7 @@ if(UNITTEST AND TARGET GTest::gtest AND TARGET GTest::gmock)
604612
unit_test(toxcore DHT)
605613
unit_test(toxcore bin_pack)
606614
unit_test(toxcore crypto_core)
615+
unit_test(toxcore ev)
607616
unit_test(toxcore group_announce)
608617
unit_test(toxcore group_moderation)
609618
unit_test(toxcore list)

INSTALL.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ requirements are that you have Docker version of >= 1.9.0 and you are running
286286
64-bit system.
287287

288288
The cross-compilation is fully automated by a parameterized
289-
[Dockerfile](/other/docker/windows/Dockerfile).
289+
[Dockerfile](/other/docker/windows/windows.Dockerfile).
290290

291291
Install Docker
292292

@@ -313,10 +313,10 @@ available to customize the building of the container image.
313313
Example of building a container image with options
314314

315315
```sh
316-
cd other/docker/windows
317316
docker build \
318317
--build-arg SUPPORT_TEST=true \
319318
-t toxcore \
319+
-f other/docker/windows/windows.Dockerfile \
320320
.
321321
```
322322

auto_tests/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ cc_library(
1717
"//c-toxcore/testing:misc_tools",
1818
"//c-toxcore/toxcore:DHT",
1919
"//c-toxcore/toxcore:Messenger",
20+
"//c-toxcore/toxcore:ev",
2021
"//c-toxcore/toxcore:mono_time",
2122
"//c-toxcore/toxcore:net_crypto",
23+
"//c-toxcore/toxcore:network",
24+
"//c-toxcore/toxcore:os_event",
2225
"//c-toxcore/toxcore:tox",
2326
"//c-toxcore/toxcore:tox_dispatch",
2427
"//c-toxcore/toxcore:tox_events",
@@ -66,6 +69,7 @@ extra_data = {
6669
"//c-toxcore/toxcore:announce",
6770
"//c-toxcore/toxcore:ccompat",
6871
"//c-toxcore/toxcore:crypto_core",
72+
"//c-toxcore/toxcore:ev",
6973
"//c-toxcore/toxcore:forwarding",
7074
"//c-toxcore/toxcore:friend_connection",
7175
"//c-toxcore/toxcore:logger",
@@ -76,6 +80,7 @@ extra_data = {
7680
"//c-toxcore/toxcore:onion",
7781
"//c-toxcore/toxcore:onion_announce",
7882
"//c-toxcore/toxcore:onion_client",
83+
"//c-toxcore/toxcore:os_event",
7984
"//c-toxcore/toxcore:os_memory",
8085
"//c-toxcore/toxcore:os_random",
8186
"//c-toxcore/toxcore:tox",

auto_tests/TCP_test.c

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include <stdlib.h>
33
#include <string.h>
44

5-
#include "../testing/misc_tools.h"
5+
#include "../toxcore/os_event.h"
66
#include "../toxcore/TCP_client.h"
77
#include "../toxcore/TCP_common.h"
88
#include "../toxcore/TCP_server.h"
@@ -59,11 +59,14 @@ static void test_basic(void)
5959
Logger *logger = logger_new(mem);
6060
logger_callback_log(logger, print_debug_logger, nullptr, nullptr);
6161

62+
Ev *ev = os_event_new(mem, logger);
63+
ck_assert(ev != nullptr);
64+
6265
// Attempt to create a new TCP_Server instance.
6366
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
6467
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
6568
crypto_new_keypair(rng, self_public_key, self_secret_key);
66-
TCP_Server *tcp_s = new_tcp_server(logger, mem, rng, ns, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr, nullptr);
69+
TCP_Server *tcp_s = new_tcp_server(logger, mem, rng, ns, ev, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr, nullptr);
6770
ck_assert_msg(tcp_s != nullptr, "Failed to create a TCP relay server.");
6871
ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS,
6972
"Failed to bind a TCP relay server to all %d attempted ports.", NUM_PORTS);
@@ -196,6 +199,7 @@ static void test_basic(void)
196199
// Closing connections.
197200
kill_sock(ns, sock);
198201
kill_tcp_server(tcp_s);
202+
ev_kill(ev);
199203

200204
logger_kill(logger);
201205
mono_time_free(mem, mono_time);
@@ -325,10 +329,13 @@ static void test_some(void)
325329
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
326330
Logger *logger = logger_new(mem);
327331

332+
Ev *ev = os_event_new(mem, logger);
333+
ck_assert(ev != nullptr);
334+
328335
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
329336
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
330337
crypto_new_keypair(rng, self_public_key, self_secret_key);
331-
TCP_Server *tcp_s = new_tcp_server(logger, mem, rng, ns, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr, nullptr);
338+
TCP_Server *tcp_s = new_tcp_server(logger, mem, rng, ns, ev, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr, nullptr);
332339
ck_assert_msg(tcp_s != nullptr, "Failed to create TCP relay server");
333340
ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports.");
334341

@@ -422,6 +429,7 @@ static void test_some(void)
422429

423430
// Kill off the connections
424431
kill_tcp_server(tcp_s);
432+
ev_kill(ev);
425433
kill_tcp_con(con1);
426434
kill_tcp_con(con2);
427435
kill_tcp_con(con3);
@@ -522,11 +530,12 @@ static void test_client(void)
522530

523531
Logger *logger = logger_new(mem);
524532
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
533+
Ev *ev = os_event_new(mem, logger);
525534

526535
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
527536
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
528537
crypto_new_keypair(rng, self_public_key, self_secret_key);
529-
TCP_Server *tcp_s = new_tcp_server(logger, mem, rng, ns, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr, nullptr);
538+
TCP_Server *tcp_s = new_tcp_server(logger, mem, rng, ns, ev, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr, nullptr);
530539
ck_assert_msg(tcp_s != nullptr, "Failed to create a TCP relay server.");
531540
ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind the relay server to all ports.");
532541

@@ -538,7 +547,7 @@ static void test_client(void)
538547
ip_port_tcp_s.port = net_htons(ports[random_u32(rng) % NUM_PORTS]);
539548
ip_port_tcp_s.ip = get_loopback();
540549

541-
TCP_Client_Connection *conn = new_tcp_connection(logger, mem, mono_time, rng, ns, &ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, nullptr, nullptr);
550+
TCP_Client_Connection *conn = new_tcp_connection(logger, mem, mono_time, rng, ns, ev, &ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, nullptr, nullptr);
542551
ck_assert_msg(conn != nullptr, "Failed to create a TCP client connection.");
543552
// TCP sockets might need a moment before they can be written to.
544553
c_sleep(50);
@@ -574,7 +583,7 @@ static void test_client(void)
574583
uint8_t f2_secret_key[CRYPTO_SECRET_KEY_SIZE];
575584
crypto_new_keypair(rng, f2_public_key, f2_secret_key);
576585
ip_port_tcp_s.port = net_htons(ports[random_u32(rng) % NUM_PORTS]);
577-
TCP_Client_Connection *conn2 = new_tcp_connection(logger, mem, mono_time, rng, ns, &ip_port_tcp_s, self_public_key, f2_public_key,
586+
TCP_Client_Connection *conn2 = new_tcp_connection(logger, mem, mono_time, rng, ns, ev, &ip_port_tcp_s, self_public_key, f2_public_key,
578587
f2_secret_key, nullptr, nullptr);
579588
ck_assert_msg(conn2 != nullptr, "Failed to create a second TCP client connection.");
580589
c_sleep(50);
@@ -645,6 +654,7 @@ static void test_client(void)
645654
kill_tcp_connection(conn2);
646655

647656
logger_kill(logger);
657+
ev_kill(ev);
648658
mono_time_free(mem, mono_time);
649659
}
650660

@@ -672,7 +682,8 @@ static void test_client_invalid(void)
672682

673683
ip_port_tcp_s.port = net_htons(ports[random_u32(rng) % NUM_PORTS]);
674684
ip_port_tcp_s.ip = get_loopback();
675-
TCP_Client_Connection *conn = new_tcp_connection(logger, mem, mono_time, rng, ns, &ip_port_tcp_s,
685+
Ev *ev = os_event_new(mem, logger);
686+
TCP_Client_Connection *conn = new_tcp_connection(logger, mem, mono_time, rng, ns, ev, &ip_port_tcp_s,
676687
self_public_key, f_public_key, f_secret_key, nullptr, nullptr);
677688
ck_assert_msg(conn != nullptr, "Failed to create a TCP client connection.");
678689

@@ -700,6 +711,7 @@ static void test_client_invalid(void)
700711
kill_tcp_connection(conn);
701712

702713
logger_kill(logger);
714+
ev_kill(ev);
703715
mono_time_free(mem, mono_time);
704716
}
705717

@@ -747,18 +759,22 @@ static void test_tcp_connection(void)
747759
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
748760
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
749761
crypto_new_keypair(rng, self_public_key, self_secret_key);
750-
TCP_Server *tcp_s = new_tcp_server(logger, mem, rng, ns, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr, nullptr);
762+
763+
Ev *ev = os_event_new(mem, logger);
764+
ck_assert(ev != nullptr);
765+
766+
TCP_Server *tcp_s = new_tcp_server(logger, mem, rng, ns, ev, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr, nullptr);
751767
ck_assert_msg(pk_equal(tcp_server_public_key(tcp_s), self_public_key), "Wrong public key");
752768

753769
TCP_Proxy_Info proxy_info;
754770
proxy_info.proxy_type = TCP_PROXY_NONE;
755771
crypto_new_keypair(rng, self_public_key, self_secret_key);
756-
TCP_Connections *tc_1 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info, tcp_np);
772+
TCP_Connections *tc_1 = new_tcp_connections(logger, mem, rng, ns, mono_time, ev, self_secret_key, &proxy_info, tcp_np);
757773
ck_assert_msg(tc_1 != nullptr, "Failed to create TCP connections");
758774
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_1), self_public_key), "Wrong public key");
759775

760776
crypto_new_keypair(rng, self_public_key, self_secret_key);
761-
TCP_Connections *tc_2 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info, tcp_np);
777+
TCP_Connections *tc_2 = new_tcp_connections(logger, mem, rng, ns, mono_time, ev, self_secret_key, &proxy_info, tcp_np);
762778
ck_assert_msg(tc_2 != nullptr, "Failed to create TCP connections");
763779
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_2), self_public_key), "Wrong public key");
764780

@@ -824,6 +840,7 @@ static void test_tcp_connection(void)
824840
netprof_kill(mem, tcp_np);
825841

826842
logger_kill(logger);
843+
ev_kill(ev);
827844
mono_time_free(mem, mono_time);
828845
}
829846

@@ -869,18 +886,22 @@ static void test_tcp_connection2(void)
869886
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
870887
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
871888
crypto_new_keypair(rng, self_public_key, self_secret_key);
872-
TCP_Server *tcp_s = new_tcp_server(logger, mem, rng, ns, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr, nullptr);
889+
890+
Ev *ev = os_event_new(mem, logger);
891+
ck_assert(ev != nullptr);
892+
893+
TCP_Server *tcp_s = new_tcp_server(logger, mem, rng, ns, ev, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr, nullptr);
873894
ck_assert_msg(pk_equal(tcp_server_public_key(tcp_s), self_public_key), "Wrong public key");
874895

875896
TCP_Proxy_Info proxy_info;
876897
proxy_info.proxy_type = TCP_PROXY_NONE;
877898
crypto_new_keypair(rng, self_public_key, self_secret_key);
878-
TCP_Connections *tc_1 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info, tcp_np);
899+
TCP_Connections *tc_1 = new_tcp_connections(logger, mem, rng, ns, mono_time, ev, self_secret_key, &proxy_info, tcp_np);
879900
ck_assert_msg(tc_1 != nullptr, "Failed to create TCP connections");
880901
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_1), self_public_key), "Wrong public key");
881902

882903
crypto_new_keypair(rng, self_public_key, self_secret_key);
883-
TCP_Connections *tc_2 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info, tcp_np);
904+
TCP_Connections *tc_2 = new_tcp_connections(logger, mem, rng, ns, mono_time, ev, self_secret_key, &proxy_info, tcp_np);
884905
ck_assert_msg(tc_2 != nullptr, "Failed to create TCP connections");
885906
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_2), self_public_key), "Wrong public key");
886907

@@ -939,6 +960,7 @@ static void test_tcp_connection2(void)
939960
kill_tcp_connections(tc_2);
940961

941962
logger_kill(logger);
963+
ev_kill(ev);
942964
mono_time_free(mem, mono_time);
943965
}
944966

auto_tests/announce_test.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "../toxcore/forwarding.h"
77
#include "../toxcore/os_memory.h"
88
#include "../toxcore/os_random.h"
9+
#include "../toxcore/os_event.h"
910
#include "auto_test_support.h"
1011
#include "check_compat.h"
1112

@@ -60,7 +61,9 @@ static void test_store_data(void)
6061
logger_callback_log(log, print_debug_logger, nullptr, nullptr);
6162
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
6263
ck_assert(mono_time != nullptr);
63-
Networking_Core *net = new_networking_no_udp(log, mem, ns);
64+
Ev *ev = os_event_new(mem, log);
65+
ck_assert(ev != nullptr);
66+
Networking_Core *net = new_networking_no_udp(log, mem, ns, ev);
6467
ck_assert(net != nullptr);
6568
DHT *dht = new_dht(log, mem, rng, ns, mono_time, net, true, true);
6669
ck_assert(dht != nullptr);
@@ -108,6 +111,7 @@ static void test_store_data(void)
108111
kill_forwarding(forwarding);
109112
kill_dht(dht);
110113
kill_networking(net);
114+
ev_kill(ev);
111115
mono_time_free(mem, mono_time);
112116
logger_kill(log);
113117
}

auto_tests/forwarding_test.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "../toxcore/net_crypto.h"
1111
#include "../toxcore/os_memory.h"
1212
#include "../toxcore/os_random.h"
13+
#include "../toxcore/os_event.h"
1314
#include "auto_test_support.h"
1415
#include "check_compat.h"
1516

@@ -96,6 +97,7 @@ static bool all_returned(Test_Data *test_data)
9697
typedef struct Forwarding_Subtox {
9798
Logger *log;
9899
Mono_Time *mono_time;
100+
Ev *ev;
99101
Networking_Core *net;
100102
Net_Profile *tcp_np;
101103
DHT *dht;
@@ -118,12 +120,13 @@ static Forwarding_Subtox *new_forwarding_subtox(const Memory *mem, bool no_udp,
118120
ck_assert(subtox->log != nullptr);
119121
logger_callback_log(subtox->log, print_debug_logger, nullptr, index);
120122
subtox->mono_time = mono_time_new(mem, nullptr, nullptr);
123+
subtox->ev = os_event_new(mem, subtox->log);
121124

122125
if (no_udp) {
123-
subtox->net = new_networking_no_udp(subtox->log, mem, ns);
126+
subtox->net = new_networking_no_udp(subtox->log, mem, ns, subtox->ev);
124127
} else {
125128
const IP ip = get_loopback();
126-
subtox->net = new_networking_ex(subtox->log, mem, ns, &ip, port, port, nullptr);
129+
subtox->net = new_networking_ex(subtox->log, mem, ns, subtox->ev, &ip, port, port, nullptr);
127130
}
128131

129132
subtox->dht = new_dht(subtox->log, mem, rng, ns, subtox->mono_time, subtox->net, true, true);
@@ -132,7 +135,7 @@ static Forwarding_Subtox *new_forwarding_subtox(const Memory *mem, bool no_udp,
132135
ck_assert(subtox->tcp_np != nullptr);
133136

134137
const TCP_Proxy_Info inf = {{{{0}}}};
135-
subtox->c = new_net_crypto(subtox->log, mem, rng, ns, subtox->mono_time, subtox->net, subtox->dht, &auto_test_dht_funcs, &inf, subtox->tcp_np);
138+
subtox->c = new_net_crypto(subtox->log, mem, rng, ns, subtox->mono_time, subtox->ev, subtox->net, subtox->dht, &auto_test_dht_funcs, &inf, subtox->tcp_np);
136139

137140
subtox->forwarding = new_forwarding(subtox->log, mem, rng, subtox->mono_time, subtox->dht, subtox->net);
138141
ck_assert(subtox->forwarding != nullptr);
@@ -151,6 +154,7 @@ static void kill_forwarding_subtox(const Memory *mem, Forwarding_Subtox *subtox)
151154
netprof_kill(mem, subtox->tcp_np);
152155
kill_dht(subtox->dht);
153156
kill_networking(subtox->net);
157+
ev_kill(subtox->ev);
154158
mono_time_free(mem, subtox->mono_time);
155159
logger_kill(subtox->log);
156160
free(subtox);

0 commit comments

Comments
 (0)