diff options
-rw-r--r-- | toxcore/TCP_connection.c | 36 | ||||
-rw-r--r-- | toxcore/TCP_connection.h | 3 |
2 files changed, 31 insertions, 8 deletions
diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c index e11e1318..331d772b 100644 --- a/toxcore/TCP_connection.c +++ b/toxcore/TCP_connection.c | |||
@@ -26,6 +26,7 @@ | |||
26 | #endif | 26 | #endif |
27 | 27 | ||
28 | #include "TCP_connection.h" | 28 | #include "TCP_connection.h" |
29 | #include "util.h" | ||
29 | 30 | ||
30 | /* Set the size of the array to num. | 31 | /* Set the size of the array to num. |
31 | * | 32 | * |
@@ -451,7 +452,9 @@ int kill_tcp_connection_to(TCP_Connections *tcp_c, int connections_number) | |||
451 | send_disconnect_request(tcp_con->connection, con_to->connections[i].connection_id); | 452 | send_disconnect_request(tcp_con->connection, con_to->connections[i].connection_id); |
452 | } | 453 | } |
453 | 454 | ||
454 | --tcp_con->lock_count; | 455 | if (con_to->connections[i].status == TCP_CONNECTIONS_STATUS_ONLINE) { |
456 | --tcp_con->lock_count; | ||
457 | } | ||
455 | } | 458 | } |
456 | } | 459 | } |
457 | 460 | ||
@@ -522,6 +525,11 @@ static int set_tcp_connection_status(TCP_Connection_to *con_to, int tcp_connecti | |||
522 | 525 | ||
523 | for (i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) { | 526 | for (i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) { |
524 | if (con_to->connections[i].tcp_connection == (tcp_connections_number + 1)) { | 527 | if (con_to->connections[i].tcp_connection == (tcp_connections_number + 1)) { |
528 | |||
529 | if (con_to->connections[i].status == status) { | ||
530 | return -1; | ||
531 | } | ||
532 | |||
525 | con_to->connections[i].status = status; | 533 | con_to->connections[i].status = status; |
526 | con_to->connections[i].connection_id = connection_id; | 534 | con_to->connections[i].connection_id = connection_id; |
527 | return i; | 535 | return i; |
@@ -621,10 +629,12 @@ static int tcp_status_callback(void *object, uint32_t number, uint8_t connection | |||
621 | if (set_tcp_connection_status(con_to, tcp_connections_number, TCP_CONNECTIONS_STATUS_REGISTERED, connection_id) == -1) | 629 | if (set_tcp_connection_status(con_to, tcp_connections_number, TCP_CONNECTIONS_STATUS_REGISTERED, connection_id) == -1) |
622 | return -1; | 630 | return -1; |
623 | 631 | ||
632 | --tcp_con->lock_count; | ||
624 | } else if (status == 2) { | 633 | } else if (status == 2) { |
625 | if (set_tcp_connection_status(con_to, tcp_connections_number, TCP_CONNECTIONS_STATUS_ONLINE, connection_id) == -1) | 634 | if (set_tcp_connection_status(con_to, tcp_connections_number, TCP_CONNECTIONS_STATUS_ONLINE, connection_id) == -1) |
626 | return -1; | 635 | return -1; |
627 | 636 | ||
637 | ++tcp_con->lock_count; | ||
628 | } | 638 | } |
629 | 639 | ||
630 | return 0; | 640 | return 0; |
@@ -725,20 +735,30 @@ static int tcp_relay_on_online(TCP_Connections *tcp_c, int tcp_connections_numbe | |||
725 | if (!tcp_con) | 735 | if (!tcp_con) |
726 | return -1; | 736 | return -1; |
727 | 737 | ||
728 | unsigned int i; | 738 | unsigned int i, sent = 0; |
729 | 739 | ||
730 | for (i = 0; i < tcp_c->connections_length; ++i) { | 740 | for (i = 0; i < tcp_c->connections_length; ++i) { |
731 | TCP_Connection_to *con_to = get_connection(tcp_c, i); | 741 | TCP_Connection_to *con_to = get_connection(tcp_c, i); |
732 | 742 | ||
733 | if (con_to) { | 743 | if (con_to) { |
734 | if (tcp_connection_in_conn(con_to, tcp_connections_number)) { | 744 | if (tcp_connection_in_conn(con_to, tcp_connections_number)) { |
735 | send_tcp_relay_routing_request(tcp_c, tcp_connections_number, con_to->public_key); | 745 | if (send_tcp_relay_routing_request(tcp_c, tcp_connections_number, con_to->public_key) == 0) { |
746 | ++sent; | ||
747 | } | ||
736 | } | 748 | } |
737 | } | 749 | } |
738 | } | 750 | } |
739 | 751 | ||
740 | tcp_relay_set_callbacks(tcp_c, tcp_connections_number); | 752 | tcp_relay_set_callbacks(tcp_c, tcp_connections_number); |
741 | tcp_con->status = TCP_CONN_CONNECTED; | 753 | tcp_con->status = TCP_CONN_CONNECTED; |
754 | |||
755 | /* If this connection isn't used by any connection, we don't need to wait for them to come online. */ | ||
756 | if (sent) { | ||
757 | tcp_con->connected_time = unix_time(); | ||
758 | } else { | ||
759 | tcp_con->connected_time = 0; | ||
760 | } | ||
761 | |||
742 | return 0; | 762 | return 0; |
743 | } | 763 | } |
744 | 764 | ||
@@ -810,10 +830,10 @@ int add_tcp_number_relay_connection(TCP_Connections *tcp_c, int connections_numb | |||
810 | if (add_tcp_connection_to_conn(con_to, tcp_connections_number) == -1) | 830 | if (add_tcp_connection_to_conn(con_to, tcp_connections_number) == -1) |
811 | return -1; | 831 | return -1; |
812 | 832 | ||
813 | ++tcp_con->lock_count; | ||
814 | |||
815 | if (tcp_con->status == TCP_CONN_CONNECTED) { | 833 | if (tcp_con->status == TCP_CONN_CONNECTED) { |
816 | send_tcp_relay_routing_request(tcp_c, tcp_connections_number, con_to->public_key); | 834 | if (send_tcp_relay_routing_request(tcp_c, tcp_connections_number, con_to->public_key) == 0) { |
835 | tcp_con->connected_time = unix_time(); | ||
836 | } | ||
817 | } | 837 | } |
818 | 838 | ||
819 | return 0; | 839 | return 0; |
@@ -847,7 +867,6 @@ int add_tcp_relay_connection(TCP_Connections *tcp_c, int connections_number, IP_ | |||
847 | if (!tcp_con) | 867 | if (!tcp_con) |
848 | return -1; | 868 | return -1; |
849 | 869 | ||
850 | ++tcp_con->lock_count; | ||
851 | return 0; | 870 | return 0; |
852 | } | 871 | } |
853 | } | 872 | } |
@@ -934,7 +953,8 @@ static void kill_nonused_tcp(TCP_Connections *tcp_c) | |||
934 | 953 | ||
935 | if (tcp_con) { | 954 | if (tcp_con) { |
936 | if (tcp_con->status == TCP_CONN_CONNECTED) { | 955 | if (tcp_con->status == TCP_CONN_CONNECTED) { |
937 | if (!tcp_con->lock_count && num_online >= MAX_FRIEND_TCP_CONNECTIONS) { | 956 | if (!tcp_con->lock_count && is_timeout(tcp_con->connected_time, TCP_CONNECTION_ANNOUNCE_TIMEOUT) |
957 | && num_online >= MAX_FRIEND_TCP_CONNECTIONS) { | ||
938 | kill_tcp_relay_connection(tcp_c, i); | 958 | kill_tcp_relay_connection(tcp_c, i); |
939 | continue; | 959 | continue; |
940 | } | 960 | } |
diff --git a/toxcore/TCP_connection.h b/toxcore/TCP_connection.h index 2c86304f..10c7198e 100644 --- a/toxcore/TCP_connection.h +++ b/toxcore/TCP_connection.h | |||
@@ -36,6 +36,8 @@ | |||
36 | 36 | ||
37 | #define MAX_FRIEND_TCP_CONNECTIONS 4 | 37 | #define MAX_FRIEND_TCP_CONNECTIONS 4 |
38 | 38 | ||
39 | /* Time until connection to friend gets killed (if it doesn't get locked withing that time) */ | ||
40 | #define TCP_CONNECTION_ANNOUNCE_TIMEOUT (TCP_CONNECTION_TIMEOUT) | ||
39 | 41 | ||
40 | typedef struct { | 42 | typedef struct { |
41 | uint8_t status; | 43 | uint8_t status; |
@@ -53,6 +55,7 @@ typedef struct { | |||
53 | typedef struct { | 55 | typedef struct { |
54 | uint8_t status; | 56 | uint8_t status; |
55 | TCP_Client_Connection *connection; | 57 | TCP_Client_Connection *connection; |
58 | uint64_t connected_time; | ||
56 | uint32_t lock_count; | 59 | uint32_t lock_count; |
57 | } TCP_con; | 60 | } TCP_con; |
58 | 61 | ||