diff --git a/mio/lib/main.c b/mio/lib/main.c index 415df6f..020bd48 100644 --- a/mio/lib/main.c +++ b/mio/lib/main.c @@ -215,6 +215,7 @@ printf ("TCP_SCK_ON_WRITE ENABLING READING..............................\n"); static int tcp_sck_on_read (mio_dev_sck_t* tcp, const void* buf, mio_iolen_t len, const mio_sckaddr_t* srcaddr) { int n; +static int x = 0; if (len <= -1) { @@ -448,7 +449,7 @@ static int schedule_icmp_wait (mio_dev_sck_t* dev) assert (icmpxtn->tmout_jobidx == MIO_TMRIDX_INVALID); - return (mio_instmrjob (dev->mio, &tmrjob) == MIO_TMRIDX_INVALID)? -1: 0; + return (mio_instmrjob(dev->mio, &tmrjob) == MIO_TMRIDX_INVALID)? -1: 0; } static int icmp_sck_on_read (mio_dev_sck_t* dev, const void* data, mio_iolen_t dlen, const mio_sckaddr_t* srcaddr) @@ -552,7 +553,7 @@ static int setup_ping4_tester (mio_t* mio) /* ========================================================================= */ -#if 0 +#if 1 static mio_t* g_mio; static void handle_signal (int sig) @@ -771,8 +772,7 @@ oops: return -1; } -#endif -#if 1 +#else int main (int argc, char* argv[]) { mio_t* mio = MIO_NULL; diff --git a/mio/lib/mio.c b/mio/lib/mio.c index fe31c13..5dc3155 100644 --- a/mio/lib/mio.c +++ b/mio/lib/mio.c @@ -61,6 +61,8 @@ static int kill_and_free_device (mio_dev_t* dev, int force); } while (0) +static void on_read_timeout (mio_t* mio, const mio_ntime_t* now, mio_tmrjob_t* job); +static void on_write_timeout (mio_t* mio, const mio_ntime_t* now, mio_tmrjob_t* job); /* ========================================================================= */ #if defined(USE_POLL) @@ -631,13 +633,34 @@ static MIO_INLINE void handle_event (mio_dev_t* dev, int events, int rdhup) dev = MIO_NULL; break; } - else if (x == 0) + + if (dev->rtmridx != MIO_TMRIDX_INVALID) + { + /* delete the read timeout job on the device as the + * read operation will be reported below. */ + mio_tmrjob_t tmrjob; + + MIO_MEMSET (&tmrjob, 0, MIO_SIZEOF(tmrjob)); + tmrjob.ctx = dev; + mio_gettime (&tmrjob.when); + mio_addtime (&tmrjob.when, &dev->rtmout, &tmrjob.when); + tmrjob.handler = on_read_timeout; + tmrjob.idxptr = &dev->rtmridx; + + mio_updtmrjob (mio, dev->rtmridx, &tmrjob); + + /*mio_deltmrjob (mio, dev->rtmridx); + dev->rtmridx = MIO_TMRIDX_INVALID;*/ + } + + if (x == 0) { /* no data is available - EWOULDBLOCK or something similar */ break; } - else if (x >= 1) + else /*if (x >= 1) */ { + if (len <= 0 && (dev->dev_capa & MIO_DEV_CAPA_STREAM)) { /* EOF received. for a stream device, a zero-length @@ -645,6 +668,7 @@ static MIO_INLINE void handle_event (mio_dev_t* dev, int events, int rdhup) dev->dev_capa |= MIO_DEV_CAPA_IN_CLOSED; mio->renew_watch = 1; + /* call the on_read callback to report EOF */ if (dev->dev_evcb->on_read(dev, mio->bigbuf, len, &srcaddr) <= -1 || (dev->dev_capa & MIO_DEV_CAPA_OUT_CLOSED)) @@ -926,6 +950,7 @@ mio_dev_t* mio_makedev (mio_t* mio, mio_oow_t dev_size, mio_dev_mth_t* dev_mth, dev->dev_capa = MIO_DEV_CAPA_IN | MIO_DEV_CAPA_OUT; dev->dev_mth = dev_mth; dev->dev_evcb = dev_evcb; + mio_inittime (&dev->rtmout, 0, 0); dev->rtmridx = MIO_TMRIDX_INVALID; MIO_WQ_INIT (&dev->wq); dev->cw_count = 0; @@ -1088,7 +1113,6 @@ void mio_killdev (mio_t* mio, mio_dev_t* dev) if (dev->rtmridx != MIO_TMRIDX_INVALID) { -printf ("REMOVING.... TIMER FOR DEV\n"); mio_deltmrjob (mio, dev->rtmridx); dev->rtmridx = MIO_TMRIDX_INVALID; } @@ -1324,6 +1348,7 @@ update_timer: /* if timer registration fails, timeout will never be triggered */ return -1; } + dev->rtmout = *tmout; } return 0; } diff --git a/mio/lib/mio.h b/mio/lib/mio.h index 2d4ea52..ec423db 100644 --- a/mio/lib/mio.h +++ b/mio/lib/mio.h @@ -307,6 +307,7 @@ struct mio_wq_t int dev_capa; \ mio_dev_mth_t* dev_mth; \ mio_dev_evcb_t* dev_evcb; \ + mio_ntime_t rtmout; \ mio_tmridx_t rtmridx; \ mio_wq_t wq; \ mio_oow_t cw_count; \ @@ -431,7 +432,13 @@ MIO_EXPORT int mio_dev_watch ( MIO_EXPORT int mio_dev_read ( mio_dev_t* dev, - int enabled + int enabled +); + +MIO_EXPORT int mio_dev_timedread ( + mio_dev_t* dev, + int enabled, + const mio_ntime_t* tmout ); /**