diff options
-rw-r--r-- | toxcore/Lossless_UDP.c | 30 | ||||
-rw-r--r-- | toxcore/Lossless_UDP.h | 5 | ||||
-rw-r--r-- | toxcore/Messenger.c | 6 | ||||
-rw-r--r-- | toxcore/net_crypto.c | 16 | ||||
-rw-r--r-- | toxcore/net_crypto.h | 5 |
5 files changed, 55 insertions, 7 deletions
diff --git a/toxcore/Lossless_UDP.c b/toxcore/Lossless_UDP.c index bdf5cd4c..0d859435 100644 --- a/toxcore/Lossless_UDP.c +++ b/toxcore/Lossless_UDP.c | |||
@@ -548,6 +548,30 @@ int discard_packet(Lossless_UDP *ludp, int connection_id) | |||
548 | } | 548 | } |
549 | 549 | ||
550 | #define MAX_SYNC_RATE 20 | 550 | #define MAX_SYNC_RATE 20 |
551 | #define MIN_SLOTS 16 | ||
552 | /* returns the number of packet slots left in the sendbuffer. | ||
553 | * return 0 if failure. | ||
554 | */ | ||
555 | uint32_t num_free_sendqueue_slots(Lossless_UDP *ludp, int connection_id) | ||
556 | { | ||
557 | if ((unsigned int)connection_id >= ludp->connections.len) | ||
558 | return 0; | ||
559 | |||
560 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); | ||
561 | uint32_t max_slots = (connection->data_rate / MAX_SYNC_RATE) * 1.5; | ||
562 | |||
563 | if (max_slots > MAX_QUEUE_NUM) | ||
564 | max_slots = MAX_QUEUE_NUM; | ||
565 | |||
566 | if (max_slots < MIN_SLOTS) | ||
567 | max_slots = MIN_SLOTS; | ||
568 | |||
569 | if (sendqueue(ludp, connection_id) > max_slots) | ||
570 | return 0; | ||
571 | |||
572 | return max_slots - sendqueue(ludp, connection_id); | ||
573 | } | ||
574 | |||
551 | 575 | ||
552 | /* return 0 if data could not be put in packet queue. | 576 | /* return 0 if data could not be put in packet queue. |
553 | * return 1 if data was put into the queue. | 577 | * return 1 if data was put into the queue. |
@@ -565,9 +589,10 @@ int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t | |||
565 | if (length > MAX_DATA_SIZE || length == 0 || sendqueue(ludp, connection_id) >= MAX_QUEUE_NUM) | 589 | if (length > MAX_DATA_SIZE || length == 0 || sendqueue(ludp, connection_id) >= MAX_QUEUE_NUM) |
566 | return 0; | 590 | return 0; |
567 | 591 | ||
592 | if (num_free_sendqueue_slots(ludp, connection_id) == 0) | ||
593 | return 0; | ||
594 | |||
568 | if (sendqueue(ludp, connection_id) >= connection->sendbuffer_length && connection->sendbuffer_length != 0) { | 595 | if (sendqueue(ludp, connection_id) >= connection->sendbuffer_length && connection->sendbuffer_length != 0) { |
569 | if (sendqueue(ludp, connection_id) > connection->data_rate/MAX_SYNC_RATE) | ||
570 | return 0; | ||
571 | uint32_t newlen = connection->sendbuffer_length = resize_queue(&connection->sendbuffer, connection->sendbuffer_length, | 596 | uint32_t newlen = connection->sendbuffer_length = resize_queue(&connection->sendbuffer, connection->sendbuffer_length, |
572 | connection->sendbuffer_length * 2, connection->successful_sent, connection->sendbuff_packetnum); | 597 | connection->sendbuffer_length * 2, connection->successful_sent, connection->sendbuff_packetnum); |
573 | 598 | ||
@@ -578,7 +603,6 @@ int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t | |||
578 | return write_packet(ludp, connection_id, data, length); | 603 | return write_packet(ludp, connection_id, data, length); |
579 | } | 604 | } |
580 | 605 | ||
581 | |||
582 | uint32_t index = connection->sendbuff_packetnum % connection->sendbuffer_length; | 606 | uint32_t index = connection->sendbuff_packetnum % connection->sendbuffer_length; |
583 | memcpy(connection->sendbuffer[index].data, data, length); | 607 | memcpy(connection->sendbuffer[index].data, data, length); |
584 | connection->sendbuffer[index].size = length; | 608 | connection->sendbuffer[index].size = length; |
diff --git a/toxcore/Lossless_UDP.h b/toxcore/Lossless_UDP.h index 8a1c9c53..3d782be7 100644 --- a/toxcore/Lossless_UDP.h +++ b/toxcore/Lossless_UDP.h | |||
@@ -217,6 +217,11 @@ int read_packet_silent(Lossless_UDP *ludp, int connection_id, uint8_t *data); | |||
217 | */ | 217 | */ |
218 | int discard_packet(Lossless_UDP *ludp, int connection_id); | 218 | int discard_packet(Lossless_UDP *ludp, int connection_id); |
219 | 219 | ||
220 | /* returns the number of packet slots left in the sendbuffer. | ||
221 | * return 0 if failure. | ||
222 | */ | ||
223 | uint32_t num_free_sendqueue_slots(Lossless_UDP *ludp, int connection_id); | ||
224 | |||
220 | /* return 0 if data could not be put in packet queue. | 225 | /* return 0 if data could not be put in packet queue. |
221 | * return 1 if data was put into the queue. | 226 | * return 1 if data was put into the queue. |
222 | */ | 227 | */ |
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 60fcc0ba..5ed94bb7 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c | |||
@@ -1091,7 +1091,7 @@ int file_control(Messenger *m, int friendnumber, uint8_t send_receive, uint8_t f | |||
1091 | } | 1091 | } |
1092 | } | 1092 | } |
1093 | 1093 | ||
1094 | 1094 | #define MIN_SLOTS_FREE 4 | |
1095 | /* Send file data. | 1095 | /* Send file data. |
1096 | * | 1096 | * |
1097 | * return 1 on success | 1097 | * return 1 on success |
@@ -1108,6 +1108,10 @@ int file_data(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t *data, | |||
1108 | if (m->friendlist[friendnumber].file_sending[filenumber].status != FILESTATUS_TRANSFERRING) | 1108 | if (m->friendlist[friendnumber].file_sending[filenumber].status != FILESTATUS_TRANSFERRING) |
1109 | return 0; | 1109 | return 0; |
1110 | 1110 | ||
1111 | /* Prevent file sending from filling up the entire buffer preventing messages from being sent. */ | ||
1112 | if (crypto_num_free_sendqueue_slots(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id) < MIN_SLOTS_FREE) | ||
1113 | return 0; | ||
1114 | |||
1111 | uint8_t packet[MAX_DATA_SIZE]; | 1115 | uint8_t packet[MAX_DATA_SIZE]; |
1112 | packet[0] = filenumber; | 1116 | packet[0] = filenumber; |
1113 | memcpy(packet + 1, data, length); | 1117 | memcpy(packet + 1, data, length); |
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 9a840d11..14398c53 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c | |||
@@ -198,6 +198,17 @@ int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data) | |||
198 | return -1; | 198 | return -1; |
199 | } | 199 | } |
200 | 200 | ||
201 | /* returns the number of packet slots left in the sendbuffer. | ||
202 | * return 0 if failure. | ||
203 | */ | ||
204 | uint32_t crypto_num_free_sendqueue_slots(Net_Crypto *c, int crypt_connection_id) | ||
205 | { | ||
206 | if (crypt_connection_id_not_valid(c, crypt_connection_id)) | ||
207 | return 0; | ||
208 | |||
209 | return num_free_sendqueue_slots(c->lossless_udp, c->crypto_connections[crypt_connection_id].number); | ||
210 | } | ||
211 | |||
201 | /* return 0 if data could not be put in packet queue. | 212 | /* return 0 if data could not be put in packet queue. |
202 | * return 1 if data was put into the queue. | 213 | * return 1 if data was put into the queue. |
203 | */ | 214 | */ |
@@ -579,9 +590,6 @@ int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, | |||
579 | * } | 590 | * } |
580 | */ | 591 | */ |
581 | 592 | ||
582 | /* Connection is accepted. */ | ||
583 | confirm_connection(c->lossless_udp, connection_id); | ||
584 | |||
585 | if (realloc_cryptoconnection(c, c->crypto_connections_length + 1) == -1 | 593 | if (realloc_cryptoconnection(c, c->crypto_connections_length + 1) == -1 |
586 | || c->crypto_connections == NULL) | 594 | || c->crypto_connections == NULL) |
587 | return -1; | 595 | return -1; |
@@ -721,6 +729,8 @@ static void receive_crypto(Net_Crypto *c) | |||
721 | c->crypto_connections[i].shared_key); | 729 | c->crypto_connections[i].shared_key); |
722 | c->crypto_connections[i].status = CONN_ESTABLISHED; | 730 | c->crypto_connections[i].status = CONN_ESTABLISHED; |
723 | c->crypto_connections[i].timeout = ~0; | 731 | c->crypto_connections[i].timeout = ~0; |
732 | /* Connection is accepted. */ | ||
733 | confirm_connection(c->lossless_udp, c->crypto_connections[i].number); | ||
724 | } else { | 734 | } else { |
725 | /* This should not happen, timeout the connection if it does. */ | 735 | /* This should not happen, timeout the connection if it does. */ |
726 | c->crypto_connections[i].status = CONN_TIMED_OUT; | 736 | c->crypto_connections[i].status = CONN_TIMED_OUT; |
diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index 5d21e4b6..0c1bffe6 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h | |||
@@ -121,6 +121,11 @@ void new_nonce(uint8_t *nonce); | |||
121 | */ | 121 | */ |
122 | int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data); | 122 | int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data); |
123 | 123 | ||
124 | /* returns the number of packet slots left in the sendbuffer. | ||
125 | * return 0 if failure. | ||
126 | */ | ||
127 | uint32_t crypto_num_free_sendqueue_slots(Net_Crypto *c, int crypt_connection_id); | ||
128 | |||
124 | /* return 0 if data could not be put in packet queue. | 129 | /* return 0 if data could not be put in packet queue. |
125 | * return 1 if data was put into the queue. | 130 | * return 1 if data was put into the queue. |
126 | */ | 131 | */ |