diff --git a/CMakeLists.txt b/CMakeLists.txt index d3cabf06c66..04c58f8bab6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 @@ -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 diff --git a/auto_tests/tox_dispatch_test.c b/auto_tests/tox_dispatch_test.c index 209e13788f2..7bb2906eb6e 100644 --- a/auto_tests/tox_dispatch_test.c +++ b/auto_tests/tox_dispatch_test.c @@ -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)); - 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)); @@ -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); diff --git a/auto_tests/tox_events_test.c b/auto_tests/tox_events_test.c index 04549b2179c..918bc17ec0e 100644 --- a/auto_tests/tox_events_test.c +++ b/auto_tests/tox_events_test.c @@ -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; } diff --git a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 index 71bc96b0450..79f9865c409 100644 --- a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 +++ b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 @@ -1 +1 @@ -6f4ec4239de5ea05ae341271bbda4cb78535f943a693321648ee1ab043a203d2 /usr/local/bin/tox-bootstrapd +ac31a168ba99d87f60abb011ad882ee60d4333148b5d06d738e5d748f1ef080d /usr/local/bin/tox-bootstrapd diff --git a/toxcore/BUILD.bazel b/toxcore/BUILD.bazel index 4701cf437d3..efb0b5eb76a 100644 --- a/toxcore/BUILD.bazel +++ b/toxcore/BUILD.bazel @@ -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", diff --git a/toxcore/Makefile.inc b/toxcore/Makefile.inc index 70df82b39e6..ab677a5ad40 100644 --- a/toxcore/Makefile.inc +++ b/toxcore/Makefile.inc @@ -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 \ @@ -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 \ @@ -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 \ diff --git a/toxcore/events/conference_connected.c b/toxcore/events/conference_connected.c index ce278755e10..ba10f99313f 100644 --- a/toxcore/events/conference_connected.c +++ b/toxcore/events/conference_connected.c @@ -13,6 +13,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "event_macros.h" /***************************************************** @@ -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) { @@ -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); @@ -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) /***************************************************** @@ -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; } diff --git a/toxcore/events/conference_invite.c b/toxcore/events/conference_invite.c index 1d88f41d768..28813997a47 100644 --- a/toxcore/events/conference_invite.c +++ b/toxcore/events/conference_invite.c @@ -13,7 +13,9 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_private.h" #include "../tox_unpack.h" +#include "event_macros.h" /***************************************************** @@ -30,6 +32,10 @@ struct Tox_Event_Conference_Invite { uint32_t cookie_length; }; +EV_ACCESS_VALUE(Conference_Invite, conference_invite, uint32_t, friend_number) +EV_ACCESS_VALUE(Conference_Invite, conference_invite, Tox_Conference_Type, type) +EV_ACCESS_ARRAY(Conference_Invite, conference_invite, uint8_t, cookie) + non_null() static void tox_event_conference_invite_construct(Tox_Event_Conference_Invite *conference_invite) { @@ -38,72 +44,12 @@ static void tox_event_conference_invite_construct(Tox_Event_Conference_Invite *c }; } non_null() -static void tox_event_conference_invite_destruct(Tox_Event_Conference_Invite *conference_invite) -{ - free(conference_invite->cookie); -} - -non_null() -static void tox_event_conference_invite_set_friend_number(Tox_Event_Conference_Invite *conference_invite, - uint32_t friend_number) -{ - assert(conference_invite != nullptr); - conference_invite->friend_number = friend_number; -} -uint32_t tox_event_conference_invite_get_friend_number(const Tox_Event_Conference_Invite *conference_invite) -{ - assert(conference_invite != nullptr); - return conference_invite->friend_number; -} - -non_null() -static void tox_event_conference_invite_set_type(Tox_Event_Conference_Invite *conference_invite, - Tox_Conference_Type type) -{ - assert(conference_invite != nullptr); - conference_invite->type = type; -} -Tox_Conference_Type tox_event_conference_invite_get_type(const Tox_Event_Conference_Invite *conference_invite) +static void tox_event_conference_invite_destruct(Tox_Event_Conference_Invite *conference_invite, const Memory *mem) { - assert(conference_invite != nullptr); - return conference_invite->type; + mem_delete(mem, conference_invite->cookie); } -non_null() -static bool tox_event_conference_invite_set_cookie(Tox_Event_Conference_Invite *conference_invite, - const uint8_t *cookie, uint32_t cookie_length) -{ - assert(conference_invite != nullptr); - - if (conference_invite->cookie != nullptr) { - free(conference_invite->cookie); - conference_invite->cookie = nullptr; - conference_invite->cookie_length = 0; - } - - conference_invite->cookie = (uint8_t *)malloc(cookie_length); - - if (conference_invite->cookie == nullptr) { - return false; - } - - memcpy(conference_invite->cookie, cookie, cookie_length); - conference_invite->cookie_length = cookie_length; - return true; -} -uint32_t tox_event_conference_invite_get_cookie_length(const Tox_Event_Conference_Invite *conference_invite) -{ - assert(conference_invite != nullptr); - return conference_invite->cookie_length; -} -const uint8_t *tox_event_conference_invite_get_cookie(const Tox_Event_Conference_Invite *conference_invite) -{ - assert(conference_invite != nullptr); - return conference_invite->cookie; -} - -non_null() -static bool tox_event_conference_invite_pack( +bool tox_event_conference_invite_pack( const Tox_Event_Conference_Invite *event, Bin_Pack *bp) { assert(event != nullptr); @@ -116,7 +62,7 @@ static bool tox_event_conference_invite_pack( } non_null() -static bool tox_event_conference_invite_unpack( +static bool tox_event_conference_invite_unpack_into( Tox_Event_Conference_Invite *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -129,94 +75,7 @@ static bool tox_event_conference_invite_unpack( && bin_unpack_bin(bu, &event->cookie, &event->cookie_length); } - -/***************************************************** - * - * :: add/clear/get - * - *****************************************************/ - - -non_null() -static Tox_Event_Conference_Invite *tox_events_add_conference_invite(Tox_Events *events) -{ - if (events->conference_invite_size == UINT32_MAX) { - return nullptr; - } - - if (events->conference_invite_size == events->conference_invite_capacity) { - const uint32_t new_conference_invite_capacity = events->conference_invite_capacity * 2 + 1; - Tox_Event_Conference_Invite *new_conference_invite = (Tox_Event_Conference_Invite *)realloc( - events->conference_invite, new_conference_invite_capacity * sizeof(Tox_Event_Conference_Invite)); - - if (new_conference_invite == nullptr) { - return nullptr; - } - - events->conference_invite = new_conference_invite; - events->conference_invite_capacity = new_conference_invite_capacity; - } - - Tox_Event_Conference_Invite *const conference_invite = &events->conference_invite[events->conference_invite_size]; - tox_event_conference_invite_construct(conference_invite); - ++events->conference_invite_size; - return conference_invite; -} - -void tox_events_clear_conference_invite(Tox_Events *events) -{ - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->conference_invite_size; ++i) { - tox_event_conference_invite_destruct(&events->conference_invite[i]); - } - - free(events->conference_invite); - events->conference_invite = nullptr; - events->conference_invite_size = 0; - events->conference_invite_capacity = 0; -} - -uint32_t tox_events_get_conference_invite_size(const Tox_Events *events) -{ - if (events == nullptr) { - return 0; - } - - return events->conference_invite_size; -} - -const Tox_Event_Conference_Invite *tox_events_get_conference_invite(const Tox_Events *events, uint32_t index) -{ - assert(index < events->conference_invite_size); - assert(events->conference_invite != nullptr); - return &events->conference_invite[index]; -} - -bool tox_events_pack_conference_invite(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_conference_invite_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_conference_invite_pack(tox_events_get_conference_invite(events, i), bp)) { - return false; - } - } - return true; -} - -bool tox_events_unpack_conference_invite(Tox_Events *events, Bin_Unpack *bu) -{ - Tox_Event_Conference_Invite *event = tox_events_add_conference_invite(events); - - if (event == nullptr) { - return false; - } - - return tox_event_conference_invite_unpack(event, bu); -} +EV_FUNCS(Conference_Invite, conference_invite, CONFERENCE_INVITE) /***************************************************** @@ -229,21 +88,15 @@ bool tox_events_unpack_conference_invite(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_conference_invite(Tox *tox, uint32_t friend_number, Tox_Conference_Type type, const uint8_t *cookie, size_t length, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Conference_Invite *conference_invite = tox_events_add_conference_invite(state->events); + Tox_Event_Conference_Invite *conference_invite = tox_event_conference_invite_alloc(user_data); if (conference_invite == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } + const Tox_System *sys = tox_get_system(tox); + tox_event_conference_invite_set_friend_number(conference_invite, friend_number); tox_event_conference_invite_set_type(conference_invite, type); - tox_event_conference_invite_set_cookie(conference_invite, cookie, length); + tox_event_conference_invite_set_cookie(conference_invite, cookie, length, sys->mem); } diff --git a/toxcore/events/conference_message.c b/toxcore/events/conference_message.c index bc9a19b7381..c6e114d41f8 100644 --- a/toxcore/events/conference_message.c +++ b/toxcore/events/conference_message.c @@ -13,7 +13,9 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_private.h" #include "../tox_unpack.h" +#include "event_macros.h" /***************************************************** @@ -31,6 +33,11 @@ struct Tox_Event_Conference_Message { uint32_t message_length; }; +EV_ACCESS_VALUE(Conference_Message, conference_message, uint32_t, conference_number) +EV_ACCESS_VALUE(Conference_Message, conference_message, uint32_t, peer_number) +EV_ACCESS_VALUE(Conference_Message, conference_message, Tox_Message_Type, type) +EV_ACCESS_ARRAY(Conference_Message, conference_message, uint8_t, message) + non_null() static void tox_event_conference_message_construct(Tox_Event_Conference_Message *conference_message) { @@ -39,85 +46,12 @@ static void tox_event_conference_message_construct(Tox_Event_Conference_Message }; } non_null() -static void tox_event_conference_message_destruct(Tox_Event_Conference_Message *conference_message) -{ - free(conference_message->message); -} - -non_null() -static void tox_event_conference_message_set_conference_number(Tox_Event_Conference_Message *conference_message, - uint32_t conference_number) -{ - assert(conference_message != nullptr); - conference_message->conference_number = conference_number; -} -uint32_t tox_event_conference_message_get_conference_number(const Tox_Event_Conference_Message *conference_message) -{ - assert(conference_message != nullptr); - return conference_message->conference_number; -} - -non_null() -static void tox_event_conference_message_set_peer_number(Tox_Event_Conference_Message *conference_message, - uint32_t peer_number) -{ - assert(conference_message != nullptr); - conference_message->peer_number = peer_number; -} -uint32_t tox_event_conference_message_get_peer_number(const Tox_Event_Conference_Message *conference_message) -{ - assert(conference_message != nullptr); - return conference_message->peer_number; -} - -non_null() -static void tox_event_conference_message_set_type(Tox_Event_Conference_Message *conference_message, - Tox_Message_Type type) -{ - assert(conference_message != nullptr); - conference_message->type = type; -} -Tox_Message_Type tox_event_conference_message_get_type(const Tox_Event_Conference_Message *conference_message) -{ - assert(conference_message != nullptr); - return conference_message->type; -} - -non_null() -static bool tox_event_conference_message_set_message(Tox_Event_Conference_Message *conference_message, - const uint8_t *message, uint32_t message_length) -{ - assert(conference_message != nullptr); - - if (conference_message->message != nullptr) { - free(conference_message->message); - conference_message->message = nullptr; - conference_message->message_length = 0; - } - - conference_message->message = (uint8_t *)malloc(message_length); - - if (conference_message->message == nullptr) { - return false; - } - - memcpy(conference_message->message, message, message_length); - conference_message->message_length = message_length; - return true; -} -uint32_t tox_event_conference_message_get_message_length(const Tox_Event_Conference_Message *conference_message) +static void tox_event_conference_message_destruct(Tox_Event_Conference_Message *conference_message, const Memory *mem) { - assert(conference_message != nullptr); - return conference_message->message_length; -} -const uint8_t *tox_event_conference_message_get_message(const Tox_Event_Conference_Message *conference_message) -{ - assert(conference_message != nullptr); - return conference_message->message; + mem_delete(mem, conference_message->message); } -non_null() -static bool tox_event_conference_message_pack( +bool tox_event_conference_message_pack( const Tox_Event_Conference_Message *event, Bin_Pack *bp) { assert(event != nullptr); @@ -131,7 +65,7 @@ static bool tox_event_conference_message_pack( } non_null() -static bool tox_event_conference_message_unpack( +static bool tox_event_conference_message_unpack_into( Tox_Event_Conference_Message *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -145,94 +79,7 @@ static bool tox_event_conference_message_unpack( && bin_unpack_bin(bu, &event->message, &event->message_length); } - -/***************************************************** - * - * :: add/clear/get - * - *****************************************************/ - - -non_null() -static Tox_Event_Conference_Message *tox_events_add_conference_message(Tox_Events *events) -{ - if (events->conference_message_size == UINT32_MAX) { - return nullptr; - } - - if (events->conference_message_size == events->conference_message_capacity) { - const uint32_t new_conference_message_capacity = events->conference_message_capacity * 2 + 1; - Tox_Event_Conference_Message *new_conference_message = (Tox_Event_Conference_Message *)realloc( - events->conference_message, new_conference_message_capacity * sizeof(Tox_Event_Conference_Message)); - - if (new_conference_message == nullptr) { - return nullptr; - } - - events->conference_message = new_conference_message; - events->conference_message_capacity = new_conference_message_capacity; - } - - Tox_Event_Conference_Message *const conference_message = &events->conference_message[events->conference_message_size]; - tox_event_conference_message_construct(conference_message); - ++events->conference_message_size; - return conference_message; -} - -void tox_events_clear_conference_message(Tox_Events *events) -{ - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->conference_message_size; ++i) { - tox_event_conference_message_destruct(&events->conference_message[i]); - } - - free(events->conference_message); - events->conference_message = nullptr; - events->conference_message_size = 0; - events->conference_message_capacity = 0; -} - -uint32_t tox_events_get_conference_message_size(const Tox_Events *events) -{ - if (events == nullptr) { - return 0; - } - - return events->conference_message_size; -} - -const Tox_Event_Conference_Message *tox_events_get_conference_message(const Tox_Events *events, uint32_t index) -{ - assert(index < events->conference_message_size); - assert(events->conference_message != nullptr); - return &events->conference_message[index]; -} - -bool tox_events_pack_conference_message(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_conference_message_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_conference_message_pack(tox_events_get_conference_message(events, i), bp)) { - return false; - } - } - return true; -} - -bool tox_events_unpack_conference_message(Tox_Events *events, Bin_Unpack *bu) -{ - Tox_Event_Conference_Message *event = tox_events_add_conference_message(events); - - if (event == nullptr) { - return false; - } - - return tox_event_conference_message_unpack(event, bu); -} +EV_FUNCS(Conference_Message, conference_message, CONFERENCE_MESSAGE) /***************************************************** @@ -245,22 +92,16 @@ bool tox_events_unpack_conference_message(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_conference_message(Tox *tox, uint32_t conference_number, uint32_t peer_number, Tox_Message_Type type, const uint8_t *message, size_t length, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Conference_Message *conference_message = tox_events_add_conference_message(state->events); + Tox_Event_Conference_Message *conference_message = tox_event_conference_message_alloc(user_data); if (conference_message == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } + const Tox_System *sys = tox_get_system(tox); + tox_event_conference_message_set_conference_number(conference_message, conference_number); tox_event_conference_message_set_peer_number(conference_message, peer_number); tox_event_conference_message_set_type(conference_message, type); - tox_event_conference_message_set_message(conference_message, message, length); + tox_event_conference_message_set_message(conference_message, message, length, sys->mem); } diff --git a/toxcore/events/conference_peer_list_changed.c b/toxcore/events/conference_peer_list_changed.c index b6aaa3bf7a3..6c7926cef6b 100644 --- a/toxcore/events/conference_peer_list_changed.c +++ b/toxcore/events/conference_peer_list_changed.c @@ -13,6 +13,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "event_macros.h" /***************************************************** @@ -26,6 +27,8 @@ struct Tox_Event_Conference_Peer_List_Changed { uint32_t conference_number; }; +EV_ACCESS_VALUE(Conference_Peer_List_Changed, conference_peer_list_changed, uint32_t, conference_number) + non_null() static void tox_event_conference_peer_list_changed_construct(Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed) @@ -35,28 +38,12 @@ static void tox_event_conference_peer_list_changed_construct(Tox_Event_Conferenc }; } non_null() -static void tox_event_conference_peer_list_changed_destruct(Tox_Event_Conference_Peer_List_Changed - *conference_peer_list_changed) +static void tox_event_conference_peer_list_changed_destruct(Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed, const Memory *mem) { return; } -non_null() -static void tox_event_conference_peer_list_changed_set_conference_number(Tox_Event_Conference_Peer_List_Changed - *conference_peer_list_changed, uint32_t conference_number) -{ - assert(conference_peer_list_changed != nullptr); - conference_peer_list_changed->conference_number = conference_number; -} -uint32_t tox_event_conference_peer_list_changed_get_conference_number(const Tox_Event_Conference_Peer_List_Changed - *conference_peer_list_changed) -{ - assert(conference_peer_list_changed != nullptr); - return conference_peer_list_changed->conference_number; -} - -non_null() -static bool tox_event_conference_peer_list_changed_pack( +bool tox_event_conference_peer_list_changed_pack( const Tox_Event_Conference_Peer_List_Changed *event, Bin_Pack *bp) { assert(event != nullptr); @@ -66,105 +53,14 @@ static bool tox_event_conference_peer_list_changed_pack( } non_null() -static bool tox_event_conference_peer_list_changed_unpack( +static bool tox_event_conference_peer_list_changed_unpack_into( Tox_Event_Conference_Peer_List_Changed *event, Bin_Unpack *bu) { assert(event != nullptr); return bin_unpack_u32(bu, &event->conference_number); } - -/***************************************************** - * - * :: add/clear/get - * - *****************************************************/ - - -non_null() -static Tox_Event_Conference_Peer_List_Changed *tox_events_add_conference_peer_list_changed(Tox_Events *events) -{ - if (events->conference_peer_list_changed_size == UINT32_MAX) { - return nullptr; - } - - if (events->conference_peer_list_changed_size == events->conference_peer_list_changed_capacity) { - const uint32_t new_conference_peer_list_changed_capacity = events->conference_peer_list_changed_capacity * 2 + 1; - Tox_Event_Conference_Peer_List_Changed *new_conference_peer_list_changed = (Tox_Event_Conference_Peer_List_Changed *) - realloc( - events->conference_peer_list_changed, - new_conference_peer_list_changed_capacity * sizeof(Tox_Event_Conference_Peer_List_Changed)); - - if (new_conference_peer_list_changed == nullptr) { - return nullptr; - } - - events->conference_peer_list_changed = new_conference_peer_list_changed; - events->conference_peer_list_changed_capacity = new_conference_peer_list_changed_capacity; - } - - Tox_Event_Conference_Peer_List_Changed *const conference_peer_list_changed = - &events->conference_peer_list_changed[events->conference_peer_list_changed_size]; - tox_event_conference_peer_list_changed_construct(conference_peer_list_changed); - ++events->conference_peer_list_changed_size; - return conference_peer_list_changed; -} - -void tox_events_clear_conference_peer_list_changed(Tox_Events *events) -{ - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->conference_peer_list_changed_size; ++i) { - tox_event_conference_peer_list_changed_destruct(&events->conference_peer_list_changed[i]); - } - - free(events->conference_peer_list_changed); - events->conference_peer_list_changed = nullptr; - events->conference_peer_list_changed_size = 0; - events->conference_peer_list_changed_capacity = 0; -} - -uint32_t tox_events_get_conference_peer_list_changed_size(const Tox_Events *events) -{ - if (events == nullptr) { - return 0; - } - - return events->conference_peer_list_changed_size; -} - -const Tox_Event_Conference_Peer_List_Changed *tox_events_get_conference_peer_list_changed(const Tox_Events *events, - uint32_t index) -{ - assert(index < events->conference_peer_list_changed_size); - assert(events->conference_peer_list_changed != nullptr); - return &events->conference_peer_list_changed[index]; -} - -bool tox_events_pack_conference_peer_list_changed(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_conference_peer_list_changed_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_conference_peer_list_changed_pack(tox_events_get_conference_peer_list_changed(events, i), bp)) { - return false; - } - } - return true; -} - -bool tox_events_unpack_conference_peer_list_changed(Tox_Events *events, Bin_Unpack *bu) -{ - Tox_Event_Conference_Peer_List_Changed *event = tox_events_add_conference_peer_list_changed(events); - - if (event == nullptr) { - return false; - } - - return tox_event_conference_peer_list_changed_unpack(event, bu); -} +EV_FUNCS(Conference_Peer_List_Changed, conference_peer_list_changed, CONFERENCE_PEER_LIST_CHANGED) /***************************************************** @@ -176,18 +72,9 @@ bool tox_events_unpack_conference_peer_list_changed(Tox_Events *events, Bin_Unpa void tox_events_handle_conference_peer_list_changed(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_Peer_List_Changed *conference_peer_list_changed = tox_events_add_conference_peer_list_changed( - state->events); + Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed = tox_event_conference_peer_list_changed_alloc(user_data); if (conference_peer_list_changed == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/conference_peer_name.c b/toxcore/events/conference_peer_name.c index 4d3f285eee7..18d3429146d 100644 --- a/toxcore/events/conference_peer_name.c +++ b/toxcore/events/conference_peer_name.c @@ -13,6 +13,8 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_private.h" +#include "event_macros.h" /***************************************************** @@ -29,6 +31,10 @@ struct Tox_Event_Conference_Peer_Name { uint32_t name_length; }; +EV_ACCESS_VALUE(Conference_Peer_Name, conference_peer_name, uint32_t, conference_number) +EV_ACCESS_VALUE(Conference_Peer_Name, conference_peer_name, uint32_t, peer_number) +EV_ACCESS_ARRAY(Conference_Peer_Name, conference_peer_name, uint8_t, name) + non_null() static void tox_event_conference_peer_name_construct(Tox_Event_Conference_Peer_Name *conference_peer_name) { @@ -37,73 +43,12 @@ static void tox_event_conference_peer_name_construct(Tox_Event_Conference_Peer_N }; } non_null() -static void tox_event_conference_peer_name_destruct(Tox_Event_Conference_Peer_Name *conference_peer_name) -{ - free(conference_peer_name->name); -} - -non_null() -static void tox_event_conference_peer_name_set_conference_number(Tox_Event_Conference_Peer_Name *conference_peer_name, - uint32_t conference_number) -{ - assert(conference_peer_name != nullptr); - conference_peer_name->conference_number = conference_number; -} -uint32_t tox_event_conference_peer_name_get_conference_number(const Tox_Event_Conference_Peer_Name - *conference_peer_name) -{ - assert(conference_peer_name != nullptr); - return conference_peer_name->conference_number; -} - -non_null() -static void tox_event_conference_peer_name_set_peer_number(Tox_Event_Conference_Peer_Name *conference_peer_name, - uint32_t peer_number) -{ - assert(conference_peer_name != nullptr); - conference_peer_name->peer_number = peer_number; -} -uint32_t tox_event_conference_peer_name_get_peer_number(const Tox_Event_Conference_Peer_Name *conference_peer_name) +static void tox_event_conference_peer_name_destruct(Tox_Event_Conference_Peer_Name *conference_peer_name, const Memory *mem) { - assert(conference_peer_name != nullptr); - return conference_peer_name->peer_number; + mem_delete(mem, conference_peer_name->name); } -non_null() -static bool tox_event_conference_peer_name_set_name(Tox_Event_Conference_Peer_Name *conference_peer_name, - const uint8_t *name, uint32_t name_length) -{ - assert(conference_peer_name != nullptr); - - if (conference_peer_name->name != nullptr) { - free(conference_peer_name->name); - conference_peer_name->name = nullptr; - conference_peer_name->name_length = 0; - } - - conference_peer_name->name = (uint8_t *)malloc(name_length); - - if (conference_peer_name->name == nullptr) { - return false; - } - - memcpy(conference_peer_name->name, name, name_length); - conference_peer_name->name_length = name_length; - return true; -} -uint32_t tox_event_conference_peer_name_get_name_length(const Tox_Event_Conference_Peer_Name *conference_peer_name) -{ - assert(conference_peer_name != nullptr); - return conference_peer_name->name_length; -} -const uint8_t *tox_event_conference_peer_name_get_name(const Tox_Event_Conference_Peer_Name *conference_peer_name) -{ - assert(conference_peer_name != nullptr); - return conference_peer_name->name; -} - -non_null() -static bool tox_event_conference_peer_name_pack( +bool tox_event_conference_peer_name_pack( const Tox_Event_Conference_Peer_Name *event, Bin_Pack *bp) { assert(event != nullptr); @@ -116,7 +61,7 @@ static bool tox_event_conference_peer_name_pack( } non_null() -static bool tox_event_conference_peer_name_unpack( +static bool tox_event_conference_peer_name_unpack_into( Tox_Event_Conference_Peer_Name *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -129,95 +74,7 @@ static bool tox_event_conference_peer_name_unpack( && bin_unpack_bin(bu, &event->name, &event->name_length); } - -/***************************************************** - * - * :: add/clear/get - * - *****************************************************/ - - -non_null() -static Tox_Event_Conference_Peer_Name *tox_events_add_conference_peer_name(Tox_Events *events) -{ - if (events->conference_peer_name_size == UINT32_MAX) { - return nullptr; - } - - if (events->conference_peer_name_size == events->conference_peer_name_capacity) { - const uint32_t new_conference_peer_name_capacity = events->conference_peer_name_capacity * 2 + 1; - Tox_Event_Conference_Peer_Name *new_conference_peer_name = (Tox_Event_Conference_Peer_Name *)realloc( - events->conference_peer_name, new_conference_peer_name_capacity * sizeof(Tox_Event_Conference_Peer_Name)); - - if (new_conference_peer_name == nullptr) { - return nullptr; - } - - events->conference_peer_name = new_conference_peer_name; - events->conference_peer_name_capacity = new_conference_peer_name_capacity; - } - - Tox_Event_Conference_Peer_Name *const conference_peer_name = - &events->conference_peer_name[events->conference_peer_name_size]; - tox_event_conference_peer_name_construct(conference_peer_name); - ++events->conference_peer_name_size; - return conference_peer_name; -} - -void tox_events_clear_conference_peer_name(Tox_Events *events) -{ - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->conference_peer_name_size; ++i) { - tox_event_conference_peer_name_destruct(&events->conference_peer_name[i]); - } - - free(events->conference_peer_name); - events->conference_peer_name = nullptr; - events->conference_peer_name_size = 0; - events->conference_peer_name_capacity = 0; -} - -uint32_t tox_events_get_conference_peer_name_size(const Tox_Events *events) -{ - if (events == nullptr) { - return 0; - } - - return events->conference_peer_name_size; -} - -const Tox_Event_Conference_Peer_Name *tox_events_get_conference_peer_name(const Tox_Events *events, uint32_t index) -{ - assert(index < events->conference_peer_name_size); - assert(events->conference_peer_name != nullptr); - return &events->conference_peer_name[index]; -} - -bool tox_events_pack_conference_peer_name(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_conference_peer_name_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_conference_peer_name_pack(tox_events_get_conference_peer_name(events, i), bp)) { - return false; - } - } - return true; -} - -bool tox_events_unpack_conference_peer_name(Tox_Events *events, Bin_Unpack *bu) -{ - Tox_Event_Conference_Peer_Name *event = tox_events_add_conference_peer_name(events); - - if (event == nullptr) { - return false; - } - - return tox_event_conference_peer_name_unpack(event, bu); -} +EV_FUNCS(Conference_Peer_Name, conference_peer_name, CONFERENCE_PEER_NAME) /***************************************************** @@ -230,21 +87,15 @@ bool tox_events_unpack_conference_peer_name(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_conference_peer_name(Tox *tox, uint32_t conference_number, uint32_t peer_number, const uint8_t *name, size_t length, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Conference_Peer_Name *conference_peer_name = tox_events_add_conference_peer_name(state->events); + Tox_Event_Conference_Peer_Name *conference_peer_name = tox_event_conference_peer_name_alloc(user_data); if (conference_peer_name == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } + const Tox_System *sys = tox_get_system(tox); + tox_event_conference_peer_name_set_conference_number(conference_peer_name, conference_number); tox_event_conference_peer_name_set_peer_number(conference_peer_name, peer_number); - tox_event_conference_peer_name_set_name(conference_peer_name, name, length); + tox_event_conference_peer_name_set_name(conference_peer_name, name, length, sys->mem); } diff --git a/toxcore/events/conference_title.c b/toxcore/events/conference_title.c index 1866966e9f3..09dff82c20f 100644 --- a/toxcore/events/conference_title.c +++ b/toxcore/events/conference_title.c @@ -13,6 +13,8 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_private.h" +#include "event_macros.h" /***************************************************** @@ -29,6 +31,10 @@ struct Tox_Event_Conference_Title { uint32_t title_length; }; +EV_ACCESS_VALUE(Conference_Title, conference_title, uint32_t, conference_number) +EV_ACCESS_VALUE(Conference_Title, conference_title, uint32_t, peer_number) +EV_ACCESS_ARRAY(Conference_Title, conference_title, uint8_t, title) + non_null() static void tox_event_conference_title_construct(Tox_Event_Conference_Title *conference_title) { @@ -37,72 +43,12 @@ static void tox_event_conference_title_construct(Tox_Event_Conference_Title *con }; } non_null() -static void tox_event_conference_title_destruct(Tox_Event_Conference_Title *conference_title) -{ - free(conference_title->title); -} - -non_null() -static void tox_event_conference_title_set_conference_number(Tox_Event_Conference_Title *conference_title, - uint32_t conference_number) -{ - assert(conference_title != nullptr); - conference_title->conference_number = conference_number; -} -uint32_t tox_event_conference_title_get_conference_number(const Tox_Event_Conference_Title *conference_title) -{ - assert(conference_title != nullptr); - return conference_title->conference_number; -} - -non_null() -static void tox_event_conference_title_set_peer_number(Tox_Event_Conference_Title *conference_title, - uint32_t peer_number) -{ - assert(conference_title != nullptr); - conference_title->peer_number = peer_number; -} -uint32_t tox_event_conference_title_get_peer_number(const Tox_Event_Conference_Title *conference_title) +static void tox_event_conference_title_destruct(Tox_Event_Conference_Title *conference_title, const Memory *mem) { - assert(conference_title != nullptr); - return conference_title->peer_number; + mem_delete(mem, conference_title->title); } -non_null() -static bool tox_event_conference_title_set_title(Tox_Event_Conference_Title *conference_title, const uint8_t *title, - uint32_t title_length) -{ - assert(conference_title != nullptr); - - if (conference_title->title != nullptr) { - free(conference_title->title); - conference_title->title = nullptr; - conference_title->title_length = 0; - } - - conference_title->title = (uint8_t *)malloc(title_length); - - if (conference_title->title == nullptr) { - return false; - } - - memcpy(conference_title->title, title, title_length); - conference_title->title_length = title_length; - return true; -} -uint32_t tox_event_conference_title_get_title_length(const Tox_Event_Conference_Title *conference_title) -{ - assert(conference_title != nullptr); - return conference_title->title_length; -} -const uint8_t *tox_event_conference_title_get_title(const Tox_Event_Conference_Title *conference_title) -{ - assert(conference_title != nullptr); - return conference_title->title; -} - -non_null() -static bool tox_event_conference_title_pack( +bool tox_event_conference_title_pack( const Tox_Event_Conference_Title *event, Bin_Pack *bp) { assert(event != nullptr); @@ -115,7 +61,7 @@ static bool tox_event_conference_title_pack( } non_null() -static bool tox_event_conference_title_unpack( +static bool tox_event_conference_title_unpack_into( Tox_Event_Conference_Title *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -128,94 +74,7 @@ static bool tox_event_conference_title_unpack( && bin_unpack_bin(bu, &event->title, &event->title_length); } - -/***************************************************** - * - * :: add/clear/get - * - *****************************************************/ - - -non_null() -static Tox_Event_Conference_Title *tox_events_add_conference_title(Tox_Events *events) -{ - if (events->conference_title_size == UINT32_MAX) { - return nullptr; - } - - if (events->conference_title_size == events->conference_title_capacity) { - const uint32_t new_conference_title_capacity = events->conference_title_capacity * 2 + 1; - Tox_Event_Conference_Title *new_conference_title = (Tox_Event_Conference_Title *)realloc( - events->conference_title, new_conference_title_capacity * sizeof(Tox_Event_Conference_Title)); - - if (new_conference_title == nullptr) { - return nullptr; - } - - events->conference_title = new_conference_title; - events->conference_title_capacity = new_conference_title_capacity; - } - - Tox_Event_Conference_Title *const conference_title = &events->conference_title[events->conference_title_size]; - tox_event_conference_title_construct(conference_title); - ++events->conference_title_size; - return conference_title; -} - -void tox_events_clear_conference_title(Tox_Events *events) -{ - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->conference_title_size; ++i) { - tox_event_conference_title_destruct(&events->conference_title[i]); - } - - free(events->conference_title); - events->conference_title = nullptr; - events->conference_title_size = 0; - events->conference_title_capacity = 0; -} - -uint32_t tox_events_get_conference_title_size(const Tox_Events *events) -{ - if (events == nullptr) { - return 0; - } - - return events->conference_title_size; -} - -const Tox_Event_Conference_Title *tox_events_get_conference_title(const Tox_Events *events, uint32_t index) -{ - assert(index < events->conference_title_size); - assert(events->conference_title != nullptr); - return &events->conference_title[index]; -} - -bool tox_events_pack_conference_title(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_conference_title_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_conference_title_pack(tox_events_get_conference_title(events, i), bp)) { - return false; - } - } - return true; -} - -bool tox_events_unpack_conference_title(Tox_Events *events, Bin_Unpack *bu) -{ - Tox_Event_Conference_Title *event = tox_events_add_conference_title(events); - - if (event == nullptr) { - return false; - } - - return tox_event_conference_title_unpack(event, bu); -} +EV_FUNCS(Conference_Title, conference_title, CONFERENCE_TITLE) /***************************************************** @@ -228,21 +87,15 @@ bool tox_events_unpack_conference_title(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_conference_title(Tox *tox, uint32_t conference_number, uint32_t peer_number, const uint8_t *title, size_t length, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Conference_Title *conference_title = tox_events_add_conference_title(state->events); + Tox_Event_Conference_Title *conference_title = tox_event_conference_title_alloc(user_data); if (conference_title == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } + const Tox_System *sys = tox_get_system(tox); + tox_event_conference_title_set_conference_number(conference_title, conference_number); tox_event_conference_title_set_peer_number(conference_title, peer_number); - tox_event_conference_title_set_title(conference_title, title, length); + tox_event_conference_title_set_title(conference_title, title, length, sys->mem); } diff --git a/toxcore/events/event_macros.h b/toxcore/events/event_macros.h new file mode 100644 index 00000000000..d16b994c360 --- /dev/null +++ b/toxcore/events/event_macros.h @@ -0,0 +1,189 @@ +/***************************************************** + * + * :: get/set + * + *****************************************************/ + +#define EV_ACCESS_VALUE(EVENT_TYPE, EVENT_NAME, MEMBER_TYPE, MEMBER_NAME) \ +non_null() \ +static void tox_event_##EVENT_NAME##_set_##MEMBER_NAME(Tox_Event_##EVENT_TYPE *EVENT_NAME, \ + MEMBER_TYPE MEMBER_NAME) \ +{ \ + assert(EVENT_NAME != nullptr); \ + EVENT_NAME->MEMBER_NAME = MEMBER_NAME; \ +} \ +MEMBER_TYPE tox_event_##EVENT_NAME##_get_##MEMBER_NAME(const Tox_Event_##EVENT_TYPE *EVENT_NAME) \ +{ \ + assert(EVENT_NAME != nullptr); \ + return EVENT_NAME->MEMBER_NAME; \ +} + +#define EV_ACCESS_FIXED(EVENT_TYPE, EVENT_NAME, MEMBER_TYPE, MEMBER_NAME, MEMBER_SIZE) \ +non_null() \ +static bool tox_event_##EVENT_NAME##_set_##MEMBER_NAME(Tox_Event_##EVENT_TYPE *EVENT_NAME, const uint8_t *MEMBER_NAME) \ +{ \ + assert(EVENT_NAME != nullptr); \ + \ + memcpy(EVENT_NAME->MEMBER_NAME, MEMBER_NAME, MEMBER_SIZE); \ + return true; \ +} \ +const uint8_t *tox_event_##EVENT_NAME##_get_##MEMBER_NAME(const Tox_Event_##EVENT_TYPE *EVENT_NAME) \ +{ \ + assert(EVENT_NAME != nullptr); \ + return EVENT_NAME->MEMBER_NAME; \ +} + +#define EV_ACCESS_ARRAY(EVENT_TYPE, EVENT_NAME, MEMBER_TYPE, MEMBER_NAME) \ +non_null() \ +static bool tox_event_##EVENT_NAME##_set_##MEMBER_NAME(Tox_Event_##EVENT_TYPE *EVENT_NAME, \ + const MEMBER_TYPE *MEMBER_NAME, uint32_t MEMBER_NAME##_length, const Memory *mem) \ +{ \ + assert(EVENT_NAME != nullptr); \ + \ + if (EVENT_NAME->MEMBER_NAME != nullptr) { \ + mem_delete(mem, EVENT_NAME->MEMBER_NAME); \ + EVENT_NAME->MEMBER_NAME = nullptr; \ + EVENT_NAME->MEMBER_NAME##_length = 0; \ + } \ + \ + EVENT_NAME->MEMBER_NAME = (MEMBER_TYPE *)mem_balloc(mem, MEMBER_NAME##_length * sizeof(MEMBER_TYPE)); \ + \ + if (EVENT_NAME->MEMBER_NAME == nullptr) { \ + return false; \ + } \ + \ + memcpy(EVENT_NAME->MEMBER_NAME, MEMBER_NAME, MEMBER_NAME##_length * sizeof(MEMBER_TYPE)); \ + EVENT_NAME->MEMBER_NAME##_length = MEMBER_NAME##_length; \ + return true; \ +} \ +uint32_t tox_event_##EVENT_NAME##_get_##MEMBER_NAME##_length(const Tox_Event_##EVENT_TYPE *EVENT_NAME) \ +{ \ + assert(EVENT_NAME != nullptr); \ + return EVENT_NAME->MEMBER_NAME##_length; \ +} \ +const MEMBER_TYPE *tox_event_##EVENT_NAME##_get_##MEMBER_NAME(const Tox_Event_##EVENT_TYPE *EVENT_NAME) \ +{ \ + assert(EVENT_NAME != nullptr); \ + return EVENT_NAME->MEMBER_NAME; \ +} + +/***************************************************** + * + * :: new/free/add/get/size/unpack + * + *****************************************************/ + +#define EV_FUNCS(EVENT_TYPE, EVENT_NAME, EVENT_ENUM) \ +const Tox_Event_##EVENT_TYPE *tox_event_get_##EVENT_NAME( \ + const Tox_Event *event) \ +{ \ + return event->type == TOX_EVENT_##EVENT_ENUM ? event->data.EVENT_NAME : nullptr; \ +} \ + \ +Tox_Event_##EVENT_TYPE *tox_event_##EVENT_NAME##_new(const Memory *mem) \ +{ \ + Tox_Event_##EVENT_TYPE *const EVENT_NAME = \ + (Tox_Event_##EVENT_TYPE *)mem_alloc(mem, sizeof(Tox_Event_##EVENT_TYPE)); \ + \ + if (EVENT_NAME == nullptr) { \ + return nullptr; \ + } \ + \ + tox_event_##EVENT_NAME##_construct(EVENT_NAME); \ + return EVENT_NAME; \ +} \ + \ +void tox_event_##EVENT_NAME##_free(Tox_Event_##EVENT_TYPE *EVENT_NAME, const Memory *mem) \ +{ \ + if (EVENT_NAME != nullptr) { \ + tox_event_##EVENT_NAME##_destruct(EVENT_NAME, mem); \ + } \ + mem_delete(mem, EVENT_NAME); \ +} \ + \ +non_null() \ +static Tox_Event_##EVENT_TYPE *tox_events_add_##EVENT_NAME(Tox_Events *events, const Memory *mem) \ +{ \ + Tox_Event_##EVENT_TYPE *const EVENT_NAME = tox_event_##EVENT_NAME##_new(mem); \ + \ + if (EVENT_NAME == nullptr) { \ + return nullptr; \ + } \ + \ + Tox_Event event; \ + event.type = TOX_EVENT_##EVENT_ENUM; \ + event.data.EVENT_NAME = EVENT_NAME; \ + \ + tox_events_add(events, &event); \ + return EVENT_NAME; \ +} \ + \ +const Tox_Event_##EVENT_TYPE *tox_events_get_##EVENT_NAME( \ + const Tox_Events *events, uint32_t index) \ +{ \ + uint32_t EVENT_NAME##_index = 0; \ + \ + for (uint32_t i = 0; i < events->events_size; ++i) { \ + if (EVENT_NAME##_index > index) { \ + return nullptr; \ + } \ + \ + if (events->events[i].type == TOX_EVENT_##EVENT_ENUM) { \ + const Tox_Event_##EVENT_TYPE *EVENT_NAME = events->events[i].data.EVENT_NAME; \ + if (EVENT_NAME##_index == index) { \ + return EVENT_NAME; \ + } \ + ++EVENT_NAME##_index; \ + } \ + } \ + \ + return nullptr; \ +} \ + \ +uint32_t tox_events_get_##EVENT_NAME##_size( \ + const Tox_Events *events) \ +{ \ + uint32_t EVENT_NAME##_size = 0; \ + \ + for (uint32_t i = 0; i < events->events_size; ++i) { \ + if (events->events[i].type == TOX_EVENT_##EVENT_ENUM) { \ + ++EVENT_NAME##_size; \ + } \ + } \ + \ + return EVENT_NAME##_size; \ +} \ + \ +bool tox_event_##EVENT_NAME##_unpack( \ + Tox_Event_##EVENT_TYPE **event, Bin_Unpack *bu, const Memory *mem) \ +{ \ + assert(event != nullptr); \ + *event = tox_event_##EVENT_NAME##_new(mem); \ + \ + if (*event == nullptr) { \ + return false; \ + } \ + \ + return tox_event_##EVENT_NAME##_unpack_into(*event, bu); \ +} \ + \ +non_null() \ +static Tox_Event_##EVENT_TYPE *tox_event_##EVENT_NAME##_alloc(void *user_data) \ +{ \ + Tox_Events_State *state = tox_events_alloc(user_data); \ + assert(state != nullptr); \ + \ + if (state->events == nullptr) { \ + return nullptr; \ + } \ + \ + Tox_Event_##EVENT_TYPE *EVENT_NAME = tox_events_add_##EVENT_NAME(state->events, state->mem); \ + \ + if (EVENT_NAME == nullptr) { \ + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; \ + return nullptr; \ + } \ + \ + return EVENT_NAME; \ +} + diff --git a/toxcore/events/events_alloc.c b/toxcore/events/events_alloc.c index f661c5c0399..e007b2e2873 100644 --- a/toxcore/events/events_alloc.c +++ b/toxcore/events/events_alloc.c @@ -13,13 +13,14 @@ Tox_Events_State *tox_events_alloc(void *user_data) { Tox_Events_State *state = (Tox_Events_State *)user_data; assert(state != nullptr); + assert(state->mem != nullptr); if (state->events != nullptr) { // Already allocated. return state; } - state->events = (Tox_Events *)calloc(1, sizeof(Tox_Events)); + state->events = (Tox_Events *)mem_alloc(state->mem, sizeof(Tox_Events)); if (state->events == nullptr) { // It's still null => allocation failed. @@ -28,6 +29,7 @@ Tox_Events_State *tox_events_alloc(void *user_data) *state->events = (Tox_Events) { nullptr }; + state->events->mem = state->mem; } return state; @@ -39,26 +41,35 @@ void tox_events_free(Tox_Events *events) return; } - tox_events_clear_conference_connected(events); - tox_events_clear_conference_invite(events); - tox_events_clear_conference_message(events); - tox_events_clear_conference_peer_list_changed(events); - tox_events_clear_conference_peer_name(events); - tox_events_clear_conference_title(events); - tox_events_clear_file_chunk_request(events); - tox_events_clear_file_recv_chunk(events); - tox_events_clear_file_recv_control(events); - tox_events_clear_file_recv(events); - tox_events_clear_friend_connection_status(events); - tox_events_clear_friend_lossless_packet(events); - tox_events_clear_friend_lossy_packet(events); - tox_events_clear_friend_message(events); - tox_events_clear_friend_name(events); - tox_events_clear_friend_read_receipt(events); - tox_events_clear_friend_request(events); - tox_events_clear_friend_status(events); - tox_events_clear_friend_status_message(events); - tox_events_clear_friend_typing(events); - tox_events_clear_self_connection_status(events); - free(events); + for (uint32_t i = 0; i < events->events_size; i++) { + tox_event_destruct(&events->events[i], events->mem); + } + + mem_delete(events->mem, events->events); + mem_delete(events->mem, events); +} + +bool tox_events_add(Tox_Events *events, const Tox_Event *event) +{ + if (events->events_size == UINT32_MAX) { + return false; + } + + if (events->events_size == events->events_capacity) { + const uint32_t new_events_capacity = events->events_capacity * 2 + 1; + Tox_Event *new_events = (Tox_Event *)mem_vrealloc( + events->mem, events->events, new_events_capacity, sizeof(Tox_Event)); + + if (new_events == nullptr) { + return false; + } + + events->events = new_events; + events->events_capacity = new_events_capacity; + } + + events->events[events->events_size] = *event; + ++events->events_size; + + return true; } diff --git a/toxcore/events/events_alloc.h b/toxcore/events/events_alloc.h index 6c5a7abd4f7..ccf5c2db90e 100644 --- a/toxcore/events/events_alloc.h +++ b/toxcore/events/events_alloc.h @@ -8,100 +8,23 @@ #include "../attributes.h" #include "../bin_pack.h" #include "../bin_unpack.h" -#include "../tox_events.h" +#include "../tox_event.h" #ifdef __cplusplus extern "C" { #endif struct Tox_Events { - Tox_Event_Conference_Connected *conference_connected; - uint32_t conference_connected_size; - uint32_t conference_connected_capacity; + Tox_Event *events; + uint32_t events_size; + uint32_t events_capacity; - Tox_Event_Conference_Invite *conference_invite; - uint32_t conference_invite_size; - uint32_t conference_invite_capacity; - - Tox_Event_Conference_Message *conference_message; - uint32_t conference_message_size; - uint32_t conference_message_capacity; - - Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed; - uint32_t conference_peer_list_changed_size; - uint32_t conference_peer_list_changed_capacity; - - Tox_Event_Conference_Peer_Name *conference_peer_name; - uint32_t conference_peer_name_size; - uint32_t conference_peer_name_capacity; - - Tox_Event_Conference_Title *conference_title; - uint32_t conference_title_size; - uint32_t conference_title_capacity; - - Tox_Event_File_Chunk_Request *file_chunk_request; - uint32_t file_chunk_request_size; - uint32_t file_chunk_request_capacity; - - Tox_Event_File_Recv *file_recv; - uint32_t file_recv_size; - uint32_t file_recv_capacity; - - Tox_Event_File_Recv_Chunk *file_recv_chunk; - uint32_t file_recv_chunk_size; - uint32_t file_recv_chunk_capacity; - - Tox_Event_File_Recv_Control *file_recv_control; - uint32_t file_recv_control_size; - uint32_t file_recv_control_capacity; - - Tox_Event_Friend_Connection_Status *friend_connection_status; - uint32_t friend_connection_status_size; - uint32_t friend_connection_status_capacity; - - Tox_Event_Friend_Lossless_Packet *friend_lossless_packet; - uint32_t friend_lossless_packet_size; - uint32_t friend_lossless_packet_capacity; - - Tox_Event_Friend_Lossy_Packet *friend_lossy_packet; - uint32_t friend_lossy_packet_size; - uint32_t friend_lossy_packet_capacity; - - Tox_Event_Friend_Message *friend_message; - uint32_t friend_message_size; - uint32_t friend_message_capacity; - - Tox_Event_Friend_Name *friend_name; - uint32_t friend_name_size; - uint32_t friend_name_capacity; - - Tox_Event_Friend_Read_Receipt *friend_read_receipt; - uint32_t friend_read_receipt_size; - uint32_t friend_read_receipt_capacity; - - Tox_Event_Friend_Request *friend_request; - uint32_t friend_request_size; - uint32_t friend_request_capacity; - - Tox_Event_Friend_Status *friend_status; - uint32_t friend_status_size; - uint32_t friend_status_capacity; - - Tox_Event_Friend_Status_Message *friend_status_message; - uint32_t friend_status_message_size; - uint32_t friend_status_message_capacity; - - Tox_Event_Friend_Typing *friend_typing; - uint32_t friend_typing_size; - uint32_t friend_typing_capacity; - - Tox_Event_Self_Connection_Status *self_connection_status; - uint32_t self_connection_status_size; - uint32_t self_connection_status_capacity; + const Memory *mem; }; typedef struct Tox_Events_State { Tox_Err_Events_Iterate error; + const Memory *mem; Tox_Events *events; } Tox_Events_State; @@ -127,88 +50,18 @@ tox_friend_status_message_cb tox_events_handle_friend_status_message; tox_friend_typing_cb tox_events_handle_friend_typing; tox_self_connection_status_cb tox_events_handle_self_connection_status; -// non_null() -typedef void tox_events_clear_cb(Tox_Events *events); - -tox_events_clear_cb tox_events_clear_conference_connected; -tox_events_clear_cb tox_events_clear_conference_invite; -tox_events_clear_cb tox_events_clear_conference_message; -tox_events_clear_cb tox_events_clear_conference_peer_list_changed; -tox_events_clear_cb tox_events_clear_conference_peer_name; -tox_events_clear_cb tox_events_clear_conference_title; -tox_events_clear_cb tox_events_clear_file_chunk_request; -tox_events_clear_cb tox_events_clear_file_recv_chunk; -tox_events_clear_cb tox_events_clear_file_recv_control; -tox_events_clear_cb tox_events_clear_file_recv; -tox_events_clear_cb tox_events_clear_friend_connection_status; -tox_events_clear_cb tox_events_clear_friend_lossless_packet; -tox_events_clear_cb tox_events_clear_friend_lossy_packet; -tox_events_clear_cb tox_events_clear_friend_message; -tox_events_clear_cb tox_events_clear_friend_name; -tox_events_clear_cb tox_events_clear_friend_read_receipt; -tox_events_clear_cb tox_events_clear_friend_request; -tox_events_clear_cb tox_events_clear_friend_status_message; -tox_events_clear_cb tox_events_clear_friend_status; -tox_events_clear_cb tox_events_clear_friend_typing; -tox_events_clear_cb tox_events_clear_self_connection_status; - -// non_null() -typedef bool tox_events_pack_cb(const Tox_Events *events, Bin_Pack *bp); - -tox_events_pack_cb tox_events_pack_conference_connected; -tox_events_pack_cb tox_events_pack_conference_invite; -tox_events_pack_cb tox_events_pack_conference_message; -tox_events_pack_cb tox_events_pack_conference_peer_list_changed; -tox_events_pack_cb tox_events_pack_conference_peer_name; -tox_events_pack_cb tox_events_pack_conference_title; -tox_events_pack_cb tox_events_pack_file_chunk_request; -tox_events_pack_cb tox_events_pack_file_recv_chunk; -tox_events_pack_cb tox_events_pack_file_recv_control; -tox_events_pack_cb tox_events_pack_file_recv; -tox_events_pack_cb tox_events_pack_friend_connection_status; -tox_events_pack_cb tox_events_pack_friend_lossless_packet; -tox_events_pack_cb tox_events_pack_friend_lossy_packet; -tox_events_pack_cb tox_events_pack_friend_message; -tox_events_pack_cb tox_events_pack_friend_name; -tox_events_pack_cb tox_events_pack_friend_read_receipt; -tox_events_pack_cb tox_events_pack_friend_request; -tox_events_pack_cb tox_events_pack_friend_status_message; -tox_events_pack_cb tox_events_pack_friend_status; -tox_events_pack_cb tox_events_pack_friend_typing; -tox_events_pack_cb tox_events_pack_self_connection_status; - -tox_events_pack_cb tox_events_pack; - -// non_null() -typedef bool tox_events_unpack_cb(Tox_Events *events, Bin_Unpack *bu); - -tox_events_unpack_cb tox_events_unpack_conference_connected; -tox_events_unpack_cb tox_events_unpack_conference_invite; -tox_events_unpack_cb tox_events_unpack_conference_message; -tox_events_unpack_cb tox_events_unpack_conference_peer_list_changed; -tox_events_unpack_cb tox_events_unpack_conference_peer_name; -tox_events_unpack_cb tox_events_unpack_conference_title; -tox_events_unpack_cb tox_events_unpack_file_chunk_request; -tox_events_unpack_cb tox_events_unpack_file_recv_chunk; -tox_events_unpack_cb tox_events_unpack_file_recv_control; -tox_events_unpack_cb tox_events_unpack_file_recv; -tox_events_unpack_cb tox_events_unpack_friend_connection_status; -tox_events_unpack_cb tox_events_unpack_friend_lossless_packet; -tox_events_unpack_cb tox_events_unpack_friend_lossy_packet; -tox_events_unpack_cb tox_events_unpack_friend_message; -tox_events_unpack_cb tox_events_unpack_friend_name; -tox_events_unpack_cb tox_events_unpack_friend_read_receipt; -tox_events_unpack_cb tox_events_unpack_friend_request; -tox_events_unpack_cb tox_events_unpack_friend_status_message; -tox_events_unpack_cb tox_events_unpack_friend_status; -tox_events_unpack_cb tox_events_unpack_friend_typing; -tox_events_unpack_cb tox_events_unpack_self_connection_status; +non_null() +bool tox_events_pack(const Tox_Events *events, Bin_Pack *bp); -tox_events_unpack_cb tox_events_unpack; +non_null() +bool tox_events_unpack(Tox_Events *events, Bin_Unpack *bu, const Memory *mem); non_null() Tox_Events_State *tox_events_alloc(void *user_data); +non_null() +bool tox_events_add(Tox_Events *events, const Tox_Event *event); + #ifdef __cplusplus } #endif diff --git a/toxcore/events/file_chunk_request.c b/toxcore/events/file_chunk_request.c index f5a9421f58d..8543143f8bb 100644 --- a/toxcore/events/file_chunk_request.c +++ b/toxcore/events/file_chunk_request.c @@ -13,6 +13,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "event_macros.h" /***************************************************** @@ -29,6 +30,11 @@ struct Tox_Event_File_Chunk_Request { uint16_t length; }; +EV_ACCESS_VALUE(File_Chunk_Request, file_chunk_request, uint32_t, friend_number) +EV_ACCESS_VALUE(File_Chunk_Request, file_chunk_request, uint32_t, file_number) +EV_ACCESS_VALUE(File_Chunk_Request, file_chunk_request, uint64_t, position) +EV_ACCESS_VALUE(File_Chunk_Request, file_chunk_request, uint16_t, length) + non_null() static void tox_event_file_chunk_request_construct(Tox_Event_File_Chunk_Request *file_chunk_request) { @@ -37,64 +43,12 @@ static void tox_event_file_chunk_request_construct(Tox_Event_File_Chunk_Request }; } non_null() -static void tox_event_file_chunk_request_destruct(Tox_Event_File_Chunk_Request *file_chunk_request) +static void tox_event_file_chunk_request_destruct(Tox_Event_File_Chunk_Request *file_chunk_request, const Memory *mem) { return; } -non_null() -static void tox_event_file_chunk_request_set_friend_number(Tox_Event_File_Chunk_Request *file_chunk_request, - uint32_t friend_number) -{ - assert(file_chunk_request != nullptr); - file_chunk_request->friend_number = friend_number; -} -uint32_t tox_event_file_chunk_request_get_friend_number(const Tox_Event_File_Chunk_Request *file_chunk_request) -{ - assert(file_chunk_request != nullptr); - return file_chunk_request->friend_number; -} - -non_null() -static void tox_event_file_chunk_request_set_file_number(Tox_Event_File_Chunk_Request *file_chunk_request, - uint32_t file_number) -{ - assert(file_chunk_request != nullptr); - file_chunk_request->file_number = file_number; -} -uint32_t tox_event_file_chunk_request_get_file_number(const Tox_Event_File_Chunk_Request *file_chunk_request) -{ - assert(file_chunk_request != nullptr); - return file_chunk_request->file_number; -} - -non_null() -static void tox_event_file_chunk_request_set_position(Tox_Event_File_Chunk_Request *file_chunk_request, - uint64_t position) -{ - assert(file_chunk_request != nullptr); - file_chunk_request->position = position; -} -uint64_t tox_event_file_chunk_request_get_position(const Tox_Event_File_Chunk_Request *file_chunk_request) -{ - assert(file_chunk_request != nullptr); - return file_chunk_request->position; -} - -non_null() -static void tox_event_file_chunk_request_set_length(Tox_Event_File_Chunk_Request *file_chunk_request, uint16_t length) -{ - assert(file_chunk_request != nullptr); - file_chunk_request->length = length; -} -uint16_t tox_event_file_chunk_request_get_length(const Tox_Event_File_Chunk_Request *file_chunk_request) -{ - assert(file_chunk_request != nullptr); - return file_chunk_request->length; -} - -non_null() -static bool tox_event_file_chunk_request_pack( +bool tox_event_file_chunk_request_pack( const Tox_Event_File_Chunk_Request *event, Bin_Pack *bp) { assert(event != nullptr); @@ -108,7 +62,7 @@ static bool tox_event_file_chunk_request_pack( } non_null() -static bool tox_event_file_chunk_request_unpack( +static bool tox_event_file_chunk_request_unpack_into( Tox_Event_File_Chunk_Request *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -122,94 +76,7 @@ static bool tox_event_file_chunk_request_unpack( && bin_unpack_u16(bu, &event->length); } - -/***************************************************** - * - * :: add/clear/get - * - *****************************************************/ - - -non_null() -static Tox_Event_File_Chunk_Request *tox_events_add_file_chunk_request(Tox_Events *events) -{ - if (events->file_chunk_request_size == UINT32_MAX) { - return nullptr; - } - - if (events->file_chunk_request_size == events->file_chunk_request_capacity) { - const uint32_t new_file_chunk_request_capacity = events->file_chunk_request_capacity * 2 + 1; - Tox_Event_File_Chunk_Request *new_file_chunk_request = (Tox_Event_File_Chunk_Request *)realloc( - events->file_chunk_request, new_file_chunk_request_capacity * sizeof(Tox_Event_File_Chunk_Request)); - - if (new_file_chunk_request == nullptr) { - return nullptr; - } - - events->file_chunk_request = new_file_chunk_request; - events->file_chunk_request_capacity = new_file_chunk_request_capacity; - } - - Tox_Event_File_Chunk_Request *const file_chunk_request = &events->file_chunk_request[events->file_chunk_request_size]; - tox_event_file_chunk_request_construct(file_chunk_request); - ++events->file_chunk_request_size; - return file_chunk_request; -} - -void tox_events_clear_file_chunk_request(Tox_Events *events) -{ - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->file_chunk_request_size; ++i) { - tox_event_file_chunk_request_destruct(&events->file_chunk_request[i]); - } - - free(events->file_chunk_request); - events->file_chunk_request = nullptr; - events->file_chunk_request_size = 0; - events->file_chunk_request_capacity = 0; -} - -uint32_t tox_events_get_file_chunk_request_size(const Tox_Events *events) -{ - if (events == nullptr) { - return 0; - } - - return events->file_chunk_request_size; -} - -const Tox_Event_File_Chunk_Request *tox_events_get_file_chunk_request(const Tox_Events *events, uint32_t index) -{ - assert(index < events->file_chunk_request_size); - assert(events->file_chunk_request != nullptr); - return &events->file_chunk_request[index]; -} - -bool tox_events_pack_file_chunk_request(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_file_chunk_request_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_file_chunk_request_pack(tox_events_get_file_chunk_request(events, i), bp)) { - return false; - } - } - return true; -} - -bool tox_events_unpack_file_chunk_request(Tox_Events *events, Bin_Unpack *bu) -{ - Tox_Event_File_Chunk_Request *event = tox_events_add_file_chunk_request(events); - - if (event == nullptr) { - return false; - } - - return tox_event_file_chunk_request_unpack(event, bu); -} +EV_FUNCS(File_Chunk_Request, file_chunk_request, FILE_CHUNK_REQUEST) /***************************************************** @@ -222,17 +89,9 @@ bool tox_events_unpack_file_chunk_request(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_file_chunk_request(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position, size_t length, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_File_Chunk_Request *file_chunk_request = tox_events_add_file_chunk_request(state->events); + Tox_Event_File_Chunk_Request *file_chunk_request = tox_event_file_chunk_request_alloc(user_data); if (file_chunk_request == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/file_recv.c b/toxcore/events/file_recv.c index a907b3fdbef..6a5ae561469 100644 --- a/toxcore/events/file_recv.c +++ b/toxcore/events/file_recv.c @@ -13,6 +13,8 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_private.h" +#include "event_macros.h" /***************************************************** @@ -31,6 +33,12 @@ struct Tox_Event_File_Recv { uint32_t filename_length; }; +EV_ACCESS_VALUE(File_Recv, file_recv, uint32_t, friend_number) +EV_ACCESS_VALUE(File_Recv, file_recv, uint32_t, file_number) +EV_ACCESS_VALUE(File_Recv, file_recv, uint32_t, kind) +EV_ACCESS_VALUE(File_Recv, file_recv, uint64_t, file_size) +EV_ACCESS_ARRAY(File_Recv, file_recv, uint8_t, filename) + non_null() static void tox_event_file_recv_construct(Tox_Event_File_Recv *file_recv) { @@ -39,98 +47,12 @@ static void tox_event_file_recv_construct(Tox_Event_File_Recv *file_recv) }; } non_null() -static void tox_event_file_recv_destruct(Tox_Event_File_Recv *file_recv) -{ - free(file_recv->filename); -} - -non_null() -static void tox_event_file_recv_set_friend_number(Tox_Event_File_Recv *file_recv, - uint32_t friend_number) -{ - assert(file_recv != nullptr); - file_recv->friend_number = friend_number; -} -uint32_t tox_event_file_recv_get_friend_number(const Tox_Event_File_Recv *file_recv) +static void tox_event_file_recv_destruct(Tox_Event_File_Recv *file_recv, const Memory *mem) { - assert(file_recv != nullptr); - return file_recv->friend_number; + mem_delete(mem, file_recv->filename); } -non_null() -static void tox_event_file_recv_set_file_number(Tox_Event_File_Recv *file_recv, - uint32_t file_number) -{ - assert(file_recv != nullptr); - file_recv->file_number = file_number; -} -uint32_t tox_event_file_recv_get_file_number(const Tox_Event_File_Recv *file_recv) -{ - assert(file_recv != nullptr); - return file_recv->file_number; -} - -non_null() -static void tox_event_file_recv_set_kind(Tox_Event_File_Recv *file_recv, - uint32_t kind) -{ - assert(file_recv != nullptr); - file_recv->kind = kind; -} -uint32_t tox_event_file_recv_get_kind(const Tox_Event_File_Recv *file_recv) -{ - assert(file_recv != nullptr); - return file_recv->kind; -} - -non_null() -static void tox_event_file_recv_set_file_size(Tox_Event_File_Recv *file_recv, - uint64_t file_size) -{ - assert(file_recv != nullptr); - file_recv->file_size = file_size; -} -uint64_t tox_event_file_recv_get_file_size(const Tox_Event_File_Recv *file_recv) -{ - assert(file_recv != nullptr); - return file_recv->file_size; -} - -non_null() -static bool tox_event_file_recv_set_filename(Tox_Event_File_Recv *file_recv, const uint8_t *filename, - uint32_t filename_length) -{ - assert(file_recv != nullptr); - - if (file_recv->filename != nullptr) { - free(file_recv->filename); - file_recv->filename = nullptr; - file_recv->filename_length = 0; - } - - file_recv->filename = (uint8_t *)malloc(filename_length); - - if (file_recv->filename == nullptr) { - return false; - } - - memcpy(file_recv->filename, filename, filename_length); - file_recv->filename_length = filename_length; - return true; -} -uint32_t tox_event_file_recv_get_filename_length(const Tox_Event_File_Recv *file_recv) -{ - assert(file_recv != nullptr); - return file_recv->filename_length; -} -const uint8_t *tox_event_file_recv_get_filename(const Tox_Event_File_Recv *file_recv) -{ - assert(file_recv != nullptr); - return file_recv->filename; -} - -non_null() -static bool tox_event_file_recv_pack( +bool tox_event_file_recv_pack( const Tox_Event_File_Recv *event, Bin_Pack *bp) { assert(event != nullptr); @@ -145,7 +67,7 @@ static bool tox_event_file_recv_pack( } non_null() -static bool tox_event_file_recv_unpack( +static bool tox_event_file_recv_unpack_into( Tox_Event_File_Recv *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -160,94 +82,7 @@ static bool tox_event_file_recv_unpack( && bin_unpack_bin(bu, &event->filename, &event->filename_length); } - -/***************************************************** - * - * :: add/clear/get - * - *****************************************************/ - - -non_null() -static Tox_Event_File_Recv *tox_events_add_file_recv(Tox_Events *events) -{ - if (events->file_recv_size == UINT32_MAX) { - return nullptr; - } - - if (events->file_recv_size == events->file_recv_capacity) { - const uint32_t new_file_recv_capacity = events->file_recv_capacity * 2 + 1; - Tox_Event_File_Recv *new_file_recv = (Tox_Event_File_Recv *)realloc( - events->file_recv, new_file_recv_capacity * sizeof(Tox_Event_File_Recv)); - - if (new_file_recv == nullptr) { - return nullptr; - } - - events->file_recv = new_file_recv; - events->file_recv_capacity = new_file_recv_capacity; - } - - Tox_Event_File_Recv *const file_recv = &events->file_recv[events->file_recv_size]; - tox_event_file_recv_construct(file_recv); - ++events->file_recv_size; - return file_recv; -} - -void tox_events_clear_file_recv(Tox_Events *events) -{ - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->file_recv_size; ++i) { - tox_event_file_recv_destruct(&events->file_recv[i]); - } - - free(events->file_recv); - events->file_recv = nullptr; - events->file_recv_size = 0; - events->file_recv_capacity = 0; -} - -uint32_t tox_events_get_file_recv_size(const Tox_Events *events) -{ - if (events == nullptr) { - return 0; - } - - return events->file_recv_size; -} - -const Tox_Event_File_Recv *tox_events_get_file_recv(const Tox_Events *events, uint32_t index) -{ - assert(index < events->file_recv_size); - assert(events->file_recv != nullptr); - return &events->file_recv[index]; -} - -bool tox_events_pack_file_recv(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_file_recv_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_file_recv_pack(tox_events_get_file_recv(events, i), bp)) { - return false; - } - } - return true; -} - -bool tox_events_unpack_file_recv(Tox_Events *events, Bin_Unpack *bu) -{ - Tox_Event_File_Recv *event = tox_events_add_file_recv(events); - - if (event == nullptr) { - return false; - } - - return tox_event_file_recv_unpack(event, bu); -} +EV_FUNCS(File_Recv, file_recv, FILE_RECV) /***************************************************** @@ -260,23 +95,17 @@ bool tox_events_unpack_file_recv(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_file_recv(Tox *tox, uint32_t friend_number, uint32_t file_number, uint32_t kind, uint64_t file_size, const uint8_t *filename, size_t filename_length, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_File_Recv *file_recv = tox_events_add_file_recv(state->events); + Tox_Event_File_Recv *file_recv = tox_event_file_recv_alloc(user_data); if (file_recv == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } + const Tox_System *sys = tox_get_system(tox); + tox_event_file_recv_set_friend_number(file_recv, friend_number); tox_event_file_recv_set_file_number(file_recv, file_number); tox_event_file_recv_set_kind(file_recv, kind); tox_event_file_recv_set_file_size(file_recv, file_size); - tox_event_file_recv_set_filename(file_recv, filename, filename_length); + tox_event_file_recv_set_filename(file_recv, filename, filename_length, sys->mem); } diff --git a/toxcore/events/file_recv_chunk.c b/toxcore/events/file_recv_chunk.c index 0fe7784465a..a2c289344aa 100644 --- a/toxcore/events/file_recv_chunk.c +++ b/toxcore/events/file_recv_chunk.c @@ -13,6 +13,8 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_private.h" +#include "event_macros.h" /***************************************************** @@ -30,6 +32,11 @@ struct Tox_Event_File_Recv_Chunk { uint32_t data_length; }; +EV_ACCESS_VALUE(File_Recv_Chunk, file_recv_chunk, uint32_t, friend_number) +EV_ACCESS_VALUE(File_Recv_Chunk, file_recv_chunk, uint32_t, file_number) +EV_ACCESS_VALUE(File_Recv_Chunk, file_recv_chunk, uint64_t, position) +EV_ACCESS_ARRAY(File_Recv_Chunk, file_recv_chunk, uint8_t, data) + non_null() static void tox_event_file_recv_chunk_construct(Tox_Event_File_Recv_Chunk *file_recv_chunk) { @@ -38,85 +45,12 @@ static void tox_event_file_recv_chunk_construct(Tox_Event_File_Recv_Chunk *file_ }; } non_null() -static void tox_event_file_recv_chunk_destruct(Tox_Event_File_Recv_Chunk *file_recv_chunk) -{ - free(file_recv_chunk->data); -} - -non_null() -static void tox_event_file_recv_chunk_set_friend_number(Tox_Event_File_Recv_Chunk *file_recv_chunk, - uint32_t friend_number) -{ - assert(file_recv_chunk != nullptr); - file_recv_chunk->friend_number = friend_number; -} -uint32_t tox_event_file_recv_chunk_get_friend_number(const Tox_Event_File_Recv_Chunk *file_recv_chunk) -{ - assert(file_recv_chunk != nullptr); - return file_recv_chunk->friend_number; -} - -non_null() -static void tox_event_file_recv_chunk_set_file_number(Tox_Event_File_Recv_Chunk *file_recv_chunk, - uint32_t file_number) -{ - assert(file_recv_chunk != nullptr); - file_recv_chunk->file_number = file_number; -} -uint32_t tox_event_file_recv_chunk_get_file_number(const Tox_Event_File_Recv_Chunk *file_recv_chunk) -{ - assert(file_recv_chunk != nullptr); - return file_recv_chunk->file_number; -} - -non_null() -static void tox_event_file_recv_chunk_set_position(Tox_Event_File_Recv_Chunk *file_recv_chunk, - uint64_t position) -{ - assert(file_recv_chunk != nullptr); - file_recv_chunk->position = position; -} -uint64_t tox_event_file_recv_chunk_get_position(const Tox_Event_File_Recv_Chunk *file_recv_chunk) -{ - assert(file_recv_chunk != nullptr); - return file_recv_chunk->position; -} - -non_null() -static bool tox_event_file_recv_chunk_set_data(Tox_Event_File_Recv_Chunk *file_recv_chunk, const uint8_t *data, - uint32_t data_length) -{ - assert(file_recv_chunk != nullptr); - - if (file_recv_chunk->data != nullptr) { - free(file_recv_chunk->data); - file_recv_chunk->data = nullptr; - file_recv_chunk->data_length = 0; - } - - file_recv_chunk->data = (uint8_t *)malloc(data_length); - - if (file_recv_chunk->data == nullptr) { - return false; - } - - memcpy(file_recv_chunk->data, data, data_length); - file_recv_chunk->data_length = data_length; - return true; -} -uint32_t tox_event_file_recv_chunk_get_length(const Tox_Event_File_Recv_Chunk *file_recv_chunk) +static void tox_event_file_recv_chunk_destruct(Tox_Event_File_Recv_Chunk *file_recv_chunk, const Memory *mem) { - assert(file_recv_chunk != nullptr); - return file_recv_chunk->data_length; -} -const uint8_t *tox_event_file_recv_chunk_get_data(const Tox_Event_File_Recv_Chunk *file_recv_chunk) -{ - assert(file_recv_chunk != nullptr); - return file_recv_chunk->data; + mem_delete(mem, file_recv_chunk->data); } -non_null() -static bool tox_event_file_recv_chunk_pack( +bool tox_event_file_recv_chunk_pack( const Tox_Event_File_Recv_Chunk *event, Bin_Pack *bp) { assert(event != nullptr); @@ -130,7 +64,7 @@ static bool tox_event_file_recv_chunk_pack( } non_null() -static bool tox_event_file_recv_chunk_unpack( +static bool tox_event_file_recv_chunk_unpack_into( Tox_Event_File_Recv_Chunk *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -144,94 +78,7 @@ static bool tox_event_file_recv_chunk_unpack( && bin_unpack_bin(bu, &event->data, &event->data_length); } - -/***************************************************** - * - * :: add/clear/get - * - *****************************************************/ - - -non_null() -static Tox_Event_File_Recv_Chunk *tox_events_add_file_recv_chunk(Tox_Events *events) -{ - if (events->file_recv_chunk_size == UINT32_MAX) { - return nullptr; - } - - if (events->file_recv_chunk_size == events->file_recv_chunk_capacity) { - const uint32_t new_file_recv_chunk_capacity = events->file_recv_chunk_capacity * 2 + 1; - Tox_Event_File_Recv_Chunk *new_file_recv_chunk = (Tox_Event_File_Recv_Chunk *)realloc( - events->file_recv_chunk, new_file_recv_chunk_capacity * sizeof(Tox_Event_File_Recv_Chunk)); - - if (new_file_recv_chunk == nullptr) { - return nullptr; - } - - events->file_recv_chunk = new_file_recv_chunk; - events->file_recv_chunk_capacity = new_file_recv_chunk_capacity; - } - - Tox_Event_File_Recv_Chunk *const file_recv_chunk = &events->file_recv_chunk[events->file_recv_chunk_size]; - tox_event_file_recv_chunk_construct(file_recv_chunk); - ++events->file_recv_chunk_size; - return file_recv_chunk; -} - -void tox_events_clear_file_recv_chunk(Tox_Events *events) -{ - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->file_recv_chunk_size; ++i) { - tox_event_file_recv_chunk_destruct(&events->file_recv_chunk[i]); - } - - free(events->file_recv_chunk); - events->file_recv_chunk = nullptr; - events->file_recv_chunk_size = 0; - events->file_recv_chunk_capacity = 0; -} - -uint32_t tox_events_get_file_recv_chunk_size(const Tox_Events *events) -{ - if (events == nullptr) { - return 0; - } - - return events->file_recv_chunk_size; -} - -const Tox_Event_File_Recv_Chunk *tox_events_get_file_recv_chunk(const Tox_Events *events, uint32_t index) -{ - assert(index < events->file_recv_chunk_size); - assert(events->file_recv_chunk != nullptr); - return &events->file_recv_chunk[index]; -} - -bool tox_events_pack_file_recv_chunk(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_file_recv_chunk_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_file_recv_chunk_pack(tox_events_get_file_recv_chunk(events, i), bp)) { - return false; - } - } - return true; -} - -bool tox_events_unpack_file_recv_chunk(Tox_Events *events, Bin_Unpack *bu) -{ - Tox_Event_File_Recv_Chunk *event = tox_events_add_file_recv_chunk(events); - - if (event == nullptr) { - return false; - } - - return tox_event_file_recv_chunk_unpack(event, bu); -} +EV_FUNCS(File_Recv_Chunk, file_recv_chunk, FILE_RECV_CHUNK) /***************************************************** @@ -244,22 +91,16 @@ bool tox_events_unpack_file_recv_chunk(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_file_recv_chunk(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position, const uint8_t *data, size_t length, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_File_Recv_Chunk *file_recv_chunk = tox_events_add_file_recv_chunk(state->events); + Tox_Event_File_Recv_Chunk *file_recv_chunk = tox_event_file_recv_chunk_alloc(user_data); if (file_recv_chunk == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } + const Tox_System *sys = tox_get_system(tox); + tox_event_file_recv_chunk_set_friend_number(file_recv_chunk, friend_number); tox_event_file_recv_chunk_set_file_number(file_recv_chunk, file_number); tox_event_file_recv_chunk_set_position(file_recv_chunk, position); - tox_event_file_recv_chunk_set_data(file_recv_chunk, data, length); + tox_event_file_recv_chunk_set_data(file_recv_chunk, data, length, sys->mem); } diff --git a/toxcore/events/file_recv_control.c b/toxcore/events/file_recv_control.c index af04b33dce7..6fe85c06061 100644 --- a/toxcore/events/file_recv_control.c +++ b/toxcore/events/file_recv_control.c @@ -14,6 +14,7 @@ #include "../tox.h" #include "../tox_events.h" #include "../tox_unpack.h" +#include "event_macros.h" /***************************************************** @@ -29,6 +30,10 @@ struct Tox_Event_File_Recv_Control { Tox_File_Control control; }; +EV_ACCESS_VALUE(File_Recv_Control, file_recv_control, uint32_t, friend_number) +EV_ACCESS_VALUE(File_Recv_Control, file_recv_control, uint32_t, file_number) +EV_ACCESS_VALUE(File_Recv_Control, file_recv_control, Tox_File_Control, control) + non_null() static void tox_event_file_recv_control_construct(Tox_Event_File_Recv_Control *file_recv_control) { @@ -37,52 +42,12 @@ static void tox_event_file_recv_control_construct(Tox_Event_File_Recv_Control *f }; } non_null() -static void tox_event_file_recv_control_destruct(Tox_Event_File_Recv_Control *file_recv_control) +static void tox_event_file_recv_control_destruct(Tox_Event_File_Recv_Control *file_recv_control, const Memory *mem) { return; } -non_null() -static void tox_event_file_recv_control_set_friend_number(Tox_Event_File_Recv_Control *file_recv_control, - uint32_t friend_number) -{ - assert(file_recv_control != nullptr); - file_recv_control->friend_number = friend_number; -} -uint32_t tox_event_file_recv_control_get_friend_number(const Tox_Event_File_Recv_Control *file_recv_control) -{ - assert(file_recv_control != nullptr); - return file_recv_control->friend_number; -} - -non_null() -static void tox_event_file_recv_control_set_file_number(Tox_Event_File_Recv_Control *file_recv_control, - uint32_t file_number) -{ - assert(file_recv_control != nullptr); - file_recv_control->file_number = file_number; -} -uint32_t tox_event_file_recv_control_get_file_number(const Tox_Event_File_Recv_Control *file_recv_control) -{ - assert(file_recv_control != nullptr); - return file_recv_control->file_number; -} - -non_null() -static void tox_event_file_recv_control_set_control(Tox_Event_File_Recv_Control *file_recv_control, - Tox_File_Control control) -{ - assert(file_recv_control != nullptr); - file_recv_control->control = control; -} -Tox_File_Control tox_event_file_recv_control_get_control(const Tox_Event_File_Recv_Control *file_recv_control) -{ - assert(file_recv_control != nullptr); - return file_recv_control->control; -} - -non_null() -static bool tox_event_file_recv_control_pack( +bool tox_event_file_recv_control_pack( const Tox_Event_File_Recv_Control *event, Bin_Pack *bp) { assert(event != nullptr); @@ -95,7 +60,7 @@ static bool tox_event_file_recv_control_pack( } non_null() -static bool tox_event_file_recv_control_unpack( +static bool tox_event_file_recv_control_unpack_into( Tox_Event_File_Recv_Control *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -108,94 +73,7 @@ static bool tox_event_file_recv_control_unpack( && tox_unpack_file_control(bu, &event->control); } - -/***************************************************** - * - * :: add/clear/get - * - *****************************************************/ - - -non_null() -static Tox_Event_File_Recv_Control *tox_events_add_file_recv_control(Tox_Events *events) -{ - if (events->file_recv_control_size == UINT32_MAX) { - return nullptr; - } - - if (events->file_recv_control_size == events->file_recv_control_capacity) { - const uint32_t new_file_recv_control_capacity = events->file_recv_control_capacity * 2 + 1; - Tox_Event_File_Recv_Control *new_file_recv_control = (Tox_Event_File_Recv_Control *)realloc( - events->file_recv_control, new_file_recv_control_capacity * sizeof(Tox_Event_File_Recv_Control)); - - if (new_file_recv_control == nullptr) { - return nullptr; - } - - events->file_recv_control = new_file_recv_control; - events->file_recv_control_capacity = new_file_recv_control_capacity; - } - - Tox_Event_File_Recv_Control *const file_recv_control = &events->file_recv_control[events->file_recv_control_size]; - tox_event_file_recv_control_construct(file_recv_control); - ++events->file_recv_control_size; - return file_recv_control; -} - -void tox_events_clear_file_recv_control(Tox_Events *events) -{ - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->file_recv_control_size; ++i) { - tox_event_file_recv_control_destruct(&events->file_recv_control[i]); - } - - free(events->file_recv_control); - events->file_recv_control = nullptr; - events->file_recv_control_size = 0; - events->file_recv_control_capacity = 0; -} - -uint32_t tox_events_get_file_recv_control_size(const Tox_Events *events) -{ - if (events == nullptr) { - return 0; - } - - return events->file_recv_control_size; -} - -const Tox_Event_File_Recv_Control *tox_events_get_file_recv_control(const Tox_Events *events, uint32_t index) -{ - assert(index < events->file_recv_control_size); - assert(events->file_recv_control != nullptr); - return &events->file_recv_control[index]; -} - -bool tox_events_pack_file_recv_control(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_file_recv_control_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_file_recv_control_pack(tox_events_get_file_recv_control(events, i), bp)) { - return false; - } - } - return true; -} - -bool tox_events_unpack_file_recv_control(Tox_Events *events, Bin_Unpack *bu) -{ - Tox_Event_File_Recv_Control *event = tox_events_add_file_recv_control(events); - - if (event == nullptr) { - return false; - } - - return tox_event_file_recv_control_unpack(event, bu); -} +EV_FUNCS(File_Recv_Control, file_recv_control, FILE_RECV_CONTROL) /***************************************************** @@ -208,17 +86,9 @@ bool tox_events_unpack_file_recv_control(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_file_recv_control(Tox *tox, uint32_t friend_number, uint32_t file_number, Tox_File_Control control, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_File_Recv_Control *file_recv_control = tox_events_add_file_recv_control(state->events); + Tox_Event_File_Recv_Control *file_recv_control = tox_event_file_recv_control_alloc(user_data); if (file_recv_control == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/friend_connection_status.c b/toxcore/events/friend_connection_status.c index 3d325ef29fe..9f1de331481 100644 --- a/toxcore/events/friend_connection_status.c +++ b/toxcore/events/friend_connection_status.c @@ -14,6 +14,7 @@ #include "../tox.h" #include "../tox_events.h" #include "../tox_unpack.h" +#include "event_macros.h" /***************************************************** @@ -28,6 +29,9 @@ struct Tox_Event_Friend_Connection_Status { Tox_Connection connection_status; }; +EV_ACCESS_VALUE(Friend_Connection_Status, friend_connection_status, uint32_t, friend_number) +EV_ACCESS_VALUE(Friend_Connection_Status, friend_connection_status, Tox_Connection, connection_status) + non_null() static void tox_event_friend_connection_status_construct(Tox_Event_Friend_Connection_Status *friend_connection_status) { @@ -36,41 +40,12 @@ static void tox_event_friend_connection_status_construct(Tox_Event_Friend_Connec }; } non_null() -static void tox_event_friend_connection_status_destruct(Tox_Event_Friend_Connection_Status *friend_connection_status) +static void tox_event_friend_connection_status_destruct(Tox_Event_Friend_Connection_Status *friend_connection_status, const Memory *mem) { return; } -non_null() -static void tox_event_friend_connection_status_set_friend_number(Tox_Event_Friend_Connection_Status - *friend_connection_status, uint32_t friend_number) -{ - assert(friend_connection_status != nullptr); - friend_connection_status->friend_number = friend_number; -} -uint32_t tox_event_friend_connection_status_get_friend_number(const Tox_Event_Friend_Connection_Status - *friend_connection_status) -{ - assert(friend_connection_status != nullptr); - return friend_connection_status->friend_number; -} - -non_null() -static void tox_event_friend_connection_status_set_connection_status(Tox_Event_Friend_Connection_Status - *friend_connection_status, Tox_Connection connection_status) -{ - assert(friend_connection_status != nullptr); - friend_connection_status->connection_status = connection_status; -} -Tox_Connection tox_event_friend_connection_status_get_connection_status(const Tox_Event_Friend_Connection_Status - *friend_connection_status) -{ - assert(friend_connection_status != nullptr); - return friend_connection_status->connection_status; -} - -non_null() -static bool tox_event_friend_connection_status_pack( +bool tox_event_friend_connection_status_pack( const Tox_Event_Friend_Connection_Status *event, Bin_Pack *bp) { assert(event != nullptr); @@ -82,7 +57,7 @@ static bool tox_event_friend_connection_status_pack( } non_null() -static bool tox_event_friend_connection_status_unpack( +static bool tox_event_friend_connection_status_unpack_into( Tox_Event_Friend_Connection_Status *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -94,96 +69,7 @@ static bool tox_event_friend_connection_status_unpack( && tox_unpack_connection(bu, &event->connection_status); } - -/***************************************************** - * - * :: add/clear/get - * - *****************************************************/ - - -non_null() -static Tox_Event_Friend_Connection_Status *tox_events_add_friend_connection_status(Tox_Events *events) -{ - if (events->friend_connection_status_size == UINT32_MAX) { - return nullptr; - } - - if (events->friend_connection_status_size == events->friend_connection_status_capacity) { - const uint32_t new_friend_connection_status_capacity = events->friend_connection_status_capacity * 2 + 1; - Tox_Event_Friend_Connection_Status *new_friend_connection_status = (Tox_Event_Friend_Connection_Status *)realloc( - events->friend_connection_status, new_friend_connection_status_capacity * sizeof(Tox_Event_Friend_Connection_Status)); - - if (new_friend_connection_status == nullptr) { - return nullptr; - } - - events->friend_connection_status = new_friend_connection_status; - events->friend_connection_status_capacity = new_friend_connection_status_capacity; - } - - Tox_Event_Friend_Connection_Status *const friend_connection_status = - &events->friend_connection_status[events->friend_connection_status_size]; - tox_event_friend_connection_status_construct(friend_connection_status); - ++events->friend_connection_status_size; - return friend_connection_status; -} - -void tox_events_clear_friend_connection_status(Tox_Events *events) -{ - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->friend_connection_status_size; ++i) { - tox_event_friend_connection_status_destruct(&events->friend_connection_status[i]); - } - - free(events->friend_connection_status); - events->friend_connection_status = nullptr; - events->friend_connection_status_size = 0; - events->friend_connection_status_capacity = 0; -} - -uint32_t tox_events_get_friend_connection_status_size(const Tox_Events *events) -{ - if (events == nullptr) { - return 0; - } - - return events->friend_connection_status_size; -} - -const Tox_Event_Friend_Connection_Status *tox_events_get_friend_connection_status(const Tox_Events *events, - uint32_t index) -{ - assert(index < events->friend_connection_status_size); - assert(events->friend_connection_status != nullptr); - return &events->friend_connection_status[index]; -} - -bool tox_events_pack_friend_connection_status(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_friend_connection_status_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_friend_connection_status_pack(tox_events_get_friend_connection_status(events, i), bp)) { - return false; - } - } - return true; -} - -bool tox_events_unpack_friend_connection_status(Tox_Events *events, Bin_Unpack *bu) -{ - Tox_Event_Friend_Connection_Status *event = tox_events_add_friend_connection_status(events); - - if (event == nullptr) { - return false; - } - - return tox_event_friend_connection_status_unpack(event, bu); -} +EV_FUNCS(Friend_Connection_Status, friend_connection_status, FRIEND_CONNECTION_STATUS) /***************************************************** @@ -196,17 +82,9 @@ bool tox_events_unpack_friend_connection_status(Tox_Events *events, Bin_Unpack * void tox_events_handle_friend_connection_status(Tox *tox, uint32_t friend_number, Tox_Connection connection_status, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Friend_Connection_Status *friend_connection_status = tox_events_add_friend_connection_status(state->events); + Tox_Event_Friend_Connection_Status *friend_connection_status = tox_event_friend_connection_status_alloc(user_data); if (friend_connection_status == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/friend_lossless_packet.c b/toxcore/events/friend_lossless_packet.c index 944abd46c77..f0866404043 100644 --- a/toxcore/events/friend_lossless_packet.c +++ b/toxcore/events/friend_lossless_packet.c @@ -13,6 +13,8 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_private.h" +#include "event_macros.h" /***************************************************** @@ -28,6 +30,9 @@ struct Tox_Event_Friend_Lossless_Packet { uint32_t data_length; }; +EV_ACCESS_VALUE(Friend_Lossless_Packet, friend_lossless_packet, uint32_t, friend_number) +EV_ACCESS_ARRAY(Friend_Lossless_Packet, friend_lossless_packet, uint8_t, data) + non_null() static void tox_event_friend_lossless_packet_construct(Tox_Event_Friend_Lossless_Packet *friend_lossless_packet) { @@ -36,60 +41,12 @@ static void tox_event_friend_lossless_packet_construct(Tox_Event_Friend_Lossless }; } non_null() -static void tox_event_friend_lossless_packet_destruct(Tox_Event_Friend_Lossless_Packet *friend_lossless_packet) -{ - free(friend_lossless_packet->data); -} - -non_null() -static void tox_event_friend_lossless_packet_set_friend_number(Tox_Event_Friend_Lossless_Packet *friend_lossless_packet, - uint32_t friend_number) -{ - assert(friend_lossless_packet != nullptr); - friend_lossless_packet->friend_number = friend_number; -} -uint32_t tox_event_friend_lossless_packet_get_friend_number(const Tox_Event_Friend_Lossless_Packet - *friend_lossless_packet) -{ - assert(friend_lossless_packet != nullptr); - return friend_lossless_packet->friend_number; -} - -non_null() -static bool tox_event_friend_lossless_packet_set_data(Tox_Event_Friend_Lossless_Packet *friend_lossless_packet, - const uint8_t *data, uint32_t data_length) -{ - assert(friend_lossless_packet != nullptr); - - if (friend_lossless_packet->data != nullptr) { - free(friend_lossless_packet->data); - friend_lossless_packet->data = nullptr; - friend_lossless_packet->data_length = 0; - } - - friend_lossless_packet->data = (uint8_t *)malloc(data_length); - - if (friend_lossless_packet->data == nullptr) { - return false; - } - - memcpy(friend_lossless_packet->data, data, data_length); - friend_lossless_packet->data_length = data_length; - return true; -} -uint32_t tox_event_friend_lossless_packet_get_data_length(const Tox_Event_Friend_Lossless_Packet *friend_lossless_packet) +static void tox_event_friend_lossless_packet_destruct(Tox_Event_Friend_Lossless_Packet *friend_lossless_packet, const Memory *mem) { - assert(friend_lossless_packet != nullptr); - return friend_lossless_packet->data_length; -} -const uint8_t *tox_event_friend_lossless_packet_get_data(const Tox_Event_Friend_Lossless_Packet *friend_lossless_packet) -{ - assert(friend_lossless_packet != nullptr); - return friend_lossless_packet->data; + mem_delete(mem, friend_lossless_packet->data); } -non_null() -static bool tox_event_friend_lossless_packet_pack( +bool tox_event_friend_lossless_packet_pack( const Tox_Event_Friend_Lossless_Packet *event, Bin_Pack *bp) { assert(event != nullptr); @@ -101,7 +58,7 @@ static bool tox_event_friend_lossless_packet_pack( } non_null() -static bool tox_event_friend_lossless_packet_unpack( +static bool tox_event_friend_lossless_packet_unpack_into( Tox_Event_Friend_Lossless_Packet *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -113,95 +70,7 @@ static bool tox_event_friend_lossless_packet_unpack( && bin_unpack_bin(bu, &event->data, &event->data_length); } - -/***************************************************** - * - * :: add/clear/get - * - *****************************************************/ - - -non_null() -static Tox_Event_Friend_Lossless_Packet *tox_events_add_friend_lossless_packet(Tox_Events *events) -{ - if (events->friend_lossless_packet_size == UINT32_MAX) { - return nullptr; - } - - if (events->friend_lossless_packet_size == events->friend_lossless_packet_capacity) { - const uint32_t new_friend_lossless_packet_capacity = events->friend_lossless_packet_capacity * 2 + 1; - Tox_Event_Friend_Lossless_Packet *new_friend_lossless_packet = (Tox_Event_Friend_Lossless_Packet *)realloc( - events->friend_lossless_packet, new_friend_lossless_packet_capacity * sizeof(Tox_Event_Friend_Lossless_Packet)); - - if (new_friend_lossless_packet == nullptr) { - return nullptr; - } - - events->friend_lossless_packet = new_friend_lossless_packet; - events->friend_lossless_packet_capacity = new_friend_lossless_packet_capacity; - } - - Tox_Event_Friend_Lossless_Packet *const friend_lossless_packet = - &events->friend_lossless_packet[events->friend_lossless_packet_size]; - tox_event_friend_lossless_packet_construct(friend_lossless_packet); - ++events->friend_lossless_packet_size; - return friend_lossless_packet; -} - -void tox_events_clear_friend_lossless_packet(Tox_Events *events) -{ - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->friend_lossless_packet_size; ++i) { - tox_event_friend_lossless_packet_destruct(&events->friend_lossless_packet[i]); - } - - free(events->friend_lossless_packet); - events->friend_lossless_packet = nullptr; - events->friend_lossless_packet_size = 0; - events->friend_lossless_packet_capacity = 0; -} - -uint32_t tox_events_get_friend_lossless_packet_size(const Tox_Events *events) -{ - if (events == nullptr) { - return 0; - } - - return events->friend_lossless_packet_size; -} - -const Tox_Event_Friend_Lossless_Packet *tox_events_get_friend_lossless_packet(const Tox_Events *events, uint32_t index) -{ - assert(index < events->friend_lossless_packet_size); - assert(events->friend_lossless_packet != nullptr); - return &events->friend_lossless_packet[index]; -} - -bool tox_events_pack_friend_lossless_packet(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_friend_lossless_packet_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_friend_lossless_packet_pack(tox_events_get_friend_lossless_packet(events, i), bp)) { - return false; - } - } - return true; -} - -bool tox_events_unpack_friend_lossless_packet(Tox_Events *events, Bin_Unpack *bu) -{ - Tox_Event_Friend_Lossless_Packet *event = tox_events_add_friend_lossless_packet(events); - - if (event == nullptr) { - return false; - } - - return tox_event_friend_lossless_packet_unpack(event, bu); -} +EV_FUNCS(Friend_Lossless_Packet, friend_lossless_packet, FRIEND_LOSSLESS_PACKET) /***************************************************** @@ -214,20 +83,14 @@ bool tox_events_unpack_friend_lossless_packet(Tox_Events *events, Bin_Unpack *bu void tox_events_handle_friend_lossless_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Friend_Lossless_Packet *friend_lossless_packet = tox_events_add_friend_lossless_packet(state->events); + Tox_Event_Friend_Lossless_Packet *friend_lossless_packet = tox_event_friend_lossless_packet_alloc(user_data); if (friend_lossless_packet == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } + const Tox_System *sys = tox_get_system(tox); + tox_event_friend_lossless_packet_set_friend_number(friend_lossless_packet, friend_number); - tox_event_friend_lossless_packet_set_data(friend_lossless_packet, data, length); + tox_event_friend_lossless_packet_set_data(friend_lossless_packet, data, length, sys->mem); } diff --git a/toxcore/events/friend_lossy_packet.c b/toxcore/events/friend_lossy_packet.c index a787fce0cf7..ef6c25b4c7f 100644 --- a/toxcore/events/friend_lossy_packet.c +++ b/toxcore/events/friend_lossy_packet.c @@ -13,6 +13,8 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_private.h" +#include "event_macros.h" /***************************************************** @@ -28,6 +30,9 @@ struct Tox_Event_Friend_Lossy_Packet { uint32_t data_length; }; +EV_ACCESS_VALUE(Friend_Lossy_Packet, friend_lossy_packet, uint32_t, friend_number) +EV_ACCESS_ARRAY(Friend_Lossy_Packet, friend_lossy_packet, uint8_t, data) + non_null() static void tox_event_friend_lossy_packet_construct(Tox_Event_Friend_Lossy_Packet *friend_lossy_packet) { @@ -36,59 +41,12 @@ static void tox_event_friend_lossy_packet_construct(Tox_Event_Friend_Lossy_Packe }; } non_null() -static void tox_event_friend_lossy_packet_destruct(Tox_Event_Friend_Lossy_Packet *friend_lossy_packet) -{ - free(friend_lossy_packet->data); -} - -non_null() -static void tox_event_friend_lossy_packet_set_friend_number(Tox_Event_Friend_Lossy_Packet *friend_lossy_packet, - uint32_t friend_number) -{ - assert(friend_lossy_packet != nullptr); - friend_lossy_packet->friend_number = friend_number; -} -uint32_t tox_event_friend_lossy_packet_get_friend_number(const Tox_Event_Friend_Lossy_Packet *friend_lossy_packet) -{ - assert(friend_lossy_packet != nullptr); - return friend_lossy_packet->friend_number; -} - -non_null() -static bool tox_event_friend_lossy_packet_set_data(Tox_Event_Friend_Lossy_Packet *friend_lossy_packet, - const uint8_t *data, uint32_t data_length) -{ - assert(friend_lossy_packet != nullptr); - - if (friend_lossy_packet->data != nullptr) { - free(friend_lossy_packet->data); - friend_lossy_packet->data = nullptr; - friend_lossy_packet->data_length = 0; - } - - friend_lossy_packet->data = (uint8_t *)malloc(data_length); - - if (friend_lossy_packet->data == nullptr) { - return false; - } - - memcpy(friend_lossy_packet->data, data, data_length); - friend_lossy_packet->data_length = data_length; - return true; -} -uint32_t tox_event_friend_lossy_packet_get_data_length(const Tox_Event_Friend_Lossy_Packet *friend_lossy_packet) +static void tox_event_friend_lossy_packet_destruct(Tox_Event_Friend_Lossy_Packet *friend_lossy_packet, const Memory *mem) { - assert(friend_lossy_packet != nullptr); - return friend_lossy_packet->data_length; -} -const uint8_t *tox_event_friend_lossy_packet_get_data(const Tox_Event_Friend_Lossy_Packet *friend_lossy_packet) -{ - assert(friend_lossy_packet != nullptr); - return friend_lossy_packet->data; + mem_delete(mem, friend_lossy_packet->data); } -non_null() -static bool tox_event_friend_lossy_packet_pack( +bool tox_event_friend_lossy_packet_pack( const Tox_Event_Friend_Lossy_Packet *event, Bin_Pack *bp) { assert(event != nullptr); @@ -100,7 +58,7 @@ static bool tox_event_friend_lossy_packet_pack( } non_null() -static bool tox_event_friend_lossy_packet_unpack( +static bool tox_event_friend_lossy_packet_unpack_into( Tox_Event_Friend_Lossy_Packet *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -112,95 +70,7 @@ static bool tox_event_friend_lossy_packet_unpack( && bin_unpack_bin(bu, &event->data, &event->data_length); } - -/***************************************************** - * - * :: add/clear/get - * - *****************************************************/ - - -non_null() -static Tox_Event_Friend_Lossy_Packet *tox_events_add_friend_lossy_packet(Tox_Events *events) -{ - if (events->friend_lossy_packet_size == UINT32_MAX) { - return nullptr; - } - - if (events->friend_lossy_packet_size == events->friend_lossy_packet_capacity) { - const uint32_t new_friend_lossy_packet_capacity = events->friend_lossy_packet_capacity * 2 + 1; - Tox_Event_Friend_Lossy_Packet *new_friend_lossy_packet = (Tox_Event_Friend_Lossy_Packet *)realloc( - events->friend_lossy_packet, new_friend_lossy_packet_capacity * sizeof(Tox_Event_Friend_Lossy_Packet)); - - if (new_friend_lossy_packet == nullptr) { - return nullptr; - } - - events->friend_lossy_packet = new_friend_lossy_packet; - events->friend_lossy_packet_capacity = new_friend_lossy_packet_capacity; - } - - Tox_Event_Friend_Lossy_Packet *const friend_lossy_packet = - &events->friend_lossy_packet[events->friend_lossy_packet_size]; - tox_event_friend_lossy_packet_construct(friend_lossy_packet); - ++events->friend_lossy_packet_size; - return friend_lossy_packet; -} - -void tox_events_clear_friend_lossy_packet(Tox_Events *events) -{ - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->friend_lossy_packet_size; ++i) { - tox_event_friend_lossy_packet_destruct(&events->friend_lossy_packet[i]); - } - - free(events->friend_lossy_packet); - events->friend_lossy_packet = nullptr; - events->friend_lossy_packet_size = 0; - events->friend_lossy_packet_capacity = 0; -} - -uint32_t tox_events_get_friend_lossy_packet_size(const Tox_Events *events) -{ - if (events == nullptr) { - return 0; - } - - return events->friend_lossy_packet_size; -} - -const Tox_Event_Friend_Lossy_Packet *tox_events_get_friend_lossy_packet(const Tox_Events *events, uint32_t index) -{ - assert(index < events->friend_lossy_packet_size); - assert(events->friend_lossy_packet != nullptr); - return &events->friend_lossy_packet[index]; -} - -bool tox_events_pack_friend_lossy_packet(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_friend_lossy_packet_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_friend_lossy_packet_pack(tox_events_get_friend_lossy_packet(events, i), bp)) { - return false; - } - } - return true; -} - -bool tox_events_unpack_friend_lossy_packet(Tox_Events *events, Bin_Unpack *bu) -{ - Tox_Event_Friend_Lossy_Packet *event = tox_events_add_friend_lossy_packet(events); - - if (event == nullptr) { - return false; - } - - return tox_event_friend_lossy_packet_unpack(event, bu); -} +EV_FUNCS(Friend_Lossy_Packet, friend_lossy_packet, FRIEND_LOSSY_PACKET) /***************************************************** @@ -213,20 +83,14 @@ bool tox_events_unpack_friend_lossy_packet(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_friend_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Friend_Lossy_Packet *friend_lossy_packet = tox_events_add_friend_lossy_packet(state->events); + Tox_Event_Friend_Lossy_Packet *friend_lossy_packet = tox_event_friend_lossy_packet_alloc(user_data); if (friend_lossy_packet == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } + const Tox_System *sys = tox_get_system(tox); + tox_event_friend_lossy_packet_set_friend_number(friend_lossy_packet, friend_number); - tox_event_friend_lossy_packet_set_data(friend_lossy_packet, data, length); + tox_event_friend_lossy_packet_set_data(friend_lossy_packet, data, length, sys->mem); } diff --git a/toxcore/events/friend_message.c b/toxcore/events/friend_message.c index 8fa6a19e34d..07f47a23789 100644 --- a/toxcore/events/friend_message.c +++ b/toxcore/events/friend_message.c @@ -13,7 +13,9 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_private.h" #include "../tox_unpack.h" +#include "event_macros.h" /***************************************************** @@ -30,6 +32,10 @@ struct Tox_Event_Friend_Message { uint32_t message_length; }; +EV_ACCESS_VALUE(Friend_Message, friend_message, uint32_t, friend_number) +EV_ACCESS_VALUE(Friend_Message, friend_message, Tox_Message_Type, type) +EV_ACCESS_ARRAY(Friend_Message, friend_message, uint8_t, message) + non_null() static void tox_event_friend_message_construct(Tox_Event_Friend_Message *friend_message) { @@ -38,71 +44,12 @@ static void tox_event_friend_message_construct(Tox_Event_Friend_Message *friend_ }; } non_null() -static void tox_event_friend_message_destruct(Tox_Event_Friend_Message *friend_message) -{ - free(friend_message->message); -} - -non_null() -static void tox_event_friend_message_set_friend_number(Tox_Event_Friend_Message *friend_message, - uint32_t friend_number) -{ - assert(friend_message != nullptr); - friend_message->friend_number = friend_number; -} -uint32_t tox_event_friend_message_get_friend_number(const Tox_Event_Friend_Message *friend_message) -{ - assert(friend_message != nullptr); - return friend_message->friend_number; -} - -non_null() -static void tox_event_friend_message_set_type(Tox_Event_Friend_Message *friend_message, Tox_Message_Type type) -{ - assert(friend_message != nullptr); - friend_message->type = type; -} -Tox_Message_Type tox_event_friend_message_get_type(const Tox_Event_Friend_Message *friend_message) +static void tox_event_friend_message_destruct(Tox_Event_Friend_Message *friend_message, const Memory *mem) { - assert(friend_message != nullptr); - return friend_message->type; + mem_delete(mem, friend_message->message); } -non_null() -static bool tox_event_friend_message_set_message(Tox_Event_Friend_Message *friend_message, const uint8_t *message, - uint32_t message_length) -{ - assert(friend_message != nullptr); - - if (friend_message->message != nullptr) { - free(friend_message->message); - friend_message->message = nullptr; - friend_message->message_length = 0; - } - - friend_message->message = (uint8_t *)malloc(message_length); - - if (friend_message->message == nullptr) { - return false; - } - - memcpy(friend_message->message, message, message_length); - friend_message->message_length = message_length; - return true; -} -uint32_t tox_event_friend_message_get_message_length(const Tox_Event_Friend_Message *friend_message) -{ - assert(friend_message != nullptr); - return friend_message->message_length; -} -const uint8_t *tox_event_friend_message_get_message(const Tox_Event_Friend_Message *friend_message) -{ - assert(friend_message != nullptr); - return friend_message->message; -} - -non_null() -static bool tox_event_friend_message_pack( +bool tox_event_friend_message_pack( const Tox_Event_Friend_Message *event, Bin_Pack *bp) { assert(event != nullptr); @@ -115,7 +62,7 @@ static bool tox_event_friend_message_pack( } non_null() -static bool tox_event_friend_message_unpack( +static bool tox_event_friend_message_unpack_into( Tox_Event_Friend_Message *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -128,94 +75,7 @@ static bool tox_event_friend_message_unpack( && bin_unpack_bin(bu, &event->message, &event->message_length); } - -/***************************************************** - * - * :: add/clear/get - * - *****************************************************/ - - -non_null() -static Tox_Event_Friend_Message *tox_events_add_friend_message(Tox_Events *events) -{ - if (events->friend_message_size == UINT32_MAX) { - return nullptr; - } - - if (events->friend_message_size == events->friend_message_capacity) { - const uint32_t new_friend_message_capacity = events->friend_message_capacity * 2 + 1; - Tox_Event_Friend_Message *new_friend_message = (Tox_Event_Friend_Message *)realloc( - events->friend_message, new_friend_message_capacity * sizeof(Tox_Event_Friend_Message)); - - if (new_friend_message == nullptr) { - return nullptr; - } - - events->friend_message = new_friend_message; - events->friend_message_capacity = new_friend_message_capacity; - } - - Tox_Event_Friend_Message *const friend_message = &events->friend_message[events->friend_message_size]; - tox_event_friend_message_construct(friend_message); - ++events->friend_message_size; - return friend_message; -} - -void tox_events_clear_friend_message(Tox_Events *events) -{ - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->friend_message_size; ++i) { - tox_event_friend_message_destruct(&events->friend_message[i]); - } - - free(events->friend_message); - events->friend_message = nullptr; - events->friend_message_size = 0; - events->friend_message_capacity = 0; -} - -uint32_t tox_events_get_friend_message_size(const Tox_Events *events) -{ - if (events == nullptr) { - return 0; - } - - return events->friend_message_size; -} - -const Tox_Event_Friend_Message *tox_events_get_friend_message(const Tox_Events *events, uint32_t index) -{ - assert(index < events->friend_message_size); - assert(events->friend_message != nullptr); - return &events->friend_message[index]; -} - -bool tox_events_pack_friend_message(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_friend_message_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_friend_message_pack(tox_events_get_friend_message(events, i), bp)) { - return false; - } - } - return true; -} - -bool tox_events_unpack_friend_message(Tox_Events *events, Bin_Unpack *bu) -{ - Tox_Event_Friend_Message *event = tox_events_add_friend_message(events); - - if (event == nullptr) { - return false; - } - - return tox_event_friend_message_unpack(event, bu); -} +EV_FUNCS(Friend_Message, friend_message, FRIEND_MESSAGE) /***************************************************** @@ -228,21 +88,15 @@ bool tox_events_unpack_friend_message(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_friend_message(Tox *tox, uint32_t friend_number, Tox_Message_Type type, const uint8_t *message, size_t length, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Friend_Message *friend_message = tox_events_add_friend_message(state->events); + Tox_Event_Friend_Message *friend_message = tox_event_friend_message_alloc(user_data); if (friend_message == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } + const Tox_System *sys = tox_get_system(tox); + tox_event_friend_message_set_friend_number(friend_message, friend_number); tox_event_friend_message_set_type(friend_message, type); - tox_event_friend_message_set_message(friend_message, message, length); + tox_event_friend_message_set_message(friend_message, message, length, sys->mem); } diff --git a/toxcore/events/friend_name.c b/toxcore/events/friend_name.c index 123918386a2..53b908e1610 100644 --- a/toxcore/events/friend_name.c +++ b/toxcore/events/friend_name.c @@ -13,6 +13,8 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_private.h" +#include "event_macros.h" /***************************************************** @@ -28,6 +30,9 @@ struct Tox_Event_Friend_Name { uint32_t name_length; }; +EV_ACCESS_VALUE(Friend_Name, friend_name, uint32_t, friend_number) +EV_ACCESS_ARRAY(Friend_Name, friend_name, uint8_t, name) + non_null() static void tox_event_friend_name_construct(Tox_Event_Friend_Name *friend_name) { @@ -36,59 +41,12 @@ static void tox_event_friend_name_construct(Tox_Event_Friend_Name *friend_name) }; } non_null() -static void tox_event_friend_name_destruct(Tox_Event_Friend_Name *friend_name) -{ - free(friend_name->name); -} - -non_null() -static void tox_event_friend_name_set_friend_number(Tox_Event_Friend_Name *friend_name, - uint32_t friend_number) -{ - assert(friend_name != nullptr); - friend_name->friend_number = friend_number; -} -uint32_t tox_event_friend_name_get_friend_number(const Tox_Event_Friend_Name *friend_name) -{ - assert(friend_name != nullptr); - return friend_name->friend_number; -} - -non_null() -static bool tox_event_friend_name_set_name(Tox_Event_Friend_Name *friend_name, const uint8_t *name, - uint32_t name_length) -{ - assert(friend_name != nullptr); - - if (friend_name->name != nullptr) { - free(friend_name->name); - friend_name->name = nullptr; - friend_name->name_length = 0; - } - - friend_name->name = (uint8_t *)malloc(name_length); - - if (friend_name->name == nullptr) { - return false; - } - - memcpy(friend_name->name, name, name_length); - friend_name->name_length = name_length; - return true; -} -uint32_t tox_event_friend_name_get_name_length(const Tox_Event_Friend_Name *friend_name) +static void tox_event_friend_name_destruct(Tox_Event_Friend_Name *friend_name, const Memory *mem) { - assert(friend_name != nullptr); - return friend_name->name_length; -} -const uint8_t *tox_event_friend_name_get_name(const Tox_Event_Friend_Name *friend_name) -{ - assert(friend_name != nullptr); - return friend_name->name; + mem_delete(mem, friend_name->name); } -non_null() -static bool tox_event_friend_name_pack( +bool tox_event_friend_name_pack( const Tox_Event_Friend_Name *event, Bin_Pack *bp) { assert(event != nullptr); @@ -100,7 +58,7 @@ static bool tox_event_friend_name_pack( } non_null() -static bool tox_event_friend_name_unpack( +static bool tox_event_friend_name_unpack_into( Tox_Event_Friend_Name *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -112,94 +70,7 @@ static bool tox_event_friend_name_unpack( && bin_unpack_bin(bu, &event->name, &event->name_length); } - -/***************************************************** - * - * :: add/clear/get - * - *****************************************************/ - - -non_null() -static Tox_Event_Friend_Name *tox_events_add_friend_name(Tox_Events *events) -{ - if (events->friend_name_size == UINT32_MAX) { - return nullptr; - } - - if (events->friend_name_size == events->friend_name_capacity) { - const uint32_t new_friend_name_capacity = events->friend_name_capacity * 2 + 1; - Tox_Event_Friend_Name *new_friend_name = (Tox_Event_Friend_Name *)realloc( - events->friend_name, new_friend_name_capacity * sizeof(Tox_Event_Friend_Name)); - - if (new_friend_name == nullptr) { - return nullptr; - } - - events->friend_name = new_friend_name; - events->friend_name_capacity = new_friend_name_capacity; - } - - Tox_Event_Friend_Name *const friend_name = &events->friend_name[events->friend_name_size]; - tox_event_friend_name_construct(friend_name); - ++events->friend_name_size; - return friend_name; -} - -void tox_events_clear_friend_name(Tox_Events *events) -{ - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->friend_name_size; ++i) { - tox_event_friend_name_destruct(&events->friend_name[i]); - } - - free(events->friend_name); - events->friend_name = nullptr; - events->friend_name_size = 0; - events->friend_name_capacity = 0; -} - -uint32_t tox_events_get_friend_name_size(const Tox_Events *events) -{ - if (events == nullptr) { - return 0; - } - - return events->friend_name_size; -} - -const Tox_Event_Friend_Name *tox_events_get_friend_name(const Tox_Events *events, uint32_t index) -{ - assert(index < events->friend_name_size); - assert(events->friend_name != nullptr); - return &events->friend_name[index]; -} - -bool tox_events_pack_friend_name(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_friend_name_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_friend_name_pack(tox_events_get_friend_name(events, i), bp)) { - return false; - } - } - return true; -} - -bool tox_events_unpack_friend_name(Tox_Events *events, Bin_Unpack *bu) -{ - Tox_Event_Friend_Name *event = tox_events_add_friend_name(events); - - if (event == nullptr) { - return false; - } - - return tox_event_friend_name_unpack(event, bu); -} +EV_FUNCS(Friend_Name, friend_name, FRIEND_NAME) /***************************************************** @@ -212,20 +83,14 @@ bool tox_events_unpack_friend_name(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_friend_name(Tox *tox, uint32_t friend_number, const uint8_t *name, size_t length, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Friend_Name *friend_name = tox_events_add_friend_name(state->events); + Tox_Event_Friend_Name *friend_name = tox_event_friend_name_alloc(user_data); if (friend_name == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } + const Tox_System *sys = tox_get_system(tox); + tox_event_friend_name_set_friend_number(friend_name, friend_number); - tox_event_friend_name_set_name(friend_name, name, length); + tox_event_friend_name_set_name(friend_name, name, length, sys->mem); } diff --git a/toxcore/events/friend_read_receipt.c b/toxcore/events/friend_read_receipt.c index 1485b67cca7..fb7568f41b3 100644 --- a/toxcore/events/friend_read_receipt.c +++ b/toxcore/events/friend_read_receipt.c @@ -13,6 +13,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "event_macros.h" /***************************************************** @@ -27,6 +28,9 @@ struct Tox_Event_Friend_Read_Receipt { uint32_t message_id; }; +EV_ACCESS_VALUE(Friend_Read_Receipt, friend_read_receipt, uint32_t, friend_number) +EV_ACCESS_VALUE(Friend_Read_Receipt, friend_read_receipt, uint32_t, message_id) + non_null() static void tox_event_friend_read_receipt_construct(Tox_Event_Friend_Read_Receipt *friend_read_receipt) { @@ -35,39 +39,12 @@ static void tox_event_friend_read_receipt_construct(Tox_Event_Friend_Read_Receip }; } non_null() -static void tox_event_friend_read_receipt_destruct(Tox_Event_Friend_Read_Receipt *friend_read_receipt) +static void tox_event_friend_read_receipt_destruct(Tox_Event_Friend_Read_Receipt *friend_read_receipt, const Memory *mem) { return; } -non_null() -static void tox_event_friend_read_receipt_set_friend_number(Tox_Event_Friend_Read_Receipt *friend_read_receipt, - uint32_t friend_number) -{ - assert(friend_read_receipt != nullptr); - friend_read_receipt->friend_number = friend_number; -} -uint32_t tox_event_friend_read_receipt_get_friend_number(const Tox_Event_Friend_Read_Receipt *friend_read_receipt) -{ - assert(friend_read_receipt != nullptr); - return friend_read_receipt->friend_number; -} - -non_null() -static void tox_event_friend_read_receipt_set_message_id(Tox_Event_Friend_Read_Receipt *friend_read_receipt, - uint32_t message_id) -{ - assert(friend_read_receipt != nullptr); - friend_read_receipt->message_id = message_id; -} -uint32_t tox_event_friend_read_receipt_get_message_id(const Tox_Event_Friend_Read_Receipt *friend_read_receipt) -{ - assert(friend_read_receipt != nullptr); - return friend_read_receipt->message_id; -} - -non_null() -static bool tox_event_friend_read_receipt_pack( +bool tox_event_friend_read_receipt_pack( const Tox_Event_Friend_Read_Receipt *event, Bin_Pack *bp) { assert(event != nullptr); @@ -79,7 +56,7 @@ static bool tox_event_friend_read_receipt_pack( } non_null() -static bool tox_event_friend_read_receipt_unpack( +static bool tox_event_friend_read_receipt_unpack_into( Tox_Event_Friend_Read_Receipt *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -91,95 +68,7 @@ static bool tox_event_friend_read_receipt_unpack( && bin_unpack_u32(bu, &event->message_id); } - -/***************************************************** - * - * :: add/clear/get - * - *****************************************************/ - - -non_null() -static Tox_Event_Friend_Read_Receipt *tox_events_add_friend_read_receipt(Tox_Events *events) -{ - if (events->friend_read_receipt_size == UINT32_MAX) { - return nullptr; - } - - if (events->friend_read_receipt_size == events->friend_read_receipt_capacity) { - const uint32_t new_friend_read_receipt_capacity = events->friend_read_receipt_capacity * 2 + 1; - Tox_Event_Friend_Read_Receipt *new_friend_read_receipt = (Tox_Event_Friend_Read_Receipt *)realloc( - events->friend_read_receipt, new_friend_read_receipt_capacity * sizeof(Tox_Event_Friend_Read_Receipt)); - - if (new_friend_read_receipt == nullptr) { - return nullptr; - } - - events->friend_read_receipt = new_friend_read_receipt; - events->friend_read_receipt_capacity = new_friend_read_receipt_capacity; - } - - Tox_Event_Friend_Read_Receipt *const friend_read_receipt = - &events->friend_read_receipt[events->friend_read_receipt_size]; - tox_event_friend_read_receipt_construct(friend_read_receipt); - ++events->friend_read_receipt_size; - return friend_read_receipt; -} - -void tox_events_clear_friend_read_receipt(Tox_Events *events) -{ - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->friend_read_receipt_size; ++i) { - tox_event_friend_read_receipt_destruct(&events->friend_read_receipt[i]); - } - - free(events->friend_read_receipt); - events->friend_read_receipt = nullptr; - events->friend_read_receipt_size = 0; - events->friend_read_receipt_capacity = 0; -} - -uint32_t tox_events_get_friend_read_receipt_size(const Tox_Events *events) -{ - if (events == nullptr) { - return 0; - } - - return events->friend_read_receipt_size; -} - -const Tox_Event_Friend_Read_Receipt *tox_events_get_friend_read_receipt(const Tox_Events *events, uint32_t index) -{ - assert(index < events->friend_read_receipt_size); - assert(events->friend_read_receipt != nullptr); - return &events->friend_read_receipt[index]; -} - -bool tox_events_pack_friend_read_receipt(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_friend_read_receipt_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_friend_read_receipt_pack(tox_events_get_friend_read_receipt(events, i), bp)) { - return false; - } - } - return true; -} - -bool tox_events_unpack_friend_read_receipt(Tox_Events *events, Bin_Unpack *bu) -{ - Tox_Event_Friend_Read_Receipt *event = tox_events_add_friend_read_receipt(events); - - if (event == nullptr) { - return false; - } - - return tox_event_friend_read_receipt_unpack(event, bu); -} +EV_FUNCS(Friend_Read_Receipt, friend_read_receipt, FRIEND_READ_RECEIPT) /***************************************************** @@ -191,17 +80,9 @@ bool tox_events_unpack_friend_read_receipt(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_friend_read_receipt(Tox *tox, uint32_t friend_number, uint32_t message_id, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Friend_Read_Receipt *friend_read_receipt = tox_events_add_friend_read_receipt(state->events); + Tox_Event_Friend_Read_Receipt *friend_read_receipt = tox_event_friend_read_receipt_alloc(user_data); if (friend_read_receipt == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/friend_request.c b/toxcore/events/friend_request.c index 3366f54fe47..e6cdf7c7693 100644 --- a/toxcore/events/friend_request.c +++ b/toxcore/events/friend_request.c @@ -13,6 +13,8 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_private.h" +#include "event_macros.h" /***************************************************** @@ -28,6 +30,9 @@ struct Tox_Event_Friend_Request { uint32_t message_length; }; +EV_ACCESS_FIXED(Friend_Request, friend_request, uint8_t, public_key, TOX_PUBLIC_KEY_SIZE) +EV_ACCESS_ARRAY(Friend_Request, friend_request, uint8_t, message) + non_null() static void tox_event_friend_request_construct(Tox_Event_Friend_Request *friend_request) { @@ -36,60 +41,12 @@ static void tox_event_friend_request_construct(Tox_Event_Friend_Request *friend_ }; } non_null() -static void tox_event_friend_request_destruct(Tox_Event_Friend_Request *friend_request) -{ - free(friend_request->message); -} - -non_null() -static bool tox_event_friend_request_set_public_key(Tox_Event_Friend_Request *friend_request, const uint8_t *public_key) +static void tox_event_friend_request_destruct(Tox_Event_Friend_Request *friend_request, const Memory *mem) { - assert(friend_request != nullptr); - - memcpy(friend_request->public_key, public_key, TOX_PUBLIC_KEY_SIZE); - return true; -} -const uint8_t *tox_event_friend_request_get_public_key(const Tox_Event_Friend_Request *friend_request) -{ - assert(friend_request != nullptr); - return friend_request->public_key; + mem_delete(mem, friend_request->message); } -non_null() -static bool tox_event_friend_request_set_message(Tox_Event_Friend_Request *friend_request, const uint8_t *message, - uint32_t message_length) -{ - assert(friend_request != nullptr); - - if (friend_request->message != nullptr) { - free(friend_request->message); - friend_request->message = nullptr; - friend_request->message_length = 0; - } - - friend_request->message = (uint8_t *)malloc(message_length); - - if (friend_request->message == nullptr) { - return false; - } - - memcpy(friend_request->message, message, message_length); - friend_request->message_length = message_length; - return true; -} -uint32_t tox_event_friend_request_get_message_length(const Tox_Event_Friend_Request *friend_request) -{ - assert(friend_request != nullptr); - return friend_request->message_length; -} -const uint8_t *tox_event_friend_request_get_message(const Tox_Event_Friend_Request *friend_request) -{ - assert(friend_request != nullptr); - return friend_request->message; -} - -non_null() -static bool tox_event_friend_request_pack( +bool tox_event_friend_request_pack( const Tox_Event_Friend_Request *event, Bin_Pack *bp) { assert(event != nullptr); @@ -101,7 +58,7 @@ static bool tox_event_friend_request_pack( } non_null() -static bool tox_event_friend_request_unpack( +static bool tox_event_friend_request_unpack_into( Tox_Event_Friend_Request *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -113,94 +70,7 @@ static bool tox_event_friend_request_unpack( && bin_unpack_bin(bu, &event->message, &event->message_length); } - -/***************************************************** - * - * :: add/clear/get - * - *****************************************************/ - - -non_null() -static Tox_Event_Friend_Request *tox_events_add_friend_request(Tox_Events *events) -{ - if (events->friend_request_size == UINT32_MAX) { - return nullptr; - } - - if (events->friend_request_size == events->friend_request_capacity) { - const uint32_t new_friend_request_capacity = events->friend_request_capacity * 2 + 1; - Tox_Event_Friend_Request *new_friend_request = (Tox_Event_Friend_Request *)realloc( - events->friend_request, new_friend_request_capacity * sizeof(Tox_Event_Friend_Request)); - - if (new_friend_request == nullptr) { - return nullptr; - } - - events->friend_request = new_friend_request; - events->friend_request_capacity = new_friend_request_capacity; - } - - Tox_Event_Friend_Request *const friend_request = &events->friend_request[events->friend_request_size]; - tox_event_friend_request_construct(friend_request); - ++events->friend_request_size; - return friend_request; -} - -void tox_events_clear_friend_request(Tox_Events *events) -{ - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->friend_request_size; ++i) { - tox_event_friend_request_destruct(&events->friend_request[i]); - } - - free(events->friend_request); - events->friend_request = nullptr; - events->friend_request_size = 0; - events->friend_request_capacity = 0; -} - -uint32_t tox_events_get_friend_request_size(const Tox_Events *events) -{ - if (events == nullptr) { - return 0; - } - - return events->friend_request_size; -} - -const Tox_Event_Friend_Request *tox_events_get_friend_request(const Tox_Events *events, uint32_t index) -{ - assert(index < events->friend_request_size); - assert(events->friend_request != nullptr); - return &events->friend_request[index]; -} - -bool tox_events_pack_friend_request(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_friend_request_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_friend_request_pack(tox_events_get_friend_request(events, i), bp)) { - return false; - } - } - return true; -} - -bool tox_events_unpack_friend_request(Tox_Events *events, Bin_Unpack *bu) -{ - Tox_Event_Friend_Request *event = tox_events_add_friend_request(events); - - if (event == nullptr) { - return false; - } - - return tox_event_friend_request_unpack(event, bu); -} +EV_FUNCS(Friend_Request, friend_request, FRIEND_REQUEST) /***************************************************** @@ -213,20 +83,14 @@ bool tox_events_unpack_friend_request(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_friend_request(Tox *tox, const uint8_t *public_key, const uint8_t *message, size_t length, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Friend_Request *friend_request = tox_events_add_friend_request(state->events); + Tox_Event_Friend_Request *friend_request = tox_event_friend_request_alloc(user_data); if (friend_request == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } + const Tox_System *sys = tox_get_system(tox); + tox_event_friend_request_set_public_key(friend_request, public_key); - tox_event_friend_request_set_message(friend_request, message, length); + tox_event_friend_request_set_message(friend_request, message, length, sys->mem); } diff --git a/toxcore/events/friend_status.c b/toxcore/events/friend_status.c index c6eabdaa474..43421f170d4 100644 --- a/toxcore/events/friend_status.c +++ b/toxcore/events/friend_status.c @@ -14,6 +14,7 @@ #include "../tox.h" #include "../tox_events.h" #include "../tox_unpack.h" +#include "event_macros.h" /***************************************************** @@ -28,6 +29,9 @@ struct Tox_Event_Friend_Status { Tox_User_Status status; }; +EV_ACCESS_VALUE(Friend_Status, friend_status, uint32_t, friend_number) +EV_ACCESS_VALUE(Friend_Status, friend_status, Tox_User_Status, status) + non_null() static void tox_event_friend_status_construct(Tox_Event_Friend_Status *friend_status) { @@ -36,39 +40,12 @@ static void tox_event_friend_status_construct(Tox_Event_Friend_Status *friend_st }; } non_null() -static void tox_event_friend_status_destruct(Tox_Event_Friend_Status *friend_status) +static void tox_event_friend_status_destruct(Tox_Event_Friend_Status *friend_status, const Memory *mem) { return; } -non_null() -static void tox_event_friend_status_set_friend_number(Tox_Event_Friend_Status *friend_status, - uint32_t friend_number) -{ - assert(friend_status != nullptr); - friend_status->friend_number = friend_number; -} -uint32_t tox_event_friend_status_get_friend_number(const Tox_Event_Friend_Status *friend_status) -{ - assert(friend_status != nullptr); - return friend_status->friend_number; -} - -non_null() -static void tox_event_friend_status_set_status(Tox_Event_Friend_Status *friend_status, - Tox_User_Status status) -{ - assert(friend_status != nullptr); - friend_status->status = status; -} -Tox_User_Status tox_event_friend_status_get_status(const Tox_Event_Friend_Status *friend_status) -{ - assert(friend_status != nullptr); - return friend_status->status; -} - -non_null() -static bool tox_event_friend_status_pack( +bool tox_event_friend_status_pack( const Tox_Event_Friend_Status *event, Bin_Pack *bp) { assert(event != nullptr); @@ -80,7 +57,7 @@ static bool tox_event_friend_status_pack( } non_null() -static bool tox_event_friend_status_unpack( +static bool tox_event_friend_status_unpack_into( Tox_Event_Friend_Status *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -92,94 +69,7 @@ static bool tox_event_friend_status_unpack( && tox_unpack_user_status(bu, &event->status); } - -/***************************************************** - * - * :: add/clear/get - * - *****************************************************/ - - -non_null() -static Tox_Event_Friend_Status *tox_events_add_friend_status(Tox_Events *events) -{ - if (events->friend_status_size == UINT32_MAX) { - return nullptr; - } - - if (events->friend_status_size == events->friend_status_capacity) { - const uint32_t new_friend_status_capacity = events->friend_status_capacity * 2 + 1; - Tox_Event_Friend_Status *new_friend_status = (Tox_Event_Friend_Status *)realloc( - events->friend_status, new_friend_status_capacity * sizeof(Tox_Event_Friend_Status)); - - if (new_friend_status == nullptr) { - return nullptr; - } - - events->friend_status = new_friend_status; - events->friend_status_capacity = new_friend_status_capacity; - } - - Tox_Event_Friend_Status *const friend_status = &events->friend_status[events->friend_status_size]; - tox_event_friend_status_construct(friend_status); - ++events->friend_status_size; - return friend_status; -} - -void tox_events_clear_friend_status(Tox_Events *events) -{ - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->friend_status_size; ++i) { - tox_event_friend_status_destruct(&events->friend_status[i]); - } - - free(events->friend_status); - events->friend_status = nullptr; - events->friend_status_size = 0; - events->friend_status_capacity = 0; -} - -uint32_t tox_events_get_friend_status_size(const Tox_Events *events) -{ - if (events == nullptr) { - return 0; - } - - return events->friend_status_size; -} - -const Tox_Event_Friend_Status *tox_events_get_friend_status(const Tox_Events *events, uint32_t index) -{ - assert(index < events->friend_status_size); - assert(events->friend_status != nullptr); - return &events->friend_status[index]; -} - -bool tox_events_pack_friend_status(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_friend_status_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_friend_status_pack(tox_events_get_friend_status(events, i), bp)) { - return false; - } - } - return true; -} - -bool tox_events_unpack_friend_status(Tox_Events *events, Bin_Unpack *bu) -{ - Tox_Event_Friend_Status *event = tox_events_add_friend_status(events); - - if (event == nullptr) { - return false; - } - - return tox_event_friend_status_unpack(event, bu); -} +EV_FUNCS(Friend_Status, friend_status, FRIEND_STATUS) /***************************************************** @@ -192,17 +82,9 @@ bool tox_events_unpack_friend_status(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_friend_status(Tox *tox, uint32_t friend_number, Tox_User_Status status, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Friend_Status *friend_status = tox_events_add_friend_status(state->events); + Tox_Event_Friend_Status *friend_status = tox_event_friend_status_alloc(user_data); if (friend_status == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/friend_status_message.c b/toxcore/events/friend_status_message.c index 91fa88e3212..6054691c52f 100644 --- a/toxcore/events/friend_status_message.c +++ b/toxcore/events/friend_status_message.c @@ -13,6 +13,8 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_private.h" +#include "event_macros.h" /***************************************************** @@ -28,6 +30,9 @@ struct Tox_Event_Friend_Status_Message { uint32_t message_length; }; +EV_ACCESS_VALUE(Friend_Status_Message, friend_status_message, uint32_t, friend_number) +EV_ACCESS_ARRAY(Friend_Status_Message, friend_status_message, uint8_t, message) + non_null() static void tox_event_friend_status_message_construct(Tox_Event_Friend_Status_Message *friend_status_message) { @@ -36,61 +41,12 @@ static void tox_event_friend_status_message_construct(Tox_Event_Friend_Status_Me }; } non_null() -static void tox_event_friend_status_message_destruct(Tox_Event_Friend_Status_Message *friend_status_message) -{ - free(friend_status_message->message); -} - -non_null() -static void tox_event_friend_status_message_set_friend_number(Tox_Event_Friend_Status_Message *friend_status_message, - uint32_t friend_number) -{ - assert(friend_status_message != nullptr); - friend_status_message->friend_number = friend_number; -} -uint32_t tox_event_friend_status_message_get_friend_number(const Tox_Event_Friend_Status_Message *friend_status_message) -{ - assert(friend_status_message != nullptr); - return friend_status_message->friend_number; -} - -non_null() -static bool tox_event_friend_status_message_set_message(Tox_Event_Friend_Status_Message *friend_status_message, - const uint8_t *message, uint32_t message_length) -{ - assert(friend_status_message != nullptr); - - if (friend_status_message->message != nullptr) { - free(friend_status_message->message); - friend_status_message->message = nullptr; - friend_status_message->message_length = 0; - } - - friend_status_message->message = (uint8_t *)malloc(message_length); - - if (friend_status_message->message == nullptr) { - return false; - } - - memcpy(friend_status_message->message, message, message_length); - friend_status_message->message_length = message_length; - return true; -} -uint32_t tox_event_friend_status_message_get_message_length(const Tox_Event_Friend_Status_Message - *friend_status_message) +static void tox_event_friend_status_message_destruct(Tox_Event_Friend_Status_Message *friend_status_message, const Memory *mem) { - assert(friend_status_message != nullptr); - return friend_status_message->message_length; -} -const uint8_t *tox_event_friend_status_message_get_message(const Tox_Event_Friend_Status_Message - *friend_status_message) -{ - assert(friend_status_message != nullptr); - return friend_status_message->message; + mem_delete(mem, friend_status_message->message); } -non_null() -static bool tox_event_friend_status_message_pack( +bool tox_event_friend_status_message_pack( const Tox_Event_Friend_Status_Message *event, Bin_Pack *bp) { assert(event != nullptr); @@ -102,7 +58,7 @@ static bool tox_event_friend_status_message_pack( } non_null() -static bool tox_event_friend_status_message_unpack( +static bool tox_event_friend_status_message_unpack_into( Tox_Event_Friend_Status_Message *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -114,95 +70,7 @@ static bool tox_event_friend_status_message_unpack( && bin_unpack_bin(bu, &event->message, &event->message_length); } - -/***************************************************** - * - * :: add/clear/get - * - *****************************************************/ - - -non_null() -static Tox_Event_Friend_Status_Message *tox_events_add_friend_status_message(Tox_Events *events) -{ - if (events->friend_status_message_size == UINT32_MAX) { - return nullptr; - } - - if (events->friend_status_message_size == events->friend_status_message_capacity) { - const uint32_t new_friend_status_message_capacity = events->friend_status_message_capacity * 2 + 1; - Tox_Event_Friend_Status_Message *new_friend_status_message = (Tox_Event_Friend_Status_Message *)realloc( - events->friend_status_message, new_friend_status_message_capacity * sizeof(Tox_Event_Friend_Status_Message)); - - if (new_friend_status_message == nullptr) { - return nullptr; - } - - events->friend_status_message = new_friend_status_message; - events->friend_status_message_capacity = new_friend_status_message_capacity; - } - - Tox_Event_Friend_Status_Message *const friend_status_message = - &events->friend_status_message[events->friend_status_message_size]; - tox_event_friend_status_message_construct(friend_status_message); - ++events->friend_status_message_size; - return friend_status_message; -} - -void tox_events_clear_friend_status_message(Tox_Events *events) -{ - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->friend_status_message_size; ++i) { - tox_event_friend_status_message_destruct(&events->friend_status_message[i]); - } - - free(events->friend_status_message); - events->friend_status_message = nullptr; - events->friend_status_message_size = 0; - events->friend_status_message_capacity = 0; -} - -uint32_t tox_events_get_friend_status_message_size(const Tox_Events *events) -{ - if (events == nullptr) { - return 0; - } - - return events->friend_status_message_size; -} - -const Tox_Event_Friend_Status_Message *tox_events_get_friend_status_message(const Tox_Events *events, uint32_t index) -{ - assert(index < events->friend_status_message_size); - assert(events->friend_status_message != nullptr); - return &events->friend_status_message[index]; -} - -bool tox_events_pack_friend_status_message(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_friend_status_message_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_friend_status_message_pack(tox_events_get_friend_status_message(events, i), bp)) { - return false; - } - } - return true; -} - -bool tox_events_unpack_friend_status_message(Tox_Events *events, Bin_Unpack *bu) -{ - Tox_Event_Friend_Status_Message *event = tox_events_add_friend_status_message(events); - - if (event == nullptr) { - return false; - } - - return tox_event_friend_status_message_unpack(event, bu); -} +EV_FUNCS(Friend_Status_Message, friend_status_message, FRIEND_STATUS_MESSAGE) /***************************************************** @@ -215,20 +83,14 @@ bool tox_events_unpack_friend_status_message(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_friend_status_message(Tox *tox, uint32_t friend_number, const uint8_t *message, size_t length, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Friend_Status_Message *friend_status_message = tox_events_add_friend_status_message(state->events); + Tox_Event_Friend_Status_Message *friend_status_message = tox_event_friend_status_message_alloc(user_data); if (friend_status_message == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } + const Tox_System *sys = tox_get_system(tox); + tox_event_friend_status_message_set_friend_number(friend_status_message, friend_number); - tox_event_friend_status_message_set_message(friend_status_message, message, length); + tox_event_friend_status_message_set_message(friend_status_message, message, length, sys->mem); } diff --git a/toxcore/events/friend_typing.c b/toxcore/events/friend_typing.c index 83c4dbac63e..3376f07f681 100644 --- a/toxcore/events/friend_typing.c +++ b/toxcore/events/friend_typing.c @@ -13,6 +13,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "event_macros.h" /***************************************************** @@ -27,6 +28,9 @@ struct Tox_Event_Friend_Typing { bool typing; }; +EV_ACCESS_VALUE(Friend_Typing, friend_typing, uint32_t, friend_number) +EV_ACCESS_VALUE(Friend_Typing, friend_typing, bool, typing) + non_null() static void tox_event_friend_typing_construct(Tox_Event_Friend_Typing *friend_typing) { @@ -35,38 +39,12 @@ static void tox_event_friend_typing_construct(Tox_Event_Friend_Typing *friend_ty }; } non_null() -static void tox_event_friend_typing_destruct(Tox_Event_Friend_Typing *friend_typing) +static void tox_event_friend_typing_destruct(Tox_Event_Friend_Typing *friend_typing, const Memory *mem) { return; } -non_null() -static void tox_event_friend_typing_set_friend_number(Tox_Event_Friend_Typing *friend_typing, - uint32_t friend_number) -{ - assert(friend_typing != nullptr); - friend_typing->friend_number = friend_number; -} -uint32_t tox_event_friend_typing_get_friend_number(const Tox_Event_Friend_Typing *friend_typing) -{ - assert(friend_typing != nullptr); - return friend_typing->friend_number; -} - -non_null() -static void tox_event_friend_typing_set_typing(Tox_Event_Friend_Typing *friend_typing, bool typing) -{ - assert(friend_typing != nullptr); - friend_typing->typing = typing; -} -bool tox_event_friend_typing_get_typing(const Tox_Event_Friend_Typing *friend_typing) -{ - assert(friend_typing != nullptr); - return friend_typing->typing; -} - -non_null() -static bool tox_event_friend_typing_pack( +bool tox_event_friend_typing_pack( const Tox_Event_Friend_Typing *event, Bin_Pack *bp) { assert(event != nullptr); @@ -78,7 +56,7 @@ static bool tox_event_friend_typing_pack( } non_null() -static bool tox_event_friend_typing_unpack( +static bool tox_event_friend_typing_unpack_into( Tox_Event_Friend_Typing *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -90,94 +68,7 @@ static bool tox_event_friend_typing_unpack( && bin_unpack_bool(bu, &event->typing); } - -/***************************************************** - * - * :: add/clear/get - * - *****************************************************/ - - -non_null() -static Tox_Event_Friend_Typing *tox_events_add_friend_typing(Tox_Events *events) -{ - if (events->friend_typing_size == UINT32_MAX) { - return nullptr; - } - - if (events->friend_typing_size == events->friend_typing_capacity) { - const uint32_t new_friend_typing_capacity = events->friend_typing_capacity * 2 + 1; - Tox_Event_Friend_Typing *new_friend_typing = (Tox_Event_Friend_Typing *)realloc( - events->friend_typing, new_friend_typing_capacity * sizeof(Tox_Event_Friend_Typing)); - - if (new_friend_typing == nullptr) { - return nullptr; - } - - events->friend_typing = new_friend_typing; - events->friend_typing_capacity = new_friend_typing_capacity; - } - - Tox_Event_Friend_Typing *const friend_typing = &events->friend_typing[events->friend_typing_size]; - tox_event_friend_typing_construct(friend_typing); - ++events->friend_typing_size; - return friend_typing; -} - -void tox_events_clear_friend_typing(Tox_Events *events) -{ - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->friend_typing_size; ++i) { - tox_event_friend_typing_destruct(&events->friend_typing[i]); - } - - free(events->friend_typing); - events->friend_typing = nullptr; - events->friend_typing_size = 0; - events->friend_typing_capacity = 0; -} - -uint32_t tox_events_get_friend_typing_size(const Tox_Events *events) -{ - if (events == nullptr) { - return 0; - } - - return events->friend_typing_size; -} - -const Tox_Event_Friend_Typing *tox_events_get_friend_typing(const Tox_Events *events, uint32_t index) -{ - assert(index < events->friend_typing_size); - assert(events->friend_typing != nullptr); - return &events->friend_typing[index]; -} - -bool tox_events_pack_friend_typing(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_friend_typing_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_friend_typing_pack(tox_events_get_friend_typing(events, i), bp)) { - return false; - } - } - return true; -} - -bool tox_events_unpack_friend_typing(Tox_Events *events, Bin_Unpack *bu) -{ - Tox_Event_Friend_Typing *event = tox_events_add_friend_typing(events); - - if (event == nullptr) { - return false; - } - - return tox_event_friend_typing_unpack(event, bu); -} +EV_FUNCS(Friend_Typing, friend_typing, FRIEND_TYPING) /***************************************************** @@ -189,17 +80,9 @@ bool tox_events_unpack_friend_typing(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_friend_typing(Tox *tox, uint32_t friend_number, bool typing, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Friend_Typing *friend_typing = tox_events_add_friend_typing(state->events); + Tox_Event_Friend_Typing *friend_typing = tox_event_friend_typing_alloc(user_data); if (friend_typing == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/self_connection_status.c b/toxcore/events/self_connection_status.c index 0e381de7fa8..9ef6142aefd 100644 --- a/toxcore/events/self_connection_status.c +++ b/toxcore/events/self_connection_status.c @@ -14,6 +14,7 @@ #include "../tox.h" #include "../tox_events.h" #include "../tox_unpack.h" +#include "event_macros.h" /***************************************************** @@ -27,6 +28,8 @@ struct Tox_Event_Self_Connection_Status { Tox_Connection connection_status; }; +EV_ACCESS_VALUE(Self_Connection_Status, self_connection_status, Tox_Connection, connection_status) + non_null() static void tox_event_self_connection_status_construct(Tox_Event_Self_Connection_Status *self_connection_status) { @@ -35,27 +38,12 @@ static void tox_event_self_connection_status_construct(Tox_Event_Self_Connection }; } non_null() -static void tox_event_self_connection_status_destruct(Tox_Event_Self_Connection_Status *self_connection_status) +static void tox_event_self_connection_status_destruct(Tox_Event_Self_Connection_Status *self_connection_status, const Memory *mem) { return; } -non_null() -static void tox_event_self_connection_status_set_connection_status(Tox_Event_Self_Connection_Status - *self_connection_status, Tox_Connection connection_status) -{ - assert(self_connection_status != nullptr); - self_connection_status->connection_status = connection_status; -} -Tox_Connection tox_event_self_connection_status_get_connection_status(const Tox_Event_Self_Connection_Status - *self_connection_status) -{ - assert(self_connection_status != nullptr); - return self_connection_status->connection_status; -} - -non_null() -static bool tox_event_self_connection_status_pack( +bool tox_event_self_connection_status_pack( const Tox_Event_Self_Connection_Status *event, Bin_Pack *bp) { assert(event != nullptr); @@ -65,102 +53,14 @@ static bool tox_event_self_connection_status_pack( } non_null() -static bool tox_event_self_connection_status_unpack( +static bool tox_event_self_connection_status_unpack_into( Tox_Event_Self_Connection_Status *event, Bin_Unpack *bu) { assert(event != nullptr); return tox_unpack_connection(bu, &event->connection_status); } - -/***************************************************** - * - * :: add/clear/get - * - *****************************************************/ - - -non_null() -static Tox_Event_Self_Connection_Status *tox_events_add_self_connection_status(Tox_Events *events) -{ - if (events->self_connection_status_size == UINT32_MAX) { - return nullptr; - } - - if (events->self_connection_status_size == events->self_connection_status_capacity) { - const uint32_t new_self_connection_status_capacity = events->self_connection_status_capacity * 2 + 1; - Tox_Event_Self_Connection_Status *new_self_connection_status = (Tox_Event_Self_Connection_Status *)realloc( - events->self_connection_status, new_self_connection_status_capacity * sizeof(Tox_Event_Self_Connection_Status)); - - if (new_self_connection_status == nullptr) { - return nullptr; - } - - events->self_connection_status = new_self_connection_status; - events->self_connection_status_capacity = new_self_connection_status_capacity; - } - - Tox_Event_Self_Connection_Status *const self_connection_status = - &events->self_connection_status[events->self_connection_status_size]; - tox_event_self_connection_status_construct(self_connection_status); - ++events->self_connection_status_size; - return self_connection_status; -} - -void tox_events_clear_self_connection_status(Tox_Events *events) -{ - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->self_connection_status_size; ++i) { - tox_event_self_connection_status_destruct(&events->self_connection_status[i]); - } - - free(events->self_connection_status); - events->self_connection_status = nullptr; - events->self_connection_status_size = 0; - events->self_connection_status_capacity = 0; -} - -uint32_t tox_events_get_self_connection_status_size(const Tox_Events *events) -{ - if (events == nullptr) { - return 0; - } - - return events->self_connection_status_size; -} - -const Tox_Event_Self_Connection_Status *tox_events_get_self_connection_status(const Tox_Events *events, uint32_t index) -{ - assert(index < events->self_connection_status_size); - assert(events->self_connection_status != nullptr); - return &events->self_connection_status[index]; -} - -bool tox_events_pack_self_connection_status(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_self_connection_status_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_self_connection_status_pack(tox_events_get_self_connection_status(events, i), bp)) { - return false; - } - } - return true; -} - -bool tox_events_unpack_self_connection_status(Tox_Events *events, Bin_Unpack *bu) -{ - Tox_Event_Self_Connection_Status *event = tox_events_add_self_connection_status(events); - - if (event == nullptr) { - return false; - } - - return tox_event_self_connection_status_unpack(event, bu); -} +EV_FUNCS(Self_Connection_Status, self_connection_status, SELF_CONNECTION_STATUS) /***************************************************** @@ -172,17 +72,9 @@ bool tox_events_unpack_self_connection_status(Tox_Events *events, Bin_Unpack *bu void tox_events_handle_self_connection_status(Tox *tox, Tox_Connection connection_status, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Self_Connection_Status *self_connection_status = tox_events_add_self_connection_status(state->events); + Tox_Event_Self_Connection_Status *self_connection_status = tox_event_self_connection_status_alloc(user_data); if (self_connection_status == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/tox_dispatch.c b/toxcore/tox_dispatch.c index 54278514700..264612d11fd 100644 --- a/toxcore/tox_dispatch.c +++ b/toxcore/tox_dispatch.c @@ -7,6 +7,8 @@ #include #include "ccompat.h" +#include "events/events_alloc.h" +#include "tox_event.h" struct Tox_Dispatch { tox_events_conference_connected_cb *conference_connected_callback; @@ -166,321 +168,166 @@ void tox_events_callback_self_connection_status( dispatch->self_connection_status_callback = callback; } -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_conference_connected( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) +non_null(1, 2, 3) nullable(4) +static void tox_dispatch_invoke_event(const Tox_Dispatch *dispatch, const Tox_Event *event, Tox *tox, void *user_data) { - const uint32_t size = tox_events_get_conference_connected_size(events); + switch (event->type) { + case TOX_EVENT_CONFERENCE_CONNECTED: + if (dispatch->conference_connected_callback != nullptr) { + dispatch->conference_connected_callback(tox, event->data.conference_connected, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->conference_connected_callback != nullptr) { - dispatch->conference_connected_callback( - tox, tox_events_get_conference_connected(events, i), user_data); - } - } -} + break; -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_conference_invite( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_conference_invite_size(events); + case TOX_EVENT_CONFERENCE_INVITE: + if (dispatch->conference_invite_callback != nullptr) { + dispatch->conference_invite_callback(tox, event->data.conference_invite, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->conference_invite_callback != nullptr) { - dispatch->conference_invite_callback( - tox, tox_events_get_conference_invite(events, i), user_data); - } - } -} + break; -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_conference_message( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_conference_message_size(events); + case TOX_EVENT_CONFERENCE_MESSAGE: + if (dispatch->conference_message_callback != nullptr) { + dispatch->conference_message_callback(tox, event->data.conference_message, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->conference_message_callback != nullptr) { - dispatch->conference_message_callback( - tox, tox_events_get_conference_message(events, i), user_data); - } - } -} + break; -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_conference_peer_list_changed( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_conference_peer_list_changed_size(events); + case TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED: + if (dispatch->conference_peer_list_changed_callback != nullptr) { + dispatch->conference_peer_list_changed_callback(tox, event->data.conference_peer_list_changed, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->conference_peer_list_changed_callback != nullptr) { - dispatch->conference_peer_list_changed_callback( - tox, tox_events_get_conference_peer_list_changed(events, i), user_data); - } - } -} + break; -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_conference_peer_name( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_conference_peer_name_size(events); + case TOX_EVENT_CONFERENCE_PEER_NAME: + if (dispatch->conference_peer_name_callback != nullptr) { + dispatch->conference_peer_name_callback(tox, event->data.conference_peer_name, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->conference_peer_name_callback != nullptr) { - dispatch->conference_peer_name_callback( - tox, tox_events_get_conference_peer_name(events, i), user_data); - } - } -} + break; -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_conference_title( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_conference_title_size(events); + case TOX_EVENT_CONFERENCE_TITLE: + if (dispatch->conference_title_callback != nullptr) { + dispatch->conference_title_callback(tox, event->data.conference_title, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->conference_title_callback != nullptr) { - dispatch->conference_title_callback( - tox, tox_events_get_conference_title(events, i), user_data); - } - } -} + break; -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_file_chunk_request( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_file_chunk_request_size(events); + case TOX_EVENT_FILE_CHUNK_REQUEST: + if (dispatch->file_chunk_request_callback != nullptr) { + dispatch->file_chunk_request_callback(tox, event->data.file_chunk_request, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->file_chunk_request_callback != nullptr) { - dispatch->file_chunk_request_callback( - tox, tox_events_get_file_chunk_request(events, i), user_data); - } - } -} + break; -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_file_recv( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_file_recv_size(events); + case TOX_EVENT_FILE_RECV_CHUNK: + if (dispatch->file_recv_chunk_callback != nullptr) { + dispatch->file_recv_chunk_callback(tox, event->data.file_recv_chunk, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->file_recv_callback != nullptr) { - dispatch->file_recv_callback( - tox, tox_events_get_file_recv(events, i), user_data); - } - } -} + break; -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_file_recv_chunk( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_file_recv_chunk_size(events); + case TOX_EVENT_FILE_RECV_CONTROL: + if (dispatch->file_recv_control_callback != nullptr) { + dispatch->file_recv_control_callback(tox, event->data.file_recv_control, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->file_recv_chunk_callback != nullptr) { - dispatch->file_recv_chunk_callback( - tox, tox_events_get_file_recv_chunk(events, i), user_data); - } - } -} + break; -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_file_recv_control( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_file_recv_control_size(events); + case TOX_EVENT_FILE_RECV: + if (dispatch->file_recv_callback != nullptr) { + dispatch->file_recv_callback(tox, event->data.file_recv, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->file_recv_control_callback != nullptr) { - dispatch->file_recv_control_callback( - tox, tox_events_get_file_recv_control(events, i), user_data); - } - } -} + break; -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_friend_connection_status( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_friend_connection_status_size(events); + case TOX_EVENT_FRIEND_CONNECTION_STATUS: + if (dispatch->friend_connection_status_callback != nullptr) { + dispatch->friend_connection_status_callback(tox, event->data.friend_connection_status, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->friend_connection_status_callback != nullptr) { - dispatch->friend_connection_status_callback( - tox, tox_events_get_friend_connection_status(events, i), user_data); - } - } -} + break; -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_friend_lossless_packet( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_friend_lossless_packet_size(events); + case TOX_EVENT_FRIEND_LOSSLESS_PACKET: + if (dispatch->friend_lossless_packet_callback != nullptr) { + dispatch->friend_lossless_packet_callback(tox, event->data.friend_lossless_packet, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->friend_lossless_packet_callback != nullptr) { - dispatch->friend_lossless_packet_callback( - tox, tox_events_get_friend_lossless_packet(events, i), user_data); - } - } -} + break; -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_friend_lossy_packet( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_friend_lossy_packet_size(events); + case TOX_EVENT_FRIEND_LOSSY_PACKET: + if (dispatch->friend_lossy_packet_callback != nullptr) { + dispatch->friend_lossy_packet_callback(tox, event->data.friend_lossy_packet, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->friend_lossy_packet_callback != nullptr) { - dispatch->friend_lossy_packet_callback( - tox, tox_events_get_friend_lossy_packet(events, i), user_data); - } - } -} + break; -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_friend_message( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_friend_message_size(events); + case TOX_EVENT_FRIEND_MESSAGE: + if (dispatch->friend_message_callback != nullptr) { + dispatch->friend_message_callback(tox, event->data.friend_message, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->friend_message_callback != nullptr) { - dispatch->friend_message_callback( - tox, tox_events_get_friend_message(events, i), user_data); - } - } -} + break; -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_friend_name( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_friend_name_size(events); + case TOX_EVENT_FRIEND_NAME: + if (dispatch->friend_name_callback != nullptr) { + dispatch->friend_name_callback(tox, event->data.friend_name, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->friend_name_callback != nullptr) { - dispatch->friend_name_callback( - tox, tox_events_get_friend_name(events, i), user_data); - } - } -} + break; -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_friend_read_receipt( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_friend_read_receipt_size(events); + case TOX_EVENT_FRIEND_READ_RECEIPT: + if (dispatch->friend_read_receipt_callback != nullptr) { + dispatch->friend_read_receipt_callback(tox, event->data.friend_read_receipt, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->friend_read_receipt_callback != nullptr) { - dispatch->friend_read_receipt_callback( - tox, tox_events_get_friend_read_receipt(events, i), user_data); - } - } -} + break; -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_friend_request( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_friend_request_size(events); + case TOX_EVENT_FRIEND_REQUEST: + if (dispatch->friend_request_callback != nullptr) { + dispatch->friend_request_callback(tox, event->data.friend_request, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->friend_request_callback != nullptr) { - dispatch->friend_request_callback( - tox, tox_events_get_friend_request(events, i), user_data); - } - } -} + break; -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_friend_status( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_friend_status_size(events); + case TOX_EVENT_FRIEND_STATUS: + if (dispatch->friend_status_callback != nullptr) { + dispatch->friend_status_callback(tox, event->data.friend_status, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->friend_status_callback != nullptr) { - dispatch->friend_status_callback( - tox, tox_events_get_friend_status(events, i), user_data); - } - } -} + break; -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_friend_status_message( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_friend_status_message_size(events); + case TOX_EVENT_FRIEND_STATUS_MESSAGE: + if (dispatch->friend_status_message_callback != nullptr) { + dispatch->friend_status_message_callback(tox, event->data.friend_status_message, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->friend_status_message_callback != nullptr) { - dispatch->friend_status_message_callback( - tox, tox_events_get_friend_status_message(events, i), user_data); - } - } -} + break; -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_friend_typing( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_friend_typing_size(events); + case TOX_EVENT_FRIEND_TYPING: + if (dispatch->friend_typing_callback != nullptr) { + dispatch->friend_typing_callback(tox, event->data.friend_typing, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->friend_typing_callback != nullptr) { - dispatch->friend_typing_callback( - tox, tox_events_get_friend_typing(events, i), user_data); - } - } -} + break; -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_self_connection_status( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_self_connection_status_size(events); + case TOX_EVENT_SELF_CONNECTION_STATUS: + if (dispatch->self_connection_status_callback != nullptr) { + dispatch->self_connection_status_callback(tox, event->data.self_connection_status, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->self_connection_status_callback != nullptr) { - dispatch->self_connection_status_callback( - tox, tox_events_get_self_connection_status(events, i), user_data); - } + break; + + case TOX_EVENT_INVALID: + break; } } void tox_dispatch_invoke(const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) { - tox_dispatch_invoke_conference_connected(dispatch, events, tox, user_data); - tox_dispatch_invoke_conference_invite(dispatch, events, tox, user_data); - tox_dispatch_invoke_conference_message(dispatch, events, tox, user_data); - tox_dispatch_invoke_conference_peer_list_changed(dispatch, events, tox, user_data); - tox_dispatch_invoke_conference_peer_name(dispatch, events, tox, user_data); - tox_dispatch_invoke_conference_title(dispatch, events, tox, user_data); - tox_dispatch_invoke_file_chunk_request(dispatch, events, tox, user_data); - tox_dispatch_invoke_file_recv(dispatch, events, tox, user_data); - tox_dispatch_invoke_file_recv_chunk(dispatch, events, tox, user_data); - tox_dispatch_invoke_file_recv_control(dispatch, events, tox, user_data); - tox_dispatch_invoke_friend_connection_status(dispatch, events, tox, user_data); - tox_dispatch_invoke_friend_lossless_packet(dispatch, events, tox, user_data); - tox_dispatch_invoke_friend_lossy_packet(dispatch, events, tox, user_data); - tox_dispatch_invoke_friend_message(dispatch, events, tox, user_data); - tox_dispatch_invoke_friend_name(dispatch, events, tox, user_data); - tox_dispatch_invoke_friend_read_receipt(dispatch, events, tox, user_data); - tox_dispatch_invoke_friend_request(dispatch, events, tox, user_data); - tox_dispatch_invoke_friend_status(dispatch, events, tox, user_data); - tox_dispatch_invoke_friend_status_message(dispatch, events, tox, user_data); - tox_dispatch_invoke_friend_typing(dispatch, events, tox, user_data); - tox_dispatch_invoke_self_connection_status(dispatch, events, tox, user_data); + for (uint32_t i = 0; i < events->events_size; ++i) { + const Tox_Event *event = &events->events[i]; + tox_dispatch_invoke_event(dispatch, event, tox, user_data); + } } diff --git a/toxcore/tox_event.c b/toxcore/tox_event.c new file mode 100644 index 00000000000..394ec09e5ba --- /dev/null +++ b/toxcore/tox_event.c @@ -0,0 +1,474 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2022 The TokTok team. + */ + +#include "tox_event.h" + +#include +#include +#include +#include + +#include "bin_unpack.h" +#include "ccompat.h" +#include "events/events_alloc.h" +#include "tox.h" + +const char *tox_event_type_name(Tox_Event_Type type) +{ + switch (type) { + case TOX_EVENT_CONFERENCE_CONNECTED: + return "TOX_EVENT_CONFERENCE_CONNECTED"; + + case TOX_EVENT_CONFERENCE_INVITE: + return "TOX_EVENT_CONFERENCE_INVITE"; + + case TOX_EVENT_CONFERENCE_MESSAGE: + return "TOX_EVENT_CONFERENCE_MESSAGE"; + + case TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED: + return "TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED"; + + case TOX_EVENT_CONFERENCE_PEER_NAME: + return "TOX_EVENT_CONFERENCE_PEER_NAME"; + + case TOX_EVENT_CONFERENCE_TITLE: + return "TOX_EVENT_CONFERENCE_TITLE"; + + case TOX_EVENT_FILE_CHUNK_REQUEST: + return "TOX_EVENT_FILE_CHUNK_REQUEST"; + + case TOX_EVENT_FILE_RECV_CHUNK: + return "TOX_EVENT_FILE_RECV_CHUNK"; + + case TOX_EVENT_FILE_RECV_CONTROL: + return "TOX_EVENT_FILE_RECV_CONTROL"; + + case TOX_EVENT_FILE_RECV: + return "TOX_EVENT_FILE_RECV"; + + case TOX_EVENT_FRIEND_CONNECTION_STATUS: + return "TOX_EVENT_FRIEND_CONNECTION_STATUS"; + + case TOX_EVENT_FRIEND_LOSSLESS_PACKET: + return "TOX_EVENT_FRIEND_LOSSLESS_PACKET"; + + case TOX_EVENT_FRIEND_LOSSY_PACKET: + return "TOX_EVENT_FRIEND_LOSSY_PACKET"; + + case TOX_EVENT_FRIEND_MESSAGE: + return "TOX_EVENT_FRIEND_MESSAGE"; + + case TOX_EVENT_FRIEND_NAME: + return "TOX_EVENT_FRIEND_NAME"; + + case TOX_EVENT_FRIEND_READ_RECEIPT: + return "TOX_EVENT_FRIEND_READ_RECEIPT"; + + case TOX_EVENT_FRIEND_REQUEST: + return "TOX_EVENT_FRIEND_REQUEST"; + + case TOX_EVENT_FRIEND_STATUS: + return "TOX_EVENT_FRIEND_STATUS"; + + case TOX_EVENT_FRIEND_STATUS_MESSAGE: + return "TOX_EVENT_FRIEND_STATUS_MESSAGE"; + + case TOX_EVENT_FRIEND_TYPING: + return "TOX_EVENT_FRIEND_TYPING"; + + case TOX_EVENT_SELF_CONNECTION_STATUS: + return "TOX_EVENT_SELF_CONNECTION_STATUS"; + + case TOX_EVENT_INVALID: + return "TOX_EVENT_INVALID"; + } + + return ""; +} + +Tox_Event_Type tox_event_get_type(const Tox_Event *event) +{ + assert(event != nullptr); + return event->type; +} + +bool tox_event_construct(Tox_Event *event, Tox_Event_Type type, const Memory *mem) +{ + event->type = type; + event->data.value = nullptr; + + switch (type) { + case TOX_EVENT_CONFERENCE_CONNECTED: + event->data.conference_connected = tox_event_conference_connected_new(mem); + break; + + case TOX_EVENT_CONFERENCE_INVITE: + event->data.conference_invite = tox_event_conference_invite_new(mem); + break; + + case TOX_EVENT_CONFERENCE_MESSAGE: + event->data.conference_message = tox_event_conference_message_new(mem); + break; + + case TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED: + event->data.conference_peer_list_changed = tox_event_conference_peer_list_changed_new(mem); + break; + + case TOX_EVENT_CONFERENCE_PEER_NAME: + event->data.conference_peer_name = tox_event_conference_peer_name_new(mem); + break; + + case TOX_EVENT_CONFERENCE_TITLE: + event->data.conference_title = tox_event_conference_title_new(mem); + break; + + case TOX_EVENT_FILE_CHUNK_REQUEST: + event->data.file_chunk_request = tox_event_file_chunk_request_new(mem); + break; + + case TOX_EVENT_FILE_RECV_CHUNK: + event->data.file_recv_chunk = tox_event_file_recv_chunk_new(mem); + break; + + case TOX_EVENT_FILE_RECV_CONTROL: + event->data.file_recv_control = tox_event_file_recv_control_new(mem); + break; + + case TOX_EVENT_FILE_RECV: + event->data.file_recv = tox_event_file_recv_new(mem); + break; + + case TOX_EVENT_FRIEND_CONNECTION_STATUS: + event->data.friend_connection_status = tox_event_friend_connection_status_new(mem); + break; + + case TOX_EVENT_FRIEND_LOSSLESS_PACKET: + event->data.friend_lossless_packet = tox_event_friend_lossless_packet_new(mem); + break; + + case TOX_EVENT_FRIEND_LOSSY_PACKET: + event->data.friend_lossy_packet = tox_event_friend_lossy_packet_new(mem); + break; + + case TOX_EVENT_FRIEND_MESSAGE: + event->data.friend_message = tox_event_friend_message_new(mem); + break; + + case TOX_EVENT_FRIEND_NAME: + event->data.friend_name = tox_event_friend_name_new(mem); + break; + + case TOX_EVENT_FRIEND_READ_RECEIPT: + event->data.friend_read_receipt = tox_event_friend_read_receipt_new(mem); + break; + + case TOX_EVENT_FRIEND_REQUEST: + event->data.friend_request = tox_event_friend_request_new(mem); + break; + + case TOX_EVENT_FRIEND_STATUS: + event->data.friend_status = tox_event_friend_status_new(mem); + break; + + case TOX_EVENT_FRIEND_STATUS_MESSAGE: + event->data.friend_status_message = tox_event_friend_status_message_new(mem); + break; + + case TOX_EVENT_FRIEND_TYPING: + event->data.friend_typing = tox_event_friend_typing_new(mem); + break; + + case TOX_EVENT_SELF_CONNECTION_STATUS: + event->data.self_connection_status = tox_event_self_connection_status_new(mem); + break; + + case TOX_EVENT_INVALID: + return false; + } + + if (event->data.value == nullptr) { + return false; + } + + return true; +} + +void tox_event_destruct(Tox_Event *event, const Memory *mem) +{ + if (event == nullptr) { + return; + } + + switch (event->type) { + case TOX_EVENT_CONFERENCE_CONNECTED: + tox_event_conference_connected_free(event->data.conference_connected, mem); + break; + + case TOX_EVENT_CONFERENCE_INVITE: + tox_event_conference_invite_free(event->data.conference_invite, mem); + break; + + case TOX_EVENT_CONFERENCE_MESSAGE: + tox_event_conference_message_free(event->data.conference_message, mem); + break; + + case TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED: + tox_event_conference_peer_list_changed_free(event->data.conference_peer_list_changed, mem); + break; + + case TOX_EVENT_CONFERENCE_PEER_NAME: + tox_event_conference_peer_name_free(event->data.conference_peer_name, mem); + break; + + case TOX_EVENT_CONFERENCE_TITLE: + tox_event_conference_title_free(event->data.conference_title, mem); + break; + + case TOX_EVENT_FILE_CHUNK_REQUEST: + tox_event_file_chunk_request_free(event->data.file_chunk_request, mem); + break; + + case TOX_EVENT_FILE_RECV_CHUNK: + tox_event_file_recv_chunk_free(event->data.file_recv_chunk, mem); + break; + + case TOX_EVENT_FILE_RECV_CONTROL: + tox_event_file_recv_control_free(event->data.file_recv_control, mem); + break; + + case TOX_EVENT_FILE_RECV: + tox_event_file_recv_free(event->data.file_recv, mem); + break; + + case TOX_EVENT_FRIEND_CONNECTION_STATUS: + tox_event_friend_connection_status_free(event->data.friend_connection_status, mem); + break; + + case TOX_EVENT_FRIEND_LOSSLESS_PACKET: + tox_event_friend_lossless_packet_free(event->data.friend_lossless_packet, mem); + break; + + case TOX_EVENT_FRIEND_LOSSY_PACKET: + tox_event_friend_lossy_packet_free(event->data.friend_lossy_packet, mem); + break; + + case TOX_EVENT_FRIEND_MESSAGE: + tox_event_friend_message_free(event->data.friend_message, mem); + break; + + case TOX_EVENT_FRIEND_NAME: + tox_event_friend_name_free(event->data.friend_name, mem); + break; + + case TOX_EVENT_FRIEND_READ_RECEIPT: + tox_event_friend_read_receipt_free(event->data.friend_read_receipt, mem); + break; + + case TOX_EVENT_FRIEND_REQUEST: + tox_event_friend_request_free(event->data.friend_request, mem); + break; + + case TOX_EVENT_FRIEND_STATUS: + tox_event_friend_status_free(event->data.friend_status, mem); + break; + + case TOX_EVENT_FRIEND_STATUS_MESSAGE: + tox_event_friend_status_message_free(event->data.friend_status_message, mem); + break; + + case TOX_EVENT_FRIEND_TYPING: + tox_event_friend_typing_free(event->data.friend_typing, mem); + break; + + case TOX_EVENT_SELF_CONNECTION_STATUS: + tox_event_self_connection_status_free(event->data.self_connection_status, mem); + break; + + case TOX_EVENT_INVALID: + break; + } + + event->data.value = nullptr; +} + +bool tox_event_pack(const Tox_Event *event, Bin_Pack *bp) +{ + assert(event->type > TOX_EVENT_INVALID); + assert(event->type <= TOX_EVENT_MAX); + + switch (event->type) { + case TOX_EVENT_CONFERENCE_CONNECTED: + return tox_event_conference_connected_pack(event->data.conference_connected, bp); + + case TOX_EVENT_CONFERENCE_INVITE: + return tox_event_conference_invite_pack(event->data.conference_invite, bp); + + case TOX_EVENT_CONFERENCE_MESSAGE: + return tox_event_conference_message_pack(event->data.conference_message, bp); + + case TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED: + return tox_event_conference_peer_list_changed_pack(event->data.conference_peer_list_changed, bp); + + case TOX_EVENT_CONFERENCE_PEER_NAME: + return tox_event_conference_peer_name_pack(event->data.conference_peer_name, bp); + + case TOX_EVENT_CONFERENCE_TITLE: + return tox_event_conference_title_pack(event->data.conference_title, bp); + + case TOX_EVENT_FILE_CHUNK_REQUEST: + return tox_event_file_chunk_request_pack(event->data.file_chunk_request, bp); + + case TOX_EVENT_FILE_RECV_CHUNK: + return tox_event_file_recv_chunk_pack(event->data.file_recv_chunk, bp); + + case TOX_EVENT_FILE_RECV_CONTROL: + return tox_event_file_recv_control_pack(event->data.file_recv_control, bp); + + case TOX_EVENT_FILE_RECV: + return tox_event_file_recv_pack(event->data.file_recv, bp); + + case TOX_EVENT_FRIEND_CONNECTION_STATUS: + return tox_event_friend_connection_status_pack(event->data.friend_connection_status, bp); + + case TOX_EVENT_FRIEND_LOSSLESS_PACKET: + return tox_event_friend_lossless_packet_pack(event->data.friend_lossless_packet, bp); + + case TOX_EVENT_FRIEND_LOSSY_PACKET: + return tox_event_friend_lossy_packet_pack(event->data.friend_lossy_packet, bp); + + case TOX_EVENT_FRIEND_MESSAGE: + return tox_event_friend_message_pack(event->data.friend_message, bp); + + case TOX_EVENT_FRIEND_NAME: + return tox_event_friend_name_pack(event->data.friend_name, bp); + + case TOX_EVENT_FRIEND_READ_RECEIPT: + return tox_event_friend_read_receipt_pack(event->data.friend_read_receipt, bp); + + case TOX_EVENT_FRIEND_REQUEST: + return tox_event_friend_request_pack(event->data.friend_request, bp); + + case TOX_EVENT_FRIEND_STATUS: + return tox_event_friend_status_pack(event->data.friend_status, bp); + + case TOX_EVENT_FRIEND_STATUS_MESSAGE: + return tox_event_friend_status_message_pack(event->data.friend_status_message, bp); + + case TOX_EVENT_FRIEND_TYPING: + return tox_event_friend_typing_pack(event->data.friend_typing, bp); + + case TOX_EVENT_SELF_CONNECTION_STATUS: + return tox_event_self_connection_status_pack(event->data.self_connection_status, bp); + + case TOX_EVENT_INVALID: + return false; + } + + return false; +} + +non_null() +static bool tox_event_type_unpack(Tox_Event_Type *type, Bin_Unpack *bu) +{ + uint8_t u08; + if (!bin_unpack_u08(bu, &u08)) { + return false; + } + + if (u08 <= TOX_EVENT_INVALID || u08 > TOX_EVENT_MAX) { + return false; + } + + *type = (Tox_Event_Type)u08; + return true; +} + +bool tox_event_unpack_into(Tox_Event *event, Bin_Unpack *bu, const Memory *mem) +{ + uint32_t size; + if (!bin_unpack_array(bu, &size)) { + return false; + } + + if (size != 2) { + return false; + } + + Tox_Event_Type type; + if (!tox_event_type_unpack(&type, bu)) { + return false; + } + + event->type = type; + + switch (type) { + case TOX_EVENT_CONFERENCE_CONNECTED: + return tox_event_conference_connected_unpack(&event->data.conference_connected, bu, mem); + + case TOX_EVENT_CONFERENCE_INVITE: + return tox_event_conference_invite_unpack(&event->data.conference_invite, bu, mem); + + case TOX_EVENT_CONFERENCE_MESSAGE: + return tox_event_conference_message_unpack(&event->data.conference_message, bu, mem); + + case TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED: + return tox_event_conference_peer_list_changed_unpack(&event->data.conference_peer_list_changed, bu, mem); + + case TOX_EVENT_CONFERENCE_PEER_NAME: + return tox_event_conference_peer_name_unpack(&event->data.conference_peer_name, bu, mem); + + case TOX_EVENT_CONFERENCE_TITLE: + return tox_event_conference_title_unpack(&event->data.conference_title, bu, mem); + + case TOX_EVENT_FILE_CHUNK_REQUEST: + return tox_event_file_chunk_request_unpack(&event->data.file_chunk_request, bu, mem); + + case TOX_EVENT_FILE_RECV_CHUNK: + return tox_event_file_recv_chunk_unpack(&event->data.file_recv_chunk, bu, mem); + + case TOX_EVENT_FILE_RECV_CONTROL: + return tox_event_file_recv_control_unpack(&event->data.file_recv_control, bu, mem); + + case TOX_EVENT_FILE_RECV: + return tox_event_file_recv_unpack(&event->data.file_recv, bu, mem); + + case TOX_EVENT_FRIEND_CONNECTION_STATUS: + return tox_event_friend_connection_status_unpack(&event->data.friend_connection_status, bu, mem); + + case TOX_EVENT_FRIEND_LOSSLESS_PACKET: + return tox_event_friend_lossless_packet_unpack(&event->data.friend_lossless_packet, bu, mem); + + case TOX_EVENT_FRIEND_LOSSY_PACKET: + return tox_event_friend_lossy_packet_unpack(&event->data.friend_lossy_packet, bu, mem); + + case TOX_EVENT_FRIEND_MESSAGE: + return tox_event_friend_message_unpack(&event->data.friend_message, bu, mem); + + case TOX_EVENT_FRIEND_NAME: + return tox_event_friend_name_unpack(&event->data.friend_name, bu, mem); + + case TOX_EVENT_FRIEND_READ_RECEIPT: + return tox_event_friend_read_receipt_unpack(&event->data.friend_read_receipt, bu, mem); + + case TOX_EVENT_FRIEND_REQUEST: + return tox_event_friend_request_unpack(&event->data.friend_request, bu, mem); + + case TOX_EVENT_FRIEND_STATUS_MESSAGE: + return tox_event_friend_status_message_unpack(&event->data.friend_status_message, bu, mem); + + case TOX_EVENT_FRIEND_STATUS: + return tox_event_friend_status_unpack(&event->data.friend_status, bu, mem); + + case TOX_EVENT_FRIEND_TYPING: + return tox_event_friend_typing_unpack(&event->data.friend_typing, bu, mem); + + case TOX_EVENT_SELF_CONNECTION_STATUS: + return tox_event_self_connection_status_unpack(&event->data.self_connection_status, bu, mem); + + case TOX_EVENT_INVALID: + return false; + } + + return false; +} diff --git a/toxcore/tox_event.h b/toxcore/tox_event.h new file mode 100644 index 00000000000..dbcc30df418 --- /dev/null +++ b/toxcore/tox_event.h @@ -0,0 +1,164 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2022 The TokTok team. + */ + +#ifndef C_TOXCORE_TOXCORE_TOX_EVENT_H +#define C_TOXCORE_TOXCORE_TOX_EVENT_H + +#include "attributes.h" +#include "bin_pack.h" +#include "bin_unpack.h" +#include "mem.h" +#include "tox_events.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef union Tox_Event_Data { + /** + * Opaque pointer just to check whether any value is set. + */ + void *value; + + Tox_Event_Conference_Connected *conference_connected; + Tox_Event_Conference_Invite *conference_invite; + Tox_Event_Conference_Message *conference_message; + Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed; + Tox_Event_Conference_Peer_Name *conference_peer_name; + Tox_Event_Conference_Title *conference_title; + Tox_Event_File_Chunk_Request *file_chunk_request; + Tox_Event_File_Recv *file_recv; + Tox_Event_File_Recv_Chunk *file_recv_chunk; + Tox_Event_File_Recv_Control *file_recv_control; + Tox_Event_Friend_Connection_Status *friend_connection_status; + Tox_Event_Friend_Lossless_Packet *friend_lossless_packet; + Tox_Event_Friend_Lossy_Packet *friend_lossy_packet; + Tox_Event_Friend_Message *friend_message; + Tox_Event_Friend_Name *friend_name; + Tox_Event_Friend_Read_Receipt *friend_read_receipt; + Tox_Event_Friend_Request *friend_request; + Tox_Event_Friend_Status *friend_status; + Tox_Event_Friend_Status_Message *friend_status_message; + Tox_Event_Friend_Typing *friend_typing; + Tox_Event_Self_Connection_Status *self_connection_status; +} Tox_Event_Data; + +struct Tox_Event { + Tox_Event_Type type; + Tox_Event_Data data; +}; + +/** + * Constructor. + */ +bool tox_event_construct(Tox_Event *event, Tox_Event_Type type, const Memory *mem); + +Tox_Event_Conference_Connected *tox_event_conference_connected_new(const Memory *mem); +Tox_Event_Conference_Invite *tox_event_conference_invite_new(const Memory *mem); +Tox_Event_Conference_Message *tox_event_conference_message_new(const Memory *mem); +Tox_Event_Conference_Peer_List_Changed *tox_event_conference_peer_list_changed_new(const Memory *mem); +Tox_Event_Conference_Peer_Name *tox_event_conference_peer_name_new(const Memory *mem); +Tox_Event_Conference_Title *tox_event_conference_title_new(const Memory *mem); +Tox_Event_File_Chunk_Request *tox_event_file_chunk_request_new(const Memory *mem); +Tox_Event_File_Recv_Chunk *tox_event_file_recv_chunk_new(const Memory *mem); +Tox_Event_File_Recv_Control *tox_event_file_recv_control_new(const Memory *mem); +Tox_Event_File_Recv *tox_event_file_recv_new(const Memory *mem); +Tox_Event_Friend_Connection_Status *tox_event_friend_connection_status_new(const Memory *mem); +Tox_Event_Friend_Lossless_Packet *tox_event_friend_lossless_packet_new(const Memory *mem); +Tox_Event_Friend_Lossy_Packet *tox_event_friend_lossy_packet_new(const Memory *mem); +Tox_Event_Friend_Message *tox_event_friend_message_new(const Memory *mem); +Tox_Event_Friend_Name *tox_event_friend_name_new(const Memory *mem); +Tox_Event_Friend_Read_Receipt *tox_event_friend_read_receipt_new(const Memory *mem); +Tox_Event_Friend_Request *tox_event_friend_request_new(const Memory *mem); +Tox_Event_Friend_Status_Message *tox_event_friend_status_message_new(const Memory *mem); +Tox_Event_Friend_Status *tox_event_friend_status_new(const Memory *mem); +Tox_Event_Friend_Typing *tox_event_friend_typing_new(const Memory *mem); +Tox_Event_Self_Connection_Status *tox_event_self_connection_status_new(const Memory *mem); + +/** + * Destructor. + */ +void tox_event_destruct(Tox_Event *event, const Memory *mem); + +non_null() void tox_event_conference_connected_free(Tox_Event_Conference_Connected *conference_connected, const Memory *mem); +non_null() void tox_event_conference_invite_free(Tox_Event_Conference_Invite *conference_invite, const Memory *mem); +non_null() void tox_event_conference_message_free(Tox_Event_Conference_Message *conference_message, const Memory *mem); +non_null() void tox_event_conference_peer_list_changed_free(Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed, const Memory *mem); +non_null() void tox_event_conference_peer_name_free(Tox_Event_Conference_Peer_Name *conference_peer_name, const Memory *mem); +non_null() void tox_event_conference_title_free(Tox_Event_Conference_Title *conference_title, const Memory *mem); +non_null() void tox_event_file_chunk_request_free(Tox_Event_File_Chunk_Request *file_chunk_request, const Memory *mem); +non_null() void tox_event_file_recv_chunk_free(Tox_Event_File_Recv_Chunk *file_recv_chunk, const Memory *mem); +non_null() void tox_event_file_recv_control_free(Tox_Event_File_Recv_Control *file_recv_control, const Memory *mem); +non_null() void tox_event_file_recv_free(Tox_Event_File_Recv *file_recv, const Memory *mem); +non_null() void tox_event_friend_connection_status_free(Tox_Event_Friend_Connection_Status *friend_connection_status, const Memory *mem); +non_null() void tox_event_friend_lossless_packet_free(Tox_Event_Friend_Lossless_Packet *friend_lossless_packet, const Memory *mem); +non_null() void tox_event_friend_lossy_packet_free(Tox_Event_Friend_Lossy_Packet *friend_lossy_packet, const Memory *mem); +non_null() void tox_event_friend_message_free(Tox_Event_Friend_Message *friend_message, const Memory *mem); +non_null() void tox_event_friend_name_free(Tox_Event_Friend_Name *friend_name, const Memory *mem); +non_null() void tox_event_friend_read_receipt_free(Tox_Event_Friend_Read_Receipt *friend_read_receipt, const Memory *mem); +non_null() void tox_event_friend_request_free(Tox_Event_Friend_Request *friend_request, const Memory *mem); +non_null() void tox_event_friend_status_message_free(Tox_Event_Friend_Status_Message *friend_status_message, const Memory *mem); +non_null() void tox_event_friend_status_free(Tox_Event_Friend_Status *friend_status, const Memory *mem); +non_null() void tox_event_friend_typing_free(Tox_Event_Friend_Typing *friend_typing, const Memory *mem); +non_null() void tox_event_self_connection_status_free(Tox_Event_Self_Connection_Status *self_connection_status, const Memory *mem); + +/** + * Pack into msgpack. + */ +non_null() bool tox_event_pack(const Tox_Event *event, Bin_Pack *bp); + +non_null() bool tox_event_conference_connected_pack(const Tox_Event_Conference_Connected *event, Bin_Pack *bp); +non_null() bool tox_event_conference_invite_pack(const Tox_Event_Conference_Invite *conference_invite, Bin_Pack *bp); +non_null() bool tox_event_conference_message_pack(const Tox_Event_Conference_Message *conference_message, Bin_Pack *bp); +non_null() bool tox_event_conference_peer_list_changed_pack(const Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed, Bin_Pack *bp); +non_null() bool tox_event_conference_peer_name_pack(const Tox_Event_Conference_Peer_Name *conference_peer_name, Bin_Pack *bp); +non_null() bool tox_event_conference_title_pack(const Tox_Event_Conference_Title *conference_title, Bin_Pack *bp); +non_null() bool tox_event_file_chunk_request_pack(const Tox_Event_File_Chunk_Request *file_chunk_request, Bin_Pack *bp); +non_null() bool tox_event_file_recv_chunk_pack(const Tox_Event_File_Recv_Chunk *file_recv_chunk, Bin_Pack *bp); +non_null() bool tox_event_file_recv_control_pack(const Tox_Event_File_Recv_Control *file_recv_control, Bin_Pack *bp); +non_null() bool tox_event_file_recv_pack(const Tox_Event_File_Recv *file_recv, Bin_Pack *bp); +non_null() bool tox_event_friend_connection_status_pack(const Tox_Event_Friend_Connection_Status *friend_connection_status, Bin_Pack *bp); +non_null() bool tox_event_friend_lossless_packet_pack(const Tox_Event_Friend_Lossless_Packet *friend_lossless_packet, Bin_Pack *bp); +non_null() bool tox_event_friend_lossy_packet_pack(const Tox_Event_Friend_Lossy_Packet *friend_lossy_packet, Bin_Pack *bp); +non_null() bool tox_event_friend_message_pack(const Tox_Event_Friend_Message *friend_message, Bin_Pack *bp); +non_null() bool tox_event_friend_name_pack(const Tox_Event_Friend_Name *friend_name, Bin_Pack *bp); +non_null() bool tox_event_friend_read_receipt_pack(const Tox_Event_Friend_Read_Receipt *friend_read_receipt, Bin_Pack *bp); +non_null() bool tox_event_friend_request_pack(const Tox_Event_Friend_Request *friend_request, Bin_Pack *bp); +non_null() bool tox_event_friend_status_message_pack(const Tox_Event_Friend_Status_Message *friend_status_message, Bin_Pack *bp); +non_null() bool tox_event_friend_status_pack(const Tox_Event_Friend_Status *friend_status, Bin_Pack *bp); +non_null() bool tox_event_friend_typing_pack(const Tox_Event_Friend_Typing *friend_typing, Bin_Pack *bp); +non_null() bool tox_event_self_connection_status_pack(const Tox_Event_Self_Connection_Status *self_connection_status, Bin_Pack *bp); + +/** + * Unpack from msgpack. + */ +non_null() bool tox_event_unpack_into(Tox_Event *event, Bin_Unpack *bu, const Memory *mem); + +non_null() bool tox_event_conference_connected_unpack(Tox_Event_Conference_Connected **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_conference_invite_unpack(Tox_Event_Conference_Invite **conference_invite, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_conference_message_unpack(Tox_Event_Conference_Message **conference_message, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_conference_peer_list_changed_unpack(Tox_Event_Conference_Peer_List_Changed **conference_peer_list_changed, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_conference_peer_name_unpack(Tox_Event_Conference_Peer_Name **conference_peer_name, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_conference_title_unpack(Tox_Event_Conference_Title **conference_title, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_file_chunk_request_unpack(Tox_Event_File_Chunk_Request **file_chunk_request, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_file_recv_chunk_unpack(Tox_Event_File_Recv_Chunk **file_recv_chunk, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_file_recv_control_unpack(Tox_Event_File_Recv_Control **file_recv_control, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_file_recv_unpack(Tox_Event_File_Recv **file_recv, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_friend_connection_status_unpack(Tox_Event_Friend_Connection_Status **friend_connection_status, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_friend_lossless_packet_unpack(Tox_Event_Friend_Lossless_Packet **friend_lossless_packet, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_friend_lossy_packet_unpack(Tox_Event_Friend_Lossy_Packet **friend_lossy_packet, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_friend_message_unpack(Tox_Event_Friend_Message **friend_message, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_friend_name_unpack(Tox_Event_Friend_Name **friend_name, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_friend_read_receipt_unpack(Tox_Event_Friend_Read_Receipt **friend_read_receipt, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_friend_request_unpack(Tox_Event_Friend_Request **friend_request, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_friend_status_message_unpack(Tox_Event_Friend_Status_Message **friend_status_message, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_friend_status_unpack(Tox_Event_Friend_Status **friend_status, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_friend_typing_unpack(Tox_Event_Friend_Typing **friend_typing, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_self_connection_status_unpack(Tox_Event_Self_Connection_Status **self_connection_status, Bin_Unpack *bu, const Memory *mem); + +#ifdef __cplusplus +} +#endif + +#endif // C_TOXCORE_TOXCORE_TOX_EVENT_H diff --git a/toxcore/tox_events.c b/toxcore/tox_events.c index cae662e58ef..706e2f2489b 100644 --- a/toxcore/tox_events.c +++ b/toxcore/tox_events.c @@ -49,9 +49,24 @@ void tox_events_init(Tox *tox) tox_callback_self_connection_status(tox, tox_events_handle_self_connection_status); } +uint32_t tox_events_get_size(const Tox_Events *events) +{ + return events == nullptr ? 0 : events->events_size; +} + +const Tox_Event *tox_events_get(const Tox_Events *events, uint32_t index) +{ + if (index >= tox_events_get_size(events)) { + return nullptr; + } + + return &events->events[index]; +} + Tox_Events *tox_events_iterate(Tox *tox, bool fail_hard, Tox_Err_Events_Iterate *error) { - Tox_Events_State state = {TOX_ERR_EVENTS_ITERATE_OK}; + const Tox_System *sys = tox_get_system(tox); + Tox_Events_State state = {TOX_ERR_EVENTS_ITERATE_OK, sys->mem}; tox_iterate(tox, &state); if (error != nullptr) { @@ -68,141 +83,21 @@ Tox_Events *tox_events_iterate(Tox *tox, bool fail_hard, Tox_Err_Events_Iterate bool tox_events_pack(const Tox_Events *events, Bin_Pack *bp) { - const uint32_t count = tox_events_get_conference_connected_size(events) - + tox_events_get_conference_invite_size(events) - + tox_events_get_conference_message_size(events) - + tox_events_get_conference_peer_list_changed_size(events) - + tox_events_get_conference_peer_name_size(events) - + tox_events_get_conference_title_size(events) - + tox_events_get_file_chunk_request_size(events) - + tox_events_get_file_recv_chunk_size(events) - + tox_events_get_file_recv_control_size(events) - + tox_events_get_file_recv_size(events) - + tox_events_get_friend_connection_status_size(events) - + tox_events_get_friend_lossless_packet_size(events) - + tox_events_get_friend_lossy_packet_size(events) - + tox_events_get_friend_message_size(events) - + tox_events_get_friend_name_size(events) - + tox_events_get_friend_read_receipt_size(events) - + tox_events_get_friend_request_size(events) - + tox_events_get_friend_status_message_size(events) - + tox_events_get_friend_status_size(events) - + tox_events_get_friend_typing_size(events) - + tox_events_get_self_connection_status_size(events); - - return bin_pack_array(bp, count) - && tox_events_pack_conference_connected(events, bp) - && tox_events_pack_conference_invite(events, bp) - && tox_events_pack_conference_message(events, bp) - && tox_events_pack_conference_peer_list_changed(events, bp) - && tox_events_pack_conference_peer_name(events, bp) - && tox_events_pack_conference_title(events, bp) - && tox_events_pack_file_chunk_request(events, bp) - && tox_events_pack_file_recv_chunk(events, bp) - && tox_events_pack_file_recv_control(events, bp) - && tox_events_pack_file_recv(events, bp) - && tox_events_pack_friend_connection_status(events, bp) - && tox_events_pack_friend_lossless_packet(events, bp) - && tox_events_pack_friend_lossy_packet(events, bp) - && tox_events_pack_friend_message(events, bp) - && tox_events_pack_friend_name(events, bp) - && tox_events_pack_friend_read_receipt(events, bp) - && tox_events_pack_friend_request(events, bp) - && tox_events_pack_friend_status_message(events, bp) - && tox_events_pack_friend_status(events, bp) - && tox_events_pack_friend_typing(events, bp) - && tox_events_pack_self_connection_status(events, bp); -} - -non_null() -static bool tox_event_unpack(Tox_Events *events, Bin_Unpack *bu) -{ - uint32_t size; - if (!bin_unpack_array(bu, &size)) { - return false; - } - - if (size != 2) { + const uint32_t size = tox_events_get_size(events); + if (!bin_pack_array(bp, size)) { return false; } - uint8_t type; - if (!bin_unpack_u08(bu, &type)) { - return false; - } - - switch (type) { - case TOX_EVENT_CONFERENCE_CONNECTED: - return tox_events_unpack_conference_connected(events, bu); - - case TOX_EVENT_CONFERENCE_INVITE: - return tox_events_unpack_conference_invite(events, bu); - - case TOX_EVENT_CONFERENCE_MESSAGE: - return tox_events_unpack_conference_message(events, bu); - - case TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED: - return tox_events_unpack_conference_peer_list_changed(events, bu); - - case TOX_EVENT_CONFERENCE_PEER_NAME: - return tox_events_unpack_conference_peer_name(events, bu); - - case TOX_EVENT_CONFERENCE_TITLE: - return tox_events_unpack_conference_title(events, bu); - - case TOX_EVENT_FILE_CHUNK_REQUEST: - return tox_events_unpack_file_chunk_request(events, bu); - - case TOX_EVENT_FILE_RECV_CHUNK: - return tox_events_unpack_file_recv_chunk(events, bu); - - case TOX_EVENT_FILE_RECV_CONTROL: - return tox_events_unpack_file_recv_control(events, bu); - - case TOX_EVENT_FILE_RECV: - return tox_events_unpack_file_recv(events, bu); - - case TOX_EVENT_FRIEND_CONNECTION_STATUS: - return tox_events_unpack_friend_connection_status(events, bu); - - case TOX_EVENT_FRIEND_LOSSLESS_PACKET: - return tox_events_unpack_friend_lossless_packet(events, bu); - - case TOX_EVENT_FRIEND_LOSSY_PACKET: - return tox_events_unpack_friend_lossy_packet(events, bu); - - case TOX_EVENT_FRIEND_MESSAGE: - return tox_events_unpack_friend_message(events, bu); - - case TOX_EVENT_FRIEND_NAME: - return tox_events_unpack_friend_name(events, bu); - - case TOX_EVENT_FRIEND_READ_RECEIPT: - return tox_events_unpack_friend_read_receipt(events, bu); - - case TOX_EVENT_FRIEND_REQUEST: - return tox_events_unpack_friend_request(events, bu); - - case TOX_EVENT_FRIEND_STATUS_MESSAGE: - return tox_events_unpack_friend_status_message(events, bu); - - case TOX_EVENT_FRIEND_STATUS: - return tox_events_unpack_friend_status(events, bu); - - case TOX_EVENT_FRIEND_TYPING: - return tox_events_unpack_friend_typing(events, bu); - - case TOX_EVENT_SELF_CONNECTION_STATUS: - return tox_events_unpack_self_connection_status(events, bu); - - default: + for (uint32_t i = 0; i < size; i++) { + if (!tox_event_pack(&events->events[i], bp)) { return false; + } } return true; } -bool tox_events_unpack(Tox_Events *events, Bin_Unpack *bu) +bool tox_events_unpack(Tox_Events *events, Bin_Unpack *bu, const Memory *mem) { uint32_t size; if (!bin_unpack_array(bu, &size)) { @@ -210,11 +105,20 @@ bool tox_events_unpack(Tox_Events *events, Bin_Unpack *bu) } for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_unpack(events, bu)) { + Tox_Event event = {TOX_EVENT_INVALID}; + if (!tox_event_unpack_into(&event, bu, mem)) { + tox_event_destruct(&event, mem); + return false; + } + + if (!tox_events_add(events, &event)) { + tox_event_destruct(&event, mem); return false; } } + // Invariant: if all adds worked, the events size must be the input array size. + assert(tox_events_get_size(events) == size); return true; } @@ -229,9 +133,9 @@ uint32_t tox_events_bytes_size(const Tox_Events *events) return bin_pack_obj_size(tox_events_bin_pack_handler, events); } -void tox_events_get_bytes(const Tox_Events *events, uint8_t *bytes) +bool tox_events_get_bytes(const Tox_Events *events, uint8_t *bytes) { - bin_pack_obj(tox_events_bin_pack_handler, events, bytes, UINT32_MAX); + return bin_pack_obj(tox_events_bin_pack_handler, events, bytes, UINT32_MAX); } Tox_Events *tox_events_load(const Tox_System *sys, const uint8_t *bytes, uint32_t bytes_size) @@ -252,8 +156,9 @@ Tox_Events *tox_events_load(const Tox_System *sys, const uint8_t *bytes, uint32_ *events = (Tox_Events) { nullptr }; + events->mem = sys->mem; - if (!tox_events_unpack(events, bu)) { + if (!tox_events_unpack(events, bu, sys->mem)) { tox_events_free(events); bin_unpack_free(bu); return nullptr; diff --git a/toxcore/tox_events.h b/toxcore/tox_events.h index e868d89098a..6871df05d9f 100644 --- a/toxcore/tox_events.h +++ b/toxcore/tox_events.h @@ -88,7 +88,7 @@ uint32_t tox_event_file_recv_get_kind( typedef struct Tox_Event_File_Recv_Chunk Tox_Event_File_Recv_Chunk; const uint8_t *tox_event_file_recv_chunk_get_data( const Tox_Event_File_Recv_Chunk *file_recv_chunk); -uint32_t tox_event_file_recv_chunk_get_length( +uint32_t tox_event_file_recv_chunk_get_data_length( const Tox_Event_File_Recv_Chunk *file_recv_chunk); uint32_t tox_event_file_recv_chunk_get_file_number( const Tox_Event_File_Recv_Chunk *file_recv_chunk); @@ -184,35 +184,96 @@ Tox_Connection tox_event_self_connection_status_get_connection_status( const Tox_Event_Self_Connection_Status *self_connection_status); -typedef enum Tox_Event { - TOX_EVENT_SELF_CONNECTION_STATUS = 0, +typedef enum Tox_Event_Type { + TOX_EVENT_INVALID = 0, - TOX_EVENT_FRIEND_REQUEST = 1, - TOX_EVENT_FRIEND_CONNECTION_STATUS = 2, - TOX_EVENT_FRIEND_LOSSY_PACKET = 3, - TOX_EVENT_FRIEND_LOSSLESS_PACKET = 4, + TOX_EVENT_SELF_CONNECTION_STATUS = 1, - TOX_EVENT_FRIEND_NAME = 5, - TOX_EVENT_FRIEND_STATUS = 6, - TOX_EVENT_FRIEND_STATUS_MESSAGE = 7, + TOX_EVENT_FRIEND_REQUEST = 2, + TOX_EVENT_FRIEND_CONNECTION_STATUS = 3, + TOX_EVENT_FRIEND_LOSSY_PACKET = 4, + TOX_EVENT_FRIEND_LOSSLESS_PACKET = 5, - TOX_EVENT_FRIEND_MESSAGE = 8, - TOX_EVENT_FRIEND_READ_RECEIPT = 9, - TOX_EVENT_FRIEND_TYPING = 10, + TOX_EVENT_FRIEND_NAME = 6, + TOX_EVENT_FRIEND_STATUS = 7, + TOX_EVENT_FRIEND_STATUS_MESSAGE = 8, - TOX_EVENT_FILE_CHUNK_REQUEST = 11, - TOX_EVENT_FILE_RECV = 12, - TOX_EVENT_FILE_RECV_CHUNK = 13, - TOX_EVENT_FILE_RECV_CONTROL = 14, + TOX_EVENT_FRIEND_MESSAGE = 9, + TOX_EVENT_FRIEND_READ_RECEIPT = 10, + TOX_EVENT_FRIEND_TYPING = 11, - TOX_EVENT_CONFERENCE_INVITE = 15, - TOX_EVENT_CONFERENCE_CONNECTED = 16, - TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED = 17, - TOX_EVENT_CONFERENCE_PEER_NAME = 18, - TOX_EVENT_CONFERENCE_TITLE = 19, + TOX_EVENT_FILE_CHUNK_REQUEST = 12, + TOX_EVENT_FILE_RECV = 13, + TOX_EVENT_FILE_RECV_CHUNK = 14, + TOX_EVENT_FILE_RECV_CONTROL = 15, - TOX_EVENT_CONFERENCE_MESSAGE = 20, -} Tox_Event; + TOX_EVENT_CONFERENCE_INVITE = 16, + TOX_EVENT_CONFERENCE_CONNECTED = 17, + TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED = 18, + TOX_EVENT_CONFERENCE_PEER_NAME = 19, + TOX_EVENT_CONFERENCE_TITLE = 20, + + TOX_EVENT_CONFERENCE_MESSAGE = 21, + + TOX_EVENT_MAX = TOX_EVENT_CONFERENCE_MESSAGE, +} Tox_Event_Type; + +const char *tox_event_type_name(Tox_Event_Type type); + +/** + * A single Tox core event. + * + * It could contain any of the above event types. Use `tox_event_get_type` to + * find out which one it is, then use one of the `get` functions to get the + * actual event object out. The `get` functions will return NULL in case of type + * mismatch. + */ +typedef struct Tox_Event Tox_Event; + +Tox_Event_Type tox_event_get_type(const Tox_Event *event); + +const Tox_Event_Conference_Connected *tox_event_get_conference_connected( + const Tox_Event *event); +const Tox_Event_Conference_Invite *tox_event_get_conference_invite( + const Tox_Event *event); +const Tox_Event_Conference_Message *tox_event_get_conference_message( + const Tox_Event *event); +const Tox_Event_Conference_Peer_List_Changed *tox_event_get_conference_peer_list_changed( + const Tox_Event *event); +const Tox_Event_Conference_Peer_Name *tox_event_get_conference_peer_name( + const Tox_Event *event); +const Tox_Event_Conference_Title *tox_event_get_conference_title( + const Tox_Event *event); +const Tox_Event_File_Chunk_Request *tox_event_get_file_chunk_request( + const Tox_Event *event); +const Tox_Event_File_Recv_Chunk *tox_event_get_file_recv_chunk( + const Tox_Event *event); +const Tox_Event_File_Recv_Control *tox_event_get_file_recv_control( + const Tox_Event *event); +const Tox_Event_File_Recv *tox_event_get_file_recv( + const Tox_Event *event); +const Tox_Event_Friend_Connection_Status *tox_event_get_friend_connection_status( + const Tox_Event *event); +const Tox_Event_Friend_Lossless_Packet *tox_event_get_friend_lossless_packet( + const Tox_Event *event); +const Tox_Event_Friend_Lossy_Packet *tox_event_get_friend_lossy_packet( + const Tox_Event *event); +const Tox_Event_Friend_Message *tox_event_get_friend_message( + const Tox_Event *event); +const Tox_Event_Friend_Name *tox_event_get_friend_name( + const Tox_Event *event); +const Tox_Event_Friend_Read_Receipt *tox_event_get_friend_read_receipt( + const Tox_Event *event); +const Tox_Event_Friend_Request *tox_event_get_friend_request( + const Tox_Event *event); +const Tox_Event_Friend_Status_Message *tox_event_get_friend_status_message( + const Tox_Event *event); +const Tox_Event_Friend_Status *tox_event_get_friend_status( + const Tox_Event *event); +const Tox_Event_Friend_Typing *tox_event_get_friend_typing( + const Tox_Event *event); +const Tox_Event_Self_Connection_Status *tox_event_get_self_connection_status( + const Tox_Event *event); /** * Container object for all Tox core events. @@ -221,6 +282,9 @@ typedef enum Tox_Event { */ typedef struct Tox_Events Tox_Events; +uint32_t tox_events_get_size(const Tox_Events *events); +const Tox_Event *tox_events_get(const Tox_Events *events, uint32_t index); + uint32_t tox_events_get_conference_connected_size(const Tox_Events *events); uint32_t tox_events_get_conference_invite_size(const Tox_Events *events); uint32_t tox_events_get_conference_message_size(const Tox_Events *events); @@ -341,7 +405,7 @@ Tox_Events *tox_events_iterate(Tox *tox, bool fail_hard, Tox_Err_Events_Iterate void tox_events_free(Tox_Events *events); uint32_t tox_events_bytes_size(const Tox_Events *events); -void tox_events_get_bytes(const Tox_Events *events, uint8_t *bytes); +bool tox_events_get_bytes(const Tox_Events *events, uint8_t *bytes); Tox_Events *tox_events_load(const Tox_System *sys, const uint8_t *bytes, uint32_t bytes_size); diff --git a/toxcore/tox_events_test.cc b/toxcore/tox_events_test.cc index 5de29b5e483..fa76d3933cb 100644 --- a/toxcore/tox_events_test.cc +++ b/toxcore/tox_events_test.cc @@ -48,14 +48,14 @@ TEST(ToxEvents, NullEventsPacksToEmptyArray) TEST(ToxEvents, PackedEventsCanBeUnpacked) { const Tox_System sys = tox_default_system(); - // [[0, 1]] == Tox_Self_Connection_Status { .connection_status = TOX_CONNECTION_TCP } - std::array packed{0x91, 0x92, 0xcc, 0x00, 0xcc, 0x01}; + // [[1, 1]] == Tox_Self_Connection_Status { .connection_status = TOX_CONNECTION_TCP } + std::array packed{0x91, 0x92, 0xcc, 0x01, 0xcc, 0x01}; Tox_Events *events = tox_events_load(&sys, packed.data(), packed.size()); ASSERT_NE(events, nullptr); std::array bytes; ASSERT_EQ(tox_events_bytes_size(events), bytes.size()); tox_events_get_bytes(events, bytes.data()); - EXPECT_EQ(bytes, (std::array{0x91, 0x92, 0x00, 0x01})); + EXPECT_EQ(bytes, (std::array{0x91, 0x92, 0x01, 0x01})); tox_events_free(events); }