diff options
-rw-r--r-- | toxcore/network.c | 33 | ||||
-rw-r--r-- | toxcore/network.h | 2 |
2 files changed, 17 insertions, 18 deletions
diff --git a/toxcore/network.c b/toxcore/network.c index 5d695b81..3abd71e0 100644 --- a/toxcore/network.c +++ b/toxcore/network.c | |||
@@ -212,12 +212,14 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, uint8_t *data, uint32_t le | |||
212 | 212 | ||
213 | if (res == length) | 213 | if (res == length) |
214 | net->send_fail_eagain = 0; | 214 | net->send_fail_eagain = 0; |
215 | |||
215 | #ifdef WIN32 | 216 | #ifdef WIN32 |
216 | else if ((res < 0) && (errno == WSAEWOULDBLOCK)) | 217 | else if ((res < 0) && (errno == WSAEWOULDBLOCK)) |
217 | #else | 218 | #else |
218 | else if ((res < 0) && (errno == EAGAIN)) | 219 | else if ((res < 0) && (errno == EAGAIN)) |
219 | #endif | 220 | #endif |
220 | net->send_fail_eagain = current_time(); | 221 | net->send_fail_eagain = current_time(); |
222 | |||
221 | return res; | 223 | return res; |
222 | } | 224 | } |
223 | 225 | ||
@@ -227,7 +229,7 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, uint8_t *data, uint32_t le | |||
227 | * Packet length is put into length. | 229 | * Packet length is put into length. |
228 | * Dump all empty packets. | 230 | * Dump all empty packets. |
229 | */ | 231 | */ |
230 | int receivepacket(sock_t sock, IP_Port *ip_port, uint8_t *data, uint32_t *length) | 232 | static int receivepacket(sock_t sock, IP_Port *ip_port, uint8_t *data, uint32_t *length) |
231 | { | 233 | { |
232 | struct sockaddr_storage addr; | 234 | struct sockaddr_storage addr; |
233 | #ifdef WIN32 | 235 | #ifdef WIN32 |
@@ -315,8 +317,7 @@ void networking_poll(Networking_Core *net) | |||
315 | /* | 317 | /* |
316 | * function to avoid excessive polling | 318 | * function to avoid excessive polling |
317 | */ | 319 | */ |
318 | typedef struct | 320 | typedef struct { |
319 | { | ||
320 | sock_t sock; | 321 | sock_t sock; |
321 | uint32_t sendqueue_length; | 322 | uint32_t sendqueue_length; |
322 | uint16_t send_fail_reset; | 323 | uint16_t send_fail_reset; |
@@ -325,8 +326,7 @@ typedef struct | |||
325 | 326 | ||
326 | int networking_wait_prepare(Networking_Core *net, uint32_t sendqueue_length, uint8_t *data, uint16_t *lenptr) | 327 | int networking_wait_prepare(Networking_Core *net, uint32_t sendqueue_length, uint8_t *data, uint16_t *lenptr) |
327 | { | 328 | { |
328 | if ((data == NULL) || (*lenptr < sizeof(select_info))) | 329 | if ((data == NULL) || (*lenptr < sizeof(select_info))) { |
329 | { | ||
330 | *lenptr = sizeof(select_info); | 330 | *lenptr = sizeof(select_info); |
331 | return 0; | 331 | return 0; |
332 | } | 332 | } |
@@ -350,8 +350,8 @@ int networking_wait_execute(uint8_t *data, uint16_t len, uint16_t milliseconds) | |||
350 | 350 | ||
351 | /* add only if we had a failed write */ | 351 | /* add only if we had a failed write */ |
352 | int writefds_add = 0; | 352 | int writefds_add = 0; |
353 | if (s->send_fail_eagain != 0) | 353 | |
354 | { | 354 | if (s->send_fail_eagain != 0) { |
355 | // current_time(): microseconds | 355 | // current_time(): microseconds |
356 | uint64_t now = current_time(); | 356 | uint64_t now = current_time(); |
357 | 357 | ||
@@ -371,6 +371,7 @@ int networking_wait_execute(uint8_t *data, uint16_t len, uint16_t milliseconds) | |||
371 | 371 | ||
372 | fd_set writefds; | 372 | fd_set writefds; |
373 | FD_ZERO(&writefds); | 373 | FD_ZERO(&writefds); |
374 | |||
374 | if (writefds_add) | 375 | if (writefds_add) |
375 | FD_SET(s->sock, &writefds); | 376 | FD_SET(s->sock, &writefds); |
376 | 377 | ||
@@ -389,8 +390,8 @@ int networking_wait_execute(uint8_t *data, uint16_t len, uint16_t milliseconds) | |||
389 | int res = select(nfds, &readfds, &writefds, &exceptfds, &timeout); | 390 | int res = select(nfds, &readfds, &writefds, &exceptfds, &timeout); |
390 | #ifdef LOGGING | 391 | #ifdef LOGGING |
391 | sprintf(logbuffer, "select(%d): %d (%d, %s) - %d %d %d\n", milliseconds, res, errno, | 392 | sprintf(logbuffer, "select(%d): %d (%d, %s) - %d %d %d\n", milliseconds, res, errno, |
392 | strerror(errno), FD_ISSET(s->sock, &readfds), FD_ISSET(s->sock, &writefds), | 393 | strerror(errno), FD_ISSET(s->sock, &readfds), FD_ISSET(s->sock, &writefds), |
393 | FD_ISSET(s->sock, &exceptfds)); | 394 | FD_ISSET(s->sock, &exceptfds)); |
394 | loglog(logbuffer); | 395 | loglog(logbuffer); |
395 | #endif | 396 | #endif |
396 | 397 | ||
@@ -403,6 +404,7 @@ int networking_wait_execute(uint8_t *data, uint16_t len, uint16_t milliseconds) | |||
403 | void networking_wait_cleanup(Networking_Core *net, uint8_t *data, uint16_t len) | 404 | void networking_wait_cleanup(Networking_Core *net, uint8_t *data, uint16_t len) |
404 | { | 405 | { |
405 | select_info *s = (select_info *)data; | 406 | select_info *s = (select_info *)data; |
407 | |||
406 | if (s->send_fail_reset) | 408 | if (s->send_fail_reset) |
407 | net->send_fail_eagain = 0; | 409 | net->send_fail_eagain = 0; |
408 | } | 410 | } |
@@ -1037,13 +1039,12 @@ static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *i | |||
1037 | uint32_t data[2]; | 1039 | uint32_t data[2]; |
1038 | data[0] = buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0; | 1040 | data[0] = buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0; |
1039 | data[1] = buflen > 7 ? ntohl(*(uint32_t *)&buffer[5]) : 0; | 1041 | data[1] = buflen > 7 ? ntohl(*(uint32_t *)&buffer[5]) : 0; |
1040 | if (res < 0) | 1042 | |
1041 | { | 1043 | if (res < 0) { |
1042 | int written = snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3hu%c %s:%hu (%u: %s) | %04x%04x\n", | 1044 | int written = snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3hu%c %s:%hu (%u: %s) | %04x%04x\n", |
1043 | buffer[0], message, (buflen < 999 ? (uint16_t)buflen : 999), 'E', | 1045 | buffer[0], message, (buflen < 999 ? (uint16_t)buflen : 999), 'E', |
1044 | ip_ntoa(&ip_port->ip), port, errno, strerror(errno), data[0], data[1]); | 1046 | ip_ntoa(&ip_port->ip), port, errno, strerror(errno), data[0], data[1]); |
1045 | } | 1047 | } else if ((res > 0) && ((size_t)res <= buflen)) |
1046 | else if ((res > 0) && ((size_t)res <= buflen)) | ||
1047 | snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3zu%c %s:%hu (%u: %s) | %04x%04x\n", | 1048 | snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3zu%c %s:%hu (%u: %s) | %04x%04x\n", |
1048 | buffer[0], message, (res < 999 ? (size_t)res : 999), ((size_t)res < buflen ? '<' : '='), | 1049 | buffer[0], message, (res < 999 ? (size_t)res : 999), ((size_t)res < buflen ? '<' : '='), |
1049 | ip_ntoa(&ip_port->ip), port, 0, errmsg_ok, data[0], data[1]); | 1050 | ip_ntoa(&ip_port->ip), port, 0, errmsg_ok, data[0], data[1]); |
diff --git a/toxcore/network.h b/toxcore/network.h index 0ce1f34d..8a0e20dc 100644 --- a/toxcore/network.h +++ b/toxcore/network.h | |||
@@ -285,6 +285,4 @@ Networking_Core *new_networking(IP ip, uint16_t port); | |||
285 | /* Function to cleanup networking stuff (doesn't do much right now). */ | 285 | /* Function to cleanup networking stuff (doesn't do much right now). */ |
286 | void kill_networking(Networking_Core *net); | 286 | void kill_networking(Networking_Core *net); |
287 | 287 | ||
288 | int receivepacket(sock_t sock, IP_Port *ip_port, uint8_t *data, uint32_t *length); | ||
289 | |||
290 | #endif | 288 | #endif |