Skip to content

Commit ee8615b

Browse files
committed
refactor: Add common msgpack array packer with callback.
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.
1 parent c650d9d commit ee8615b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+183
-91
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
c31bc1cd7249c62b4f1d2c9ea26c9e8528d9b897dc29afbdbe95894d30c92c8d /usr/local/bin/tox-bootstrapd
1+
95bcb5742f9d92cf0cc29a3aa840887bc1074c76637a9eabe5c63a2e4e709737 /usr/local/bin/tox-bootstrapd

other/event_tooling/generate_event_c.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
452452
f << "bool tox_event_" << event_name_l << "_unpack(\n";
453453
f << " Tox_Event_" << event_name << " **event, Bin_Unpack *bu, const Memory *mem)\n{\n";
454454
f << " assert(event != nullptr);\n";
455+
f << " assert(*event == nullptr);\n";
455456
f << " *event = tox_event_" << event_name_l << "_new(mem);\n\n";
456457
f << " if (*event == nullptr) {\n return false;\n }\n\n";
457458
f << " return tox_event_" << event_name_l << "_unpack_into(*event, bu);\n}\n\n";

toxcore/bin_pack.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,26 @@ bool bin_pack_obj_array_b(bin_pack_array_cb *callback, const void *arr, uint32_t
109109
return true;
110110
}
111111

