summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toxcore/Messenger.c5
-rw-r--r--toxcore/net_crypto.c70
-rw-r--r--toxcore/net_crypto.h7
3 files changed, 56 insertions, 26 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index f61050bc..83aaf19b 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -1386,6 +1386,11 @@ static void do_reqchunk_filecb(Messenger *m, int32_t friendnumber)
1386 ft->requested += length; 1386 ft->requested += length;
1387 1387
1388 --free_slots; 1388 --free_slots;
1389
1390 if (max_speed_reached(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
1391 m->friendlist[friendnumber].friendcon_id))) {
1392 free_slots = 0;
1393 }
1389 } 1394 }
1390 1395
1391 if (num == 0) 1396 if (num == 0)
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index 0e700a41..50f4d8c1 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -722,7 +722,7 @@ static int handle_request_packet(Packets_Array *send_array, const uint8_t *data,
722 722
723 if (n == data[0]) { 723 if (n == data[0]) {
724 if (send_array->buffer[num]) { 724 if (send_array->buffer[num]) {
725 send_array->buffer[num]->time = 0; 725 send_array->buffer[num]->sent = 0;
726 } 726 }
727 727
728 ++data; 728 ++data;
@@ -809,22 +809,13 @@ static int send_data_packet_helper(Net_Crypto *c, int crypt_connection_id, uint3
809 return send_data_packet(c, crypt_connection_id, packet, sizeof(packet)); 809 return send_data_packet(c, crypt_connection_id, packet, sizeof(packet));
810} 810}
811 811
812/* return -1 if data could not be put in packet queue. 812static int reset_max_speed_reached(Net_Crypto *c, int crypt_connection_id)
813 * return positive packet number if data was put into the queue.
814 */
815static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length,
816 uint8_t congestion_control)
817{ 813{
818 if (length == 0 || length > MAX_CRYPTO_DATA_SIZE)
819 return -1;
820
821 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 814 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
822 815
823 if (conn == 0) 816 if (conn == 0)
824 return -1; 817 return -1;
825 818
826 uint64_t temp_time = current_time_monotonic();
827
828 /* If last packet send failed, try to send packet again. 819 /* If last packet send failed, try to send packet again.
829 If sending it fails we won't be able to send the new packet. */ 820 If sending it fails we won't be able to send the new packet. */
830 if (conn->maximum_speed_reached) { 821 if (conn->maximum_speed_reached) {
@@ -835,27 +826,50 @@ static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, cons
835 uint8_t send_failed = 0; 826 uint8_t send_failed = 0;
836 827
837 if (ret == 1) { 828 if (ret == 1) {
838 if (!dt->time) { 829 if (!dt->sent) {
839 if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, dt->data, 830 if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, dt->data,
840 dt->length) != 0) { 831 dt->length) != 0) {
841 if (congestion_control) { 832 send_failed = 1;
842 return -1;
843 } else {
844 send_failed = 1;
845 }
846 } else { 833 } else {
847 dt->time = temp_time; 834 dt->sent = 1;
848 } 835 }
849 } 836 }
850 } 837 }
851 838
852 if (!send_failed) { 839 if (!send_failed) {
853 conn->maximum_speed_reached = 0; 840 conn->maximum_speed_reached = 0;
841 } else {
842 return -1;
854 } 843 }
855 } 844 }
856 845
846 return 0;
847}
848
849/* return -1 if data could not be put in packet queue.
850 * return positive packet number if data was put into the queue.
851 */
852static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length,
853 uint8_t congestion_control)
854{
855 if (length == 0 || length > MAX_CRYPTO_DATA_SIZE)
856 return -1;
857
858 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
859
860 if (conn == 0)
861 return -1;
862
863 /* If last packet send failed, try to send packet again.
864 If sending it fails we won't be able to send the new packet. */
865 reset_max_speed_reached(c, crypt_connection_id);
866
867 if (conn->maximum_speed_reached && congestion_control) {
868 return -1;
869 }
870
857 Packet_Data dt; 871 Packet_Data dt;
858 dt.time = 0; 872 dt.sent = 0;
859 dt.length = length; 873 dt.length = length;
860 memcpy(dt.data, data, length); 874 memcpy(dt.data, data, length);
861 pthread_mutex_lock(&conn->mutex); 875 pthread_mutex_lock(&conn->mutex);
@@ -873,7 +887,7 @@ static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, cons
873 Packet_Data *dt1 = NULL; 887 Packet_Data *dt1 = NULL;
874 888
875 if (get_data_pointer(&conn->send_array, &dt1, packet_num) == 1) 889 if (get_data_pointer(&conn->send_array, &dt1, packet_num) == 1)
876 dt1->time = temp_time; 890 dt1->sent = 1;
877 } else { 891 } else {
878 conn->maximum_speed_reached = 1; 892 conn->maximum_speed_reached = 1;
879 LOGGER_ERROR("send_data_packet failed\n"); 893 LOGGER_ERROR("send_data_packet failed\n");
@@ -983,15 +997,15 @@ static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint16
983 continue; 997 continue;
984 } 998 }
985 999
986 if (dt->time != 0) { 1000 if (dt->sent) {
987 continue; 1001 continue;
988 } 1002 }
989 1003
990 dt->time = current_time_monotonic();
991
992 if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, dt->data, 1004 if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, dt->data,
993 dt->length) == 0) 1005 dt->length) == 0) {
1006 dt->sent = 1;
994 ++num_sent; 1007 ++num_sent;
1008 }
995 1009
996 if (num_sent >= max_num) 1010 if (num_sent >= max_num)
997 break; 1011 break;
@@ -1189,7 +1203,6 @@ static int handle_data_packet_helper(const Net_Crypto *c, int crypt_connection_i
1189 set_buffer_end(&conn->recv_array, num); 1203 set_buffer_end(&conn->recv_array, num);
1190 } else if (real_data[0] >= CRYPTO_RESERVED_PACKETS && real_data[0] < PACKET_ID_LOSSY_RANGE_START) { 1204 } else if (real_data[0] >= CRYPTO_RESERVED_PACKETS && real_data[0] < PACKET_ID_LOSSY_RANGE_START) {
1191 Packet_Data dt; 1205 Packet_Data dt;
1192 dt.time = current_time_monotonic();
1193 dt.length = real_length; 1206 dt.length = real_length;
1194 memcpy(dt.data, real_data, real_length); 1207 memcpy(dt.data, real_data, real_length);
1195 1208
@@ -2575,6 +2588,13 @@ static void send_crypto_packets(Net_Crypto *c)
2575 } 2588 }
2576} 2589}
2577 2590
2591/* Return 1 if max speed was reached for this connection (no more data can be physically through the pipe).
2592 * Return 0 if it wasn't reached.
2593 */
2594_Bool max_speed_reached(Net_Crypto *c, int crypt_connection_id)
2595{
2596 return reset_max_speed_reached(c, crypt_connection_id) != 0;
2597}
2578 2598
2579/* returns the number of packet slots left in the sendbuffer. 2599/* returns the number of packet slots left in the sendbuffer.
2580 * return 0 if failure. 2600 * return 0 if failure.
diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h
index 7e59475e..ca79586f 100644
--- a/toxcore/net_crypto.h
+++ b/toxcore/net_crypto.h
@@ -89,7 +89,7 @@
89#define CONGESTION_QUEUE_ARRAY_SIZE 24 89#define CONGESTION_QUEUE_ARRAY_SIZE 24
90 90
91typedef struct { 91typedef struct {
92 uint64_t time; 92 _Bool sent;
93 uint16_t length; 93 uint16_t length;
94 uint8_t data[MAX_CRYPTO_DATA_SIZE]; 94 uint8_t data[MAX_CRYPTO_DATA_SIZE];
95} Packet_Data; 95} Packet_Data;
@@ -317,6 +317,11 @@ int nc_dht_pk_callback(Net_Crypto *c, int crypt_connection_id, void (*function)(
317 */ 317 */
318uint32_t crypto_num_free_sendqueue_slots(const Net_Crypto *c, int crypt_connection_id); 318uint32_t crypto_num_free_sendqueue_slots(const Net_Crypto *c, int crypt_connection_id);
319 319
320/* Return 1 if max speed was reached for this connection (no more data can be physically through the pipe).
321 * Return 0 if it wasn't reached.
322 */
323_Bool max_speed_reached(Net_Crypto *c, int crypt_connection_id);
324
320/* Sends a lossless cryptopacket. 325/* Sends a lossless cryptopacket.
321 * 326 *
322 * return -1 if data could not be put in packet queue. 327 * return -1 if data could not be put in packet queue.