X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=test.c;h=1df172efa56556443bfa5c6ec913f9e9014b0035;hb=7034cc75e0324210e96b3da5ac09c979b73173b7;hp=d3319328c8919dcb3a0429447761be5c18810fa8;hpb=eab23bb139afcfb7c3720b8615b04c569a3605a9;p=utcp diff --git a/test.c b/test.c index d331932..1df172e 100644 --- a/test.c +++ b/test.c @@ -26,6 +26,9 @@ long dropfrom; long dropto; double dropin; double dropout; +long total_out; +long total_in; + void debug(const char *format, ...) { struct timeval now; @@ -63,6 +66,7 @@ ssize_t do_send(struct utcp *utcp, const void *data, size_t len) { if(outpktno < dropto && outpktno >= dropfrom && drand48() < dropout) return len; + total_out += len; ssize_t result = send(s, data, len, MSG_DONTWAIT); if(result <= 0) debug("Error sending UDP packet: %s\n", strerror(errno)); @@ -128,19 +132,21 @@ int main(int argc, char *argv[]) { struct timeval timeout = utcp_timeout(u); while(!connected || utcp_is_active(u)) { - debug("\n"); size_t max = c ? utcp_get_sndbuf_free(c) : 0; if(max > sizeof buf) max = sizeof buf; + int timeout_ms = timeout.tv_sec * 1000 + timeout.tv_usec / 1000 + 1; + + debug("polling, dir = %d, timeout = %d\n", dir, timeout_ms); if((dir & DIR_READ) && max) - poll(fds, 2, timeout.tv_sec * 1000 + timeout.tv_usec / 1000); + poll(fds, 2, timeout_ms); else - poll(fds + 1, 1, timeout.tv_sec * 1000 + timeout.tv_usec / 1000); + poll(fds + 1, 1, timeout_ms); if(fds[0].revents) { fds[0].revents = 0; - debug("stdin"); + debug("stdin\n"); ssize_t len = read(0, buf, max); if(len <= 0) { fds[0].fd = -1; @@ -161,7 +167,7 @@ int main(int argc, char *argv[]) { if(fds[1].revents) { fds[1].revents = 0; - debug("netout\n"); + debug("netin\n"); struct sockaddr_storage ss; socklen_t sl = sizeof ss; int len = recvfrom(s, buf, sizeof buf, MSG_DONTWAIT, (struct sockaddr *)&ss, &sl); @@ -173,8 +179,10 @@ int main(int argc, char *argv[]) { if(!connect(s, (struct sockaddr *)&ss, sl)) connected = true; inpktno++; - if(inpktno >= dropto || inpktno < dropfrom || drand48() >= dropin) + if(inpktno >= dropto || inpktno < dropfrom || drand48() >= dropin) { + total_in += len; utcp_recv(u, buf, len); + } } timeout = utcp_timeout(u); @@ -183,5 +191,7 @@ int main(int argc, char *argv[]) { utcp_close(c); utcp_exit(u); + debug("Total bytes in: %ld, out: %ld\n", total_in, total_out); + return 0; }