]> git.meshlink.io Git - utcp/blobdiff - test.c
Handle channel closure during a receive callback when the ringbuffer wraps.
[utcp] / test.c
diff --git a/test.c b/test.c
index 68b42b25e9ab19f122b4ae9425adc32b086d5342..2a273de70463dbe4f39117cc50ed438dc46c4834 100644 (file)
--- a/test.c
+++ b/test.c
 #define DIR_READ 1
 #define DIR_WRITE 2
 
-struct utcp_connection *c;
-int dir = DIR_READ | DIR_WRITE;
-bool running = true;
-long inpktno;
-long outpktno;
-long dropfrom;
-long dropto;
-double reorder;
-long reorder_dist = 10;
-double dropin;
-double dropout;
-long total_out;
-long total_in;
-FILE *reference;
-long mtu = 0;
-
-char *reorder_data;
-size_t reorder_len;
-int reorder_countdown;
+static struct utcp_connection *c;
+static int dir = DIR_READ | DIR_WRITE;
+static long inpktno;
+static long outpktno;
+static long dropfrom;
+static long dropto;
+static double reorder;
+static long reorder_dist = 10;
+static double dropin;
+static double dropout;
+static long total_out;
+static long total_in;
+static FILE *reference;
+static long mtu;
+static long bufsize;
+
+static char *reorder_data;
+static size_t reorder_len;
+static int reorder_countdown;
 
 #if UTCP_DEBUG
 static void debug(const char *format, ...) {
@@ -58,7 +58,7 @@ static void debug(const char *format, ...) {
 #define debug(...) do {} while(0)
 #endif
 
-ssize_t do_recv(struct utcp_connection *c, const void *data, size_t len) {
+static ssize_t do_recv(struct utcp_connection *c, const void *data, size_t len) {
        (void)c;
 
        if(!data || !len) {
@@ -90,14 +90,20 @@ ssize_t do_recv(struct utcp_connection *c, const void *data, size_t len) {
        return write(1, data, len);
 }
 
-void do_accept(struct utcp_connection *nc, uint16_t port) {
+static void do_accept(struct utcp_connection *nc, uint16_t port) {
        (void)port;
        utcp_accept(nc, do_recv, NULL);
        c = nc;
+
+       if(bufsize) {
+               utcp_set_sndbuf(c, bufsize);
+               utcp_set_rcvbuf(c, bufsize);
+       }
+
        utcp_set_accept_cb(c->utcp, NULL, NULL);
 }
 
-ssize_t do_send(struct utcp *utcp, const void *data, size_t len) {
+static ssize_t do_send(struct utcp *utcp, const void *data, size_t len) {
        int s = *(int *)utcp->priv;
        outpktno++;
 
@@ -153,7 +159,7 @@ static void set_mtu(struct utcp *u, int s) {
 
        debug("Using MTU %lu\n", mtu);
 
-       utcp_set_mtu(u, mtu ? mtu - 48 : 1300);
+       utcp_set_mtu(u, mtu ? mtu - 28 : 1300);
 }
 
 int main(int argc, char *argv[]) {
@@ -205,6 +211,10 @@ int main(int argc, char *argv[]) {
                mtu = atoi(getenv("MTU"));
        }
 
+       if(getenv("BUFSIZE")) {
+               bufsize = atoi(getenv("BUFSIZE"));
+       }
+
        char *reference_filename = getenv("REFERENCE");
 
        if(reference_filename) {
@@ -267,6 +277,11 @@ int main(int argc, char *argv[]) {
        if(!server) {
                set_mtu(u, s);
                c = utcp_connect_ex(u, 1, do_recv, NULL, flags);
+
+               if(bufsize) {
+                       utcp_set_sndbuf(c, bufsize);
+                       utcp_set_rcvbuf(c, bufsize);
+               }
        }
 
        struct pollfd fds[2] = {
@@ -276,7 +291,7 @@ int main(int argc, char *argv[]) {
 
        char buf[102400];
 
-       struct timeval timeout = utcp_timeout(u);
+       struct timespec timeout = utcp_timeout(u);
 
        while(!connected || utcp_is_active(u)) {
                size_t max = c ? utcp_get_sndbuf_free(c) : 0;
@@ -289,7 +304,7 @@ int main(int argc, char *argv[]) {
                        max = read_size;
                }
 
-               int timeout_ms = timeout.tv_sec * 1000 + timeout.tv_usec / 1000 + 1;
+               int timeout_ms = timeout.tv_sec * 1000 + timeout.tv_nsec / 1000000 + 1;
 
                debug("polling, dir = %d, timeout = %d\n", dir, timeout_ms);