summaryrefslogtreecommitdiff
path: root/toxcore/net_crypto.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/net_crypto.c')
-rw-r--r--toxcore/net_crypto.c46
1 files changed, 33 insertions, 13 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index ea04d71c..d975a356 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -777,7 +777,8 @@ static int send_data_packet_helper(Net_Crypto *c, int crypt_connection_id, uint3
777/* return -1 if data could not be put in packet queue. 777/* return -1 if data could not be put in packet queue.
778 * return positive packet number if data was put into the queue. 778 * return positive packet number if data was put into the queue.
779 */ 779 */
780static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint32_t length) 780static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint32_t length,
781 uint8_t congestion_control)
781{ 782{
782 if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) 783 if (length == 0 || length > MAX_CRYPTO_DATA_SIZE)
783 return -1; 784 return -1;
@@ -796,20 +797,30 @@ static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, cons
796 uint32_t packet_num = conn->send_array.buffer_end - 1; 797 uint32_t packet_num = conn->send_array.buffer_end - 1;
797 int ret = get_data_pointer(&conn->send_array, &dt, packet_num); 798 int ret = get_data_pointer(&conn->send_array, &dt, packet_num);
798 799
800 uint8_t send_failed = 0;
801
799 if (ret == 1) { 802 if (ret == 1) {
800 if (!dt->time) { 803 if (!dt->time) {
801 if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, dt->data, 804 if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, dt->data,
802 dt->length) != 0) { 805 dt->length) != 0) {
803 return -1; 806 if (congestion_control) {
807 return -1;
808 } else {
809 send_failed = 1;
810 }
811 } else {
812 dt->time = temp_time;
804 } 813 }
805
806 dt->time = temp_time;
807 } 814 }
808 } 815 }
816
817 if (!send_failed) {
818 conn->maximum_speed_reached = 0;
819 }
809 } 820 }
810 821
811 Packet_Data dt; 822 Packet_Data dt;
812 dt.time = temp_time; 823 dt.time = 0;
813 dt.length = length; 824 dt.length = length;
814 memcpy(dt.data, data, length); 825 memcpy(dt.data, data, length);
815 int64_t packet_num = add_data_end_of_buffer(&conn->send_array, &dt); 826 int64_t packet_num = add_data_end_of_buffer(&conn->send_array, &dt);
@@ -817,12 +828,17 @@ static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, cons
817 if (packet_num == -1) 828 if (packet_num == -1)
818 return -1; 829 return -1;
819 830
831 if (!congestion_control && conn->maximum_speed_reached) {
832 return packet_num;
833 }
834
820 conn->maximum_speed_reached = 0; 835 conn->maximum_speed_reached = 0;
821 836
822 if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, data, length) != 0) { 837 if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, data, length) == 0) {
823 Packet_Data *dt1 = NULL; 838 Packet_Data *dt1 = NULL;
824 get_data_pointer(&conn->send_array, &dt1, packet_num); 839 get_data_pointer(&conn->send_array, &dt1, packet_num);
825 dt1->time = 0; 840 dt1->time = temp_time;
841 } else {
826 conn->maximum_speed_reached = 1; 842 conn->maximum_speed_reached = 1;
827 fprintf(stderr, "send_data_packet failed\n"); 843 fprintf(stderr, "send_data_packet failed\n");
828 } 844 }
@@ -2438,9 +2454,10 @@ uint32_t crypto_num_free_sendqueue_slots(const Net_Crypto *c, int crypt_connecti
2438 * return -1 if data could not be put in packet queue. 2454 * return -1 if data could not be put in packet queue.
2439 * return positive packet number if data was put into the queue. 2455 * return positive packet number if data was put into the queue.
2440 * 2456 *
2441 * The first byte of data must be in the CRYPTO_RESERVED_PACKETS to PACKET_ID_LOSSY_RANGE_START range. 2457 * congestion_control: should congestion control apply to this packet?
2442 */ 2458 */
2443int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint32_t length) 2459int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint32_t length,
2460 uint8_t congestion_control)
2444{ 2461{
2445 if (length == 0) 2462 if (length == 0)
2446 return -1; 2463 return -1;
@@ -2459,16 +2476,19 @@ int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t
2459 if (conn->status != CRYPTO_CONN_ESTABLISHED) 2476 if (conn->status != CRYPTO_CONN_ESTABLISHED)
2460 return -1; 2477 return -1;
2461 2478
2462 if (conn->packets_left == 0) 2479 if (congestion_control && conn->packets_left == 0)
2463 return -1; 2480 return -1;
2464 2481
2465 int64_t ret = send_lossless_packet(c, crypt_connection_id, data, length); 2482 int64_t ret = send_lossless_packet(c, crypt_connection_id, data, length, congestion_control);
2466 2483
2467 if (ret == -1) 2484 if (ret == -1)
2468 return -1; 2485 return -1;
2469 2486
2470 --conn->packets_left; 2487 if (congestion_control) {
2471 conn->packets_sent++; 2488 --conn->packets_left;
2489 conn->packets_sent++;
2490 }
2491
2472 return ret; 2492 return ret;
2473} 2493}
2474 2494