From a5141679527b10de8cc6e0aa211873706abac149 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Thu, 15 May 2014 20:57:55 -0400 Subject: [PATCH] Exposed and tested disconnect notification TCP packets. --- auto_tests/TCP_test.c | 9 +++++++++ toxcore/TCP_client.c | 15 +++++++++++++++ toxcore/TCP_client.h | 6 ++++++ 3 files changed, 30 insertions(+) diff --git a/auto_tests/TCP_test.c b/auto_tests/TCP_test.c index 29a9f518..ca5afd9a 100644 --- a/auto_tests/TCP_test.c +++ b/auto_tests/TCP_test.c @@ -446,6 +446,15 @@ START_TEST(test_client) do_TCP_connection(conn); do_TCP_connection(conn2); ck_assert_msg(data_callback_good == 1, "data callback not called"); + status_callback_good = 0; + send_disconnect_request(conn2, 0); + c_sleep(50); + do_TCP_server(tcp_s); + c_sleep(50); + do_TCP_connection(conn); + do_TCP_connection(conn2); + ck_assert_msg(status_callback_good == 1, "status callback not called"); + ck_assert_msg(status_callback_status == 1, "wrong status"); } END_TEST diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index 4652b4a6..209c1775 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c @@ -300,6 +300,20 @@ static int send_ping_response(TCP_Client_Connection *con, uint64_t ping_id) return write_packet_TCP_secure_connection(con, packet, sizeof(packet)); } +/* return 1 on success. + * return 0 if could not send packet. + * return -1 on failure (connection must be killed). + */ +int send_disconnect_request(TCP_Client_Connection *con, uint8_t con_id) +{ + if (con_id >= NUM_CLIENT_CONNECTIONS) + return -1; + + con->connections[con_id].status = 0; + con->connections[con_id].number = 0; + return send_disconnect_notification(con, con_id + NUM_RESERVED_PORTS); +} + /* return 1 on success. * return 0 if could not send packet. * return -1 on failure (connection must be killed). @@ -564,6 +578,7 @@ void do_TCP_connection(TCP_Client_Connection *TCP_connection) if (sizeof(data) == len) { if (handle_handshake(TCP_connection, data) == 0) { + TCP_connection->kill_at = ~0; TCP_connection->status = TCP_CLIENT_CONFIRMED; } else { TCP_connection->kill_at = 0; diff --git a/toxcore/TCP_client.h b/toxcore/TCP_client.h index 277378b9..768ef9d2 100644 --- a/toxcore/TCP_client.h +++ b/toxcore/TCP_client.h @@ -105,6 +105,12 @@ void routing_response_handler(TCP_Client_Connection *con, int (*response_callbac void routing_status_handler(TCP_Client_Connection *con, int (*status_callback)(void *object, uint32_t number, uint8_t connection_id, uint8_t status), void *object); +/* return 1 on success. + * return 0 if could not send packet. + * return -1 on failure (connection must be killed). + */ +int send_disconnect_request(TCP_Client_Connection *con, uint8_t con_id); + /* Set the number that will be used as an argument in the callbacks related to con_id. * * When not set by this function, the number is ~0.