112+
bool bin_pack_obj_array(Bin_Pack *bp, bin_pack_array_cb *callback, const void *arr, uint32_t arr_size, const Logger *logger)
113+
{
114+
if (arr == nullptr) {
115+
assert(arr_size == 0);
116+
return bin_pack_array(bp, 0);
117+
}
118+
119+
if (!bin_pack_array(bp, arr_size)) {
120+
return false;
121+
}
122+
123+
for (uint32_t i = 0; i < arr_size; ++i) {
124+
if (!callback(arr, i, logger, bp)) {
125+
return false;
126+
}
127+
}
128+
129+
return true;
130+
}
131+
112132
bool bin_pack_array(Bin_Pack *bp, uint32_t size)
113133
{
114134
return cmp_write_array(&bp->ctx, size);

toxcore/bin_pack.h

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ extern "C" {
1717
/**
1818
* @brief Binary serialisation object.
1919
*
20+
* Naming convention:
21+
* - Functions ending in `_b` (or `_b_size`) are NOT MessagePack, i.e. write
22+
* data in plain big endian binary format.
23+
* - All other functions encode their input in MessagePack format.
24+
*
2025
* Some notes on parameter order:
2126
*
2227
* - We pass the `obj` pointer as `this`-like pointer first to the callbacks.
@@ -66,7 +71,9 @@ uint32_t bin_pack_obj_size(bin_pack_cb *callback, const void *obj, const Logger
6671
/** @brief Pack an object into a buffer of a given size.
6772
*
6873
* This function creates and initialises a `Bin_Pack` packer object, calls the callback with the
69-
* packer object and the to-be-packed object, and then cleans up the packer object.
74+
* packer object and the to-be-packed object, and then cleans up the packer object. Note that
75+
* there is nothing MessagePack-specific about this function, so it can be used for both custom
76+
* binary and MessagePack formats.
7077
*
7178
* You can use `bin_pack_obj_size` to determine the minimum required size of `buf`. If packing
7279
* overflows `uint32_t`, this function returns `false`.
@@ -102,13 +109,8 @@ uint32_t bin_pack_obj_array_b_size(bin_pack_array_cb *callback, const void *arr,
102109

103110
/** @brief Pack an object array into a buffer of a given size.
104111
*
105-
* Calls the callback `arr_size` times with increasing `index` argument from 0 to
106-
* `arr_size`. This function is here just so we don't need to write the same
107-
* trivial loop many times and so we don't need an extra struct just to contain
108-
* an array with size so it can be passed to `bin_pack_obj`.
109-
*
110-
* Similar to `bin_pack_obj` but for arrays. Does not write the array length, so
111-
* if you need that, write it manually using `bin_pack_array`.
112+
* Similar to `bin_pack_obj_array` but does not write the array length, so
113+
* if you need that, encoding it is on you.
112114
*
113115
* Passing NULL for `arr` has no effect, but requires that `arr_size` is 0.
114116
*
@@ -125,6 +127,32 @@ uint32_t bin_pack_obj_array_b_size(bin_pack_array_cb *callback, const void *arr,
125127
non_null(1, 5) nullable(2, 4)
126128
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);
127129

130+
/** @brief Encode an object array as MessagePack array into a bin packer.
131+
*
132+
* Calls the callback `arr_size` times with increasing `index` argument from 0 to
133+
* `arr_size`. This function is here just so we don't need to write the same
134+
* trivial loop many times and so we don't need an extra struct just to contain
135+
* an array with size so it can be passed to `bin_pack_obj`.
136+
*
137+
* Similar to `bin_pack_obj` but for arrays. Note that a `Bin_Pack` object is
138+
* required here, so it must be called from within a callback to one of the
139+
* functions above.
140+
*
141+
* Passing NULL for `arr` requires that `arr_size` is 0. This will write a 0-size
142+
* MessagePack array to the packer.
143+
*
144+
* @param bp Bin packer object.
145+
* @param callback The function called on the created packer and packed object
146+
* array.
147+
* @param arr The object array to be packed, passed as `arr` to the callback.
148+
* @param arr_size The number of elements in the object array.
149+
* @param logger Optional logger object to pass to the callback.
150+
*
151+
* @retval false if an error occurred (e.g. buffer overflow).
152+
*/
153+
non_null(1, 2) nullable(3, 5)
154+
bool bin_pack_obj_array(Bin_Pack *bp, bin_pack_array_cb *callback, const void *arr, uint32_t arr_size, const Logger *logger);
155+
128156
/** @brief Start packing a MessagePack array.
129157
*
130158
* A call to this function must be followed by exactly `size` calls to other functions below.

toxcore/events/conference_connected.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ bool tox_event_conference_connected_unpack(
119119
Tox_Event_Conference_Connected **event, Bin_Unpack *bu, const Memory *mem)
120120
{
121121
assert(event != nullptr);
122+
assert(*event == nullptr);
122123
*event = tox_event_conference_connected_new(mem);
123124

124125
if (*event == nullptr) {

toxcore/events/conference_invite.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ bool tox_event_conference_invite_unpack(
187187
Tox_Event_Conference_Invite **event, Bin_Unpack *bu, const Memory *mem)
188188
{
189189
assert(event != nullptr);
190+
assert(*event == nullptr);
190191
*event = tox_event_conference_invite_new(mem);
191192

192193
if (*event == nullptr) {

toxcore/events/conference_message.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ bool tox_event_conference_message_unpack(
203203
Tox_Event_Conference_Message **event, Bin_Unpack *bu, const Memory *mem)
204204
{
205205
assert(event != nullptr);
206+
assert(*event == nullptr);
206207
*event = tox_event_conference_message_new(mem);
207208

208209
if (*event == nullptr) {

toxcore/events/conference_peer_list_changed.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ bool tox_event_conference_peer_list_changed_unpack(
119119
Tox_Event_Conference_Peer_List_Changed **event, Bin_Unpack *bu, const Memory *mem)
120120
{
121121
assert(event != nullptr);
122+
assert(*event == nullptr);
122123
*event = tox_event_conference_peer_list_changed_new(mem);
123124

124125
if (*event == nullptr) {

toxcore/events/conference_peer_name.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ bool tox_event_conference_peer_name_unpack(
185185
Tox_Event_Conference_Peer_Name **event, Bin_Unpack *bu, const Memory *mem)
186186
{
187187
assert(event != nullptr);
188+
assert(*event == nullptr);
188189
*event = tox_event_conference_peer_name_new(mem);
189190

190191
if (*event == nullptr) {

toxcore/events/conference_title.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ bool tox_event_conference_title_unpack(
185185
Tox_Event_Conference_Title **event, Bin_Unpack *bu, const Memory *mem)
186186
{
187187
assert(event != nullptr);
188+
assert(*event == nullptr);
188189
*event = tox_event_conference_title_new(mem);
189190

190191
if (*event == nullptr) {

0 commit comments

Comments
 (0)