summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-11-06 20:44:31 -0500
committerirungentoo <irungentoo@gmail.com>2014-11-06 20:44:31 -0500
commit0bcfd032e2c8dc4bc7eebf4d908d5d03c7f1dead (patch)
tree951c4203fc2b8a26c5201dcd5a3148b4cad569e8
parentbe27c6cfc80cd8564bb8197f51a1954662c881e5 (diff)
Added some functions to send and handle lossy packets.
-rw-r--r--toxcore/group.c51
-rw-r--r--toxcore/group.h19
2 files changed, 67 insertions, 3 deletions
diff --git a/toxcore/group.c b/toxcore/group.c
index 19d7ffa5..da56c572 100644
--- a/toxcore/group.c
+++ b/toxcore/group.c
@@ -925,6 +925,19 @@ void g_callback_group_action(Group_Chats *g_c, void (*function)(Messenger *m, in
925 g_c->action_callback_userdata = userdata; 925 g_c->action_callback_userdata = userdata;
926} 926}
927 927
928/* Set handlers for custom lossy packets.
929 * NOTE: Handler must return 0 if packet is to be relayed, -1 if the packet should not be relayed.
930 *
931 * return -1 on failure.
932 * return 0 on success.
933 */
934void group_lossy_packet_registerhandler(Group_Chats *g_c, uint8_t byte, int (*function)(Messenger *m, int, int,
935 const uint8_t *, uint16_t, void *), void *userdata)
936{
937 g_c->lossy_packethandlers[byte].function = function;
938 g_c->lossy_packethandlers[byte].userdata = userdata;
939}
940
928/* Set callback function for peer name list changes. 941/* Set callback function for peer name list changes.
929 * 942 *
930 * It gets called every time the name list changes(new peer/name, deleted peer) 943 * It gets called every time the name list changes(new peer/name, deleted peer)
@@ -1466,6 +1479,34 @@ int group_action_send(const Group_Chats *g_c, int groupnumber, const uint8_t *ac
1466 } 1479 }
1467} 1480}
1468 1481
1482/* High level function to send custom lossy packets.
1483 *
1484 * return -1 on failure.
1485 * return 0 on success.
1486 */
1487int send_group_lossy_packet(const Group_Chats *g_c, int groupnumber, const uint8_t *data, uint16_t length)
1488{
1489 //TODO: length check here?
1490 Group_c *g = get_group_c(g_c, groupnumber);
1491
1492 if (!g)
1493 return -1;
1494
1495 uint8_t packet[sizeof(uint16_t) * 2 + length];
1496 uint16_t peer_number = htons(g->peer_number);
1497 memcpy(packet, &peer_number, sizeof(uint16_t));
1498 uint16_t message_num = htons(g->lossy_message_number);
1499 memcpy(packet + sizeof(uint16_t), &message_num, sizeof(uint16_t));
1500 memcpy(packet + sizeof(uint16_t) * 2, data, length);
1501
1502 if (send_lossy_all_close(g_c, groupnumber, packet, sizeof(packet), -1) == 0) {
1503 return -1;
1504 }
1505
1506 ++g->lossy_message_number;
1507 return 0;
1508}
1509
1469static void handle_message_packet_group(Group_Chats *g_c, int groupnumber, const uint8_t *data, uint16_t length, 1510static void handle_message_packet_group(Group_Chats *g_c, int groupnumber, const uint8_t *data, uint16_t length,
1470 int close_index) 1511 int close_index)
1471{ 1512{
@@ -1730,9 +1771,13 @@ static int handle_lossy(void *object, int friendcon_id, const uint8_t *data, uin
1730 ++lossy_data; 1771 ++lossy_data;
1731 --lossy_length; 1772 --lossy_length;
1732 1773
1733 switch (message_id) { 1774 if (g_c->lossy_packethandlers[message_id].function) {
1734 1775 if (g_c->lossy_packethandlers[message_id].function(g_c->m, groupnumber, index, lossy_data, lossy_length,
1735 1776 g_c->lossy_packethandlers[message_id].userdata) == -1) {
1777 return -1;
1778 }
1779 } else {
1780 return -1;
1736 } 1781 }
1737 1782
1738 send_lossy_all_close(g_c, groupnumber, data + 1 + sizeof(uint16_t), length - (1 + sizeof(uint16_t)), 1783 send_lossy_all_close(g_c, groupnumber, data + 1 + sizeof(uint16_t), length - (1 + sizeof(uint16_t)),
diff --git a/toxcore/group.h b/toxcore/group.h
index 5adde463..e307bec9 100644
--- a/toxcore/group.h
+++ b/toxcore/group.h
@@ -108,6 +108,11 @@ typedef struct {
108 void *action_callback_userdata; 108 void *action_callback_userdata;
109 void (*peer_namelistchange)(Messenger *m, int, int, uint8_t, void *); 109 void (*peer_namelistchange)(Messenger *m, int, int, uint8_t, void *);
110 void *group_namelistchange_userdata; 110 void *group_namelistchange_userdata;
111
112 struct {
113 int (*function)(Messenger *m, int, int, const uint8_t *, uint16_t, void *);
114 void *userdata;
115 } lossy_packethandlers[256];
111} Group_Chats; 116} Group_Chats;
112 117
113/* Set the callback for group invites. 118/* Set the callback for group invites.
@@ -216,6 +221,20 @@ unsigned int group_peernumber_is_ours(const Group_Chats *g_c, int groupnumber, i
216int group_names(const Group_Chats *g_c, int groupnumber, uint8_t names[][MAX_NAME_LENGTH], uint16_t lengths[], 221int group_names(const Group_Chats *g_c, int groupnumber, uint8_t names[][MAX_NAME_LENGTH], uint16_t lengths[],
217 uint16_t length); 222 uint16_t length);
218 223
224/* Set handlers for custom lossy packets.
225 *
226 * NOTE: Handler must return 0 if packet is to be relayed, -1 if the packet should not be relayed.
227 */
228void group_lossy_packet_registerhandler(Group_Chats *g_c, uint8_t byte, int (*function)(Messenger *m, int, int,
229 const uint8_t *, uint16_t, void *), void *userdata);
230
231/* High level function to send custom lossy packets.
232 *
233 * return -1 on failure.
234 * return 0 on success.
235 */
236int send_group_lossy_packet(const Group_Chats *g_c, int groupnumber, const uint8_t *data, uint16_t length);
237
219/* Return the number of chats in the instance m. 238/* Return the number of chats in the instance m.
220 * You should use this to determine how much memory to allocate 239 * You should use this to determine how much memory to allocate
221 * for copy_chatlist. 240 * for copy_chatlist.