Skip to content

Commit

Permalink
refactor: Add common msgpack array packer with callback.
Browse files Browse the repository at this point in the history
There will be more object arrays that need to be packed. This function
takes care of NULL (creating an empty array), and putting the correct
array size and calling the per-element callback the right amount of
times.
  • Loading branch information
iphydf committed Jan 17, 2024
1 parent c650d9d commit ee8615b
Show file tree
Hide file tree
Showing 44 changed files with 183 additions and 91 deletions.
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/docker/tox-bootstrapd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
c31bc1cd7249c62b4f1d2c9ea26c9e8528d9b897dc29afbdbe95894d30c92c8d /usr/local/bin/tox-bootstrapd
95bcb5742f9d92cf0cc29a3aa840887bc1074c76637a9eabe5c63a2e4e709737 /usr/local/bin/tox-bootstrapd
1 change: 1 addition & 0 deletions other/event_tooling/generate_event_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
f << "bool tox_event_" << event_name_l << "_unpack(\n";
f << " Tox_Event_" << event_name << " **event, Bin_Unpack *bu, const Memory *mem)\n{\n";
f << " assert(event != nullptr);\n";
f << " assert(*event == nullptr);\n";
f << " *event = tox_event_" << event_name_l << "_new(mem);\n\n";
f << " if (*event == nullptr) {\n return false;\n }\n\n";
f << " return tox_event_" << event_name_l << "_unpack_into(*event, bu);\n}\n\n";
Expand Down
20 changes: 20 additions & 0 deletions toxcore/bin_pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,26 @@ bool bin_pack_obj_array_b(bin_pack_array_cb *callback, const void *arr, uint32_t
return true;
}

bool bin_pack_obj_array(Bin_Pack *bp, bin_pack_array_cb *callback, const void *arr, uint32_t arr_size, const Logger *logger)
{
if (arr == nullptr) {
assert(arr_size == 0);
return bin_pack_array(bp, 0);
}

if (!bin_pack_array(bp, arr_size)) {
return false;
}

for (uint32_t i = 0; i < arr_size; ++i) {
if (!callback(arr, i, logger, bp)) {
return false;
}
}

return true;
}

