summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toxcore/Lossless_UDP.c14
-rw-r--r--toxcore/Lossless_UDP.h3
-rw-r--r--toxcore/Messenger.c2
-rw-r--r--toxcore/network.c36
-rw-r--r--toxcore/network.h2
5 files changed, 44 insertions, 13 deletions
diff --git a/toxcore/Lossless_UDP.c b/toxcore/Lossless_UDP.c
index 53cdea09..d82c3e27 100644
--- a/toxcore/Lossless_UDP.c
+++ b/toxcore/Lossless_UDP.c
@@ -461,6 +461,20 @@ uint32_t sendqueue(Lossless_UDP *ludp, int connection_id)
461 return connection->sendbuff_packetnum - connection->successful_sent; 461 return connection->sendbuff_packetnum - connection->successful_sent;
462} 462}
463 463
464/* return number of packets in all queues waiting to be successfully sent. */
465uint32_t sendqueue_total(Lossless_UDP *ludp)
466{
467 uint32_t total = 0;
468 int i;
469 for(i = 0; i < ludp->connections.len; i++) {
470 Connection *connection = &tox_array_get(&ludp->connections, i, Connection);
471 if (connection->status != 0)
472 total += connection->sendbuff_packetnum - connection->successful_sent;
473 }
474
475 return total;
476}
477
464/* return the number of packets in the queue waiting to be successfully read with read_packet(...). */ 478/* return the number of packets in the queue waiting to be successfully read with read_packet(...). */
465uint32_t recvqueue(Lossless_UDP *ludp, int connection_id) 479uint32_t recvqueue(Lossless_UDP *ludp, int connection_id)
466{ 480{
diff --git a/toxcore/Lossless_UDP.h b/toxcore/Lossless_UDP.h
index 8a1c9c53..5b79d50f 100644
--- a/toxcore/Lossless_UDP.h
+++ b/toxcore/Lossless_UDP.h
@@ -225,6 +225,9 @@ int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t
225/* return number of packets in the queue waiting to be successfully sent. */ 225/* return number of packets in the queue waiting to be successfully sent. */
226uint32_t sendqueue(Lossless_UDP *ludp, int connection_id); 226uint32_t sendqueue(Lossless_UDP *ludp, int connection_id);
227 227
228/* return number of packets in all queues waiting to be successfully sent. */
229uint32_t sendqueue_total(Lossless_UDP *ludp);
230
228/* 231/*
229 * return number of packets in the queue waiting to be successfully 232 * return number of packets in the queue waiting to be successfully
230 * read with read_packet(...). 233 * read with read_packet(...).
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 54ee6cce..09095e5f 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -1401,7 +1401,7 @@ void doMessenger(Messenger *m)
1401 */ 1401 */
1402int waitMessenger(Messenger *m, uint16_t milliseconds) 1402int waitMessenger(Messenger *m, uint16_t milliseconds)
1403{ 1403{
1404 return networking_wait(m->net, milliseconds); 1404 return networking_wait(m->net, sendqueue_total(m->net_crypto->lossless_udp), milliseconds);
1405}; 1405};
1406 1406
1407/* return size of the messenger data (for saving) */ 1407/* return size of the messenger data (for saving) */
diff --git a/toxcore/network.c b/toxcore/network.c
index 5d308507..a5786801 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -309,31 +309,43 @@ void networking_poll(Networking_Core *net)
309 * returns 1 if there is socket activity (i.e. tox_do() should be called) 309 * returns 1 if there is socket activity (i.e. tox_do() should be called)
310 * 310 *
311 */ 311 */
312int networking_wait(Networking_Core *net, uint16_t milliseconds) 312int networking_wait(Networking_Core *net, uint32_t sendqueue_len, uint16_t milliseconds)
313{ 313{
314 sock_t sock = net->sock;
314 /* WIN32: supported since Win2K, but might need some adjustements */ 315 /* WIN32: supported since Win2K, but might need some adjustements */
315 /* UNIX: this should work for any remotely Unix'ish system */ 316 /* UNIX: this should work for any remotely Unix'ish system */
316 int nfds = 1 + net->sock; 317 int nfds = 1 + sock;
317 318
318 /* the FD_ZERO calls might be superfluous */ 319 /* the FD_ZERO calls might be superfluous */
319 fd_set readfds; 320 fd_set readfds;
320 FD_ZERO(&readfds); 321 FD_ZERO(&readfds);
321 FD_SET(net->sock, &readfds); 322 FD_SET(sock, &readfds);
322 323
323 fd_set writefds; 324 fd_set writefds;
324 FD_ZERO(&writefds); 325 FD_ZERO(&writefds);
325 FD_SET(net->sock, &writefds); 326 /* add only if we have packets queued, signals that a write won't block */
327 if (sendqueue_len > 0)
328 FD_SET(sock, &writefds);
326 329
327 fd_set exceptfds; 330 fd_set exceptfds;
328 FD_ZERO(&exceptfds); 331 FD_ZERO(&exceptfds);
329 FD_SET(net->sock, &exceptfds); 332 FD_SET(sock, &exceptfds);
330 333
331 struct timeval timeout; 334 struct timeval timeout;
332 timeout.tv_sec = 0; 335 timeout.tv_sec = 0;
333 timeout.tv_usec = milliseconds * 1000; 336 timeout.tv_usec = milliseconds * 1000;
334 337
338#ifdef LOGGING
339 errno = 0;
340#endif
335 /* returns -1 on error, 0 on timeout, the socket on activity */ 341 /* returns -1 on error, 0 on timeout, the socket on activity */
336 int res = select(nfds, &readfds, &writefds, &exceptfds, &timeout); 342 int res = select(nfds, &readfds, &writefds, &exceptfds, &timeout);
343#ifdef LOGGING
344 sprintf(logbuffer, "select(%d): %d (%d, %s) - %d %d %d\n", milliseconds, res, errno,
345 strerror(errno), FD_ISSET(sock, &readfds), FD_ISSET(sock, &writefds),
346 FD_ISSET(sock, &exceptfds));
347 loglog(logbuffer);
348#endif
337 349
338 return res > 0 ? 1 : 0; 350 return res > 0 ? 1 : 0;
339}; 351};
@@ -486,6 +498,7 @@ Networking_Core *new_networking(IP ip, uint16_t port)
486 { 498 {
487 char ipv6only = 0; 499 char ipv6only = 0;
488#ifdef LOGGING 500#ifdef LOGGING
501 errno = 0;
489 int res = 502 int res =
490#endif 503#endif
491 setsockopt(temp->sock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&ipv6only, sizeof(ipv6only)); 504 setsockopt(temp->sock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&ipv6only, sizeof(ipv6only));
@@ -509,6 +522,7 @@ Networking_Core *new_networking(IP ip, uint16_t port)
509 mreq.ipv6mr_multiaddr.s6_addr[15] = 0x01; 522 mreq.ipv6mr_multiaddr.s6_addr[15] = 0x01;
510 mreq.ipv6mr_interface = 0; 523 mreq.ipv6mr_interface = 0;
511#ifdef LOGGING 524#ifdef LOGGING
525 errno = 0;
512 res = 526 res =
513#endif 527#endif
514 setsockopt(temp->sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq)); 528 setsockopt(temp->sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq));
@@ -961,23 +975,23 @@ int addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra)
961static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *ip_port, ssize_t res) 975static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *ip_port, ssize_t res)
962{ 976{
963 if (res < 0) 977 if (res < 0)
964 snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3u%c %s:%u (%u: %s) | %04x%04x\n", 978 snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3u%c %s:%u (%d: %s) | %04x%04x\n",
965 buffer[0], message, buflen < 999 ? buflen : 999, 'E', 979 buffer[0], message, buflen < 999 ? buflen : 999, 'E',
966 ip_ntoa(&ip_port->ip), ntohs(ip_port->port), errno, 980 ip_ntoa(&ip_port->ip), ntohs(ip_port->port), errno,
967 strerror(errno), buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0, 981 strerror(errno), buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0,
968 buflen > 7 ? ntohl(*(uint32_t *)(&buffer[5])) : 0); 982 (buflen > 7) ? ntohl(*(uint32_t *)(&buffer[5])) : 0);
969 else if ((res > 0) && ((size_t)res <= buflen)) 983 else if ((res > 0) && ((size_t)res <= buflen))
970 snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3u%c %s:%u (%u: %s) | %04x%04x\n", 984 snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3u%c %s:%u (%d: %s) | %04x%04x\n",
971 buffer[0], message, res < 999 ? res : 999, (size_t)res < buflen ? '<' : '=', 985 buffer[0], message, res < 999 ? res : 999, (size_t)res < buflen ? '<' : '=',
972 ip_ntoa(&ip_port->ip), ntohs(ip_port->port), 0, 986 ip_ntoa(&ip_port->ip), ntohs(ip_port->port), 0,
973 "OK", buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0, 987 "OK", buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0,
974 buflen > 7 ? ntohl(*(uint32_t *)(&buffer[5])) : 0); 988 (buflen > 7) ? ntohl(*(uint32_t *)(&buffer[5])) : 0);
975 else /* empty or overwrite */ 989 else /* empty or overwrite */
976 snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %u%c%u %s:%u (%u: %s) | %04x%04x\n", 990 snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %u%c%u %s:%u (%d: %s) | %04x%04x\n",
977 buffer[0], message, res, !res ? '!' : '>', buflen, 991 buffer[0], message, res, !res ? '!' : '>', buflen,
978 ip_ntoa(&ip_port->ip), ntohs(ip_port->port), 0, 992 ip_ntoa(&ip_port->ip), ntohs(ip_port->port), 0,
979 "OK", buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0, 993 "OK", buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0,
980 buflen > 7 ? ntohl(*(uint32_t *)(&buffer[5])) : 0); 994 (buflen > 7) ? ntohl(*(uint32_t *)(&buffer[5])) : 0);
981 995
982 logbuffer[sizeof(logbuffer) - 1] = 0; 996 logbuffer[sizeof(logbuffer) - 1] = 0;
983 loglog(logbuffer); 997 loglog(logbuffer);
diff --git a/toxcore/network.h b/toxcore/network.h
index a7ae75ab..d143b48d 100644
--- a/toxcore/network.h
+++ b/toxcore/network.h
@@ -274,7 +274,7 @@ void networking_poll(Networking_Core *net);
274 * returns 1 if there is socket activity (i.e. tox_do() should be called) 274 * returns 1 if there is socket activity (i.e. tox_do() should be called)
275 * 275 *
276 */ 276 */
277int networking_wait(Networking_Core *net, uint16_t milliseconds); 277int networking_wait(Networking_Core *net, uint32_t sendqueue_len, uint16_t milliseconds);
278 278
279/* Initialize networking. 279/* Initialize networking.
280 * bind to ip and port. 280 * bind to ip and port.