From: Cyrill Gorcunov <gorcunov@gmail.com> To: tml <tarantool-patches@dev.tarantool.org> Subject: [Tarantool-patches] [PATCH v6 1/4] coio: Export helpers and provide coio_read_fd_timeout Date: Tue, 17 Dec 2019 15:54:17 +0300 [thread overview] Message-ID: <20191217125420.20881-2-gorcunov@gmail.com> (raw) In-Reply-To: <20191217125420.20881-1-gorcunov@gmail.com> There is no reason to hide functions. In particular we will use coio_write_fd_timeout and coio_read_fd_timeout for popen. Part-of #4031 Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com> --- src/box/applier.cc | 2 +- src/lib/core/coio.cc | 41 +++++++++++++++++++++++++++++++++++++++++ src/lib/core/coio.h | 31 ++++++++++++++++++++++++++----- 3 files changed, 68 insertions(+), 6 deletions(-) diff --git a/src/box/applier.cc b/src/box/applier.cc index 42374f886..a44c6693f 100644 --- a/src/box/applier.cc +++ b/src/box/applier.cc @@ -935,7 +935,7 @@ applier_disconnect(struct applier *applier, enum applier_state state) applier->writer = NULL; } - coio_close(loop(), &applier->io); + coio_close_io(loop(), &applier->io); /* Clear all unparsed input. */ ibuf_reinit(&applier->ibuf); fiber_gc(); diff --git a/src/lib/core/coio.cc b/src/lib/core/coio.cc index e88d724d5..b16497b7b 100644 --- a/src/lib/core/coio.cc +++ b/src/lib/core/coio.cc @@ -740,6 +740,47 @@ int coio_close(int fd) return close(fd); } +ssize_t +coio_read_fd_timeout(int fd, void *data, size_t size, ev_tstamp timeout) +{ + ev_tstamp start, delay; + ev_loop *loop = loop(); + ssize_t pos = 0; + + evio_timeout_init(loop, &start, &delay, timeout); + + while (size > 0) { + ssize_t rc = read(fd, data, size); + if (rc > 0) { + size -= (size_t)rc; + pos += rc; + data = (char *)data + rc; + continue; + } else if (rc == 0) { + /* + * Socket peer could be closed + * or shot down. + */ + break; + } + + if (delay <= 0) { + diag_set(TimedOut); + return -1; + } + + if (errno == EAGAIN || errno == EWOULDBLOCK) { + coio_wait(fd, COIO_READ, delay); + evio_timeout_update(loop, &start, &delay); + } else if (errno != EINTR) { + diag_set(SocketError, sio_socketname(fd), "read"); + return -1; + } + } + + return pos; +} + int coio_write_fd_timeout(int fd, const void *data, size_t size, ev_tstamp timeout) { diff --git a/src/lib/core/coio.h b/src/lib/core/coio.h index 6a2337689..e1b80ad68 100644 --- a/src/lib/core/coio.h +++ b/src/lib/core/coio.h @@ -32,9 +32,16 @@ */ #include "fiber.h" #include "trivia/util.h" -#if defined(__cplusplus) + #include "evio.h" +#if defined(__cplusplus) +extern "C" { +#endif /* defined(__cplusplus) */ + +struct sockaddr; +struct uri; + /** * Co-operative I/O * Yield the current fiber until IO is ready. @@ -70,8 +77,12 @@ coio_accept(struct ev_io *coio, struct sockaddr *addr, socklen_t addrlen, void coio_create(struct ev_io *coio, int fd); +/* + * Due to name conflict with coio_close in API_EXPORT + * we have to use coio_close_io() instead of plain coio_close(). + */ static inline void -coio_close(ev_loop *loop, struct ev_io *coio) +coio_close_io(ev_loop *loop, struct ev_io *coio) { return evio_close(loop, coio); } @@ -185,9 +196,6 @@ coio_stat_stat_timeout(ev_stat *stat, ev_tstamp delay); int coio_waitpid(pid_t pid); -extern "C" { -#endif /* defined(__cplusplus) */ - /** \cond public */ enum { @@ -232,6 +240,19 @@ coio_close(int fd); int coio_write_fd_timeout(int fd, const void *data, size_t size, ev_tstamp timeout); +/** + * Read from a socket in at most @a timeout seconds. + * @param fd Socket descriptor. + * @param data Data to read. + * @param size Size of @a data. + * @param timeout Timeout on the operation. + * + * @retval >= 0 Read bytes (0 treated as EOF). + * @retval -1 Timeout or socket error. + */ +ssize_t +coio_read_fd_timeout(int fd, void *data, size_t size, ev_tstamp timeout); + #if defined(__cplusplus) } /* extern "C" */ #endif /* defined(__cplusplus) */ -- 2.20.1
next prev parent reply other threads:[~2019-12-17 12:54 UTC|newest] Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top 2019-12-17 12:54 [Tarantool-patches] [PATCH v6 0/4] popen: Add ability to run external process Cyrill Gorcunov 2019-12-17 12:54 ` Cyrill Gorcunov [this message] 2019-12-20 7:48 ` [Tarantool-patches] [PATCH v6 1/4] coio: Export helpers and provide coio_read_fd_timeout Konstantin Osipov 2019-12-20 14:50 ` Cyrill Gorcunov 2019-12-17 12:54 ` [Tarantool-patches] [PATCH v6 2/4] popen: Introduce a backend engine Cyrill Gorcunov 2019-12-20 8:11 ` Konstantin Osipov 2019-12-20 11:52 ` Cyrill Gorcunov 2019-12-20 12:04 ` Konstantin Osipov 2019-12-20 12:10 ` Cyrill Gorcunov 2019-12-20 12:11 ` Alexander Turenko 2019-12-26 7:14 ` Konstantin Osipov 2019-12-26 7:19 ` Cyrill Gorcunov 2020-01-09 11:23 ` Cyrill Gorcunov 2019-12-17 12:54 ` [Tarantool-patches] [PATCH v6 3/4] popen/lua: Add popen module Cyrill Gorcunov 2019-12-20 15:41 ` Maxim Melentiev 2019-12-17 12:54 ` [Tarantool-patches] [PATCH v6 4/4] popen/test: Add base test cases Cyrill Gorcunov
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20191217125420.20881-2-gorcunov@gmail.com \ --to=gorcunov@gmail.com \ --cc=tarantool-patches@dev.tarantool.org \ --subject='Re: [Tarantool-patches] [PATCH v6 1/4] coio: Export helpers and provide coio_read_fd_timeout' \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox