diff options
author | irungentoo <irungentoo@gmail.com> | 2015-04-16 19:19:21 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2015-04-16 19:19:21 -0400 |
commit | de8956267c06444443e43980a53e7ded49ee4d3a (patch) | |
tree | ad5c86ff67eb71c9ba92ede1eb7aa7d0ab391147 | |
parent | 7d466fda2cb7108fb203e4e1b94f88187597a014 (diff) |
Some fixes and improvements.
Fixed that the first TCP relays in the list would never be disconnected
even if they were useless.
-rw-r--r-- | toxcore/TCP_connection.c | 53 | ||||
-rw-r--r-- | toxcore/TCP_connection.h | 4 |
2 files changed, 49 insertions, 8 deletions
diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c index 331d772b..49a213a7 100644 --- a/toxcore/TCP_connection.c +++ b/toxcore/TCP_connection.c | |||
@@ -515,6 +515,24 @@ static int rm_tcp_connection_from_conn(TCP_Connection_to *con_to, int tcp_connec | |||
515 | return -1; | 515 | return -1; |
516 | } | 516 | } |
517 | 517 | ||
518 | /* return number of online connections on success. | ||
519 | * return -1 on failure. | ||
520 | */ | ||
521 | static unsigned int online_tcp_connection_from_conn(TCP_Connection_to *con_to) | ||
522 | { | ||
523 | unsigned int i, count = 0; | ||
524 | |||
525 | for (i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) { | ||
526 | if (con_to->connections[i].tcp_connection) { | ||
527 | if (con_to->connections[i].status == TCP_CONNECTIONS_STATUS_ONLINE) { | ||
528 | ++count; | ||
529 | } | ||
530 | } | ||
531 | } | ||
532 | |||
533 | return count; | ||
534 | } | ||
535 | |||
518 | /* return index on success. | 536 | /* return index on success. |
519 | * return -1 on failure. | 537 | * return -1 on failure. |
520 | */ | 538 | */ |
@@ -856,17 +874,21 @@ int add_tcp_relay_connection(TCP_Connections *tcp_c, int connections_number, IP_ | |||
856 | if (tcp_connections_number != -1) { | 874 | if (tcp_connections_number != -1) { |
857 | return add_tcp_number_relay_connection(tcp_c, connections_number, tcp_connections_number); | 875 | return add_tcp_number_relay_connection(tcp_c, connections_number, tcp_connections_number); |
858 | } else { | 876 | } else { |
859 | int tcp_connections_number = add_tcp_relay(tcp_c, ip_port, relay_pk); | 877 | if (online_tcp_connection_from_conn(con_to) >= RECOMMENDED_FRIEND_TCP_CONNECTIONS) { |
860 | |||
861 | if (add_tcp_connection_to_conn(con_to, tcp_connections_number) == -1) { | ||
862 | return -1; | 878 | return -1; |
863 | } | 879 | } |
864 | 880 | ||
881 | int tcp_connections_number = add_tcp_relay(tcp_c, ip_port, relay_pk); | ||
882 | |||
865 | TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); | 883 | TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); |
866 | 884 | ||
867 | if (!tcp_con) | 885 | if (!tcp_con) |
868 | return -1; | 886 | return -1; |
869 | 887 | ||
888 | if (add_tcp_connection_to_conn(con_to, tcp_connections_number) == -1) { | ||
889 | return -1; | ||
890 | } | ||
891 | |||
870 | return 0; | 892 | return 0; |
871 | } | 893 | } |
872 | } | 894 | } |
@@ -946,23 +968,38 @@ static void do_tcp_conns(TCP_Connections *tcp_c) | |||
946 | 968 | ||
947 | static void kill_nonused_tcp(TCP_Connections *tcp_c) | 969 | static void kill_nonused_tcp(TCP_Connections *tcp_c) |
948 | { | 970 | { |
949 | unsigned int i, num_online = 0; | 971 | if (tcp_c->tcp_connections_length == 0) |
972 | return; | ||
973 | |||
974 | unsigned int i, num_online = 0, num_kill = 0, to_kill[tcp_c->tcp_connections_length]; | ||
950 | 975 | ||
951 | for (i = 0; i < tcp_c->tcp_connections_length; ++i) { | 976 | for (i = 0; i < tcp_c->tcp_connections_length; ++i) { |
952 | TCP_con *tcp_con = get_tcp_connection(tcp_c, i); | 977 | TCP_con *tcp_con = get_tcp_connection(tcp_c, i); |
953 | 978 | ||
954 | if (tcp_con) { | 979 | if (tcp_con) { |
955 | if (tcp_con->status == TCP_CONN_CONNECTED) { | 980 | if (tcp_con->status == TCP_CONN_CONNECTED) { |
956 | if (!tcp_con->lock_count && is_timeout(tcp_con->connected_time, TCP_CONNECTION_ANNOUNCE_TIMEOUT) | 981 | if (!tcp_con->lock_count && is_timeout(tcp_con->connected_time, TCP_CONNECTION_ANNOUNCE_TIMEOUT)) { |
957 | && num_online >= MAX_FRIEND_TCP_CONNECTIONS) { | 982 | to_kill[num_kill] = i; |
958 | kill_tcp_relay_connection(tcp_c, i); | 983 | ++num_kill; |
959 | continue; | ||
960 | } | 984 | } |
961 | 985 | ||
962 | ++num_online; | 986 | ++num_online; |
963 | } | 987 | } |
964 | } | 988 | } |
965 | } | 989 | } |
990 | |||
991 | if (num_online <= RECOMMENDED_FRIEND_TCP_CONNECTIONS) { | ||
992 | return; | ||
993 | } else { | ||
994 | unsigned int n = num_online - RECOMMENDED_FRIEND_TCP_CONNECTIONS; | ||
995 | |||
996 | if (n < num_kill) | ||
997 | num_kill = n; | ||
998 | } | ||
999 | |||
1000 | for (i = 0; i < num_kill; ++i) { | ||
1001 | kill_tcp_relay_connection(tcp_c, to_kill[i]); | ||
1002 | } | ||
966 | } | 1003 | } |
967 | 1004 | ||
968 | void do_tcp_connections(TCP_Connections *tcp_c) | 1005 | void do_tcp_connections(TCP_Connections *tcp_c) |
diff --git a/toxcore/TCP_connection.h b/toxcore/TCP_connection.h index 10c7198e..98f52f8d 100644 --- a/toxcore/TCP_connection.h +++ b/toxcore/TCP_connection.h | |||
@@ -39,6 +39,10 @@ | |||
39 | /* Time until connection to friend gets killed (if it doesn't get locked withing that time) */ | 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) | 40 | #define TCP_CONNECTION_ANNOUNCE_TIMEOUT (TCP_CONNECTION_TIMEOUT) |
41 | 41 | ||
42 | /* The amount of recommended connections for each friend | ||
43 | NOTE: Must be equal or smaller than MAX_FRIEND_TCP_CONNECTIONS */ | ||
44 | #define RECOMMENDED_FRIEND_TCP_CONNECTIONS 3 | ||
45 | |||
42 | typedef struct { | 46 | typedef struct { |
43 | uint8_t status; | 47 | uint8_t status; |
44 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* The dht public key of the peer */ | 48 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* The dht public key of the peer */ |