Skip to content

Commit 8fa2c5b

Browse files
committed
refactor: Implement buffered event dispatch in tox_iterate.
Aligns the behaviour of events and non-events iterate code.
1 parent 2e4b423 commit 8fa2c5b

71 files changed

Lines changed: 1598 additions & 952 deletions

Some content is hidden

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

auto_tests/BUILD.bazel

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ cc_library(
1515
deps = [
1616
":check_compat",
1717
"//c-toxcore/testing:misc_tools",
18+
"//c-toxcore/toxcore",
1819
"//c-toxcore/toxcore:DHT",
1920
"//c-toxcore/toxcore:Messenger",
2021
"//c-toxcore/toxcore:mono_time",
2122
"//c-toxcore/toxcore:net_crypto",
2223
"//c-toxcore/toxcore:tox",
2324
"//c-toxcore/toxcore:tox_dispatch",
24-
"//c-toxcore/toxcore:tox_events",
2525
"//c-toxcore/toxcore:tox_log_level",
2626
],
2727
)
@@ -58,6 +58,7 @@ extra_data = {
5858
":check_compat",
5959
"//c-toxcore/testing:misc_tools",
6060
"//c-toxcore/toxav",
61+
"//c-toxcore/toxcore",
6162
"//c-toxcore/toxcore:Messenger",
6263
"//c-toxcore/toxcore:TCP_client",
6364
"//c-toxcore/toxcore:TCP_common",
@@ -80,8 +81,6 @@ extra_data = {
8081
"//c-toxcore/toxcore:os_random",
8182
"//c-toxcore/toxcore:tox",
8283
"//c-toxcore/toxcore:tox_dispatch",
83-
"//c-toxcore/toxcore:tox_events",
84-
"//c-toxcore/toxcore:tox_unpack",
8584
"//c-toxcore/toxcore:util",
8685
"//c-toxcore/toxencryptsave",
8786
"@libsodium",

auto_tests/scenarios/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ cc_library(
88
visibility = ["//visibility:public"],
99
deps = [
1010
"//c-toxcore/testing:misc_tools",
11+
"//c-toxcore/toxcore",
1112
"//c-toxcore/toxcore:attributes",
1213
"//c-toxcore/toxcore:ccompat",
1314
"//c-toxcore/toxcore:mono_time",
1415
"//c-toxcore/toxcore:network",
1516
"//c-toxcore/toxcore:tox",
1617
"//c-toxcore/toxcore:tox_dispatch",
17-
"//c-toxcore/toxcore:tox_events",
1818
],
1919
)
2020

@@ -25,7 +25,7 @@ cc_library(
2525
deps = [
2626
":scenario_framework",
2727
"//c-toxcore/toxav",
28+
"//c-toxcore/toxcore",
2829
"//c-toxcore/toxcore:tox",
29-
"//c-toxcore/toxcore:tox_events",
3030
],
3131
) for src in glob(["scenario_*_test.c"])]

auto_tests/scenarios/framework/framework.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,6 @@ ToxNode *tox_scenario_add_node_ex(ToxScenario *s, const char *alias, tox_node_sc
577577
}
578578

579579
node->dispatch = tox_dispatch_new(nullptr);
580-
tox_events_init(node->tox);
581580
mono_time_set_current_time_callback(node->tox->mono_time, get_scenario_clock, s);
582581

583582
// Initial mirror population
@@ -784,7 +783,6 @@ void tox_node_reload(ToxNode *node)
784783

785784
ck_assert_msg(new_tox != nullptr, "tox_new said OK but returned NULL");
786785
Tox_Dispatch *new_dispatch = tox_dispatch_new(nullptr);
787-
tox_events_init(new_tox);
788786
mono_time_set_current_time_callback(new_tox->mono_time, get_scenario_clock, s);
789787

790788
pthread_mutex_lock(&s->mutex);

auto_tests/tox_dispatch_test.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ static void test_tox_events(void)
108108
for (uint32_t i = 0; i < 2; ++i) {
109109
index[i] = i + 1;
110110
toxes[i] = tox_new_log(nullptr, nullptr, &index[i]);
111-
tox_events_init(toxes[i]);
112111
ck_assert_msg(toxes[i] != nullptr, "failed to create tox instances %u", i);
113112
}
114113

other/docker/esp32/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ cc_binary(
99
"main/tox_main.h",
1010
],
1111
deps = [
12+
"//c-toxcore/toxcore",
1213
"//c-toxcore/toxcore:ccompat",
1314
"//c-toxcore/toxcore:tox",
14-
"//c-toxcore/toxcore:tox_events",
1515
],
1616
)

