mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Code cleanups.
This commit is contained in:
parent
97f192969c
commit
6ef2b5ead6
|
@ -202,12 +202,12 @@ int main(int argc, char *argv[])
|
|||
networking_poll(ludp->net);
|
||||
do_lossless_udp(ludp);
|
||||
|
||||
if (is_connected(ludp, connection) == 3) {
|
||||
if (is_connected(ludp, connection) == LUDP_ESTABLISHED) {
|
||||
printf("Connecting took: %llu us\n", (unsigned long long)(current_time() - timer));
|
||||
break;
|
||||
}
|
||||
|
||||
if (is_connected(ludp, connection) == 0) {
|
||||
if (is_connected(ludp, connection) == LUDP_NO_CONNECTION) {
|
||||
printf("Connection timeout after: %llu us\n", (unsigned long long)(current_time() - timer));
|
||||
return 1;
|
||||
}
|
||||
|
@ -226,7 +226,7 @@ int main(int argc, char *argv[])
|
|||
networking_poll(ludp->net);
|
||||
do_lossless_udp(ludp);
|
||||
|
||||
if (is_connected(ludp, connection) == 3) {
|
||||
if (is_connected(ludp, connection) == LUDP_ESTABLISHED) {
|
||||
|
||||
while (write_packet(ludp, connection, buffer, read)) {
|
||||
bytes_sent += read;
|
||||
|
|
|
@ -188,7 +188,7 @@ int main(int argc, char *argv[])
|
|||
connection = incoming_connection(ludp, 0);
|
||||
|
||||
if (connection != -1) {
|
||||
if (is_connected(ludp, connection) == 2) {
|
||||
if (is_connected(ludp, connection) == LUDP_NOT_CONFIRMED) {
|
||||
printf("Received the connection.\n");
|
||||
|
||||
}
|
||||
|
@ -205,7 +205,7 @@ int main(int argc, char *argv[])
|
|||
//printconnection(0);
|
||||
networking_poll(ludp->net);
|
||||
|
||||
if (is_connected(ludp, connection) >= 2) {
|
||||
if (is_connected(ludp, connection) >= LUDP_NOT_CONFIRMED) {
|
||||
confirm_connection(ludp, connection);
|
||||
|
||||
while (1) {
|
||||
|
@ -223,7 +223,7 @@ int main(int argc, char *argv[])
|
|||
|
||||
do_lossless_udp(ludp);
|
||||
|
||||
if (is_connected(ludp, connection) == 4) {
|
||||
if (is_connected(ludp, connection) == LUDP_TIMED_OUT) {
|
||||
printf("Server Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer));
|
||||
fclose(file);
|
||||
return 1;
|
||||
|
|
|
@ -44,7 +44,7 @@
|
|||
int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port)
|
||||
{
|
||||
tox_array_for_each(&ludp->connections, Connection, tmp) {
|
||||
if (tmp->status > 0 && ipport_equal(&tmp->ip_port, &ip_port)) {
|
||||
if (tmp->status != LUDP_NO_CONNECTION && ipport_equal(&tmp->ip_port, &ip_port)) {
|
||||
return tmp_i;
|
||||
}
|
||||
}
|
||||
|
@ -191,7 +191,7 @@ int new_connection(Lossless_UDP *ludp, IP_Port ip_port)
|
|||
}
|
||||
|
||||
tox_array_for_each(&ludp->connections, Connection, tmp) {
|
||||
if (tmp->status == 0) {
|
||||
if (tmp->status == LUDP_NO_CONNECTION) {
|
||||
connection_id = tmp_i;
|
||||
break;
|
||||
}
|
||||
|
@ -214,7 +214,7 @@ int new_connection(Lossless_UDP *ludp, IP_Port ip_port)
|
|||
|
||||
*connection = (Connection) {
|
||||
.ip_port = ip_port,
|
||||
.status = 1,
|
||||
.status = LUDP_HANDSHAKE_SENDING,
|
||||
.inbound = 0,
|
||||
.handshake_id1 = handshake_id1,
|
||||
.sent_packetnum = handshake_id1,
|
||||
|
@ -255,7 +255,7 @@ static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port)
|
|||
|
||||
int connection_id = -1;
|
||||
tox_array_for_each(&ludp->connections, Connection, tmp) {
|
||||
if (tmp->status == 0) {
|
||||
if (tmp->status == LUDP_NO_CONNECTION) {
|
||||
connection_id = tmp_i;
|
||||
break;
|
||||
}
|
||||
|
@ -275,7 +275,7 @@ static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port)
|
|||
|
||||
*connection = (Connection) {
|
||||
.ip_port = ip_port,
|
||||
.status = 2,
|
||||
.status = LUDP_NOT_CONFIRMED,
|
||||
.inbound = 2,
|
||||
.SYNC_rate = SYNC_RATE,
|
||||
.data_rate = DATA_SYNC_RATE,
|
||||
|
@ -324,7 +324,7 @@ static void free_connections(Lossless_UDP *ludp)
|
|||
for (i = ludp->connections.len; i != 0; --i) {
|
||||
Connection *connection = &tox_array_get(&ludp->connections, i - 1, Connection);
|
||||
|
||||
if (connection->status != 0)
|
||||
if (connection->status != LUDP_NO_CONNECTION)
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -341,8 +341,8 @@ int kill_connection(Lossless_UDP *ludp, int connection_id)
|
|||
if ((unsigned int)connection_id < ludp->connections.len) {
|
||||
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||
|
||||
if (connection->status > 0) {
|
||||
connection->status = 0;
|
||||
if (connection->status != LUDP_NO_CONNECTION) {
|
||||
connection->status = LUDP_NO_CONNECTION;
|
||||
change_handshake(ludp, connection->ip_port);
|
||||
free(connection->sendbuffer);
|
||||
free(connection->recvbuffer);
|
||||
|
@ -366,7 +366,7 @@ int timeout_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t second
|
|||
if ((unsigned int)connection_id < ludp->connections.len) {
|
||||
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||
|
||||
if (connection->status > 0) {
|
||||
if (connection->status != LUDP_NO_CONNECTION) {
|
||||
connection->killat = current_time() + 1000000UL * seconds;
|
||||
return 0;
|
||||
}
|
||||
|
@ -378,11 +378,11 @@ int timeout_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t second
|
|||
/*
|
||||
* Check if connection is connected:
|
||||
*
|
||||
* return 0 if not.
|
||||
* return 1 if attempting handshake.
|
||||
* return 2 if handshake is done.
|
||||
* return 3 if fully connected.
|
||||
* return 4 if timed out and waiting to be killed.
|
||||
* return LUDP_NO_CONNECTION if not.
|
||||
* return LUDP_HANDSHAKE_SENDING if attempting handshake.
|
||||
* return LUDP_NOT_CONFIRMED if handshake is done.
|
||||
* return LUDP_ESTABLISHED if fully connected.
|
||||
* return LUDP_TIMED_OUT if timed out and waiting to be killed.
|
||||
*/
|
||||
int is_connected(Lossless_UDP *ludp, int connection_id)
|
||||
{
|
||||
|
@ -404,7 +404,7 @@ int connection_confirmed(Lossless_UDP *ludp, int connection_id)
|
|||
|
||||
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||
|
||||
if (connection->status == 0)
|
||||
if (connection->status == LUDP_NO_CONNECTION)
|
||||
return 0;
|
||||
|
||||
if (connection->confirmed == 1)
|
||||
|
@ -426,7 +426,7 @@ int confirm_connection(Lossless_UDP *ludp, int connection_id)
|
|||
|
||||
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||
|
||||
if (connection->status == 0)
|
||||
if (connection->status == LUDP_NO_CONNECTION)
|
||||
return -1;
|
||||
|
||||
connection->killat = ~0;
|
||||
|
@ -455,7 +455,7 @@ uint32_t sendqueue(Lossless_UDP *ludp, int connection_id)
|
|||
|
||||
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||
|
||||
if (connection->status == 0)
|
||||
if (connection->status == LUDP_NO_CONNECTION)
|
||||
return 0;
|
||||
|
||||
return connection->sendbuff_packetnum - connection->successful_sent;
|
||||
|
@ -469,7 +469,7 @@ uint32_t recvqueue(Lossless_UDP *ludp, int connection_id)
|
|||
|
||||
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||
|
||||
if (connection->status == 0)
|
||||
if (connection->status == LUDP_NO_CONNECTION)
|
||||
return 0;
|
||||
|
||||
return connection->recv_packetnum - connection->successful_read;
|
||||
|
@ -485,7 +485,7 @@ char id_packet(Lossless_UDP *ludp, int connection_id)
|
|||
|
||||
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||
|
||||
if (connection->status != 0)
|
||||
if (connection->status != LUDP_NO_CONNECTION)
|
||||
return connection->recvbuffer[connection->successful_read % connection->recvbuffer_length].data[0];
|
||||
|
||||
return -1;
|
||||
|
@ -501,7 +501,7 @@ int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data)
|
|||
|
||||
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||
|
||||
if (connection->status == 0)
|
||||
if (connection->status == LUDP_NO_CONNECTION)
|
||||
return 0;
|
||||
|
||||
uint16_t index = connection->successful_read % connection->recvbuffer_length;
|
||||
|
@ -523,7 +523,7 @@ int read_packet_silent(Lossless_UDP *ludp, int connection_id, uint8_t *data)
|
|||
|
||||
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||
|
||||
if (connection->status == 0)
|
||||
if (connection->status == LUDP_NO_CONNECTION)
|
||||
return 0;
|
||||
|
||||
uint16_t index = connection->successful_read % connection->recvbuffer_length;
|
||||
|
@ -583,7 +583,7 @@ int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t
|
|||
|
||||
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||
|
||||
if (connection->status == 0)
|
||||
if (connection->status == LUDP_NO_CONNECTION)
|
||||
return 0;
|
||||
|
||||
if (length > MAX_DATA_SIZE || length == 0 || sendqueue(ludp, connection_id) >= MAX_QUEUE_NUM)
|
||||
|
@ -760,19 +760,20 @@ static int handle_handshake(void *object, IP_Port source, uint8_t *packet, uint3
|
|||
handshake_id2 = ntohl(temp);
|
||||
|
||||
|
||||
if (handshake_id2 == 0 && is_connected(ludp, connection_id) < 3) {
|
||||
if (handshake_id2 == 0 && is_connected(ludp, connection_id) != LUDP_ESTABLISHED &&
|
||||
is_connected(ludp, connection_id) != LUDP_TIMED_OUT) {
|
||||
send_handshake(ludp, source, handshake_id(ludp, source), handshake_id1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (is_connected(ludp, connection_id) != 1)
|
||||
if (is_connected(ludp, connection_id) != LUDP_HANDSHAKE_SENDING)
|
||||
return 1;
|
||||
|
||||
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||
|
||||
/* if handshake_id2 is what we sent previously as handshake_id1 */
|
||||
if (handshake_id2 == connection->handshake_id1) {
|
||||
connection->status = 2;
|
||||
connection->status = LUDP_NOT_CONFIRMED;
|
||||
/* NOTE: is this necessary?
|
||||
connection->handshake_id2 = handshake_id1; */
|
||||
connection->orecv_packetnum = handshake_id2;
|
||||
|
@ -830,7 +831,7 @@ static int handle_SYNC2(Lossless_UDP *ludp, int connection_id, uint8_t counter,
|
|||
|
||||
if (recv_packetnum == connection->orecv_packetnum) {
|
||||
/* && sent_packetnum == connection->osent_packetnum) */
|
||||
connection->status = 3;
|
||||
connection->status = LUDP_ESTABLISHED;
|
||||
connection->recv_counter = counter;
|
||||
++connection->send_counter;
|
||||
send_SYNC(ludp, connection_id);
|
||||
|
@ -938,11 +939,11 @@ static int handle_SYNC(void *object, IP_Port source, uint8_t *packet, uint32_t l
|
|||
|
||||
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||
|
||||
if (connection->status == 2)
|
||||
if (connection->status == LUDP_NOT_CONFIRMED)
|
||||
return handle_SYNC2(ludp, connection_id, counter,
|
||||
recv_packetnum, sent_packetnum);
|
||||
|
||||
if (connection->status == 3)
|
||||
if (connection->status == LUDP_ESTABLISHED)
|
||||
return handle_SYNC3(ludp, connection_id, counter, recv_packetnum,
|
||||
sent_packetnum, req_packets, number);
|
||||
|
||||
|
@ -1017,7 +1018,7 @@ static int handle_data(void *object, IP_Port source, uint8_t *packet, uint32_t l
|
|||
if (connection_id == -1)
|
||||
return 1;
|
||||
|
||||
if (tox_array_get(&ludp->connections, connection_id, Connection).status != 3)
|
||||
if (tox_array_get(&ludp->connections, connection_id, Connection).status != LUDP_ESTABLISHED)
|
||||
return 1;
|
||||
|
||||
if (length > 1 + 4 + MAX_DATA_SIZE || length < 1 + 4 + 1)
|
||||
|
@ -1065,19 +1066,20 @@ static void do_new(Lossless_UDP *ludp)
|
|||
uint64_t temp_time = current_time();
|
||||
|
||||
tox_array_for_each(&ludp->connections, Connection, tmp) {
|
||||
if (tmp->status == 1 && (tmp->last_sent + (1000000UL / tmp->SYNC_rate)) <= temp_time) {
|
||||
if (tmp->status == LUDP_HANDSHAKE_SENDING && (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 (tmp->status > 0 && (tmp->last_recvSYNC + tmp->timeout * 1000000UL) < temp_time && tmp->status != 4) {
|
||||
tmp->status = 4;
|
||||
if (tmp->status != LUDP_NO_CONNECTION && (tmp->last_recvSYNC + tmp->timeout * 1000000UL) < temp_time
|
||||
&& tmp->status != LUDP_TIMED_OUT) {
|
||||
tmp->status = LUDP_TIMED_OUT;
|
||||
/* kill_connection(i); */
|
||||
}
|
||||
|
||||
if (tmp->status > 0 && tmp->killat < temp_time)
|
||||
tmp->status = 4;
|
||||
if (tmp->status != LUDP_NO_CONNECTION && tmp->killat < temp_time)
|
||||
tmp->status = LUDP_TIMED_OUT;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1086,7 +1088,7 @@ static void do_SYNC(Lossless_UDP *ludp)
|
|||
uint64_t temp_time = current_time();
|
||||
|
||||
tox_array_for_each(&ludp->connections, Connection, tmp) {
|
||||
if (tmp->status == 2 || tmp->status == 3)
|
||||
if (tmp->status == LUDP_NOT_CONFIRMED || tmp->status == LUDP_ESTABLISHED)
|
||||
if ((tmp->last_SYNC + (1000000UL / tmp->SYNC_rate)) <= temp_time) {
|
||||
send_SYNC(ludp, tmp_i);
|
||||
tmp->last_SYNC = temp_time;
|
||||
|
@ -1100,7 +1102,7 @@ static void do_data(Lossless_UDP *ludp)
|
|||
uint64_t temp_time = current_time();
|
||||
|
||||
tox_array_for_each(&ludp->connections, Connection, tmp) {
|
||||
if (tmp->status == 3 && sendqueue(ludp, tmp_i) != 0 &&
|
||||
if (tmp->status == LUDP_ESTABLISHED && sendqueue(ludp, tmp_i) != 0 &&
|
||||
(tmp->last_sent + (1000000UL / tmp->data_rate)) <= temp_time) {
|
||||
for (j = tmp->last_sent; j < temp_time; j += (1000000UL / tmp->data_rate))
|
||||
if (send_DATA(ludp, tmp_i) <= 0)
|
||||
|
@ -1124,10 +1126,10 @@ static void adjust_rates(Lossless_UDP *ludp)
|
|||
uint64_t temp_time = current_time();
|
||||
|
||||
tox_array_for_each(&ludp->connections, Connection, tmp) {
|
||||
if (tmp->status == 1 || tmp->status == 2)
|
||||
if (tmp->status == LUDP_HANDSHAKE_SENDING || tmp->status == LUDP_NOT_CONFIRMED)
|
||||
tmp->SYNC_rate = MAX_SYNC_RATE;
|
||||
|
||||
if (tmp->status == 3) {
|
||||
if (tmp->status == LUDP_ESTABLISHED) {
|
||||
if (sendqueue(ludp, tmp_i) != 0) {
|
||||
tmp->SYNC_rate = MAX_SYNC_RATE;
|
||||
} else if (tmp->last_recvdata + 200000UL > temp_time) { /* 200 ms */
|
||||
|
|
|
@ -52,15 +52,21 @@ typedef struct {
|
|||
uint16_t size;
|
||||
} Data;
|
||||
|
||||
#define LUDP_NO_CONNECTION 0
|
||||
#define LUDP_HANDSHAKE_SENDING 1
|
||||
#define LUDP_NOT_CONFIRMED 2
|
||||
#define LUDP_ESTABLISHED 3
|
||||
#define LUDP_TIMED_OUT 4
|
||||
|
||||
typedef struct {
|
||||
IP_Port ip_port;
|
||||
|
||||
/*
|
||||
* return 0 if connection is dead.
|
||||
* return 1 if attempting handshake.
|
||||
* return 2 if handshake is done (we start sending SYNC packets).
|
||||
* return 3 if we are sending SYNC packets and can send data.
|
||||
* return 4 if the connection has timed out.
|
||||
* return LUDP_NO_CONNECTION if connection is dead.
|
||||
* return LUDP_HANDSHAKE_SENDING if attempting handshake.
|
||||
* return LUDP_NOT_CONFIRMED if handshake is done (we start sending SYNC packets).
|
||||
* return LUDP_ESTABLISHED if we are sending SYNC packets and can send data.
|
||||
* return LUDP_TIMED_OUT if the connection has timed out.
|
||||
*/
|
||||
uint8_t status;
|
||||
|
||||
|
@ -238,11 +244,11 @@ uint32_t recvqueue(Lossless_UDP *ludp, int connection_id);
|
|||
|
||||
/* Check if connection is connected:
|
||||
*
|
||||
* return 0 not.
|
||||
* return 1 if attempting handshake.
|
||||
* return 2 if handshake is done.
|
||||
* return 3 if fully connected.
|
||||
* return 4 if timed out and wating to be killed.
|
||||
* return LUDP_NO_CONNECTION if not.
|
||||
* return LUDP_HANDSHAKE_SENDING if attempting handshake.
|
||||
* return LUDP_NOT_CONFIRMED if handshake is done.
|
||||
* return LUDP_ESTABLISHED if fully connected.
|
||||
* return LUDP_TIMED_OUT if timed out and wating to be killed.
|
||||
*/
|
||||
int is_connected(Lossless_UDP *ludp, int connection_id);
|
||||
|
||||
|
|
|
@ -519,7 +519,7 @@ int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, ui
|
|||
int incoming_con = incoming_connection(c->lossless_udp, 1);
|
||||
|
||||
if (incoming_con != -1) {
|
||||
if (is_connected(c->lossless_udp, incoming_con) == 4) {
|
||||
if (is_connected(c->lossless_udp, incoming_con) == LUDP_TIMED_OUT) {
|
||||
kill_connection(c->lossless_udp, incoming_con);
|
||||
continue;
|
||||
}
|
||||
|
@ -782,7 +782,7 @@ static void kill_timedout(Net_Crypto *c)
|
|||
|
||||
for (i = 0; i < c->crypto_connections_length; ++i) {
|
||||
if (c->crypto_connections[i].status != CONN_NO_CONNECTION
|
||||
&& is_connected(c->lossless_udp, c->crypto_connections[i].number) == 4)
|
||||
&& is_connected(c->lossless_udp, c->crypto_connections[i].number) == LUDP_TIMED_OUT)
|
||||
c->crypto_connections[i].status = CONN_TIMED_OUT;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user