diff options
Diffstat (limited to 'core/Lossless_UDP.c')
-rw-r--r-- | core/Lossless_UDP.c | 226 |
1 files changed, 113 insertions, 113 deletions
diff --git a/core/Lossless_UDP.c b/core/Lossless_UDP.c index 427d764f..710bcc4c 100644 --- a/core/Lossless_UDP.c +++ b/core/Lossless_UDP.c | |||
@@ -21,29 +21,29 @@ | |||
21 | along with Tox. If not, see <http://www.gnu.org/licenses/>. | 21 | along with Tox. If not, see <http://www.gnu.org/licenses/>. |
22 | 22 | ||
23 | */ | 23 | */ |
24 | //TODO: clean this file a bit. | 24 | /* TODO: clean this file a bit. |
25 | //There are a couple of useless variables to get rid of. | 25 | There are a couple of useless variables to get rid of. */ |
26 | #include "Lossless_UDP.h" | 26 | #include "Lossless_UDP.h" |
27 | 27 | ||
28 | 28 | ||
29 | 29 | ||
30 | //maximum data packets in sent and receive queues. | 30 | /* maximum data packets in sent and receive queues. */ |
31 | #define MAX_QUEUE_NUM 16 | 31 | #define MAX_QUEUE_NUM 16 |
32 | 32 | ||
33 | //maximum length of the data in the data packets | 33 | /* maximum length of the data in the data packets */ |
34 | //#define MAX_DATA_SIZE 1024 //defined in Lossless_UDP.h | 34 | /* #define MAX_DATA_SIZE 1024 */ /* defined in Lossless_UDP.h */ |
35 | 35 | ||
36 | //maximum number of data packets in the buffer | 36 | /* maximum number of data packets in the buffer */ |
37 | #define BUFFER_PACKET_NUM (16-1) | 37 | #define BUFFER_PACKET_NUM (16-1) |
38 | 38 | ||
39 | //Lossless UDP connection timeout. | 39 | /* Lossless UDP connection timeout. |
40 | //timeout per connection is randomly set between CONNEXION_TIMEOUT and 2*CONNEXION_TIMEOUT | 40 | timeout per connection is randomly set between CONNEXION_TIMEOUT and 2*CONNEXION_TIMEOUT */ |
41 | #define CONNEXION_TIMEOUT 5 | 41 | #define CONNEXION_TIMEOUT 5 |
42 | 42 | ||
43 | //initial amount of sync/hanshake packets to send per second. | 43 | /* initial amount of sync/hanshake packets to send per second. */ |
44 | #define SYNC_RATE 2 | 44 | #define SYNC_RATE 2 |
45 | 45 | ||
46 | //initial send rate of data. | 46 | /* initial send rate of data. */ |
47 | #define DATA_SYNC_RATE 30 | 47 | #define DATA_SYNC_RATE 30 |
48 | 48 | ||
49 | typedef struct | 49 | typedef struct |
@@ -55,37 +55,37 @@ typedef struct | |||
55 | typedef struct | 55 | typedef struct |
56 | { | 56 | { |
57 | IP_Port ip_port; | 57 | IP_Port ip_port; |
58 | uint8_t status;//0 if connection is dead, 1 if attempting handshake, | 58 | uint8_t status; /* 0 if connection is dead, 1 if attempting handshake, |
59 | //2 if handshake is done (we start sending SYNC packets) | 59 | 2 if handshake is done (we start sending SYNC packets) |
60 | //3 if we are sending SYNC packets and can send data | 60 | 3 if we are sending SYNC packets and can send data |
61 | //4 if the connection has timed out. | 61 | 4 if the connection has timed out. */ |
62 | 62 | ||
63 | uint8_t inbound; //1 or 2 if connection was initiated by someone else, 0 if not. | 63 | uint8_t inbound; /* 1 or 2 if connection was initiated by someone else, 0 if not. |
64 | //2 if incoming_connection() has not returned it yet, 1 if it has. | 64 | 2 if incoming_connection() has not returned it yet, 1 if it has. */ |
65 | 65 | ||
66 | uint16_t SYNC_rate;//current SYNC packet send rate packets per second. | 66 | uint16_t SYNC_rate; /* current SYNC packet send rate packets per second. */ |
67 | uint16_t data_rate;//current data packet send rate packets per second. | 67 | uint16_t data_rate; /* current data packet send rate packets per second. */ |
68 | uint64_t last_SYNC; //time at which our last SYNC packet was sent. | 68 | uint64_t last_SYNC; /* time at which our last SYNC packet was sent. */ |
69 | uint64_t last_sent; //time at which our last data or handshake packet was sent. | 69 | uint64_t last_sent; /* time at which our last data or handshake packet was sent. */ |
70 | uint64_t last_recvSYNC; //time at which we last received a SYNC packet from the other | 70 | uint64_t last_recvSYNC; /* time at which we last received a SYNC packet from the other */ |
71 | uint64_t last_recvdata; //time at which we last received a DATA packet from the other | 71 | uint64_t last_recvdata; /* time at which we last received a DATA packet from the other */ |
72 | uint64_t killat; //time at which to kill the connection | 72 | uint64_t killat; /* time at which to kill the connection */ |
73 | Data sendbuffer[MAX_QUEUE_NUM];//packet send buffer. | 73 | Data sendbuffer[MAX_QUEUE_NUM]; /* packet send buffer. */ |
74 | Data recvbuffer[MAX_QUEUE_NUM];//packet receive buffer. | 74 | Data recvbuffer[MAX_QUEUE_NUM]; /* packet receive buffer. */ |
75 | uint32_t handshake_id1; | 75 | uint32_t handshake_id1; |
76 | uint32_t handshake_id2; | 76 | uint32_t handshake_id2; |
77 | uint32_t recv_packetnum; //number of data packets received (also used as handshake_id1) | 77 | uint32_t recv_packetnum; /* number of data packets received (also used as handshake_id1) */ |
78 | uint32_t orecv_packetnum; //number of packets received by the other peer | 78 | uint32_t orecv_packetnum; /* number of packets received by the other peer */ |
79 | uint32_t sent_packetnum; //number of data packets sent | 79 | uint32_t sent_packetnum; /* number of data packets sent */ |
80 | uint32_t osent_packetnum; //number of packets sent by the other peer. | 80 | uint32_t osent_packetnum; /* number of packets sent by the other peer. */ |
81 | uint32_t sendbuff_packetnum; //number of latest packet written onto the sendbuffer | 81 | uint32_t sendbuff_packetnum; /* number of latest packet written onto the sendbuffer */ |
82 | uint32_t successful_sent;//we know all packets before that number were successfully sent | 82 | uint32_t successful_sent; /* we know all packets before that number were successfully sent */ |
83 | uint32_t successful_read;//packet number of last packet read with the read_packet function | 83 | uint32_t successful_read; /* packet number of last packet read with the read_packet function */ |
84 | uint32_t req_packets[BUFFER_PACKET_NUM]; //list of currently requested packet numbers(by the other person) | 84 | uint32_t req_packets[BUFFER_PACKET_NUM]; /* list of currently requested packet numbers(by the other person) */ |
85 | uint16_t num_req_paquets; //total number of currently requested packets(by the other person) | 85 | uint16_t num_req_paquets; /* total number of currently requested packets(by the other person) */ |
86 | uint8_t recv_counter; | 86 | uint8_t recv_counter; |
87 | uint8_t send_counter; | 87 | uint8_t send_counter; |
88 | uint8_t timeout; //connection timeout in seconds. | 88 | uint8_t timeout; /* connection timeout in seconds. */ |
89 | }Connection; | 89 | }Connection; |
90 | 90 | ||
91 | 91 | ||
@@ -93,13 +93,13 @@ typedef struct | |||
93 | 93 | ||
94 | static Connection connections[MAX_CONNECTIONS]; | 94 | static Connection connections[MAX_CONNECTIONS]; |
95 | 95 | ||
96 | //static uint32_t numconnections; | 96 | /* static uint32_t numconnections; */ |
97 | 97 | ||
98 | //Functions | 98 | /* Functions */ |
99 | 99 | ||
100 | //get connection id from IP_Port | 100 | /* get connection id from IP_Port |
101 | //return -1 if there are no connections like we are looking for | 101 | return -1 if there are no connections like we are looking for |
102 | //return id if it found it | 102 | return id if it found it */ |
103 | int getconnection_id(IP_Port ip_port) | 103 | int getconnection_id(IP_Port ip_port) |
104 | { | 104 | { |
105 | uint32_t i; | 105 | uint32_t i; |
@@ -114,13 +114,13 @@ int getconnection_id(IP_Port ip_port) | |||
114 | return -1; | 114 | return -1; |
115 | } | 115 | } |
116 | 116 | ||
117 | //table of random numbers used below. | 117 | /* table of random numbers used below. */ |
118 | static uint32_t randtable[6][256]; | 118 | static uint32_t randtable[6][256]; |
119 | 119 | ||
120 | 120 | ||
121 | //generate a handshake_id which depends on the ip_port. | 121 | /* generate a handshake_id which depends on the ip_port. |
122 | //this function will always give one unique handshake_id per ip_port. | 122 | this function will always give one unique handshake_id per ip_port. |
123 | //TODO: make this better | 123 | TODO: make this better */ |
124 | uint32_t handshake_id(IP_Port source) | 124 | uint32_t handshake_id(IP_Port source) |
125 | { | 125 | { |
126 | uint32_t id = 0, i; | 126 | uint32_t id = 0, i; |
@@ -132,14 +132,14 @@ uint32_t handshake_id(IP_Port source) | |||
132 | } | 132 | } |
133 | id ^= randtable[i][((uint8_t *)&source)[i]]; | 133 | id ^= randtable[i][((uint8_t *)&source)[i]]; |
134 | } | 134 | } |
135 | if(id == 0)//id can't be zero | 135 | if(id == 0) /* id can't be zero */ |
136 | { | 136 | { |
137 | id = 1; | 137 | id = 1; |
138 | } | 138 | } |
139 | return id; | 139 | return id; |
140 | } | 140 | } |
141 | //change the hnshake id associated with that ip_port | 141 | /* change the hnshake id associated with that ip_port |
142 | //TODO: make this better | 142 | TODO: make this better */ |
143 | void change_handshake(IP_Port source) | 143 | void change_handshake(IP_Port source) |
144 | { | 144 | { |
145 | uint8_t rand = random_int() % 4; | 145 | uint8_t rand = random_int() % 4; |
@@ -147,10 +147,10 @@ void change_handshake(IP_Port source) | |||
147 | } | 147 | } |
148 | 148 | ||
149 | 149 | ||
150 | //initialize a new connection to ip_port | 150 | /* initialize a new connection to ip_port |
151 | //returns an integer corresponding to the connection id. | 151 | returns an integer corresponding to the connection id. |
152 | //return -1 if it could not initialize the connection. | 152 | return -1 if it could not initialize the connection. |
153 | //if there already was an existing connection to that ip_port return its number. | 153 | if there already was an existing connection to that ip_port return its number. */ |
154 | int new_connection(IP_Port ip_port) | 154 | int new_connection(IP_Port ip_port) |
155 | { | 155 | { |
156 | int connect = getconnection_id(ip_port); | 156 | int connect = getconnection_id(ip_port); |
@@ -177,7 +177,7 @@ int new_connection(IP_Port ip_port) | |||
177 | connections[i].last_sent = current_time(); | 177 | connections[i].last_sent = current_time(); |
178 | connections[i].killat = ~0; | 178 | connections[i].killat = ~0; |
179 | connections[i].send_counter = 0; | 179 | connections[i].send_counter = 0; |
180 | //add randomness to timeout to prevent connections getting stuck in a loop. | 180 | /* add randomness to timeout to prevent connections getting stuck in a loop. */ |
181 | connections[i].timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT; | 181 | connections[i].timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT; |
182 | return i; | 182 | return i; |
183 | } | 183 | } |
@@ -185,9 +185,9 @@ int new_connection(IP_Port ip_port) | |||
185 | return -1; | 185 | return -1; |
186 | } | 186 | } |
187 | 187 | ||
188 | //initialize a new inbound connection from ip_port | 188 | /* initialize a new inbound connection from ip_port |
189 | //returns an integer corresponding to the connection id. | 189 | returns an integer corresponding to the connection id. |
190 | //return -1 if it could not initialize the connection. | 190 | return -1 if it could not initialize the connection. */ |
191 | int new_inconnection(IP_Port ip_port) | 191 | int new_inconnection(IP_Port ip_port) |
192 | { | 192 | { |
193 | if(getconnection_id(ip_port) != -1) | 193 | if(getconnection_id(ip_port) != -1) |
@@ -207,9 +207,9 @@ int new_inconnection(IP_Port ip_port) | |||
207 | connections[i].data_rate = DATA_SYNC_RATE; | 207 | connections[i].data_rate = DATA_SYNC_RATE; |
208 | connections[i].last_recvSYNC = current_time(); | 208 | connections[i].last_recvSYNC = current_time(); |
209 | connections[i].last_sent = current_time(); | 209 | connections[i].last_sent = current_time(); |
210 | //add randomness to timeout to prevent connections getting stuck in a loop. | 210 | /* add randomness to timeout to prevent connections getting stuck in a loop. */ |
211 | connections[i].timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT; | 211 | connections[i].timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT; |
212 | //if this connection isn't handled within the timeout kill it. | 212 | /* if this connection isn't handled within the timeout kill it. */ |
213 | connections[i].killat = current_time() + 1000000UL*connections[i].timeout; | 213 | connections[i].killat = current_time() + 1000000UL*connections[i].timeout; |
214 | connections[i].send_counter = 127; | 214 | connections[i].send_counter = 127; |
215 | return i; | 215 | return i; |
@@ -218,8 +218,8 @@ int new_inconnection(IP_Port ip_port) | |||
218 | return -1; | 218 | return -1; |
219 | } | 219 | } |
220 | 220 | ||
221 | //returns an integer corresponding to the next connection in our incoming connection list | 221 | /* returns an integer corresponding to the next connection in our incoming connection list |
222 | //return -1 if there are no new incoming connections in the list. | 222 | return -1 if there are no new incoming connections in the list. */ |
223 | int incoming_connection() | 223 | int incoming_connection() |
224 | { | 224 | { |
225 | uint32_t i; | 225 | uint32_t i; |
@@ -234,8 +234,8 @@ int incoming_connection() | |||
234 | return -1; | 234 | return -1; |
235 | } | 235 | } |
236 | 236 | ||
237 | //return -1 if it could not kill the connection. | 237 | /* return -1 if it could not kill the connection. |
238 | //return 0 if killed successfully | 238 | return 0 if killed successfully */ |
239 | int kill_connection(int connection_id) | 239 | int kill_connection(int connection_id) |
240 | { | 240 | { |
241 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) | 241 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) |
@@ -250,9 +250,9 @@ int kill_connection(int connection_id) | |||
250 | return -1; | 250 | return -1; |
251 | } | 251 | } |
252 | 252 | ||
253 | //kill connection in seconds seconds. | 253 | /* kill connection in seconds seconds. |
254 | //return -1 if it can not kill the connection. | 254 | return -1 if it can not kill the connection. |
255 | //return 0 if it will kill it | 255 | return 0 if it will kill it */ |
256 | int kill_connection_in(int connection_id, uint32_t seconds) | 256 | int kill_connection_in(int connection_id, uint32_t seconds) |
257 | { | 257 | { |
258 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) | 258 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) |
@@ -266,12 +266,12 @@ int kill_connection_in(int connection_id, uint32_t seconds) | |||
266 | return -1; | 266 | return -1; |
267 | } | 267 | } |
268 | 268 | ||
269 | //check if connection is connected | 269 | /* check if connection is connected |
270 | //return 0 no. | 270 | return 0 no. |
271 | //return 1 if attempting handshake | 271 | return 1 if attempting handshake |
272 | //return 2 if handshake is done | 272 | return 2 if handshake is done |
273 | //return 3 if fully connected | 273 | return 3 if fully connected |
274 | //return 4 if timed out and waiting to be killed | 274 | return 4 if timed out and waiting to be killed */ |
275 | int is_connected(int connection_id) | 275 | int is_connected(int connection_id) |
276 | { | 276 | { |
277 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) | 277 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) |
@@ -281,7 +281,7 @@ int is_connected(int connection_id) | |||
281 | return 0; | 281 | return 0; |
282 | } | 282 | } |
283 | 283 | ||
284 | //returns the ip_port of the corresponding connection. | 284 | /* returns the ip_port of the corresponding connection. */ |
285 | IP_Port connection_ip(int connection_id) | 285 | IP_Port connection_ip(int connection_id) |
286 | { | 286 | { |
287 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) | 287 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) |
@@ -292,20 +292,20 @@ IP_Port connection_ip(int connection_id) | |||
292 | return zero; | 292 | return zero; |
293 | } | 293 | } |
294 | 294 | ||
295 | //returns the number of packets in the queue waiting to be successfully sent. | 295 | /* returns the number of packets in the queue waiting to be successfully sent. */ |
296 | uint32_t sendqueue(int connection_id) | 296 | uint32_t sendqueue(int connection_id) |
297 | { | 297 | { |
298 | return connections[connection_id].sendbuff_packetnum - connections[connection_id].successful_sent; | 298 | return connections[connection_id].sendbuff_packetnum - connections[connection_id].successful_sent; |
299 | } | 299 | } |
300 | 300 | ||
301 | //returns the number of packets in the queue waiting to be successfully read with read_packet(...) | 301 | /* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */ |
302 | uint32_t recvqueue(int connection_id) | 302 | uint32_t recvqueue(int connection_id) |
303 | { | 303 | { |
304 | return connections[connection_id].recv_packetnum - connections[connection_id].successful_read; | 304 | return connections[connection_id].recv_packetnum - connections[connection_id].successful_read; |
305 | } | 305 | } |
306 | 306 | ||
307 | //returns the id of the next packet in the queue | 307 | /* returns the id of the next packet in the queue |
308 | //return -1 if no packet in queue | 308 | return -1 if no packet in queue */ |
309 | char id_packet(int connection_id) | 309 | char id_packet(int connection_id) |
310 | { | 310 | { |
311 | if(recvqueue(connection_id) != 0 && connections[connection_id].status != 0) | 311 | if(recvqueue(connection_id) != 0 && connections[connection_id].status != 0) |
@@ -314,8 +314,8 @@ char id_packet(int connection_id) | |||
314 | } | 314 | } |
315 | return -1; | 315 | return -1; |
316 | } | 316 | } |
317 | //return 0 if there is no received data in the buffer. | 317 | /* return 0 if there is no received data in the buffer. |
318 | //return length of received packet if successful | 318 | return length of received packet if successful */ |
319 | int read_packet(int connection_id, uint8_t * data) | 319 | int read_packet(int connection_id, uint8_t * data) |
320 | { | 320 | { |
321 | if(recvqueue(connection_id) != 0) | 321 | if(recvqueue(connection_id) != 0) |
@@ -330,8 +330,8 @@ int read_packet(int connection_id, uint8_t * data) | |||
330 | return 0; | 330 | return 0; |
331 | } | 331 | } |
332 | 332 | ||
333 | //return 0 if data could not be put in packet queue | 333 | /* return 0 if data could not be put in packet queue |
334 | //return 1 if data was put into the queue | 334 | return 1 if data was put into the queue */ |
335 | int write_packet(int connection_id, uint8_t * data, uint32_t length) | 335 | int write_packet(int connection_id, uint8_t * data, uint32_t length) |
336 | { | 336 | { |
337 | if(length > MAX_DATA_SIZE) | 337 | if(length > MAX_DATA_SIZE) |
@@ -356,13 +356,13 @@ int write_packet(int connection_id, uint8_t * data, uint32_t length) | |||
356 | 356 | ||
357 | 357 | ||
358 | 358 | ||
359 | //put the packet numbers the we are missing in requested and return the number | 359 | /* put the packet numbers the we are missing in requested and return the number */ |
360 | uint32_t missing_packets(int connection_id, uint32_t * requested) | 360 | uint32_t missing_packets(int connection_id, uint32_t * requested) |
361 | { | 361 | { |
362 | uint32_t number = 0; | 362 | uint32_t number = 0; |
363 | uint32_t i; | 363 | uint32_t i; |
364 | uint32_t temp; | 364 | uint32_t temp; |
365 | if(recvqueue(connection_id) >= (BUFFER_PACKET_NUM - 1))//don't request packets if the buffer is full. | 365 | if(recvqueue(connection_id) >= (BUFFER_PACKET_NUM - 1)) /* don't request packets if the buffer is full. */ |
366 | { | 366 | { |
367 | return 0; | 367 | return 0; |
368 | } | 368 | } |
@@ -383,9 +383,9 @@ uint32_t missing_packets(int connection_id, uint32_t * requested) | |||
383 | 383 | ||
384 | } | 384 | } |
385 | 385 | ||
386 | //Packet sending functions | 386 | /* Packet sending functions |
387 | //One per packet type. | 387 | One per packet type. |
388 | //see docs/Lossless_UDP.txt for more information. | 388 | see docs/Lossless_UDP.txt for more information. */ |
389 | 389 | ||
390 | 390 | ||
391 | int send_handshake(IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2) | 391 | int send_handshake(IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2) |
@@ -444,7 +444,7 @@ int send_data_packet(uint32_t connection_id, uint32_t packet_num) | |||
444 | 1 + 4 + connections[connection_id].sendbuffer[index].size); | 444 | 1 + 4 + connections[connection_id].sendbuffer[index].size); |
445 | } | 445 | } |
446 | 446 | ||
447 | //sends 1 data packet | 447 | /* sends 1 data packet */ |
448 | int send_DATA(uint32_t connection_id) | 448 | int send_DATA(uint32_t connection_id) |
449 | { | 449 | { |
450 | int ret; | 450 | int ret; |
@@ -466,13 +466,13 @@ int send_DATA(uint32_t connection_id) | |||
466 | return 0; | 466 | return 0; |
467 | } | 467 | } |
468 | 468 | ||
469 | //END of packet sending functions | 469 | /* END of packet sending functions */ |
470 | 470 | ||
471 | 471 | ||
472 | 472 | ||
473 | //Packet handling functions | 473 | /* Packet handling functions |
474 | //One to handle each type of packets we receive | 474 | One to handle each type of packets we receive |
475 | //return 0 if handled correctly, 1 if packet is bad. | 475 | return 0 if handled correctly, 1 if packet is bad. */ |
476 | int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) | 476 | int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) |
477 | { | 477 | { |
478 | if(length != (1 + 4 + 4)) | 478 | if(length != (1 + 4 + 4)) |
@@ -496,11 +496,11 @@ int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) | |||
496 | { | 496 | { |
497 | return 1; | 497 | return 1; |
498 | } | 498 | } |
499 | if(handshake_id2 == connections[connection].handshake_id1)//if handshake_id2 is what we sent previously as handshake_id1 | 499 | if(handshake_id2 == connections[connection].handshake_id1) /* if handshake_id2 is what we sent previously as handshake_id1 */ |
500 | { | 500 | { |
501 | connections[connection].status = 2; | 501 | connections[connection].status = 2; |
502 | //NOTE:is this necessary? | 502 | /* NOTE: is this necessary? |
503 | //connections[connection].handshake_id2 = handshake_id1; | 503 | connections[connection].handshake_id2 = handshake_id1; */ |
504 | connections[connection].orecv_packetnum = handshake_id2; | 504 | connections[connection].orecv_packetnum = handshake_id2; |
505 | connections[connection].osent_packetnum = handshake_id1; | 505 | connections[connection].osent_packetnum = handshake_id1; |
506 | connections[connection].recv_packetnum = handshake_id1; | 506 | connections[connection].recv_packetnum = handshake_id1; |
@@ -510,8 +510,8 @@ int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) | |||
510 | 510 | ||
511 | } | 511 | } |
512 | 512 | ||
513 | //returns 1 if sync packet is valid | 513 | /* returns 1 if sync packet is valid |
514 | //0 if not. | 514 | 0 if not. */ |
515 | int SYNC_valid(uint32_t length) | 515 | int SYNC_valid(uint32_t length) |
516 | { | 516 | { |
517 | if(length < 4 + 4 + 2) | 517 | if(length < 4 + 4 + 2) |
@@ -526,7 +526,7 @@ int SYNC_valid(uint32_t length) | |||
526 | return 1; | 526 | return 1; |
527 | } | 527 | } |
528 | 528 | ||
529 | //case 1: | 529 | /* case 1: */ |
530 | int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum) | 530 | int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum) |
531 | { | 531 | { |
532 | if(handshake_id(source) == recv_packetnum) | 532 | if(handshake_id(source) == recv_packetnum) |
@@ -548,11 +548,11 @@ int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnu | |||
548 | return -1; | 548 | return -1; |
549 | } | 549 | } |
550 | 550 | ||
551 | //case 2: | 551 | /* case 2: */ |
552 | int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum) | 552 | int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum) |
553 | { | 553 | { |
554 | if(recv_packetnum == connections[connection_id].orecv_packetnum) | 554 | if(recv_packetnum == connections[connection_id].orecv_packetnum) |
555 | //&& sent_packetnum == connections[connection_id].osent_packetnum) | 555 | /* && sent_packetnum == connections[connection_id].osent_packetnum) */ |
556 | { | 556 | { |
557 | connections[connection_id].status = 3; | 557 | connections[connection_id].status = 3; |
558 | connections[connection_id].recv_counter = counter; | 558 | connections[connection_id].recv_counter = counter; |
@@ -562,17 +562,17 @@ int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packetnum, ui | |||
562 | } | 562 | } |
563 | return 1; | 563 | return 1; |
564 | } | 564 | } |
565 | //case 3: | 565 | /* case 3: */ |
566 | int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum, uint32_t * req_packets, | 566 | int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum, uint32_t * req_packets, |
567 | uint16_t number) | 567 | uint16_t number) |
568 | { | 568 | { |
569 | uint8_t comp_counter = (counter - connections[connection_id].recv_counter ); | 569 | uint8_t comp_counter = (counter - connections[connection_id].recv_counter ); |
570 | uint32_t i, temp; | 570 | uint32_t i, temp; |
571 | //uint32_t comp_1 = (recv_packetnum - connections[connection_id].successful_sent); | 571 | /* uint32_t comp_1 = (recv_packetnum - connections[connection_id].successful_sent); |
572 | //uint32_t comp_2 = (sent_packetnum - connections[connection_id].successful_read); | 572 | uint32_t comp_2 = (sent_packetnum - connections[connection_id].successful_read); */ |
573 | uint32_t comp_1 = (recv_packetnum - connections[connection_id].orecv_packetnum); | 573 | uint32_t comp_1 = (recv_packetnum - connections[connection_id].orecv_packetnum); |
574 | uint32_t comp_2 = (sent_packetnum - connections[connection_id].osent_packetnum); | 574 | uint32_t comp_2 = (sent_packetnum - connections[connection_id].osent_packetnum); |
575 | if(comp_1 <= BUFFER_PACKET_NUM && comp_2 <= BUFFER_PACKET_NUM && comp_counter < 10 && comp_counter != 0) //packet valid | 575 | if(comp_1 <= BUFFER_PACKET_NUM && comp_2 <= BUFFER_PACKET_NUM && comp_counter < 10 && comp_counter != 0) /* packet valid */ |
576 | { | 576 | { |
577 | connections[connection_id].orecv_packetnum = recv_packetnum; | 577 | connections[connection_id].orecv_packetnum = recv_packetnum; |
578 | connections[connection_id].osent_packetnum = sent_packetnum; | 578 | connections[connection_id].osent_packetnum = sent_packetnum; |
@@ -629,8 +629,8 @@ int handle_SYNC(uint8_t * packet, uint32_t length, IP_Port source) | |||
629 | return 0; | 629 | return 0; |
630 | } | 630 | } |
631 | 631 | ||
632 | //add a packet to the received buffer and set the recv_packetnum of the connection to its proper value. | 632 | /* add a packet to the received buffer and set the recv_packetnum of the connection to its proper value. |
633 | //return 1 if data was too big, 0 if not. | 633 | return 1 if data was too big, 0 if not. */ |
634 | int add_recv(int connection_id, uint32_t data_num, uint8_t * data, uint16_t size) | 634 | int add_recv(int connection_id, uint32_t data_num, uint8_t * data, uint16_t size) |
635 | { | 635 | { |
636 | if(size > MAX_DATA_SIZE) | 636 | if(size > MAX_DATA_SIZE) |
@@ -679,7 +679,7 @@ int handle_data(uint8_t * packet, uint32_t length, IP_Port source) | |||
679 | return 1; | 679 | return 1; |
680 | } | 680 | } |
681 | 681 | ||
682 | if(connections[connection].status != 3)//Drop the data packet if connection is not connected. | 682 | if(connections[connection].status != 3) /* Drop the data packet if connection is not connected. */ |
683 | { | 683 | { |
684 | return 1; | 684 | return 1; |
685 | } | 685 | } |
@@ -698,7 +698,7 @@ int handle_data(uint8_t * packet, uint32_t length, IP_Port source) | |||
698 | 698 | ||
699 | } | 699 | } |
700 | 700 | ||
701 | //END of packet handling functions | 701 | /* END of packet handling functions */ |
702 | 702 | ||
703 | 703 | ||
704 | int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) | 704 | int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) |
@@ -723,8 +723,8 @@ int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) | |||
723 | 723 | ||
724 | } | 724 | } |
725 | 725 | ||
726 | //Send handshake requests | 726 | /* Send handshake requests |
727 | //handshake packets are sent at the same rate as SYNC packets | 727 | handshake packets are sent at the same rate as SYNC packets */ |
728 | void doNew() | 728 | void doNew() |
729 | { | 729 | { |
730 | uint32_t i; | 730 | uint32_t i; |
@@ -740,11 +740,11 @@ void doNew() | |||
740 | } | 740 | } |
741 | 741 | ||
742 | } | 742 | } |
743 | //kill all timed out connections | 743 | /* kill all timed out connections */ |
744 | if( connections[i].status > 0 && (connections[i].last_recvSYNC + connections[i].timeout * 1000000UL) < temp_time && | 744 | if( connections[i].status > 0 && (connections[i].last_recvSYNC + connections[i].timeout * 1000000UL) < temp_time && |
745 | connections[i].status != 4) | 745 | connections[i].status != 4) |
746 | { | 746 | { |
747 | //kill_connection(i); | 747 | /* kill_connection(i); */ |
748 | connections[i].status = 4; | 748 | connections[i].status = 4; |
749 | } | 749 | } |
750 | if(connections[i].status > 0 && connections[i].killat < temp_time) | 750 | if(connections[i].status > 0 && connections[i].killat < temp_time) |
@@ -792,8 +792,8 @@ void doData() | |||
792 | } | 792 | } |
793 | } | 793 | } |
794 | 794 | ||
795 | //TODO: flow control. | 795 | /* TODO: flow control. |
796 | //automatically adjusts send rates of packets for optimal transmission. | 796 | automatically adjusts send rates of packets for optimal transmission. */ |
797 | 797 | ||
798 | #define MAX_SYNC_RATE 10 | 798 | #define MAX_SYNC_RATE 10 |
799 | 799 | ||
@@ -824,8 +824,8 @@ void adjustRates() | |||
824 | } | 824 | } |
825 | } | 825 | } |
826 | } | 826 | } |
827 | //Call this function a couple times per second | 827 | /* Call this function a couple times per second |
828 | //It's the main loop. | 828 | It's the main loop. */ |
829 | void doLossless_UDP() | 829 | void doLossless_UDP() |
830 | { | 830 | { |
831 | doNew(); | 831 | doNew(); |