enhanced event handlings a bit

This commit is contained in:
hyung-hwan 2016-02-01 07:42:49 +00:00
parent e0736bd657
commit cccb7bee65
10 changed files with 208 additions and 198 deletions

View File

@ -29,8 +29,6 @@ pkginclude_HEADERS = \
pkglib_LTLIBRARIES = libstio.la
libstio_la_SOURCES = \
stio-prv.h \
stio-tim.h \
stio-tmr.h \
stio.c \
stio-sck.c \
stio-tcp.c \

View File

@ -386,8 +386,6 @@ pkginclude_HEADERS = \
pkglib_LTLIBRARIES = libstio.la
libstio_la_SOURCES = \
stio-prv.h \
stio-tim.h \
stio-tmr.h \
stio.c \
stio-sck.c \
stio-tcp.c \

View File

@ -176,8 +176,8 @@ int main ()
memset (&sin, 0, STIO_SIZEOF(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(9999);
//inet_pton (sin.sin_family, "192.168.1.1", &sin.sin_addr);
inet_pton (sin.sin_family, "127.0.0.1", &sin.sin_addr);
inet_pton (sin.sin_family, "192.168.1.1", &sin.sin_addr);
//inet_pton (sin.sin_family, "127.0.0.1", &sin.sin_addr);
memset (&tcp_conn, 0, STIO_SIZEOF(tcp_conn));
memcpy (&tcp_conn.addr, &sin, STIO_SIZEOF(sin));

View File

@ -28,7 +28,6 @@
#define _STIO_PRV_H_
#include "stio.h"
#include "stio-tim.h"
#include <sys/epoll.h>
@ -42,6 +41,9 @@
#define STIO_MEMMOVE(dst,src,count) memmove(dst,src,count)
#define STIO_ASSERT assert
#define STIO_USE_TMRJOB_IDXPTR
struct stio_tmrjob_t
{
void* ctx;
@ -87,6 +89,68 @@ struct stio_t
};
#define STIO_EPOCH_YEAR (1970)
#define STIO_EPOCH_MON (1)
#define STIO_EPOCH_DAY (1)
#define STIO_EPOCH_WDAY (4)
/* windows specific epoch time */
#define STIO_EPOCH_YEAR_WIN (1601)
#define STIO_EPOCH_MON_WIN (1)
#define STIO_EPOCH_DAY_WIN (1)
#define STIO_DAYS_PER_WEEK (7)
#define STIO_MONS_PER_YEAR (12)
#define STIO_HOURS_PER_DAY (24)
#define STIO_MINS_PER_HOUR (60)
#define STIO_MINS_PER_DAY (STIO_MINS_PER_HOUR*STIO_HOURS_PER_DAY)
#define STIO_SECS_PER_MIN (60)
#define STIO_SECS_PER_HOUR (STIO_SECS_PER_MIN*STIO_MINS_PER_HOUR)
#define STIO_SECS_PER_DAY (STIO_SECS_PER_MIN*STIO_MINS_PER_DAY)
#define STIO_MSECS_PER_SEC (1000)
#define STIO_MSECS_PER_MIN (STIO_MSECS_PER_SEC*STIO_SECS_PER_MIN)
#define STIO_MSECS_PER_HOUR (STIO_MSECS_PER_SEC*STIO_SECS_PER_HOUR)
#define STIO_MSECS_PER_DAY (STIO_MSECS_PER_SEC*STIO_SECS_PER_DAY)
#define STIO_USECS_PER_MSEC (1000)
#define STIO_NSECS_PER_USEC (1000)
#define STIO_NSECS_PER_MSEC (STIO_NSECS_PER_USEC*STIO_USECS_PER_MSEC)
#define STIO_USECS_PER_SEC (STIO_USECS_PER_MSEC*STIO_MSECS_PER_SEC)
#define STIO_NSECS_PER_SEC (STIO_NSECS_PER_USEC*STIO_USECS_PER_MSEC*STIO_MSECS_PER_SEC)
#define STIO_SECNSEC_TO_MSEC(sec,nsec) \
(((stio_intptr_t)(sec) * STIO_MSECS_PER_SEC) + ((stio_intptr_t)(nsec) / STIO_NSECS_PER_MSEC))
#define STIO_SECNSEC_TO_USEC(sec,nsec) \
(((stio_intptr_t)(sec) * STIO_USECS_PER_SEC) + ((stio_intptr_t)(nsec) / STIO_NSECS_PER_USEC))
#define STIO_SEC_TO_MSEC(sec) ((sec) * STIO_MSECS_PER_SEC)
#define STIO_MSEC_TO_SEC(sec) ((sec) / STIO_MSECS_PER_SEC)
#define STIO_USEC_TO_NSEC(usec) ((usec) * STIO_NSECS_PER_USEC)
#define STIO_NSEC_TO_USEC(nsec) ((nsec) / STIO_NSECS_PER_USEC)
#define STIO_MSEC_TO_NSEC(msec) ((msec) * STIO_NSECS_PER_MSEC)
#define STIO_NSEC_TO_MSEC(nsec) ((nsec) / STIO_NSECS_PER_MSEC)
#define STIO_SEC_TO_NSEC(sec) ((sec) * STIO_NSECS_PER_SEC)
#define STIO_NSEC_TO_SEC(nsec) ((nsec) / STIO_NSECS_PER_SEC)
#define STIO_SEC_TO_USEC(sec) ((sec) * STIO_USECS_PER_SEC)
#define STIO_USEC_TO_SEC(usec) ((usec) / STIO_USECS_PER_SEC)
#define stio_inittime(x,s,ns) (((x)->sec = (s)), ((x)->nsec = (ns)))
#define stio_cleartime(x) stio_inittime(x,0,0)
/*#define stio_cleartime(x) ((x)->sec = (x)->nsec = 0)*/
#define stio_cmptime(x,y) \
(((x)->sec == (y)->sec)? ((x)->nsec - (y)->nsec): \
((x)->sec - (y)->sec))
#define stio_iszerotime(x) ((x)->sec == 0 && (x)->nsec == 0)
#ifdef __cplusplus
extern "C" {
#endif
@ -100,6 +164,32 @@ int stio_makesckasync (stio_sckhnd_t sck);
int stio_getsckadrinfo (stio_t* stio, const stio_sckadr_t* addr, stio_scklen_t* len, stio_sckfam_t* family);
/**
* 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.
*

View File

@ -88,7 +88,7 @@ static void tcp_kill (stio_dev_t* dev)
{
stio_dev_tcp_t* tcp = (stio_dev_tcp_t*)dev;
if (tcp->state & (STIO_DEV_TCP_ACCEPTED | STIO_DEV_TCP_CONNECTED))
if (tcp->state & (STIO_DEV_TCP_ACCEPTED | STIO_DEV_TCP_CONNECTED | STIO_DEV_TCP_CONNECTING | STIO_DEV_TCP_LISTENING))
{
if (tcp->on_disconnected) tcp->on_disconnected (tcp);
}
@ -160,12 +160,15 @@ static void tmr_connect_handle (stio_t* stio, const stio_ntime_t* now, stio_tmrj
if (tcp->state & STIO_DEV_TCP_CONNECTING)
{
/* the state check is actually redundant as it must not be fired
* after it gets connected. the timer job doesn't need to be deleted
* when it gets connected for this check here */
/* the state check for STIO_DEV_TCP_CONNECTING is actually redundant
* as it must not be fired after it gets connected. the timer job
* doesn't need to be deleted when it gets connected for this check
* here. this libarary, however, deletes the job when it gets
* connected. */
/*
if (tcp->on_disconnected) tcp->on_disconnected (tcp);
tcp->state &= ~STIO_DEV_TCP_CONNECTING;
*/
stio_dev_tcp_kill (tcp);
}
}
@ -341,9 +344,7 @@ static int tcp_ready (stio_dev_t* dev, int events)
stio_dev_tcp_t* tcp = (stio_dev_tcp_t*)dev;
printf ("TCP READY...%p\n", dev);
if (tcp->state & STIO_DEV_TCP_CONNECTING)
{
if (events & (STIO_DEV_EVENT_ERR | STIO_DEV_EVENT_HUP | STIO_DEV_EVENT_PRI | STIO_DEV_EVENT_IN))
if (events & STIO_DEV_EVENT_ERR)
{
int errcode;
stio_scklen_t len;
@ -351,14 +352,31 @@ printf ("TCP READY...%p\n", dev);
len = STIO_SIZEOF(errcode);
if (getsockopt (tcp->sck, SOL_SOCKET, SO_ERROR, (char*)&errcode, &len) == -1)
{
printf ("CANNOT CONNECT ERRORCODE - %s\n", strerror(errcode));
tcp->stio->errnum = stio_syserrtoerrnum(errno);
/* the error number is set to the socket error code.
* errno resulting from getsockopt() doesn't reflect the actual
* socket error. so errno is not used to set the error number.
* instead, the generic device error STIO_EDEVERRR is used */
tcp->stio->errnum = STIO_EDEVERR;
}
else
{
tcp->stio->errnum = stio_syserrtoerrnum (errcode);
}
return -1;
}
if (tcp->state & STIO_DEV_TCP_CONNECTING)
{
if (events & STIO_DEV_EVENT_HUP)
{
/* device hang-up */
tcp->stio->errnum = STIO_EDEVHUP;
return -1;
}
else if (events & (STIO_DEV_EVENT_PRI | STIO_DEV_EVENT_IN))
{
/* invalid event masks. generic device error */
tcp->stio->errnum = STIO_EDEVERR;
return -1;
}
else if (events & STIO_DEV_EVENT_OUT)
@ -412,6 +430,19 @@ printf ("CAANOT MANIPULTE EVENT ...\n");
return 0; /* success but don't invoke on_recv() */
}
else if (tcp->state & STIO_DEV_TCP_LISTENING)
{
if (events & STIO_DEV_EVENT_HUP)
{
/* device hang-up */
tcp->stio->errnum = STIO_EDEVHUP;
return -1;
}
else if (events & (STIO_DEV_EVENT_PRI | STIO_DEV_EVENT_OUT))
{
tcp->stio->errnum = STIO_EDEVERR;
return -1;
}
else if (events & STIO_DEV_EVENT_IN)
{
stio_sckhnd_t clisck;
stio_sckadr_t peer;
@ -431,7 +462,6 @@ printf ("CAANOT MANIPULTE EVENT ...\n");
return -1;
}
/* addr is the address of the peer */
/* local addresss is inherited from the server */
clitcp = (stio_dev_tcp_t*)stio_makedev (tcp->stio, STIO_SIZEOF(*tcp), &tcp_acc_mth, tcp->evcb, &clisck);
@ -443,7 +473,7 @@ printf ("CAANOT MANIPULTE EVENT ...\n");
clitcp->state |= STIO_DEV_TCP_ACCEPTED;
clitcp->peer = peer;
clitcp->parent = tcp;
/*clitcp->parent = tcp;*/
/* inherit some event handlers from the parent.
@ -457,13 +487,17 @@ printf ("CAANOT MANIPULTE EVENT ...\n");
if (clitcp->on_connected (clitcp) <= -1) stio_dev_tcp_kill (clitcp);
return 0; /* success but don't invoke on_recv() */
}
printf ("READY WITH %d\n", events);
if (events & (STIO_DEV_EVENT_ERR | STIO_DEV_EVENT_HUP))
}
else if (events & STIO_DEV_EVENT_HUP)
{
printf ("DISCONNECTED or ERROR \n");
return -1; /* the caller must kill the device */
if (events & (STIO_DEV_EVENT_PRI | STIO_DEV_EVENT_IN | STIO_DEV_EVENT_OUT))
{
/* probably half-open? */
return 1;
}
tcp->stio->errnum = STIO_EDEVHUP;
return -1;
}
return 1; /* the device is ok. carry on reading or writing */

View File

@ -28,7 +28,6 @@
#define _STIO_TCP_H_
#include <stio.h>
#include <stio-tim.h>
enum stio_dev_tcp_ioctl_cmd_t
{
@ -72,7 +71,7 @@ struct stio_dev_tcp_t
stio_sckadr_t peer;
/* parent tcp device. valid if STIO_DEV_TCP_ACCEPTED is set */
stio_dev_tcp_t* parent;
/*stio_dev_tcp_t* parent;*/
/** return 0 on succes, -1 on failure/
* called on a new tcp device for an accepted client or
@ -125,7 +124,7 @@ struct stio_dev_tcp_accept_t
{
stio_syshnd_t sck;
/* TODO: add timeout */
stio_dev_tcp_t* parent; /* TODO: is this really needed? */
/*stio_dev_tcp_t* parent;*/
stio_sckadr_t peer;
};

View File

@ -24,7 +24,6 @@
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "stio-tim.h"
#include "stio-prv.h"
#if defined(_WIN32)

View File

@ -1,124 +0,0 @@
/*
* $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 WARRANTIES
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.
*/
#ifndef _STIO_TIME_H_
#define _STIO_TIME_H_
#include <stio.h>
#define STIO_EPOCH_YEAR (1970)
#define STIO_EPOCH_MON (1)
#define STIO_EPOCH_DAY (1)
#define STIO_EPOCH_WDAY (4)
/* windows specific epoch time */
#define STIO_EPOCH_YEAR_WIN (1601)
#define STIO_EPOCH_MON_WIN (1)
#define STIO_EPOCH_DAY_WIN (1)
#define STIO_DAYS_PER_WEEK (7)
#define STIO_MONS_PER_YEAR (12)
#define STIO_HOURS_PER_DAY (24)
#define STIO_MINS_PER_HOUR (60)
#define STIO_MINS_PER_DAY (STIO_MINS_PER_HOUR*STIO_HOURS_PER_DAY)
#define STIO_SECS_PER_MIN (60)
#define STIO_SECS_PER_HOUR (STIO_SECS_PER_MIN*STIO_MINS_PER_HOUR)
#define STIO_SECS_PER_DAY (STIO_SECS_PER_MIN*STIO_MINS_PER_DAY)
#define STIO_MSECS_PER_SEC (1000)
#define STIO_MSECS_PER_MIN (STIO_MSECS_PER_SEC*STIO_SECS_PER_MIN)
#define STIO_MSECS_PER_HOUR (STIO_MSECS_PER_SEC*STIO_SECS_PER_HOUR)
#define STIO_MSECS_PER_DAY (STIO_MSECS_PER_SEC*STIO_SECS_PER_DAY)
#define STIO_USECS_PER_MSEC (1000)
#define STIO_NSECS_PER_USEC (1000)
#define STIO_NSECS_PER_MSEC (STIO_NSECS_PER_USEC*STIO_USECS_PER_MSEC)
#define STIO_USECS_PER_SEC (STIO_USECS_PER_MSEC*STIO_MSECS_PER_SEC)
#define STIO_NSECS_PER_SEC (STIO_NSECS_PER_USEC*STIO_USECS_PER_MSEC*STIO_MSECS_PER_SEC)
#define STIO_SECNSEC_TO_MSEC(sec,nsec) \
(((stio_intptr_t)(sec) * STIO_MSECS_PER_SEC) + ((stio_intptr_t)(nsec) / STIO_NSECS_PER_MSEC))
#define STIO_SECNSEC_TO_USEC(sec,nsec) \
(((stio_intptr_t)(sec) * STIO_USECS_PER_SEC) + ((stio_intptr_t)(nsec) / STIO_NSECS_PER_USEC))
#define STIO_SEC_TO_MSEC(sec) ((sec) * STIO_MSECS_PER_SEC)
#define STIO_MSEC_TO_SEC(sec) ((sec) / STIO_MSECS_PER_SEC)
#define STIO_USEC_TO_NSEC(usec) ((usec) * STIO_NSECS_PER_USEC)
#define STIO_NSEC_TO_USEC(nsec) ((nsec) / STIO_NSECS_PER_USEC)
#define STIO_MSEC_TO_NSEC(msec) ((msec) * STIO_NSECS_PER_MSEC)
#define STIO_NSEC_TO_MSEC(nsec) ((nsec) / STIO_NSECS_PER_MSEC)
#define STIO_SEC_TO_NSEC(sec) ((sec) * STIO_NSECS_PER_SEC)
#define STIO_NSEC_TO_SEC(nsec) ((nsec) / STIO_NSECS_PER_SEC)
#define STIO_SEC_TO_USEC(sec) ((sec) * STIO_USECS_PER_SEC)
#define STIO_USEC_TO_SEC(usec) ((usec) / STIO_USECS_PER_SEC)
#define stio_inittime(x,s,ns) (((x)->sec = (s)), ((x)->nsec = (ns)))
#define stio_cleartime(x) stio_inittime(x,0,0)
/*#define stio_cleartime(x) ((x)->sec = (x)->nsec = 0)*/
#define stio_cmptime(x,y) \
(((x)->sec == (y)->sec)? ((x)->nsec - (y)->nsec): \
((x)->sec - (y)->sec))
#define stio_iszerotime(x) ((x)->sec == 0 && (x)->nsec == 0)
#if defined(__cplusplus)
extern "C" {
#endif
/**
* 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
);
#if defined(__cplusplus)
}
#endif
#endif

View File

@ -267,15 +267,16 @@ int stio_exec (stio_t* stio)
int x, events = 0;
if (stio->revs[i].events & EPOLLERR) events |= STIO_DEV_EVENT_ERR;
#if defined(EPOLLRDHUP)
if (stio->revs[i].events & (EPOLLHUP | EPOLLRDHUP)) events |= STIO_DEV_EVENT_HUP;
#else
if (stio->revs[i].events & EPOLLHUP) events |= STIO_DEV_EVENT_HUP;
#if defined(EPOLLRDHUP)
/* treat it the same way as EPOLLHUP */
if (stio->revs[i].events & EPOLLRDHUP) events |= STIO_DEV_EVENT_HUP;
#endif
if (stio->revs[i].events & EPOLLIN) events |= STIO_DEV_EVENT_IN;
if (stio->revs[i].events & EPOLLOUT) events |= STIO_DEV_EVENT_OUT;
if (stio->revs[i].events & EPOLLPRI) events |= STIO_DEV_EVENT_PRI;
/* return value of ready()
* <= -1 - failure. kill the device.
* == 0 - ok. but don't invoke recv() or send().
@ -594,6 +595,16 @@ stio_errnum_t stio_syserrtoerrnum (int no)
return STIO_ENFILE;
#endif
#if defined(ECONNREFUSED)
case ECONNREFUSED:
return STIO_ECONRF;
#endif
#if defined(ECONNRESETD)
case ECONNRESET:
return STIO_ECONRS;
#endif
default:
return STIO_ESYSERR;
}

View File

@ -105,8 +105,13 @@ enum stio_errnum_t
STIO_ENOSUP, /* not supported */
STIO_EMFILE,
STIO_ENFILE,
STIO_ECONRF, /* connection refused */
STIO_ECONRS, /* connection reset */
STIO_EDEVMAKE,
STIO_EDEVERR,
STIO_EDEVHUP,
STIO_ESYSERR
};