From e21acce5c1f790751cfaba02d286e316ef0fe33d Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Sun, 27 Jan 2019 16:22:55 +0000 Subject: [PATCH] more code refactoring --- mio/lib/mio-pro.c | 14 +++---- mio/lib/mio-prv.h | 6 +++ mio/lib/mio-sck.c | 32 ++++++++-------- mio/lib/mio.c | 94 ++++------------------------------------------- mio/lib/mio.h | 8 ++++ mio/lib/sys-mux.c | 15 +++++--- 6 files changed, 54 insertions(+), 115 deletions(-) diff --git a/mio/lib/mio-pro.c b/mio/lib/mio-pro.c index 9b46a75..556f549 100644 --- a/mio/lib/mio-pro.c +++ b/mio/lib/mio-pro.c @@ -234,7 +234,7 @@ static int dev_pro_make_master (mio_dev_t* dev, void* ctx) { if (pipe(&pfds[0]) == -1) { - dev->mio_seterrwithsyserr (mio, 0, errno); + mio_seterrwithsyserr (dev->mio, 0, errno); goto oops; } minidx = 0; maxidx = 1; @@ -244,7 +244,7 @@ static int dev_pro_make_master (mio_dev_t* dev, void* ctx) { if (pipe(&pfds[2]) == -1) { - dev->mio_seterrwithsyserr (mio, 0, errno); + mio_seterrwithsyserr (dev->mio, 0, errno); goto oops; } if (minidx == -1) minidx = 2; @@ -255,7 +255,7 @@ static int dev_pro_make_master (mio_dev_t* dev, void* ctx) { if (pipe(&pfds[4]) == -1) { - dev->mio_seterrwithsyserr (mio, 0, errno); + mio_seterrwithsyserr (dev->mio, 0, errno); goto oops; } if (minidx == -1) minidx = 4; @@ -264,7 +264,7 @@ static int dev_pro_make_master (mio_dev_t* dev, void* ctx) if (maxidx == -1) { - dev->mio->errnum = MIO_EINVAL; + mio_seterrnum (dev->mio, MIO_EINVAL); goto oops; } @@ -550,7 +550,7 @@ static int dev_pro_read_slave (mio_dev_t* dev, void* buf, mio_iolen_t* len, mio_ { if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0; /* no data available */ if (errno == EINTR) return 0; - pro->mio_seterrwithsyserr (mio, 0, errno); + mio_seterrwithsyserr (pro->mio, 0, errno); return -1; } @@ -568,7 +568,7 @@ static int dev_pro_write_slave (mio_dev_t* dev, const void* data, mio_iolen_t* l { if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0; /* no data can be written */ if (errno == EINTR) return 0; - pro->mio_seterrwithsyserr (mio, 0, errno); + mio_seterrwithsyserr (pro->mio, 0, errno); return -1; } @@ -618,7 +618,7 @@ static int dev_pro_ioctl (mio_dev_t* dev, int cmd, void* arg) { if (kill (rdev->child_pid, SIGKILL) == -1) { - rdev->mio_seterrwithsyserr (mio, 0, errno); + mio_seterrwithsyserr (rdev->mio, 0, errno); return -1; } } diff --git a/mio/lib/mio-prv.h b/mio/lib/mio-prv.h index c9a4675..66453bc 100644 --- a/mio/lib/mio-prv.h +++ b/mio/lib/mio-prv.h @@ -183,6 +183,12 @@ int mio_sys_ctrlmux ( int dev_capa ); +int mio_sys_waitmux ( + mio_t* mio, + const mio_ntime_t* tmout, + mio_sys_mux_evtcb_t event_handler +); + /** * The mio_sys_gettime() function gets the current time. */ diff --git a/mio/lib/mio-sck.c b/mio/lib/mio-sck.c index 7d6ee62..ae056a7 100644 --- a/mio/lib/mio-sck.c +++ b/mio/lib/mio-sck.c @@ -467,7 +467,7 @@ static int dev_sck_make_client (mio_dev_t* dev, void* ctx) int flags = fcntl(rdev->sck, F_GETFD, 0); if (fcntl(rdev->sck, F_SETFD, flags | FD_CLOEXEC) == -1) { - rdev->mio_seterrwithsyserr (mio, 0, errno); + mio_seterrwithsyserr (rdev->mio, 0, errno); return -1; } } @@ -562,7 +562,7 @@ static int dev_sck_read_stateful (mio_dev_t* dev, void* buf, mio_iolen_t* len, m { if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0; /* no data available */ if (errno == EINTR) return 0; - rdev->mio_seterrwithsyserr (mio, 0, errno); + mio_seterrwithsyserr (rdev->mio, 0, errno); return -1; } @@ -585,7 +585,7 @@ static int dev_sck_read_stateless (mio_dev_t* dev, void* buf, mio_iolen_t* len, { if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0; /* no data available */ if (errno == EINTR) return 0; - rdev->mio_seterrwithsyserr (mio, 0, errno); + mio_seterrwithsyserr (rdev->mio, 0, errno); return -1; } @@ -642,7 +642,7 @@ static int dev_sck_write_stateful (mio_dev_t* dev, const void* data, mio_iolen_t * the socket, probably leaving it in the half-closed state */ if (shutdown(rdev->sck, SHUT_WR) == -1) { - rdev->mio_seterrwithsyserr (mio, 0, errno); + mio_seterrwithsyserr (rdev->mio, 0, errno); return -1; } @@ -658,7 +658,7 @@ static int dev_sck_write_stateful (mio_dev_t* dev, const void* data, mio_iolen_t { if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0; /* no data can be written */ if (errno == EINTR) return 0; - rdev->mio_seterrwithsyserr (mio, 0, errno); + mio_seterrwithsyserr (rdev->mio, 0, errno); return -1; } @@ -679,7 +679,7 @@ static int dev_sck_write_stateless (mio_dev_t* dev, const void* data, mio_iolen_ { if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0; /* no data can be written */ if (errno == EINTR) return 0; - rdev->mio_seterrwithsyserr (mio, 0, errno); + mio_seterrwithsyserr (rdev->mio, 0, errno); return -1; } @@ -798,7 +798,7 @@ static int dev_sck_ioctl (mio_dev_t* dev, int cmd, void* arg) int v = 1; if (setsockopt (rdev->sck, SOL_SOCKET, SO_BROADCAST, &v, MIO_SIZEOF(v)) == -1) { - rdev->mio_seterrwithsyserr (mio, 0, errno); + mio_seterrwithsyserr (rdev->mio, 0, errno); return -1; } } @@ -809,7 +809,7 @@ static int dev_sck_ioctl (mio_dev_t* dev, int cmd, void* arg) int v = 1; if (setsockopt (rdev->sck, SOL_SOCKET, SO_REUSEADDR, &v, MIO_SIZEOF(v)) == -1) { - rdev->mio_seterrwithsyserr (mio, 0, errno); + mio_seterrwithsyserr (rdev->mio, 0, errno); return -1; } #else @@ -824,7 +824,7 @@ static int dev_sck_ioctl (mio_dev_t* dev, int cmd, void* arg) int v = 1; if (setsockopt (rdev->sck, SOL_SOCKET, SO_REUSEPORT, &v, MIO_SIZEOF(v)) == -1) { - rdev->mio_seterrwithsyserr (mio, 0, errno); + mio_seterrwithsyserr (rdev->mio, 0, errno); return -1; } #else @@ -839,7 +839,7 @@ static int dev_sck_ioctl (mio_dev_t* dev, int cmd, void* arg) int v = 1; if (setsockopt(rdev->sck, SOL_IP, IP_TRANSPARENT, &v, MIO_SIZEOF(v)) == -1) { - rdev->mio_seterrwithsyserr (mio, 0, errno); + mio_seterrwithsyserr (rdev->mio, 0, errno); return -1; } #else @@ -909,7 +909,7 @@ static int dev_sck_ioctl (mio_dev_t* dev, int cmd, void* arg) x = bind(rdev->sck, sa, sl); if (x == -1) { - rdev->mio_seterrwithsyserr (mio, 0, errno); + mio_seterrwithsyserr (rdev->mio, 0, errno); #if defined(USE_SSL) if (ssl_ctx) SSL_CTX_free (ssl_ctx); #endif @@ -1033,7 +1033,7 @@ fcntl (rdev->sck, F_SETFL, flags | O_NONBLOCK); } } - rdev->mio_seterrwithsyserr (mio, 0, errno); + mio_seterrwithsyserr (rdev->mio, 0, errno); oops_connect: if (mio_dev_watch((mio_dev_t*)rdev, MIO_DEV_WATCH_UPDATE, MIO_DEV_EVENT_IN) <= -1) @@ -1139,7 +1139,7 @@ fcntl (rdev->sck, F_SETFL, flags | O_NONBLOCK); x = listen (rdev->sck, lstn->backlogs); if (x == -1) { - rdev->mio_seterrwithsyserr (mio, 0, errno); + mio_seterrwithsyserr (rdev->mio, 0, errno); return -1; } @@ -1196,7 +1196,7 @@ static int harvest_outgoing_connection (mio_dev_sck_t* rdev) len = MIO_SIZEOF(errcode); if (getsockopt(rdev->sck, SOL_SOCKET, SO_ERROR, (char*)&errcode, &len) == -1) { - rdev->mio_seterrwithsyserr (mio, 0, errno); + mio_seterrwithsyserr (rdev->mio, 0, errno); return -1; } else if (errcode == 0) @@ -1300,7 +1300,7 @@ static int accept_incoming_connection (mio_dev_sck_t* rdev) if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0; if (errno == EINTR) return 0; /* if interrupted by a signal, treat it as if it's EINPROGRESS */ - rdev->mio_seterrwithsyserr (mio, 0, errno); + mio_seterrwithsyserr (rdev->mio, 0, errno); return -1; } @@ -1319,7 +1319,7 @@ static int accept_incoming_connection (mio_dev_sck_t* rdev) if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0; if (errno == EINTR) return 0; /* if interrupted by a signal, treat it as if it's EINPROGRESS */ - rdev->mio_seterrwithsyserr (mio, 0, errno); + mio_seterrwithsyserr (rdev->mio, 0, errno); return -1; } diff --git a/mio/lib/mio.c b/mio/lib/mio.c index 14e4a8c..97f058b 100644 --- a/mio/lib/mio.c +++ b/mio/lib/mio.c @@ -271,11 +271,10 @@ static MIO_INLINE void unlink_wq (mio_t* mio, mio_wq_t* q) MIO_WQ_UNLINK (q); } -static MIO_INLINE void handle_event (mio_dev_t* dev, int events, int rdhup) +static MIO_INLINE void handle_event (mio_t* mio, mio_dev_t* dev, int events, int rdhup) { - mio_t* mio; + MIO_ASSERT (mio, mio == dev->mio); - mio = dev->mio; mio->renew_watch = 0; MIO_ASSERT (mio, mio == dev->mio); @@ -538,9 +537,8 @@ skip_evcb: static MIO_INLINE int __exec (mio_t* mio) { mio_ntime_t tmout; - - int nentries, i; mio_sys_mux_t* mux; + int ret = 0; /*if (!mio->actdev.head) return 0;*/ @@ -580,97 +578,21 @@ static MIO_INLINE int __exec (mio_t* mio) tmout.nsec = 0; } -#if defined(_WIN32) -/* - if (GetQueuedCompletionStatusEx (mio->iocp, mio->ovls, MIO_COUNTOF(mio->ovls), &nentries, timeout, FALSE) == FALSE) + if (mio_sys_waitmux(mio, &tmout, handle_event) <= -1) { - // TODO: set errnum - return -1; + MIO_DEBUG0 (mio, "WARNING - Failed to wait on mutiplexer\n"); + ret = -1; } - for (i = 0; i < nentries; i++) - { - } -*/ -#elif defined(USE_POLL) - - mux = (mio_sys_mux_t*)mio->sys.mux; - - nentries = poll(mux->pd.pfd, mux->pd.size, MIO_SECNSEC_TO_MSEC(tmout.sec, tmout.nsec)); - if (nentries == -1) - { - if (errno == EINTR) return 0; - mio_seterrwithsyserr (mio, 0, errno); - return -1; - } - - for (i = 0; i < mux->pd.size; i++) - { - if (mux->pd.pfd[i].fd >= 0 && mux->pd.pfd[i].revents) - { - int events = 0; - mio_dev_t* dev; - - dev = mux->pd.dptr[i]; - - MIO_ASSERT (mio, !(mux->pd.pfd[i].revents & POLLNVAL)); - if (mux->pd.pfd[i].revents & POLLIN) events |= MIO_DEV_EVENT_IN; - if (mux->pd.pfd[i].revents & POLLOUT) events |= MIO_DEV_EVENT_OUT; - if (mux->pd.pfd[i].revents & POLLPRI) events |= MIO_DEV_EVENT_PRI; - if (mux->pd.pfd[i].revents & POLLERR) events |= MIO_DEV_EVENT_ERR; - if (mux->pd.pfd[i].revents & POLLHUP) events |= MIO_DEV_EVENT_HUP; - - handle_event (dev, events, 0); - } - } - -#elif defined(USE_EPOLL) - - mux = (mio_sys_mux_t*)mio->sys.mux; - - nentries = epoll_wait(mux->hnd, mux->revs, MIO_COUNTOF(mux->revs), MIO_SECNSEC_TO_MSEC(tmout.sec, tmout.nsec)); - if (nentries == -1) - { - if (errno == EINTR) return 0; /* it's actually ok */ - /* other errors are critical - EBADF, EFAULT, EINVAL */ - mio_seterrwithsyserr (mio, 0, errno); - return -1; - } - - /* TODO: merge events??? for the same descriptor */ - - for (i = 0; i < nentries; i++) - { - int events = 0, rdhup = 0; - mio_dev_t* dev; - - dev = mux->revs[i].data.ptr; - - if (mux->revs[i].events & EPOLLIN) events |= MIO_DEV_EVENT_IN; - if (mux->revs[i].events & EPOLLOUT) events |= MIO_DEV_EVENT_OUT; - if (mux->revs[i].events & EPOLLPRI) events |= MIO_DEV_EVENT_PRI; - if (mux->revs[i].events & EPOLLERR) events |= MIO_DEV_EVENT_ERR; - if (mux->revs[i].events & EPOLLHUP) events |= MIO_DEV_EVENT_HUP; - #if defined(EPOLLRDHUP) - else if (mux->revs[i].events & EPOLLRDHUP) rdhup = 1; - #endif - handle_event (dev, events, rdhup); - } - -#else - -# error NO SUPPORTED MULTIPLEXER -#endif - /* kill all halted devices */ while (mio->hltdev.head) { MIO_DEBUG1 (mio, "Killing HALTED device %p\n", mio->hltdev.head); mio_killdev (mio, mio->hltdev.head); } - MIO_ASSERT (mio, mio->hltdev.tail == MIO_NULL); - return 0; + MIO_ASSERT (mio, mio->hltdev.tail == MIO_NULL); + return ret; } int mio_exec (mio_t* mio) diff --git a/mio/lib/mio.h b/mio/lib/mio.h index 93e551d..d8726f6 100644 --- a/mio/lib/mio.h +++ b/mio/lib/mio.h @@ -456,6 +456,14 @@ enum mio_sys_mux_cmd_t }; typedef enum mio_sys_mux_cmd_t mio_sys_mux_cmd_t; + +typedef void (*mio_sys_mux_evtcb_t) ( + mio_t* mio, + mio_dev_t* dev, + int events, + int rdhup +); + typedef struct mio_sys_mux_t mio_sys_mux_t; typedef struct mio_sys_log_t mio_sys_log_t; diff --git a/mio/lib/sys-mux.c b/mio/lib/sys-mux.c index 9e78524..97ddea4 100644 --- a/mio/lib/sys-mux.c +++ b/mio/lib/sys-mux.c @@ -255,12 +255,12 @@ void mio_sys_finimux (mio_t* mio) if (mio->sys.mux) { close (mio->sys.mux->hnd); - mio_Freemem (mio, mio->sys.mux); + mio_freemem (mio, mio->sys.mux); mio->sys.mux = MIO_NULL; } } -int mio_sys_ctrlmux (mio_t* mio, int cmd, mio_dev_t* dev, int dev_capa) +int mio_sys_ctrlmux (mio_t* mio, mio_sys_mux_cmd_t cmd, mio_dev_t* dev, int dev_capa) { static int epoll_cmd[] = { EPOLL_CTL_ADD, EPOLL_CTL_MOD, EPOLL_CTL_DEL }; struct epoll_event ev; @@ -291,7 +291,7 @@ int mio_sys_ctrlmux (mio_t* mio, int cmd, mio_dev_t* dev, int dev_capa) } #endif -static void mio_sys_waitmux (mio_t* mio) +int mio_sys_waitmux (mio_t* mio, const mio_ntime_t* tmout, mio_sys_mux_evtcb_t event_handler) { #if defined(_WIN32) /* @@ -306,6 +306,7 @@ static void mio_sys_waitmux (mio_t* mio) } */ #elif defined(USE_POLL) + int nentries; mux = (mio_sys_mux_t*)mio->sys.mux; @@ -339,9 +340,10 @@ static void mio_sys_waitmux (mio_t* mio) #elif defined(USE_EPOLL) - mux = (mio_sys_mux_t*)mio->sys.mux; + mio_sys_mux_t* mux = mio->sys.mux; + int nentries, i; - nentries = epoll_wait(mux->hnd, mux->revs, MIO_COUNTOF(mux->revs), MIO_SECNSEC_TO_MSEC(tmout.sec, tmout.nsec)); + nentries = epoll_wait(mux->hnd, mux->revs, MIO_COUNTOF(mux->revs), MIO_SECNSEC_TO_MSEC(tmout->sec, tmout->nsec)); if (nentries == -1) { if (errno == EINTR) return 0; /* it's actually ok */ @@ -367,7 +369,8 @@ static void mio_sys_waitmux (mio_t* mio) #if defined(EPOLLRDHUP) else if (mux->revs[i].events & EPOLLRDHUP) rdhup = 1; #endif - handle_event (dev, events, rdhup); + + event_handler (mio, dev, events, rdhup); } #else