summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornotsecure <notsecure@marek.ca>2014-07-16 08:33:24 -0400
committernotsecure <notsecure@marek.ca>2014-07-16 08:33:24 -0400
commite04fff3eac1c1fe1bb663edd8d0b45a9798ace83 (patch)
tree6857af674cfac133154a2fedffef5f038d0f505d
parent96249e8b020c76e9f80da6538ea19699612479dc (diff)
applied priority queue changes to the TCP client too
-rw-r--r--toxcore/TCP_client.c107
-rw-r--r--toxcore/TCP_client.h2
2 files changed, 96 insertions, 13 deletions
diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c
index dbcae11b..4aed0ff5 100644
--- a/toxcore/TCP_client.c
+++ b/toxcore/TCP_client.c
@@ -109,7 +109,7 @@ static int handle_handshake(TCP_Client_Connection *TCP_conn, const uint8_t *data
109/* return 0 if pending data was sent completely 109/* return 0 if pending data was sent completely
110 * return -1 if it wasn't 110 * return -1 if it wasn't
111 */ 111 */
112static int send_pending_data(TCP_Client_Connection *con) 112static int send_pending_data_nonpriority(TCP_Client_Connection *con)
113{ 113{
114 if (con->last_packet_length == 0) { 114 if (con->last_packet_length == 0) {
115 return 0; 115 return 0;
@@ -127,24 +127,90 @@ static int send_pending_data(TCP_Client_Connection *con)
127 return 0; 127 return 0;
128 } 128 }
129 129
130 if (len > left) 130 con->last_packet_sent += len;
131 return -1;
132}
133
134/* return 0 if pending data was sent completely
135 * return -1 if it wasn't
136 */
137static int send_pending_data(TCP_Client_Connection *con)
138{
139 /* finish sending current non-priority packet */
140 if(send_pending_data_nonpriority(con) == -1) {
131 return -1; 141 return -1;
142 }
143
144 TCP_Priority_List *p = con->priority_queue_start;
145
146 while(p) {
147 uint16_t left = p->size - p->sent;
148 int len = send(con->sock, p->data + p->sent, left, MSG_NOSIGNAL);
149
150 if(len != left) {
151 if(len > 0) {
152 p->sent += len;
153 }
154 break;
155 }
156
157 TCP_Priority_List *pp = p;
158 p = p->next;
159 free(pp);
160 }
161
162 con->priority_queue_start = p;
163 if(!p) {
164 con->priority_queue_end = NULL;
165 return 0;
166 }
132 167
133 con->last_packet_sent += len;
134 return -1; 168 return -1;
135} 169}
136 170
171/* return 0 on failure (only if malloc fails)
172 * return 1 on success
173 */
174static _Bool add_priority(TCP_Client_Connection *con, const uint8_t *packet, uint16_t size, uint16_t sent)
175{
176 TCP_Priority_List *p = con->priority_queue_end, *new;
177 new = malloc(sizeof(TCP_Priority_List) + size);
178 if(!new) {
179 return 0;
180 }
181
182 new->next = NULL;
183 new->size = size;
184 new->sent = sent;
185 memcpy(new->data, packet, size);
186
187 if(p) {
188 p->next = new;
189 } else {
190 con->priority_queue_start = new;
191 }
192
193 con->priority_queue_end = new;
194 return 1;
195}
196
137/* return 1 on success. 197/* return 1 on success.
138 * return 0 if could not send packet. 198 * return 0 if could not send packet.
139 * return -1 on failure (connection must be killed). 199 * return -1 on failure (connection must be killed).
140 */ 200 */
141static int write_packet_TCP_secure_connection(TCP_Client_Connection *con, const uint8_t *data, uint16_t length) 201static int write_packet_TCP_secure_connection(TCP_Client_Connection *con, const uint8_t *data, uint16_t length, _Bool priority)
142{ 202{
143 if (length + crypto_box_MACBYTES > MAX_PACKET_SIZE) 203 if (length + crypto_box_MACBYTES > MAX_PACKET_SIZE)
144 return -1; 204 return -1;
145 205
146 if (send_pending_data(con) == -1) 206 _Bool sendpriority = 1;
147 return 0; 207 if (send_pending_data(con) == -1) {
208 if (priority) {
209 sendpriority = 0;
210 } else {
211 return 0;
212 }
213 }
148 214
149 uint8_t packet[sizeof(uint16_t) + length + crypto_box_MACBYTES]; 215 uint8_t packet[sizeof(uint16_t) + length + crypto_box_MACBYTES];
150 216
@@ -155,6 +221,21 @@ static int write_packet_TCP_secure_connection(TCP_Client_Connection *con, const
155 if ((unsigned int)len != (sizeof(packet) - sizeof(uint16_t))) 221 if ((unsigned int)len != (sizeof(packet) - sizeof(uint16_t)))
156 return -1; 222 return -1;
157 223
224 if (priority) {
225 len = sendpriority ? send(con->sock, packet, sizeof(packet), MSG_NOSIGNAL) : 0;
226 if(len <= 0) {
227 len = 0;
228 } else {
229 increment_nonce(con->sent_nonce);
230 }
231
232 if(len == sizeof(packet)) {
233 return 1;
234 }
235
236 return add_priority(con, packet, sizeof(packet), len);
237 }
238
158 len = send(con->sock, packet, sizeof(packet), MSG_NOSIGNAL); 239 len = send(con->sock, packet, sizeof(packet), MSG_NOSIGNAL);
159 240
160 if (len <= 0) 241 if (len <= 0)
@@ -180,7 +261,7 @@ int send_routing_request(TCP_Client_Connection *con, uint8_t *public_key)
180 uint8_t packet[1 + crypto_box_PUBLICKEYBYTES]; 261 uint8_t packet[1 + crypto_box_PUBLICKEYBYTES];
181 packet[0] = TCP_PACKET_ROUTING_REQUEST; 262 packet[0] = TCP_PACKET_ROUTING_REQUEST;
182 memcpy(packet + 1, public_key, crypto_box_PUBLICKEYBYTES); 263 memcpy(packet + 1, public_key, crypto_box_PUBLICKEYBYTES);
183 return write_packet_TCP_secure_connection(con, packet, sizeof(packet)); 264 return write_packet_TCP_secure_connection(con, packet, sizeof(packet), 1);
184} 265}
185 266
186void routing_response_handler(TCP_Client_Connection *con, int (*response_callback)(void *object, uint8_t connection_id, 267void routing_response_handler(TCP_Client_Connection *con, int (*response_callback)(void *object, uint8_t connection_id,
@@ -218,7 +299,7 @@ int send_data(TCP_Client_Connection *con, uint8_t con_id, const uint8_t *data, u
218 uint8_t packet[1 + length]; 299 uint8_t packet[1 + length];
219 packet[0] = con_id + NUM_RESERVED_PORTS; 300 packet[0] = con_id + NUM_RESERVED_PORTS;
220 memcpy(packet + 1, data, length); 301 memcpy(packet + 1, data, length);
221 return write_packet_TCP_secure_connection(con, packet, sizeof(packet)); 302 return write_packet_TCP_secure_connection(con, packet, sizeof(packet), 0);
222} 303}
223 304
224/* return 1 on success. 305/* return 1 on success.
@@ -234,7 +315,7 @@ int send_oob_packet(TCP_Client_Connection *con, const uint8_t *public_key, const
234 packet[0] = TCP_PACKET_OOB_SEND; 315 packet[0] = TCP_PACKET_OOB_SEND;
235 memcpy(packet + 1, public_key, crypto_box_PUBLICKEYBYTES); 316 memcpy(packet + 1, public_key, crypto_box_PUBLICKEYBYTES);
236 memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES, data, length); 317 memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES, data, length);
237 return write_packet_TCP_secure_connection(con, packet, sizeof(packet)); 318 return write_packet_TCP_secure_connection(con, packet, sizeof(packet), 0);
238} 319}
239 320
240 321
@@ -280,7 +361,7 @@ static int send_disconnect_notification(TCP_Client_Connection *con, uint8_t id)
280 uint8_t packet[1 + 1]; 361 uint8_t packet[1 + 1];
281 packet[0] = TCP_PACKET_DISCONNECT_NOTIFICATION; 362 packet[0] = TCP_PACKET_DISCONNECT_NOTIFICATION;
282 packet[1] = id; 363 packet[1] = id;
283 return write_packet_TCP_secure_connection(con, packet, sizeof(packet)); 364 return write_packet_TCP_secure_connection(con, packet, sizeof(packet), 1);
284} 365}
285 366
286/* return 1 on success. 367/* return 1 on success.
@@ -297,7 +378,7 @@ static int send_ping_request(TCP_Client_Connection *con)
297 memcpy(packet + 1, &con->ping_request_id, sizeof(uint64_t)); 378 memcpy(packet + 1, &con->ping_request_id, sizeof(uint64_t));
298 int ret; 379 int ret;
299 380
300 if ((ret = write_packet_TCP_secure_connection(con, packet, sizeof(packet))) == 1) { 381 if ((ret = write_packet_TCP_secure_connection(con, packet, sizeof(packet), 1)) == 1) {
301 con->ping_request_id = 0; 382 con->ping_request_id = 0;
302 } 383 }
303 384
@@ -318,7 +399,7 @@ static int send_ping_response(TCP_Client_Connection *con)
318 memcpy(packet + 1, &con->ping_response_id, sizeof(uint64_t)); 399 memcpy(packet + 1, &con->ping_response_id, sizeof(uint64_t));
319 int ret; 400 int ret;
320 401
321 if ((ret = write_packet_TCP_secure_connection(con, packet, sizeof(packet))) == 1) { 402 if ((ret = write_packet_TCP_secure_connection(con, packet, sizeof(packet), 1)) == 1) {
322 con->ping_response_id = 0; 403 con->ping_response_id = 0;
323 } 404 }
324 405
@@ -348,7 +429,7 @@ int send_onion_request(TCP_Client_Connection *con, const uint8_t *data, uint16_t
348 uint8_t packet[1 + length]; 429 uint8_t packet[1 + length];
349 packet[0] = TCP_PACKET_ONION_REQUEST; 430 packet[0] = TCP_PACKET_ONION_REQUEST;
350 memcpy(packet + 1, data, length); 431 memcpy(packet + 1, data, length);
351 return write_packet_TCP_secure_connection(con, packet, sizeof(packet)); 432 return write_packet_TCP_secure_connection(con, packet, sizeof(packet), 0);
352} 433}
353 434
354void onion_response_handler(TCP_Client_Connection *con, int (*onion_callback)(void *object, const uint8_t *data, 435void onion_response_handler(TCP_Client_Connection *con, int (*onion_callback)(void *object, const uint8_t *data,
diff --git a/toxcore/TCP_client.h b/toxcore/TCP_client.h
index f57f79e6..9998d20c 100644
--- a/toxcore/TCP_client.h
+++ b/toxcore/TCP_client.h
@@ -52,6 +52,8 @@ typedef struct {
52 uint16_t last_packet_length; 52 uint16_t last_packet_length;
53 uint16_t last_packet_sent; 53 uint16_t last_packet_sent;
54 54
55 TCP_Priority_List *priority_queue_start, *priority_queue_end;
56
55 uint64_t kill_at; 57 uint64_t kill_at;
56 58
57 uint64_t last_pinged; 59 uint64_t last_pinged;