summaryrefslogtreecommitdiff
path: root/toxcore/TCP_connection.c
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2016-08-31 20:40:20 +0100
committeriphydf <iphydf@users.noreply.github.com>2016-09-02 11:02:56 +0100
commita9fbdaf46b23db5c598bf33d6bc5c4555b06e674 (patch)
treee7894501bd010d9904fe0069fc1b8121d2da4040 /toxcore/TCP_connection.c
parent6f42eadc54e81be50b7a817c72b0cf4d7ec5feb4 (diff)
Do not use `else` after `return`.
http://llvm.org/docs/CodingStandards.html#use-early-exits-and-continue-to-simplify-code
Diffstat (limited to 'toxcore/TCP_connection.c')
-rw-r--r--toxcore/TCP_connection.c97
1 files changed, 48 insertions, 49 deletions
diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c
index 786cb164..922bf0df 100644
--- a/toxcore/TCP_connection.c
+++ b/toxcore/TCP_connection.c
@@ -256,7 +256,9 @@ int send_packet_tcp_connection(TCP_Connections *tcp_c, int connections_number, c
256 256
257 if (ret == 1) { 257 if (ret == 1) {
258 return 0; 258 return 0;
259 } else if (!limit_reached) { 259 }
260
261 if (!limit_reached) {
260 ret = 0; 262 ret = 0;
261 263
262 /* Send oob packets to all relays tied to the connection. */ 264 /* Send oob packets to all relays tied to the connection. */
@@ -280,12 +282,12 @@ int send_packet_tcp_connection(TCP_Connections *tcp_c, int connections_number, c
280 282
281 if (ret >= 1) { 283 if (ret >= 1) {
282 return 0; 284 return 0;
283 } else {
284 return -1;
285 } 285 }
286 } else { 286
287 return -1; 287 return -1;
288 } 288 }
289
290 return -1;
289} 291}
290 292
291/* Return a random TCP connection number for use in send_tcp_onion_request. 293/* Return a random TCP connection number for use in send_tcp_onion_request.
@@ -527,7 +529,7 @@ int set_tcp_connection_to_status(TCP_Connections *tcp_c, int connections_number,
527 } 529 }
528 530
529 if (status) { 531 if (status) {
530 /* Conection is unsleeping. */ 532 /* Connection is unsleeping. */
531 if (con_to->status != TCP_CONN_SLEEPING) { 533 if (con_to->status != TCP_CONN_SLEEPING) {
532 return -1; 534 return -1;
533 } 535 }
@@ -551,32 +553,32 @@ int set_tcp_connection_to_status(TCP_Connections *tcp_c, int connections_number,
551 553
552 con_to->status = TCP_CONN_VALID; 554 con_to->status = TCP_CONN_VALID;
553 return 0; 555 return 0;
554 } else { 556 }
555 /* Conection is going to sleep. */
556 if (con_to->status != TCP_CONN_VALID) {
557 return -1;
558 }
559 557
560 unsigned int i; 558 /* Connection is going to sleep. */
559 if (con_to->status != TCP_CONN_VALID) {
560 return -1;
561 }
561 562
562 for (i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) { 563 unsigned int i;
563 if (con_to->connections[i].tcp_connection) {
564 unsigned int tcp_connections_number = con_to->connections[i].tcp_connection - 1;
565 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
566 564
567 if (!tcp_con) { 565 for (i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
568 continue; 566 if (con_to->connections[i].tcp_connection) {
569 } 567 unsigned int tcp_connections_number = con_to->connections[i].tcp_connection - 1;
568 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
570 569
571 if (con_to->connections[i].status == TCP_CONNECTIONS_STATUS_ONLINE) { 570 if (!tcp_con) {
572 ++tcp_con->sleep_count; 571 continue;
573 }
574 } 572 }
575 }
576 573
577 con_to->status = TCP_CONN_SLEEPING; 574 if (con_to->connections[i].status == TCP_CONNECTIONS_STATUS_ONLINE) {
578 return 0; 575 ++tcp_con->sleep_count;
576 }
577 }
579 } 578 }
579
580 con_to->status = TCP_CONN_SLEEPING;
581 return 0;
580} 582}
581 583
582static _Bool tcp_connection_in_conn(TCP_Connection_to *con_to, unsigned int tcp_connections_number) 584static _Bool tcp_connection_in_conn(TCP_Connection_to *con_to, unsigned int tcp_connections_number)
@@ -980,10 +982,10 @@ static int tcp_oob_callback(void *object, const uint8_t *public_key, const uint8
980 982
981 if (con_to && tcp_connection_in_conn(con_to, tcp_connections_number)) { 983 if (con_to && tcp_connection_in_conn(con_to, tcp_connections_number)) {
982 return tcp_data_callback(object, connections_number, 0, data, length, userdata); 984 return tcp_data_callback(object, connections_number, 0, data, length, userdata);
983 } else { 985 }
984 if (tcp_c->tcp_oob_callback) { 986
985 tcp_c->tcp_oob_callback(tcp_c->tcp_oob_callback_object, public_key, tcp_connections_number, data, length); 987 if (tcp_c->tcp_oob_callback) {
986 } 988 tcp_c->tcp_oob_callback(tcp_c->tcp_oob_callback_object, public_key, tcp_connections_number, data, length);
987 } 989 }
988 990
989 return 0; 991 return 0;
@@ -1086,7 +1088,6 @@ static int add_tcp_relay_instance(TCP_Connections *tcp_c, IP_Port ip_port, const
1086 1088
1087 TCP_con *tcp_con = &tcp_c->tcp_connections[tcp_connections_number]; 1089 TCP_con *tcp_con = &tcp_c->tcp_connections[tcp_connections_number];
1088 1090
1089
1090 tcp_con->connection = new_TCP_connection(ip_port, relay_pk, tcp_c->self_public_key, tcp_c->self_secret_key, 1091 tcp_con->connection = new_TCP_connection(ip_port, relay_pk, tcp_c->self_public_key, tcp_c->self_secret_key,
1091 &tcp_c->proxy_info); 1092 &tcp_c->proxy_info);
1092 1093
@@ -1174,25 +1175,25 @@ int add_tcp_relay_connection(TCP_Connections *tcp_c, int connections_number, IP_
1174 1175
1175 if (tcp_connections_number != -1) { 1176 if (tcp_connections_number != -1) {
1176 return add_tcp_number_relay_connection(tcp_c, connections_number, tcp_connections_number); 1177 return add_tcp_number_relay_connection(tcp_c, connections_number, tcp_connections_number);
1177 } else { 1178 }
1178 if (online_tcp_connection_from_conn(con_to) >= RECOMMENDED_FRIEND_TCP_CONNECTIONS) {
1179 return -1;
1180 }
1181 1179
1182 int tcp_connections_number = add_tcp_relay_instance(tcp_c, ip_port, relay_pk); 1180 if (online_tcp_connection_from_conn(con_to) >= RECOMMENDED_FRIEND_TCP_CONNECTIONS) {
1181 return -1;
1182 }
1183 1183
1184 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); 1184 tcp_connections_number = add_tcp_relay_instance(tcp_c, ip_port, relay_pk);
1185 1185
1186 if (!tcp_con) { 1186 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
1187 return -1;
1188 }
1189 1187
1190 if (add_tcp_connection_to_conn(con_to, tcp_connections_number) == -1) { 1188 if (!tcp_con) {
1191 return -1; 1189 return -1;
1192 } 1190 }
1193 1191
1194 return 0; 1192 if (add_tcp_connection_to_conn(con_to, tcp_connections_number) == -1) {
1193 return -1;
1195 } 1194 }
1195
1196 return 0;
1196} 1197}
1197 1198
1198/* return number of online tcp relays tied to the connection on success. 1199/* return number of online tcp relays tied to the connection on success.
@@ -1406,12 +1407,12 @@ static void kill_nonused_tcp(TCP_Connections *tcp_c)
1406 1407
1407 if (num_online <= RECOMMENDED_FRIEND_TCP_CONNECTIONS) { 1408 if (num_online <= RECOMMENDED_FRIEND_TCP_CONNECTIONS) {
1408 return; 1409 return;
1409 } else { 1410 }
1410 unsigned int n = num_online - RECOMMENDED_FRIEND_TCP_CONNECTIONS;
1411 1411
1412 if (n < num_kill) { 1412 unsigned int n = num_online - RECOMMENDED_FRIEND_TCP_CONNECTIONS;
1413 num_kill = n; 1413
1414 } 1414 if (n < num_kill) {
1415 num_kill = n;
1415 } 1416 }
1416 1417
1417 for (i = 0; i < num_kill; ++i) { 1418 for (i = 0; i < num_kill; ++i) {
@@ -1437,5 +1438,3 @@ void kill_tcp_connections(TCP_Connections *tcp_c)
1437 free(tcp_c->connections); 1438 free(tcp_c->connections);
1438 free(tcp_c); 1439 free(tcp_c);
1439} 1440}
1440
1441