mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
cleanup: Minor cleanups in TCP_connection.c.
Nothing very noteworthy, I just came across this and made it slightly more readable. I'm not making this function `bool` right now because it's used in NGC and that will break.
This commit is contained in:
parent
2f10cb1730
commit
d15d9fa72d
|
@ -1 +1 @@
|
||||||
2e5b749cd2a911c127e197d3791527a9b13f5b7a5a5f6cbdea80cc199707156d /usr/local/bin/tox-bootstrapd
|
d67f642c84a92d92c799aa1d0bb9e0c2727ee67e565eb88dfa7a49f7e163cb82 /usr/local/bin/tox-bootstrapd
|
||||||
|
|
|
@ -308,7 +308,7 @@ int send_packet_tcp_connection(const TCP_Connections *tcp_c, int connections_num
|
||||||
|
|
||||||
bool limit_reached = false;
|
bool limit_reached = false;
|
||||||
|
|
||||||
for (unsigned int i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
|
for (uint32_t i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
|
||||||
uint32_t tcp_con_num = con_to->connections[i].tcp_connection;
|
uint32_t tcp_con_num = con_to->connections[i].tcp_connection;
|
||||||
const uint8_t status = con_to->connections[i].status;
|
const uint8_t status = con_to->connections[i].status;
|
||||||
const uint8_t connection_id = con_to->connections[i].connection_id;
|
const uint8_t connection_id = con_to->connections[i].connection_id;
|
||||||
|
@ -337,36 +337,32 @@ int send_packet_tcp_connection(const TCP_Connections *tcp_c, int connections_num
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!limit_reached) {
|
if (limit_reached) {
|
||||||
ret = 0;
|
|
||||||
|
|
||||||
/* Send oob packets to all relays tied to the connection. */
|
|
||||||
for (unsigned int i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
|
|
||||||
uint32_t tcp_con_num = con_to->connections[i].tcp_connection;
|
|
||||||
uint8_t status = con_to->connections[i].status;
|
|
||||||
|
|
||||||
if (tcp_con_num > 0 && status == TCP_CONNECTIONS_STATUS_REGISTERED) {
|
|
||||||
tcp_con_num -= 1;
|
|
||||||
TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_con_num);
|
|
||||||
|
|
||||||
if (tcp_con == nullptr) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (send_oob_packet(tcp_c->logger, tcp_con->connection, con_to->public_key, packet, length) == 1) {
|
|
||||||
ret += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ret >= 1) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return -1;
|
bool sent_any = false;
|
||||||
|
|
||||||
|
/* Send oob packets to all relays tied to the connection. */
|
||||||
|
for (uint32_t i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
|
||||||
|
uint32_t tcp_con_num = con_to->connections[i].tcp_connection;
|
||||||
|
const uint8_t status = con_to->connections[i].status;
|
||||||
|
|
||||||
|
if (tcp_con_num > 0 && status == TCP_CONNECTIONS_STATUS_REGISTERED) {
|
||||||
|
tcp_con_num -= 1;
|
||||||
|
TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_con_num);
|
||||||
|
|
||||||
|
if (tcp_con == nullptr) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (send_oob_packet(tcp_c->logger, tcp_con->connection, con_to->public_key, packet, length) == 1) {
|
||||||
|
sent_any = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sent_any ? 0 : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @brief Return a random TCP connection number for use in send_tcp_onion_request.
|
/** @brief Return a random TCP connection number for use in send_tcp_onion_request.
|
||||||
|
@ -492,7 +488,7 @@ void set_onion_packet_tcp_connection_callback(TCP_Connections *tcp_c, tcp_onion_
|
||||||
non_null()
|
non_null()
|
||||||
static int find_tcp_connection_to(const TCP_Connections *tcp_c, const uint8_t *public_key)
|
static int find_tcp_connection_to(const TCP_Connections *tcp_c, const uint8_t *public_key)
|
||||||
{
|
{
|
||||||
for (unsigned int i = 0; i < tcp_c->connections_length; ++i) {
|
for (uint32_t i = 0; i < tcp_c->connections_length; ++i) {
|
||||||
const TCP_Connection_to *con_to = get_connection(tcp_c, i);
|
const TCP_Connection_to *con_to = get_connection(tcp_c, i);
|
||||||
|
|
||||||
if (con_to != nullptr) {
|
if (con_to != nullptr) {
|
||||||
|
@ -573,7 +569,7 @@ int kill_tcp_connection_to(TCP_Connections *tcp_c, int connections_number)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (unsigned int i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
|
for (uint32_t i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
|
||||||
if (con_to->connections[i].tcp_connection > 0) {
|
if (con_to->connections[i].tcp_connection > 0) {
|
||||||
const unsigned int tcp_connections_number = con_to->connections[i].tcp_connection - 1;
|
const unsigned int tcp_connections_number = con_to->connections[i].tcp_connection - 1;
|
||||||
TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
|
TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
|
||||||
|
@ -623,7 +619,7 @@ int set_tcp_connection_to_status(const TCP_Connections *tcp_c, int connections_n
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (unsigned int i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
|
for (uint32_t i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
|
||||||
if (con_to->connections[i].tcp_connection > 0) {
|
if (con_to->connections[i].tcp_connection > 0) {
|
||||||
const unsigned int tcp_connections_number = con_to->connections[i].tcp_connection - 1;
|
const unsigned int tcp_connections_number = con_to->connections[i].tcp_connection - 1;
|
||||||
TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
|
TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
|
||||||
|
@ -647,7 +643,7 @@ int set_tcp_connection_to_status(const TCP_Connections *tcp_c, int connections_n
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (unsigned int i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
|
for (uint32_t i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
|
||||||
if (con_to->connections[i].tcp_connection > 0) {
|
if (con_to->connections[i].tcp_connection > 0) {
|
||||||
unsigned int tcp_connections_number = con_to->connections[i].tcp_connection - 1;
|
unsigned int tcp_connections_number = con_to->connections[i].tcp_connection - 1;
|
||||||
TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
|
TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
|
||||||
|
@ -669,7 +665,7 @@ int set_tcp_connection_to_status(const TCP_Connections *tcp_c, int connections_n
|
||||||
non_null()
|
non_null()
|
||||||
static bool tcp_connection_in_conn(const TCP_Connection_to *con_to, unsigned int tcp_connections_number)
|
static bool tcp_connection_in_conn(const TCP_Connection_to *con_to, unsigned int tcp_connections_number)
|
||||||
{
|
{
|
||||||
for (unsigned int i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
|
for (uint32_t i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
|
||||||
if (con_to->connections[i].tcp_connection == (tcp_connections_number + 1)) {
|
if (con_to->connections[i].tcp_connection == (tcp_connections_number + 1)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -689,7 +685,7 @@ static int add_tcp_connection_to_conn(TCP_Connection_to *con_to, unsigned int tc
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (unsigned int i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
|
for (uint32_t i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
|
||||||
if (con_to->connections[i].tcp_connection == 0) {
|
if (con_to->connections[i].tcp_connection == 0) {
|
||||||
con_to->connections[i].tcp_connection = tcp_connections_number + 1;
|
con_to->connections[i].tcp_connection = tcp_connections_number + 1;
|
||||||
con_to->connections[i].status = TCP_CONNECTIONS_STATUS_NONE;
|
con_to->connections[i].status = TCP_CONNECTIONS_STATUS_NONE;
|
||||||
|
@ -708,7 +704,7 @@ static int add_tcp_connection_to_conn(TCP_Connection_to *con_to, unsigned int tc
|
||||||
non_null()
|
non_null()
|
||||||
static int rm_tcp_connection_from_conn(TCP_Connection_to *con_to, unsigned int tcp_connections_number)
|
static int rm_tcp_connection_from_conn(TCP_Connection_to *con_to, unsigned int tcp_connections_number)
|
||||||
{
|
{
|
||||||
for (unsigned int i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
|
for (uint32_t i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
|
||||||
if (con_to->connections[i].tcp_connection == (tcp_connections_number + 1)) {
|
if (con_to->connections[i].tcp_connection == (tcp_connections_number + 1)) {
|
||||||
con_to->connections[i].tcp_connection = 0;
|
con_to->connections[i].tcp_connection = 0;
|
||||||
con_to->connections[i].status = TCP_CONNECTIONS_STATUS_NONE;
|
con_to->connections[i].status = TCP_CONNECTIONS_STATUS_NONE;
|
||||||
|
@ -749,7 +745,7 @@ static int set_tcp_connection_status(TCP_Connection_to *con_to, unsigned int tcp
|
||||||
uint8_t status,
|
uint8_t status,
|
||||||
uint8_t connection_id)
|
uint8_t connection_id)
|
||||||
{
|
{
|
||||||
for (unsigned int i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
|
for (uint32_t i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
|
||||||
if (con_to->connections[i].tcp_connection == (tcp_connections_number + 1)) {
|
if (con_to->connections[i].tcp_connection == (tcp_connections_number + 1)) {
|
||||||
|
|
||||||
if (con_to->connections[i].status == status) {
|
if (con_to->connections[i].status == status) {
|
||||||
|
@ -778,7 +774,7 @@ int kill_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connections_number
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (unsigned int i = 0; i < tcp_c->connections_length; ++i) {
|
for (uint32_t i = 0; i < tcp_c->connections_length; ++i) {
|
||||||
TCP_Connection_to *con_to = get_connection(tcp_c, i);
|
TCP_Connection_to *con_to = get_connection(tcp_c, i);
|
||||||
|
|
||||||
if (con_to != nullptr) {
|
if (con_to != nullptr) {
|
||||||
|
@ -820,7 +816,7 @@ static int reconnect_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connec
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (unsigned int i = 0; i < tcp_c->connections_length; ++i) {
|
for (uint32_t i = 0; i < tcp_c->connections_length; ++i) {
|
||||||
TCP_Connection_to *con_to = get_connection(tcp_c, i);
|
TCP_Connection_to *con_to = get_connection(tcp_c, i);
|
||||||
|
|
||||||
if (con_to != nullptr) {
|
if (con_to != nullptr) {
|
||||||
|
@ -865,7 +861,7 @@ static int sleep_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connection
|
||||||
kill_TCP_connection(tcp_con->connection);
|
kill_TCP_connection(tcp_con->connection);
|
||||||
tcp_con->connection = nullptr;
|
tcp_con->connection = nullptr;
|
||||||
|
|
||||||
for (unsigned int i = 0; i < tcp_c->connections_length; ++i) {
|
for (uint32_t i = 0; i < tcp_c->connections_length; ++i) {
|
||||||
TCP_Connection_to *con_to = get_connection(tcp_c, i);
|
TCP_Connection_to *con_to = get_connection(tcp_c, i);
|
||||||
|
|
||||||
if (con_to != nullptr) {
|
if (con_to != nullptr) {
|
||||||
|
@ -1131,7 +1127,7 @@ static int tcp_relay_on_online(TCP_Connections *tcp_c, int tcp_connections_numbe
|
||||||
|
|
||||||
bool sent_any = false;
|
bool sent_any = false;
|
||||||
|
|
||||||
for (unsigned int i = 0; i < tcp_c->connections_length; ++i) {
|
for (uint32_t i = 0; i < tcp_c->connections_length; ++i) {
|
||||||
TCP_Connection_to *con_to = get_connection(tcp_c, i);
|
TCP_Connection_to *con_to = get_connection(tcp_c, i);
|
||||||
|
|
||||||
if (con_to != nullptr) {
|
if (con_to != nullptr) {
|
||||||
|
|
|
@ -857,7 +857,6 @@ bool tox_add_tcp_relay(Tox *tox, const char *host, uint16_t port, const uint8_t
|
||||||
lock(tox);
|
lock(tox);
|
||||||
assert(count >= 0);
|
assert(count >= 0);
|
||||||
|
|
||||||
LOGGER_DEBUG(tox->m->log, "count: %d", count);
|
|
||||||
for (int32_t i = 0; i < count; ++i) {
|
for (int32_t i = 0; i < count; ++i) {
|
||||||
root[i].port = net_htons(port);
|
root[i].port = net_htons(port);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user