summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2020-08-20 09:38:40 -0400
committerAndrew Cady <d@jerkface.net>2020-08-20 16:29:57 -0400
commitab4fa64b6353d9e30da9b8beeb5e52d4b2fbe5b6 (patch)
treee51260bd115bc60008097d2db3e891b8bfb0dc64
parent6ce9e569d8afa86d80005775c0a86cce9c4847a7 (diff)
use tox_callback_friend_connection_status instead of polling
-rw-r--r--client.c50
-rw-r--r--main.c15
-rw-r--r--main.h2
3 files changed, 20 insertions, 47 deletions
diff --git a/client.c b/client.c
index 5ea3b4d..67d4fd3 100644
--- a/client.c
+++ b/client.c
@@ -279,13 +279,10 @@ int do_client_loop(uint8_t *tox_id_str)
279 unsigned char tox_packet_buf[PROTOCOL_MAX_PACKET_SIZE]; 279 unsigned char tox_packet_buf[PROTOCOL_MAX_PACKET_SIZE];
280 unsigned char tox_id[TOX_ADDRESS_SIZE]; 280 unsigned char tox_id[TOX_ADDRESS_SIZE];
281 uint32_t friendnumber = 0; 281 uint32_t friendnumber = 0;
282 TOX_CONNECTION last_friend_connection_status = TOX_CONNECTION_NONE;
283 time_t last_friend_connection_status_received = 0;
284 struct timeval tv; 282 struct timeval tv;
285 fd_set fds; 283 fd_set fds;
286 static time_t invitation_sent_time = 0; 284 static time_t invitation_sent_time = 0;
287 uint32_t invitations_sent = 0; 285 uint32_t invitations_sent = 0;
288 TOX_ERR_FRIEND_QUERY friend_query_error;
289 TOX_ERR_FRIEND_CUSTOM_PACKET custom_packet_error; 286 TOX_ERR_FRIEND_CUSTOM_PACKET custom_packet_error;
290 287
291 client_tunnel.sockfd = 0; 288 client_tunnel.sockfd = 0;
@@ -371,17 +368,7 @@ int do_client_loop(uint8_t *tox_id_str)
371 break; 368 break;
372 case CLIENT_STATE_SENTREQUEST: 369 case CLIENT_STATE_SENTREQUEST:
373 { 370 {
374 TOX_CONNECTION friend_connection_status;
375 friend_connection_status = tox_friend_get_connection_status(tox, friendnumber, &friend_query_error);
376 if(friend_query_error != TOX_ERR_FRIEND_QUERY_OK)
377 { 371 {
378 log_printf(L_DEBUG, "tox_friend_get_connection_status: error %u", friend_query_error);
379 }
380 else
381 {
382 last_friend_connection_status_received = time(NULL);
383 last_friend_connection_status = friend_connection_status;
384
385 if(friend_connection_status != TOX_CONNECTION_NONE) 372 if(friend_connection_status != TOX_CONNECTION_NONE)
386 { 373 {
387 const char* status = readable_connection_status(friend_connection_status); 374 const char* status = readable_connection_status(friend_connection_status);
@@ -390,7 +377,8 @@ int do_client_loop(uint8_t *tox_id_str)
390 } 377 }
391 else 378 else
392 { 379 {
393 if(1 && (time(NULL) - invitation_sent_time > 45)) 380 const int INVITATION_SEND_INTERVAL = 90;
381 if (time(NULL) - invitation_sent_time > INVITATION_SEND_INTERVAL)
394 { 382 {
395 TOX_ERR_FRIEND_DELETE error = 0; 383 TOX_ERR_FRIEND_DELETE error = 0;
396 384
@@ -597,44 +585,14 @@ int do_client_loop(uint8_t *tox_id_str)
597 fds = client_master_fdset; 585 fds = client_master_fdset;
598 586
599 /* Check friend connection status changes */ 587 /* Check friend connection status changes */
600 /* TODO: learned about tox_friend_connection_status_cb after writing this... */ 588 if(friend_connection_status == TOX_CONNECTION_NONE)
601 /* TODO: also check friend status tox_callback_friend_status */
602 if(time(NULL) - last_friend_connection_status_received > 15)
603 { 589 {
604 TOX_CONNECTION friend_connection_status; 590 state = CLIENT_STATE_CONNECTION_LOST;
605 friend_connection_status = tox_friend_get_connection_status(tox, friendnumber, &friend_query_error);
606 if(friend_query_error != TOX_ERR_FRIEND_QUERY_OK)
607 {
608 log_printf(L_DEBUG, "tox_friend_get_connection_status: error %u\n", friend_query_error);
609 }
610 else
611 {
612 if(friend_connection_status != last_friend_connection_status)
613 {
614 const char* status = readable_connection_status(friend_connection_status);
615 log_printf(L_INFO, "Friend connection status changed to: %s (%d)\n", status, friend_connection_status);
616
617 if(friend_connection_status == TOX_CONNECTION_NONE)
618 {
619 state = CLIENT_STATE_CONNECTION_LOST;
620 }
621 }
622
623 last_friend_connection_status_received = time(NULL);
624 last_friend_connection_status = friend_connection_status;
625 }
626 } 591 }
627 } 592 }
628 break; 593 break;
629 case CLIENT_STATE_CONNECTION_LOST: 594 case CLIENT_STATE_CONNECTION_LOST:
630 { 595 {
631 TOX_CONNECTION friend_connection_status;
632 friend_connection_status = tox_friend_get_connection_status(tox, friendnumber, &friend_query_error);
633 if(friend_query_error != TOX_ERR_FRIEND_QUERY_OK)
634 {
635 log_printf(L_DEBUG, "tox_friend_get_connection_status: error %u\n", friend_query_error);
636 }
637 else
638 { 596 {
639 if(friend_connection_status == TOX_CONNECTION_NONE) 597 if(friend_connection_status == TOX_CONNECTION_NONE)
640 { 598 {
diff --git a/main.c b/main.c
index 117747b..f38784e 100644
--- a/main.c
+++ b/main.c
@@ -12,6 +12,7 @@ Tox *tox;
12int client_socket = 0; 12int client_socket = 0;
13TOX_CONNECTION connection_status = TOX_CONNECTION_NONE; 13TOX_CONNECTION connection_status = TOX_CONNECTION_NONE;
14 14
15TOX_CONNECTION friend_connection_status = TOX_CONNECTION_NONE;
15/** CONFIGURATION OPTIONS **/ 16/** CONFIGURATION OPTIONS **/
16/* Whether we're a client */ 17/* Whether we're a client */
17int client_mode = 0; 18int client_mode = 0;
@@ -908,7 +909,7 @@ void accept_friend_request(Tox *tox, const uint8_t *public_key, const uint8_t *m
908 return; 909 return;
909 } 910 }
910 911
911 log_printf(L_INFO, "Accepted friend request from %s as %d\n", tox_printable_id, friendnumber); 912 log_printf(L_INFO, "Accepted friend request from %s as #%d\n", tox_printable_id, friendnumber);
912} 913}
913 914
914/* Callback for tox_callback_self_connection_status() */ 915/* Callback for tox_callback_self_connection_status() */
@@ -920,6 +921,17 @@ void handle_connection_status_change(Tox *tox, TOX_CONNECTION p_connection_statu
920 log_printf(L_INFO, "Connection status changed: %s", status); 921 log_printf(L_INFO, "Connection status changed: %s", status);
921} 922}
922 923
924void handle_friend_connection_status(Tox *tox, uint32_t friend_number, TOX_CONNECTION connection_status, void *user_data)
925{
926 const char *status = NULL;
927 status = readable_connection_status(connection_status);
928 log_printf(L_INFO, "Friend #%d connection status changed: %s", friend_number, status);
929 if(client_mode)
930 {
931 friend_connection_status = connection_status;
932 }
933}
934
923void cleanup() 935void cleanup()
924{ 936{
925 log_printf(L_DEBUG, "kthxbye\n"); 937 log_printf(L_DEBUG, "kthxbye\n");
@@ -1521,6 +1533,7 @@ int main(int argc, char *argv[])
1521 1533
1522 set_tox_username(tox); 1534 set_tox_username(tox);
1523 tox_callback_self_connection_status(tox, handle_connection_status_change); 1535 tox_callback_self_connection_status(tox, handle_connection_status_change);
1536 tox_callback_friend_connection_status(tox, handle_friend_connection_status);
1524 1537
1525 do_bootstrap(tox); 1538 do_bootstrap(tox);
1526 1539
diff --git a/main.h b/main.h
index 17fb9b9..b998acc 100644
--- a/main.h
+++ b/main.h
@@ -112,6 +112,8 @@ extern char shared_secret[TOX_MAX_FRIEND_REQUEST_LENGTH];
112extern int select_nfds; 112extern int select_nfds;
113extern tunnel *by_id; 113extern tunnel *by_id;
114 114
115extern TOX_CONNECTION friend_connection_status;
116
115void parse_lossless_packet(Tox *tox, uint32_t friendnumber, const uint8_t *data, size_t len, void *tmp); 117void parse_lossless_packet(Tox *tox, uint32_t friendnumber, const uint8_t *data, size_t len, void *tmp);
116tunnel *tunnel_create(int sockfd, int connid, uint32_t friendnumber); 118tunnel *tunnel_create(int sockfd, int connid, uint32_t friendnumber);
117void tunnel_delete(tunnel *t); 119void tunnel_delete(tunnel *t);