From eb8c9fe176721d5e8dd24febec88e1665ca70c26 Mon Sep 17 00:00:00 2001
From: Uros Majstorovic <majstor@majstor.org>
Date: Thu, 11 Jan 2018 15:32:30 +0100
Subject: transport refactoring

---
 code/core/posix/transport.c | 34 +++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 17 deletions(-)

(limited to 'code/core/posix')

diff --git a/code/core/posix/transport.c b/code/core/posix/transport.c
index e3fe612..df8e0f5 100644
--- a/code/core/posix/transport.c
+++ b/code/core/posix/transport.c
@@ -71,13 +71,6 @@ static void t_close(int *sock) {
     close(*sock);
 }
 
-static int t_poll(int *sock, int timeout) {
-    struct pollfd fds[] = {
-        {*sock, POLLIN, 0}
-    };
-    return poll(fds, 1, timeout);
-}
-
 static ssize_t t_send(int *sock, void *msg, size_t msg_size, ECPNetAddr *addr) {
     struct sockaddr_in servaddr;
 
@@ -88,26 +81,33 @@ static ssize_t t_send(int *sock, void *msg, size_t msg_size, ECPNetAddr *addr) {
     return sendto(*sock, msg, msg_size, 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
 }
 
-static ssize_t t_recv(int *sock, void *msg, size_t msg_size, ECPNetAddr *addr) {
+static ssize_t t_recv(int *sock, void *msg, size_t msg_size, ECPNetAddr *addr, int timeout) {
     struct sockaddr_in servaddr;
     socklen_t addrlen = sizeof(servaddr);
-    memset((void *)&servaddr, 0, sizeof(servaddr));
-
-    ssize_t recvlen = recvfrom(*sock, msg, msg_size, 0, (struct sockaddr *)&servaddr, &addrlen);
-    if (recvlen < 0) return recvlen;
+    struct pollfd fds[] = {
+        {*sock, POLLIN, 0}
+    };
 
-    if (addr) {
-        addr->port = servaddr.sin_port;
-        memcpy(addr->host, (void *)&servaddr.sin_addr, sizeof(addr->host));
+    int rv = poll(fds, 1, timeout);
+    memset((void *)&servaddr, 0, sizeof(servaddr));
+    if (rv == 1) {
+        ssize_t recvlen = recvfrom(*sock, msg, msg_size, 0, (struct sockaddr *)&servaddr, &addrlen);
+        if (recvlen < 0) return recvlen;
+        if (recvlen < ECP_MIN_PKT) return ECP_ERR_RECV;
+
+        if (addr) {
+            addr->port = servaddr.sin_port;
+            memcpy(addr->host, (void *)&servaddr.sin_addr, sizeof(addr->host));
+        }
+        return recvlen;
     }
-    return recvlen;
+    return ECP_ERR_TIMEOUT;
 }
 
 int ecp_transport_init(ECPTransportIface *t) {
     t->init = 1;
     t->open = t_open;
     t->close = t_close;
-    t->poll = t_poll;
     t->send = t_send;
     t->recv = t_recv;
     t->addr_eq = t_addr_eq;
-- 
cgit v1.2.3