Skip to content

Commit

Permalink
refactor: Make event dispatch ordered by receive time.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Jan 10, 2024
1 parent 9592d59 commit bfdf8fc
Show file tree
Hide file tree
Showing 38 changed files with 3,180 additions and 2,198 deletions.
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
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,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
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
9 changes: 9 additions & 0 deletions auto_tests/tox_events_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ static bool await_message(Tox **toxes)
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);

const uint32_t event_count = tox_events_get_size(events);
for (uint32_t j = 0; j < event_count; ++j) {
const Tox_Event *event = tox_events_get(events, j);
if (tox_event_get_type(event) == TOX_EVENT_FRIEND_MESSAGE) {
ck_assert(tox_event_get_friend_message(event) == msg_event);
}
}

tox_events_free(events);
return true;
}
Expand Down
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"]
2 changes: 1 addition & 1 deletion other/event_tooling/generate_event_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,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
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
12 changes: 9 additions & 3 deletions toxcore/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -892,11 +892,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
6 changes: 4 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,8 +37,6 @@ 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/DHT.h \
../toxcore/DHT.c \
Expand Down Expand Up @@ -72,6 +72,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

0 comments on commit bfdf8fc

Please sign in to comment.