Skip to content

Commit a080382

Browse files
committed
refactor: Move some OS-specifics into tox_system.
1 parent ad368fe commit a080382

File tree

154 files changed

+2551
-956
lines changed

Some content is hidden

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

154 files changed

+2551
-956
lines changed

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
_build
2+
_install

.github/scripts/flags.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ add_flag -O3 -march=native
2929
# Warn on non-ISO C.
3030
add_c_flag -pedantic
3131
add_c_flag -std=c99
32-
add_cxx_flag -std=c++11
32+
add_cxx_flag -std=c++17
3333

3434
add_flag -g3
3535
add_flag -ftrapv

BUILD.bazel

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,49 @@ project()
88
genrule(
99
name = "public_headers",
1010
srcs = [
11+
"toxav.h",
12+
"tox.h",
13+
"toxencryptsave.h",
1114
"//c-toxcore/toxav:toxav.h",
1215
"//c-toxcore/toxcore:tox.h",
16+
"//c-toxcore/toxcore:tox_attributes.h",
17+
"//c-toxcore/toxcore:tox_logger.h",
18+
"//c-toxcore/toxcore:tox_memory.h",
19+
"//c-toxcore/toxcore:tox_network.h",
20+
"//c-toxcore/toxcore:tox_random.h",
21+
"//c-toxcore/toxcore:tox_system.h",
22+
"//c-toxcore/toxcore:tox_time.h",
1323
"//c-toxcore/toxencryptsave:toxencryptsave.h",
1424
],
1525
outs = [
1626
"tox/toxav.h",
1727
"tox/tox.h",
1828
"tox/toxencryptsave.h",
29+
"tox/toxav/toxav.h",
30+
"tox/toxcore/tox.h",
31+
"tox/toxcore/tox_attributes.h",
32+
"tox/toxcore/tox_logger.h",
33+
"tox/toxcore/tox_memory.h",
34+
"tox/toxcore/tox_network.h",
35+
"tox/toxcore/tox_random.h",
36+
"tox/toxcore/tox_system.h",
37+
"tox/toxcore/tox_time.h",
38+
"tox/toxencryptsave/toxencryptsave.h",
1939
],
2040
cmd = """
21-
cp $(location //c-toxcore/toxav:toxav.h) $(GENDIR)/c-toxcore/tox/toxav.h
22-
cp $(location //c-toxcore/toxcore:tox.h) $(GENDIR)/c-toxcore/tox/tox.h
23-
cp $(location //c-toxcore/toxencryptsave:toxencryptsave.h) $(GENDIR)/c-toxcore/tox/toxencryptsave.h
41+
cp $(location toxav.h) $(GENDIR)/c-toxcore/tox/toxav.h
42+
cp $(location tox.h) $(GENDIR)/c-toxcore/tox/tox.h
43+
cp $(location toxencryptsave.h) $(GENDIR)/c-toxcore/tox/toxencryptsave.h
44+
cp $(location //c-toxcore/toxav:toxav.h) $(GENDIR)/c-toxcore/tox/toxav/toxav.h
45+
cp $(location //c-toxcore/toxcore:tox.h) $(GENDIR)/c-toxcore/tox/toxcore/tox.h
46+
cp $(location //c-toxcore/toxcore:tox_attributes.h) $(GENDIR)/c-toxcore/tox/toxcore/tox_attributes.h
47+
cp $(location //c-toxcore/toxcore:tox_logger.h) $(GENDIR)/c-toxcore/tox/toxcore/tox_logger.h
48+
cp $(location //c-toxcore/toxcore:tox_memory.h) $(GENDIR)/c-toxcore/tox/toxcore/tox_memory.h
49+
cp $(location //c-toxcore/toxcore:tox_network.h) $(GENDIR)/c-toxcore/tox/toxcore/tox_network.h
50+
cp $(location //c-toxcore/toxcore:tox_random.h) $(GENDIR)/c-toxcore/tox/toxcore/tox_random.h
51+
cp $(location //c-toxcore/toxcore:tox_system.h) $(GENDIR)/c-toxcore/tox/toxcore/tox_system.h
52+
cp $(location //c-toxcore/toxcore:tox_time.h) $(GENDIR)/c-toxcore/tox/toxcore/tox_time.h
53+
cp $(location //c-toxcore/toxencryptsave:toxencryptsave.h) $(GENDIR)/c-toxcore/tox/toxencryptsave/toxencryptsave.h
2454
""",
2555
visibility = ["//visibility:public"],
2656
)

