summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/Lossless_UDP.c49
1 files changed, 28 insertions, 21 deletions
diff --git a/toxcore/Lossless_UDP.c b/toxcore/Lossless_UDP.c
index db0fe201..d264a150 100644
--- a/toxcore/Lossless_UDP.c
+++ b/toxcore/Lossless_UDP.c
@@ -299,11 +299,12 @@ char id_packet(Lossless_UDP *ludp, int connection_id)
299int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data) 299int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data)
300{ 300{
301 if (recvqueue(ludp, connection_id) != 0) { 301 if (recvqueue(ludp, connection_id) != 0) {
302 uint16_t index = tox_array_get(&ludp->connections, connection_id, Connection).successful_read % MAX_QUEUE_NUM; 302 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
303 uint16_t size = tox_array_get(&ludp->connections, connection_id, Connection).recvbuffer[index].size; 303 uint16_t index = connection->successful_read % MAX_QUEUE_NUM;
304 memcpy(data, tox_array_get(&ludp->connections, connection_id, Connection).recvbuffer[index].data, size); 304 uint16_t size = connection->recvbuffer[index].size;
305 ++tox_array_get(&ludp->connections, connection_id, Connection).successful_read; 305 memcpy(data, connection->recvbuffer[index].data, size);
306 tox_array_get(&ludp->connections, connection_id, Connection).recvbuffer[index].size = 0; 306 ++connection->successful_read;
307 connection->recvbuffer[index].size = 0;
307 return size; 308 return size;
308 } 309 }
309 310
@@ -319,11 +320,13 @@ int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t
319 if (length > MAX_DATA_SIZE || length == 0) 320 if (length > MAX_DATA_SIZE || length == 0)
320 return 0; 321 return 0;
321 322
323 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
324
322 if (sendqueue(ludp, connection_id) < BUFFER_PACKET_NUM) { 325 if (sendqueue(ludp, connection_id) < BUFFER_PACKET_NUM) {
323 uint32_t index = tox_array_get(&ludp->connections, connection_id, Connection).sendbuff_packetnum % MAX_QUEUE_NUM; 326 uint32_t index = connection->sendbuff_packetnum % MAX_QUEUE_NUM;
324 memcpy(tox_array_get(&ludp->connections, connection_id, Connection).sendbuffer[index].data, data, length); 327 memcpy(connection->sendbuffer[index].data, data, length);
325 tox_array_get(&ludp->connections, connection_id, Connection).sendbuffer[index].size = length; 328 connection->sendbuffer[index].size = length;
326 tox_array_get(&ludp->connections, connection_id, Connection).sendbuff_packetnum++; 329 connection->sendbuff_packetnum++;
327 return 1; 330 return 1;
328 } 331 }
329 332
@@ -341,11 +344,13 @@ uint32_t missing_packets(Lossless_UDP *ludp, int connection_id, uint32_t *reques
341 if (recvqueue(ludp, connection_id) >= (BUFFER_PACKET_NUM - 1)) 344 if (recvqueue(ludp, connection_id) >= (BUFFER_PACKET_NUM - 1))
342 return 0; 345 return 0;
343 346
344 for (i = tox_array_get(&ludp->connections, connection_id, Connection).recv_packetnum; 347 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
345 i != tox_array_get(&ludp->connections, connection_id, Connection).osent_packetnum; 348
349 for (i = connection->recv_packetnum;
350 i != connection->osent_packetnum;
346 i++) 351 i++)
347 { 352 {
348 if (tox_array_get(&ludp->connections, connection_id, Connection).recvbuffer[i % MAX_QUEUE_NUM].size == 0) { 353 if (connection->recvbuffer[i % MAX_QUEUE_NUM].size == 0) {
349 temp = htonl(i); 354 temp = htonl(i);
350 memcpy(requested + number, &temp, 4); 355 memcpy(requested + number, &temp, 4);
351 ++number; 356 ++number;
@@ -353,7 +358,7 @@ uint32_t missing_packets(Lossless_UDP *ludp, int connection_id, uint32_t *reques
353 } 358 }
354 359
355 if (number == 0) 360 if (number == 0)
356 tox_array_get(&ludp->connections, connection_id, Connection).recv_packetnum = tox_array_get(&ludp->connections, connection_id, Connection).osent_packetnum; 361 connection->recv_packetnum = connection->osent_packetnum;
357 362
358 return number; 363 return number;
359} 364}
@@ -380,13 +385,15 @@ static int send_handshake(Lossless_UDP *ludp, IP_Port ip_port, uint32_t handshak
380 385
381static int send_SYNC(Lossless_UDP *ludp, uint32_t connection_id) 386static int send_SYNC(Lossless_UDP *ludp, uint32_t connection_id)
382{ 387{
388 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
389
383 uint8_t packet[(BUFFER_PACKET_NUM * 4 + 4 + 4 + 2)]; 390 uint8_t packet[(BUFFER_PACKET_NUM * 4 + 4 + 4 + 2)];
384 uint16_t index = 0; 391 uint16_t index = 0;
385 392
386 IP_Port ip_port = tox_array_get(&ludp->connections, connection_id, Connection).ip_port; 393 IP_Port ip_port = connection->ip_port;
387 uint8_t counter = tox_array_get(&ludp->connections, connection_id, Connection).send_counter; 394 uint8_t counter = connection->send_counter;
388 uint32_t recv_packetnum = htonl(tox_array_get(&ludp->connections, connection_id, Connection).recv_packetnum); 395 uint32_t recv_packetnum = htonl(connection->recv_packetnum);
389 uint32_t sent_packetnum = htonl(tox_array_get(&ludp->connections, connection_id, Connection).sent_packetnum); 396 uint32_t sent_packetnum = htonl(connection->sent_packetnum);
390 397
391 uint32_t requested[BUFFER_PACKET_NUM]; 398 uint32_t requested[BUFFER_PACKET_NUM];
392 uint32_t number = missing_packets(ludp, connection_id, requested); 399 uint32_t number = missing_packets(ludp, connection_id, requested);
@@ -407,16 +414,16 @@ static int send_SYNC(Lossless_UDP *ludp, uint32_t connection_id)
407 414
408static int send_data_packet(Lossless_UDP *ludp, uint32_t connection_id, uint32_t packet_num) 415static int send_data_packet(Lossless_UDP *ludp, uint32_t connection_id, uint32_t packet_num)
409{ 416{
417 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
418
410 uint32_t index = packet_num % MAX_QUEUE_NUM; 419 uint32_t index = packet_num % MAX_QUEUE_NUM;
411 uint32_t temp; 420 uint32_t temp;
412 uint8_t packet[1 + 4 + MAX_DATA_SIZE]; 421 uint8_t packet[1 + 4 + MAX_DATA_SIZE];
413 packet[0] = NET_PACKET_DATA; 422 packet[0] = NET_PACKET_DATA;
414 temp = htonl(packet_num); 423 temp = htonl(packet_num);
415 memcpy(packet + 1, &temp, 4); 424 memcpy(packet + 1, &temp, 4);
416 memcpy(packet + 5, tox_array_get(&ludp->connections, connection_id, Connection).sendbuffer[index].data, 425 memcpy(packet + 5, connection->sendbuffer[index].data, connection->sendbuffer[index].size);
417 tox_array_get(&ludp->connections, connection_id, Connection).sendbuffer[index].size); 426 return sendpacket(ludp->net->sock, connection->ip_port, packet, 1 + 4 + connection->sendbuffer[index].size);
418 return sendpacket(ludp->net->sock, tox_array_get(&ludp->connections, connection_id, Connection).ip_port, packet,
419 1 + 4 + tox_array_get(&ludp->connections, connection_id, Connection).sendbuffer[index].size);
420} 427}
421 428
422/* sends 1 data packet */ 429/* sends 1 data packet */