Made TCP a bit more solid.

TCP ping requests in TCP_client should have a higher success rate.
This commit is contained in:
irungentoo 2014-07-10 18:15:55 -04:00
parent e334188353
commit 1d6c393473
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98
2 changed files with 21 additions and 14 deletions

View File

@ -198,6 +198,7 @@ void routing_status_handler(TCP_Client_Connection *con, int (*status_callback)(v
} }
static int send_ping_response(TCP_Client_Connection *con); static int send_ping_response(TCP_Client_Connection *con);
static int send_ping_request(TCP_Client_Connection *con);
/* return 1 on success. /* return 1 on success.
* return 0 if could not send packet. * 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
if (con->connections[con_id].status != 2) if (con->connections[con_id].status != 2)
return -1; return -1;
if (send_ping_response(con) == 0) if (send_ping_response(con) == 0 || send_ping_request(con) == 0)
return 0; return 0;
uint8_t packet[1 + length]; uint8_t packet[1 + length];
@ -286,12 +287,21 @@ static int send_disconnect_notification(TCP_Client_Connection *con, uint8_t id)
* return 0 if could not send packet. * return 0 if could not send packet.
* return -1 on failure (connection must be killed). * return -1 on failure (connection must be killed).
*/ */
static int send_ping_request(TCP_Client_Connection *con, uint64_t ping_id) static int send_ping_request(TCP_Client_Connection *con)
{ {
if (!con->ping_request_id)
return 1;
uint8_t packet[1 + sizeof(uint64_t)]; uint8_t packet[1 + sizeof(uint64_t)];
packet[0] = TCP_PACKET_PING; packet[0] = TCP_PACKET_PING;
memcpy(packet + 1, &ping_id, sizeof(uint64_t)); memcpy(packet + 1, &con->ping_request_id, sizeof(uint64_t));
return write_packet_TCP_secure_connection(con, packet, sizeof(packet)); int ret;
if ((ret = write_packet_TCP_secure_connection(con, packet, sizeof(packet))) == 1) {
con->ping_request_id = 0;
}
return ret;
} }
/* return 1 on success. /* return 1 on success.
@ -539,6 +549,8 @@ static int do_confirmed_TCP(TCP_Client_Connection *conn)
{ {
send_pending_data(conn); send_pending_data(conn);
send_ping_response(conn); send_ping_response(conn);
send_ping_request(conn);
uint8_t packet[MAX_PACKET_SIZE]; uint8_t packet[MAX_PACKET_SIZE];
int len; int len;
@ -548,16 +560,9 @@ static int do_confirmed_TCP(TCP_Client_Connection *conn)
if (!ping_id) if (!ping_id)
++ping_id; ++ping_id;
int ret = send_ping_request(conn, ping_id); conn->ping_request_id = conn->ping_id = ping_id;
send_ping_request(conn);
if (ret == 1) { conn->last_pinged = unix_time();
conn->last_pinged = unix_time();
conn->ping_id = ping_id;
} else {
if (is_timeout(conn->last_pinged, TCP_PING_FREQUENCY + TCP_PING_TIMEOUT)) {
conn->status = TCP_CLIENT_DISCONNECTED;
}
}
} }
if (conn->ping_id && is_timeout(conn->last_pinged, TCP_PING_TIMEOUT)) { if (conn->ping_id && is_timeout(conn->last_pinged, TCP_PING_TIMEOUT)) {

View File

@ -58,6 +58,8 @@ typedef struct {
uint64_t ping_id; uint64_t ping_id;
uint64_t ping_response_id; uint64_t ping_response_id;
uint64_t ping_request_id;
void *net_crypto_pointer; void *net_crypto_pointer;
uint32_t net_crypto_location; uint32_t net_crypto_location;
struct { struct {