added moo_dev_timedread()

This commit is contained in:
hyung-hwan 2019-01-17 08:09:19 +00:00
parent 2372fc535e
commit 115031d2ce
6 changed files with 129 additions and 32 deletions

View File

@ -189,6 +189,7 @@ printf ("DEVICE accepted client device... .LOCAL %s:%d REMOTE %s:%d\n", buf1, mi
static int tcp_sck_on_write (mio_dev_sck_t* tcp, mio_iolen_t wrlen, void* wrctx, const mio_sckaddr_t* dstaddr)
{
tcp_server_t* ts;
mio_ntime_t tmout;
if (wrlen <= -1)
{
@ -204,19 +205,24 @@ else
// if (ts->tally >= 2) mio_dev_sck_halt (tcp);
printf ("TCP_SCK_ON_WRITE ENABLING READING..............................\n");
mio_dev_sck_read (tcp, 1);
//mio_dev_sck_timedread (tcp, 1, 1000);
mio_inittime (&tmout, 5, 0);
//mio_dev_sck_read (tcp, 1);
mio_dev_sck_timedread (tcp, 1, &tmout);
}
return 0;
}
/* TODO: serialize callbacks... */
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;
if (len <= 0)
if (len <= -1)
{
printf ("TCP_SCK_ON_READ STREAM DEVICE: TIMED OUT...\n");
mio_dev_sck_halt (tcp);
return 0;
}
else if (len <= 0)
{
printf ("TCP_SCK_ON_READ STREAM DEVICE: EOF RECEIVED...\n");
/* no outstanding request. but EOF */

View File

@ -1712,10 +1712,6 @@ int mio_dev_sck_timedwrite (mio_dev_sck_t* dev, const void* data, mio_iolen_t dl
}
/* ========================================================================= */
mio_uint16_t mio_checksumip (const void* hdr, mio_oow_t len)

View File

@ -564,10 +564,15 @@ static MIO_INLINE int mio_dev_sck_read (mio_dev_sck_t* sck, int enabled)
return mio_dev_read((mio_dev_t*)sck, enabled);
}
static MIO_INLINE int mio_dev_sck_timedread (mio_dev_sck_t* sck, int enabled, mio_ntime_t* tmout)
{
return mio_dev_timedread((mio_dev_t*)sck, enabled, tmout);
}
#else
#define mio_dev_sck_halt(sck) mio_dev_halt((mio_dev_t*)sck)
#define mio_dev_sck_read(sck,enabled) mio_dev_read((mio_dev_t*)sck, enabled)
#define mio_dev_sck_timedread(sck,enabled,tmout) mio_dev_timedread((mio_dev_t*)sck, enabled, tmout)
#endif

View File

@ -926,6 +926,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;
dev->rtmridx = MIO_TMRIDX_INVALID;
MIO_WQ_INIT (&dev->wq);
dev->cw_count = 0;
@ -1080,9 +1081,18 @@ void mio_killdev (mio_t* mio, mio_dev_t* dev)
if (dev->dev_capa & MIO_DEV_CAPA_ZOMBIE)
{
MIO_ASSERT (MIO_WQ_ISEMPTY(&dev->wq));
MIO_ASSERT (dev->cw_count == 0);
MIO_ASSERT (dev->rtmridx == MIO_TMRIDX_INVALID);
goto kill_device;
}
if (dev->rtmridx != MIO_TMRIDX_INVALID)
{
printf ("REMOVING.... TIMER FOR DEV\n");
mio_deltmrjob (mio, dev->rtmridx);
dev->rtmridx = MIO_TMRIDX_INVALID;
}
/* clear completed write event queues - TODO: do i need to fire these? */
if (dev->cw_count > 0)
{
@ -1248,8 +1258,81 @@ int mio_dev_watch (mio_dev_t* dev, mio_dev_watch_cmd_t cmd, int events)
return 0;
}
static void on_read_timeout (mio_t* mio, const mio_ntime_t* now, mio_tmrjob_t* job)
{
mio_dev_t* dev;
int x;
dev = (mio_dev_t*)job->ctx;
dev->mio->errnum = MIO_ETMOUT;
x = dev->dev_evcb->on_read(dev, MIO_NULL, -1, MIO_NULL);
MIO_ASSERT (dev->rtmridx == MIO_TMRIDX_INVALID);
if (x <= -1) mio_dev_halt (dev);
}
static int __dev_read (mio_dev_t* dev, int enabled, const mio_ntime_t* tmout, void* rdctx)
{
if (dev->dev_capa & MIO_DEV_CAPA_IN_CLOSED)
{
dev->mio->errnum = MIO_ENOCAPA;
return -1;
}
if (enabled)
{
dev->dev_capa &= ~MIO_DEV_CAPA_IN_DISABLED;
if (!dev->mio->in_exec && (dev->dev_capa & MIO_DEV_CAPA_IN_WATCHED)) goto renew_watch_now;
}
else
{
dev->dev_capa |= MIO_DEV_CAPA_IN_DISABLED;
if (!dev->mio->in_exec && !(dev->dev_capa & MIO_DEV_CAPA_IN_WATCHED)) goto renew_watch_now;
}
dev->mio->renew_watch = 1;
goto update_timer;
renew_watch_now:
if (mio_dev_watch(dev, MIO_DEV_WATCH_RENEW, 0) <= -1) return -1;
goto update_timer;
update_timer:
if (dev->rtmridx != MIO_TMRIDX_INVALID)
{
/* read timeout already on the socket. remove it first */
mio_deltmrjob (dev->mio, dev->rtmridx);
dev->rtmridx = MIO_TMRIDX_INVALID;
}
if (tmout && mio_ispostime(tmout))
{
mio_tmrjob_t tmrjob;
MIO_MEMSET (&tmrjob, 0, MIO_SIZEOF(tmrjob));
tmrjob.ctx = dev;
mio_gettime (&tmrjob.when);
mio_addtime (&tmrjob.when, tmout, &tmrjob.when);
tmrjob.handler = on_read_timeout;
tmrjob.idxptr = &dev->rtmridx;
dev->rtmridx = mio_instmrjob(dev->mio, &tmrjob);
if (dev->rtmridx == MIO_TMRIDX_INVALID)
{
/* if timer registration fails, timeout will never be triggered */
return -1;
}
}
return 0;
}
int mio_dev_read (mio_dev_t* dev, int enabled)
{
return __dev_read(dev, enabled, MIO_NULL, MIO_NULL);
#if 0
if (dev->dev_capa & MIO_DEV_CAPA_IN_CLOSED)
{
dev->mio->errnum = MIO_ENOCAPA;
@ -1273,6 +1356,12 @@ int mio_dev_read (mio_dev_t* dev, int enabled)
renew_watch_now:
if (mio_dev_watch(dev, MIO_DEV_WATCH_RENEW, 0) <= -1) return -1;
return 0;
#endif
}
int mio_dev_timedread (mio_dev_t* dev, int enabled, const mio_ntime_t* tmout)
{
return __dev_read(dev, enabled, tmout, MIO_NULL);
}
static void on_write_timeout (mio_t* mio, const mio_ntime_t* now, mio_tmrjob_t* job)

View File

@ -307,6 +307,7 @@ struct mio_wq_t
int dev_capa; \
mio_dev_mth_t* dev_mth; \
mio_dev_evcb_t* dev_evcb; \
mio_tmridx_t rtmridx; \
mio_wq_t wq; \
mio_oow_t cw_count; \
mio_dev_t* dev_prev; \