From de5eb613cacfabbd741d375f343a290ef6f507d2 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Mon, 20 Nov 2023 02:30:38 +0900 Subject: [PATCH] simple code cleanup in http-file.c partial code to support select --- lib/hio.c | 2 +- lib/http-file.c | 10 ++++------ lib/sys-mux.c | 39 +++++++++++++++++++++++++++++++++++++++ lib/sys-prv.h | 8 ++++++++ 4 files changed, 52 insertions(+), 7 deletions(-) diff --git a/lib/hio.c b/lib/hio.c index aec5735..09f0032 100644 --- a/lib/hio.c +++ b/lib/hio.c @@ -2057,7 +2057,7 @@ void hio_freemem (hio_t* hio, void* ptr) void hio_addcfmb (hio_t* hio, hio_cfmb_t* cfmb, hio_cfmb_checker_t checker, hio_cfmb_freeer_t freeer) { cfmb->cfmb_checker = checker; - cfmb->cfmb_freeer = freeer? freeer: hio_freemem; + cfmb->cfmb_freeer = freeer? freeer: (hio_cfmb_freeer_t)hio_freemem; HIO_CFMBL_APPEND_CFMB (&hio->cfmb, cfmb); } diff --git a/lib/http-file.c b/lib/http-file.c index 8b6c506..5cfe483 100644 --- a/lib/http-file.c +++ b/lib/http-file.c @@ -135,6 +135,7 @@ static void file_mark_over (file_t* file, int over_bits) { if (file->task_keep_client_alive) { + /* TODO: restore to the original value... */ set_tcp_cork (file->task_csck, 0); /* the file task must not be accessed from here down as it could have been destroyed */ @@ -226,7 +227,7 @@ static void file_client_on_disconnect (hio_dev_sck_t* sck) unbind_task_from_client (file, 1); /* the current file peer implemenation is not async. so there is no IO event associated - * when the client side is disconnecte, simple close the peer side as it's not needed. + * when the client side is disconnected, simple close the peer side as it's not needed. * this behavior is different from http-fcgi or http-cgi */ unbind_task_from_peer (file, 1); @@ -316,7 +317,7 @@ static int file_client_on_write (hio_dev_sck_t* sck, hio_iolen_t wrlen, void* wr { if (file->task_req_method == HIO_HTTP_GET) { - if (file_send_contents_to_client (file) <= -1) goto oops; + if (file_send_contents_to_client (file) <= -1) n = -1; } if ((file->over & FILE_OVER_READ_FROM_PEER) && file->task_res_pending_writes <= 0) @@ -327,10 +328,6 @@ static int file_client_on_write (hio_dev_sck_t* sck, hio_iolen_t wrlen, void* wr if (n <= -1 || wrlen <= -1) file_halt_participating_devices (file); return 0; - -oops: - file_halt_participating_devices (file); - return 0; } /* --------------------------------------------------------------------- */ @@ -741,6 +738,7 @@ static int bind_task_to_peer (file_t* file, hio_htre_t* req, const hio_bch_t* fi #if defined(HAVE_POSIX_FADVISE) posix_fadvise (file->peer, file->start_offset, file->end_offset - file->start_offset + 1, POSIX_FADV_SEQUENTIAL); #endif + /* TODO: store the current value and let the program restore to the current value when exiting.. */ set_tcp_cork (file->task_csck, 1); if (file_send_header_to_client(file, HIO_HTTP_STATUS_OK, 0, actual_mime_type) <= -1) goto oops; diff --git a/lib/sys-mux.c b/lib/sys-mux.c index 06f45af..7c75a89 100644 --- a/lib/sys-mux.c +++ b/lib/sys-mux.c @@ -22,6 +22,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#define _GNU_SOURCE #include "sys-prv.h" #include @@ -84,6 +85,9 @@ int hio_sys_initmux (hio_t* hio) mux->pd.dptr[idx] = HIO_NULL; mux->map.ptr[mux->ctrlp[0]] = idx; } +#elif defined(USE_SELECT) + FD_ZERO (&mux->rfds); + FD_ZERO (&mux->wfds); #elif defined(USE_KQUEUE) #if defined(HAVE_KQUEUE1) && defined(O_CLOEXEC) @@ -188,6 +192,10 @@ void hio_sys_finimux (hio_t* hio) } mux->pd.capa = 0; +#elif defined(USE_SELECT) + + /* nothing to do */ + #elif defined(USE_KQUEUE) if (mux->ctrlp[0] != HIO_SYSHND_INVALID) { @@ -409,6 +417,8 @@ int hio_sys_ctrlmux (hio_t* hio, hio_sys_mux_cmd_t cmd, hio_dev_t* dev, int dev_ return -1; } +#elif defined(USE_SELECT) + #elif defined(USE_KQUEUE) hio_sys_mux_t* mux = &hio->sysdep->mux; @@ -628,6 +638,35 @@ int hio_sys_waitmux (hio_t* hio, const hio_ntime_t* tmout, hio_sys_mux_evtcb_t e event_handler (hio, dev, events, 0); } } + +#elif defined(USE_SELECT) + +/* TODO: not complete */ + hio_sys_mux_t* mux = &hio->sysdep->mux; + struct timeval tv; + struct rfds, wfds; + int n; + + tv.tv_sec = tmout->sec; + tv.tv_usec = tmout->nsec / HIO_NSEC_PER_USEC; + + rfds = mux->rfds; + wfds = mux->wfds; + + n = select(mux->maxfd + 1, rfds, wfds, NULL, &tv); + if (n <= -1) + { + if (errno == EINTR) return 0; /* it's actually ok */ + /* other errors are critical - EBADF, EFAULT, EINVAL */ + hio_seterrwithsyserr (hio, 0, errno); + return -1; + } + + if (FD_ISSET(rfds, xxx)) + { + } + + #elif defined(USE_KQUEUE) hio_sys_mux_t* mux = &hio->sysdep->mux; diff --git a/lib/sys-prv.h b/lib/sys-prv.h index 876a9fc..c212191 100644 --- a/lib/sys-prv.h +++ b/lib/sys-prv.h @@ -69,6 +69,14 @@ struct hio_sys_mux_t int ctrlp[2]; }; +#elif defined(USE_SELECT) +struct hio_sys_mux_t +{ + fd_set rfds; + fd_set wfds; +}; + + #elif defined(USE_KQUEUE) struct hio_sys_mux_t