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 Aug 25, 2023
1 parent 9c26abb commit d40009d
Show file tree
Hide file tree
Showing 36 changed files with 1,431 additions and 3,734 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ set(toxcore_SOURCES
toxcore/events/conference_title.c
toxcore/events/events_alloc.c
toxcore/events/events_alloc.h
toxcore/events/event_macros.h
toxcore/events/file_chunk_request.c
toxcore/events/file_recv.c
toxcore/events/file_recv_chunk.c
Expand Down Expand Up @@ -304,6 +305,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 @@ -30,31 +30,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));

Check warning on line 54 in auto_tests/tox_dispatch_test.c

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

auto_tests/tox_dispatch_test.c#L54

MISRA 17.7 rule

Check warning on line 54 in auto_tests/tox_dispatch_test.c

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

auto_tests/tox_dispatch_test.c#L54

MISRA 21.8 rule

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

Check warning on line 57 in auto_tests/tox_dispatch_test.c

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

auto_tests/tox_dispatch_test.c#L57

MISRA 17.7 rule

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));

Check warning on line 64 in auto_tests/tox_dispatch_test.c

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

auto_tests/tox_dispatch_test.c#L64

MISRA 17.7 rule

Expand All @@ -72,7 +77,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
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 @@
c302aa5ae7f2cb3bf9517c67f7bc33699632d06c3ad2927906ef59f3a88c686d /usr/local/bin/tox-bootstrapd
56e04c693b334e8f797c1a756b6b6bcb016495d129c7acaefd9ee3e009094860 /usr/local/bin/tox-bootstrapd
13 changes: 10 additions & 3 deletions toxcore/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -829,11 +829,18 @@ cc_library(

cc_library(
name = "tox_events",
srcs = ["tox_events.c"] + glob([
srcs = [
"events/event_macros.h",
"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
7 changes: 5 additions & 2 deletions toxcore/Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ 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/event_macros.h \
../toxcore/events/file_chunk_request.c \
../toxcore/events/file_recv.c \
../toxcore/events/file_recv_chunk.c \
Expand All @@ -34,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 @@ -71,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
127 changes: 8 additions & 119 deletions toxcore/events/conference_connected.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "../ccompat.h"
#include "../tox.h"
#include "../tox_events.h"
#include "event_macros.h"


/*****************************************************
Expand All @@ -26,6 +27,8 @@ struct Tox_Event_Conference_Connected {
uint32_t conference_number;
};

EV_ACCESS_VALUE(Conference_Connected, conference_connected, uint32_t, conference_number)

non_null()
static void tox_event_conference_connected_construct(Tox_Event_Conference_Connected *conference_connected)
{
Expand All @@ -34,27 +37,12 @@ static void tox_event_conference_connected_construct(Tox_Event_Conference_Connec
};
}
non_null()
static void tox_event_conference_connected_destruct(Tox_Event_Conference_Connected *conference_connected)
static void tox_event_conference_connected_destruct(Tox_Event_Conference_Connected *conference_connected, const Memory *mem)
{
return;
}

non_null()
static void tox_event_conference_connected_set_conference_number(
Tox_Event_Conference_Connected *conference_connected, uint32_t conference_number)
{
assert(conference_connected != nullptr);
conference_connected->conference_number = conference_number;
}
uint32_t tox_event_conference_connected_get_conference_number(
const Tox_Event_Conference_Connected *conference_connected)
{
assert(conference_connected != nullptr);
return conference_connected->conference_number;
}

non_null()
static bool tox_event_conference_connected_pack(
bool tox_event_conference_connected_pack(
const Tox_Event_Conference_Connected *event, Bin_Pack *bp)
{
assert(event != nullptr);
Expand All @@ -63,104 +51,14 @@ static bool tox_event_conference_connected_pack(
&& bin_pack_u32(bp, event->conference_number);
}

non_null()
static bool tox_event_conference_connected_unpack(
static bool tox_event_conference_connected_unpack_into(
Tox_Event_Conference_Connected *event, Bin_Unpack *bu)
{
assert(event != nullptr);
return bin_unpack_u32(bu, &event->conference_number);
}


/*****************************************************
*
* :: add/clear/get
*
*****************************************************/


non_null()
static Tox_Event_Conference_Connected *tox_events_add_conference_connected(Tox_Events *events)
{
if (events->conference_connected_size == UINT32_MAX) {
return nullptr;
}

if (events->conference_connected_size == events->conference_connected_capacity) {
const uint32_t new_conference_connected_capacity = events->conference_connected_capacity * 2 + 1;
Tox_Event_Conference_Connected *new_conference_connected = (Tox_Event_Conference_Connected *)realloc(
events->conference_connected, new_conference_connected_capacity * sizeof(Tox_Event_Conference_Connected));

if (new_conference_connected == nullptr) {
return nullptr;
}

events->conference_connected = new_conference_connected;
events->conference_connected_capacity = new_conference_connected_capacity;
}

Tox_Event_Conference_Connected *const conference_connected =
&events->conference_connected[events->conference_connected_size];
tox_event_conference_connected_construct(conference_connected);
++events->conference_connected_size;
return conference_connected;
}

void tox_events_clear_conference_connected(Tox_Events *events)
{
if (events == nullptr) {
return;
}

for (uint32_t i = 0; i < events->conference_connected_size; ++i) {
tox_event_conference_connected_destruct(&events->conference_connected[i]);
}

free(events->conference_connected);
events->conference_connected = nullptr;
events->conference_connected_size = 0;
events->conference_connected_capacity = 0;
}

uint32_t tox_events_get_conference_connected_size(const Tox_Events *events)
{
if (events == nullptr) {
return 0;
}

return events->conference_connected_size;
}

const Tox_Event_Conference_Connected *tox_events_get_conference_connected(const Tox_Events *events, uint32_t index)
{
assert(index < events->conference_connected_size);
assert(events->conference_connected != nullptr);
return &events->conference_connected[index];
}

bool tox_events_pack_conference_connected(const Tox_Events *events, Bin_Pack *bp)
{
const uint32_t size = tox_events_get_conference_connected_size(events);

for (uint32_t i = 0; i < size; ++i) {
if (!tox_event_conference_connected_pack(tox_events_get_conference_connected(events, i), bp)) {
return false;
}
}
return true;
}

bool tox_events_unpack_conference_connected(Tox_Events *events, Bin_Unpack *bu)
{
Tox_Event_Conference_Connected *event = tox_events_add_conference_connected(events);

if (event == nullptr) {
return false;
}

return tox_event_conference_connected_unpack(event, bu);
}

EV_FUNCS(Conference_Connected, conference_connected, CONFERENCE_CONNECTED)


/*****************************************************
Expand All @@ -169,20 +67,11 @@ bool tox_events_unpack_conference_connected(Tox_Events *events, Bin_Unpack *bu)
*
*****************************************************/


void tox_events_handle_conference_connected(Tox *tox, uint32_t conference_number, void *user_data)
{
Tox_Events_State *state = tox_events_alloc(user_data);
assert(state != nullptr);

if (state->events == nullptr) {
return;
}

Tox_Event_Conference_Connected *conference_connected = tox_events_add_conference_connected(state->events);
Tox_Event_Conference_Connected *conference_connected = tox_event_conference_connected_alloc(user_data);

if (conference_connected == nullptr) {
state->error = TOX_ERR_EVENTS_ITERATE_MALLOC;
return;
}

Expand Down
Loading

0 comments on commit d40009d

Please sign in to comment.