]> git.meshlink.io Git - utcp/blobdiff - test.c
Add a function to check for active connections.
[utcp] / test.c
diff --git a/test.c b/test.c
index b5d64a3eb3ba698c8c763010131fbd386df5fbd0..0eb8462b4b60f26a17c87860384d238500748db8 100644 (file)
--- a/test.c
+++ b/test.c
 
 #include "utcp.h"
 
+#define DIR_READ 1
+#define DIR_WRITE 2
+
 struct utcp_connection *c;
-int dir = 3;
+int dir = DIR_READ | DIR_WRITE;
 bool running = true;
 double dropin;
 double dropout;
@@ -25,7 +28,7 @@ ssize_t do_recv(struct utcp_connection *c, const void *data, size_t len) {
                        fprintf(stderr, "Error: %s\n", strerror(errno));
                        dir = 0;
                } else {
-                       dir &= ~2;
+                       dir &= ~DIR_WRITE;
                        fprintf(stderr, "Connection closed by peer\n");
                }
                return -1;
@@ -36,6 +39,7 @@ ssize_t do_recv(struct utcp_connection *c, const void *data, size_t len) {
 void do_accept(struct utcp_connection *nc, uint16_t port) {
        utcp_accept(nc, do_recv, NULL);
        c = nc;
+       utcp_set_accept_cb(c->utcp, NULL, NULL);
 }
 
 ssize_t do_send(struct utcp *utcp, const void *data, size_t len) {
@@ -73,7 +77,7 @@ int main(int argc, char *argv[]) {
                return 1;
 
        int s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
-       if(s < 0)
+       if(s == -1)
                return 1;
 
        if(server) {
@@ -105,12 +109,12 @@ int main(int argc, char *argv[]) {
        char buf[102400];
        struct timeval timeout = utcp_timeout(u);
 
-       while(dir) {
+       while(!connected || utcp_is_active(u)) {
                size_t max = c ? utcp_get_sndbuf_free(c) : 0;
                if(max > sizeof buf)
                        max = sizeof buf;
 
-               if((dir & 1) && max)
+               if((dir & DIR_READ) && max)
                        poll(fds, 2, timeout.tv_sec * 1000 + timeout.tv_usec / 1000);
                else
                        poll(fds + 1, 1, timeout.tv_sec * 1000 + timeout.tv_usec / 1000);
@@ -121,10 +125,10 @@ int main(int argc, char *argv[]) {
                        ssize_t len = read(0, buf, max);
                        if(len <= 0) {
                                fds[0].fd = -1;
-                               dir &= ~1;
+                               dir &= ~DIR_READ;
                                if(c)
                                        utcp_shutdown(c, SHUT_WR);
-                               if(len < 0)
+                               if(len == -1)
                                        break;
                                else
                                        continue;