Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Make event dispatch ordered by receive time. #2392

Merged
merged 3 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ jobs:
- name: Build, test, and upload coverage
run: other/docker/coverage/run

generate-events:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- name: Run generate_event_c
run: |
other/event_tooling/run
git diff --exit-code

cimplefmt:
runs-on: ubuntu-latest
steps:
Expand Down
20 changes: 20 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,24 @@ set(toxcore_SOURCES
toxcore/events/friend_status_message.c
toxcore/events/friend_typing.c
toxcore/events/self_connection_status.c
toxcore/events/group_custom_packet.c
toxcore/events/group_custom_private_packet.c
toxcore/events/group_invite.c
toxcore/events/group_join_fail.c
toxcore/events/group_message.c
toxcore/events/group_moderation.c
toxcore/events/group_password.c
toxcore/events/group_peer_exit.c
toxcore/events/group_peer_join.c
toxcore/events/group_peer_limit.c
toxcore/events/group_peer_name.c
toxcore/events/group_peer_status.c
toxcore/events/group_privacy_state.c
toxcore/events/group_private_message.c
toxcore/events/group_self_join.c
toxcore/events/group_topic.c
toxcore/events/group_topic_lock.c
toxcore/events/group_voice_state.c
toxcore/forwarding.c
toxcore/forwarding.h
toxcore/friend_connection.c
Expand Down Expand Up @@ -313,6 +331,8 @@ set(toxcore_SOURCES
toxcore/tox.c
toxcore/tox_dispatch.c
toxcore/tox_dispatch.h
toxcore/tox_event.c
toxcore/tox_event.h
toxcore/tox_events.c
toxcore/tox_events.h
toxcore/tox.h
Expand Down
8 changes: 4 additions & 4 deletions auto_tests/Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,14 @@ TCP_test_SOURCES = ../auto_tests/TCP_test.c
TCP_test_CFLAGS = $(AUTOTEST_CFLAGS)
TCP_test_LDADD = $(AUTOTEST_LDADD)

tox_events_test_SOURCES = ../auto_tests/tox_events_test.c
tox_events_test_CFLAGS = $(AUTOTEST_CFLAGS)
tox_events_test_LDADD = $(AUTOTEST_LDADD)

tox_dispatch_test_SOURCES = ../auto_tests/tox_dispatch_test.c
tox_dispatch_test_CFLAGS = $(AUTOTEST_CFLAGS)
tox_dispatch_test_LDADD = $(AUTOTEST_LDADD)

tox_events_test_SOURCES = ../auto_tests/tox_events_test.c
tox_events_test_CFLAGS = $(AUTOTEST_CFLAGS)
tox_events_test_LDADD = $(AUTOTEST_LDADD)

tox_many_tcp_test_SOURCES = ../auto_tests/tox_many_tcp_test.c
tox_many_tcp_test_CFLAGS = $(AUTOTEST_CFLAGS)
tox_many_tcp_test_LDADD = $(AUTOTEST_LDADD)
Expand Down
41 changes: 24 additions & 17 deletions auto_tests/tox_dispatch_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,36 @@ static void handle_events_friend_message(Tox *tox, const Tox_Event_Friend_Messag

static void dump_events(const char *path, const Tox_Events *events)
{
if (want_dump_events) {
FILE *fh = fopen(path, "w");
ck_assert(fh != nullptr);
const uint32_t len = tox_events_bytes_size(events);
uint8_t *buf = (uint8_t *)malloc(len);
ck_assert(buf != nullptr);
tox_events_get_bytes(events, buf);
fwrite(buf, 1, len, fh);
free(buf);
fclose(fh);
}
FILE *fh = fopen(path, "w");
ck_assert(fh != nullptr);
const uint32_t len = tox_events_bytes_size(events);
uint8_t *buf = (uint8_t *)malloc(len);
ck_assert(buf != nullptr);
ck_assert(tox_events_get_bytes(events, buf));
fwrite(buf, 1, len, fh);
free(buf);
fclose(fh);
}

static void print_events(const Tox_System *sys, Tox_Events *events)
{
const uint32_t size = tox_events_bytes_size(events);

uint8_t *bytes = (uint8_t *)malloc(size);
ck_assert(bytes != nullptr);
uint8_t *bytes1 = (uint8_t *)malloc(size);
uint8_t *bytes2 = (uint8_t *)malloc(size);
ck_assert(bytes1 != nullptr);
ck_assert(bytes2 != nullptr);

ck_assert(tox_events_get_bytes(events, bytes1));
ck_assert(tox_events_get_bytes(events, bytes2));

tox_events_get_bytes(events, bytes);
// Make sure get_bytes is deterministic.
ck_assert(memcmp(bytes1, bytes2, size) == 0);

Tox_Events *events_copy = tox_events_load(sys, bytes, size);
Tox_Events *events_copy = tox_events_load(sys, bytes1, size);
ck_assert(events_copy != nullptr);
free(bytes);
free(bytes1);
free(bytes2);

ck_assert(tox_events_equal(sys, events, events_copy));

Expand All @@ -74,7 +79,9 @@ static bool await_message(Tox **toxes, const Tox_Dispatch *dispatch)
// Check if tox 2 got the message from tox 1.
Tox_Events *events = tox_events_iterate(toxes[1], false, nullptr);

dump_events("/tmp/test.mp", events);
if (want_dump_events) {
dump_events("/tmp/test.mp", events);
}

bool success = false;
tox_dispatch_invoke(dispatch, events, toxes[1], &success);
Expand Down
15 changes: 13 additions & 2 deletions auto_tests/tox_events_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,23 @@ static bool await_message(Tox **toxes)
Tox_Events *events = tox_events_iterate(toxes[1], false, nullptr);

if (events != nullptr) {
ck_assert(tox_events_get_friend_message_size(events) == 1);
const Tox_Event_Friend_Message *msg_event = tox_events_get_friend_message(events, 0);
uint32_t events_size = tox_events_get_size(events);
ck_assert(events_size == 1);

const Tox_Event_Friend_Message *msg_event = nullptr;
for (uint32_t j = 0; j < events_size; ++j) {
const Tox_Event *ev = tox_events_get(events, j);
if (tox_event_get_type(ev) == TOX_EVENT_FRIEND_MESSAGE) {
msg_event = tox_event_get_friend_message(ev);
}
}

ck_assert(msg_event != nullptr);
ck_assert(tox_event_friend_message_get_message_length(msg_event) == sizeof("hello"));
const uint8_t *msg = tox_event_friend_message_get_message(msg_event);
ck_assert_msg(memcmp(msg, "hello", sizeof("hello")) == 0,
"message was not expected 'hello' but '%s'", (const char *)msg);

tox_events_free(events);
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/docker/tox-bootstrapd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ccaf7a29c16d97068952d872b8663e38cc3d958aff01baeafee04e0ac6f3ac98 /usr/local/bin/tox-bootstrapd
0eb835fe755f1162748029660f46717952f33775e7a845c389ecb4b4e3fbb998 /usr/local/bin/tox-bootstrapd
12 changes: 12 additions & 0 deletions other/event_tooling/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM ubuntu:22.04

RUN apt-get update && apt-get install --no-install-recommends -y \
build-essential \
ca-certificates \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /src
COPY generate_event_c.cpp /src/
RUN ["g++", "generate_event_c.cpp", "-o", "generate_event_c"]
CMD ["./generate_event_c"]
79 changes: 44 additions & 35 deletions other/event_tooling/generate_event_c.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright © 2023 The TokTok team.
// Copyright © 2023-2024 The TokTok team.

// this file can be used to generate event.c files
// requires c++17
Expand Down Expand Up @@ -119,22 +119,54 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
exit(1);
}

bool need_stdlib_h = false;
bool need_string_h = false;
bool need_tox_unpack_h = false;
for (const auto& t : event_types) {
std::visit(
overloaded{
[&](const EventTypeTrivial& t) {
if (bin_unpack_name_from_type(t.type).rfind("tox_", 0) == 0) {
need_tox_unpack_h = true;
}
},
[&](const EventTypeByteRange&) {
need_stdlib_h = true;
need_string_h = true;
}
},
t
);
}

f << R"(/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/

#include "events_alloc.h"

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>)";
if (need_stdlib_h) {
f << R"(
#include <stdlib.h>)";
}
if (need_string_h) {
f << R"(
#include <string.h>)";
}
f << R"(

#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"
#include "../tox_events.h")";
if (need_tox_unpack_h) {
f << R"(
#include "../tox_unpack.h")";
}
f << R"(


/*****************************************************
Expand Down Expand Up @@ -205,10 +237,11 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
f << " " << event_name_l << "->" << t.name_data << " = nullptr;\n";
f << " " << event_name_l << "->" << t.name_length << " = 0;\n";
f << " }\n\n";
f << " " << event_name_l << "->" << t.name_data << " = (uint8_t *)malloc(" << t.name_length << ");\n\n";
f << " if (" << event_name_l << "->" << t.name_data << " == nullptr) {\n";
f << " uint8_t *" << t.name_data << "_copy = (uint8_t *)malloc(" << t.name_length << ");\n\n";
f << " if (" << t.name_data << "_copy == nullptr) {\n";
f << " return false;\n }\n\n";
f << " memcpy(" << event_name_l << "->" << t.name_data << ", " << t.name_data << ", " << t.name_length << ");\n";
f << " memcpy(" << t.name_data << "_copy, " << t.name_data << ", " << t.name_length << ");\n";
f << " " << event_name_l << "->" << t.name_data << " = " << t.name_data << "_copy;\n";
f << " " << event_name_l << "->" << t.name_length << " = " << t.name_length << ";\n";
f << " return true;\n";
}
Expand Down Expand Up @@ -371,28 +404,6 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
f << " tox_events_add(events, &event);\n";
f << " return " << event_name_l << ";\n}\n\n";

// get
f << "const Tox_Event_" << event_name << " *tox_events_get_" << event_name_l << "(const Tox_Events *events, uint32_t index)\n{\n";
f << " uint32_t " << event_name_l << "_index = 0;\n";
f << " const uint32_t size = tox_events_get_size(events);\n\n";
f << " for (uint32_t i = 0; i < size; ++i) {\n";
f << " if (" << event_name_l << "_index > index) {\n";
f << " return nullptr;\n }\n\n";
f << " if (events->events[i].type == TOX_EVENT_" << str_toupper(event_name) << ") {\n";
f << " const Tox_Event_" << event_name << " *" << event_name_l << " = events->events[i].data." << event_name_l << ";\n";
f << " if (" << event_name_l << "_index == index) {\n";
f << " return " << event_name_l << ";\n }\n";
f << " ++" << event_name_l << "_index;\n }\n }\n\n return nullptr;\n}\n\n";

// get size
f << "uint32_t tox_events_get_" << event_name_l << "_size(const Tox_Events *events)\n{\n";
f << " uint32_t " << event_name_l << "_size = 0;\n";
f << " const uint32_t size = tox_events_get_size(events);\n\n";
f << " for (uint32_t i = 0; i < size; ++i) {\n";
f << " if (events->events[i].type == TOX_EVENT_" << str_toupper(event_name) << ") {\n";
f << " ++" << event_name_l << "_size;\n }\n }\n\n";
f << " return " << event_name_l << "_size;\n}\n\n";

// unpack
f << "bool tox_event_" << event_name_l << "_unpack(\n";
f << " Tox_Event_" << event_name << " **event, Bin_Unpack *bu, const Memory *mem)\n{\n";
Expand Down Expand Up @@ -524,7 +535,7 @@ int main(int argc, char** argv) {
EventTypeTrivial{"uint32_t", "file_number"},
EventTypeTrivial{"uint32_t", "kind"},
EventTypeTrivial{"uint64_t", "file_size"},
EventTypeByteRange{"filename", "filename_length", "length"}, // the latter two are ideally the same
EventTypeByteRange{"filename", "filename_length", "filename_length"},
}
},
{
Expand Down Expand Up @@ -626,7 +637,6 @@ int main(int argc, char** argv) {
}
},

#if 0 // not yet :)
{
"Group_Peer_Name",
{
Expand Down Expand Up @@ -768,7 +778,6 @@ int main(int argc, char** argv) {
EventTypeTrivial{"Tox_Group_Mod_Event", "mod_type"},
}
},
#endif
};

if (argc < 2) {
Expand Down
8 changes: 8 additions & 0 deletions other/event_tooling/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh

set -eux
BUILD=events
docker build -t "toxchat/c-toxcore:$BUILD" "other/event_tooling"
docker run --rm -v "$PWD/toxcore/events:/src/out" -t "toxchat/c-toxcore:$BUILD"
sed -i -e 's/, uint16_t length,/, size_t length,/' toxcore/events/file_chunk_request.c
sed -i -e 's/^ 0/ TOX_CONNECTION_NONE/' toxcore/events/self_connection_status.c
13 changes: 10 additions & 3 deletions toxcore/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -918,11 +918,17 @@ cc_library(

cc_library(
name = "tox_events",
srcs = ["tox_events.c"] + glob([
srcs = [
"tox_event.c",
"tox_events.c",
] + glob([
"events/*.c",
"events/*.h",
]),
hdrs = ["tox_events.h"],
hdrs = [
"events/events_alloc.h",
"tox_event.h",
"tox_events.h",
],
visibility = ["//c-toxcore:__subpackages__"],
deps = [
":attributes",
Expand Down Expand Up @@ -955,6 +961,7 @@ cc_fuzz_test(
srcs = ["tox_events_fuzz_test.cc"],
corpus = ["//tools/toktok-fuzzer/corpus:tox_events_fuzz_test"],
deps = [
":tox_dispatch",
":tox_events",
"//c-toxcore/testing/fuzzing:fuzz_support",
],
Expand Down
24 changes: 22 additions & 2 deletions toxcore/Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \
../toxcore/events/conference_peer_list_changed.c \
../toxcore/events/conference_peer_name.c \
../toxcore/events/conference_title.c \
../toxcore/events/events_alloc.c \
../toxcore/events/events_alloc.h \
../toxcore/events/file_chunk_request.c \
../toxcore/events/file_recv.c \
../toxcore/events/file_recv_chunk.c \
Expand All @@ -35,9 +37,25 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \
../toxcore/events/friend_status.c \
../toxcore/events/friend_status_message.c \
../toxcore/events/friend_typing.c \
../toxcore/events/events_alloc.c \
../toxcore/events/events_alloc.h \
../toxcore/events/self_connection_status.c \
../toxcore/events/group_custom_packet.c \
../toxcore/events/group_custom_private_packet.c \
../toxcore/events/group_invite.c \
../toxcore/events/group_join_fail.c \
../toxcore/events/group_message.c \
../toxcore/events/group_moderation.c \
../toxcore/events/group_password.c \
../toxcore/events/group_peer_exit.c \
../toxcore/events/group_peer_join.c \
../toxcore/events/group_peer_limit.c \
../toxcore/events/group_peer_name.c \
../toxcore/events/group_peer_status.c \
../toxcore/events/group_privacy_state.c \
../toxcore/events/group_private_message.c \
../toxcore/events/group_self_join.c \
../toxcore/events/group_topic.c \
../toxcore/events/group_topic_lock.c \
../toxcore/events/group_voice_state.c \
../toxcore/DHT.h \
../toxcore/DHT.c \
../toxcore/mem.h \
Expand Down Expand Up @@ -72,6 +90,8 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \
../toxcore/tox.c \
../toxcore/tox_dispatch.h \
../toxcore/tox_dispatch.c \
../toxcore/tox_event.h \
../toxcore/tox_event.c \
../toxcore/tox_events.h \
../toxcore/tox_events.c \
../toxcore/tox_unpack.h \
Expand Down
Loading
Loading