other/docker/modules/check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ class BuildContext:
110110
def bzl_exports_files(self, *args: Any, **kwargs: Any) -> None:
111111
pass
112112

113-
def bzl_alias(self, *args: Any, **kwargs: Any) -> None:
114-
pass
113+
def bzl_alias(self, name: str, actual: str, **kwargs: Any) -> None:
114+
self._add_target(name, (), (), (actual, ))
115115

116116
def bzl_sh_library(self, *args: Any, **kwargs: Any) -> None:
117117
pass

other/event_tooling/generate_event_c.cpp

Lines changed: 83 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
// this file can be used to generate event.c files
55
// requires c++17
66

7+
#include <algorithm>
8+
#include <cstdint>
79
#include <fstream>
810
#include <iostream>
11+
#include <map>
912
#include <string>
10-
#include <algorithm>
11-
#include <vector>
1213
#include <variant>
13-
#include <map>
14+
#include <vector>
1415

1516
std::string str_tolower(std::string s) {
1617
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c){ return std::tolower(c); });
@@ -168,6 +169,7 @@ std::string zero_initializer_for_type(const std::string& type) {
168169

169170
void generate_event_impl(const std::string& event_name, const std::vector<EventType>& event_types) {
170171
const std::string event_name_l = str_tolower(event_name);
172+
const std::string event_name_u = str_toupper(event_name);
171173
std::string file_name = output_folder + "/" + event_name_l + ".c";
172174

173175
std::ofstream f(file_name);
@@ -218,11 +220,20 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
218220
#include "../tox.h"
219221
#include "../tox_event.h"
220222
#include "../tox_events.h")";
223+
224+
if (need_tox_unpack_h) {
225+
f << R"(
226+
#include "../tox_pack.h")";
227+
}
228+
229+
f << R"(
230+
#include "../tox_struct.h")";
231+
221232
if (need_tox_unpack_h) {
222233
f << R"(
223-
#include "../tox_pack.h"
224234
#include "../tox_unpack.h")";
225235
}
236+
226237
f << R"(
227238
228239
/*****************************************************
@@ -242,7 +253,7 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
242253
f << " " << t.type << " " << t.name << ";\n";
243254
},
244255
[&](const EventTypeByteRange& t) {
245-
f << " " << "uint8_t" << " *" << t.name_data << ";\n";
256+
f << " " << t.type_c_arg << " *" << t.name_data << ";\n";
246257
f << " " << "uint32_t" << " " << t.name_length << ";\n";
247258
},
248259
[&](const EventTypeByteArray& t) {
@@ -279,7 +290,7 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
279290
f << " " << t.type << " " << t.name << ")\n";
280291
},
281292
[&](const EventTypeByteRange& t) {
282-
f << "\n const Memory *_Nonnull mem, const uint8_t *_Nullable " << t.name_data << ", uint32_t " << t.name_length << ")\n";
293+
f << "\n const Memory *_Nonnull mem, const " << t.type_c_arg << " *_Nullable " << t.name_data << ", uint32_t " << t.name_length << ")\n";
283294
},
284295
[&](const EventTypeByteArray& t) {
285296
f << " const uint8_t " << t.name << "[" << t.length_constant << "])\n";
@@ -303,9 +314,9 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
303314
f << " assert(" << t.name_length << " == 0);\n";
304315
f << " return true;\n }\n\n";
305316
if (t.null_terminated) {
306-
f << " uint8_t *" << t.name_data << "_copy = (uint8_t *)mem_balloc(mem, " << t.name_length << " + 1);\n\n";
317+
f << " " << t.type_c_arg << " *" << t.name_data << "_copy = (" << t.type_c_arg << " *)mem_balloc(mem, " << t.name_length << " + 1);\n\n";
307318
} else {
308-
f << " uint8_t *" << t.name_data << "_copy = (uint8_t *)mem_balloc(mem, " << t.name_length << ");\n\n";
319+
f << " " << t.type_c_arg << " *" << t.name_data << "_copy = (" << t.type_c_arg << " *)mem_balloc(mem, " << t.name_length << ");\n\n";
309320
}
310321
f << " if (" << t.name_data << "_copy == nullptr) {\n";
311322
f << " return false;\n }\n\n";
@@ -342,7 +353,7 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
342353
f << "(const Tox_Event_" << event_name << " *" << event_name_l << ")\n";
343354
f << "{\n assert(" << event_name_l << " != nullptr);\n";
344355
f << " return " << event_name_l << "->" << t.name_length << ";\n}\n";
345-
f << "const uint8_t *tox_event_" << event_name_l << "_get_" << t.name_data;
356+
f << "const " << t.type_c_arg << " *tox_event_" << event_name_l << "_get_" << t.name_data;
346357
f << "(const Tox_Event_" << event_name << " *" << event_name_l << ")\n";
347358
f << "{\n assert(" << event_name_l << " != nullptr);\n";
348359
f << " return " << event_name_l << "->" << t.name_data << ";\n}\n\n";
@@ -435,7 +446,11 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
435446
}
436447
},
437448
[&](const EventTypeByteRange& t) {
438-
f << "bin_pack_bin(bp, event->" << t.name_data << ", event->" << t.name_length << ")";
449+
if (t.type_c_arg == "char") {
450+
f << "bin_pack_str(bp, event->" << t.name_data << ", event->" << t.name_length << ")";
451+
} else {
452+
f << "bin_pack_bin(bp, event->" << t.name_data << ", event->" << t.name_length << ")";
453+
}
439454
},
440455
[&](const EventTypeByteArray& t) {
441456
f << "bin_pack_bin(bp, event->" << t.name << ", " << t.length_constant << ")";
@@ -471,7 +486,11 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
471486
}
472487
},
473488
[&](const EventTypeByteRange& t) {
474-
f << "bin_unpack_bin(bu, &event->" << t.name_data << ", &event->" << t.name_length << ")";
489+
if (t.type_c_arg == "char") {
490+
f << "bin_unpack_str(bu, &event->" << t.name_data << ", &event->" << t.name_length << ")";
491+
} else {
492+
f << "bin_unpack_bin(bu, &event->" << t.name_data << ", &event->" << t.name_length << ")";
493+
}
475494
},
476495
[&](const EventTypeByteArray& t) {
477496
f << "bin_unpack_bin_fixed(bu, event->" << t.name << ", " << t.length_constant << ")";
@@ -493,7 +512,7 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
493512
)";
494513

495514
f << "const Tox_Event_" << event_name << " *tox_event_get_" << event_name_l << "(const Tox_Event *event)\n{\n";
496-
f << " return event->type == TOX_EVENT_" << str_toupper(event_name) << " ? event->data." << event_name_l << " : nullptr;\n}\n\n";
515+
f << " return event->type == TOX_EVENT_" << event_name_u << " ? event->data." << event_name_l << " : nullptr;\n}\n\n";
497516

498517
// new
499518
f << "Tox_Event_" << event_name << " *tox_event_" << event_name_l << "_new(const Memory *mem)\n{\n";
@@ -506,7 +525,7 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
506525
// free
507526
f << "void tox_event_" << event_name_l << "_free(Tox_Event_" << event_name << " *" << event_name_l << ", const Memory *mem)\n{\n";
508527
f << " if (" << event_name_l << " != nullptr) {\n";
509-
f << " tox_event_" << event_name_l << "_destruct((Tox_Event_" << event_name << " * _Nonnull)" << event_name_l << ", mem);\n }\n";
528+
f << " tox_event_" << event_name_l << "_destruct(" << event_name_l << ", mem);\n }\n";
510529
f << " mem_delete(mem, " << event_name_l << ");\n}\n\n";
511530

512531
// add
@@ -515,7 +534,7 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
515534
f << " if (" << event_name_l << " == nullptr) {\n";
516535
f << " return nullptr;\n }\n\n";
517536
f << " Tox_Event event;\n";
518-
f << " event.type = TOX_EVENT_" << str_toupper(event_name) << ";\n";
537+
f << " event.type = TOX_EVENT_" << event_name_u << ";\n";
519538
f << " event.data." << event_name_l << " = " << event_name_l << ";\n\n";
520539
f << " if (!tox_events_add(events, &event)) {\n";
521540
f << " tox_event_" << event_name_l << "_free(" << event_name_l << ", mem);\n";
@@ -550,28 +569,31 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
550569
551570
)";
552571
f << "void tox_events_handle_" << event_name_l << "(\n";
553-
f << " Tox *tox";
572+
bool first_arg = true;
554573

555574
for (const auto& t : event_types) {
575+
if (!first_arg) f << ",\n";
576+
f << " ";
556577
std::visit(
557578
overloaded{
558579
[&](const EventTypeTrivial& t) {
559-
f << ", " << (t.cb_type.empty() ? t.type : t.cb_type) << " " << t.name;
580+
f << (t.cb_type.empty() ? t.type : t.cb_type) << " " << t.name;
560581
},
561582
[&](const EventTypeByteRange& t) {
562-
f << ", const " << t.type_c_arg << " *" << t.name_data << ", " << t.type_length_cb << " " << t.name_length_cb;
583+
f << "const " << t.type_c_arg << " *" << t.name_data << ", " << t.type_length_cb << " " << t.name_length_cb;
563584
},
564585
[&](const EventTypeByteArray& t) {
565-
f << ", const uint8_t *" << t.name;
586+
f << "const uint8_t *" << t.name;
566587
}
567588
},
568589
t
569590
);
591+
first_arg = false;
570592
}
571593

572-
f << ",\n void *user_data)\n{\n";
573-
f << " Tox_Events_State *state = tox_events_alloc(user_data);\n";
574-
f << " Tox_Event_" << event_name << " *" << event_name_l << " = tox_event_" << event_name_l << "_alloc(state);\n\n";
594+
if (!first_arg) f << ",\n ";
595+
f << "Tox_Events_State *state)\n{\n";
596+
f << " Tox_Event_" << event_name << " *" << event_name_l << " = tox_event_" << event_name_l << "_alloc(tox_events_alloc(state));\n\n";
575597
f << " if (" << event_name_l << " == nullptr) {\n return;\n }\n\n";
576598

577599
for (const auto& t : event_types) {
@@ -582,9 +604,6 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
582604
},
583605
[&](const EventTypeByteRange& t) {
584606
f << " tox_event_" << event_name_l << "_set_" << t.name_data << "(" << event_name_l << ", state->mem, ";
585-
if (t.type_c_arg != "uint8_t") {
586-
f << "(const uint8_t *)";
587-
}
588607
f << t.name_data << ", " << t.name_length_cb << ");\n";
589608
},
590609
[&](const EventTypeByteArray& t) {
@@ -595,6 +614,46 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
595614
);
596615
}
597616
f << "}\n";
617+
618+
f << "\nvoid tox_events_handle_" << event_name_l << "_dispatch(Tox *tox, void *user_data, const Tox_Event *event)\n{\n";
619+
if (event_name_l == "friend_lossy_packet" || event_name_l == "friend_lossless_packet") {
620+
f << " const Tox_Event_" << event_name << " *ev = event->data." << event_name_l << ";\n";
621+
f << " if (ev->data_length > 0 && tox->" << event_name_l << "_callback_per_pktid[ev->data[0]] != nullptr) {\n";
622+
f << " tox_unlock(tox);\n";
623+
f << " tox->" << event_name_l << "_callback_per_pktid[ev->data[0]](tox, ev->friend_number, ev->data, ev->data_length, user_data);\n";
624+
f << " tox_lock(tox);\n";
625+
f << " }\n";
626+
} else {
627+
f << " if (tox->" << event_name_l << "_callback == nullptr) {\n return;\n }\n\n";
628+
f << " const Tox_Event_" << event_name << " *ev = event->data." << event_name_l << ";\n";
629+
f << " tox_unlock(tox);\n";
630+
f << " tox->" << event_name_l << "_callback(tox, ";
631+
first_arg = true;
632+
for (const auto& t : event_types) {
633+
if (!first_arg) f << ", ";
634+
std::visit(
635+
overloaded{
636+
[&](const EventTypeTrivial& t) {
637+
f << "ev->" << t.name;
638+
},
639+
[&](const EventTypeByteRange& t) {
640+
if (t.type_c_arg != "uint8_t") {
641+
f << "(const " << t.type_c_arg << " *)";
642+
}
643+
f << "ev->" << t.name_data << ", ev->" << t.name_length;
644+
},
645+
[&](const EventTypeByteArray& t) {
646+
f << "ev->" << t.name;
647+
}
648+
},
649+
t
650+
);
651+
first_arg = false;
652+
}
653+
f << ", user_data);\n";
654+
f << " tox_lock(tox);\n";
655+
}
656+
f << "}\n";
598657
}
599658

600659
// c++ generate_event_c.cpp -std=c++17 && ./a.out Friend_Lossy_Packet && diff --color ../../toxcore/events/friend_lossy_packet.c out/friend_lossy_packet.c

testing/fuzzing/BUILD.bazel

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ cc_fuzz_test(
99
corpus = ["//tools/toktok-fuzzer/corpus:bootstrap_fuzz_test"],
1010
deps = [
1111
"//c-toxcore/testing/support",
12+
"//c-toxcore/toxcore",
1213
"//c-toxcore/toxcore:tox",
13-
"//c-toxcore/toxcore:tox_dispatch",
14-
"//c-toxcore/toxcore:tox_events",
1514
],
1615
)
1716

@@ -24,10 +23,9 @@ cc_fuzz_test(
2423
data = ["//tools/toktok-fuzzer/init:e2e_fuzz_test.dat"],
2524
deps = [
2625
"//c-toxcore/testing/support",
26+
"//c-toxcore/toxcore",
2727
"//c-toxcore/toxcore:crypto_core",
2828
"//c-toxcore/toxcore:tox",
29-
"//c-toxcore/toxcore:tox_dispatch",
30-
"//c-toxcore/toxcore:tox_events",
3129
],
3230
)
3331

@@ -49,9 +47,8 @@ cc_binary(
4947
copts = ["-UNDEBUG"],
5048
deps = [
5149
"//c-toxcore/testing/support",
50+
"//c-toxcore/toxcore",
5251
"//c-toxcore/toxcore:tox",
53-
"//c-toxcore/toxcore:tox_dispatch",
54-
"//c-toxcore/toxcore:tox_events",
5552
"//c-toxcore/toxcore:util",
5653
],
5754
)

testing/fuzzing/bootstrap_fuzz_test.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,6 @@ void TestBootstrap(Fuzz_Data &input)
190190
tox_bootstrap(tox, "127.0.0.2", 33446, pub_key, nullptr);
191191
tox_add_tcp_relay(tox, "127.0.0.2", 33446, pub_key, nullptr);
192192

193-
tox_events_init(tox);
194-
195193
Tox_Dispatch *dispatch = tox_dispatch_new(nullptr);
196194
assert(dispatch != nullptr);
197195
setup_callbacks(dispatch);

testing/fuzzing/e2e_fuzz_test.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,6 @@ void TestEndToEnd(Fuzz_Data &input)
202202
assert(error_new == TOX_ERR_NEW_OK);
203203
assert(error_new_testing == TOX_ERR_NEW_TESTING_OK);
204204

205-
tox_events_init(tox);
206-
207205
Tox_Dispatch *dispatch = tox_dispatch_new(nullptr);
208206
assert(dispatch != nullptr);
209207
setup_callbacks(dispatch);

0 commit comments

Comments
 (0)