hio/stio/lib/stio.h

463 lines
13 KiB
C
Raw Normal View History

2016-01-26 16:07:52 +00:00
/*
* $Id$
*
Copyright (c) 2015-2016 Chung, Hyung-Hwan. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAfRRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
2016-01-26 16:02:20 +00:00
#ifndef _STIO_H_
#define _STIO_H_
2016-01-30 05:24:23 +00:00
#include <stio-cmn.h>
2016-01-26 16:02:20 +00:00
/**
* The stio_ntime_t type defines a numeric time type expressed in the
* number of milliseconds since the Epoch (00:00:00 UTC, Jan 1, 1970).
*/
typedef struct stio_ntime_t stio_ntime_t;
struct stio_ntime_t
{
stio_intptr_t sec;
stio_int32_t nsec; /* nanoseconds */
};
2016-01-26 16:02:20 +00:00
#if defined(_WIN32)
2016-01-30 05:24:23 +00:00
typedef stio_uintptr_t qse_syshnd_t;
#define STIO_SYSHND_INVALID (~(stio_uintptr_t)0)
2016-01-26 16:02:20 +00:00
#else
2016-01-30 05:24:23 +00:00
typedef int stio_syshnd_t;
#define STIO_SYSHND_INVALID (-1)
2016-01-26 16:02:20 +00:00
#endif
2016-03-30 16:31:56 +00:00
typedef struct stio_devadr_t stio_devadr_t;
struct stio_devadr_t
2016-03-29 15:02:01 +00:00
{
int len;
2016-03-29 15:02:01 +00:00
void* ptr;
};
#define STIO_CONST_SWAP16(x) \
((stio_uint16_t)((((stio_uint16_t)(x) & (stio_uint16_t)0x00ffU) << 8) | \
(((stio_uint16_t)(x) & (stio_uint16_t)0xff00U) >> 8) ))
#define STIO_CONST_SWAP32(x) \
((stio_uint32_t)((((stio_uint32_t)(x) & (stio_uint32_t)0x000000ffUL) << 24) | \
(((stio_uint32_t)(x) & (stio_uint32_t)0x0000ff00UL) << 8) | \
(((stio_uint32_t)(x) & (stio_uint32_t)0x00ff0000UL) >> 8) | \
(((stio_uint32_t)(x) & (stio_uint32_t)0xff000000UL) >> 24) ))
#if defined(STIO_ENDIAN_LITTLE)
# define STIO_CONST_NTOH16(x) STIO_CONST_SWAP16(x)
# define STIO_CONST_HTON16(x) STIO_CONST_SWAP16(x)
# define STIO_CONST_NTOH32(x) STIO_CONST_SWAP32(x)
# define STIO_CONST_HTON32(x) STIO_CONST_SWAP32(x)
#elif defined(STIO_ENDIAN_BIG)
# define STIO_CONST_NTOH16(x)
# define STIO_CONST_HTON16(x)
# define STIO_CONST_NTOH32(x)
# define STIO_CONST_HTON32(x)
#else
# error UNKNOWN ENDIAN
#endif
2016-01-26 16:02:20 +00:00
/* ------------------------------------------------------------------------- */
typedef struct stio_t stio_t;
typedef struct stio_dev_t stio_dev_t;
typedef struct stio_dev_mth_t stio_dev_mth_t;
typedef struct stio_dev_evcb_t stio_dev_evcb_t;
typedef struct stio_wq_t stio_wq_t;
2016-02-05 13:25:59 +00:00
typedef stio_intptr_t stio_len_t; /* NOTE: this is a signed type */
2016-01-26 16:02:20 +00:00
enum stio_errnum_t
{
STIO_ENOERR,
STIO_ENOMEM,
STIO_EINVAL,
2016-01-30 05:24:23 +00:00
STIO_ENOENT,
2016-02-05 13:25:59 +00:00
STIO_ENOSUP, /* not supported */
STIO_EMFILE, /* too many open files */
2016-01-31 02:05:39 +00:00
STIO_ENFILE,
2016-02-05 13:25:59 +00:00
STIO_ECONRF, /* connection refused */
STIO_ECONRS, /* connection reset */
STIO_ENOCAPA, /* no capability */
STIO_ETMOUT, /* timed out */
2016-01-26 16:02:20 +00:00
2016-01-31 02:05:39 +00:00
STIO_EDEVMAKE,
2016-02-01 07:42:49 +00:00
STIO_EDEVERR,
STIO_EDEVHUP,
2016-01-31 02:05:39 +00:00
STIO_ESYSERR
2016-01-26 16:02:20 +00:00
};
typedef enum stio_errnum_t stio_errnum_t;
2016-02-05 13:25:59 +00:00
typedef struct stio_tmrjob_t stio_tmrjob_t;
typedef stio_size_t stio_tmridx_t;
typedef void (*stio_tmrjob_handler_t) (
stio_t* stio,
const stio_ntime_t* now,
stio_tmrjob_t* tmrjob
);
typedef void (*stio_tmrjob_updater_t) (
stio_t* stio,
stio_tmridx_t old_index,
stio_tmridx_t new_index,
stio_tmrjob_t* tmrjob
);
2016-01-26 16:02:20 +00:00
struct stio_dev_mth_t
{
/* ------------------------------------------------------------------ */
2016-01-30 05:24:23 +00:00
int (*make) (stio_dev_t* dev, void* ctx); /* mandatory. called in stio_makedev() */
/* ------------------------------------------------------------------ */
2016-01-30 05:24:23 +00:00
void (*kill) (stio_dev_t* dev); /* mandatory. called in stio_killdev(). called in stio_makedev() upon failure after make() success */
2016-01-26 16:02:20 +00:00
/* ------------------------------------------------------------------ */
2016-01-30 05:24:23 +00:00
stio_syshnd_t (*getsyshnd) (stio_dev_t* dev); /* mandatory. called in stio_makedev() after successful make() */
2016-01-26 16:02:20 +00:00
/* ------------------------------------------------------------------ */
2016-01-30 05:24:23 +00:00
/* return -1 on failure, 0 if no data is availble, 1 otherwise.
* when returning 1, *len must be sent to the length of data read.
* if *len is set to 0, it's treated as EOF. */
2016-03-30 16:31:56 +00:00
int (*read) (stio_dev_t* dev, void* data, stio_len_t* len, stio_devadr_t* srcadr);
/* ------------------------------------------------------------------ */
2016-03-30 16:31:56 +00:00
int (*write) (stio_dev_t* dev, const void* data, stio_len_t* len, const stio_devadr_t* dstadr);
/* ------------------------------------------------------------------ */
int (*ioctl) (stio_dev_t* dev, int cmd, void* arg);
2016-01-26 16:02:20 +00:00
};
struct stio_dev_evcb_t
{
/* return -1 on failure. 0 or 1 on success.
* when 0 is returned, it doesn't attempt to perform actual I/O.
* when 1 is returned, it attempts to perform actual I/O. */
int (*ready) (stio_dev_t* dev, int events);
/* return -1 on failure, 0 or 1 on success.
* when 0 is returned, the main loop stops the attempt to read more data.
* when 1 is returned, the main loop attempts to read more data without*/
2016-03-30 16:31:56 +00:00
int (*on_read) (stio_dev_t* dev, const void* data, stio_len_t len, const stio_devadr_t* srcadr);
/* return -1 on failure, 0 on success.
2016-02-05 13:25:59 +00:00
* wrlen is the length of data written. it is the length of the originally
* posted writing request for a stream device. For a non stream device, it
* may be shorter than the originally posted length. */
2016-03-30 16:31:56 +00:00
int (*on_write) (stio_dev_t* dev, stio_len_t wrlen, void* wrctx, const stio_devadr_t* dstadr);
};
struct stio_wq_t
{
stio_wq_t* next;
stio_wq_t* prev;
2016-03-29 15:02:01 +00:00
stio_len_t olen; /* original data length */
stio_uint8_t* ptr; /* pointer to data */
stio_len_t len; /* remaining data length */
void* ctx;
2016-02-05 13:25:59 +00:00
stio_dev_t* dev; /* back-pointer to the device */
stio_tmridx_t tmridx;
2016-03-30 16:31:56 +00:00
stio_devadr_t dstadr;
2016-01-26 16:02:20 +00:00
};
#define STIO_WQ_INIT(wq) ((wq)->next = (wq)->prev = (wq))
#define STIO_WQ_TAIL(wq) ((wq)->prev)
#define STIO_WQ_HEAD(wq) ((wq)->next)
#define STIO_WQ_ISEMPTY(wq) (STIO_WQ_HEAD(wq) == (wq))
#define STIO_WQ_ISNODE(wq,x) ((wq) != (x))
#define STIO_WQ_ISHEAD(wq,x) (STIO_WQ_HEAD(wq) == (x))
#define STIO_WQ_ISTAIL(wq,x) (STIO_WQ_TAIL(wq) == (x))
#define STIO_WQ_NEXT(x) ((x)->next)
#define STIO_WQ_PREV(x) ((x)->prev)
#define STIO_WQ_LINK(p,x,n) do { \
stio_wq_t* pp = (p), * nn = (n); \
(x)->prev = (p); \
(x)->next = (n); \
nn->prev = (x); \
pp->next = (x); \
} while (0)
#define STIO_WQ_UNLINK(x) do { \
stio_wq_t* pp = (x)->prev, * nn = (x)->next; \
nn->prev = pp; pp->next = nn; \
} while (0)
#define STIO_WQ_REPL(o,n) do { \
stio_wq_t* oo = (o), * nn = (n); \
nn->next = oo->next; \
nn->next->prev = nn; \
nn->prev = oo->prev; \
nn->prev->next = nn; \
} while (0)
/* insert an item at the back of the queue */
/*#define STIO_WQ_ENQ(wq,x) STIO_WQ_LINK(STIO_WQ_TAIL(wq), x, STIO_WQ_TAIL(wq)->next)*/
#define STIO_WQ_ENQ(wq,x) STIO_WQ_LINK(STIO_WQ_TAIL(wq), x, wq)
/* remove an item in the front from the queue */
#define STIO_WQ_DEQ(wq) STIO_WQ_UNLINK(STIO_WQ_HEAD(wq))
2016-01-26 16:02:20 +00:00
#define STIO_DEV_HEADERS \
stio_t* stio; \
stio_size_t dev_size; \
int dev_capa; \
stio_dev_mth_t* dev_mth; \
stio_dev_evcb_t* dev_evcb; \
stio_wq_t wq; \
stio_dev_t* dev_prev; \
stio_dev_t* dev_next
2016-01-26 16:02:20 +00:00
struct stio_dev_t
{
STIO_DEV_HEADERS;
};
enum stio_dev_capa_t
{
STIO_DEV_CAPA_IN = (1 << 0),
STIO_DEV_CAPA_OUT = (1 << 1),
/* #STIO_DEV_CAPA_PRI is meaningful only if #STIO_DEV_CAPA_IN is set */
STIO_DEV_CAPA_PRI = (1 << 2),
/*STIO_DEV_CAPA_HALFOPEN = (1 << 3),*/
STIO_DEV_CAPA_STREAM = (1 << 4),
2016-03-29 15:02:01 +00:00
STIO_DEV_CAPA_OUT_QUEUED = (1 << 5),
/* internal use only. never set this bit to the dev_capa field */
STIO_DEV_CAPA_IN_DISABLED = (1 << 9),
STIO_DEV_CAPA_IN_CLOSED = (1 << 10),
STIO_DEV_CAPA_OUT_CLOSED = (1 << 11),
STIO_DEV_CAPA_IN_WATCHED = (1 << 12),
STIO_DEV_CAPA_OUT_WATCHED = (1 << 13),
STIO_DEV_CAPA_PRI_WATCHED = (1 << 14), /**< can be set only if STIO_DEV_CAPA_IN_WATCHED is set */
STIO_DEV_CAPA_HALTED = (1 << 15)
};
typedef enum stio_dev_capa_t stio_dev_capa_t;
enum stio_dev_watch_cmd_t
2016-01-26 16:02:20 +00:00
{
STIO_DEV_WATCH_START,
STIO_DEV_WATCH_UPDATE,
STIO_DEV_WATCH_RENEW, /* automatic update */
STIO_DEV_WATCH_STOP
2016-01-26 16:02:20 +00:00
};
typedef enum stio_dev_watch_cmd_t stio_dev_watch_cmd_t;
2016-01-26 16:02:20 +00:00
enum stio_dev_event_t
2016-01-26 16:02:20 +00:00
{
STIO_DEV_EVENT_IN = (1 << 0),
STIO_DEV_EVENT_OUT = (1 << 1),
2016-01-26 16:02:20 +00:00
STIO_DEV_EVENT_PRI = (1 << 2),
STIO_DEV_EVENT_HUP = (1 << 3),
STIO_DEV_EVENT_ERR = (1 << 4)
};
typedef enum stio_dev_event_t stio_dev_event_t;
2016-01-26 16:02:20 +00:00
#ifdef __cplusplus
extern "C" {
#endif
2016-01-30 05:24:23 +00:00
STIO_EXPORT stio_t* stio_open (
stio_mmgr_t* mmgr,
stio_size_t xtnsize,
stio_size_t tmrcapa, /**< initial timer capacity */
2016-01-26 16:02:20 +00:00
stio_errnum_t* errnum
);
2016-01-30 05:24:23 +00:00
STIO_EXPORT void stio_close (
2016-01-26 16:02:20 +00:00
stio_t* stio
);
2016-01-30 05:24:23 +00:00
STIO_EXPORT int stio_init (
stio_t* stio,
stio_mmgr_t* mmgr,
stio_size_t tmrcapa
);
STIO_EXPORT void stio_fini (
stio_t* stio
);
STIO_EXPORT int stio_exec (
stio_t* stio
);
2016-01-30 05:24:23 +00:00
STIO_EXPORT int stio_loop (
stio_t* stio
);
2016-01-30 05:24:23 +00:00
STIO_EXPORT void stio_stop (
stio_t* stio
);
2016-01-30 05:24:23 +00:00
STIO_EXPORT stio_dev_t* stio_makedev (
2016-01-26 16:02:20 +00:00
stio_t* stio,
stio_size_t dev_size,
stio_dev_mth_t* dev_mth,
stio_dev_evcb_t* dev_evcb,
void* make_ctx
);
2016-01-30 05:24:23 +00:00
STIO_EXPORT void stio_killdev (
2016-01-26 16:02:20 +00:00
stio_t* stio,
stio_dev_t* dev
);
2016-01-30 05:24:23 +00:00
STIO_EXPORT int stio_dev_ioctl (
stio_dev_t* dev,
int cmd,
void* arg
2016-01-26 16:02:20 +00:00
);
STIO_EXPORT int stio_dev_watch (
stio_dev_t* dev,
stio_dev_watch_cmd_t cmd,
/** 0 or bitwise-ORed of #STIO_DEV_EVENT_IN and #STIO_DEV_EVENT_OUT */
int events
2016-01-26 16:02:20 +00:00
);
2016-02-05 13:25:59 +00:00
STIO_EXPORT int stio_dev_read (
stio_dev_t* dev,
int enabled
);
2016-02-05 13:25:59 +00:00
/**
* The stio_dev_write() function posts a writing request.
* It attempts to write data immediately if there is no pending requests.
* If writing fails, it returns -1. If writing succeeds, it calls the
* on_write callback. If the callback fails, it returns -1. If the callback
* succeeds, it returns 1. If no immediate writing is possible, the request
* is enqueued to a pending request list. If enqueing gets successful,
* it returns 0. otherwise it returns -1.
*/
STIO_EXPORT int stio_dev_write (
2016-02-05 13:25:59 +00:00
stio_dev_t* dev,
const void* data,
stio_len_t len,
2016-03-29 15:02:01 +00:00
void* wrctx,
2016-03-30 16:31:56 +00:00
const stio_devadr_t* dstadr
2016-02-05 13:25:59 +00:00
);
STIO_EXPORT int stio_dev_timedwrite (
stio_dev_t* dev,
const void* data,
stio_len_t len,
const stio_ntime_t* tmout,
2016-03-29 15:02:01 +00:00
void* wrctx,
2016-03-30 16:31:56 +00:00
const stio_devadr_t* dstadr
2016-01-26 16:02:20 +00:00
);
STIO_EXPORT void stio_dev_halt (
stio_dev_t* dev
);
2016-02-05 13:25:59 +00:00
/* ------------------------------------------------ */
#define stio_inittime(x,s,ns) (((x)->sec = (s)), ((x)->nsec = (ns)))
#define stio_cleartime(x) stio_inittime(x,0,0)
#define stio_cmptime(x,y) \
(((x)->sec == (y)->sec)? ((x)->nsec - (y)->nsec): \
((x)->sec - (y)->sec))
/* if time has been normalized properly, nsec must be equal to or
* greater than 0. */
#define stio_isnegtime(x) ((x)->sec < 0)
/**
* The stio_gettime() function gets the current time.
*/
STIO_EXPORT void stio_gettime (
stio_ntime_t* nt
);
/**
* The stio_addtime() function adds x and y and stores the result in z
*/
STIO_EXPORT void stio_addtime (
const stio_ntime_t* x,
const stio_ntime_t* y,
stio_ntime_t* z
);
/**
* The stio_subtime() function subtract y from x and stores the result in z.
*/
STIO_EXPORT void stio_subtime (
const stio_ntime_t* x,
const stio_ntime_t* y,
stio_ntime_t* z
);
/**
* The stio_instmrjob() function schedules a new event.
*
* \return #STIO_TMRIDX_INVALID on failure, valid index on success.
*/
STIO_EXPORT stio_tmridx_t stio_instmrjob (
stio_t* stio,
const stio_tmrjob_t* job
);
STIO_EXPORT stio_tmridx_t stio_updtmrjob (
stio_t* stio,
stio_tmridx_t index,
const stio_tmrjob_t* job
);
STIO_EXPORT void stio_deltmrjob (
stio_t* stio,
stio_tmridx_t index
);
/**
* The stio_gettmrjob() function returns the
* pointer to the registered event at the given index.
*/
STIO_EXPORT stio_tmrjob_t* stio_gettmrjob (
stio_t* stio,
stio_tmridx_t index
);
2016-01-26 16:02:20 +00:00
#ifdef __cplusplus
}
#endif
#endif