CMakeLists.txt

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,17 @@ set(toxcore_SOURCES
282282
toxcore/onion_client.c
283283
toxcore/onion_client.h
284284
toxcore/onion.h
285+
toxcore/os_logger.c
286+
toxcore/os_logger.h
287+
toxcore/os_memory.c
288+
toxcore/os_memory.h
289+
toxcore/os_network.c
290+
toxcore/os_network.h
291+
toxcore/os_network_impl.h
292+
toxcore/os_random.c
293+
toxcore/os_random.h
294+
toxcore/os_system.c
295+
toxcore/os_system.h
285296
toxcore/ping_array.c
286297
toxcore/ping_array.h
287298
toxcore/ping.c
@@ -302,11 +313,29 @@ set(toxcore_SOURCES
302313
toxcore/timed_auth.h
303314
toxcore/tox_api.c
304315
toxcore/tox.c
316+
toxcore/tox.h
305317
toxcore/tox_dispatch.c
306318
toxcore/tox_dispatch.h
307319
toxcore/tox_events.c
308320
toxcore/tox_events.h
309-
toxcore/tox.h
321+
toxcore/tox_logger.c
322+
toxcore/tox_logger.h
323+
toxcore/tox_logger_impl.h
324+
toxcore/tox_memory.c
325+
toxcore/tox_memory.h
326+
toxcore/tox_memory_impl.h
327+
toxcore/tox_network.c
328+
toxcore/tox_network.h
329+
toxcore/tox_network_impl.h
330+
toxcore/tox_random.c
331+
toxcore/tox_random.h
332+
toxcore/tox_random_impl.h
333+
toxcore/tox_system.c
334+
toxcore/tox_system.h
335+
toxcore/tox_system_impl.h
336+
toxcore/tox_time.c
337+
toxcore/tox_time.h
338+
toxcore/tox_time_impl.h
310339
toxcore/tox_private.c
311340
toxcore/tox_private.h
312341
toxcore/tox_unpack.c
@@ -316,9 +345,16 @@ set(toxcore_SOURCES
316345
set(toxcore_LINK_MODULES ${toxcore_LINK_MODULES} ${LIBSODIUM_LIBRARIES})
317346
set(toxcore_PKGCONFIG_REQUIRES ${toxcore_PKGCONFIG_REQUIRES} libsodium)
318347
set(toxcore_API_HEADERS
348+
${toxcore_SOURCE_DIR}/tox.h^tox
319349
${toxcore_SOURCE_DIR}/toxcore/tox.h^tox
350+
${toxcore_SOURCE_DIR}/toxcore/tox_dispatch.h^tox
320351
${toxcore_SOURCE_DIR}/toxcore/tox_events.h^tox
321-
${toxcore_SOURCE_DIR}/toxcore/tox_dispatch.h^tox)
352+
${toxcore_SOURCE_DIR}/toxcore/tox_logger.h^tox
353+
${toxcore_SOURCE_DIR}/toxcore/tox_memory.h^tox
354+
${toxcore_SOURCE_DIR}/toxcore/tox_network.h^tox
355+
${toxcore_SOURCE_DIR}/toxcore/tox_random.h^tox
356+
${toxcore_SOURCE_DIR}/toxcore/tox_system.h^tox
357+
${toxcore_SOURCE_DIR}/toxcore/tox_time.h^tox)
322358

323359
################################################################################
324360
#
@@ -346,6 +382,7 @@ if(BUILD_TOXAV)
346382
toxav/video.c
347383
toxav/video.h)
348384
set(toxcore_API_HEADERS ${toxcore_API_HEADERS}
385+
${toxcore_SOURCE_DIR}/toxav.h^toxav
349386
${toxcore_SOURCE_DIR}/toxav/toxav.h^toxav)
350387

351388
set(toxcore_LINK_MODULES ${toxcore_LINK_MODULES} ${OPUS_LIBRARIES} ${VPX_LIBRARIES})
@@ -362,6 +399,7 @@ set(toxcore_SOURCES ${toxcore_SOURCES}
362399
toxencryptsave/toxencryptsave.c
363400
toxencryptsave/toxencryptsave.h)
364401
set(toxcore_API_HEADERS ${toxcore_API_HEADERS}
402+
${toxcore_SOURCE_DIR}/toxencryptsave.h^tox
365403
${toxcore_SOURCE_DIR}/toxencryptsave/toxencryptsave.h^tox)
366404

367405
################################################################################

auto_tests/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ cc_library(
1919
"//c-toxcore/toxcore:Messenger",
2020
"//c-toxcore/toxcore:mono_time",
2121
"//c-toxcore/toxcore:tox",
22+
"//c-toxcore/toxcore:tox_time",
2223
],
2324
)
2425

