summaryrefslogtreecommitdiff
path: root/toxcore/tox.c
diff options
context:
space:
mode:
authorzoff99 <zoff@zoff.cc>2020-04-09 20:07:32 +0200
committeriphydf <iphydf@users.noreply.github.com>2020-04-16 12:28:13 +0000
commitcdafc6ff5ee13595ecf5a4e1fe0cb28e255c37df (patch)
treee11907b288b1ff68a69ed74ed33c0f48676a307f /toxcore/tox.c
parentc08b2fb3e2c3c8e8a5722e7350b61efd6992be45 (diff)
Add new semi-private API functions to set per-packet-id custom handlers.
This is to prepare for ToxAV becoming independent of toxcore internal calls.
Diffstat (limited to 'toxcore/tox.c')
-rw-r--r--toxcore/tox.c66
1 files changed, 55 insertions, 11 deletions
diff --git a/toxcore/tox.c b/toxcore/tox.c
index fdf2b42c..7126631e 100644
--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -17,6 +17,7 @@
17#endif 17#endif
18 18
19#include "tox.h" 19#include "tox.h"
20#include "tox_private.h"
20 21
21#include <assert.h> 22#include <assert.h>
22#include <stdlib.h> 23#include <stdlib.h>
@@ -89,8 +90,10 @@ struct Tox {
89 tox_conference_title_cb *conference_title_callback; 90 tox_conference_title_cb *conference_title_callback;
90 tox_conference_peer_name_cb *conference_peer_name_callback; 91 tox_conference_peer_name_cb *conference_peer_name_callback;
91 tox_conference_peer_list_changed_cb *conference_peer_list_changed_callback; 92 tox_conference_peer_list_changed_cb *conference_peer_list_changed_callback;
92 tox_friend_lossy_packet_cb *friend_lossy_packet_callback; 93 tox_friend_lossy_packet_cb *friend_lossy_packet_callback_per_pktid[UINT8_MAX + 1];
93 tox_friend_lossless_packet_cb *friend_lossless_packet_callback; 94 tox_friend_lossless_packet_cb *friend_lossless_packet_callback_per_pktid[UINT8_MAX + 1];
95
96 void *toxav_object; // workaround to store a ToxAV object (setter and getter functions are available)
94}; 97};
95 98
96static void lock(const Tox *tox) 99static void lock(const Tox *tox)
@@ -309,20 +312,28 @@ static void tox_conference_peer_list_changed_handler(Messenger *m, uint32_t conf
309static void tox_friend_lossy_packet_handler(Messenger *m, uint32_t friend_number, uint8_t packet_id, 312static void tox_friend_lossy_packet_handler(Messenger *m, uint32_t friend_number, uint8_t packet_id,
310 const uint8_t *data, size_t length, void *user_data) 313 const uint8_t *data, size_t length, void *user_data)
311{ 314{
315 assert(data != nullptr);
316 assert(length > 0);
317
312 struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; 318 struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
313 319
314 if (tox_data->tox->friend_lossy_packet_callback != nullptr) { 320 if (tox_data->tox->friend_lossy_packet_callback_per_pktid[packet_id] != nullptr) {
315 tox_data->tox->friend_lossy_packet_callback(tox_data->tox, friend_number, data, length, tox_data->user_data); 321 tox_data->tox->friend_lossy_packet_callback_per_pktid[packet_id](tox_data->tox, friend_number, data, length,
322 tox_data->user_data);
316 } 323 }
317} 324}
318 325
319static void tox_friend_lossless_packet_handler(Messenger *m, uint32_t friend_number, uint8_t packet_id, 326static void tox_friend_lossless_packet_handler(Messenger *m, uint32_t friend_number, uint8_t packet_id,
320 const uint8_t *data, size_t length, void *user_data) 327 const uint8_t *data, size_t length, void *user_data)
321{ 328{
329 assert(data != nullptr);
330 assert(length > 0);
331
322 struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; 332 struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
323 333
324 if (tox_data->tox->friend_lossless_packet_callback != nullptr) { 334 if (tox_data->tox->friend_lossless_packet_callback_per_pktid[packet_id] != nullptr) {
325 tox_data->tox->friend_lossless_packet_callback(tox_data->tox, friend_number, data, length, tox_data->user_data); 335 tox_data->tox->friend_lossless_packet_callback_per_pktid[packet_id](tox_data->tox, friend_number, data, length,
336 tox_data->user_data);
326 } 337 }
327} 338}
328 339
@@ -2171,9 +2182,7 @@ bool tox_friend_send_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_
2171 return 0; 2182 return 0;
2172 } 2183 }
2173 2184
2174 // TODO(oxij): this feels ugly, this is needed only because m_send_custom_lossy_packet in Messenger.c 2185 if (data[0] < PACKET_ID_RANGE_LOSSY_START || data[0] > PACKET_ID_RANGE_LOSSY_END) {
2175 // sends both AV and custom packets despite its name and this API hides those AV packets
2176 if (data[0] <= PACKET_ID_RANGE_LOSSY_AV_END) {
2177 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_CUSTOM_PACKET_INVALID); 2186 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_CUSTOM_PACKET_INVALID);
2178 return 0; 2187 return 0;
2179 } 2188 }
@@ -2193,7 +2202,17 @@ bool tox_friend_send_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_
2193 2202
2194void tox_callback_friend_lossy_packet(Tox *tox, tox_friend_lossy_packet_cb *callback) 2203void tox_callback_friend_lossy_packet(Tox *tox, tox_friend_lossy_packet_cb *callback)
2195{ 2204{
2196 tox->friend_lossy_packet_callback = callback; 2205 /* start at PACKET_ID_RANGE_LOSSY_CUSTOM_START so ToxAV Packets are excluded */
2206 for (uint8_t i = PACKET_ID_RANGE_LOSSY_CUSTOM_START; i <= PACKET_ID_RANGE_LOSSY_END; ++i) {
2207 tox->friend_lossy_packet_callback_per_pktid[i] = callback;
2208 }
2209}
2210
2211void tox_callback_friend_lossy_packet_per_pktid(Tox *tox, tox_friend_lossy_packet_cb *callback, uint8_t pktid)
2212{
2213 if (pktid >= PACKET_ID_RANGE_LOSSY_START && pktid <= PACKET_ID_RANGE_LOSSY_END) {
2214 tox->friend_lossy_packet_callback_per_pktid[pktid] = callback;
2215 }
2197} 2216}
2198 2217
2199bool tox_friend_send_lossless_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, 2218bool tox_friend_send_lossless_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length,
@@ -2224,7 +2243,17 @@ bool tox_friend_send_lossless_packet(Tox *tox, uint32_t friend_number, const uin
2224 2243
2225void tox_callback_friend_lossless_packet(Tox *tox, tox_friend_lossless_packet_cb *callback) 2244void tox_callback_friend_lossless_packet(Tox *tox, tox_friend_lossless_packet_cb *callback)
2226{ 2245{
2227 tox->friend_lossless_packet_callback = callback; 2246 for (uint8_t i = PACKET_ID_RANGE_LOSSLESS_CUSTOM_START; i <= PACKET_ID_RANGE_LOSSLESS_CUSTOM_END; ++i) {
2247 tox->friend_lossless_packet_callback_per_pktid[i] = callback;
2248 }
2249}
2250
2251void tox_callback_friend_lossless_packet_per_pktid(Tox *tox, tox_friend_lossless_packet_cb *callback, uint8_t pktid)
2252{
2253 if ((pktid >= PACKET_ID_RANGE_LOSSLESS_CUSTOM_START && pktid <= PACKET_ID_RANGE_LOSSLESS_CUSTOM_END)
2254 || pktid == PACKET_ID_MSI) {
2255 tox->friend_lossless_packet_callback_per_pktid[pktid] = callback;
2256 }
2228} 2257}
2229 2258
2230void tox_self_get_dht_id(const Tox *tox, uint8_t *dht_id) 2259void tox_self_get_dht_id(const Tox *tox, uint8_t *dht_id)
@@ -2236,6 +2265,21 @@ void tox_self_get_dht_id(const Tox *tox, uint8_t *dht_id)
2236 } 2265 }
2237} 2266}
2238 2267
2268void tox_set_av_object(Tox *tox, void *object)
2269{
2270 lock(tox);
2271 tox->toxav_object = object;
2272 unlock(tox);
2273}
2274
2275void *tox_get_av_object(const Tox *tox)
2276{
2277 lock(tox);
2278 void *object = tox->toxav_object;
2279 unlock(tox);
2280 return object;
2281}
2282
2239uint16_t tox_self_get_udp_port(const Tox *tox, Tox_Err_Get_Port *error) 2283uint16_t tox_self_get_udp_port(const Tox *tox, Tox_Err_Get_Port *error)
2240{ 2284{
2241 lock(tox); 2285 lock(tox);