Skip to content

Commit e34f7bd

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 e34f7bd

63 files changed

Lines changed: 998 additions & 633 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 & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ cc_library(
2020
"//c-toxcore/toxcore:mono_time",
2121
"//c-toxcore/toxcore:net_crypto",
2222
"//c-toxcore/toxcore:tox",
23-
"//c-toxcore/toxcore:tox_dispatch",
24-
"//c-toxcore/toxcore:tox_events",
23+
"//c-toxcore/toxcore:toxcore",
2524
"//c-toxcore/toxcore:tox_log_level",
2625
],
2726
)
@@ -79,9 +78,7 @@ extra_data = {
7978
"//c-toxcore/toxcore:os_memory",
8079
"//c-toxcore/toxcore:os_random",
8180
"//c-toxcore/toxcore:tox",
82-
"//c-toxcore/toxcore:tox_dispatch",
83-
"//c-toxcore/toxcore:tox_events",
84-
"//c-toxcore/toxcore:tox_unpack",
81+
"//c-toxcore/toxcore:toxcore",
8582
"//c-toxcore/toxcore:util",
8683
"//c-toxcore/toxencryptsave",
8784
"@libsodium",

auto_tests/scenarios/BUILD.bazel

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ cc_library(
1313
"//c-toxcore/toxcore:mono_time",
1414
"//c-toxcore/toxcore:network",
1515
"//c-toxcore/toxcore:tox",
16-
"//c-toxcore/toxcore:tox_dispatch",
17-
"//c-toxcore/toxcore:tox_events",
16+
"//c-toxcore/toxcore:toxcore",
1817
],
1918
)
2019

@@ -26,6 +25,6 @@ cc_library(
2625
":scenario_framework",
2726
"//c-toxcore/toxav",
2827
"//c-toxcore/toxcore:tox",
29-
"//c-toxcore/toxcore:tox_events",
28+
"//c-toxcore/toxcore:toxcore",
3029
],
3130
) 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
@@ -11,6 +11,6 @@ cc_binary(
1111
deps = [
1212
"//c-toxcore/toxcore:ccompat",
1313
"//c-toxcore/toxcore:tox",
14-
"//c-toxcore/toxcore:tox_events",
14+
"//c-toxcore/toxcore:toxcore",
1515
],
1616
)

other/event_tooling/generate_event_c.cpp

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
242242
f << " " << t.type << " " << t.name << ";\n";
243243
},
244244
[&](const EventTypeByteRange& t) {
245-
f << " " << "uint8_t" << " *" << t.name_data << ";\n";
245+
f << " " << t.type_c_arg << " *" << t.name_data << ";\n";
246246
f << " " << "uint32_t" << " " << t.name_length << ";\n";
247247
},
248248
[&](const EventTypeByteArray& t) {
@@ -279,7 +279,7 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
279279
f << " " << t.type << " " << t.name << ")\n";
280280
},
281281
[&](const EventTypeByteRange& t) {
282-
f << "\n const Memory *_Nonnull mem, const uint8_t *_Nullable " << t.name_data << ", uint32_t " << t.name_length << ")\n";
282+
f << "\n const Memory *_Nonnull mem, const " << t.type_c_arg << " *_Nullable " << t.name_data << ", uint32_t " << t.name_length << ")\n";
283283
},
284284
[&](const EventTypeByteArray& t) {
285285
f << " const uint8_t " << t.name << "[" << t.length_constant << "])\n";
@@ -303,9 +303,9 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
303303
f << " assert(" << t.name_length << " == 0);\n";
304304
f << " return true;\n }\n\n";
305305
if (t.null_terminated) {
306-
f << " uint8_t *" << t.name_data << "_copy = (uint8_t *)mem_balloc(mem, " << t.name_length << " + 1);\n\n";
306+
f << " " << t.type_c_arg << " *" << t.name_data << "_copy = (" << t.type_c_arg << " *)mem_balloc(mem, " << t.name_length << " + 1);\n\n";
307307
} else {
308-
f << " uint8_t *" << t.name_data << "_copy = (uint8_t *)mem_balloc(mem, " << t.name_length << ");\n\n";
308+
f << " " << t.type_c_arg << " *" << t.name_data << "_copy = (" << t.type_c_arg << " *)mem_balloc(mem, " << t.name_length << ");\n\n";
309309
}
310310
f << " if (" << t.name_data << "_copy == nullptr) {\n";
311311
f << " return false;\n }\n\n";
@@ -342,7 +342,7 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
342342
f << "(const Tox_Event_" << event_name << " *" << event_name_l << ")\n";
343343
f << "{\n assert(" << event_name_l << " != nullptr);\n";
344344
f << " return " << event_name_l << "->" << t.name_length << ";\n}\n";
345-
f << "const uint8_t *tox_event_" << event_name_l << "_get_" << t.name_data;
345+
f << "const " << t.type_c_arg << " *tox_event_" << event_name_l << "_get_" << t.name_data;
346346
f << "(const Tox_Event_" << event_name << " *" << event_name_l << ")\n";
347347
f << "{\n assert(" << event_name_l << " != nullptr);\n";
348348
f << " return " << event_name_l << "->" << t.name_data << ";\n}\n\n";
@@ -435,7 +435,11 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
435435
}
436436
},
437437
[&](const EventTypeByteRange& t) {
438-
f << "bin_pack_bin(bp, event->" << t.name_data << ", event->" << t.name_length << ")";
438+
if (t.type_c_arg == "char") {
439+
f << "bin_pack_str(bp, event->" << t.name_data << ", event->" << t.name_length << ")";
440+
} else {
441+
f << "bin_pack_bin(bp, event->" << t.name_data << ", event->" << t.name_length << ")";
442+
}
439443
},
440444
[&](const EventTypeByteArray& t) {
441445
f << "bin_pack_bin(bp, event->" << t.name << ", " << t.length_constant << ")";
@@ -471,7 +475,11 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
471475
}
472476
},
473477
[&](const EventTypeByteRange& t) {
474-
f << "bin_unpack_bin(bu, &event->" << t.name_data << ", &event->" << t.name_length << ")";
478+
if (t.type_c_arg == "char") {
479+
f << "bin_unpack_str(bu, &event->" << t.name_data << ", &event->" << t.name_length << ")";
480+
} else {
481+
f << "bin_unpack_bin(bu, &event->" << t.name_data << ", &event->" << t.name_length << ")";
482+
}
475483
},
476484
[&](const EventTypeByteArray& t) {
477485
f << "bin_unpack_bin_fixed(bu, event->" << t.name << ", " << t.length_constant << ")";
@@ -550,28 +558,31 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
550558
551559
)";
552560
f << "void tox_events_handle_" << event_name_l << "(\n";
553-
f << " Tox *tox";
561+
bool first_arg = true;
554562