@@ -61,9 +62,13 @@ flaky_tests = {
6162
"//c-toxcore/toxcore:onion",
6263
"//c-toxcore/toxcore:onion_announce",
6364
"//c-toxcore/toxcore:onion_client",
65+
"//c-toxcore/toxcore:os_memory",
66+
"//c-toxcore/toxcore:os_network",
67+
"//c-toxcore/toxcore:os_random",
6468
"//c-toxcore/toxcore:tox",
6569
"//c-toxcore/toxcore:tox_dispatch",
6670
"//c-toxcore/toxcore:tox_events",
71+
"//c-toxcore/toxcore:tox_time",
6772
"//c-toxcore/toxcore:util",
6873
"//c-toxcore/toxencryptsave",
6974
"@libsodium",

auto_tests/TCP_test.c

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#include "../toxcore/TCP_server.h"
99
#include "../toxcore/crypto_core.h"
1010
#include "../toxcore/mono_time.h"
11+
#include "../toxcore/os_random.h"
12+
#include "../toxcore/os_network.h"
13+
#include "../toxcore/os_memory.h"
1114
#include "../toxcore/util.h"
1215
#include "auto_test_support.h"
1316

@@ -45,14 +48,14 @@ static uint16_t ports[NUM_PORTS] = {13215, 33445, 25643};
4548

4649
static void test_basic(void)
4750
{
48-
const Random *rng = system_random();
51+
const Random *rng = os_random();
4952
ck_assert(rng != nullptr);
50-
const Network *ns = system_network();
53+
const Network *ns = os_network();
5154
ck_assert(ns != nullptr);
52-
const Memory *mem = system_memory();
55+
const Memory *mem = os_memory();
5356
ck_assert(mem != nullptr);
5457

55-
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
58+
Mono_Time *mono_time = mono_time_new(mem, nullptr);
5659
Logger *logger = logger_new();
5760
logger_callback_log(logger, print_debug_logger, nullptr, nullptr);
5861

@@ -303,14 +306,14 @@ static int read_packet_sec_TCP(const Logger *logger, struct sec_TCP_con *con, ui
303306

304307
static void test_some(void)
305308
{
306-
const Random *rng = system_random();
309+
const Random *rng = os_random();
307310
ck_assert(rng != nullptr);
308-
const Network *ns = system_network();
311+
const Network *ns = os_network();
309312
ck_assert(ns != nullptr);
310-
const Memory *mem = system_memory();
313+
const Memory *mem = os_memory();
311314
ck_assert(mem != nullptr);
312315

313-
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
316+
Mono_Time *mono_time = mono_time_new(mem, nullptr);
314317
Logger *logger = logger_new();
315318

316319
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
@@ -498,15 +501,15 @@ static int oob_data_callback(void *object, const uint8_t *public_key, const uint
498501

499502
static void test_client(void)
500503
{
501-
const Random *rng = system_random();
504+
const Random *rng = os_random();
502505
ck_assert(rng != nullptr);
503-
const Network *ns = system_network();
506+
const Network *ns = os_network();
504507
ck_assert(ns != nullptr);
505-
const Memory *mem = system_memory();
508+
const Memory *mem = os_memory();
506509
ck_assert(mem != nullptr);
507510

508511
Logger *logger = logger_new();
509-
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
512+
Mono_Time *mono_time = mono_time_new(mem, nullptr);
510513

511514
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
512515
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
@@ -632,14 +635,14 @@ static void test_client(void)
632635
// Test how the client handles servers that don't respond.
633636
static void test_client_invalid(void)
634637
{
635-
const Random *rng = system_random();
638+
const Random *rng = os_random();
636639
ck_assert(rng != nullptr);
637-
const Network *ns = system_network();
640+
const Network *ns = os_network();
638641
ck_assert(ns != nullptr);
639-
const Memory *mem = system_memory();
642+
const Memory *mem = os_memory();
640643
ck_assert(mem != nullptr);
641644

642-
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
645+
Mono_Time *mono_time = mono_time_new(mem, nullptr);
643646
Logger *logger = logger_new();
644647

645648
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
@@ -711,14 +714,14 @@ static int tcp_data_callback(void *object, int id, const uint8_t *data, uint16_t
711714

712715
static void test_tcp_connection(void)
713716
{
714-
const Random *rng = system_random();
717+
const Random *rng = os_random();
715718
ck_assert(rng != nullptr);
716-
const Network *ns = system_network();
719+
const Network *ns = os_network();
717720
ck_assert(ns != nullptr);
718-
const Memory *mem = system_memory();
721+
const Memory *mem = os_memory();
719722
ck_assert(mem != nullptr);
720723

721-
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
724+
Mono_Time *mono_time = mono_time_new(mem, nullptr);
722725
Logger *logger = logger_new();
723726

724727
tcp_data_callback_called = 0;
@@ -824,14 +827,14 @@ static int tcp_oobdata_callback(void *object, const uint8_t *public_key, unsigne
824827

825828
static void test_tcp_connection2(void)
826829
{
827-
const Random *rng = system_random();
830+
const Random *rng = os_random();
828831
ck_assert(rng != nullptr);
829-
const Network *ns = system_network();
832+
const Network *ns = os_network();
830833
ck_assert(ns != nullptr);
831-
const Memory *mem = system_memory();
834+
const Memory *mem = os_memory();
832835
ck_assert(mem != nullptr);
833836

834-
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
837+
Mono_Time *mono_time = mono_time_new(mem, nullptr);
835838
Logger *logger = logger_new();
836839

837840
tcp_oobdata_callback_called = 0;

auto_tests/announce_test.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
#include "../toxcore/mono_time.h"
88
#include "../toxcore/forwarding.h"
99
#include "../toxcore/net_crypto.h"
10+
#include "../toxcore/os_memory.h"
11+
#include "../toxcore/os_network.h"
12+
#include "../toxcore/os_random.h"
1013
#include "../toxcore/util.h"
1114
#include "auto_test_support.h"
1215
#include "check_compat.h"
1316

1417
static void test_bucketnum(void)
1518
{
16-
const Random *rng = system_random();
19+
const Random *rng = os_random();
1720
ck_assert(rng != nullptr);
1821
uint8_t key1[CRYPTO_PUBLIC_KEY_SIZE], key2[CRYPTO_PUBLIC_KEY_SIZE];
1922
random_bytes(rng, key1, sizeof(key1));
@@ -50,17 +53,17 @@ static void test_announce_data(void *object, const uint8_t *data, uint16_t lengt
5053

5154
static void test_store_data(void)
5255
{
53-
const Random *rng = system_random();
56+
const Random *rng = os_random();
5457
ck_assert(rng != nullptr);
55-
const Network *ns = system_network();
58+
const Network *ns = os_network();
5659
ck_assert(ns != nullptr);
57-
const Memory *mem = system_memory();
60+
const Memory *mem = os_memory();
5861
ck_assert(mem != nullptr);
5962

6063
Logger *log = logger_new();
6164
ck_assert(log != nullptr);
6265
logger_callback_log(log, print_debug_logger, nullptr, nullptr);
63-
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
66+
Mono_Time *mono_time = mono_time_new(mem, nullptr);
6467
Networking_Core *net = new_networking_no_udp(log, mem, ns);
6568
DHT *dht = new_dht(log, mem, rng, ns, mono_time, net, true, true);
6669
Forwarding *forwarding = new_forwarding(log, rng, mono_time, dht);

auto_tests/auto_test_support.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
#include "../toxcore/Messenger.h"
77
#include "../toxcore/mono_time.h"
88
#include "../toxcore/tox_struct.h"
9+
#include "../toxcore/tox_time_impl.h"
910

1011
#include "auto_test_support.h"
1112

1213
#ifndef ABORT_ON_LOG_ERROR
1314
#define ABORT_ON_LOG_ERROR true
1415
#endif
1516

16-
Run_Auto_Options default_run_auto_options()
17+
Run_Auto_Options default_run_auto_options(void)
1718
{
1819
return (Run_Auto_Options) {
1920
.graph = GRAPH_COMPLETE,
@@ -159,15 +160,23 @@ static uint64_t get_state_clock_callback(void *user_data)
159160
return *clock;
160161
}
161162

163+
static const Tox_Time_Funcs autotox_time_funcs = {
164+
get_state_clock_callback,
165+
};
166+
162167
void set_mono_time_callback(AutoTox *autotox)
163168
{
164169
ck_assert(autotox != nullptr);
165170

171+
if (autotox->tm == nullptr) {
172+
autotox->tm = tox_time_new(&autotox_time_funcs, &autotox->clock, autotox->tox->sys.mem);
173+
}
174+
166175
Mono_Time *mono_time = autotox->tox->mono_time;
167176

168177
autotox->clock = current_time_monotonic(mono_time);
169-
mono_time_set_current_time_callback(mono_time, nullptr, nullptr); // set to default first
170-
mono_time_set_current_time_callback(mono_time, get_state_clock_callback, &autotox->clock);
178+
mono_time_set_current_time_callback(mono_time, nullptr); // set to default first
179+
mono_time_set_current_time_callback(mono_time, autotox->tm);
171180
}
172181

173182
void save_autotox(AutoTox *autotox)
@@ -193,6 +202,8 @@ void kill_autotox(AutoTox *autotox)
193202
fprintf(stderr, "Killing #%u\n", autotox->index);
194203
autotox->alive = false;
195204
tox_kill(autotox->tox);
205+
tox_time_free(autotox->tm);
206+
autotox->tm = nullptr;
196207
}
197208

198209
void reload(AutoTox *autotox)
@@ -380,6 +391,7 @@ void run_auto_test(struct Tox_Options *options, uint32_t tox_count, void test(Au
380391

381392
for (uint32_t i = 0; i < tox_count; ++i) {
382393
tox_kill(autotoxes[i].tox);
394+
tox_time_free(autotoxes[i].tm);
383395
free(autotoxes[i].state);
384396
free(autotoxes[i].save_state);
385397
}

0 commit comments

Comments
 (0)