diff options
author | Uros Majstorovic <majstor@majstor.org> | 2024-05-11 15:58:48 +0200 |
---|---|---|
committer | Uros Majstorovic <majstor@majstor.org> | 2024-05-11 15:58:48 +0200 |
commit | 7a0e44998f426752c58975ae686d8e88aa131195 (patch) | |
tree | b410237e4d9986a06108e9c0beff015a97effc55 /ecp/src/platform | |
parent | b3c62e6acc5761171822f522dc9d784558f9afbe (diff) |
added error handler per connection type; improved handlers API and error reporting
Diffstat (limited to 'ecp/src/platform')
-rw-r--r-- | ecp/src/platform/fe310/transport.c | 13 | ||||
-rw-r--r-- | ecp/src/platform/posix/transport.c | 38 |
2 files changed, 29 insertions, 22 deletions
diff --git a/ecp/src/platform/fe310/transport.c b/ecp/src/platform/fe310/transport.c index bc758c9..35addfa 100644 --- a/ecp/src/platform/fe310/transport.c +++ b/ecp/src/platform/fe310/transport.c @@ -66,11 +66,12 @@ int ecp_tr_addr_set(ecp_tr_addr_t *addr, void *addr_s) { } int ecp_tr_open(ECPSocket *sock, ecp_tr_addr_t *addr) { - sock->sock = eos_sock_open_udp(packet_handler, NULL); - if (sock->sock < 0) { - sock->sock = 0; - return ECP_ERR; - } + int rv; + + rv = eos_sock_open_udp(packet_handler, NULL); + if (rv < 0) return ECP_ERR_OPEN; + + sock->sock = rv; _ecp_tr_sock = sock; return ECP_OK; @@ -97,7 +98,7 @@ ssize_t ecp_tr_send(ECPSocket *sock, ECPBuffer *packet, size_t pkt_size, ecp_tr_ } else { buf = eos_net_alloc(); } - if (buf == NULL) return ECP_ERR; + if (buf == NULL) return ECP_ERR_ALLOC; rv = eos_sock_sendto_async(sock->sock, reply ? NULL : packet->buffer, pkt_size, addr, buf, !!(flags & ECP_SEND_FLAG_MORE)); if (rv) return ECP_ERR_SEND; diff --git a/ecp/src/platform/posix/transport.c b/ecp/src/platform/posix/transport.c index 51f311a..71267a1 100644 --- a/ecp/src/platform/posix/transport.c +++ b/ecp/src/platform/posix/transport.c @@ -36,17 +36,17 @@ int ecp_tr_addr_set(ecp_tr_addr_t *addr, void *addr_s) { memset(addr_c, 0, sizeof(addr_c)); strncpy(addr_c, addr_s, sizeof(addr_c)-1); colon = strchr(addr_c, ':'); - if (colon == NULL) return -1; + if (colon == NULL) return ECP_ERR_ADDR; *colon = '\0'; colon++; - if (*colon == '\0') return -1; + if (*colon == '\0') return ECP_ERR_ADDR; rv = inet_pton(AF_INET, addr_c, addr->host); - if (rv != 1) return -1; + if (rv != 1) return ECP_ERR_ADDR; hport = strtol(colon, &endptr, 10); - if (*endptr != '\0') return -1; + if (*endptr != '\0') return ECP_ERR_ADDR; addr->port = htons(hport); - return 0; + return ECP_OK; } int ecp_tr_open(ECPSocket *sock, ecp_tr_addr_t *addr) { @@ -63,13 +63,14 @@ int ecp_tr_open(ECPSocket *sock, ecp_tr_addr_t *addr) { _myaddr.sin_port = htons(0); } - sock->sock = socket(PF_INET, SOCK_DGRAM, 0); - if (sock->sock < 0) return sock->sock; + rv = socket(PF_INET, SOCK_DGRAM, 0); + if (rv < 0) return ECP_ERR_OPEN; + sock->sock = rv; rv = bind(sock->sock, (struct sockaddr *)&_myaddr, sizeof(_myaddr)); if (rv < 0) { close(sock->sock); - return rv; + return ECP_ERR_BIND; } return ECP_OK; @@ -81,12 +82,16 @@ void ecp_tr_close(ECPSocket *sock) { ssize_t ecp_tr_send(ECPSocket *sock, ECPBuffer *packet, size_t pkt_size, ecp_tr_addr_t *addr, unsigned char flags) { struct sockaddr_in servaddr; + ssize_t rv; memset((void *)&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = addr->port; memcpy((void *)&servaddr.sin_addr, addr->host, sizeof(addr->host)); - return sendto(sock->sock, packet->buffer, pkt_size, 0, (struct sockaddr *)&servaddr, sizeof(servaddr)); + rv = sendto(sock->sock, packet->buffer, pkt_size, 0, (struct sockaddr *)&servaddr, sizeof(servaddr)); + if (rv < 0) return ECP_ERR_SEND; + + return rv; } ssize_t ecp_tr_recv(ECPSocket *sock, ECPBuffer *packet, ecp_tr_addr_t *addr, int timeout) { @@ -95,20 +100,21 @@ ssize_t ecp_tr_recv(ECPSocket *sock, ECPBuffer *packet, ecp_tr_addr_t *addr, int struct pollfd fds[] = { {sock->sock, POLLIN, 0} }; - int rv; + ssize_t rv; + int _rv; - rv = poll(fds, 1, timeout); + _rv = poll(fds, 1, timeout); memset((void *)&servaddr, 0, sizeof(servaddr)); - if (rv == 1) { - ssize_t recvlen = recvfrom(sock->sock, packet->buffer, packet->size, 0, (struct sockaddr *)&servaddr, &addrlen); - if (recvlen < 0) return recvlen; - if (recvlen < ECP_MIN_PKT) return ECP_ERR_RECV; + if (_rv == 1) { + rv = recvfrom(sock->sock, packet->buffer, packet->size, 0, (struct sockaddr *)&servaddr, &addrlen); + if (rv < 0) return ECP_ERR_RECV; + if (rv < 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 rv; } return ECP_ERR_TIMEOUT; |