summaryrefslogtreecommitdiff
path: root/core/Lossless_UDP.c
diff options
context:
space:
mode:
Diffstat (limited to 'core/Lossless_UDP.c')
-rw-r--r--core/Lossless_UDP.c78
1 files changed, 52 insertions, 26 deletions
diff --git a/core/Lossless_UDP.c b/core/Lossless_UDP.c
index 7a994724..1073d134 100644
--- a/core/Lossless_UDP.c
+++ b/core/Lossless_UDP.c
@@ -506,11 +506,16 @@ int send_DATA(uint32_t connection_id)
506 return 0; 506 return 0;
507} 507}
508 508
509/* END of packet sending functions */ 509/*
510 * END of packet sending functions
511 *
512 *
513 * BEGIN Packet handling functions
514 * One to handle each type of packets we receive
515 */
510 516
511/* Packet handling functions 517
512 One to handle each type of packets we receive 518/* Return 0 if handled correctly, 1 if packet is bad. */
513 return 0 if handled correctly, 1 if packet is bad. */
514int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) 519int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source)
515{ 520{
516 if (length != (1 + 4 + 4)) 521 if (length != (1 + 4 + 4))
@@ -542,8 +547,7 @@ int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source)
542 547
543} 548}
544 549
545/* returns 1 if sync packet is valid 550/* returns 1 if sync packet is valid 0 if not. */
546 0 if not. */
547int SYNC_valid(uint32_t length) 551int SYNC_valid(uint32_t length)
548{ 552{
549 if (length < 4 + 4 + 2) 553 if (length < 4 + 4 + 2)
@@ -554,19 +558,19 @@ int SYNC_valid(uint32_t length)
554 return 1; 558 return 1;
555} 559}
556 560
557/* case 1: */ 561/* case 1 in handle_SYNC: */
558int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum) 562int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum)
559{ 563{
560 if (handshake_id(source) == recv_packetnum) { 564 if (handshake_id(source) == recv_packetnum) {
561 int x = new_inconnection(source); 565 int x = new_inconnection(source);
562 if (x != -1) { 566 if (x != -1) {
563 connections[x].orecv_packetnum = recv_packetnum; 567 connections[x].orecv_packetnum = recv_packetnum;
564 connections[x].sent_packetnum = recv_packetnum; 568 connections[x].sent_packetnum = recv_packetnum;
565 connections[x].sendbuff_packetnum = recv_packetnum; 569 connections[x].sendbuff_packetnum = recv_packetnum;
566 connections[x].successful_sent = recv_packetnum; 570 connections[x].successful_sent = recv_packetnum;
567 connections[x].osent_packetnum = sent_packetnum; 571 connections[x].osent_packetnum = sent_packetnum;
568 connections[x].recv_packetnum = sent_packetnum; 572 connections[x].recv_packetnum = sent_packetnum;
569 connections[x].successful_read = sent_packetnum; 573 connections[x].successful_read = sent_packetnum;
570 574
571 return x; 575 return x;
572 } 576 }
@@ -574,7 +578,7 @@ int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnu
574 return -1; 578 return -1;
575} 579}
576 580
577/* case 2: */ 581/* case 2 in handle_SYNC: */
578int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum) 582int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum)
579{ 583{
580 if (recv_packetnum == connections[connection_id].orecv_packetnum) { 584 if (recv_packetnum == connections[connection_id].orecv_packetnum) {
@@ -587,7 +591,7 @@ int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packetnum, ui
587 } 591 }
588 return 1; 592 return 1;
589} 593}
590/* case 3: */ 594/* case 3 in handle_SYNC: */
591int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum, uint32_t * req_packets, 595int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum, uint32_t * req_packets,
592 uint16_t number) 596 uint16_t number)
593{ 597{
@@ -597,17 +601,24 @@ int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, ui
597 uint32_t comp_2 = (sent_packetnum - connections[connection_id].successful_read); */ 601 uint32_t comp_2 = (sent_packetnum - connections[connection_id].successful_read); */
598 uint32_t comp_1 = (recv_packetnum - connections[connection_id].orecv_packetnum); 602 uint32_t comp_1 = (recv_packetnum - connections[connection_id].orecv_packetnum);
599 uint32_t comp_2 = (sent_packetnum - connections[connection_id].osent_packetnum); 603 uint32_t comp_2 = (sent_packetnum - connections[connection_id].osent_packetnum);
600 if (comp_1 <= BUFFER_PACKET_NUM && comp_2 <= BUFFER_PACKET_NUM && comp_counter < 10 && comp_counter != 0) { /* packet valid */ 604
605 /* packet valid */
606 if (comp_1 <= BUFFER_PACKET_NUM &&
607 comp_2 <= BUFFER_PACKET_NUM &&
608 comp_counter < 10 && comp_counter != 0) {
609
601 connections[connection_id].orecv_packetnum = recv_packetnum; 610 connections[connection_id].orecv_packetnum = recv_packetnum;
602 connections[connection_id].osent_packetnum = sent_packetnum; 611 connections[connection_id].osent_packetnum = sent_packetnum;
603 connections[connection_id].successful_sent = recv_packetnum; 612 connections[connection_id].successful_sent = recv_packetnum;
604 connections[connection_id].last_recvSYNC = current_time(); 613 connections[connection_id].last_recvSYNC = current_time();
605 connections[connection_id].recv_counter = counter; 614 connections[connection_id].recv_counter = counter;
606 ++connections[connection_id].send_counter; 615 ++connections[connection_id].send_counter;
616
607 for (i = 0; i < number; ++i) { 617 for (i = 0; i < number; ++i) {
608 temp = ntohl(req_packets[i]); 618 temp = ntohl(req_packets[i]);
609 memcpy(connections[connection_id].req_packets + i, &temp, 4 * number); 619 memcpy(connections[connection_id].req_packets + i, &temp, 4 * number);
610 } 620 }
621
611 connections[connection_id].num_req_paquets = number; 622 connections[connection_id].num_req_paquets = number;
612 return 0; 623 return 0;
613 } 624 }
@@ -631,14 +642,20 @@ int handle_SYNC(uint8_t *packet, uint32_t length, IP_Port source)
631 recv_packetnum = ntohl(temp); 642 recv_packetnum = ntohl(temp);
632 memcpy(&temp,packet + 6, 4); 643 memcpy(&temp,packet + 6, 4);
633 sent_packetnum = ntohl(temp); 644 sent_packetnum = ntohl(temp);
645
634 if (number != 0) 646 if (number != 0)
635 memcpy(req_packets, packet + 10, 4 * number); 647 memcpy(req_packets, packet + 10, 4 * number);
648
636 if (connection == -1) 649 if (connection == -1)
637 return handle_SYNC1(source, recv_packetnum, sent_packetnum); 650 return handle_SYNC1(source, recv_packetnum, sent_packetnum);
651
638 if (connections[connection].status == 2) 652 if (connections[connection].status == 2)
639 return handle_SYNC2(connection, counter, recv_packetnum, sent_packetnum); 653 return handle_SYNC2(connection, counter,
654 recv_packetnum, sent_packetnum);
655
640 if (connections[connection].status == 3) 656 if (connections[connection].status == 3)
641 return handle_SYNC3(connection, counter, recv_packetnum, sent_packetnum, req_packets, number); 657 return handle_SYNC3(connection, counter, recv_packetnum,
658 sent_packetnum, req_packets, number);
642 return 0; 659 return 0;
643} 660}
644 661
@@ -654,13 +671,17 @@ int add_recv(int connection_id, uint32_t data_num, uint8_t *data, uint16_t size)
654 uint32_t i; 671 uint32_t i;
655 uint32_t maxnum = connections[connection_id].successful_read + BUFFER_PACKET_NUM; 672 uint32_t maxnum = connections[connection_id].successful_read + BUFFER_PACKET_NUM;
656 uint32_t sent_packet = data_num - connections[connection_id].osent_packetnum; 673 uint32_t sent_packet = data_num - connections[connection_id].osent_packetnum;
674
657 for (i = connections[connection_id].recv_packetnum; i != maxnum; ++i) { 675 for (i = connections[connection_id].recv_packetnum; i != maxnum; ++i) {
658 if (i == data_num) { 676 if (i == data_num) {
659 memcpy(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].data, data, size); 677 memcpy(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].data, data, size);
678
660 connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size = size; 679 connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size = size;
661 connections[connection_id].last_recvdata = current_time(); 680 connections[connection_id].last_recvdata = current_time();
681
662 if (sent_packet < BUFFER_PACKET_NUM) 682 if (sent_packet < BUFFER_PACKET_NUM)
663 connections[connection_id].osent_packetnum = data_num; 683 connections[connection_id].osent_packetnum = data_num;
684
664 break; 685 break;
665 } 686 }
666 } 687 }
@@ -696,7 +717,10 @@ int handle_data(uint8_t *packet, uint32_t length, IP_Port source)
696 return add_recv(connection, number, packet + 5, size); 717 return add_recv(connection, number, packet + 5, size);
697} 718}
698 719
699/* END of packet handling functions */ 720/*
721 * END of packet handling functions
722 */
723
700int LosslessUDP_handlepacket(uint8_t *packet, uint32_t length, IP_Port source) 724int LosslessUDP_handlepacket(uint8_t *packet, uint32_t length, IP_Port source)
701{ 725{
702 switch (packet[0]) { 726 switch (packet[0]) {
@@ -732,10 +756,13 @@ void doNew()
732 } 756 }
733 757
734 /* kill all timed out connections */ 758 /* kill all timed out connections */
735 if ( connections[i].status > 0 && (connections[i].last_recvSYNC + connections[i].timeout * 1000000UL) < temp_time && 759 if (connections[i].status > 0 &&
736 connections[i].status != 4) 760 (connections[i].last_recvSYNC + connections[i].timeout * 1000000UL) < temp_time &&
737 /* kill_connection(i); */ 761 connections[i].status != 4) {
738 connections[i].status = 4; 762 connections[i].status = 4;
763 /* kill_connection(i); */
764 }
765
739 if (connections[i].status > 0 && connections[i].killat < temp_time) 766 if (connections[i].status > 0 && connections[i].killat < temp_time)
740 kill_connection(i); 767 kill_connection(i);
741 } 768 }
@@ -794,8 +821,7 @@ void adjustRates()
794 } 821 }
795} 822}
796 823
797/* Call this function a couple times per second 824/* Call this function a couple times per second It's the main loop. */
798 It's the main loop. */
799void doLossless_UDP() 825void doLossless_UDP()
800{ 826{
801 doNew(); 827 doNew();