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.c65
1 files changed, 51 insertions, 14 deletions
diff --git a/core/Lossless_UDP.c b/core/Lossless_UDP.c
index bcbfb18a..b249760a 100644
--- a/core/Lossless_UDP.c
+++ b/core/Lossless_UDP.c
@@ -47,18 +47,19 @@
47 47
48typedef struct 48typedef struct
49{ 49{
50 char data[MAX_DATA_SIZE]; 50 uint8_t data[MAX_DATA_SIZE];
51 uint16_t size; 51 uint16_t size;
52}Data; 52}Data;
53 53
54typedef struct 54typedef struct
55{ 55{
56 IP_Port ip_port; 56 IP_Port ip_port;
57 char status;//0 if connection is dead, 1 if attempting handshake, 57 uint8_t status;//0 if connection is dead, 1 if attempting handshake,
58 //2 if handshake is done (we start sending SYNC packets) 58 //2 if handshake is done (we start sending SYNC packets)
59 //3 if we are sending SYNC packets and can send data 59 //3 if we are sending SYNC packets and can send data
60 //4 if the connection has timed out.
60 61
61 char inbound; //1 or 2 if connection was initiated by someone else, 0 if not. 62 uint8_t inbound; //1 or 2 if connection was initiated by someone else, 0 if not.
62 //2 if incoming_connection() has not returned it yet, 1 if it has. 63 //2 if incoming_connection() has not returned it yet, 1 if it has.
63 64
64 uint16_t SYNC_rate;//current SYNC packet send rate packets per second. 65 uint16_t SYNC_rate;//current SYNC packet send rate packets per second.
@@ -66,6 +67,7 @@ typedef struct
66 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.
67 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.
68 uint64_t last_recv; //time at which we last recieved something from the other 69 uint64_t last_recv; //time at which we last recieved something from the other
70 uint64_t killat; //time at which to kill the connection
69 Data sendbuffer[MAX_QUEUE_NUM];//packet send buffer. 71 Data sendbuffer[MAX_QUEUE_NUM];//packet send buffer.
70 Data recvbuffer[MAX_QUEUE_NUM];//packet recieve buffer. 72 Data recvbuffer[MAX_QUEUE_NUM];//packet recieve buffer.
71 uint32_t handshake_id1; 73 uint32_t handshake_id1;
@@ -160,6 +162,7 @@ int new_connection(IP_Port ip_port)
160 connections[i].SYNC_rate = SYNC_RATE; 162 connections[i].SYNC_rate = SYNC_RATE;
161 connections[i].data_rate = DATA_SYNC_RATE; 163 connections[i].data_rate = DATA_SYNC_RATE;
162 connections[i].last_recv = current_time(); 164 connections[i].last_recv = current_time();
165 connections[i].killat = ~0;
163 connections[i].send_counter = 0; 166 connections[i].send_counter = 0;
164 return i; 167 return i;
165 } 168 }
@@ -187,6 +190,7 @@ int new_inconnection(IP_Port ip_port)
187 connections[i].SYNC_rate = SYNC_RATE; 190 connections[i].SYNC_rate = SYNC_RATE;
188 connections[i].data_rate = DATA_SYNC_RATE; 191 connections[i].data_rate = DATA_SYNC_RATE;
189 connections[i].last_recv = current_time(); 192 connections[i].last_recv = current_time();
193 connections[i].killat = ~0;
190 connections[i].send_counter = 127; 194 connections[i].send_counter = 127;
191 return i; 195 return i;
192 } 196 }
@@ -225,11 +229,28 @@ int kill_connection(int connection_id)
225 return -1; 229 return -1;
226} 230}
227 231
232//kill connection in seconds seconds.
233//return -1 if it can not kill the connection.
234//return 0 if it will kill it
235int kill_connection_in(int connection_id, uint32_t seconds)
236{
237 if(connection_id >= 0 && connection_id < MAX_CONNECTIONS)
238 {
239 if(connections[connection_id].status > 0)
240 {
241 connections[connection_id].killat = current_time() + 1000000UL*seconds;
242 return 0;
243 }
244 }
245 return -1;
246}
247
228//check if connection is connected 248//check if connection is connected
229//return 0 no. 249//return 0 no.
230//return 1 if attempting handshake 250//return 1 if attempting handshake
231//return 2 if handshake is done 251//return 2 if handshake is done
232//return 3 if fully connected 252//return 3 if fully connected
253//return 4 if timed out and wating to be killed
233int is_connected(int connection_id) 254int is_connected(int connection_id)
234{ 255{
235 if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) 256 if(connection_id >= 0 && connection_id < MAX_CONNECTIONS)
@@ -262,9 +283,19 @@ uint32_t recvqueue(int connection_id)
262 return connections[connection_id].recv_packetnum - connections[connection_id].successful_read; 283 return connections[connection_id].recv_packetnum - connections[connection_id].successful_read;
263} 284}
264 285
286//returns the id of the next packet in the queue
287//return -1 if no packet in queue
288char id_packet(int connection_id)
289{
290 if(recvqueue(connection_id) != 0 && connections[connection_id].status != 0)
291 {
292 return connections[connection_id].recvbuffer[connections[connection_id].successful_read % MAX_QUEUE_NUM].data[0];
293 }
294 return -1;
295}
265//return 0 if there is no received data in the buffer. 296//return 0 if there is no received data in the buffer.
266//return length of received packet if successful 297//return length of received packet if successful
267int read_packet(int connection_id, char * data) 298int read_packet(int connection_id, uint8_t * data)
268{ 299{
269 if(recvqueue(connection_id) != 0) 300 if(recvqueue(connection_id) != 0)
270 { 301 {
@@ -280,7 +311,7 @@ int read_packet(int connection_id, char * data)
280 311
281//return 0 if data could not be put in packet queue 312//return 0 if data could not be put in packet queue
282//return 1 if data was put into the queue 313//return 1 if data was put into the queue
283int write_packet(int connection_id, char * data, uint32_t length) 314int write_packet(int connection_id, uint8_t * data, uint32_t length)
284{ 315{
285 if(length > MAX_DATA_SIZE) 316 if(length > MAX_DATA_SIZE)
286 { 317 {
@@ -338,7 +369,7 @@ uint32_t missing_packets(int connection_id, uint32_t * requested)
338 369
339int send_handshake(IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2) 370int send_handshake(IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2)
340{ 371{
341 char packet[1 + 4 + 4]; 372 uint8_t packet[1 + 4 + 4];
342 uint32_t temp; 373 uint32_t temp;
343 374
344 packet[0] = 16; 375 packet[0] = 16;
@@ -354,7 +385,7 @@ int send_handshake(IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_i
354int send_SYNC(uint32_t connection_id) 385int send_SYNC(uint32_t connection_id)
355{ 386{
356 387
357 char packet[(BUFFER_PACKET_NUM*4 + 4 + 4 + 2)]; 388 uint8_t packet[(BUFFER_PACKET_NUM*4 + 4 + 4 + 2)];
358 uint16_t index = 0; 389 uint16_t index = 0;
359 390
360 IP_Port ip_port = connections[connection_id].ip_port; 391 IP_Port ip_port = connections[connection_id].ip_port;
@@ -382,7 +413,7 @@ int send_data_packet(uint32_t connection_id, uint32_t packet_num)
382{ 413{
383 uint32_t index = packet_num % MAX_QUEUE_NUM; 414 uint32_t index = packet_num % MAX_QUEUE_NUM;
384 uint32_t temp; 415 uint32_t temp;
385 char packet[1 + 4 + MAX_DATA_SIZE]; 416 uint8_t packet[1 + 4 + MAX_DATA_SIZE];
386 packet[0] = 18; 417 packet[0] = 18;
387 temp = htonl(packet_num); 418 temp = htonl(packet_num);
388 memcpy(packet + 1, &temp, 4); 419 memcpy(packet + 1, &temp, 4);
@@ -421,7 +452,7 @@ int send_DATA(uint32_t connection_id)
421//Packet handling functions 452//Packet handling functions
422//One to handle each type of packets we recieve 453//One to handle each type of packets we recieve
423//return 0 if handled correctly, 1 if packet is bad. 454//return 0 if handled correctly, 1 if packet is bad.
424int handle_handshake(char * packet, uint32_t length, IP_Port source) 455int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source)
425{ 456{
426 if(length != (1 + 4 + 4)) 457 if(length != (1 + 4 + 4))
427 { 458 {
@@ -538,7 +569,7 @@ int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, ui
538 return 1; 569 return 1;
539} 570}
540 571
541int handle_SYNC(char * packet, uint32_t length, IP_Port source) 572int handle_SYNC(uint8_t * packet, uint32_t length, IP_Port source)
542{ 573{
543 574
544 if(!SYNC_valid(length)) 575 if(!SYNC_valid(length))
@@ -578,7 +609,7 @@ int handle_SYNC(char * packet, uint32_t length, IP_Port source)
578 609
579//add a packet to the recieved buffer and set the recv_packetnum of the connection to its proper value. 610//add a packet to the recieved buffer and set the recv_packetnum of the connection to its proper value.
580//return 1 if data was too big, 0 if not. 611//return 1 if data was too big, 0 if not.
581int add_recv(int connection_id, uint32_t data_num, char * data, uint16_t size) 612int add_recv(int connection_id, uint32_t data_num, uint8_t * data, uint16_t size)
582{ 613{
583 if(size > MAX_DATA_SIZE) 614 if(size > MAX_DATA_SIZE)
584 { 615 {
@@ -616,7 +647,7 @@ int add_recv(int connection_id, uint32_t data_num, char * data, uint16_t size)
616 return 0; 647 return 0;
617} 648}
618 649
619int handle_data(char * packet, uint32_t length, IP_Port source) 650int handle_data(uint8_t * packet, uint32_t length, IP_Port source)
620{ 651{
621 int connection = getconnection_id(source); 652 int connection = getconnection_id(source);
622 653
@@ -641,7 +672,7 @@ int handle_data(char * packet, uint32_t length, IP_Port source)
641//END of packet handling functions 672//END of packet handling functions
642 673
643 674
644int LosslessUDP_handlepacket(char * packet, uint32_t length, IP_Port source) 675int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
645{ 676{
646 677
647 switch (packet[0]) { 678 switch (packet[0]) {
@@ -681,7 +712,13 @@ void doNew()
681 712
682 } 713 }
683 //kill all timed out connections 714 //kill all timed out connections
684 if( connections[i].status > 0 && (connections[i].last_recv + CONNEXION_TIMEOUT * 1000000UL) < temp_time) 715 if( connections[i].status > 0 && (connections[i].last_recv + CONNEXION_TIMEOUT * 1000000UL) < temp_time &&
716 connections[i].status != 4)
717 {
718 //kill_connection(i);
719 connections[i].status = 4;
720 }
721 if(connections[i].status > 0 && connections[i].killat < temp_time)
685 { 722 {
686 kill_connection(i); 723 kill_connection(i);
687 } 724 }