diff options
Diffstat (limited to 'toxcore/group_chats.c')
-rw-r--r-- | toxcore/group_chats.c | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c index fdf6e834..85057e9f 100644 --- a/toxcore/group_chats.c +++ b/toxcore/group_chats.c | |||
@@ -205,13 +205,14 @@ static int addpeer(Group_Chat *chat, uint8_t *client_id) | |||
205 | 205 | ||
206 | Group_Peer *temp; | 206 | Group_Peer *temp; |
207 | temp = realloc(chat->group, sizeof(Group_Peer) * (chat->numpeers + 1)); | 207 | temp = realloc(chat->group, sizeof(Group_Peer) * (chat->numpeers + 1)); |
208 | memset(&(temp[chat->numpeers]), 0, sizeof(Group_Peer)); | ||
209 | 208 | ||
210 | if (temp == NULL) | 209 | if (temp == NULL) |
211 | return -1; | 210 | return -1; |
212 | 211 | ||
212 | memset(&(temp[chat->numpeers]), 0, sizeof(Group_Peer)); | ||
213 | chat->group = temp; | 213 | chat->group = temp; |
214 | id_copy(chat->group[chat->numpeers].client_id, client_id); | 214 | id_copy(chat->group[chat->numpeers].client_id, client_id); |
215 | chat->group[chat->numpeers].last_recv = unix_time(); | ||
215 | ++chat->numpeers; | 216 | ++chat->numpeers; |
216 | return (chat->numpeers - 1); | 217 | return (chat->numpeers - 1); |
217 | } | 218 | } |
@@ -392,9 +393,9 @@ static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len) | |||
392 | 393 | ||
393 | if (chat->group[peernum].last_recv == temp_time) | 394 | if (chat->group[peernum].last_recv == temp_time) |
394 | return 1; | 395 | return 1; |
395 | |||
396 | chat->group[peernum].last_recv = temp_time; | ||
397 | */ | 396 | */ |
397 | chat->group[peernum].last_recv = unix_time(); | ||
398 | |||
398 | uint32_t message_num; | 399 | uint32_t message_num; |
399 | memcpy(&message_num, data + crypto_box_PUBLICKEYBYTES, sizeof(uint32_t)); | 400 | memcpy(&message_num, data + crypto_box_PUBLICKEYBYTES, sizeof(uint32_t)); |
400 | message_num = ntohl(message_num); | 401 | message_num = ntohl(message_num); |
@@ -461,7 +462,10 @@ static uint8_t send_data(Group_Chat *chat, uint8_t *data, uint32_t len, uint8_t | |||
461 | //TODO | 462 | //TODO |
462 | id_copy(packet, chat->self_public_key); | 463 | id_copy(packet, chat->self_public_key); |
463 | memcpy(packet + crypto_box_PUBLICKEYBYTES, &message_num, sizeof(message_num)); | 464 | memcpy(packet + crypto_box_PUBLICKEYBYTES, &message_num, sizeof(message_num)); |
464 | memcpy(packet + GROUP_DATA_MIN_SIZE, data, len); | 465 | |
466 | if (len != 0) | ||
467 | memcpy(packet + GROUP_DATA_MIN_SIZE, data, len); | ||
468 | |||
465 | packet[crypto_box_PUBLICKEYBYTES + sizeof(message_num)] = message_id; | 469 | packet[crypto_box_PUBLICKEYBYTES + sizeof(message_num)] = message_id; |
466 | return sendto_allpeers(chat, packet, len + GROUP_DATA_MIN_SIZE, 50); | 470 | return sendto_allpeers(chat, packet, len + GROUP_DATA_MIN_SIZE, 50); |
467 | } | 471 | } |
@@ -531,7 +535,7 @@ void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, | |||
531 | Group_Chat *new_groupchat(Networking_Core *net) | 535 | Group_Chat *new_groupchat(Networking_Core *net) |
532 | { | 536 | { |
533 | unix_time_update(); | 537 | unix_time_update(); |
534 | 538 | ||
535 | if (net == 0) | 539 | if (net == 0) |
536 | return 0; | 540 | return 0; |
537 | 541 | ||
@@ -561,10 +565,22 @@ static void ping_close(Group_Chat *chat) | |||
561 | } | 565 | } |
562 | } | 566 | } |
563 | 567 | ||
568 | /* Interval in seconds to send ping messages */ | ||
569 | #define GROUP_PING_INTERVAL 30 | ||
570 | |||
571 | static void ping_group(Group_Chat *chat) | ||
572 | { | ||
573 | if (is_timeout(chat->last_sent_ping, GROUP_PING_INTERVAL)) { | ||
574 | if (send_data(chat, 0, 0, 0) != 0) /* Ping */ | ||
575 | chat->last_sent_ping = unix_time(); | ||
576 | } | ||
577 | } | ||
578 | |||
564 | void do_groupchat(Group_Chat *chat) | 579 | void do_groupchat(Group_Chat *chat) |
565 | { | 580 | { |
566 | unix_time_update(); | 581 | unix_time_update(); |
567 | ping_close(chat); | 582 | ping_close(chat); |
583 | ping_group(chat); | ||
568 | } | 584 | } |
569 | 585 | ||
570 | void kill_groupchat(Group_Chat *chat) | 586 | void kill_groupchat(Group_Chat *chat) |