mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
TCP should be a bit more solid.
When a TCP ping request is recieved, try to send the response until success instead of just dropping it if sending the response fails on the first try.
This commit is contained in:
parent
bd4c142e38
commit
422ff3b77b
|
@ -197,6 +197,8 @@ void routing_status_handler(TCP_Client_Connection *con, int (*status_callback)(v
|
||||||
con->status_callback_object = object;
|
con->status_callback_object = object;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int send_ping_response(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.
|
||||||
* return -1 on failure.
|
* return -1 on failure.
|
||||||
|
@ -209,6 +211,9 @@ 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)
|
||||||
|
return 0;
|
||||||
|
|
||||||
uint8_t packet[1 + length];
|
uint8_t packet[1 + length];
|
||||||
packet[0] = con_id + NUM_RESERVED_PORTS;
|
packet[0] = con_id + NUM_RESERVED_PORTS;
|
||||||
memcpy(packet + 1, data, length);
|
memcpy(packet + 1, data, length);
|
||||||
|
@ -293,12 +298,21 @@ static int send_ping_request(TCP_Client_Connection *con, uint64_t ping_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_response(TCP_Client_Connection *con, uint64_t ping_id)
|
static int send_ping_response(TCP_Client_Connection *con)
|
||||||
{
|
{
|
||||||
|
if (!con->ping_response_id)
|
||||||
|
return 1;
|
||||||
|
|
||||||
uint8_t packet[1 + sizeof(uint64_t)];
|
uint8_t packet[1 + sizeof(uint64_t)];
|
||||||
packet[0] = TCP_PACKET_PONG;
|
packet[0] = TCP_PACKET_PONG;
|
||||||
memcpy(packet + 1, &ping_id, sizeof(uint64_t));
|
memcpy(packet + 1, &con->ping_response_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_response_id = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* return 1 on success.
|
/* return 1 on success.
|
||||||
|
@ -468,7 +482,8 @@ static int handle_TCP_packet(TCP_Client_Connection *conn, const uint8_t *data, u
|
||||||
|
|
||||||
uint64_t ping_id;
|
uint64_t ping_id;
|
||||||
memcpy(&ping_id, data + 1, sizeof(uint64_t));
|
memcpy(&ping_id, data + 1, sizeof(uint64_t));
|
||||||
send_ping_response(conn, ping_id);
|
conn->ping_response_id = ping_id;
|
||||||
|
send_ping_response(conn);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -523,6 +538,7 @@ static int handle_TCP_packet(TCP_Client_Connection *conn, const uint8_t *data, u
|
||||||
static int do_confirmed_TCP(TCP_Client_Connection *conn)
|
static int do_confirmed_TCP(TCP_Client_Connection *conn)
|
||||||
{
|
{
|
||||||
send_pending_data(conn);
|
send_pending_data(conn);
|
||||||
|
send_ping_response(conn);
|
||||||
uint8_t packet[MAX_PACKET_SIZE];
|
uint8_t packet[MAX_PACKET_SIZE];
|
||||||
int len;
|
int len;
|
||||||
|
|
||||||
|
|
|
@ -57,6 +57,7 @@ typedef struct {
|
||||||
uint64_t last_pinged;
|
uint64_t last_pinged;
|
||||||
uint64_t ping_id;
|
uint64_t ping_id;
|
||||||
|
|
||||||
|
uint64_t ping_response_id;
|
||||||
void *net_crypto_pointer;
|
void *net_crypto_pointer;
|
||||||
uint32_t net_crypto_location;
|
uint32_t net_crypto_location;
|
||||||
struct {
|
struct {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user