diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/Lossless_UDP.c | 54 |
1 files changed, 42 insertions, 12 deletions
diff --git a/core/Lossless_UDP.c b/core/Lossless_UDP.c index 1f123eda..06e05fdb 100644 --- a/core/Lossless_UDP.c +++ b/core/Lossless_UDP.c | |||
@@ -40,7 +40,7 @@ | |||
40 | #define CONNEXION_TIMEOUT 5 | 40 | #define CONNEXION_TIMEOUT 5 |
41 | 41 | ||
42 | //initial amount of sync/hanshake packets to send per second. | 42 | //initial amount of sync/hanshake packets to send per second. |
43 | #define SYNC_RATE 10 | 43 | #define SYNC_RATE 2 |
44 | 44 | ||
45 | //initial send rate of data. | 45 | //initial send rate of data. |
46 | #define DATA_SYNC_RATE 30 | 46 | #define DATA_SYNC_RATE 30 |
@@ -66,7 +66,8 @@ typedef struct | |||
66 | uint16_t data_rate;//current data packet send rate packets per second. | 66 | uint16_t data_rate;//current data packet send rate packets per second. |
67 | uint64_t last_SYNC; //time at which our last SYNC packet was sent. | 67 | uint64_t last_SYNC; //time at which our last SYNC packet was sent. |
68 | uint64_t last_sent; //time at which our last data or handshake packet was sent. | 68 | uint64_t last_sent; //time at which our last data or handshake packet was sent. |
69 | uint64_t last_recv; //time at which we last received something from the other | 69 | uint64_t last_recvSYNC; //time at which we last received a SYNC packet from the other |
70 | uint64_t last_recvdata; //time at which we last received a DATA packet from the other | ||
70 | uint64_t killat; //time at which to kill the connection | 71 | uint64_t killat; //time at which to kill the connection |
71 | Data sendbuffer[MAX_QUEUE_NUM];//packet send buffer. | 72 | Data sendbuffer[MAX_QUEUE_NUM];//packet send buffer. |
72 | Data recvbuffer[MAX_QUEUE_NUM];//packet receive buffer. | 73 | Data recvbuffer[MAX_QUEUE_NUM];//packet receive buffer. |
@@ -168,7 +169,7 @@ int new_connection(IP_Port ip_port) | |||
168 | connections[i].successful_sent = connections[i].handshake_id1; | 169 | connections[i].successful_sent = connections[i].handshake_id1; |
169 | connections[i].SYNC_rate = SYNC_RATE; | 170 | connections[i].SYNC_rate = SYNC_RATE; |
170 | connections[i].data_rate = DATA_SYNC_RATE; | 171 | connections[i].data_rate = DATA_SYNC_RATE; |
171 | connections[i].last_recv = current_time(); | 172 | connections[i].last_recvSYNC = current_time(); |
172 | connections[i].killat = ~0; | 173 | connections[i].killat = ~0; |
173 | connections[i].send_counter = 0; | 174 | connections[i].send_counter = 0; |
174 | return i; | 175 | return i; |
@@ -197,7 +198,7 @@ int new_inconnection(IP_Port ip_port) | |||
197 | connections[i].inbound = 2; | 198 | connections[i].inbound = 2; |
198 | connections[i].SYNC_rate = SYNC_RATE; | 199 | connections[i].SYNC_rate = SYNC_RATE; |
199 | connections[i].data_rate = DATA_SYNC_RATE; | 200 | connections[i].data_rate = DATA_SYNC_RATE; |
200 | connections[i].last_recv = current_time(); | 201 | connections[i].last_recvSYNC = current_time(); |
201 | //if this connection isn't handled within 5 seconds, kill it | 202 | //if this connection isn't handled within 5 seconds, kill it |
202 | connections[i].killat = current_time() + 1000000UL*CONNEXION_TIMEOUT; | 203 | connections[i].killat = current_time() + 1000000UL*CONNEXION_TIMEOUT; |
203 | connections[i].send_counter = 127; | 204 | connections[i].send_counter = 127; |
@@ -565,7 +566,7 @@ int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, ui | |||
565 | connections[connection_id].orecv_packetnum = recv_packetnum; | 566 | connections[connection_id].orecv_packetnum = recv_packetnum; |
566 | connections[connection_id].osent_packetnum = sent_packetnum; | 567 | connections[connection_id].osent_packetnum = sent_packetnum; |
567 | connections[connection_id].successful_sent = recv_packetnum; | 568 | connections[connection_id].successful_sent = recv_packetnum; |
568 | connections[connection_id].last_recv = current_time(); | 569 | connections[connection_id].last_recvSYNC = current_time(); |
569 | connections[connection_id].recv_counter = counter; | 570 | connections[connection_id].recv_counter = counter; |
570 | connections[connection_id].send_counter++; | 571 | connections[connection_id].send_counter++; |
571 | for(i = 0; i < number; i++) | 572 | for(i = 0; i < number; i++) |
@@ -635,6 +636,7 @@ int add_recv(int connection_id, uint32_t data_num, uint8_t * data, uint16_t size | |||
635 | { | 636 | { |
636 | memcpy(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].data, data, size); | 637 | memcpy(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].data, data, size); |
637 | connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size = size; | 638 | connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size = size; |
639 | connections[connection_id].last_recvdata = current_time(); | ||
638 | if(sent_packet < BUFFER_PACKET_NUM) | 640 | if(sent_packet < BUFFER_PACKET_NUM) |
639 | { | 641 | { |
640 | connections[connection_id].osent_packetnum = data_num; | 642 | connections[connection_id].osent_packetnum = data_num; |
@@ -722,7 +724,7 @@ void doNew() | |||
722 | 724 | ||
723 | } | 725 | } |
724 | //kill all timed out connections | 726 | //kill all timed out connections |
725 | if( connections[i].status > 0 && (connections[i].last_recv + CONNEXION_TIMEOUT * 1000000UL) < temp_time && | 727 | if( connections[i].status > 0 && (connections[i].last_recvSYNC + CONNEXION_TIMEOUT * 1000000UL) < temp_time && |
726 | connections[i].status != 4) | 728 | connections[i].status != 4) |
727 | { | 729 | { |
728 | //kill_connection(i); | 730 | //kill_connection(i); |
@@ -755,15 +757,19 @@ void doSYNC() | |||
755 | void doData() | 757 | void doData() |
756 | { | 758 | { |
757 | uint32_t i; | 759 | uint32_t i; |
760 | uint64_t j; | ||
758 | uint64_t temp_time = current_time(); | 761 | uint64_t temp_time = current_time(); |
759 | for(i = 0; i < MAX_CONNECTIONS; i++) | 762 | for(i = 0; i < MAX_CONNECTIONS; i++) |
760 | { | 763 | { |
761 | if(connections[i].status == 3) | 764 | if(connections[i].status == 3 && sendqueue(i) != 0) |
762 | { | 765 | { |
763 | if((connections[i].last_sent + (1000000UL/connections[i].data_rate)) <= temp_time) | 766 | if((connections[i].last_sent + (1000000UL/connections[i].data_rate)) <= temp_time) |
764 | { | 767 | { |
765 | send_DATA(i); | 768 | for(j = connections[i].last_sent; j < temp_time; j += (1000000UL/connections[i].data_rate)) |
766 | connections[i].last_sent = temp_time; | 769 | { |
770 | send_DATA(i); | ||
771 | } | ||
772 | connections[i].last_sent = temp_time; | ||
767 | } | 773 | } |
768 | } | 774 | } |
769 | } | 775 | } |
@@ -771,12 +777,36 @@ void doData() | |||
771 | 777 | ||
772 | //TODO: flow control. | 778 | //TODO: flow control. |
773 | //automatically adjusts send rates of packets for optimal transmission. | 779 | //automatically adjusts send rates of packets for optimal transmission. |
780 | |||
781 | #define MAX_SYNC_RATE 10 | ||
782 | |||
774 | void adjustRates() | 783 | void adjustRates() |
775 | { | 784 | { |
776 | //if() | 785 | uint32_t i; |
777 | 786 | uint64_t temp_time = current_time(); | |
778 | } | 787 | for(i = 0; i < MAX_CONNECTIONS; i++) |
788 | { | ||
789 | if(connections[i].status == 3) | ||
790 | { | ||
791 | if(sendqueue(i) != 0) | ||
792 | { | ||
793 | |||
794 | connections[i].data_rate = (BUFFER_PACKET_NUM - connections[i].num_req_paquets) * MAX_SYNC_RATE; | ||
795 | |||
796 | connections[i].SYNC_rate = MAX_SYNC_RATE; | ||
779 | 797 | ||
798 | } | ||
799 | else if(connections[i].last_recvdata + 1000000UL > temp_time) | ||
800 | { | ||
801 | connections[i].SYNC_rate = MAX_SYNC_RATE; | ||
802 | } | ||
803 | else | ||
804 | { | ||
805 | connections[i].SYNC_rate = SYNC_RATE; | ||
806 | } | ||
807 | } | ||
808 | } | ||
809 | } | ||
780 | //Call this function a couple times per second | 810 | //Call this function a couple times per second |
781 | //It's the main loop. | 811 | //It's the main loop. |
782 | void doLossless_UDP() | 812 | void doLossless_UDP() |