mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Modified Lossless_UDP connections to work with tox_array.
This commit is contained in:
parent
9a7f7b5ec4
commit
76761a605b
|
@ -117,7 +117,7 @@ static inline void tox_list_remove(tox_list *lst)
|
|||
************************************************************/
|
||||
|
||||
typedef struct tox_array {
|
||||
void *data;
|
||||
void *data; /* last elem is data[len-1] */
|
||||
uint32_t len;
|
||||
size_t elem_size; /* in bytes */
|
||||
} tox_array;
|
||||
|
@ -135,34 +135,48 @@ static inline void tox_array_delete(tox_array *arr)
|
|||
arr->len = arr->elem_size = 0;
|
||||
}
|
||||
|
||||
static inline void _tox_array_push(tox_array *arr, uint8_t *item)
|
||||
static inline uint8_t tox_array_push_ptr(tox_array *arr, uint8_t *item)
|
||||
{
|
||||
arr->data = realloc(arr->data, arr->elem_size * (arr->len+1));
|
||||
|
||||
memcpy(arr->data + arr->elem_size*arr->len, item, arr->elem_size);
|
||||
if (arr->data == NULL)
|
||||
return 0;
|
||||
|
||||
if (item != NULL)
|
||||
memcpy(arr->data + arr->elem_size*arr->len, item, arr->elem_size);
|
||||
arr->len++;
|
||||
|
||||
return 1;
|
||||
}
|
||||
#define tox_array_push(arr, item) _tox_array_push(arr, (void*)(&(item)))
|
||||
#define tox_array_push(arr, item) tox_array_push_ptr(arr, (uint8_t*)(&(item)))
|
||||
|
||||
/* Deletes num items from array.
|
||||
* Not same as pop in stacks, because to access elements you use data.
|
||||
*/
|
||||
static inline void tox_array_pop(tox_array *arr, uint32_t num)
|
||||
{
|
||||
arr->len--;
|
||||
if (num == 0)
|
||||
num = 1;
|
||||
arr->len -= num;
|
||||
arr->data = realloc(arr->data, arr->elem_size*arr->len);
|
||||
}
|
||||
|
||||
/* TODO: do not use type, since we track the needed info in
|
||||
* elem_size (but array user will have to cast)
|
||||
*/
|
||||
#define tox_array_get(arr, i, type) (((type*)(arr)->data)[i])
|
||||
|
||||
/* TODO: what about nested for loops? */
|
||||
#define tox_array_for_each(arr, type) \
|
||||
/* This version requires C99 (declaring variables inside for loop)
|
||||
#define tox_array_for_each(arr, type, tmp_name) \
|
||||
for ( \
|
||||
struct { type tmp; uint32_t i; } tox_array_tmp = { tox_array_get(arr, 0, type), 0 }; \
|
||||
tox_array_tmp.i != (arr)->len; \
|
||||
tox_array_tmp.tmp = tox_array_get(arr, ++tox_array_tmp.i, type) \
|
||||
struct { type val; uint32_t i; } tmp_name = { tox_array_get(arr, 0, type), 0 }; \
|
||||
tmp_name.i != (arr)->len; \
|
||||
tmp_name.val = tox_array_get(arr, ++tmp_name.i, type) \
|
||||
)
|
||||
*/
|
||||
|
||||
#define TOX_ARRAY_TMP (tox_array_tmp.tmp)
|
||||
#define tox_array_for_each(arr, type, tmp_name) \
|
||||
type tmp_name; uint32_t tmp_name ## _i = 0; \
|
||||
for (; tmp_name ## _i != (arr)->len; tmp_name = tox_array_get(arr, ++ tmp_name ## _i, type))
|
||||
|
||||
#endif // MISC_TOOLS_H
|
||||
#endif // MISC_TOOLS_H
|
||||
|
|
|
@ -38,13 +38,22 @@
|
|||
*/
|
||||
int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port)
|
||||
{
|
||||
uint32_t i;
|
||||
/*vx-t uint32_t i;
|
||||
|
||||
for (i = 0; i < ludp->connections_length; ++i) {
|
||||
if (ludp->connections[i].ip_port.ip.i == ip_port.ip.i &&
|
||||
ludp->connections[i].ip_port.port == ip_port.port &&
|
||||
ludp->connections[i].status > 0)
|
||||
return i;
|
||||
}*/
|
||||
|
||||
tox_array_for_each(&ludp->connections, Connection, tmp) {
|
||||
if (tmp.ip_port.ip.i == ip_port.ip.i &&
|
||||
tmp.ip_port.port == ip_port.port &&
|
||||
tmp.status > 0)
|
||||
{
|
||||
return tmp_i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
|
@ -98,7 +107,12 @@ int new_connection(Lossless_UDP *ludp, IP_Port ip_port)
|
|||
if (connect != -1)
|
||||
return connect;
|
||||
|
||||
if (ludp->connections_number == ludp->connections_length) {
|
||||
/* TODO: this allocates space regardless of whether it's needed or not
|
||||
* Rewrite so it will first scan for empty, and if not found make a new one.
|
||||
*/
|
||||
tox_array_push_ptr(&ludp->connections, 0);
|
||||
memset(&tox_array_get(&ludp->connections, ludp->connections.len, Connection), 0, sizeof(Connection));
|
||||
/*vx-t if (ludp->connections_number == ludp->connections_length) {
|
||||
Connection *temp;
|
||||
temp = realloc(ludp->connections, sizeof(Connection) * (ludp->connections_length + 1));
|
||||
|
||||
|
@ -108,9 +122,34 @@ int new_connection(Lossless_UDP *ludp, IP_Port ip_port)
|
|||
memset(&temp[ludp->connections_length], 0, sizeof(Connection));
|
||||
++ludp->connections_length;
|
||||
ludp->connections = temp;
|
||||
}
|
||||
}*/
|
||||
|
||||
uint32_t i;
|
||||
tox_array_for_each(&ludp->connections, Connection, tmp) {
|
||||
if (tmp.status == 0) {
|
||||
memset(&tox_array_get(&ludp->connections, tmp_i, Connection), 0, sizeof(Connection));
|
||||
uint32_t handshake_id1 = handshake_id(ludp, ip_port);
|
||||
|
||||
tox_array_get(&ludp->connections, tmp_i, Connection) = (Connection) {
|
||||
.ip_port = ip_port,
|
||||
.status = 1,
|
||||
.inbound = 0,
|
||||
.handshake_id1 = handshake_id1,
|
||||
.sent_packetnum = handshake_id1,
|
||||
.sendbuff_packetnum = handshake_id1,
|
||||
.successful_sent = handshake_id1,
|
||||
.SYNC_rate = SYNC_RATE,
|
||||
.data_rate = DATA_SYNC_RATE,
|
||||
.last_recvSYNC = current_time(),
|
||||
.last_sent = current_time(),
|
||||
.killat = ~0,
|
||||
.send_counter = 0,
|
||||
/* add randomness to timeout to prevent connections getting stuck in a loop. */
|
||||
.timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT
|
||||
};
|
||||
return tmp_i;
|
||||
}
|
||||
}
|
||||
/*vx-t uint32_t i;
|
||||
|
||||
for (i = 0; i < ludp->connections_length; ++i) {
|
||||
if (ludp->connections[i].status == 0) {
|
||||
|
@ -131,14 +170,14 @@ int new_connection(Lossless_UDP *ludp, IP_Port ip_port)
|
|||
.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. /
|
||||
.timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT
|
||||
};
|
||||
++ludp->connections_number;
|
||||
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
@ -153,7 +192,11 @@ static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port)
|
|||
if (getconnection_id(ludp, ip_port) != -1)
|
||||
return -1;
|
||||
|
||||
if (ludp->connections_number == ludp->connections_length) {
|
||||
/* TODO: See comment in int new_connection(Lossless_UDP *ludp, IP_Port ip_port) */
|
||||
tox_array_push_ptr(&ludp->connections, 0);
|
||||
memset(&tox_array_get(&ludp->connections, ludp->connections.len, Connection), 0, sizeof(Connection));
|
||||
|
||||
/*vx-t if (ludp->connections_number == ludp->connections_length) {
|
||||
Connection *temp;
|
||||
temp = realloc(ludp->connections, sizeof(Connection) * (ludp->connections_length + 1));
|
||||
|
||||
|
@ -163,8 +206,8 @@ static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port)
|
|||
memset(&temp[ludp->connections_length], 0, sizeof(Connection));
|
||||
++ludp->connections_length;
|
||||
ludp->connections = temp;
|
||||
}
|
||||
|
||||
}*/
|
||||
/* vx-k: IMPORTANT!!!!!!!!! DO THIS
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < ludp->connections_length; ++i) {
|
||||
|
@ -182,17 +225,17 @@ static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port)
|
|||
.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. /
|
||||
.timeout = timeout,
|
||||
|
||||
/* if this connection isn't handled within the timeout kill it. */
|
||||
/* if this connection isn't handled within the timeout kill it. /
|
||||
.killat = current_time() + 1000000UL * timeout
|
||||
};
|
||||
++ludp->connections_number;
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -202,14 +245,21 @@ static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port)
|
|||
*/
|
||||
int incoming_connection(Lossless_UDP *ludp)
|
||||
{
|
||||
uint32_t i;
|
||||
tox_array_for_each(&ludp->connections, Connection, tmp) {
|
||||
if (tmp.inbound == 2) {
|
||||
tmp.inbound = 1;
|
||||
return tmp_i;
|
||||
}
|
||||
}
|
||||
|
||||
/*vx-k uint32_t i;
|
||||
|
||||
for (i = 0; i < ludp->connections_length; ++i) {
|
||||
if (ludp->connections[i].inbound == 2) {
|
||||
ludp->connections[i].inbound = 1;
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
@ -217,6 +267,7 @@ int incoming_connection(Lossless_UDP *ludp)
|
|||
/* Try to free some memory from the connections array. */
|
||||
static void free_connections(Lossless_UDP *ludp)
|
||||
{
|
||||
/*vx-k: fix this to work with new tox_array
|
||||
uint32_t i;
|
||||
|
||||
for (i = ludp->connections_length; i != 0; --i)
|
||||
|
@ -241,6 +292,7 @@ static void free_connections(Lossless_UDP *ludp)
|
|||
|
||||
ludp->connections = temp;
|
||||
ludp->connections_length = i;
|
||||
*/
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -249,11 +301,11 @@ static void free_connections(Lossless_UDP *ludp)
|
|||
*/
|
||||
int kill_connection(Lossless_UDP *ludp, int connection_id)
|
||||
{
|
||||
if (connection_id >= 0 && connection_id < ludp->connections_length) {
|
||||
if (ludp->connections[connection_id].status > 0) {
|
||||
ludp->connections[connection_id].status = 0;
|
||||
change_handshake(ludp, ludp->connections[connection_id].ip_port);
|
||||
--ludp->connections_number;
|
||||
if (connection_id >= 0 && connection_id < ludp->connections.len) {
|
||||
if (tox_array_get(&ludp->connections, connection_id, Connection).status > 0) {
|
||||
tox_array_get(&ludp->connections, connection_id, Connection).status = 0;
|
||||
change_handshake(ludp, tox_array_get(&ludp->connections, connection_id, Connection).ip_port);
|
||||
tox_array_pop(&ludp->connections, 0);
|
||||
free_connections(ludp);
|
||||
return 0;
|
||||
}
|
||||
|
@ -269,9 +321,9 @@ int kill_connection(Lossless_UDP *ludp, int connection_id)
|
|||
*/
|
||||
int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds)
|
||||
{
|
||||
if (connection_id >= 0 && connection_id < ludp->connections_length) {
|
||||
if (ludp->connections[connection_id].status > 0) {
|
||||
ludp->connections[connection_id].killat = current_time() + 1000000UL * seconds;
|
||||
if (connection_id >= 0 && connection_id < ludp->connections.len) {
|
||||
if (tox_array_get(&ludp->connections, connection_id, Connection).status > 0) {
|
||||
tox_array_get(&ludp->connections, connection_id, Connection).killat = current_time() + 1000000UL * seconds;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -289,8 +341,8 @@ int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds)
|
|||
*/
|
||||
int is_connected(Lossless_UDP *ludp, int connection_id)
|
||||
{
|
||||
if (connection_id >= 0 && connection_id < ludp->connections_length)
|
||||
return ludp->connections[connection_id].status;
|
||||
if (connection_id >= 0 && connection_id < ludp->connections.len)
|
||||
return tox_array_get(&ludp->connections, connection_id, Connection).status;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -298,8 +350,8 @@ int is_connected(Lossless_UDP *ludp, int connection_id)
|
|||
/* returns the ip_port of the corresponding connection. */
|
||||
IP_Port connection_ip(Lossless_UDP *ludp, int connection_id)
|
||||
{
|
||||
if (connection_id >= 0 && connection_id < ludp->connections_length)
|
||||
return ludp->connections[connection_id].ip_port;
|
||||
if (connection_id >= 0 && connection_id < ludp->connections.len)
|
||||
return tox_array_get(&ludp->connections, connection_id, Connection).ip_port;
|
||||
|
||||
IP_Port zero = {{{0}}, 0};
|
||||
return zero;
|
||||
|
@ -308,30 +360,31 @@ IP_Port connection_ip(Lossless_UDP *ludp, int connection_id)
|
|||
/* returns the number of packets in the queue waiting to be successfully sent. */
|
||||
uint32_t sendqueue(Lossless_UDP *ludp, int connection_id)
|
||||
{
|
||||
if (connection_id < 0 || connection_id >= ludp->connections_length)
|
||||
if (connection_id < 0 || connection_id >= ludp->connections.len)
|
||||
return 0;
|
||||
|
||||
return ludp->connections[connection_id].sendbuff_packetnum - ludp->connections[connection_id].successful_sent;
|
||||
return tox_array_get(&ludp->connections, connection_id, Connection).sendbuff_packetnum -
|
||||
tox_array_get(&ludp->connections, connection_id, Connection).successful_sent;
|
||||
}
|
||||
|
||||
/* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */
|
||||
uint32_t recvqueue(Lossless_UDP *ludp, int connection_id)
|
||||
{
|
||||
if (connection_id < 0 || connection_id >= ludp->connections_length)
|
||||
if (connection_id < 0 || connection_id >= ludp->connections.len)
|
||||
return 0;
|
||||
|
||||
return ludp->connections[connection_id].recv_packetnum - ludp->connections[connection_id].successful_read;
|
||||
return tox_array_get(&ludp->connections, connection_id, Connection).recv_packetnum - tox_array_get(&ludp->connections, connection_id, Connection).successful_read;
|
||||
}
|
||||
|
||||
/* returns the id of the next packet in the queue
|
||||
return -1 if no packet in queue */
|
||||
char id_packet(Lossless_UDP *ludp, int connection_id)
|
||||
{
|
||||
if (connection_id < 0 || connection_id >= ludp->connections_length)
|
||||
if (connection_id < 0 || connection_id >= ludp->connections.len)
|
||||
return -1;
|
||||
|
||||
if (recvqueue(ludp, connection_id) != 0 && ludp->connections[connection_id].status != 0)
|
||||
return ludp->connections[connection_id].recvbuffer[ludp->connections[connection_id].successful_read %
|
||||
if (recvqueue(ludp, connection_id) != 0 && tox_array_get(&ludp->connections, connection_id, Connection).status != 0)
|
||||
return tox_array_get(&ludp->connections, connection_id, Connection).recvbuffer[tox_array_get(&ludp->connections, connection_id, Connection).successful_read %
|
||||
MAX_QUEUE_NUM].data[0];
|
||||
|
||||
return -1;
|
||||
|
@ -342,11 +395,11 @@ char id_packet(Lossless_UDP *ludp, int connection_id)
|
|||
int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data)
|
||||
{
|
||||
if (recvqueue(ludp, connection_id) != 0) {
|
||||
uint16_t index = ludp->connections[connection_id].successful_read % MAX_QUEUE_NUM;
|
||||
uint16_t size = ludp->connections[connection_id].recvbuffer[index].size;
|
||||
memcpy(data, ludp->connections[connection_id].recvbuffer[index].data, size);
|
||||
++ludp->connections[connection_id].successful_read;
|
||||
ludp->connections[connection_id].recvbuffer[index].size = 0;
|
||||
uint16_t index = tox_array_get(&ludp->connections, connection_id, Connection).successful_read % MAX_QUEUE_NUM;
|
||||
uint16_t size = tox_array_get(&ludp->connections, connection_id, Connection).recvbuffer[index].size;
|
||||
memcpy(data, tox_array_get(&ludp->connections, connection_id, Connection).recvbuffer[index].data, size);
|
||||
++tox_array_get(&ludp->connections, connection_id, Connection).successful_read;
|
||||
tox_array_get(&ludp->connections, connection_id, Connection).recvbuffer[index].size = 0;
|
||||
return size;
|
||||
}
|
||||
|
||||
|
@ -363,10 +416,10 @@ int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t
|
|||
return 0;
|
||||
|
||||
if (sendqueue(ludp, connection_id) < BUFFER_PACKET_NUM) {
|
||||
uint32_t index = ludp->connections[connection_id].sendbuff_packetnum % MAX_QUEUE_NUM;
|
||||
memcpy(ludp->connections[connection_id].sendbuffer[index].data, data, length);
|
||||
ludp->connections[connection_id].sendbuffer[index].size = length;
|
||||
ludp->connections[connection_id].sendbuff_packetnum++;
|
||||
uint32_t index = tox_array_get(&ludp->connections, connection_id, Connection).sendbuff_packetnum % MAX_QUEUE_NUM;
|
||||
memcpy(tox_array_get(&ludp->connections, connection_id, Connection).sendbuffer[index].data, data, length);
|
||||
tox_array_get(&ludp->connections, connection_id, Connection).sendbuffer[index].size = length;
|
||||
tox_array_get(&ludp->connections, connection_id, Connection).sendbuff_packetnum++;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -384,8 +437,11 @@ uint32_t missing_packets(Lossless_UDP *ludp, int connection_id, uint32_t *reques
|
|||
if (recvqueue(ludp, connection_id) >= (BUFFER_PACKET_NUM - 1))
|
||||
return 0;
|
||||
|
||||
for (i = ludp->connections[connection_id].recv_packetnum; i != ludp->connections[connection_id].osent_packetnum; i++) {
|
||||
if (ludp->connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size == 0) {
|
||||
for (i = tox_array_get(&ludp->connections, connection_id, Connection).recv_packetnum;
|
||||
i != tox_array_get(&ludp->connections, connection_id, Connection).osent_packetnum;
|
||||
i++)
|
||||
{
|
||||
if (tox_array_get(&ludp->connections, connection_id, Connection).recvbuffer[i % MAX_QUEUE_NUM].size == 0) {
|
||||
temp = htonl(i);
|
||||
memcpy(requested + number, &temp, 4);
|
||||
++number;
|
||||
|
@ -393,7 +449,7 @@ uint32_t missing_packets(Lossless_UDP *ludp, int connection_id, uint32_t *reques
|
|||
}
|
||||
|
||||
if (number == 0)
|
||||
ludp->connections[connection_id].recv_packetnum = ludp->connections[connection_id].osent_packetnum;
|
||||
tox_array_get(&ludp->connections, connection_id, Connection).recv_packetnum = tox_array_get(&ludp->connections, connection_id, Connection).osent_packetnum;
|
||||
|
||||
return number;
|
||||
}
|
||||
|
@ -423,10 +479,10 @@ static int send_SYNC(Lossless_UDP *ludp, uint32_t connection_id)
|
|||
uint8_t packet[(BUFFER_PACKET_NUM * 4 + 4 + 4 + 2)];
|
||||
uint16_t index = 0;
|
||||
|
||||
IP_Port ip_port = ludp->connections[connection_id].ip_port;
|
||||
uint8_t counter = ludp->connections[connection_id].send_counter;
|
||||
uint32_t recv_packetnum = htonl(ludp->connections[connection_id].recv_packetnum);
|
||||
uint32_t sent_packetnum = htonl(ludp->connections[connection_id].sent_packetnum);
|
||||
IP_Port ip_port = tox_array_get(&ludp->connections, connection_id, Connection).ip_port;
|
||||
uint8_t counter = tox_array_get(&ludp->connections, connection_id, Connection).send_counter;
|
||||
uint32_t recv_packetnum = htonl(tox_array_get(&ludp->connections, connection_id, Connection).recv_packetnum);
|
||||
uint32_t sent_packetnum = htonl(tox_array_get(&ludp->connections, connection_id, Connection).sent_packetnum);
|
||||
|
||||
uint32_t requested[BUFFER_PACKET_NUM];
|
||||
uint32_t number = missing_packets(ludp, connection_id, requested);
|
||||
|
@ -453,29 +509,30 @@ static int send_data_packet(Lossless_UDP *ludp, uint32_t connection_id, uint32_t
|
|||
packet[0] = NET_PACKET_DATA;
|
||||
temp = htonl(packet_num);
|
||||
memcpy(packet + 1, &temp, 4);
|
||||
memcpy(packet + 5, ludp->connections[connection_id].sendbuffer[index].data,
|
||||
ludp->connections[connection_id].sendbuffer[index].size);
|
||||
return sendpacket(ludp->net->sock, ludp->connections[connection_id].ip_port, packet,
|
||||
1 + 4 + ludp->connections[connection_id].sendbuffer[index].size);
|
||||
memcpy(packet + 5, tox_array_get(&ludp->connections, connection_id, Connection).sendbuffer[index].data,
|
||||
tox_array_get(&ludp->connections, connection_id, Connection).sendbuffer[index].size);
|
||||
return sendpacket(ludp->net->sock, tox_array_get(&ludp->connections, connection_id, Connection).ip_port, packet,
|
||||
1 + 4 + tox_array_get(&ludp->connections, connection_id, Connection).sendbuffer[index].size);
|
||||
}
|
||||
|
||||
/* sends 1 data packet */
|
||||
static int send_DATA(Lossless_UDP *ludp, uint32_t connection_id)
|
||||
{
|
||||
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||
int ret;
|
||||
uint32_t buffer[BUFFER_PACKET_NUM];
|
||||
|
||||
if (ludp->connections[connection_id].num_req_paquets > 0) {
|
||||
ret = send_data_packet(ludp, connection_id, ludp->connections[connection_id].req_packets[0]);
|
||||
ludp->connections[connection_id].num_req_paquets--;
|
||||
memcpy(buffer, ludp->connections[connection_id].req_packets + 1, ludp->connections[connection_id].num_req_paquets * 4);
|
||||
memcpy(ludp->connections[connection_id].req_packets, buffer, ludp->connections[connection_id].num_req_paquets * 4);
|
||||
if (connection->num_req_paquets > 0) {
|
||||
ret = send_data_packet(ludp, connection_id, connection->req_packets[0]);
|
||||
connection->num_req_paquets--;
|
||||
memcpy(buffer, connection->req_packets + 1, connection->num_req_paquets * 4);
|
||||
memcpy(connection->req_packets, buffer, connection->num_req_paquets * 4);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (ludp->connections[connection_id].sendbuff_packetnum != ludp->connections[connection_id].sent_packetnum) {
|
||||
ret = send_data_packet(ludp, connection_id, ludp->connections[connection_id].sent_packetnum);
|
||||
ludp->connections[connection_id].sent_packetnum++;
|
||||
if (connection->sendbuff_packetnum != connection->sent_packetnum) {
|
||||
ret = send_data_packet(ludp, connection_id, connection->sent_packetnum);
|
||||
connection->sent_packetnum++;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -502,29 +559,31 @@ static int handle_handshake(void *object, IP_Port source, uint8_t *packet, uint3
|
|||
uint32_t temp;
|
||||
uint32_t handshake_id1, handshake_id2;
|
||||
|
||||
int connection = getconnection_id(ludp, source);
|
||||
int connection_id = getconnection_id(ludp, source);
|
||||
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||
|
||||
memcpy(&temp, packet + 1, 4);
|
||||
handshake_id1 = ntohl(temp);
|
||||
memcpy(&temp, packet + 5, 4);
|
||||
handshake_id2 = ntohl(temp);
|
||||
|
||||
if (handshake_id2 == 0 && is_connected(ludp, connection) < 3) {
|
||||
if (handshake_id2 == 0 && is_connected(ludp, connection_id) < 3) {
|
||||
send_handshake(ludp, source, handshake_id(ludp, source), handshake_id1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (is_connected(ludp, connection) != 1)
|
||||
if (is_connected(ludp, connection_id) != 1)
|
||||
return 1;
|
||||
|
||||
/* if handshake_id2 is what we sent previously as handshake_id1 */
|
||||
if (handshake_id2 == ludp->connections[connection].handshake_id1) {
|
||||
ludp->connections[connection].status = 2;
|
||||
if (handshake_id2 == connection->handshake_id1) {
|
||||
connection->status = 2;
|
||||
/* NOTE: is this necessary?
|
||||
ludp->connections[connection].handshake_id2 = handshake_id1; */
|
||||
ludp->connections[connection].orecv_packetnum = handshake_id2;
|
||||
ludp->connections[connection].osent_packetnum = handshake_id1;
|
||||
ludp->connections[connection].recv_packetnum = handshake_id1;
|
||||
ludp->connections[connection].successful_read = handshake_id1;
|
||||
connection->handshake_id2 = handshake_id1; */
|
||||
connection->orecv_packetnum = handshake_id2;
|
||||
connection->osent_packetnum = handshake_id1;
|
||||
connection->recv_packetnum = handshake_id1;
|
||||
connection->successful_read = handshake_id1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -547,18 +606,19 @@ static int SYNC_valid(uint32_t length)
|
|||
static int handle_SYNC1(Lossless_UDP *ludp, IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum)
|
||||
{
|
||||
if (handshake_id(ludp, source) == recv_packetnum) {
|
||||
int x = new_inconnection(ludp, source);
|
||||
int connection_id = new_inconnection(ludp, source);
|
||||
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||
|
||||
if (x != -1) {
|
||||
ludp->connections[x].orecv_packetnum = recv_packetnum;
|
||||
ludp->connections[x].sent_packetnum = recv_packetnum;
|
||||
ludp->connections[x].sendbuff_packetnum = recv_packetnum;
|
||||
ludp->connections[x].successful_sent = recv_packetnum;
|
||||
ludp->connections[x].osent_packetnum = sent_packetnum;
|
||||
ludp->connections[x].recv_packetnum = sent_packetnum;
|
||||
ludp->connections[x].successful_read = sent_packetnum;
|
||||
if (connection_id != -1) {
|
||||
connection->orecv_packetnum = recv_packetnum;
|
||||
connection->sent_packetnum = recv_packetnum;
|
||||
connection->sendbuff_packetnum = recv_packetnum;
|
||||
connection->successful_sent = recv_packetnum;
|
||||
connection->osent_packetnum = sent_packetnum;
|
||||
connection->recv_packetnum = sent_packetnum;
|
||||
connection->successful_read = sent_packetnum;
|
||||
|
||||
return x;
|
||||
return connection_id;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -569,11 +629,13 @@ static int handle_SYNC1(Lossless_UDP *ludp, IP_Port source, uint32_t recv_packet
|
|||
static int handle_SYNC2(Lossless_UDP *ludp, int connection_id, uint8_t counter, uint32_t recv_packetnum,
|
||||
uint32_t sent_packetnum)
|
||||
{
|
||||
if (recv_packetnum == ludp->connections[connection_id].orecv_packetnum) {
|
||||
/* && sent_packetnum == ludp->connections[connection_id].osent_packetnum) */
|
||||
ludp->connections[connection_id].status = 3;
|
||||
ludp->connections[connection_id].recv_counter = counter;
|
||||
++ludp->connections[connection_id].send_counter;
|
||||
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||
|
||||
if (recv_packetnum == connection->orecv_packetnum) {
|
||||
/* && sent_packetnum == connection->osent_packetnum) */
|
||||
connection->status = 3;
|
||||
connection->recv_counter = counter;
|
||||
++connection->send_counter;
|
||||
send_SYNC(ludp, connection_id);
|
||||
return 0;
|
||||
}
|
||||
|
@ -583,35 +645,37 @@ static int handle_SYNC2(Lossless_UDP *ludp, int connection_id, uint8_t counter,
|
|||
/* case 3 in handle_SYNC: */
|
||||
static int handle_SYNC3(Lossless_UDP *ludp, int connection_id, uint8_t counter, uint32_t recv_packetnum,
|
||||
uint32_t sent_packetnum,
|
||||
uint32_t *req_packets,
|
||||
uint32_t *req_packets,
|
||||
uint16_t number)
|
||||
{
|
||||
uint8_t comp_counter = (counter - ludp->connections[connection_id].recv_counter );
|
||||
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||
|
||||
uint8_t comp_counter = (counter - connection->recv_counter);
|
||||
uint32_t i, temp;
|
||||
/* uint32_t comp_1 = (recv_packetnum - ludp->connections[connection_id].successful_sent);
|
||||
uint32_t comp_2 = (sent_packetnum - ludp->connections[connection_id].successful_read); */
|
||||
uint32_t comp_1 = (recv_packetnum - ludp->connections[connection_id].orecv_packetnum);
|
||||
uint32_t comp_2 = (sent_packetnum - ludp->connections[connection_id].osent_packetnum);
|
||||
/* uint32_t comp_1 = (recv_packetnum - connection->successful_sent);
|
||||
uint32_t comp_2 = (sent_packetnum - connection->successful_read); */
|
||||
uint32_t comp_1 = (recv_packetnum - connection->orecv_packetnum);
|
||||
uint32_t comp_2 = (sent_packetnum - connection->osent_packetnum);
|
||||
|
||||
/* packet valid */
|
||||
if (comp_1 <= BUFFER_PACKET_NUM &&
|
||||
comp_2 <= BUFFER_PACKET_NUM &&
|
||||
comp_counter < 10 && comp_counter != 0) {
|
||||
|
||||
ludp->connections[connection_id].orecv_packetnum = recv_packetnum;
|
||||
ludp->connections[connection_id].osent_packetnum = sent_packetnum;
|
||||
ludp->connections[connection_id].successful_sent = recv_packetnum;
|
||||
ludp->connections[connection_id].last_recvSYNC = current_time();
|
||||
ludp->connections[connection_id].recv_counter = counter;
|
||||
connection->orecv_packetnum = recv_packetnum;
|
||||
connection->osent_packetnum = sent_packetnum;
|
||||
connection->successful_sent = recv_packetnum;
|
||||
connection->last_recvSYNC = current_time();
|
||||
connection->recv_counter = counter;
|
||||
|
||||
++ludp->connections[connection_id].send_counter;
|
||||
++connection->send_counter;
|
||||
|
||||
for (i = 0; i < number; ++i) {
|
||||
temp = ntohl(req_packets[i]);
|
||||
memcpy(ludp->connections[connection_id].req_packets + i, &temp, 4 * number);
|
||||
memcpy(connection->req_packets + i, &temp, 4 * number);
|
||||
}
|
||||
|
||||
ludp->connections[connection_id].num_req_paquets = number;
|
||||
connection->num_req_paquets = number;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -625,7 +689,6 @@ static int handle_SYNC(void *object, IP_Port source, uint8_t *packet, uint32_t l
|
|||
if (!SYNC_valid(length))
|
||||
return 1;
|
||||
|
||||
int connection = getconnection_id(ludp, source);
|
||||
uint8_t counter;
|
||||
uint32_t temp;
|
||||
uint32_t recv_packetnum, sent_packetnum;
|
||||
|
@ -641,15 +704,17 @@ static int handle_SYNC(void *object, IP_Port source, uint8_t *packet, uint32_t l
|
|||
if (number != 0)
|
||||
memcpy(req_packets, packet + 10, 4 * number);
|
||||
|
||||
if (connection == -1)
|
||||
int connection_id = getconnection_id(ludp, source);
|
||||
if (connection_id == -1)
|
||||
return handle_SYNC1(ludp, source, recv_packetnum, sent_packetnum);
|
||||
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||
|
||||
if (ludp->connections[connection].status == 2)
|
||||
return handle_SYNC2(ludp, connection, counter,
|
||||
if (connection->status == 2)
|
||||
return handle_SYNC2(ludp, connection_id, counter,
|
||||
recv_packetnum, sent_packetnum);
|
||||
|
||||
if (ludp->connections[connection].status == 3)
|
||||
return handle_SYNC3(ludp, connection, counter, recv_packetnum,
|
||||
if (connection->status == 3)
|
||||
return handle_SYNC3(ludp, connection_id, counter, recv_packetnum,
|
||||
sent_packetnum, req_packets, number);
|
||||
|
||||
return 0;
|
||||
|
@ -661,31 +726,33 @@ static int handle_SYNC(void *object, IP_Port source, uint8_t *packet, uint32_t l
|
|||
*/
|
||||
static int add_recv(Lossless_UDP *ludp, int connection_id, uint32_t data_num, uint8_t *data, uint16_t size)
|
||||
{
|
||||
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||
|
||||
if (size > MAX_DATA_SIZE)
|
||||
return 1;
|
||||
|
||||
uint32_t i;
|
||||
uint32_t maxnum = ludp->connections[connection_id].successful_read + BUFFER_PACKET_NUM;
|
||||
uint32_t sent_packet = data_num - ludp->connections[connection_id].osent_packetnum;
|
||||
uint32_t maxnum = connection->successful_read + BUFFER_PACKET_NUM;
|
||||
uint32_t sent_packet = data_num - connection->osent_packetnum;
|
||||
|
||||
for (i = ludp->connections[connection_id].recv_packetnum; i != maxnum; ++i) {
|
||||
for (i = connection->recv_packetnum; i != maxnum; ++i) {
|
||||
if (i == data_num) {
|
||||
memcpy(ludp->connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].data, data, size);
|
||||
memcpy(connection->recvbuffer[i % MAX_QUEUE_NUM].data, data, size);
|
||||
|
||||
ludp->connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size = size;
|
||||
ludp->connections[connection_id].last_recvdata = current_time();
|
||||
connection->recvbuffer[i % MAX_QUEUE_NUM].size = size;
|
||||
connection->last_recvdata = current_time();
|
||||
|
||||
if (sent_packet < BUFFER_PACKET_NUM) {
|
||||
ludp->connections[connection_id].osent_packetnum = data_num;
|
||||
connection->osent_packetnum = data_num;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = ludp->connections[connection_id].recv_packetnum; i != maxnum; ++i) {
|
||||
if (ludp->connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size != 0)
|
||||
ludp->connections[connection_id].recv_packetnum = i;
|
||||
for (i = connection->recv_packetnum; i != maxnum; ++i) {
|
||||
if (connection->recvbuffer[i % MAX_QUEUE_NUM].size != 0)
|
||||
connection->recv_packetnum = i;
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
@ -696,13 +763,13 @@ static int add_recv(Lossless_UDP *ludp, int connection_id, uint32_t data_num, ui
|
|||
static int handle_data(void *object, IP_Port source, uint8_t *packet, uint32_t length)
|
||||
{
|
||||
Lossless_UDP *ludp = object;
|
||||
int connection = getconnection_id(ludp, source);
|
||||
|
||||
if (connection == -1)
|
||||
int connection_id = getconnection_id(ludp, source);
|
||||
if (connection_id == -1)
|
||||
return 1;
|
||||
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||
|
||||
/* Drop the data packet if connection is not connected. */
|
||||
if (ludp->connections[connection].status != 3)
|
||||
if (connection->status != 3)
|
||||
return 1;
|
||||
|
||||
if (length > 1 + 4 + MAX_DATA_SIZE || length < 1 + 4 + 1)
|
||||
|
@ -715,7 +782,7 @@ static int handle_data(void *object, IP_Port source, uint8_t *packet, uint32_t l
|
|||
memcpy(&temp, packet + 1, 4);
|
||||
number = ntohl(temp);
|
||||
|
||||
return add_recv(ludp, connection, number, packet + 5, size);
|
||||
return add_recv(ludp, connection_id, number, packet + 5, size);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -748,22 +815,20 @@ static void do_new(Lossless_UDP *ludp)
|
|||
uint32_t i;
|
||||
uint64_t temp_time = current_time();
|
||||
|
||||
for (i = 0; i < ludp->connections_length; ++i) {
|
||||
if (ludp->connections[i].status == 1)
|
||||
if ((ludp->connections[i].last_sent + (1000000UL / ludp->connections[i].SYNC_rate)) <= temp_time) {
|
||||
send_handshake(ludp, ludp->connections[i].ip_port, ludp->connections[i].handshake_id1, 0);
|
||||
ludp->connections[i].last_sent = temp_time;
|
||||
tox_array_for_each(&ludp->connections, Connection, tmp) {
|
||||
if (tmp.status == 1)
|
||||
if ((tmp.last_sent + (1000000UL / tmp.SYNC_rate)) <= temp_time) {
|
||||
send_handshake(ludp, tmp.ip_port, tmp.handshake_id1, 0);
|
||||
tmp.last_sent = temp_time;
|
||||
}
|
||||
|
||||
/* kill all timed out connections */
|
||||
if (ludp->connections[i].status > 0 &&
|
||||
(ludp->connections[i].last_recvSYNC + ludp->connections[i].timeout * 1000000UL) < temp_time &&
|
||||
ludp->connections[i].status != 4) {
|
||||
ludp->connections[i].status = 4;
|
||||
if (tmp.status > 0 && (tmp.last_recvSYNC + tmp.timeout * 1000000UL) < temp_time && tmp.status != 4) {
|
||||
tmp.status = 4;
|
||||
/* kill_connection(i); */
|
||||
}
|
||||
|
||||
if (ludp->connections[i].status > 0 && ludp->connections[i].killat < temp_time)
|
||||
if (tmp.status > 0 && tmp.killat < temp_time)
|
||||
kill_connection(ludp, i);
|
||||
}
|
||||
}
|
||||
|
@ -773,11 +838,11 @@ static void do_SYNC(Lossless_UDP *ludp)
|
|||
uint32_t i;
|
||||
uint64_t temp_time = current_time();
|
||||
|
||||
for (i = 0; i < ludp->connections_length; ++i) {
|
||||
if (ludp->connections[i].status == 2 || ludp->connections[i].status == 3)
|
||||
if ((ludp->connections[i].last_SYNC + (1000000UL / ludp->connections[i].SYNC_rate)) <= temp_time) {
|
||||
tox_array_for_each(&ludp->connections, Connection, tmp) {
|
||||
if (tmp.status == 2 || tmp.status == 3)
|
||||
if ((tmp.last_SYNC + (1000000UL / tmp.SYNC_rate)) <= temp_time) {
|
||||
send_SYNC(ludp, i);
|
||||
ludp->connections[i].last_SYNC = temp_time;
|
||||
tmp.last_SYNC = temp_time;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -788,14 +853,17 @@ static void do_data(Lossless_UDP *ludp)
|
|||
uint64_t j;
|
||||
uint64_t temp_time = current_time();
|
||||
|
||||
for (i = 0; i < ludp->connections_length; ++i)
|
||||
if (ludp->connections[i].status == 3 && sendqueue(ludp, i) != 0)
|
||||
if ((ludp->connections[i].last_sent + (1000000UL / ludp->connections[i].data_rate)) <= temp_time) {
|
||||
for (j = ludp->connections[i].last_sent; j < temp_time; j += (1000000UL / ludp->connections[i].data_rate))
|
||||
send_DATA(ludp, i);
|
||||
tox_array_for_each(&ludp->connections, Connection, tmp) {
|
||||
if (tmp.status == 3 && sendqueue(ludp, i) != 0 &&
|
||||
(tmp.last_sent + (1000000UL / tmp.data_rate)) <= temp_time)
|
||||
{
|
||||
for (j = tmp.last_sent; j < temp_time; j += (1000000UL / tmp.data_rate))
|
||||
send_DATA(ludp, i);
|
||||
|
||||
ludp->connections[i].last_sent = temp_time;
|
||||
}
|
||||
tmp.last_sent = temp_time;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#define MAX_SYNC_RATE 10
|
||||
|
@ -810,18 +878,18 @@ static void adjust_rates(Lossless_UDP *ludp)
|
|||
uint32_t i;
|
||||
uint64_t temp_time = current_time();
|
||||
|
||||
for (i = 0; i < ludp->connections_length; ++i) {
|
||||
if (ludp->connections[i].status == 1 || ludp->connections[i].status == 2)
|
||||
ludp->connections[i].SYNC_rate = MAX_SYNC_RATE;
|
||||
tox_array_for_each(&ludp->connections, Connection, tmp) {
|
||||
if (tmp.status == 1 || tmp.status == 2)
|
||||
tmp.SYNC_rate = MAX_SYNC_RATE;
|
||||
|
||||
if (ludp->connections[i].status == 3) {
|
||||
if (tmp.status == 3) {
|
||||
if (sendqueue(ludp, i) != 0) {
|
||||
ludp->connections[i].data_rate = (BUFFER_PACKET_NUM - ludp->connections[i].num_req_paquets) * MAX_SYNC_RATE;
|
||||
ludp->connections[i].SYNC_rate = MAX_SYNC_RATE;
|
||||
} else if (ludp->connections[i].last_recvdata + 1000000UL > temp_time)
|
||||
ludp->connections[i].SYNC_rate = MAX_SYNC_RATE;
|
||||
tmp.data_rate = (BUFFER_PACKET_NUM - tmp.num_req_paquets) * MAX_SYNC_RATE;
|
||||
tmp.SYNC_rate = MAX_SYNC_RATE;
|
||||
} else if (tmp.last_recvdata + 1000000UL > temp_time)
|
||||
tmp.SYNC_rate = MAX_SYNC_RATE;
|
||||
else
|
||||
ludp->connections[i].SYNC_rate = SYNC_RATE;
|
||||
tmp.SYNC_rate = SYNC_RATE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -837,6 +905,6 @@ void do_lossless_udp(Lossless_UDP *ludp)
|
|||
|
||||
void kill_lossless_udp(Lossless_UDP *ludp)
|
||||
{
|
||||
free(ludp->connections);
|
||||
tox_array_delete(&ludp->connections);
|
||||
free(ludp);
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
#define LOSSLESS_UDP_H
|
||||
|
||||
#include "network.h"
|
||||
#include "../testing/misc_tools.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
|
@ -119,10 +120,12 @@ typedef struct {
|
|||
|
||||
typedef struct {
|
||||
Networking_Core *net;
|
||||
Connection *connections;
|
||||
|
||||
uint32_t connections_length; /* Length of connections array */
|
||||
uint32_t connections_number; /* Number of connections in connections array */
|
||||
tox_array connections;
|
||||
//kk Connection *connections;
|
||||
|
||||
//kk uint32_t connections_length; /* Length of connections array */
|
||||
//kk uint32_t connections_number; /* Number of connections in connections array */
|
||||
|
||||
/* table of random numbers used in handshake_id. */
|
||||
uint32_t randtable[6][256];
|
||||
|
|
Loading…
Reference in New Issue
Block a user