]> git.lizzy.rs Git - dragonnet.git/blobdiff - recv_thread.c
Tidy address string parser
[dragonnet.git] / recv_thread.c
index 9a85d94b698bce7f16c681b107b9df1362959529..bff48ebb8b6443cd7867db987173137c1f6f8486 100644 (file)
@@ -1,5 +1,5 @@
 #include <assert.h>
-#include <errno.h>
+#include <dragontype/number.h>
 #include <pthread.h>
 #include <stdbool.h>
 #include <stdio.h>
@@ -18,30 +18,33 @@ void *dragonnet_peer_recv_thread(void *g_peer)
        pthread_rwlock_unlock(&p->mu);
 
        while (true) {
-               uint16_t msg;
+               u16 msg;
 
+               // Copy socket fd so that shutdown doesn't block
                pthread_rwlock_rdlock(&p->mu);
-               ssize_t len = recv(p->sock, &msg, sizeof msg, MSG_WAITALL);
+               int sock = p->sock;
                pthread_rwlock_unlock(&p->mu);
 
-               if (len < 0 && errno != EWOULDBLOCK) {
+               ssize_t len = recv(sock, &msg, sizeof msg, MSG_WAITALL);
+
+               if (len < 0) {
                        perror("recv");
                        dragonnet_peer_delete(p);
                        return NULL;
                }
 
-               // connection closed
-               if ((len >= 0 && len != sizeof msg) || errno == EWOULDBLOCK) {
+               // Connection closed
+               if (len == 0) {
                        pthread_rwlock_wrlock(&p->mu);
 
                        close(p->sock);
-                       p->sock = 0;
+                       p->sock = -1;
                        p->state++;
 
                        pthread_rwlock_unlock(&p->mu);
                        return NULL;
                }
 
-               // deserialization
+               // Deserialization
        }
 }