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 23, 2023
1 parent a1e2458 commit beb0ef9
Show file tree
Hide file tree
Showing 34 changed files with 1,333 additions and 3,680 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 @@ -302,6 +303,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(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(bytes, size);
Tox_Events *events_copy = tox_events_load(bytes1, size);
ck_assert(events_copy != nullptr);
free(bytes);
free(bytes1);
free(bytes2);

ck_assert(tox_events_equal(events, events_copy));

Expand All @@ -70,7 +75,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
13 changes: 10 additions & 3 deletions toxcore/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -799,11 +799,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 @@ -69,6 +70,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
125 changes: 7 additions & 118 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 @@ -39,22 +42,7 @@ static void tox_event_conference_connected_destruct(Tox_Event_Conference_Connect
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 beb0ef9

Please sign in to comment.