summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornfkd <nfkd@koszmial.pl>2013-07-26 19:52:20 -0300
committernfkd <nfkd@koszmial.pl>2013-07-26 19:52:20 -0300
commit4478cd4cd3f7eb89dcc3cf18ceac0386f02b5ee1 (patch)
treefd3382d3c7a59f943d7ba8485469ac1d3cc377f9
parent0565374ddc7f3efcdf9899c675a7f7b74c5cf82a (diff)
Fix braces and some white-spaces
-rw-r--r--core/Lossless_UDP.c217
1 files changed, 44 insertions, 173 deletions
diff --git a/core/Lossless_UDP.c b/core/Lossless_UDP.c
index 83bb1785..2a553b8e 100644
--- a/core/Lossless_UDP.c
+++ b/core/Lossless_UDP.c
@@ -101,13 +101,9 @@ int getconnection_id(IP_Port ip_port)
101{ 101{
102 uint32_t i; 102 uint32_t i;
103 for(i = 0; i < MAX_CONNECTIONS; ++i) 103 for(i = 0; i < MAX_CONNECTIONS; ++i)
104 {
105 if(connections[i].ip_port.ip.i == ip_port.ip.i && 104 if(connections[i].ip_port.ip.i == ip_port.ip.i &&
106 connections[i].ip_port.port == ip_port.port && connections[i].status > 0) 105 connections[i].ip_port.port == ip_port.port && connections[i].status > 0)
107 {
108 return i; 106 return i;
109 }
110 }
111 return -1; 107 return -1;
112} 108}
113 109
@@ -120,18 +116,13 @@ static uint32_t randtable[6][256];
120uint32_t handshake_id(IP_Port source) 116uint32_t handshake_id(IP_Port source)
121{ 117{
122 uint32_t id = 0, i; 118 uint32_t id = 0, i;
123 for(i = 0; i < 6; ++i) 119 for(i = 0; i < 6; ++i) {
124 {
125 if(randtable[i][((uint8_t *)&source)[i]] == 0) 120 if(randtable[i][((uint8_t *)&source)[i]] == 0)
126 {
127 randtable[i][((uint8_t *)&source)[i]] = random_int(); 121 randtable[i][((uint8_t *)&source)[i]] = random_int();
128 }
129 id ^= randtable[i][((uint8_t *)&source)[i]]; 122 id ^= randtable[i][((uint8_t *)&source)[i]];
130 } 123 }
131 if(id == 0) /* id can't be zero */ 124 if(id == 0) /* id can't be zero */
132 {
133 id = 1; 125 id = 1;
134 }
135 return id; 126 return id;
136} 127}
137 128
@@ -151,14 +142,10 @@ int new_connection(IP_Port ip_port)
151{ 142{
152 int connect = getconnection_id(ip_port); 143 int connect = getconnection_id(ip_port);
153 if(connect != -1) 144 if(connect != -1)
154 {
155 return connect; 145 return connect;
156 }
157 uint32_t i; 146 uint32_t i;
158 for(i = 0; i < MAX_CONNECTIONS; ++i) 147 for(i = 0; i < MAX_CONNECTIONS; ++i) {
159 { 148 if(connections[i].status == 0) {
160 if(connections[i].status == 0)
161 {
162 memset(&connections[i], 0, sizeof(Connection)); 149 memset(&connections[i], 0, sizeof(Connection));
163 connections[i].ip_port = ip_port; 150 connections[i].ip_port = ip_port;
164 connections[i].status = 1; 151 connections[i].status = 1;
@@ -187,14 +174,10 @@ int new_connection(IP_Port ip_port)
187int new_inconnection(IP_Port ip_port) 174int new_inconnection(IP_Port ip_port)
188{ 175{
189 if(getconnection_id(ip_port) != -1) 176 if(getconnection_id(ip_port) != -1)
190 {
191 return -1; 177 return -1;
192 }
193 uint32_t i; 178 uint32_t i;
194 for(i = 0; i < MAX_CONNECTIONS; ++i) 179 for(i = 0; i < MAX_CONNECTIONS; ++i)
195 { 180 if(connections[i].status == 0) {
196 if(connections[i].status == 0)
197 {
198 memset(&connections[i], 0, sizeof(Connection)); 181 memset(&connections[i], 0, sizeof(Connection));
199 connections[i].ip_port = ip_port; 182 connections[i].ip_port = ip_port;
200 connections[i].status = 2; 183 connections[i].status = 2;
@@ -210,7 +193,6 @@ int new_inconnection(IP_Port ip_port)
210 connections[i].send_counter = 127; 193 connections[i].send_counter = 127;
211 return i; 194 return i;
212 } 195 }
213 }
214 return -1; 196 return -1;
215} 197}
216 198
@@ -220,13 +202,10 @@ int incoming_connection()
220{ 202{
221 uint32_t i; 203 uint32_t i;
222 for(i = 0; i < MAX_CONNECTIONS; ++i) 204 for(i = 0; i < MAX_CONNECTIONS; ++i)
223 { 205 if(connections[i].inbound == 2) {
224 if(connections[i].inbound == 2)
225 {
226 connections[i].inbound = 1; 206 connections[i].inbound = 1;
227 return i; 207 return i;
228 } 208 }
229 }
230 return -1; 209 return -1;
231} 210}
232 211
@@ -235,14 +214,11 @@ int incoming_connection()
235int kill_connection(int connection_id) 214int kill_connection(int connection_id)
236{ 215{
237 if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) 216 if(connection_id >= 0 && connection_id < MAX_CONNECTIONS)
238 { 217 if(connections[connection_id].status > 0) {
239 if(connections[connection_id].status > 0)
240 {
241 connections[connection_id].status = 0; 218 connections[connection_id].status = 0;
242 change_handshake(connections[connection_id].ip_port); 219 change_handshake(connections[connection_id].ip_port);
243 return 0; 220 return 0;
244 } 221 }
245 }
246 return -1; 222 return -1;
247} 223}
248 224
@@ -252,13 +228,10 @@ int kill_connection(int connection_id)
252int kill_connection_in(int connection_id, uint32_t seconds) 228int kill_connection_in(int connection_id, uint32_t seconds)
253{ 229{
254 if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) 230 if(connection_id >= 0 && connection_id < MAX_CONNECTIONS)
255 { 231 if(connections[connection_id].status > 0) {
256 if(connections[connection_id].status > 0)
257 {
258 connections[connection_id].killat = current_time() + 1000000UL*seconds; 232 connections[connection_id].killat = current_time() + 1000000UL*seconds;
259 return 0; 233 return 0;
260 } 234 }
261 }
262 return -1; 235 return -1;
263} 236}
264 237
@@ -271,9 +244,7 @@ int kill_connection_in(int connection_id, uint32_t seconds)
271int is_connected(int connection_id) 244int is_connected(int connection_id)
272{ 245{
273 if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) 246 if(connection_id >= 0 && connection_id < MAX_CONNECTIONS)
274 {
275 return connections[connection_id].status; 247 return connections[connection_id].status;
276 }
277 return 0; 248 return 0;
278} 249}
279 250
@@ -281,9 +252,7 @@ int is_connected(int connection_id)
281IP_Port connection_ip(int connection_id) 252IP_Port connection_ip(int connection_id)
282{ 253{
283 if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) 254 if(connection_id >= 0 && connection_id < MAX_CONNECTIONS)
284 {
285 return connections[connection_id].ip_port; 255 return connections[connection_id].ip_port;
286 }
287 IP_Port zero = {{{0}}, 0}; 256 IP_Port zero = {{{0}}, 0};
288 return zero; 257 return zero;
289} 258}
@@ -305,9 +274,7 @@ uint32_t recvqueue(int connection_id)
305char id_packet(int connection_id) 274char id_packet(int connection_id)
306{ 275{
307 if(recvqueue(connection_id) != 0 && connections[connection_id].status != 0) 276 if(recvqueue(connection_id) != 0 && connections[connection_id].status != 0)
308 {
309 return connections[connection_id].recvbuffer[connections[connection_id].successful_read % MAX_QUEUE_NUM].data[0]; 277 return connections[connection_id].recvbuffer[connections[connection_id].successful_read % MAX_QUEUE_NUM].data[0];
310 }
311 return -1; 278 return -1;
312} 279}
313 280
@@ -315,8 +282,7 @@ char id_packet(int connection_id)
315 return length of received packet if successful */ 282 return length of received packet if successful */
316int read_packet(int connection_id, uint8_t * data) 283int read_packet(int connection_id, uint8_t * data)
317{ 284{
318 if(recvqueue(connection_id) != 0) 285 if(recvqueue(connection_id) != 0) {
319 {
320 uint16_t index = connections[connection_id].successful_read % MAX_QUEUE_NUM; 286 uint16_t index = connections[connection_id].successful_read % MAX_QUEUE_NUM;
321 uint16_t size = connections[connection_id].recvbuffer[index].size; 287 uint16_t size = connections[connection_id].recvbuffer[index].size;
322 memcpy(data, connections[connection_id].recvbuffer[index].data, size); 288 memcpy(data, connections[connection_id].recvbuffer[index].data, size);
@@ -332,15 +298,10 @@ int read_packet(int connection_id, uint8_t * data)
332int write_packet(int connection_id, uint8_t * data, uint32_t length) 298int write_packet(int connection_id, uint8_t * data, uint32_t length)
333{ 299{
334 if(length > MAX_DATA_SIZE) 300 if(length > MAX_DATA_SIZE)
335 {
336 return 0; 301 return 0;
337 }
338 if(length == 0) 302 if(length == 0)
339 {
340 return 0; 303 return 0;
341 } 304 if(sendqueue(connection_id) < BUFFER_PACKET_NUM) {
342 if(sendqueue(connection_id) < BUFFER_PACKET_NUM)
343 {
344 uint32_t index = connections[connection_id].sendbuff_packetnum % MAX_QUEUE_NUM; 305 uint32_t index = connections[connection_id].sendbuff_packetnum % MAX_QUEUE_NUM;
345 memcpy(connections[connection_id].sendbuffer[index].data, data, length); 306 memcpy(connections[connection_id].sendbuffer[index].data, data, length);
346 connections[connection_id].sendbuffer[index].size = length; 307 connections[connection_id].sendbuffer[index].size = length;
@@ -357,24 +318,16 @@ uint32_t missing_packets(int connection_id, uint32_t * requested)
357 uint32_t i; 318 uint32_t i;
358 uint32_t temp; 319 uint32_t temp;
359 if(recvqueue(connection_id) >= (BUFFER_PACKET_NUM - 1)) /* don't request packets if the buffer is full. */ 320 if(recvqueue(connection_id) >= (BUFFER_PACKET_NUM - 1)) /* don't request packets if the buffer is full. */
360 {
361 return 0; 321 return 0;
362 }
363 for(i = connections[connection_id].recv_packetnum; i != connections[connection_id].osent_packetnum; i++ ) 322 for(i = connections[connection_id].recv_packetnum; i != connections[connection_id].osent_packetnum; i++ )
364 { 323 if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size == 0) {
365 if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size == 0)
366 {
367 temp = htonl(i); 324 temp = htonl(i);
368 memcpy(requested + number, &temp, 4); 325 memcpy(requested + number, &temp, 4);
369 ++number; 326 ++number;
370 } 327 }
371 }
372 if(number == 0) 328 if(number == 0)
373 {
374 connections[connection_id].recv_packetnum = connections[connection_id].osent_packetnum; 329 connections[connection_id].recv_packetnum = connections[connection_id].osent_packetnum;
375 }
376 return number; 330 return number;
377
378} 331}
379 332
380/* Packet sending functions 333/* Packet sending functions
@@ -384,29 +337,28 @@ int send_handshake(IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_i
384{ 337{
385 uint8_t packet[1 + 4 + 4]; 338 uint8_t packet[1 + 4 + 4];
386 uint32_t temp; 339 uint32_t temp;
387 340
388 packet[0] = 16; 341 packet[0] = 16;
389 temp = htonl(handshake_id1); 342 temp = htonl(handshake_id1);
390 memcpy(packet + 1, &temp, 4); 343 memcpy(packet + 1, &temp, 4);
391 temp = htonl(handshake_id2); 344 temp = htonl(handshake_id2);
392 memcpy(packet + 5, &temp, 4); 345 memcpy(packet + 5, &temp, 4);
393 return sendpacket(ip_port, packet, sizeof(packet)); 346 return sendpacket(ip_port, packet, sizeof(packet));
394
395} 347}
396 348
397int send_SYNC(uint32_t connection_id) 349int send_SYNC(uint32_t connection_id)
398{ 350{
399 351
400 uint8_t packet[(BUFFER_PACKET_NUM*4 + 4 + 4 + 2)]; 352 uint8_t packet[(BUFFER_PACKET_NUM*4 + 4 + 4 + 2)];
401 uint16_t index = 0; 353 uint16_t index = 0;
402 354
403 IP_Port ip_port = connections[connection_id].ip_port; 355 IP_Port ip_port = connections[connection_id].ip_port;
404 uint8_t counter = connections[connection_id].send_counter; 356 uint8_t counter = connections[connection_id].send_counter;
405 uint32_t recv_packetnum = htonl(connections[connection_id].recv_packetnum); 357 uint32_t recv_packetnum = htonl(connections[connection_id].recv_packetnum);
406 uint32_t sent_packetnum = htonl(connections[connection_id].sent_packetnum); 358 uint32_t sent_packetnum = htonl(connections[connection_id].sent_packetnum);
407 uint32_t requested[BUFFER_PACKET_NUM]; 359 uint32_t requested[BUFFER_PACKET_NUM];
408 uint32_t number = missing_packets(connection_id, requested); 360 uint32_t number = missing_packets(connection_id, requested);
409 361
410 packet[0] = 17; 362 packet[0] = 17;
411 index += 1; 363 index += 1;
412 memcpy(packet + index, &counter, 1); 364 memcpy(packet + index, &counter, 1);
@@ -418,7 +370,7 @@ int send_SYNC(uint32_t connection_id)
418 memcpy(packet + index, requested, 4 * number); 370 memcpy(packet + index, requested, 4 * number);
419 371
420 return sendpacket(ip_port, packet, (number*4 + 4 + 4 + 2)); 372 return sendpacket(ip_port, packet, (number*4 + 4 + 4 + 2));
421 373
422} 374}
423 375
424int send_data_packet(uint32_t connection_id, uint32_t packet_num) 376int send_data_packet(uint32_t connection_id, uint32_t packet_num)
@@ -440,16 +392,14 @@ int send_DATA(uint32_t connection_id)
440{ 392{
441 int ret; 393 int ret;
442 uint32_t buffer[BUFFER_PACKET_NUM]; 394 uint32_t buffer[BUFFER_PACKET_NUM];
443 if(connections[connection_id].num_req_paquets > 0) 395 if(connections[connection_id].num_req_paquets > 0) {
444 {
445 ret = send_data_packet(connection_id, connections[connection_id].req_packets[0]); 396 ret = send_data_packet(connection_id, connections[connection_id].req_packets[0]);
446 connections[connection_id].num_req_paquets--; 397 connections[connection_id].num_req_paquets--;
447 memcpy(buffer, connections[connection_id].req_packets + 1, connections[connection_id].num_req_paquets * 4); 398 memcpy(buffer, connections[connection_id].req_packets + 1, connections[connection_id].num_req_paquets * 4);
448 memcpy(connections[connection_id].req_packets, buffer, connections[connection_id].num_req_paquets * 4); 399 memcpy(connections[connection_id].req_packets, buffer, connections[connection_id].num_req_paquets * 4);
449 return ret; 400 return ret;
450 } 401 }
451 if(connections[connection_id].sendbuff_packetnum != connections[connection_id].sent_packetnum) 402 if(connections[connection_id].sendbuff_packetnum != connections[connection_id].sent_packetnum) {
452 {
453 ret = send_data_packet(connection_id, connections[connection_id].sent_packetnum); 403 ret = send_data_packet(connection_id, connections[connection_id].sent_packetnum);
454 connections[connection_id].sent_packetnum++; 404 connections[connection_id].sent_packetnum++;
455 return ret; 405 return ret;
@@ -465,9 +415,7 @@ int send_DATA(uint32_t connection_id)
465int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) 415int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source)
466{ 416{
467 if(length != (1 + 4 + 4)) 417 if(length != (1 + 4 + 4))
468 {
469 return 1; 418 return 1;
470 }
471 uint32_t temp; 419 uint32_t temp;
472 uint32_t handshake_id1, handshake_id2; 420 uint32_t handshake_id1, handshake_id2;
473 int connection = getconnection_id(source); 421 int connection = getconnection_id(source);
@@ -476,17 +424,13 @@ int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source)
476 memcpy(&temp, packet + 5, 4); 424 memcpy(&temp, packet + 5, 4);
477 handshake_id2 = ntohl(temp); 425 handshake_id2 = ntohl(temp);
478 426
479 if(handshake_id2 == 0) 427 if(handshake_id2 == 0) {
480 {
481 send_handshake(source, handshake_id(source), handshake_id1); 428 send_handshake(source, handshake_id(source), handshake_id1);
482 return 0; 429 return 0;
483 } 430 }
484 if(is_connected(connection) != 1) 431 if(is_connected(connection) != 1)
485 {
486 return 1; 432 return 1;
487 } 433 if(handshake_id2 == connections[connection].handshake_id1) { /* if handshake_id2 is what we sent previously as handshake_id1 */
488 if(handshake_id2 == connections[connection].handshake_id1) /* if handshake_id2 is what we sent previously as handshake_id1 */
489 {
490 connections[connection].status = 2; 434 connections[connection].status = 2;
491 /* NOTE: is this necessary? 435 /* NOTE: is this necessary?
492 connections[connection].handshake_id2 = handshake_id1; */ 436 connections[connection].handshake_id2 = handshake_id1; */
@@ -504,25 +448,19 @@ int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source)
504int SYNC_valid(uint32_t length) 448int SYNC_valid(uint32_t length)
505{ 449{
506 if(length < 4 + 4 + 2) 450 if(length < 4 + 4 + 2)
507 {
508 return 0; 451 return 0;
509 }
510 if(length > (BUFFER_PACKET_NUM*4 + 4 + 4 + 2) || 452 if(length > (BUFFER_PACKET_NUM*4 + 4 + 4 + 2) ||
511 ((length - 4 - 4 - 2) % 4) != 0) 453 ((length - 4 - 4 - 2) % 4) != 0)
512 {
513 return 0; 454 return 0;
514 }
515 return 1; 455 return 1;
516} 456}
517 457
518/* case 1: */ 458/* case 1: */
519int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum) 459int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum)
520{ 460{
521 if(handshake_id(source) == recv_packetnum) 461 if(handshake_id(source) == recv_packetnum) {
522 {
523 int x = new_inconnection(source); 462 int x = new_inconnection(source);
524 if(x != -1) 463 if(x != -1) {
525 {
526 connections[x].orecv_packetnum = recv_packetnum; 464 connections[x].orecv_packetnum = recv_packetnum;
527 connections[x].sent_packetnum = recv_packetnum; 465 connections[x].sent_packetnum = recv_packetnum;
528 connections[x].sendbuff_packetnum = recv_packetnum; 466 connections[x].sendbuff_packetnum = recv_packetnum;
@@ -540,9 +478,8 @@ int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnu
540/* case 2: */ 478/* case 2: */
541int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum) 479int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum)
542{ 480{
543 if(recv_packetnum == connections[connection_id].orecv_packetnum) 481 if(recv_packetnum == connections[connection_id].orecv_packetnum) {
544 /* && sent_packetnum == connections[connection_id].osent_packetnum) */ 482 /* && sent_packetnum == connections[connection_id].osent_packetnum) */
545 {
546 connections[connection_id].status = 3; 483 connections[connection_id].status = 3;
547 connections[connection_id].recv_counter = counter; 484 connections[connection_id].recv_counter = counter;
548 ++connections[connection_id].send_counter; 485 ++connections[connection_id].send_counter;
@@ -561,16 +498,14 @@ int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, ui
561 uint32_t comp_2 = (sent_packetnum - connections[connection_id].successful_read); */ 498 uint32_t comp_2 = (sent_packetnum - connections[connection_id].successful_read); */
562 uint32_t comp_1 = (recv_packetnum - connections[connection_id].orecv_packetnum); 499 uint32_t comp_1 = (recv_packetnum - connections[connection_id].orecv_packetnum);
563 uint32_t comp_2 = (sent_packetnum - connections[connection_id].osent_packetnum); 500 uint32_t comp_2 = (sent_packetnum - connections[connection_id].osent_packetnum);
564 if(comp_1 <= BUFFER_PACKET_NUM && comp_2 <= BUFFER_PACKET_NUM && comp_counter < 10 && comp_counter != 0) /* packet valid */ 501 if(comp_1 <= BUFFER_PACKET_NUM && comp_2 <= BUFFER_PACKET_NUM && comp_counter < 10 && comp_counter != 0) { /* packet valid */
565 {
566 connections[connection_id].orecv_packetnum = recv_packetnum; 502 connections[connection_id].orecv_packetnum = recv_packetnum;
567 connections[connection_id].osent_packetnum = sent_packetnum; 503 connections[connection_id].osent_packetnum = sent_packetnum;
568 connections[connection_id].successful_sent = recv_packetnum; 504 connections[connection_id].successful_sent = recv_packetnum;
569 connections[connection_id].last_recvSYNC = current_time(); 505 connections[connection_id].last_recvSYNC = current_time();
570 connections[connection_id].recv_counter = counter; 506 connections[connection_id].recv_counter = counter;
571 ++connections[connection_id].send_counter; 507 ++connections[connection_id].send_counter;
572 for(i = 0; i < number; ++i) 508 for(i = 0; i < number; ++i) {
573 {
574 temp = ntohl(req_packets[i]); 509 temp = ntohl(req_packets[i]);
575 memcpy(connections[connection_id].req_packets + i, &temp, 4 * number); 510 memcpy(connections[connection_id].req_packets + i, &temp, 4 * number);
576 } 511 }
@@ -584,9 +519,7 @@ int handle_SYNC(uint8_t * packet, uint32_t length, IP_Port source)
584{ 519{
585 520
586 if(!SYNC_valid(length)) 521 if(!SYNC_valid(length))
587 {
588 return 1; 522 return 1;
589 }
590 int connection = getconnection_id(source); 523 int connection = getconnection_id(source);
591 uint8_t counter; 524 uint8_t counter;
592 uint32_t temp; 525 uint32_t temp;
@@ -600,21 +533,13 @@ int handle_SYNC(uint8_t * packet, uint32_t length, IP_Port source)
600 memcpy(&temp,packet + 6, 4); 533 memcpy(&temp,packet + 6, 4);
601 sent_packetnum = ntohl(temp); 534 sent_packetnum = ntohl(temp);
602 if(number != 0) 535 if(number != 0)
603 {
604 memcpy(req_packets, packet + 10, 4 * number); 536 memcpy(req_packets, packet + 10, 4 * number);
605 }
606 if(connection == -1) 537 if(connection == -1)
607 {
608 return handle_SYNC1(source, recv_packetnum, sent_packetnum); 538 return handle_SYNC1(source, recv_packetnum, sent_packetnum);
609 }
610 if(connections[connection].status == 2) 539 if(connections[connection].status == 2)
611 {
612 return handle_SYNC2(connection, counter, recv_packetnum, sent_packetnum); 540 return handle_SYNC2(connection, counter, recv_packetnum, sent_packetnum);
613 }
614 if(connections[connection].status == 3) 541 if(connections[connection].status == 3)
615 {
616 return handle_SYNC3(connection, counter, recv_packetnum, sent_packetnum, req_packets, number); 542 return handle_SYNC3(connection, counter, recv_packetnum, sent_packetnum, req_packets, number);
617 }
618 return 0; 543 return 0;
619} 544}
620 545
@@ -623,37 +548,26 @@ int handle_SYNC(uint8_t * packet, uint32_t length, IP_Port source)
623int add_recv(int connection_id, uint32_t data_num, uint8_t * data, uint16_t size) 548int add_recv(int connection_id, uint32_t data_num, uint8_t * data, uint16_t size)
624{ 549{
625 if(size > MAX_DATA_SIZE) 550 if(size > MAX_DATA_SIZE)
626 {
627 return 1; 551 return 1;
628 }
629 552
630 uint32_t i; 553 uint32_t i;
631 uint32_t maxnum = connections[connection_id].successful_read + BUFFER_PACKET_NUM; 554 uint32_t maxnum = connections[connection_id].successful_read + BUFFER_PACKET_NUM;
632 uint32_t sent_packet = data_num - connections[connection_id].osent_packetnum; 555 uint32_t sent_packet = data_num - connections[connection_id].osent_packetnum;
633 for(i = connections[connection_id].recv_packetnum; i != maxnum; ++i) 556 for(i = connections[connection_id].recv_packetnum; i != maxnum; ++i) {
634 { 557 if(i == data_num) {
635 if(i == data_num)
636 {
637 memcpy(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].data, data, size); 558 memcpy(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].data, data, size);
638 connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size = size; 559 connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size = size;
639 connections[connection_id].last_recvdata = current_time(); 560 connections[connection_id].last_recvdata = current_time();
640 if(sent_packet < BUFFER_PACKET_NUM) 561 if(sent_packet < BUFFER_PACKET_NUM)
641 {
642 connections[connection_id].osent_packetnum = data_num; 562 connections[connection_id].osent_packetnum = data_num;
643 }
644 break; 563 break;
645 } 564 }
646 } 565 }
647 for(i = connections[connection_id].recv_packetnum; i != maxnum; ++i) 566 for(i = connections[connection_id].recv_packetnum; i != maxnum; ++i) {
648 {
649 if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size != 0) 567 if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size != 0)
650 {
651 connections[connection_id].recv_packetnum = i; 568 connections[connection_id].recv_packetnum = i;
652 }
653 else 569 else
654 {
655 break; 570 break;
656 }
657 } 571 }
658 572
659 return 0; 573 return 0;
@@ -662,53 +576,43 @@ int add_recv(int connection_id, uint32_t data_num, uint8_t * data, uint16_t size
662int handle_data(uint8_t * packet, uint32_t length, IP_Port source) 576int handle_data(uint8_t * packet, uint32_t length, IP_Port source)
663{ 577{
664 int connection = getconnection_id(source); 578 int connection = getconnection_id(source);
665 579
666 if(connection == -1) 580 if(connection == -1)
667 {
668 return 1; 581 return 1;
669 } 582
670
671 if(connections[connection].status != 3) /* Drop the data packet if connection is not connected. */ 583 if(connections[connection].status != 3) /* Drop the data packet if connection is not connected. */
672 {
673 return 1; 584 return 1;
674 } 585
675
676 if(length > 1 + 4 + MAX_DATA_SIZE || length < 1 + 4 + 1) 586 if(length > 1 + 4 + MAX_DATA_SIZE || length < 1 + 4 + 1)
677 {
678 return 1; 587 return 1;
679 }
680 uint32_t temp; 588 uint32_t temp;
681 uint32_t number; 589 uint32_t number;
682 uint16_t size = length - 1 - 4; 590 uint16_t size = length - 1 - 4;
683 591
684 memcpy(&temp, packet + 1, 4); 592 memcpy(&temp, packet + 1, 4);
685 number = ntohl(temp); 593 number = ntohl(temp);
686 return add_recv(connection, number, packet + 5, size); 594 return add_recv(connection, number, packet + 5, size);
687
688} 595}
689 596
690/* END of packet handling functions */ 597/* END of packet handling functions */
691 598
692int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) 599int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
693{ 600{
694
695 switch (packet[0]) { 601 switch (packet[0]) {
696 case 16: 602 case 16:
697 return handle_handshake(packet, length, source); 603 return handle_handshake(packet, length, source);
698 604
699 case 17: 605 case 17:
700 return handle_SYNC(packet, length, source); 606 return handle_SYNC(packet, length, source);
701 607
702 case 18: 608 case 18:
703 return handle_data(packet, length, source); 609 return handle_data(packet, length, source);
704 610
705 default: 611 default:
706 return 1; 612 return 1;
707
708 } 613 }
709 614
710 return 0; 615 return 0;
711
712} 616}
713 617
714/* Send handshake requests 618/* Send handshake requests
@@ -717,28 +621,20 @@ void doNew()
717{ 621{
718 uint32_t i; 622 uint32_t i;
719 uint64_t temp_time = current_time(); 623 uint64_t temp_time = current_time();
720 for(i = 0; i < MAX_CONNECTIONS; ++i) 624 for(i = 0; i < MAX_CONNECTIONS; ++i) {
721 {
722 if(connections[i].status == 1) 625 if(connections[i].status == 1)
723 { 626 if((connections[i].last_sent + (1000000UL/connections[i].SYNC_rate)) <= temp_time) {
724 if((connections[i].last_sent + (1000000UL/connections[i].SYNC_rate)) <= temp_time)
725 {
726 send_handshake(connections[i].ip_port, connections[i].handshake_id1, 0); 627 send_handshake(connections[i].ip_port, connections[i].handshake_id1, 0);
727 connections[i].last_sent = temp_time; 628 connections[i].last_sent = temp_time;
728 } 629 }
729 630
730 }
731 /* kill all timed out connections */ 631 /* kill all timed out connections */
732 if( connections[i].status > 0 && (connections[i].last_recvSYNC + connections[i].timeout * 1000000UL) < temp_time && 632 if( connections[i].status > 0 && (connections[i].last_recvSYNC + connections[i].timeout * 1000000UL) < temp_time &&
733 connections[i].status != 4) 633 connections[i].status != 4)
734 {
735 /* kill_connection(i); */ 634 /* kill_connection(i); */
736 connections[i].status = 4; 635 connections[i].status = 4;
737 }
738 if(connections[i].status > 0 && connections[i].killat < temp_time) 636 if(connections[i].status > 0 && connections[i].killat < temp_time)
739 {
740 kill_connection(i); 637 kill_connection(i);
741 }
742 } 638 }
743} 639}
744 640
@@ -746,16 +642,12 @@ void doSYNC()
746{ 642{
747 uint32_t i; 643 uint32_t i;
748 uint64_t temp_time = current_time(); 644 uint64_t temp_time = current_time();
749 for(i = 0; i < MAX_CONNECTIONS; ++i) 645 for(i = 0; i < MAX_CONNECTIONS; ++i) {
750 {
751 if(connections[i].status == 2 || connections[i].status == 3) 646 if(connections[i].status == 2 || connections[i].status == 3)
752 { 647 if((connections[i].last_SYNC + (1000000UL/connections[i].SYNC_rate)) <= temp_time) {
753 if((connections[i].last_SYNC + (1000000UL/connections[i].SYNC_rate)) <= temp_time)
754 {
755 send_SYNC(i); 648 send_SYNC(i);
756 connections[i].last_SYNC = temp_time; 649 connections[i].last_SYNC = temp_time;
757 } 650 }
758 }
759 } 651 }
760} 652}
761 653
@@ -765,19 +657,12 @@ void doData()
765 uint64_t j; 657 uint64_t j;
766 uint64_t temp_time = current_time(); 658 uint64_t temp_time = current_time();
767 for(i = 0; i < MAX_CONNECTIONS; ++i) 659 for(i = 0; i < MAX_CONNECTIONS; ++i)
768 {
769 if(connections[i].status == 3 && sendqueue(i) != 0) 660 if(connections[i].status == 3 && sendqueue(i) != 0)
770 { 661 if((connections[i].last_sent + (1000000UL/connections[i].data_rate)) <= temp_time) {
771 if((connections[i].last_sent + (1000000UL/connections[i].data_rate)) <= temp_time)
772 {
773 for(j = connections[i].last_sent; j < temp_time; j += (1000000UL/connections[i].data_rate)) 662 for(j = connections[i].last_sent; j < temp_time; j += (1000000UL/connections[i].data_rate))
774 {
775 send_DATA(i); 663 send_DATA(i);
776 }
777 connections[i].last_sent = temp_time; 664 connections[i].last_sent = temp_time;
778 } 665 }
779 }
780 }
781} 666}
782 667
783/* TODO: flow control. 668/* TODO: flow control.
@@ -789,32 +674,20 @@ void adjustRates()
789{ 674{
790 uint32_t i; 675 uint32_t i;
791 uint64_t temp_time = current_time(); 676 uint64_t temp_time = current_time();
792 for(i = 0; i < MAX_CONNECTIONS; ++i) 677 for(i = 0; i < MAX_CONNECTIONS; ++i) {
793 {
794 if(connections[i].status == 1 || connections[i].status == 2) 678 if(connections[i].status == 1 || connections[i].status == 2)
795 {
796 connections[i].SYNC_rate = MAX_SYNC_RATE; 679 connections[i].SYNC_rate = MAX_SYNC_RATE;
797 } 680 if(connections[i].status == 3) {
798 if(connections[i].status == 3) 681 if(sendqueue(i) != 0) {
799 {
800 if(sendqueue(i) != 0)
801 {
802
803 connections[i].data_rate = (BUFFER_PACKET_NUM - connections[i].num_req_paquets) * MAX_SYNC_RATE; 682 connections[i].data_rate = (BUFFER_PACKET_NUM - connections[i].num_req_paquets) * MAX_SYNC_RATE;
804
805 connections[i].SYNC_rate = MAX_SYNC_RATE; 683 connections[i].SYNC_rate = MAX_SYNC_RATE;
806
807 } 684 }
808 else if(connections[i].last_recvdata + 1000000UL > temp_time) 685 else if(connections[i].last_recvdata + 1000000UL > temp_time)
809 {
810 connections[i].SYNC_rate = MAX_SYNC_RATE; 686 connections[i].SYNC_rate = MAX_SYNC_RATE;
811 }
812 else 687 else
813 {
814 connections[i].SYNC_rate = SYNC_RATE; 688 connections[i].SYNC_rate = SYNC_RATE;
815 }
816 } 689 }
817 } 690 }
818} 691}
819 692
820/* Call this function a couple times per second 693/* Call this function a couple times per second
@@ -825,6 +698,4 @@ void doLossless_UDP()
825 doSYNC(); 698 doSYNC();
826 doData(); 699 doData();
827 adjustRates(); 700 adjustRates();
828
829
830} 701}