X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=test.c;h=0f7f33368dc1def41192c494b4a29ea918298eac;hb=7d8a65c11dd4b0555575ff75efab859236c4964f;hp=0eb8462b4b60f26a17c87860384d238500748db8;hpb=2049874f3c436cc1d85bb8bcccb272a3b933f4a9;p=utcp diff --git a/test.c b/test.c index 0eb8462..0f7f333 100644 --- a/test.c +++ b/test.c @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -19,17 +20,34 @@ struct utcp_connection *c; int dir = DIR_READ | DIR_WRITE; bool running = true; +long inpktno; +long outpktno; +long dropfrom; +long dropto; double dropin; double dropout; +long total_out; +long total_in; + + +void debug(const char *format, ...) { + struct timeval now; + gettimeofday(&now, NULL); + fprintf(stderr, "%lu.%lu ", now.tv_sec, now.tv_usec / 1000); + va_list ap; + va_start(ap, format); + vfprintf(stderr, format, ap); + va_end(ap); +} ssize_t do_recv(struct utcp_connection *c, const void *data, size_t len) { if(!data || !len) { if(errno) { - fprintf(stderr, "Error: %s\n", strerror(errno)); + debug("Error: %s\n", strerror(errno)); dir = 0; } else { dir &= ~DIR_WRITE; - fprintf(stderr, "Connection closed by peer\n"); + debug("Connection closed by peer\n"); } return -1; } @@ -44,12 +62,14 @@ void do_accept(struct utcp_connection *nc, uint16_t port) { ssize_t do_send(struct utcp *utcp, const void *data, size_t len) { int s = *(int *)utcp->priv; - if(drand48() < dropout) + outpktno++; + if(outpktno < dropto && outpktno >= dropfrom && drand48() < dropout) return len; + total_out += len; ssize_t result = send(s, data, len, MSG_DONTWAIT); if(result <= 0) - fprintf(stderr, "Error sending UDP packet: %s\n", strerror(errno)); + debug("Error sending UDP packet: %s\n", strerror(errno)); return result; } @@ -65,6 +85,8 @@ int main(int argc, char *argv[]) { dropin = atof(getenv("DROPIN") ?: "0"); dropout = atof(getenv("DROPOUT") ?: "0"); + dropfrom = atoi(getenv("DROPFROM") ?: "0"); + dropto = atoi(getenv("DROPTO") ?: "0"); struct addrinfo *ai; struct addrinfo hint = { @@ -110,6 +132,7 @@ 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; @@ -121,7 +144,7 @@ int main(int argc, char *argv[]) { if(fds[0].revents) { fds[0].revents = 0; - fprintf(stderr, "0"); + debug("stdin\n"); ssize_t len = read(0, buf, max); if(len <= 0) { fds[0].fd = -1; @@ -136,25 +159,28 @@ int main(int argc, char *argv[]) { if(c) { ssize_t sent = utcp_send(c, buf, len); if(sent != len) - fprintf(stderr, "PANIEK: %zd != %zd\n", sent, len); + debug("Short send: %zd != %zd\n", sent, len); } } if(fds[1].revents) { fds[1].revents = 0; - fprintf(stderr, "1"); + debug("netout\n"); struct sockaddr_storage ss; socklen_t sl = sizeof ss; int len = recvfrom(s, buf, sizeof buf, MSG_DONTWAIT, (struct sockaddr *)&ss, &sl); if(len <= 0) { - fprintf(stderr, "Error receiving UDP packet: %s\n", strerror(errno)); + debug("Error receiving UDP packet: %s\n", strerror(errno)); break; } if(!connected) if(!connect(s, (struct sockaddr *)&ss, sl)) connected = true; - if(drand48() >= dropin) + inpktno++; + if(inpktno >= dropto || inpktno < dropfrom || drand48() >= dropin) { + total_in += len; utcp_recv(u, buf, len); + } } timeout = utcp_timeout(u); @@ -163,5 +189,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; }