summaryrefslogtreecommitdiff
path: root/toxcore/TCP_client.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/TCP_client.c')
-rw-r--r--toxcore/TCP_client.c33
1 files changed, 19 insertions, 14 deletions
diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c
index 82720ae8..6ead44a5 100644
--- a/toxcore/TCP_client.c
+++ b/toxcore/TCP_client.c
@@ -198,6 +198,7 @@ void routing_status_handler(TCP_Client_Connection *con, int (*status_callback)(v
198} 198}
199 199
200static int send_ping_response(TCP_Client_Connection *con); 200static int send_ping_response(TCP_Client_Connection *con);
201static int send_ping_request(TCP_Client_Connection *con);
201 202
202/* return 1 on success. 203/* return 1 on success.
203 * return 0 if could not send packet. 204 * return 0 if could not send packet.
@@ -211,7 +212,7 @@ int send_data(TCP_Client_Connection *con, uint8_t con_id, const uint8_t *data, u
211 if (con->connections[con_id].status != 2) 212 if (con->connections[con_id].status != 2)
212 return -1; 213 return -1;
213 214
214 if (send_ping_response(con) == 0) 215 if (send_ping_response(con) == 0 || send_ping_request(con) == 0)
215 return 0; 216 return 0;
216 217
217 uint8_t packet[1 + length]; 218 uint8_t packet[1 + length];
@@ -286,12 +287,21 @@ static int send_disconnect_notification(TCP_Client_Connection *con, uint8_t id)
286 * return 0 if could not send packet. 287 * return 0 if could not send packet.
287 * return -1 on failure (connection must be killed). 288 * return -1 on failure (connection must be killed).
288 */ 289 */
289static int send_ping_request(TCP_Client_Connection *con, uint64_t ping_id) 290static int send_ping_request(TCP_Client_Connection *con)
290{ 291{
292 if (!con->ping_request_id)
293 return 1;
294
291 uint8_t packet[1 + sizeof(uint64_t)]; 295 uint8_t packet[1 + sizeof(uint64_t)];
292 packet[0] = TCP_PACKET_PING; 296 packet[0] = TCP_PACKET_PING;
293 memcpy(packet + 1, &ping_id, sizeof(uint64_t)); 297 memcpy(packet + 1, &con->ping_request_id, sizeof(uint64_t));
294 return write_packet_TCP_secure_connection(con, packet, sizeof(packet)); 298 int ret;
299
300 if ((ret = write_packet_TCP_secure_connection(con, packet, sizeof(packet))) == 1) {
301 con->ping_request_id = 0;
302 }
303
304 return ret;
295} 305}
296 306
297/* return 1 on success. 307/* return 1 on success.
@@ -539,6 +549,8 @@ static int do_confirmed_TCP(TCP_Client_Connection *conn)
539{ 549{
540 send_pending_data(conn); 550 send_pending_data(conn);
541 send_ping_response(conn); 551 send_ping_response(conn);
552 send_ping_request(conn);
553
542 uint8_t packet[MAX_PACKET_SIZE]; 554 uint8_t packet[MAX_PACKET_SIZE];
543 int len; 555 int len;
544 556
@@ -548,16 +560,9 @@ static int do_confirmed_TCP(TCP_Client_Connection *conn)
548 if (!ping_id) 560 if (!ping_id)
549 ++ping_id; 561 ++ping_id;
550 562
551 int ret = send_ping_request(conn, ping_id); 563 conn->ping_request_id = conn->ping_id = ping_id;
552 564 send_ping_request(conn);
553 if (ret == 1) { 565 conn->last_pinged = unix_time();
554 conn->last_pinged = unix_time();
555 conn->ping_id = ping_id;
556 } else {
557 if (is_timeout(conn->last_pinged, TCP_PING_FREQUENCY + TCP_PING_TIMEOUT)) {
558 conn->status = TCP_CLIENT_DISCONNECTED;
559 }
560 }
561 } 566 }
562 567
563 if (conn->ping_id && is_timeout(conn->last_pinged, TCP_PING_TIMEOUT)) { 568 if (conn->ping_id && is_timeout(conn->last_pinged, TCP_PING_TIMEOUT)) {