555563
for (const auto& t : event_types) {
564+
f << " ";
556565
std::visit(
557566
overloaded{
558567
[&](const EventTypeTrivial& t) {
559-
f << ", " << (t.cb_type.empty() ? t.type : t.cb_type) << " " << t.name;
568+
f << (t.cb_type.empty() ? t.type : t.cb_type) << " " << t.name;
560569
},
561570
[&](const EventTypeByteRange& t) {
562-
f << ", const " << t.type_c_arg << " *" << t.name_data << ", " << t.type_length_cb << " " << t.name_length_cb;
571+
f << "const " << t.type_c_arg << " *" << t.name_data << ", " << t.type_length_cb << " " << t.name_length_cb;
563572
},
564573
[&](const EventTypeByteArray& t) {
565-
f << ", const uint8_t *" << t.name;
574+
f << "const uint8_t *" << t.name;
566575
}
567576
},
568577
t
569578
);
579+
f << ",\n";
580+
first_arg = false;
570581
}
571582

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";
583+
f << " Tox_Events_State *state)\n{\n";
584+
f << " Tox_Event_" << event_name << " *" << event_name_l << " = tox_event_" << event_name_l << "_alloc(tox_events_alloc(state));\n\n";
585+
575586
f << " if (" << event_name_l << " == nullptr) {\n return;\n }\n\n";
576587

577588
for (const auto& t : event_types) {
@@ -582,9 +593,6 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
582593
},
583594
[&](const EventTypeByteRange& t) {
584595
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-
}
588596
f << t.name_data << ", " << t.name_length_cb << ");\n";
589597
},
590598
[&](const EventTypeByteArray& t) {

testing/fuzzing/BUILD.bazel

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ cc_fuzz_test(
1010
deps = [
1111
"//c-toxcore/testing/support",
1212
"//c-toxcore/toxcore:tox",
13-
"//c-toxcore/toxcore:tox_dispatch",
14-
"//c-toxcore/toxcore:tox_events",
13+
"//c-toxcore/toxcore:toxcore",
1514
],
1615
)
1716

@@ -26,8 +25,7 @@ cc_fuzz_test(
2625
"//c-toxcore/testing/support",
2726
"//c-toxcore/toxcore:crypto_core",
2827
"//c-toxcore/toxcore:tox",
29-
"//c-toxcore/toxcore:tox_dispatch",
30-
"//c-toxcore/toxcore:tox_events",
28+
"//c-toxcore/toxcore:toxcore",
3129
],
3230
)
3331

@@ -50,8 +48,7 @@ cc_binary(
5048
deps = [
5149
"//c-toxcore/testing/support",
5250
"//c-toxcore/toxcore:tox",
53-
"//c-toxcore/toxcore:tox_dispatch",
54-
"//c-toxcore/toxcore:tox_events",
51+
"//c-toxcore/toxcore:toxcore",
5552
"//c-toxcore/toxcore:util",
5653
],
5754
)

testing/fuzzing/bootstrap_fuzz_test.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +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);
194193

195194
Tox_Dispatch *dispatch = tox_dispatch_new(nullptr);
196195
assert(dispatch != nullptr);

testing/fuzzing/e2e_fuzz_test.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +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);
206205

207206
Tox_Dispatch *dispatch = tox_dispatch_new(nullptr);
208207
assert(dispatch != nullptr);

testing/fuzzing/protodump.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,6 @@ void RecordBootstrap(const char *init, const char *bootstrap)
262262
const bool udp_success = tox_bootstrap(tox2, "127.0.0.1", 33445, dht_key1.data(), nullptr);
263263
assert(udp_success);
264264

265-
tox_events_init(tox1);
266-
tox_events_init(tox2);
267265

268266
Tox_Dispatch *dispatch = tox_dispatch_new(nullptr);
269267
setup_callbacks(dispatch);

0 commit comments

Comments
 (0)