From: Guus Sliepen Date: Sat, 9 Aug 2014 17:15:22 +0000 (+0200) Subject: Allow variable packet loss rates to be set in the test program. X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=2c9dfa2823b30638bc2f7166c15008b722ddebbe;p=utcp Allow variable packet loss rates to be set in the test program. --- diff --git a/test.c b/test.c index 10ebc9a..cda5f17 100644 --- a/test.c +++ b/test.c @@ -16,6 +16,8 @@ struct utcp_connection *c; int dir = 3; bool running = true; +double dropin; +double dropout; int do_recv(struct utcp_connection *c, const void *data, size_t len) { if(!data || !len) { @@ -38,11 +40,15 @@ void do_accept(struct utcp_connection *nc, uint16_t port) { int do_send(struct utcp *utcp, const void *data, size_t len) { int s = *(int *)utcp->priv; - return send(s, data, len, MSG_DONTWAIT); + if(drand48() >= dropout) + return send(s, data, len, MSG_DONTWAIT); + else + return 0; } int main(int argc, char *argv[]) { srand(time(NULL)); + srand48(time(NULL)); if(argc < 2 || argc > 3) return 1; @@ -50,6 +56,9 @@ int main(int argc, char *argv[]) { bool server = argc == 2; bool connected = false; + dropin = atof(getenv("DROPIN") ?: "0"); + dropout = atof(getenv("DROPOUT") ?: "0"); + struct addrinfo *ai; struct addrinfo hint = { .ai_flags = server ? AI_PASSIVE : 0, @@ -118,7 +127,8 @@ int main(int argc, char *argv[]) { if(!connected) if(!connect(s, (struct sockaddr *)&ss, sl)) connected = true; - utcp_recv(u, buf, len); + if(drand48() >= dropin) + utcp_recv(u, buf, len); } timeout = utcp_timeout(u);