diff options
Diffstat (limited to 'libhw_generic/include')
-rw-r--r-- | libhw_generic/include/libhw/generic/io.h | 34 | ||||
-rw-r--r-- | libhw_generic/include/libhw/generic/net.h | 52 |
2 files changed, 35 insertions, 51 deletions
diff --git a/libhw_generic/include/libhw/generic/io.h b/libhw_generic/include/libhw/generic/io.h index 8b3fb9c..ebbd6cb 100644 --- a/libhw_generic/include/libhw/generic/io.h +++ b/libhw_generic/include/libhw/generic/io.h @@ -9,8 +9,8 @@ #include <stddef.h> /* for size_t */ #include <stdint.h> /* for uintptr_t */ -#include <sys/types.h> /* for ssize_t */ +#include <libmisc/error.h> #include <libmisc/obj.h> /* structs ********************************************************************/ @@ -58,20 +58,19 @@ void io_slice_wr_to_duplex(struct duplex_iovec *dst, const struct iovec *src, in /* basic interfaces ***********************************************************/ /** - * Return bytes-read on success, 0 on EOF, -errno on error; a short - * read is *not* an error. + * Return bytes-read on success. A short read is *not* an error + * (unlike writev). * * It is invalid to call readv when the sum length of iovecs is 0. */ #define io_reader_LO_IFACE \ - LO_FUNC(ssize_t, readv, const struct iovec *iov, int iovcnt) + LO_FUNC(size_t_or_error, readv, const struct iovec *iov, int iovcnt) LO_INTERFACE(io_reader); #define io_readv(r, iov, iovcnt) LO_CALL(r, readv, iov, iovcnt) #define io_read(r, buf, count) LO_CALL(r, readv, &((struct iovec){.iov_base = buf, .iov_len = count}), 1) /** - * Return `count` on success, -errno on error; a short write *is* an - * error. + * Return bytes-written. A short write *is* an error (unlike readv) * * Writes are *not* guaranteed to be atomic, so if you have concurrent * writers then you should arrange for a mutex to protect the writer. @@ -79,38 +78,35 @@ LO_INTERFACE(io_reader); * It is invalid to call writev when the sum length of iovecs is 0. */ #define io_writer_LO_IFACE \ - LO_FUNC(ssize_t, writev, const struct iovec *iov, int iovcnt) + LO_FUNC(size_t_and_error, writev, const struct iovec *iov, int iovcnt) LO_INTERFACE(io_writer); #define io_writev(w, iov, iovcnt) LO_CALL(w, writev, iov, iovcnt) #define io_write(w, buf, count) LO_CALL(w, writev, &((struct iovec){.iov_base = buf, .iov_len = count}), 1) -/** - * Return 0 on success, -errno on error. - */ #define io_closer_LO_IFACE \ - LO_FUNC(int, close) + LO_FUNC(error, close) LO_INTERFACE(io_closer); #define io_close(c) LO_CALL(c, close) -/** - * All methods return 0 on success, -errno on error. - */ #define io_bidi_closer_LO_IFACE \ LO_NEST(io_closer) \ - LO_FUNC(int, close_read) \ - LO_FUNC(int, close_write) + LO_FUNC(error, close_read) \ + LO_FUNC(error, close_write) LO_INTERFACE(io_bidi_closer); #define io_close_read(c) LO_CALL(c, close_read) #define io_close_write(c) LO_CALL(c, close_write) /** - * Return bytes-read and bytes-written on success, -errno on error; a - * short read/write *is* an error. + * A short read/write *is* an error (like writev and unlike readv). + * + * Reads/writes are *not* guaranteed to be atomic, so if you have + * concurrent readers/writers then you should arrange for a mutex to + * protect the duplex_readwriter. * * It is invalid to call readwritev when the sum length of iovecs is 0. */ #define io_duplex_readwriter_LO_IFACE \ - LO_FUNC(void, readwritev, const struct duplex_iovec *iov, int iovcnt) + LO_FUNC(size_t_and_error, readwritev, const struct duplex_iovec *iov, int iovcnt) LO_INTERFACE(io_duplex_readwriter); #define io_readwritev(rw, iov, iovcnt) \ diff --git a/libhw_generic/include/libhw/generic/net.h b/libhw_generic/include/libhw/generic/net.h index 55e4a6f..d1d4194 100644 --- a/libhw_generic/include/libhw/generic/net.h +++ b/libhw_generic/include/libhw/generic/net.h @@ -9,23 +9,10 @@ #include <stddef.h> /* for size_t */ #include <stdint.h> /* for uint{n}_t} */ -#include <sys/types.h> /* for ssize_t */ #include <libhw/generic/io.h> #include <libmisc/fmt.h> -/* Errnos *********************************************************************/ - -#define NET_EOTHER 1 -#define NET_EARP_TIMEOUT 2 -#define NET_EACK_TIMEOUT 3 -#define NET_ERECV_TIMEOUT 4 -#define NET_ETHREAD 5 -#define NET_ECLOSED 6 -#define NET_EMSGSIZE 7 - -const char *net_strerror(int net_errno); - /* Address types **************************************************************/ struct net_ip4_addr { @@ -44,22 +31,6 @@ void fmt_print_net_eth_addr(lo_interface fmt_dest, struct net_eth_addr); /* Streams (e.g. TCP) *********************************************************/ -lo_interface net_stream_conn; - -#define net_stream_listener_LO_IFACE \ - /** \ - * It is invalid to accept() a new connection if an existing \ - * connection is still open. \ - */ \ - LO_FUNC(lo_interface net_stream_conn, accept) \ - \ - /** \ - * The net_stream_conn returned from accept() may still be \ - * valid after the listener is closed. \ - */ \ - LO_NEST(io_closer) -LO_INTERFACE(net_stream_listener); - #define net_stream_conn_LO_IFACE \ LO_NEST(io_readwriter) \ LO_NEST(io_bidi_closer) \ @@ -78,10 +49,27 @@ LO_INTERFACE(net_stream_listener); LO_FUNC(void, set_read_deadline, uint64_t ns_since_boot) LO_INTERFACE(net_stream_conn); +typedef lo_interface net_stream_conn net_stream_conn; +DECLARE_ERROR_OR(net_stream_conn); + +#define net_stream_listener_LO_IFACE \ + /** \ + * It is invalid to accept() a new connection if an existing \ + * connection is still open. \ + */ \ + LO_FUNC(net_stream_conn_or_error, accept) \ + \ + /** \ + * The net_stream_conn returned from accept() may still be \ + * valid after the listener is closed. \ + */ \ + LO_NEST(io_closer) +LO_INTERFACE(net_stream_listener); + /* Packets (e.g. UDP) *********************************************************/ #define net_packet_conn_LO_IFACE \ - LO_FUNC(ssize_t, sendto, \ + LO_FUNC(error, sendto, \ void *buf, size_t len, \ struct net_ip4_addr node, uint16_t port) \ \ @@ -90,7 +78,7 @@ LO_INTERFACE(net_stream_conn); * than the given `len` (as if the Linux MSG_TRUNC flag were \ * given). \ */ \ - LO_FUNC(ssize_t, recvfrom, \ + LO_FUNC(size_t_or_error, recvfrom, \ void *buf, size_t len, \ struct net_ip4_addr *ret_node, uint16_t *ret_port) \ \ @@ -125,7 +113,7 @@ struct net_iface_config { LO_FUNC(void , ifdown ) \ \ LO_FUNC(lo_interface net_stream_listener, tcp_listen, uint16_t local_port) \ - LO_FUNC(lo_interface net_stream_conn , tcp_dial , struct net_ip4_addr remote_node, uint16_t remote_port) \ + LO_FUNC(net_stream_conn_or_error , tcp_dial , struct net_ip4_addr remote_node, uint16_t remote_port) \ LO_FUNC(lo_interface net_packet_conn , udp_conn , uint16_t local_port) \ \ /** FIXME: arp_ping should probably have an explicit timeout or something. */ \ |