diff options
author | Uros Majstorovic <majstor@majstor.org> | 2018-01-11 15:32:30 +0100 |
---|---|---|
committer | Uros Majstorovic <majstor@majstor.org> | 2018-01-11 15:32:30 +0100 |
commit | eb8c9fe176721d5e8dd24febec88e1665ca70c26 (patch) | |
tree | ff5e58d57867f8a8283a04f0a008a93fee3d2c02 /code/core/posix | |
parent | 848f44266dd4b9213af632d55747d66f03881422 (diff) |
transport refactoring
Diffstat (limited to 'code/core/posix')
-rw-r--r-- | code/core/posix/transport.c | 34 |
1 files changed, 17 insertions, 17 deletions
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; |