bool bin_pack_array(Bin_Pack *bp, uint32_t size)
{
return cmp_write_array(&bp->ctx, size);
Expand Down
44 changes: 36 additions & 8 deletions toxcore/bin_pack.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ extern "C" {
/**
* @brief Binary serialisation object.
*
* Naming convention:
* - Functions ending in `_b` (or `_b_size`) are NOT MessagePack, i.e. write
* data in plain big endian binary format.
* - All other functions encode their input in MessagePack format.
*
* Some notes on parameter order:
*
* - We pass the `obj` pointer as `this`-like pointer first to the callbacks.
Expand Down Expand Up @@ -66,7 +71,9 @@ uint32_t bin_pack_obj_size(bin_pack_cb *callback, const void *obj, const Logger
/** @brief Pack an object into a buffer of a given size.
*
* This function creates and initialises a `Bin_Pack` packer object, calls the callback with the
* packer object and the to-be-packed object, and then cleans up the packer object.
* packer object and the to-be-packed object, and then cleans up the packer object. Note that
* there is nothing MessagePack-specific about this function, so it can be used for both custom
* binary and MessagePack formats.
*
* You can use `bin_pack_obj_size` to determine the minimum required size of `buf`. If packing
* overflows `uint32_t`, this function returns `false`.
Expand Down Expand Up @@ -102,13 +109,8 @@ uint32_t bin_pack_obj_array_b_size(bin_pack_array_cb *callback, const void *arr,

/** @brief Pack an object array into a buffer of a given size.
*
* Calls the callback `arr_size` times with increasing `index` argument from 0 to
* `arr_size`. This function is here just so we don't need to write the same
* trivial loop many times and so we don't need an extra struct just to contain
* an array with size so it can be passed to `bin_pack_obj`.
*
* Similar to `bin_pack_obj` but for arrays. Does not write the array length, so
* if you need that, write it manually using `bin_pack_array`.
* Similar to `bin_pack_obj_array` but does not write the array length, so
* if you need that, encoding it is on you.
*
* Passing NULL for `arr` has no effect, but requires that `arr_size` is 0.
*
Expand All @@ -125,6 +127,32 @@ uint32_t bin_pack_obj_array_b_size(bin_pack_array_cb *callback, const void *arr,
non_null(1, 5) nullable(2, 4)
bool bin_pack_obj_array_b(bin_pack_array_cb *callback, const void *arr, uint32_t arr_size, const Logger *logger, uint8_t *buf, uint32_t buf_size);

/** @brief Encode an object array as MessagePack array into a bin packer.
*
* Calls the callback `arr_size` times with increasing `index` argument from 0 to
* `arr_size`. This function is here just so we don't need to write the same
* trivial loop many times and so we don't need an extra struct just to contain
* an array with size so it can be passed to `bin_pack_obj`.
*
* Similar to `bin_pack_obj` but for arrays. Note that a `Bin_Pack` object is
* required here, so it must be called from within a callback to one of the
* functions above.
*
* Passing NULL for `arr` requires that `arr_size` is 0. This will write a 0-size
* MessagePack array to the packer.
*
* @param bp Bin packer object.
* @param callback The function called on the created packer and packed object
* array.
* @param arr The object array to be packed, passed as `arr` to the callback.
* @param arr_size The number of elements in the object array.
* @param logger Optional logger object to pass to the callback.
*
* @retval false if an error occurred (e.g. buffer overflow).
*/
non_null(1, 2) nullable(3, 5)
bool bin_pack_obj_array(Bin_Pack *bp, bin_pack_array_cb *callback, const void *arr, uint32_t arr_size, const Logger *logger);

/** @brief Start packing a MessagePack array.
*
* A call to this function must be followed by exactly `size` calls to other functions below.
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/conference_connected.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ bool tox_event_conference_connected_unpack(
Tox_Event_Conference_Connected **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_conference_connected_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/conference_invite.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ bool tox_event_conference_invite_unpack(
Tox_Event_Conference_Invite **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_conference_invite_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/conference_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ bool tox_event_conference_message_unpack(
Tox_Event_Conference_Message **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_conference_message_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/conference_peer_list_changed.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ bool tox_event_conference_peer_list_changed_unpack(
Tox_Event_Conference_Peer_List_Changed **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_conference_peer_list_changed_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/conference_peer_name.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ bool tox_event_conference_peer_name_unpack(
Tox_Event_Conference_Peer_Name **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_conference_peer_name_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/conference_title.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ bool tox_event_conference_title_unpack(
Tox_Event_Conference_Title **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_conference_title_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/file_chunk_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ bool tox_event_file_chunk_request_unpack(
Tox_Event_File_Chunk_Request **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_file_chunk_request_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/file_recv.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ bool tox_event_file_recv_unpack(
Tox_Event_File_Recv **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_file_recv_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/file_recv_chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ bool tox_event_file_recv_chunk_unpack(
Tox_Event_File_Recv_Chunk **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_file_recv_chunk_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/file_recv_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ bool tox_event_file_recv_control_unpack(
Tox_Event_File_Recv_Control **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_file_recv_control_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/friend_connection_status.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ bool tox_event_friend_connection_status_unpack(
Tox_Event_Friend_Connection_Status **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_friend_connection_status_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/friend_lossless_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ bool tox_event_friend_lossless_packet_unpack(
Tox_Event_Friend_Lossless_Packet **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_friend_lossless_packet_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/friend_lossy_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ bool tox_event_friend_lossy_packet_unpack(
Tox_Event_Friend_Lossy_Packet **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_friend_lossy_packet_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/friend_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ bool tox_event_friend_message_unpack(
Tox_Event_Friend_Message **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_friend_message_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/friend_name.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ bool tox_event_friend_name_unpack(
Tox_Event_Friend_Name **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_friend_name_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/friend_read_receipt.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ bool tox_event_friend_read_receipt_unpack(
Tox_Event_Friend_Read_Receipt **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_friend_read_receipt_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/friend_status.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ bool tox_event_friend_status_unpack(
Tox_Event_Friend_Status **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_friend_status_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/friend_status_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ bool tox_event_friend_status_message_unpack(
Tox_Event_Friend_Status_Message **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_friend_status_message_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/friend_typing.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ bool tox_event_friend_typing_unpack(
Tox_Event_Friend_Typing **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_friend_typing_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/group_custom_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ bool tox_event_group_custom_packet_unpack(
Tox_Event_Group_Custom_Packet **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_custom_packet_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/group_custom_private_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ bool tox_event_group_custom_private_packet_unpack(
Tox_Event_Group_Custom_Private_Packet **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_custom_private_packet_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/group_invite.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ bool tox_event_group_invite_unpack(
Tox_Event_Group_Invite **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_invite_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/group_join_fail.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ bool tox_event_group_join_fail_unpack(
Tox_Event_Group_Join_Fail **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_join_fail_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/group_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ bool tox_event_group_message_unpack(
Tox_Event_Group_Message **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_message_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/group_moderation.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ bool tox_event_group_moderation_unpack(
Tox_Event_Group_Moderation **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_moderation_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/group_password.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ bool tox_event_group_password_unpack(
Tox_Event_Group_Password **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_password_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/group_peer_exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ bool tox_event_group_peer_exit_unpack(
Tox_Event_Group_Peer_Exit **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_peer_exit_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/group_peer_join.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ bool tox_event_group_peer_join_unpack(
Tox_Event_Group_Peer_Join **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_peer_join_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/group_peer_limit.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ bool tox_event_group_peer_limit_unpack(
Tox_Event_Group_Peer_Limit **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_peer_limit_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/group_peer_name.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ bool tox_event_group_peer_name_unpack(
Tox_Event_Group_Peer_Name **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_peer_name_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/group_peer_status.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ bool tox_event_group_peer_status_unpack(
Tox_Event_Group_Peer_Status **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_peer_status_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/group_privacy_state.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ bool tox_event_group_privacy_state_unpack(
Tox_Event_Group_Privacy_State **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_privacy_state_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/group_private_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ bool tox_event_group_private_message_unpack(
Tox_Event_Group_Private_Message **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_private_message_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/group_self_join.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ bool tox_event_group_self_join_unpack(
Tox_Event_Group_Self_Join **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_self_join_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/group_topic.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ bool tox_event_group_topic_unpack(
Tox_Event_Group_Topic **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_topic_new(mem);

if (*event == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/events/group_topic_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ bool tox_event_group_topic_lock_unpack(
Tox_Event_Group_Topic_Lock **event, Bin_Unpack *bu, const Memory *mem)
{
assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_topic_lock_new(mem);

if (*event == nullptr) {
Expand Down
Loading

0 comments on commit ee8615b

Please sign in to comment.