Merge pull request #110 from nfkd/patch-2

Fix braces and some white-spaces
This commit is contained in:
irungentoo 2013-07-26 16:29:35 -07:00
commit d04f5f40d4

View File

@ -101,13 +101,9 @@ int getconnection_id(IP_Port ip_port)
{ {
uint32_t i; uint32_t i;
for(i = 0; i < MAX_CONNECTIONS; ++i) for(i = 0; i < MAX_CONNECTIONS; ++i)
{
if(connections[i].ip_port.ip.i == ip_port.ip.i && if(connections[i].ip_port.ip.i == ip_port.ip.i &&
connections[i].ip_port.port == ip_port.port && connections[i].status > 0) connections[i].ip_port.port == ip_port.port && connections[i].status > 0)
{
return i; return i;
}
}
return -1; return -1;
} }
@ -120,18 +116,13 @@ static uint32_t randtable[6][256];
uint32_t handshake_id(IP_Port source) uint32_t handshake_id(IP_Port source)
{ {
uint32_t id = 0, i; uint32_t id = 0, i;
for(i = 0; i < 6; ++i) for(i = 0; i < 6; ++i) {
{
if(randtable[i][((uint8_t *)&source)[i]] == 0) if(randtable[i][((uint8_t *)&source)[i]] == 0)
{
randtable[i][((uint8_t *)&source)[i]] = random_int(); randtable[i][((uint8_t *)&source)[i]] = random_int();
}
id ^= randtable[i][((uint8_t *)&source)[i]]; id ^= randtable[i][((uint8_t *)&source)[i]];
} }
if(id == 0) /* id can't be zero */ if(id == 0) /* id can't be zero */
{
id = 1; id = 1;
}
return id; return id;
} }
@ -151,14 +142,10 @@ int new_connection(IP_Port ip_port)
{ {
int connect = getconnection_id(ip_port); int connect = getconnection_id(ip_port);
if(connect != -1) if(connect != -1)
{
return connect; return connect;
}
uint32_t i; uint32_t i;
for(i = 0; i < MAX_CONNECTIONS; ++i) for(i = 0; i < MAX_CONNECTIONS; ++i) {
{ if(connections[i].status == 0) {
if(connections[i].status == 0)
{
memset(&connections[i], 0, sizeof(Connection)); memset(&connections[i], 0, sizeof(Connection));
connections[i].ip_port = ip_port; connections[i].ip_port = ip_port;
connections[i].status = 1; connections[i].status = 1;
@ -187,14 +174,10 @@ int new_connection(IP_Port ip_port)
int new_inconnection(IP_Port ip_port) int new_inconnection(IP_Port ip_port)
{ {
if(getconnection_id(ip_port) != -1) if(getconnection_id(ip_port) != -1)
{
return -1; return -1;
}
uint32_t i; uint32_t i;
for(i = 0; i < MAX_CONNECTIONS; ++i) for(i = 0; i < MAX_CONNECTIONS; ++i)
{ if(connections[i].status == 0) {
if(connections[i].status == 0)
{
memset(&connections[i], 0, sizeof(Connection)); memset(&connections[i], 0, sizeof(Connection));
connections[i].ip_port = ip_port; connections[i].ip_port = ip_port;
connections[i].status = 2; connections[i].status = 2;
@ -210,7 +193,6 @@ int new_inconnection(IP_Port ip_port)
connections[i].send_counter = 127; connections[i].send_counter = 127;
return i; return i;
} }
}
return -1; return -1;
} }
@ -220,13 +202,10 @@ int incoming_connection()
{ {
uint32_t i; uint32_t i;
for(i = 0; i < MAX_CONNECTIONS; ++i) for(i = 0; i < MAX_CONNECTIONS; ++i)
{ if(connections[i].inbound == 2) {
if(connections[i].inbound == 2)
{
connections[i].inbound = 1; connections[i].inbound = 1;
return i; return i;
} }
}
return -1; return -1;
} }
@ -235,14 +214,11 @@ int incoming_connection()
int kill_connection(int connection_id) int kill_connection(int connection_id)
{ {
if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) if(connection_id >= 0 && connection_id < MAX_CONNECTIONS)
{ if(connections[connection_id].status > 0) {
if(connections[connection_id].status > 0)
{
connections[connection_id].status = 0; connections[connection_id].status = 0;
change_handshake(connections[connection_id].ip_port); change_handshake(connections[connection_id].ip_port);
return 0; return 0;
} }
}
return -1; return -1;
} }
@ -252,13 +228,10 @@ int kill_connection(int connection_id)
int kill_connection_in(int connection_id, uint32_t seconds) int kill_connection_in(int connection_id, uint32_t seconds)
{ {
if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) if(connection_id >= 0 && connection_id < MAX_CONNECTIONS)
{ if(connections[connection_id].status > 0) {
if(connections[connection_id].status > 0)
{
connections[connection_id].killat = current_time() + 1000000UL*seconds; connections[connection_id].killat = current_time() + 1000000UL*seconds;
return 0; return 0;
} }
}
return -1; return -1;
} }
@ -271,9 +244,7 @@ int kill_connection_in(int connection_id, uint32_t seconds)
int is_connected(int connection_id) int is_connected(int connection_id)
{ {
if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) if(connection_id >= 0 && connection_id < MAX_CONNECTIONS)
{
return connections[connection_id].status; return connections[connection_id].status;
}
return 0; return 0;
} }
@ -281,9 +252,7 @@ int is_connected(int connection_id)
IP_Port connection_ip(int connection_id) IP_Port connection_ip(int connection_id)
{ {
if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) if(connection_id >= 0 && connection_id < MAX_CONNECTIONS)
{
return connections[connection_id].ip_port; return connections[connection_id].ip_port;
}
IP_Port zero = {{{0}}, 0}; IP_Port zero = {{{0}}, 0};
return zero; return zero;
} }
@ -305,9 +274,7 @@ uint32_t recvqueue(int connection_id)
char id_packet(int connection_id) char id_packet(int connection_id)
{ {
if(recvqueue(connection_id) != 0 && connections[connection_id].status != 0) if(recvqueue(connection_id) != 0 && connections[connection_id].status != 0)
{
return connections[connection_id].recvbuffer[connections[connection_id].successful_read % MAX_QUEUE_NUM].data[0]; return connections[connection_id].recvbuffer[connections[connection_id].successful_read % MAX_QUEUE_NUM].data[0];
}
return -1; return -1;
} }
@ -315,8 +282,7 @@ char id_packet(int connection_id)
return length of received packet if successful */ return length of received packet if successful */
int read_packet(int connection_id, uint8_t * data) int read_packet(int connection_id, uint8_t * data)
{ {
if(recvqueue(connection_id) != 0) if(recvqueue(connection_id) != 0) {
{
uint16_t index = connections[connection_id].successful_read % MAX_QUEUE_NUM; uint16_t index = connections[connection_id].successful_read % MAX_QUEUE_NUM;
uint16_t size = connections[connection_id].recvbuffer[index].size; uint16_t size = connections[connection_id].recvbuffer[index].size;
memcpy(data, connections[connection_id].recvbuffer[index].data, size); memcpy(data, connections[connection_id].recvbuffer[index].data, size);
@ -332,15 +298,10 @@ int read_packet(int connection_id, uint8_t * data)
int write_packet(int connection_id, uint8_t * data, uint32_t length) int write_packet(int connection_id, uint8_t * data, uint32_t length)
{ {
if(length > MAX_DATA_SIZE) if(length > MAX_DATA_SIZE)
{
return 0; return 0;
}
if(length == 0) if(length == 0)
{
return 0; return 0;
} if(sendqueue(connection_id) < BUFFER_PACKET_NUM) {
if(sendqueue(connection_id) < BUFFER_PACKET_NUM)
{
uint32_t index = connections[connection_id].sendbuff_packetnum % MAX_QUEUE_NUM; uint32_t index = connections[connection_id].sendbuff_packetnum % MAX_QUEUE_NUM;
memcpy(connections[connection_id].sendbuffer[index].data, data, length); memcpy(connections[connection_id].sendbuffer[index].data, data, length);
connections[connection_id].sendbuffer[index].size = length; connections[connection_id].sendbuffer[index].size = length;
@ -357,24 +318,16 @@ uint32_t missing_packets(int connection_id, uint32_t * requested)
uint32_t i; uint32_t i;
uint32_t temp; uint32_t temp;
if(recvqueue(connection_id) >= (BUFFER_PACKET_NUM - 1)) /* don't request packets if the buffer is full. */ if(recvqueue(connection_id) >= (BUFFER_PACKET_NUM - 1)) /* don't request packets if the buffer is full. */
{
return 0; return 0;
}
for(i = connections[connection_id].recv_packetnum; i != connections[connection_id].osent_packetnum; i++ ) for(i = connections[connection_id].recv_packetnum; i != connections[connection_id].osent_packetnum; i++ )
{ if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size == 0) {
if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size == 0)
{
temp = htonl(i); temp = htonl(i);
memcpy(requested + number, &temp, 4); memcpy(requested + number, &temp, 4);
++number; ++number;
} }
}
if(number == 0) if(number == 0)
{
connections[connection_id].recv_packetnum = connections[connection_id].osent_packetnum; connections[connection_id].recv_packetnum = connections[connection_id].osent_packetnum;
}
return number; return number;
} }
/* Packet sending functions /* Packet sending functions
@ -384,29 +337,28 @@ int send_handshake(IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_i
{ {
uint8_t packet[1 + 4 + 4]; uint8_t packet[1 + 4 + 4];
uint32_t temp; uint32_t temp;
packet[0] = 16; packet[0] = 16;
temp = htonl(handshake_id1); temp = htonl(handshake_id1);
memcpy(packet + 1, &temp, 4); memcpy(packet + 1, &temp, 4);
temp = htonl(handshake_id2); temp = htonl(handshake_id2);
memcpy(packet + 5, &temp, 4); memcpy(packet + 5, &temp, 4);
return sendpacket(ip_port, packet, sizeof(packet)); return sendpacket(ip_port, packet, sizeof(packet));
} }
int send_SYNC(uint32_t connection_id) int send_SYNC(uint32_t connection_id)
{ {
uint8_t packet[(BUFFER_PACKET_NUM*4 + 4 + 4 + 2)]; uint8_t packet[(BUFFER_PACKET_NUM*4 + 4 + 4 + 2)];
uint16_t index = 0; uint16_t index = 0;
IP_Port ip_port = connections[connection_id].ip_port; IP_Port ip_port = connections[connection_id].ip_port;
uint8_t counter = connections[connection_id].send_counter; uint8_t counter = connections[connection_id].send_counter;
uint32_t recv_packetnum = htonl(connections[connection_id].recv_packetnum); uint32_t recv_packetnum = htonl(connections[connection_id].recv_packetnum);
uint32_t sent_packetnum = htonl(connections[connection_id].sent_packetnum); uint32_t sent_packetnum = htonl(connections[connection_id].sent_packetnum);
uint32_t requested[BUFFER_PACKET_NUM]; uint32_t requested[BUFFER_PACKET_NUM];
uint32_t number = missing_packets(connection_id, requested); uint32_t number = missing_packets(connection_id, requested);
packet[0] = 17; packet[0] = 17;
index += 1; index += 1;
memcpy(packet + index, &counter, 1); memcpy(packet + index, &counter, 1);
@ -418,7 +370,7 @@ int send_SYNC(uint32_t connection_id)
memcpy(packet + index, requested, 4 * number); memcpy(packet + index, requested, 4 * number);
return sendpacket(ip_port, packet, (number*4 + 4 + 4 + 2)); return sendpacket(ip_port, packet, (number*4 + 4 + 4 + 2));
} }
int send_data_packet(uint32_t connection_id, uint32_t packet_num) int send_data_packet(uint32_t connection_id, uint32_t packet_num)
@ -440,16 +392,14 @@ int send_DATA(uint32_t connection_id)
{ {
int ret; int ret;
uint32_t buffer[BUFFER_PACKET_NUM]; uint32_t buffer[BUFFER_PACKET_NUM];
if(connections[connection_id].num_req_paquets > 0) if(connections[connection_id].num_req_paquets > 0) {
{
ret = send_data_packet(connection_id, connections[connection_id].req_packets[0]); ret = send_data_packet(connection_id, connections[connection_id].req_packets[0]);
connections[connection_id].num_req_paquets--; connections[connection_id].num_req_paquets--;
memcpy(buffer, connections[connection_id].req_packets + 1, connections[connection_id].num_req_paquets * 4); memcpy(buffer, connections[connection_id].req_packets + 1, connections[connection_id].num_req_paquets * 4);
memcpy(connections[connection_id].req_packets, buffer, connections[connection_id].num_req_paquets * 4); memcpy(connections[connection_id].req_packets, buffer, connections[connection_id].num_req_paquets * 4);
return ret; return ret;
} }
if(connections[connection_id].sendbuff_packetnum != connections[connection_id].sent_packetnum) if(connections[connection_id].sendbuff_packetnum != connections[connection_id].sent_packetnum) {
{
ret = send_data_packet(connection_id, connections[connection_id].sent_packetnum); ret = send_data_packet(connection_id, connections[connection_id].sent_packetnum);
connections[connection_id].sent_packetnum++; connections[connection_id].sent_packetnum++;
return ret; return ret;
@ -465,9 +415,7 @@ int send_DATA(uint32_t connection_id)
int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source)
{ {
if(length != (1 + 4 + 4)) if(length != (1 + 4 + 4))
{
return 1; return 1;
}
uint32_t temp; uint32_t temp;
uint32_t handshake_id1, handshake_id2; uint32_t handshake_id1, handshake_id2;
int connection = getconnection_id(source); int connection = getconnection_id(source);
@ -476,17 +424,13 @@ int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source)
memcpy(&temp, packet + 5, 4); memcpy(&temp, packet + 5, 4);
handshake_id2 = ntohl(temp); handshake_id2 = ntohl(temp);
if(handshake_id2 == 0) if(handshake_id2 == 0) {
{
send_handshake(source, handshake_id(source), handshake_id1); send_handshake(source, handshake_id(source), handshake_id1);
return 0; return 0;
} }
if(is_connected(connection) != 1) if(is_connected(connection) != 1)
{
return 1; return 1;
} if(handshake_id2 == connections[connection].handshake_id1) { /* if handshake_id2 is what we sent previously as handshake_id1 */
if(handshake_id2 == connections[connection].handshake_id1) /* if handshake_id2 is what we sent previously as handshake_id1 */
{
connections[connection].status = 2; connections[connection].status = 2;
/* NOTE: is this necessary? /* NOTE: is this necessary?
connections[connection].handshake_id2 = handshake_id1; */ connections[connection].handshake_id2 = handshake_id1; */
@ -504,25 +448,19 @@ int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source)
int SYNC_valid(uint32_t length) int SYNC_valid(uint32_t length)
{ {
if(length < 4 + 4 + 2) if(length < 4 + 4 + 2)
{
return 0; return 0;
}
if(length > (BUFFER_PACKET_NUM*4 + 4 + 4 + 2) || if(length > (BUFFER_PACKET_NUM*4 + 4 + 4 + 2) ||
((length - 4 - 4 - 2) % 4) != 0) ((length - 4 - 4 - 2) % 4) != 0)
{
return 0; return 0;
}
return 1; return 1;
} }
/* case 1: */ /* case 1: */
int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum) int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum)
{ {
if(handshake_id(source) == recv_packetnum) if(handshake_id(source) == recv_packetnum) {
{
int x = new_inconnection(source); int x = new_inconnection(source);
if(x != -1) if(x != -1) {
{
connections[x].orecv_packetnum = recv_packetnum; connections[x].orecv_packetnum = recv_packetnum;
connections[x].sent_packetnum = recv_packetnum; connections[x].sent_packetnum = recv_packetnum;
connections[x].sendbuff_packetnum = recv_packetnum; connections[x].sendbuff_packetnum = recv_packetnum;
@ -540,9 +478,8 @@ int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnu
/* case 2: */ /* case 2: */
int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum) int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum)
{ {
if(recv_packetnum == connections[connection_id].orecv_packetnum) if(recv_packetnum == connections[connection_id].orecv_packetnum) {
/* && sent_packetnum == connections[connection_id].osent_packetnum) */ /* && sent_packetnum == connections[connection_id].osent_packetnum) */
{
connections[connection_id].status = 3; connections[connection_id].status = 3;
connections[connection_id].recv_counter = counter; connections[connection_id].recv_counter = counter;
++connections[connection_id].send_counter; ++connections[connection_id].send_counter;
@ -561,16 +498,14 @@ int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, ui
uint32_t comp_2 = (sent_packetnum - connections[connection_id].successful_read); */ uint32_t comp_2 = (sent_packetnum - connections[connection_id].successful_read); */
uint32_t comp_1 = (recv_packetnum - connections[connection_id].orecv_packetnum); uint32_t comp_1 = (recv_packetnum - connections[connection_id].orecv_packetnum);
uint32_t comp_2 = (sent_packetnum - connections[connection_id].osent_packetnum); uint32_t comp_2 = (sent_packetnum - connections[connection_id].osent_packetnum);
if(comp_1 <= BUFFER_PACKET_NUM && comp_2 <= BUFFER_PACKET_NUM && comp_counter < 10 && comp_counter != 0) /* packet valid */ if(comp_1 <= BUFFER_PACKET_NUM && comp_2 <= BUFFER_PACKET_NUM && comp_counter < 10 && comp_counter != 0) { /* packet valid */
{
connections[connection_id].orecv_packetnum = recv_packetnum; connections[connection_id].orecv_packetnum = recv_packetnum;
connections[connection_id].osent_packetnum = sent_packetnum; connections[connection_id].osent_packetnum = sent_packetnum;
connections[connection_id].successful_sent = recv_packetnum; connections[connection_id].successful_sent = recv_packetnum;
connections[connection_id].last_recvSYNC = current_time(); connections[connection_id].last_recvSYNC = current_time();
connections[connection_id].recv_counter = counter; connections[connection_id].recv_counter = counter;
++connections[connection_id].send_counter; ++connections[connection_id].send_counter;
for(i = 0; i < number; ++i) for(i = 0; i < number; ++i) {
{
temp = ntohl(req_packets[i]); temp = ntohl(req_packets[i]);
memcpy(connections[connection_id].req_packets + i, &temp, 4 * number); memcpy(connections[connection_id].req_packets + i, &temp, 4 * number);
} }
@ -584,9 +519,7 @@ int handle_SYNC(uint8_t * packet, uint32_t length, IP_Port source)
{ {
if(!SYNC_valid(length)) if(!SYNC_valid(length))
{
return 1; return 1;
}
int connection = getconnection_id(source); int connection = getconnection_id(source);
uint8_t counter; uint8_t counter;
uint32_t temp; uint32_t temp;
@ -600,21 +533,13 @@ int handle_SYNC(uint8_t * packet, uint32_t length, IP_Port source)
memcpy(&temp,packet + 6, 4); memcpy(&temp,packet + 6, 4);
sent_packetnum = ntohl(temp); sent_packetnum = ntohl(temp);
if(number != 0) if(number != 0)
{
memcpy(req_packets, packet + 10, 4 * number); memcpy(req_packets, packet + 10, 4 * number);
}
if(connection == -1) if(connection == -1)
{
return handle_SYNC1(source, recv_packetnum, sent_packetnum); return handle_SYNC1(source, recv_packetnum, sent_packetnum);
}
if(connections[connection].status == 2) if(connections[connection].status == 2)
{
return handle_SYNC2(connection, counter, recv_packetnum, sent_packetnum); return handle_SYNC2(connection, counter, recv_packetnum, sent_packetnum);
}
if(connections[connection].status == 3) if(connections[connection].status == 3)
{
return handle_SYNC3(connection, counter, recv_packetnum, sent_packetnum, req_packets, number); return handle_SYNC3(connection, counter, recv_packetnum, sent_packetnum, req_packets, number);
}
return 0; return 0;
} }
@ -623,37 +548,26 @@ int handle_SYNC(uint8_t * packet, uint32_t length, IP_Port source)
int add_recv(int connection_id, uint32_t data_num, uint8_t * data, uint16_t size) int add_recv(int connection_id, uint32_t data_num, uint8_t * data, uint16_t size)
{ {
if(size > MAX_DATA_SIZE) if(size > MAX_DATA_SIZE)
{
return 1; return 1;
}
uint32_t i; uint32_t i;
uint32_t maxnum = connections[connection_id].successful_read + BUFFER_PACKET_NUM; uint32_t maxnum = connections[connection_id].successful_read + BUFFER_PACKET_NUM;
uint32_t sent_packet = data_num - connections[connection_id].osent_packetnum; uint32_t sent_packet = data_num - connections[connection_id].osent_packetnum;
for(i = connections[connection_id].recv_packetnum; i != maxnum; ++i) for(i = connections[connection_id].recv_packetnum; i != maxnum; ++i) {
{ if(i == data_num) {
if(i == data_num)
{
memcpy(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].data, data, size); memcpy(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].data, data, size);
connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size = size; connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size = size;
connections[connection_id].last_recvdata = current_time(); connections[connection_id].last_recvdata = current_time();
if(sent_packet < BUFFER_PACKET_NUM) if(sent_packet < BUFFER_PACKET_NUM)
{
connections[connection_id].osent_packetnum = data_num; connections[connection_id].osent_packetnum = data_num;
}
break; break;
} }
} }
for(i = connections[connection_id].recv_packetnum; i != maxnum; ++i) for(i = connections[connection_id].recv_packetnum; i != maxnum; ++i) {
{
if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size != 0) if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size != 0)
{
connections[connection_id].recv_packetnum = i; connections[connection_id].recv_packetnum = i;
}
else else
{
break; break;
}
} }
return 0; return 0;
@ -662,53 +576,43 @@ int add_recv(int connection_id, uint32_t data_num, uint8_t * data, uint16_t size
int handle_data(uint8_t * packet, uint32_t length, IP_Port source) int handle_data(uint8_t * packet, uint32_t length, IP_Port source)
{ {
int connection = getconnection_id(source); int connection = getconnection_id(source);
if(connection == -1) if(connection == -1)
{
return 1; return 1;
}
if(connections[connection].status != 3) /* Drop the data packet if connection is not connected. */ if(connections[connection].status != 3) /* Drop the data packet if connection is not connected. */
{
return 1; return 1;
}
if(length > 1 + 4 + MAX_DATA_SIZE || length < 1 + 4 + 1) if(length > 1 + 4 + MAX_DATA_SIZE || length < 1 + 4 + 1)
{
return 1; return 1;
}
uint32_t temp; uint32_t temp;
uint32_t number; uint32_t number;
uint16_t size = length - 1 - 4; uint16_t size = length - 1 - 4;
memcpy(&temp, packet + 1, 4); memcpy(&temp, packet + 1, 4);
number = ntohl(temp); number = ntohl(temp);
return add_recv(connection, number, packet + 5, size); return add_recv(connection, number, packet + 5, size);
} }
/* END of packet handling functions */ /* END of packet handling functions */
int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
{ {
switch (packet[0]) { switch (packet[0]) {
case 16: case 16:
return handle_handshake(packet, length, source); return handle_handshake(packet, length, source);
case 17: case 17:
return handle_SYNC(packet, length, source); return handle_SYNC(packet, length, source);
case 18: case 18:
return handle_data(packet, length, source); return handle_data(packet, length, source);
default: default:
return 1; return 1;
} }
return 0; return 0;
} }
/* Send handshake requests /* Send handshake requests
@ -717,28 +621,20 @@ void doNew()
{ {
uint32_t i; uint32_t i;
uint64_t temp_time = current_time(); uint64_t temp_time = current_time();
for(i = 0; i < MAX_CONNECTIONS; ++i) for(i = 0; i < MAX_CONNECTIONS; ++i) {
{
if(connections[i].status == 1) if(connections[i].status == 1)
{ if((connections[i].last_sent + (1000000UL/connections[i].SYNC_rate)) <= temp_time) {
if((connections[i].last_sent + (1000000UL/connections[i].SYNC_rate)) <= temp_time)
{
send_handshake(connections[i].ip_port, connections[i].handshake_id1, 0); send_handshake(connections[i].ip_port, connections[i].handshake_id1, 0);
connections[i].last_sent = temp_time; connections[i].last_sent = temp_time;
} }
}
/* kill all timed out connections */ /* kill all timed out connections */
if( connections[i].status > 0 && (connections[i].last_recvSYNC + connections[i].timeout * 1000000UL) < temp_time && if( connections[i].status > 0 && (connections[i].last_recvSYNC + connections[i].timeout * 1000000UL) < temp_time &&
connections[i].status != 4) connections[i].status != 4)
{
/* kill_connection(i); */ /* kill_connection(i); */
connections[i].status = 4; connections[i].status = 4;
}
if(connections[i].status > 0 && connections[i].killat < temp_time) if(connections[i].status > 0 && connections[i].killat < temp_time)
{
kill_connection(i); kill_connection(i);
}
} }
} }
@ -746,16 +642,12 @@ void doSYNC()
{ {
uint32_t i; uint32_t i;
uint64_t temp_time = current_time(); uint64_t temp_time = current_time();
for(i = 0; i < MAX_CONNECTIONS; ++i) for(i = 0; i < MAX_CONNECTIONS; ++i) {
{
if(connections[i].status == 2 || connections[i].status == 3) if(connections[i].status == 2 || connections[i].status == 3)
{ if((connections[i].last_SYNC + (1000000UL/connections[i].SYNC_rate)) <= temp_time) {
if((connections[i].last_SYNC + (1000000UL/connections[i].SYNC_rate)) <= temp_time)
{
send_SYNC(i); send_SYNC(i);
connections[i].last_SYNC = temp_time; connections[i].last_SYNC = temp_time;
} }
}
} }
} }
@ -765,19 +657,12 @@ void doData()
uint64_t j; uint64_t j;
uint64_t temp_time = current_time(); uint64_t temp_time = current_time();
for(i = 0; i < MAX_CONNECTIONS; ++i) for(i = 0; i < MAX_CONNECTIONS; ++i)
{
if(connections[i].status == 3 && sendqueue(i) != 0) if(connections[i].status == 3 && sendqueue(i) != 0)
{ if((connections[i].last_sent + (1000000UL/connections[i].data_rate)) <= temp_time) {
if((connections[i].last_sent + (1000000UL/connections[i].data_rate)) <= temp_time)
{
for(j = connections[i].last_sent; j < temp_time; j += (1000000UL/connections[i].data_rate)) for(j = connections[i].last_sent; j < temp_time; j += (1000000UL/connections[i].data_rate))
{
send_DATA(i); send_DATA(i);
}
connections[i].last_sent = temp_time; connections[i].last_sent = temp_time;
} }
}
}
} }
/* TODO: flow control. /* TODO: flow control.
@ -789,32 +674,20 @@ void adjustRates()
{ {
uint32_t i; uint32_t i;
uint64_t temp_time = current_time(); uint64_t temp_time = current_time();
for(i = 0; i < MAX_CONNECTIONS; ++i) for(i = 0; i < MAX_CONNECTIONS; ++i) {
{
if(connections[i].status == 1 || connections[i].status == 2) if(connections[i].status == 1 || connections[i].status == 2)
{
connections[i].SYNC_rate = MAX_SYNC_RATE; connections[i].SYNC_rate = MAX_SYNC_RATE;
} if(connections[i].status == 3) {
if(connections[i].status == 3) if(sendqueue(i) != 0) {
{
if(sendqueue(i) != 0)
{
connections[i].data_rate = (BUFFER_PACKET_NUM - connections[i].num_req_paquets) * MAX_SYNC_RATE; connections[i].data_rate = (BUFFER_PACKET_NUM - connections[i].num_req_paquets) * MAX_SYNC_RATE;
connections[i].SYNC_rate = MAX_SYNC_RATE; connections[i].SYNC_rate = MAX_SYNC_RATE;
} }
else if(connections[i].last_recvdata + 1000000UL > temp_time) else if(connections[i].last_recvdata + 1000000UL > temp_time)
{
connections[i].SYNC_rate = MAX_SYNC_RATE; connections[i].SYNC_rate = MAX_SYNC_RATE;
}
else else
{
connections[i].SYNC_rate = SYNC_RATE; connections[i].SYNC_rate = SYNC_RATE;
}
} }
} }
} }
/* Call this function a couple times per second /* Call this function a couple times per second
@ -825,6 +698,4 @@ void doLossless_UDP()
doSYNC(); doSYNC();
doData(); doData();
adjustRates(); adjustRates();
} }