mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Merge pull request #249 from rlt3/master
Re-styling Lossless_UDP.c and .h
This commit is contained in:
commit
e3c47d90b9
|
@ -21,21 +21,20 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* TODO: clean this file a bit.
|
/*
|
||||||
There are a couple of useless variables to get rid of. */
|
* TODO: clean this file a bit.
|
||||||
|
* There are a couple of useless variables to get rid of.
|
||||||
|
*/
|
||||||
|
|
||||||
#include "Lossless_UDP.h"
|
#include "Lossless_UDP.h"
|
||||||
|
|
||||||
/* maximum data packets in sent and receive queues. */
|
/* maximum data packets in sent and receive queues. */
|
||||||
#define MAX_QUEUE_NUM 16
|
#define MAX_QUEUE_NUM 16
|
||||||
|
|
||||||
/* maximum length of the data in the data packets */
|
|
||||||
/* #define MAX_DATA_SIZE 1024 */ /* defined in Lossless_UDP.h */
|
|
||||||
|
|
||||||
/* maximum number of data packets in the buffer */
|
/* maximum number of data packets in the buffer */
|
||||||
#define BUFFER_PACKET_NUM (16-1)
|
#define BUFFER_PACKET_NUM (16-1)
|
||||||
|
|
||||||
/* Lossless UDP connection timeout.
|
/* timeout per connection is randomly set between CONNEXION_TIMEOUT and 2*CONNEXION_TIMEOUT */
|
||||||
timeout per connection is randomly set between CONNEXION_TIMEOUT and 2*CONNEXION_TIMEOUT */
|
|
||||||
#define CONNEXION_TIMEOUT 5
|
#define CONNEXION_TIMEOUT 5
|
||||||
|
|
||||||
/* initial amount of sync/hanshake packets to send per second. */
|
/* initial amount of sync/hanshake packets to send per second. */
|
||||||
|
@ -51,34 +50,63 @@ typedef struct {
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
IP_Port ip_port;
|
IP_Port ip_port;
|
||||||
uint8_t status; /* 0 if connection is dead, 1 if attempting handshake,
|
|
||||||
2 if handshake is done (we start sending SYNC packets)
|
|
||||||
3 if we are sending SYNC packets and can send data
|
|
||||||
4 if the connection has timed out. */
|
|
||||||
|
|
||||||
uint8_t inbound; /* 1 or 2 if connection was initiated by someone else, 0 if not.
|
/*
|
||||||
2 if incoming_connection() has not returned it yet, 1 if it has. */
|
* 0 if connection is dead, 1 if attempting handshake,
|
||||||
|
* 2 if handshake is done (we start sending SYNC packets)
|
||||||
|
* 3 if we are sending SYNC packets and can send data
|
||||||
|
* 4 if the connection has timed out.
|
||||||
|
*/
|
||||||
|
uint8_t status;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 1 or 2 if connection was initiated by someone else, 0 if not.
|
||||||
|
* 2 if incoming_connection() has not returned it yet, 1 if it has.
|
||||||
|
*/
|
||||||
|
uint8_t inbound;
|
||||||
|
|
||||||
uint16_t SYNC_rate; /* current SYNC packet send rate packets per second. */
|
uint16_t SYNC_rate; /* current SYNC packet send rate packets per second. */
|
||||||
uint16_t data_rate; /* current data packet send rate packets per second. */
|
uint16_t data_rate; /* current data packet send rate packets per second. */
|
||||||
uint64_t last_SYNC; /* time at which our last SYNC packet was sent. */
|
|
||||||
uint64_t last_sent; /* time at which our last data or handshake packet was sent. */
|
uint64_t last_SYNC; /* time our last SYNC packet was sent. */
|
||||||
uint64_t last_recvSYNC; /* time at which we last received a SYNC packet from the other */
|
uint64_t last_sent; /* time our last data or handshake packet was sent. */
|
||||||
uint64_t last_recvdata; /* time at which we last received a DATA packet from the other */
|
uint64_t last_recvSYNC; /* time we last received a SYNC packet from the other */
|
||||||
uint64_t killat; /* time at which to kill the connection */
|
uint64_t last_recvdata; /* time we last received a DATA packet from the other */
|
||||||
|
uint64_t killat; /* time to kill the connection */
|
||||||
|
|
||||||
Data sendbuffer[MAX_QUEUE_NUM]; /* packet send buffer. */
|
Data sendbuffer[MAX_QUEUE_NUM]; /* packet send buffer. */
|
||||||
Data recvbuffer[MAX_QUEUE_NUM]; /* packet receive buffer. */
|
Data recvbuffer[MAX_QUEUE_NUM]; /* packet receive buffer. */
|
||||||
|
|
||||||
uint32_t handshake_id1;
|
uint32_t handshake_id1;
|
||||||
uint32_t handshake_id2;
|
uint32_t handshake_id2;
|
||||||
uint32_t recv_packetnum; /* number of data packets received (also used as handshake_id1) */
|
|
||||||
uint32_t orecv_packetnum; /* number of packets received by the other peer */
|
/* number of data packets received (also used as handshake_id1) */
|
||||||
uint32_t sent_packetnum; /* number of data packets sent */
|
uint32_t recv_packetnum;
|
||||||
uint32_t osent_packetnum; /* number of packets sent by the other peer. */
|
|
||||||
uint32_t sendbuff_packetnum; /* number of latest packet written onto the sendbuffer */
|
/* number of packets received by the other peer */
|
||||||
uint32_t successful_sent; /* we know all packets before that number were successfully sent */
|
uint32_t orecv_packetnum;
|
||||||
uint32_t successful_read; /* packet number of last packet read with the read_packet function */
|
|
||||||
uint32_t req_packets[BUFFER_PACKET_NUM]; /* list of currently requested packet numbers(by the other person) */
|
/* number of data packets sent */
|
||||||
uint16_t num_req_paquets; /* total number of currently requested packets(by the other person) */
|
uint32_t sent_packetnum;
|
||||||
|
|
||||||
|
/* number of packets sent by the other peer. */
|
||||||
|
uint32_t osent_packetnum;
|
||||||
|
|
||||||
|
/* number of latest packet written onto the sendbuffer */
|
||||||
|
uint32_t sendbuff_packetnum;
|
||||||
|
|
||||||
|
/* we know all packets before that number were successfully sent */
|
||||||
|
uint32_t successful_sent;
|
||||||
|
|
||||||
|
/* packet number of last packet read with the read_packet function */
|
||||||
|
uint32_t successful_read;
|
||||||
|
|
||||||
|
/* list of currently requested packet numbers(by the other person) */
|
||||||
|
uint32_t req_packets[BUFFER_PACKET_NUM];
|
||||||
|
|
||||||
|
/* total number of currently requested packets(by the other person) */
|
||||||
|
uint16_t num_req_paquets;
|
||||||
|
|
||||||
uint8_t recv_counter;
|
uint8_t recv_counter;
|
||||||
uint8_t send_counter;
|
uint8_t send_counter;
|
||||||
uint8_t timeout; /* connection timeout in seconds. */
|
uint8_t timeout; /* connection timeout in seconds. */
|
||||||
|
@ -94,26 +122,33 @@ static uint32_t connections_number; /* Number of connections in connections arra
|
||||||
|
|
||||||
/* Functions */
|
/* Functions */
|
||||||
|
|
||||||
/* get connection id from IP_Port
|
/*
|
||||||
return -1 if there are no connections like we are looking for
|
* Get connection id from IP_Port
|
||||||
return id if it found it */
|
* Return -1 if there are no connections like we are looking for
|
||||||
|
* Return id if it found it
|
||||||
|
*/
|
||||||
int getconnection_id(IP_Port ip_port)
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* table of random numbers used below. */
|
/* table of random numbers used below. */
|
||||||
static uint32_t randtable[6][256];
|
static uint32_t randtable[6][256];
|
||||||
|
|
||||||
/* generate a handshake_id which depends on the ip_port.
|
/*
|
||||||
this function will always give one unique handshake_id per ip_port.
|
* Generate a handshake_id which depends on the ip_port.
|
||||||
TODO: make this better */
|
* This function will always give one unique handshake_id per ip_port.
|
||||||
|
*
|
||||||
|
* TODO: make this better
|
||||||
|
*/
|
||||||
uint32_t handshake_id(IP_Port source)
|
uint32_t handshake_id(IP_Port source)
|
||||||
{
|
{
|
||||||
uint32_t id = 0, i;
|
uint32_t id = 0, i;
|
||||||
|
@ -124,21 +159,27 @@ uint32_t handshake_id(IP_Port source)
|
||||||
}
|
}
|
||||||
if (id == 0) /* id can't be zero */
|
if (id == 0) /* id can't be zero */
|
||||||
id = 1;
|
id = 1;
|
||||||
|
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* change the hnshake id associated with that ip_port
|
/*
|
||||||
TODO: make this better */
|
* Change the hanshake id associated with that ip_port
|
||||||
|
*
|
||||||
|
* TODO: make this better
|
||||||
|
*/
|
||||||
void change_handshake(IP_Port source)
|
void change_handshake(IP_Port source)
|
||||||
{
|
{
|
||||||
uint8_t rand = random_int() % 4;
|
uint8_t rand = random_int() % 4;
|
||||||
randtable[rand][((uint8_t *)&source)[rand]] = random_int();
|
randtable[rand][((uint8_t *)&source)[rand]] = random_int();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* initialize a new connection to ip_port
|
/*
|
||||||
returns an integer corresponding to the connection id.
|
* Initialize a new connection to ip_port
|
||||||
return -1 if it could not initialize the connection.
|
* Returns an integer corresponding to the connection idt
|
||||||
if there already was an existing connection to that ip_port return its number. */
|
* Return -1 if it could not initialize the connectiont
|
||||||
|
* If there already was an existing connection to that ip_port return its number.
|
||||||
|
*/
|
||||||
int new_connection(IP_Port ip_port)
|
int new_connection(IP_Port ip_port)
|
||||||
{
|
{
|
||||||
int connect = getconnection_id(ip_port);
|
int connect = getconnection_id(ip_port);
|
||||||
|
@ -148,8 +189,10 @@ int new_connection(IP_Port ip_port)
|
||||||
if(connections_number == connections_length) {
|
if(connections_number == connections_length) {
|
||||||
Connection * temp;
|
Connection * temp;
|
||||||
temp = realloc(connections, sizeof(Connection) * (connections_length + 1));
|
temp = realloc(connections, sizeof(Connection) * (connections_length + 1));
|
||||||
|
|
||||||
if(temp == NULL)
|
if(temp == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
memset(&temp[connections_length], 0, sizeof(Connection));
|
memset(&temp[connections_length], 0, sizeof(Connection));
|
||||||
++connections_length;
|
++connections_length;
|
||||||
connections = temp;
|
connections = temp;
|
||||||
|
@ -159,31 +202,37 @@ int new_connection(IP_Port ip_port)
|
||||||
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].status = 1;
|
connections[i] = (Connection) {
|
||||||
connections[i].inbound = 0;
|
.ip_port = ip_port,
|
||||||
connections[i].handshake_id1 = handshake_id(ip_port);
|
.status = 1,
|
||||||
connections[i].sent_packetnum = connections[i].handshake_id1;
|
.inbound = 0,
|
||||||
connections[i].sendbuff_packetnum = connections[i].handshake_id1;
|
.handshake_id1 = handshake_id(ip_port),
|
||||||
connections[i].successful_sent = connections[i].handshake_id1;
|
.sent_packetnum = connections[i].handshake_id1,
|
||||||
connections[i].SYNC_rate = SYNC_RATE;
|
.sendbuff_packetnum = connections[i].handshake_id1,
|
||||||
connections[i].data_rate = DATA_SYNC_RATE;
|
.successful_sent = connections[i].handshake_id1,
|
||||||
connections[i].last_recvSYNC = current_time();
|
.SYNC_rate = SYNC_RATE,
|
||||||
connections[i].last_sent = current_time();
|
.data_rate = DATA_SYNC_RATE,
|
||||||
connections[i].killat = ~0;
|
.last_recvSYNC = current_time(),
|
||||||
connections[i].send_counter = 0;
|
.last_sent = current_time(),
|
||||||
|
.killat = ~0,
|
||||||
|
.send_counter = 0,
|
||||||
/* add randomness to timeout to prevent connections getting stuck in a loop. */
|
/* add randomness to timeout to prevent connections getting stuck in a loop. */
|
||||||
connections[i].timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT;
|
.timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT
|
||||||
|
};
|
||||||
++connections_number;
|
++connections_number;
|
||||||
|
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* initialize a new inbound connection from ip_port
|
/*
|
||||||
returns an integer corresponding to the connection id.
|
* Initialize a new inbound connection from ip_port
|
||||||
return -1 if it could not initialize the connection. */
|
* Returns an integer corresponding to the connection id.
|
||||||
|
* Return -1 if it could not initialize the connection.
|
||||||
|
*/
|
||||||
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)
|
||||||
|
@ -192,8 +241,10 @@ int new_inconnection(IP_Port ip_port)
|
||||||
if(connections_number == connections_length) {
|
if(connections_number == connections_length) {
|
||||||
Connection * temp;
|
Connection * temp;
|
||||||
temp = realloc(connections, sizeof(Connection) * (connections_length + 1));
|
temp = realloc(connections, sizeof(Connection) * (connections_length + 1));
|
||||||
|
|
||||||
if(temp == NULL)
|
if(temp == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
memset(&temp[connections_length], 0, sizeof(Connection));
|
memset(&temp[connections_length], 0, sizeof(Connection));
|
||||||
++connections_length;
|
++connections_length;
|
||||||
connections = temp;
|
connections = temp;
|
||||||
|
@ -203,18 +254,23 @@ int new_inconnection(IP_Port ip_port)
|
||||||
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].status = 2;
|
connections[i] = (Connection){
|
||||||
connections[i].inbound = 2;
|
.ip_port = ip_port,
|
||||||
connections[i].SYNC_rate = SYNC_RATE;
|
.status = 2,
|
||||||
connections[i].data_rate = DATA_SYNC_RATE;
|
.inbound = 2,
|
||||||
connections[i].last_recvSYNC = current_time();
|
.SYNC_rate = SYNC_RATE,
|
||||||
connections[i].last_sent = current_time();
|
.data_rate = DATA_SYNC_RATE,
|
||||||
|
.last_recvSYNC = current_time(),
|
||||||
|
.last_sent = current_time(),
|
||||||
|
.send_counter = 127,
|
||||||
|
|
||||||
/* add randomness to timeout to prevent connections getting stuck in a loop. */
|
/* add randomness to timeout to prevent connections getting stuck in a loop. */
|
||||||
connections[i].timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT;
|
.timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT,
|
||||||
|
|
||||||
/* if this connection isn't handled within the timeout kill it. */
|
/* if this connection isn't handled within the timeout kill it. */
|
||||||
connections[i].killat = current_time() + 1000000UL*connections[i].timeout;
|
.killat = current_time() + 1000000UL*connections[i].timeout
|
||||||
connections[i].send_counter = 127;
|
};
|
||||||
++connections_number;
|
++connections_number;
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
@ -222,8 +278,10 @@ int new_inconnection(IP_Port ip_port)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* returns an integer corresponding to the next connection in our incoming connection list
|
/*
|
||||||
return -1 if there are no new incoming connections in the list. */
|
* Returns an integer corresponding to the next connection in our incoming connection list.
|
||||||
|
* Return -1 if there are no new incoming connections in the list.
|
||||||
|
*/
|
||||||
int incoming_connection()
|
int incoming_connection()
|
||||||
{
|
{
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
|
@ -233,9 +291,11 @@ int incoming_connection()
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
/*Try to free some memory from the connections array.*/
|
|
||||||
|
/* Try to free some memory from the connections array. */
|
||||||
static void free_connections()
|
static void free_connections()
|
||||||
{
|
{
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
|
@ -245,16 +305,20 @@ static void free_connections()
|
||||||
|
|
||||||
if(connections_length == i)
|
if(connections_length == i)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Connection * temp;
|
Connection * temp;
|
||||||
temp = realloc(connections, sizeof(Connection) * i);
|
temp = realloc(connections, sizeof(Connection) * i);
|
||||||
if(temp == NULL && i != 0)
|
if(temp == NULL && i != 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
connections = temp;
|
connections = temp;
|
||||||
connections_length = i;
|
connections_length = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* return -1 if it could not kill the connection.
|
/*
|
||||||
return 0 if killed successfully */
|
* Return -1 if it could not kill the connection.
|
||||||
|
* Return 0 if killed successfully
|
||||||
|
*/
|
||||||
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) {
|
||||||
|
@ -269,9 +333,11 @@ int kill_connection(int connection_id)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* kill connection in seconds seconds.
|
/*
|
||||||
return -1 if it can not kill the connection.
|
* Kill connection in seconds.
|
||||||
return 0 if it will kill it */
|
* Return -1 if it can not kill the connection.
|
||||||
|
* Return 0 if it will kill it.
|
||||||
|
*/
|
||||||
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) {
|
||||||
|
@ -283,12 +349,14 @@ int kill_connection_in(int connection_id, uint32_t seconds)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* check if connection is connected
|
/*
|
||||||
return 0 no.
|
* Check if connection is connected:
|
||||||
return 1 if attempting handshake
|
* Return 0 no.
|
||||||
return 2 if handshake is done
|
* Return 1 if attempting handshake.
|
||||||
return 3 if fully connected
|
* Return 2 if handshake is done.
|
||||||
return 4 if timed out and waiting to be killed */
|
* Return 3 if fully connected.
|
||||||
|
* Return 4 if timed out and waiting to be killed.
|
||||||
|
*/
|
||||||
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)
|
||||||
|
@ -327,8 +395,10 @@ char id_packet(int connection_id)
|
||||||
{
|
{
|
||||||
if (connection_id < 0 || connection_id >= MAX_CONNECTIONS)
|
if (connection_id < 0 || connection_id >= MAX_CONNECTIONS)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -347,14 +417,15 @@ int read_packet(int connection_id, uint8_t * data)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* return 0 if data could not be put in packet queue
|
/*
|
||||||
return 1 if data was put into the queue */
|
* Return 0 if data could not be put in packet queue
|
||||||
|
* Return 1 if data was put into the queue
|
||||||
|
*/
|
||||||
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 || length == 0)
|
||||||
return 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);
|
||||||
|
@ -362,6 +433,7 @@ int write_packet(int connection_id, uint8_t * data, uint32_t length)
|
||||||
connections[connection_id].sendbuff_packetnum++;
|
connections[connection_id].sendbuff_packetnum++;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -371,8 +443,11 @@ uint32_t missing_packets(int connection_id, uint32_t * requested)
|
||||||
uint32_t number = 0;
|
uint32_t number = 0;
|
||||||
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. */
|
|
||||||
|
/* don't request packets if the buffer is full. */
|
||||||
|
if (recvqueue(connection_id) >= (BUFFER_PACKET_NUM - 1))
|
||||||
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);
|
||||||
|
@ -380,14 +455,19 @@ uint32_t missing_packets(int connection_id, uint32_t * requested)
|
||||||
++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
|
/*
|
||||||
One per packet type.
|
* BEGIN Packet sending functions
|
||||||
see docs/Lossless_UDP.txt for more information. */
|
* One per packet type.
|
||||||
|
* see docs/Lossless_UDP.txt for more information.
|
||||||
|
*/
|
||||||
|
|
||||||
int send_handshake(IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2)
|
int send_handshake(IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2)
|
||||||
{
|
{
|
||||||
uint8_t packet[1 + 4 + 4];
|
uint8_t packet[1 + 4 + 4];
|
||||||
|
@ -398,12 +478,12 @@ int send_handshake(IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_i
|
||||||
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;
|
||||||
|
|
||||||
|
@ -411,6 +491,7 @@ int send_SYNC(uint32_t connection_id)
|
||||||
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);
|
||||||
|
|
||||||
|
@ -462,17 +543,24 @@ int send_DATA(uint32_t connection_id)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* END of packet sending functions */
|
/*
|
||||||
|
* END of packet sending functions
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* BEGIN Packet handling functions
|
||||||
|
* One to handle each type of packets we receive
|
||||||
|
*/
|
||||||
|
|
||||||
/* Packet handling functions
|
|
||||||
One to handle each type of packets we receive
|
/* Return 0 if handled correctly, 1 if packet is bad. */
|
||||||
return 0 if handled correctly, 1 if packet is bad. */
|
|
||||||
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);
|
||||||
memcpy(&temp, packet + 1, 4);
|
memcpy(&temp, packet + 1, 4);
|
||||||
handshake_id1 = ntohl(temp);
|
handshake_id1 = ntohl(temp);
|
||||||
|
@ -485,7 +573,9 @@ int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source)
|
||||||
}
|
}
|
||||||
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 is what we sent previously as handshake_id1 */
|
||||||
|
if (handshake_id2 == connections[connection].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; */
|
||||||
|
@ -494,12 +584,11 @@ int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source)
|
||||||
connections[connection].recv_packetnum = handshake_id1;
|
connections[connection].recv_packetnum = handshake_id1;
|
||||||
connections[connection].successful_read = handshake_id1;
|
connections[connection].successful_read = handshake_id1;
|
||||||
}
|
}
|
||||||
return 0;
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* returns 1 if sync packet is valid
|
/* returns 1 if sync packet is valid 0 if not. */
|
||||||
0 if not. */
|
|
||||||
int SYNC_valid(uint32_t length)
|
int SYNC_valid(uint32_t length)
|
||||||
{
|
{
|
||||||
if (length < 4 + 4 + 2)
|
if (length < 4 + 4 + 2)
|
||||||
|
@ -510,7 +599,7 @@ int SYNC_valid(uint32_t length)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* case 1: */
|
/* case 1 in handle_SYNC: */
|
||||||
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) {
|
||||||
|
@ -530,7 +619,7 @@ int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnu
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* case 2: */
|
/* case 2 in handle_SYNC: */
|
||||||
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) {
|
||||||
|
@ -543,7 +632,7 @@ int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packetnum, ui
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
/* case 3: */
|
/* case 3 in handle_SYNC: */
|
||||||
int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum, uint32_t * req_packets,
|
int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum, uint32_t * req_packets,
|
||||||
uint16_t number)
|
uint16_t number)
|
||||||
{
|
{
|
||||||
|
@ -553,17 +642,25 @@ 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 */
|
|
||||||
|
/* packet valid */
|
||||||
|
if (comp_1 <= BUFFER_PACKET_NUM &&
|
||||||
|
comp_2 <= BUFFER_PACKET_NUM &&
|
||||||
|
comp_counter < 10 && comp_counter != 0) {
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
connections[connection_id].num_req_paquets = number;
|
connections[connection_id].num_req_paquets = number;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -575,6 +672,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;
|
||||||
|
@ -587,19 +685,27 @@ int handle_SYNC(uint8_t *packet, uint32_t length, IP_Port source)
|
||||||
recv_packetnum = ntohl(temp);
|
recv_packetnum = ntohl(temp);
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* add a packet to the received buffer and set the recv_packetnum of the connection to its proper value.
|
/*
|
||||||
return 1 if data was too big, 0 if not. */
|
* Add a packet to the received buffer and set the recv_packetnum of the
|
||||||
|
* connection to its proper value. Return 1 if data was too big, 0 if not.
|
||||||
|
*/
|
||||||
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)
|
||||||
|
@ -608,16 +714,22 @@ int add_recv(int connection_id, uint32_t data_num, uint8_t *data, uint16_t size)
|
||||||
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;
|
||||||
|
@ -635,25 +747,30 @@ int handle_data(uint8_t *packet, uint32_t length, IP_Port 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. */
|
/* Drop the data packet if connection is not connected. */
|
||||||
|
if (connections[connection].status != 3)
|
||||||
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]) { //TODO: check if no break statement is correct???
|
switch (packet[0]) {
|
||||||
case 16:
|
case 16:
|
||||||
return handle_handshake(packet, length, source);
|
return handle_handshake(packet, length, source);
|
||||||
|
|
||||||
|
@ -670,8 +787,10 @@ int LosslessUDP_handlepacket(uint8_t *packet, uint32_t length, IP_Port source)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Send handshake requests
|
/*
|
||||||
handshake packets are sent at the same rate as SYNC packets */
|
* Send handshake requests
|
||||||
|
* handshake packets are sent at the same rate as SYNC packets
|
||||||
|
*/
|
||||||
void doNew()
|
void doNew()
|
||||||
{
|
{
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
|
@ -684,10 +803,13 @@ void doNew()
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 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].status != 4)
|
(connections[i].last_recvSYNC + connections[i].timeout * 1000000UL) < temp_time &&
|
||||||
/* kill_connection(i); */
|
connections[i].status != 4) {
|
||||||
connections[i].status = 4;
|
connections[i].status = 4;
|
||||||
|
/* kill_connection(i); */
|
||||||
|
}
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
@ -720,11 +842,13 @@ void doData()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO: flow control.
|
|
||||||
automatically adjusts send rates of packets for optimal transmission. */
|
|
||||||
|
|
||||||
#define MAX_SYNC_RATE 10
|
#define MAX_SYNC_RATE 10
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Automatically adjusts send rates of packets for optimal transmission.
|
||||||
|
*
|
||||||
|
* TODO: flow control.
|
||||||
|
*/
|
||||||
void adjustRates()
|
void adjustRates()
|
||||||
{
|
{
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
|
@ -744,8 +868,7 @@ void adjustRates()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Call this function a couple times per second
|
/* Call this function a couple times per second It's the main loop. */
|
||||||
It's the main loop. */
|
|
||||||
void doLossless_UDP()
|
void doLossless_UDP()
|
||||||
{
|
{
|
||||||
doNew();
|
doNew();
|
||||||
|
|
|
@ -33,70 +33,90 @@ extern "C" {
|
||||||
/* maximum length of the data in the data packets */
|
/* maximum length of the data in the data packets */
|
||||||
#define MAX_DATA_SIZE 1024
|
#define MAX_DATA_SIZE 1024
|
||||||
|
|
||||||
/* Functions */
|
/*
|
||||||
|
* Initialize a new connection to ip_port
|
||||||
/* initialize a new connection to ip_port
|
* Returns an integer corresponding to the connection id.
|
||||||
returns an integer corresponding to the connection id.
|
* Return -1 if it could not initialize the connection.
|
||||||
return -1 if it could not initialize the connection.
|
* Return number if there already was an existing connection to that ip_port.
|
||||||
if there already was an existing connection to that ip_port return its number. */
|
*/
|
||||||
int new_connection(IP_Port ip_port);
|
int new_connection(IP_Port ip_port);
|
||||||
|
|
||||||
/* get connection id from IP_Port
|
/*
|
||||||
return -1 if there are no connections like we are looking for
|
* Get connection id from IP_Port.
|
||||||
return id if it found it */
|
* Return -1 if there are no connections like we are looking for.
|
||||||
|
* Return id if it found it .
|
||||||
|
*/
|
||||||
int getconnection_id(IP_Port ip_port);
|
int getconnection_id(IP_Port ip_port);
|
||||||
|
|
||||||
/* returns an integer corresponding to the next connection in our imcoming connection list
|
/*
|
||||||
return -1 if there are no new incoming connections in the list. */
|
* Returns an int corresponding to the next connection in our imcoming connection list
|
||||||
|
* Return -1 if there are no new incoming connections in the list.
|
||||||
|
*/
|
||||||
int incoming_connection();
|
int incoming_connection();
|
||||||
|
|
||||||
/* return -1 if it could not kill the connection.
|
/*
|
||||||
return 0 if killed successfully */
|
* Return -1 if it could not kill the connection.
|
||||||
|
* Return 0 if killed successfully
|
||||||
|
*/
|
||||||
int kill_connection(int connection_id);
|
int kill_connection(int connection_id);
|
||||||
|
|
||||||
/* kill connection in seconds seconds.
|
/*
|
||||||
return -1 if it can not kill the connection.
|
* Kill connection in seconds seconds.
|
||||||
return 0 if it will kill it */
|
* Return -1 if it can not kill the connection.
|
||||||
|
* Return 0 if it will kill it
|
||||||
|
*/
|
||||||
int kill_connection_in(int connection_id, uint32_t seconds);
|
int kill_connection_in(int connection_id, uint32_t seconds);
|
||||||
|
|
||||||
/* returns the ip_port of the corresponding connection.
|
/*
|
||||||
return 0 if there is no such connection. */
|
* Returns the ip_port of the corresponding connection.
|
||||||
|
* Return 0 if there is no such connection.
|
||||||
|
*/
|
||||||
IP_Port connection_ip(int connection_id);
|
IP_Port connection_ip(int connection_id);
|
||||||
|
|
||||||
/* returns the id of the next packet in the queue
|
/*
|
||||||
return -1 if no packet in queue */
|
* Returns the id of the next packet in the queue
|
||||||
|
* Return -1 if no packet in queue
|
||||||
|
*/
|
||||||
char id_packet(int connection_id);
|
char id_packet(int connection_id);
|
||||||
|
|
||||||
/* return 0 if there is no received data in the buffer.
|
/*
|
||||||
return length of received packet if successful */
|
* Return 0 if there is no received data in the buffer.
|
||||||
|
* 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);
|
||||||
|
|
||||||
/* return 0 if data could not be put in packet queue
|
/*
|
||||||
return 1 if data was put into the queue */
|
* Return 0 if data could not be put in packet queue
|
||||||
|
* Return 1 if data was put into the queue
|
||||||
|
*/
|
||||||
int write_packet(int connection_id, uint8_t *data, uint32_t length);
|
int write_packet(int connection_id, uint8_t *data, uint32_t length);
|
||||||
|
|
||||||
/* returns the number of packets in the queue waiting to be successfully sent. */
|
/* Returns the number of packets in the queue waiting to be successfully sent. */
|
||||||
uint32_t sendqueue(int connection_id);
|
uint32_t sendqueue(int connection_id);
|
||||||
|
|
||||||
/* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */
|
/*
|
||||||
|
* returns the number of packets in the queue waiting to be successfully
|
||||||
|
* read with read_packet(...)
|
||||||
|
*/
|
||||||
uint32_t recvqueue(int connection_id);
|
uint32_t recvqueue(int connection_id);
|
||||||
|
|
||||||
/* check if connection is connected
|
/* Check if connection is connected:
|
||||||
return 0 no.
|
* Return 0 no.
|
||||||
return 1 if attempting handshake
|
* Return 1 if attempting handshake.
|
||||||
return 2 if handshake is done
|
* Return 2 if handshake is done.
|
||||||
return 3 if fully connected
|
* Return 3 if fully connected.
|
||||||
return 4 if timed out and wating to be killed */
|
* Return 4 if timed out and wating to be killed.
|
||||||
|
*/
|
||||||
int is_connected(int connection_id);
|
int is_connected(int connection_id);
|
||||||
|
|
||||||
/* Call this function a couple times per second
|
/* Call this function a couple times per second It's the main loop. */
|
||||||
It's the main loop. */
|
|
||||||
void doLossless_UDP();
|
void doLossless_UDP();
|
||||||
|
|
||||||
|
/*
|
||||||
/* if we receive a Lossless_UDP packet we call this function so it can be handled.
|
* If we receive a Lossless_UDP packet, call this function so it can be handled.
|
||||||
return 0 if packet is handled correctly.
|
* Return 0 if packet is handled correctly.
|
||||||
return 1 if it didn't handle the packet or if the packet was shit. */
|
* Return 1 if it didn't handle the packet or if the packet was shit.
|
||||||
|
*/
|
||||||
int LosslessUDP_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);
|
int LosslessUDP_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
Loading…
Reference in New Issue
Block a user