touched up code for renewal

This commit is contained in:
hyung-hwan 2019-01-11 07:35:43 +00:00
parent 27695e69cd
commit b30bbf8063
10 changed files with 214 additions and 115 deletions

12
mio/configure vendored
View File

@ -17940,6 +17940,18 @@ _ACEOF
fi fi
done done
for ac_func in accept4 sendmsg recvmsg writev readv
do :
as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
if eval test \"x\$"$as_ac_var"\" = x"yes"; then :
cat >>confdefs.h <<_ACEOF
#define `$as_echo "HAVE_$ac_func" | $as_tr_cpp` 1
_ACEOF
fi
done
OLDLIBS="$LIBS" OLDLIBS="$LIBS"
LIBS="$LIBM $LIBS" LIBS="$LIBM $LIBS"

View File

@ -160,6 +160,7 @@ AC_CHECK_FUNCS([backtrace backtrace_symbols])
AC_CHECK_FUNCS([fork vfork posix_spawn gettid nanosleep select]) AC_CHECK_FUNCS([fork vfork posix_spawn gettid nanosleep select])
AC_CHECK_FUNCS([makecontext swapcontext getcontext setcontext]) AC_CHECK_FUNCS([makecontext swapcontext getcontext setcontext])
AC_CHECK_FUNCS([snprintf _vsnprintf _vsnwprintf]) AC_CHECK_FUNCS([snprintf _vsnprintf _vsnwprintf])
AC_CHECK_FUNCS([accept4 sendmsg recvmsg writev readv])
OLDLIBS="$LIBS" OLDLIBS="$LIBS"
LIBS="$LIBM $LIBS" LIBS="$LIBM $LIBS"

View File

@ -44,7 +44,7 @@ bin_PROGRAMS = mio
mio_SOURCES = main.c mio_SOURCES = main.c
mio_CPPFLAGS = $(CPPFLAGS_LIB_COMMON) mio_CPPFLAGS = $(CPPFLAGS_LIB_COMMON)
mio_LDFLAGS = $(LDFLAGS_LIB_COMMON) mio_LDFLAGS = $(LDFLAGS_LIB_COMMON)
mio_LDADD = $(LIBADD_LIB_COMMON) -lmio mio_LDADD = $(LIBADD_LIB_COMMON) -lmio $(SSL_LIBS)
install-data-hook: install-data-hook:
@echo "#ifndef _MIO_CFG_H_" > "$(DESTDIR)$(includedir)/mio-cfg.h" @echo "#ifndef _MIO_CFG_H_" > "$(DESTDIR)$(includedir)/mio-cfg.h"

View File

@ -154,7 +154,7 @@ libmio_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
PROGRAMS = $(bin_PROGRAMS) PROGRAMS = $(bin_PROGRAMS)
am_mio_OBJECTS = mio-main.$(OBJEXT) am_mio_OBJECTS = mio-main.$(OBJEXT)
mio_OBJECTS = $(am_mio_OBJECTS) mio_OBJECTS = $(am_mio_OBJECTS)
mio_DEPENDENCIES = $(am__DEPENDENCIES_2) mio_DEPENDENCIES = $(am__DEPENDENCIES_2) $(am__DEPENDENCIES_1)
mio_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ mio_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
$(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
$(mio_LDFLAGS) $(LDFLAGS) -o $@ $(mio_LDFLAGS) $(LDFLAGS) -o $@
@ -411,7 +411,7 @@ libmio_la_LIBADD = $(LIBADD_LIB_COMMON) $(SSL_LIBS)
mio_SOURCES = main.c mio_SOURCES = main.c
mio_CPPFLAGS = $(CPPFLAGS_LIB_COMMON) mio_CPPFLAGS = $(CPPFLAGS_LIB_COMMON)
mio_LDFLAGS = $(LDFLAGS_LIB_COMMON) mio_LDFLAGS = $(LDFLAGS_LIB_COMMON)
mio_LDADD = $(LIBADD_LIB_COMMON) -lmio mio_LDADD = $(LIBADD_LIB_COMMON) -lmio $(SSL_LIBS)
all: mio-cfg.h all: mio-cfg.h
$(MAKE) $(AM_MAKEFLAGS) all-am $(MAKE) $(AM_MAKEFLAGS) all-am

View File

@ -299,6 +299,11 @@ static int arp_sck_on_write (mio_dev_sck_t* dev, mio_iolen_t wrlen, void* wrctx,
return 0; return 0;
} }
static void arp_sck_on_connect (mio_dev_sck_t* dev)
{
printf ("STARTING UP ARP SOCKET %d...\n", dev->sck);
}
static void arp_sck_on_disconnect (mio_dev_sck_t* dev) static void arp_sck_on_disconnect (mio_dev_sck_t* dev)
{ {
printf ("SHUTTING DOWN ARP SOCKET %d...\n", dev->sck); printf ("SHUTTING DOWN ARP SOCKET %d...\n", dev->sck);
@ -316,6 +321,7 @@ static int setup_arp_tester (mio_t* mio)
//sck_make.type = MIO_DEV_SCK_ARP_DGRAM; //sck_make.type = MIO_DEV_SCK_ARP_DGRAM;
sck_make.on_write = arp_sck_on_write; sck_make.on_write = arp_sck_on_write;
sck_make.on_read = arp_sck_on_read; sck_make.on_read = arp_sck_on_read;
sck_make.on_connect = arp_sck_on_connect;
sck_make.on_disconnect = arp_sck_on_disconnect; sck_make.on_disconnect = arp_sck_on_disconnect;
sck = mio_dev_sck_make (mio, 0, &sck_make); sck = mio_dev_sck_make (mio, 0, &sck_make);
if (!sck) if (!sck)
@ -325,7 +331,9 @@ static int setup_arp_tester (mio_t* mio)
} }
//mio_sckaddr_initforeth (&ethdst, if_nametoindex("enp0s25.3"), (mio_ethaddr_t*)"\xFF\xFF\xFF\xFF\xFF\xFF"); //mio_sckaddr_initforeth (&ethdst, if_nametoindex("enp0s25.3"), (mio_ethaddr_t*)"\xFF\xFF\xFF\xFF\xFF\xFF");
mio_sckaddr_initforeth (&ethdst, if_nametoindex("enp0s25.3"), (mio_ethaddr_t*)"\xAA\xBB\xFF\xCC\xDD\xFF"); //mio_sckaddr_initforeth (&ethdst, if_nametoindex("enp0s25.3"), (mio_ethaddr_t*)"\xAA\xBB\xFF\xCC\xDD\xFF");
mio_sckaddr_initforeth (&ethdst, if_nametoindex("wlan0"), (mio_ethaddr_t*)"\xAA\xBB\xFF\xCC\xDD\xFF");
memset (&etharp, 0, sizeof(etharp)); memset (&etharp, 0, sizeof(etharp));
@ -342,7 +350,7 @@ static int setup_arp_tester (mio_t* mio)
memcpy (etharp.arppld.sha, "\xB8\x6B\x23\x9C\x10\x76", MIO_ETHADDR_LEN); memcpy (etharp.arppld.sha, "\xB8\x6B\x23\x9C\x10\x76", MIO_ETHADDR_LEN);
if (mio_dev_sck_write (sck, &etharp, sizeof(etharp), NULL, &ethdst) <= -1) if (mio_dev_sck_write(sck, &etharp, sizeof(etharp), NULL, &ethdst) <= -1)
//if (mio_dev_sck_write (sck, &etharp.arphdr, sizeof(etharp) - sizeof(etharp.ethhdr), NULL, &ethaddr) <= -1) //if (mio_dev_sck_write (sck, &etharp.arphdr, sizeof(etharp) - sizeof(etharp.ethhdr), NULL, &ethaddr) <= -1)
{ {
printf ("CANNOT WRITE ARP...\n"); printf ("CANNOT WRITE ARP...\n");
@ -542,7 +550,7 @@ static void handle_signal (int sig)
if (g_mio) mio_stop (g_mio, MIO_STOPREQ_TERMINATION); if (g_mio) mio_stop (g_mio, MIO_STOPREQ_TERMINATION);
} }
int main () int main (int argc, char* argv[])
{ {
int i; int i;
@ -590,7 +598,7 @@ int main ()
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(1234); */ sin.sin_port = htons(1234); */
/* /*
udp = (mio_dev_udp_t*)mio_makedev (mio, MIO_SIZEOF(*udp), &udp_mth, &udp_evcb, &sin); udp = (mio_dev_udp_t*)mio_makedev(mio, MIO_SIZEOF(*udp), &udp_mth, &udp_evcb, &sin);
if (!udp) if (!udp)
{ {
printf ("Cannot make udp\n"); printf ("Cannot make udp\n");
@ -602,8 +610,9 @@ int main ()
tcp_make.type = MIO_DEV_SCK_TCP4; tcp_make.type = MIO_DEV_SCK_TCP4;
tcp_make.on_write = tcp_sck_on_write; tcp_make.on_write = tcp_sck_on_write;
tcp_make.on_read = tcp_sck_on_read; tcp_make.on_read = tcp_sck_on_read;
tcp_make.on_connect = tcp_sck_on_connect;
tcp_make.on_disconnect = tcp_sck_on_disconnect; tcp_make.on_disconnect = tcp_sck_on_disconnect;
tcp[0] = mio_dev_sck_make (mio, MIO_SIZEOF(tcp_server_t), &tcp_make); tcp[0] = mio_dev_sck_make(mio, MIO_SIZEOF(tcp_server_t), &tcp_make);
if (!tcp[0]) if (!tcp[0])
{ {
printf ("Cannot make tcp\n"); printf ("Cannot make tcp\n");
@ -621,7 +630,6 @@ int main ()
} }
mio_inittime (&tcp_conn.connect_tmout, 5, 0); mio_inittime (&tcp_conn.connect_tmout, 5, 0);
tcp_conn.on_connect = tcp_sck_on_connect;
tcp_conn.options = MIO_DEV_SCK_CONNECT_SSL; tcp_conn.options = MIO_DEV_SCK_CONNECT_SSL;
if (mio_dev_sck_connect (tcp[0], &tcp_conn) <= -1) if (mio_dev_sck_connect (tcp[0], &tcp_conn) <= -1)
{ {
@ -634,9 +642,10 @@ int main ()
tcp_make.type = MIO_DEV_SCK_TCP4; tcp_make.type = MIO_DEV_SCK_TCP4;
tcp_make.on_write = tcp_sck_on_write; tcp_make.on_write = tcp_sck_on_write;
tcp_make.on_read = tcp_sck_on_read; tcp_make.on_read = tcp_sck_on_read;
tcp_make.on_connect = tcp_sck_on_connect;
tcp_make.on_disconnect = tcp_sck_on_disconnect; tcp_make.on_disconnect = tcp_sck_on_disconnect;
tcp[1] = mio_dev_sck_make (mio, MIO_SIZEOF(tcp_server_t), &tcp_make); tcp[1] = mio_dev_sck_make(mio, MIO_SIZEOF(tcp_server_t), &tcp_make);
if (!tcp[1]) if (!tcp[1])
{ {
printf ("Cannot make tcp\n"); printf ("Cannot make tcp\n");
@ -651,16 +660,15 @@ int main ()
if (mio_dev_sck_bind (tcp[1],&tcp_bind) <= -1) if (mio_dev_sck_bind (tcp[1],&tcp_bind) <= -1)
{ {
printf ("mio_dev_sck_bind() failed....\n"); printf ("tcp[1] mio_dev_sck_bind() failed....\n");
goto oops; goto oops;
} }
tcp_lstn.backlogs = 100; tcp_lstn.backlogs = 100;
tcp_lstn.on_connect = tcp_sck_on_connect;
if (mio_dev_sck_listen (tcp[1], &tcp_lstn) <= -1) if (mio_dev_sck_listen (tcp[1], &tcp_lstn) <= -1)
{ {
printf ("mio_dev_sck_listen() failed....\n"); printf ("tcp[1] mio_dev_sck_listen() failed....\n");
goto oops; goto oops;
} }
@ -669,6 +677,7 @@ int main ()
tcp_make.type = MIO_DEV_SCK_TCP4; tcp_make.type = MIO_DEV_SCK_TCP4;
tcp_make.on_write = tcp_sck_on_write; tcp_make.on_write = tcp_sck_on_write;
tcp_make.on_read = tcp_sck_on_read; tcp_make.on_read = tcp_sck_on_read;
tcp_make.on_connect = tcp_sck_on_connect;
tcp_make.on_disconnect = tcp_sck_on_disconnect; tcp_make.on_disconnect = tcp_sck_on_disconnect;
tcp[2] = mio_dev_sck_make (mio, MIO_SIZEOF(tcp_server_t), &tcp_make); tcp[2] = mio_dev_sck_make (mio, MIO_SIZEOF(tcp_server_t), &tcp_make);
@ -687,17 +696,16 @@ int main ()
tcp_bind.ssl_keyfile = "localhost.key"; tcp_bind.ssl_keyfile = "localhost.key";
mio_inittime (&tcp_bind.accept_tmout, 5, 1); mio_inittime (&tcp_bind.accept_tmout, 5, 1);
if (mio_dev_sck_bind (tcp[2],&tcp_bind) <= -1) if (mio_dev_sck_bind(tcp[2], &tcp_bind) <= -1)
{ {
printf ("mio_dev_sck_bind() failed....\n"); printf ("tcp[2] mio_dev_sck_bind() failed....\n");
goto oops; goto oops;
} }
tcp_lstn.backlogs = 100; tcp_lstn.backlogs = 100;
tcp_lstn.on_connect = tcp_sck_on_connect; if (mio_dev_sck_listen(tcp[2], &tcp_lstn) <= -1)
if (mio_dev_sck_listen (tcp[2], &tcp_lstn) <= -1)
{ {
printf ("mio_dev_sck_listen() failed....\n"); printf ("tcp[2] mio_dev_sck_listen() failed....\n");
goto oops; goto oops;
} }
@ -706,7 +714,6 @@ int main ()
if (setup_arp_tester(mio) <= -1) goto oops; if (setup_arp_tester(mio) <= -1) goto oops;
if (setup_ping4_tester(mio) <= -1) goto oops; if (setup_ping4_tester(mio) <= -1) goto oops;
for (i = 0; i < 5; i++) for (i = 0; i < 5; i++)
{ {
mio_dev_pro_t* pro; mio_dev_pro_t* pro;
@ -753,3 +760,47 @@ oops:
#endif #endif
return -1; return -1;
} }
#if 0
int main (int argc, char* argv[])
{
mio_t* mio = MIO_NULL;
mio_dev_sck_t* tcpsvr;
mio = mio_open (&mmgr, 0, 512, MIO_NULL);
if (!mio)
{
printf ("Cannot open mio\n");
goto oops;
}
memset (&tcp_make, 0, MIO_SIZEOF(&tcp_make));
tcp_make.type = MIO_DEV_SCK_TCP4;
tcp_make.on_write = tcp_sck_on_write;
tcp_make.on_read = tcp_sck_on_read;
tcp_make.on_connect = tcp_sck_on_connect;
tcp_make.on_disconnect = tcp_sck_on_disconnect;
tcpsvr = mio_dev_sck_make(mio, MIO_SIZEOF(tcp_server_t), &tcp_make);
if (!tcpsvr)
{
printf ("Cannot make a tcp server\n");
goto oops;
}
ts = (tcp_server_t*)(tcpsvr + 1);
ts->tally = 0;
#if 0
while (1)
{
mio_exec (mio);
}
#endif
mio_loop (mio);
oops:
if (mio) mio_close (mio);
return 0;
}
#endif

View File

@ -3,6 +3,9 @@
/* Define if building universal (internal helper macro) */ /* Define if building universal (internal helper macro) */
#undef AC_APPLE_UNIVERSAL_BUILD #undef AC_APPLE_UNIVERSAL_BUILD
/* Define to 1 if you have the `accept4' function. */
#undef HAVE_ACCEPT4
/* Define to 1 if you have the `acos' function. */ /* Define to 1 if you have the `acos' function. */
#undef HAVE_ACOS #undef HAVE_ACOS
@ -385,6 +388,12 @@
/* Define to 1 if you have the `readdir64' function. */ /* Define to 1 if you have the `readdir64' function. */
#undef HAVE_READDIR64 #undef HAVE_READDIR64
/* Define to 1 if you have the `readv' function. */
#undef HAVE_READV
/* Define to 1 if you have the `recvmsg' function. */
#undef HAVE_RECVMSG
/* Define to 1 if you have the `round' function. */ /* Define to 1 if you have the `round' function. */
#undef HAVE_ROUND #undef HAVE_ROUND
@ -412,6 +421,9 @@
/* Define to 1 if you have the `sendfilev64' function. */ /* Define to 1 if you have the `sendfilev64' function. */
#undef HAVE_SENDFILEV64 #undef HAVE_SENDFILEV64
/* Define to 1 if you have the `sendmsg' function. */
#undef HAVE_SENDMSG
/* Define to 1 if you have the `setcontext' function. */ /* Define to 1 if you have the `setcontext' function. */
#undef HAVE_SETCONTEXT #undef HAVE_SETCONTEXT
@ -687,6 +699,9 @@
/* Define to 1 if you have the <wctype.h> header file. */ /* Define to 1 if you have the <wctype.h> header file. */
#undef HAVE_WCTYPE_H #undef HAVE_WCTYPE_H
/* Define to 1 if you have the `writev' function. */
#undef HAVE_WRITEV
/* Define to 1 if you have the `_vsnprintf' function. */ /* Define to 1 if you have the `_vsnprintf' function. */
#undef HAVE__VSNPRINTF #undef HAVE__VSNPRINTF

View File

@ -74,59 +74,63 @@
/* ========================================================================= */ /* ========================================================================= */
void mio_closeasyncsck (mio_t* mio, mio_sckhnd_t sck)
{
#if defined(_WIN32)
closesocket (sck);
#else
close (sck);
#endif
}
int mio_makesckasync (mio_t* mio, mio_sckhnd_t sck) int mio_makesckasync (mio_t* mio, mio_sckhnd_t sck)
{ {
return mio_makesyshndasync (mio, (mio_syshnd_t)sck); return mio_makesyshndasync (mio, (mio_syshnd_t)sck);
} }
mio_sckhnd_t mio_openasyncsck (mio_t* mio, int domain, int type, int proto) static void close_async_socket (mio_t* mio, mio_sckhnd_t sck)
{ {
mio_sckhnd_t sck;
#if defined(_WIN32)
sck = WSASocket (domain, type, proto, NULL, 0, WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT);
if (sck == MIO_SCKHND_INVALID)
{
/* mio_seterrnum (dev->mio, MIO_ESYSERR); or translate errno to mio errnum */
return MIO_SCKHND_INVALID;
}
#else
sck = socket (domain, type, proto);
if (sck == MIO_SCKHND_INVALID)
{
mio->errnum = mio_syserrtoerrnum(errno);
return MIO_SCKHND_INVALID;
}
#if defined(FD_CLOEXEC)
{
int flags = fcntl (sck, F_GETFD, 0);
if (fcntl (sck, F_SETFD, flags | FD_CLOEXEC) == -1)
{
mio->errnum = mio_syserrtoerrnum(errno);
return MIO_SCKHND_INVALID;
}
}
#endif
if (mio_makesckasync (mio, sck) <= -1)
{
close (sck); close (sck);
return MIO_SCKHND_INVALID; }
static mio_sckhnd_t open_async_socket (mio_t* mio, int domain, int type, int proto)
{
mio_sckhnd_t sck = MIO_SCKHND_INVALID;
int flags;
#if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC)
type |= SOCK_NONBLOCK;
type |= SOCK_CLOEXEC;
open_socket:
#endif
sck = socket(domain, type, proto);
if (sck == MIO_SCKHND_INVALID)
{
#if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC)
if (errno == EINVAL && (type & (SOCK_NONBLOCK | SOCK_CLOEXEC)))
{
type &= ~(SOCK_NONBLOCK | SOCK_CLOEXEC);
goto open_socket;
}
#endif
goto oops;
}
else
{
#if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC)
if (type & (SOCK_NONBLOCK | SOCK_CLOEXEC)) goto done;
#endif
} }
flags = fcntl(sck, F_GETFD, 0);
if (flags == -1) goto oops;
#if defined(FD_CLOEXEC)
flags |= FD_CLOEXEC;
#endif #endif
#if defined(O_NONBLOCK)
flags |= O_NONBLOCK;
#endif
if (fcntl(sck, F_SETFD, flags) == -1) goto oops;
done:
return sck; return sck;
oops:
if (sck != MIO_SCKHND_INVALID) close (sck);
mio->errnum = mio_syserrtoerrnum(errno);
return MIO_SCKHND_INVALID;
} }
int mio_getsckaddrinfo (mio_t* mio, const mio_sckaddr_t* addr, mio_scklen_t* len, mio_sckfam_t* family) int mio_getsckaddrinfo (mio_t* mio, const mio_sckaddr_t* addr, mio_scklen_t* len, mio_sckfam_t* family)
@ -421,12 +425,13 @@ static int dev_sck_make (mio_dev_t* dev, void* ctx)
return -1; return -1;
} }
rdev->sck = mio_openasyncsck (dev->mio, sck_type_map[arg->type].domain, sck_type_map[arg->type].type, sck_type_map[arg->type].proto); rdev->sck = open_async_socket(dev->mio, sck_type_map[arg->type].domain, sck_type_map[arg->type].type, sck_type_map[arg->type].proto);
if (rdev->sck == MIO_SCKHND_INVALID) goto oops; if (rdev->sck == MIO_SCKHND_INVALID) goto oops;
rdev->dev_capa = MIO_DEV_CAPA_IN | MIO_DEV_CAPA_OUT | sck_type_map[arg->type].extra_dev_capa; rdev->dev_capa = MIO_DEV_CAPA_IN | MIO_DEV_CAPA_OUT | sck_type_map[arg->type].extra_dev_capa;
rdev->on_write = arg->on_write; rdev->on_write = arg->on_write;
rdev->on_read = arg->on_read; rdev->on_read = arg->on_read;
rdev->on_connect = arg->on_connect;
rdev->on_disconnect = arg->on_disconnect; rdev->on_disconnect = arg->on_disconnect;
rdev->type = arg->type; rdev->type = arg->type;
rdev->tmrjob_index = MIO_TMRIDX_INVALID; rdev->tmrjob_index = MIO_TMRIDX_INVALID;
@ -436,7 +441,7 @@ static int dev_sck_make (mio_dev_t* dev, void* ctx)
oops: oops:
if (rdev->sck != MIO_SCKHND_INVALID) if (rdev->sck != MIO_SCKHND_INVALID)
{ {
mio_closeasyncsck (rdev->mio, rdev->sck); close_async_socket (rdev->mio, rdev->sck);
rdev->sck = MIO_SCKHND_INVALID; rdev->sck = MIO_SCKHND_INVALID;
} }
return -1; return -1;
@ -447,18 +452,20 @@ static int dev_sck_make_client (mio_dev_t* dev, void* ctx)
mio_dev_sck_t* rdev = (mio_dev_sck_t*)dev; mio_dev_sck_t* rdev = (mio_dev_sck_t*)dev;
mio_syshnd_t* sck = (mio_syshnd_t*)ctx; mio_syshnd_t* sck = (mio_syshnd_t*)ctx;
/* nothing special is done here except setting the sock et handle. /* create a socket device that is made of a socket connection
* on a listening socket.
* nothing special is done here except setting the socket handle.
* most of the initialization is done by the listening socket device * most of the initialization is done by the listening socket device
* after a client socket has been created. */ * after a client socket has been created. */
rdev->sck = *sck; rdev->sck = *sck;
rdev->tmrjob_index = MIO_TMRIDX_INVALID; rdev->tmrjob_index = MIO_TMRIDX_INVALID;
if (mio_makesckasync (rdev->mio, rdev->sck) <= -1) return -1; if (mio_makesckasync(rdev->mio, rdev->sck) <= -1) return -1;
#if defined(FD_CLOEXEC) #if defined(FD_CLOEXEC)
{ {
int flags = fcntl (rdev->sck, F_GETFD, 0); int flags = fcntl(rdev->sck, F_GETFD, 0);
if (fcntl (rdev->sck, F_SETFD, flags | FD_CLOEXEC) == -1) if (fcntl(rdev->sck, F_SETFD, flags | FD_CLOEXEC) == -1)
{ {
rdev->mio->errnum = mio_syserrtoerrnum(errno); rdev->mio->errnum = mio_syserrtoerrnum(errno);
return -1; return -1;
@ -512,7 +519,7 @@ static int dev_sck_kill (mio_dev_t* dev, int force)
if (rdev->sck != MIO_SCKHND_INVALID) if (rdev->sck != MIO_SCKHND_INVALID)
{ {
mio_closeasyncsck (rdev->mio, rdev->sck); close_async_socket (rdev->mio, rdev->sck);
rdev->sck = MIO_SCKHND_INVALID; rdev->sck = MIO_SCKHND_INVALID;
} }
@ -979,7 +986,7 @@ fcntl (rdev->sck, F_SETFL, flags & ~O_NONBLOCK);
}*/ }*/
/* the socket is already non-blocking */ /* the socket is already non-blocking */
x = connect (rdev->sck, sa, sl); x = connect(rdev->sck, sa, sl);
/*{ /*{
int flags = fcntl (rdev->sck, F_GETFL); int flags = fcntl (rdev->sck, F_GETFL);
fcntl (rdev->sck, F_SETFL, flags | O_NONBLOCK); fcntl (rdev->sck, F_SETFL, flags | O_NONBLOCK);
@ -1013,7 +1020,6 @@ fcntl (rdev->sck, F_SETFL, flags | O_NONBLOCK);
} }
rdev->remoteaddr = conn->remoteaddr; rdev->remoteaddr = conn->remoteaddr;
rdev->on_connect = conn->on_connect;
#if defined(USE_SSL) #if defined(USE_SSL)
rdev->ssl_ctx = ssl_ctx; rdev->ssl_ctx = ssl_ctx;
#endif #endif
@ -1040,10 +1046,9 @@ fcntl (rdev->sck, F_SETFL, flags | O_NONBLOCK);
{ {
/* connected immediately */ /* connected immediately */
rdev->remoteaddr = conn->remoteaddr; rdev->remoteaddr = conn->remoteaddr;
rdev->on_connect = conn->on_connect;
sl = MIO_SIZEOF(localaddr); sl = MIO_SIZEOF(localaddr);
if (getsockname (rdev->sck, (struct sockaddr*)&localaddr, &sl) == 0) rdev->localaddr = localaddr; if (getsockname(rdev->sck, (struct sockaddr*)&localaddr, &sl) == 0) rdev->localaddr = localaddr;
#if defined(USE_SSL) #if defined(USE_SSL)
if (ssl_ctx) if (ssl_ctx)
@ -1051,7 +1056,7 @@ fcntl (rdev->sck, F_SETFL, flags | O_NONBLOCK);
int x; int x;
rdev->ssl_ctx = ssl_ctx; rdev->ssl_ctx = ssl_ctx;
x = connect_ssl (rdev); x = connect_ssl(rdev);
if (x <= -1) if (x <= -1)
{ {
SSL_CTX_free (rdev->ssl_ctx); SSL_CTX_free (rdev->ssl_ctx);
@ -1100,7 +1105,7 @@ fcntl (rdev->sck, F_SETFL, flags | O_NONBLOCK);
ssl_connected: ssl_connected:
#endif #endif
MIO_DEV_SCK_SET_PROGRESS (rdev, MIO_DEV_SCK_CONNECTED); MIO_DEV_SCK_SET_PROGRESS (rdev, MIO_DEV_SCK_CONNECTED);
if (rdev->on_connect (rdev) <= -1) return -1; if (rdev->on_connect(rdev) <= -1) return -1;
#if defined(USE_SSL) #if defined(USE_SSL)
} }
#endif #endif
@ -1134,7 +1139,6 @@ fcntl (rdev->sck, F_SETFL, flags | O_NONBLOCK);
} }
MIO_DEV_SCK_SET_PROGRESS (rdev, MIO_DEV_SCK_LISTENING); MIO_DEV_SCK_SET_PROGRESS (rdev, MIO_DEV_SCK_LISTENING);
rdev->on_connect = lstn->on_connect;
return 0; return 0;
} }
} }
@ -1274,11 +1278,36 @@ static int accept_incoming_connection (mio_dev_sck_t* rdev)
mio_sckaddr_t remoteaddr; mio_sckaddr_t remoteaddr;
mio_scklen_t addrlen; mio_scklen_t addrlen;
mio_dev_sck_t* clidev; mio_dev_sck_t* clidev;
int flags;
/* this is a server(lisening) socket */ /* this is a server(lisening) socket */
#if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC) && defined(HAVE_ACCEPT4)
flags = SOCK_NONBLOCK | SOCK_CLOEXEC;
addrlen = MIO_SIZEOF(remoteaddr); addrlen = MIO_SIZEOF(remoteaddr);
clisck = accept (rdev->sck, (struct sockaddr*)&remoteaddr, &addrlen); clisck = accept4(rdev->sck, (struct sockaddr*)&remoteaddr, &addrlen, flags);
if (clisck == MIO_SCKHND_INVALID)
{
if (errno != ENOSYS)
{
if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0;
if (errno == EINTR) return 0; /* if interrupted by a signal, treat it as if it's EINPROGRESS */
rdev->mio->errnum = mio_syserrtoerrnum(errno);
return -1;
}
// go on for the normal 3-parameter accept
}
else
{
goto accept_done;
}
#endif
addrlen = MIO_SIZEOF(remoteaddr);
clisck = accept(rdev->sck, (struct sockaddr*)&remoteaddr, &addrlen);
if (clisck == MIO_SCKHND_INVALID) if (clisck == MIO_SCKHND_INVALID)
{ {
if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0; if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0;
@ -1288,11 +1317,13 @@ static int accept_incoming_connection (mio_dev_sck_t* rdev)
return -1; return -1;
} }
accept_done:
/* use rdev->dev_size when instantiating a client sck device /* use rdev->dev_size when instantiating a client sck device
* instead of MIO_SIZEOF(mio_dev_sck_t). therefore, the * instead of MIO_SIZEOF(mio_dev_sck_t). therefore, the
* extension area as big as that of the master sck device * extension area as big as that of the master sck device
* is created in the client sck device */ * is created in the client sck device */
clidev = (mio_dev_sck_t*)mio_makedev (rdev->mio, rdev->dev_size, &dev_mth_clisck, rdev->dev_evcb, &clisck); clidev = (mio_dev_sck_t*)mio_makedev(rdev->mio, rdev->dev_size, &dev_mth_clisck, rdev->dev_evcb, &clisck);
if (!clidev) if (!clidev)
{ {
close (clisck); close (clisck);
@ -1336,7 +1367,7 @@ static int accept_incoming_connection (mio_dev_sck_t* rdev)
clidev->state |= MIO_DEV_SCK_INTERCEPTED; clidev->state |= MIO_DEV_SCK_INTERCEPTED;
} }
#if 0 #if 0
else if ((clidev->initial_ifindex = resolve_ifindex (fd, clidev->localaddr)) <= -1) else if ((clidev->initial_ifindex = resolve_ifindex(fd, clidev->localaddr)) <= -1)
{ {
/* the local_address is not one of a local address. /* the local_address is not one of a local address.
* it's probably proxied. */ * it's probably proxied. */
@ -1637,13 +1668,13 @@ mio_dev_sck_t* mio_dev_sck_make (mio_t* mio, mio_oow_t xtnsize, const mio_dev_sc
if (sck_type_map[info->type].extra_dev_capa & MIO_DEV_CAPA_STREAM) /* can't use the IS_STATEFUL() macro yet */ if (sck_type_map[info->type].extra_dev_capa & MIO_DEV_CAPA_STREAM) /* can't use the IS_STATEFUL() macro yet */
{ {
rdev = (mio_dev_sck_t*)mio_makedev ( rdev = (mio_dev_sck_t*)mio_makedev(
mio, MIO_SIZEOF(mio_dev_sck_t) + xtnsize, mio, MIO_SIZEOF(mio_dev_sck_t) + xtnsize,
&dev_sck_methods_stateful, &dev_sck_event_callbacks_stateful, (void*)info); &dev_sck_methods_stateful, &dev_sck_event_callbacks_stateful, (void*)info);
} }
else else
{ {
rdev = (mio_dev_sck_t*)mio_makedev ( rdev = (mio_dev_sck_t*)mio_makedev(
mio, MIO_SIZEOF(mio_dev_sck_t) + xtnsize, mio, MIO_SIZEOF(mio_dev_sck_t) + xtnsize,
&dev_sck_methods_stateless, &dev_sck_event_callbacks_stateless, (void*)info); &dev_sck_methods_stateless, &dev_sck_event_callbacks_stateless, (void*)info);
} }
@ -1653,29 +1684,29 @@ mio_dev_sck_t* mio_dev_sck_make (mio_t* mio, mio_oow_t xtnsize, const mio_dev_sc
int mio_dev_sck_bind (mio_dev_sck_t* dev, mio_dev_sck_bind_t* info) int mio_dev_sck_bind (mio_dev_sck_t* dev, mio_dev_sck_bind_t* info)
{ {
return mio_dev_ioctl ((mio_dev_t*)dev, MIO_DEV_SCK_BIND, info); return mio_dev_ioctl((mio_dev_t*)dev, MIO_DEV_SCK_BIND, info);
} }
int mio_dev_sck_connect (mio_dev_sck_t* dev, mio_dev_sck_connect_t* info) int mio_dev_sck_connect (mio_dev_sck_t* dev, mio_dev_sck_connect_t* info)
{ {
return mio_dev_ioctl ((mio_dev_t*)dev, MIO_DEV_SCK_CONNECT, info); return mio_dev_ioctl((mio_dev_t*)dev, MIO_DEV_SCK_CONNECT, info);
} }
int mio_dev_sck_listen (mio_dev_sck_t* dev, mio_dev_sck_listen_t* info) int mio_dev_sck_listen (mio_dev_sck_t* dev, mio_dev_sck_listen_t* info)
{ {
return mio_dev_ioctl ((mio_dev_t*)dev, MIO_DEV_SCK_LISTEN, info); return mio_dev_ioctl((mio_dev_t*)dev, MIO_DEV_SCK_LISTEN, info);
} }
int mio_dev_sck_write (mio_dev_sck_t* dev, const void* data, mio_iolen_t dlen, void* wrctx, const mio_sckaddr_t* dstaddr) int mio_dev_sck_write (mio_dev_sck_t* dev, const void* data, mio_iolen_t dlen, void* wrctx, const mio_sckaddr_t* dstaddr)
{ {
mio_devaddr_t devaddr; mio_devaddr_t devaddr;
return mio_dev_write ((mio_dev_t*)dev, data, dlen, wrctx, sckaddr_to_devaddr(dev, dstaddr, &devaddr)); return mio_dev_write((mio_dev_t*)dev, data, dlen, wrctx, sckaddr_to_devaddr(dev, dstaddr, &devaddr));
} }
int mio_dev_sck_timedwrite (mio_dev_sck_t* dev, const void* data, mio_iolen_t dlen, const mio_ntime_t* tmout, void* wrctx, const mio_sckaddr_t* dstaddr) int mio_dev_sck_timedwrite (mio_dev_sck_t* dev, const void* data, mio_iolen_t dlen, const mio_ntime_t* tmout, void* wrctx, const mio_sckaddr_t* dstaddr)
{ {
mio_devaddr_t devaddr; mio_devaddr_t devaddr;
return mio_dev_timedwrite ((mio_dev_t*)dev, data, dlen, tmout, wrctx, sckaddr_to_devaddr(dev, dstaddr, &devaddr)); return mio_dev_timedwrite((mio_dev_t*)dev, data, dlen, tmout, wrctx, sckaddr_to_devaddr(dev, dstaddr, &devaddr));
} }

View File

@ -349,6 +349,7 @@ struct mio_dev_sck_make_t
mio_dev_sck_type_t type; mio_dev_sck_type_t type;
mio_dev_sck_on_write_t on_write; mio_dev_sck_on_write_t on_write;
mio_dev_sck_on_read_t on_read; mio_dev_sck_on_read_t on_read;
mio_dev_sck_on_connect_t on_connect;
mio_dev_sck_on_disconnect_t on_disconnect; mio_dev_sck_on_disconnect_t on_disconnect;
}; };
@ -390,14 +391,12 @@ struct mio_dev_sck_connect_t
int options; int options;
mio_sckaddr_t remoteaddr; mio_sckaddr_t remoteaddr;
mio_ntime_t connect_tmout; mio_ntime_t connect_tmout;
mio_dev_sck_on_connect_t on_connect;
}; };
typedef struct mio_dev_sck_listen_t mio_dev_sck_listen_t; typedef struct mio_dev_sck_listen_t mio_dev_sck_listen_t;
struct mio_dev_sck_listen_t struct mio_dev_sck_listen_t
{ {
int backlogs; int backlogs;
mio_dev_sck_on_connect_t on_connect; /* optional, but new connections are dropped immediately without this */
}; };
typedef struct mio_dev_sck_accept_t mio_dev_sck_accept_t; typedef struct mio_dev_sck_accept_t mio_dev_sck_accept_t;
@ -463,18 +462,6 @@ struct mio_dev_sck_t
extern "C" { extern "C" {
#endif #endif
MIO_EXPORT mio_sckhnd_t mio_openasyncsck (
mio_t* mio,
int domain,
int type,
int proto
);
MIO_EXPORT void mio_closeasyncsck (
mio_t* mio,
mio_sckhnd_t sck
);
MIO_EXPORT int mio_makesckasync ( MIO_EXPORT int mio_makesckasync (
mio_t* mio, mio_t* mio,
mio_sckhnd_t sck mio_sckhnd_t sck

View File

@ -208,6 +208,7 @@ mio_uint128_t mio_hton128 (mio_uint128_t x)
/* ========================================================================= */ /* ========================================================================= */
#define MIO_MT(x) (x)
#define IS_MSPACE(x) ((x) == MIO_MT(' ') || (x) == MIO_MT('\t') || (x) == MIO_MT('\n') || (x) == MIO_MT('\r')) #define IS_MSPACE(x) ((x) == MIO_MT(' ') || (x) == MIO_MT('\t') || (x) == MIO_MT('\n') || (x) == MIO_MT('\r'))
mio_bch_t* mio_mbsdup (mio_t* mio, const mio_bch_t* src) mio_bch_t* mio_mbsdup (mio_t* mio, const mio_bch_t* src)
@ -522,3 +523,4 @@ int mio_mbsspl (
{ {
return mio_mbsspltrn (s, delim, lquote, rquote, escape, MIO_NULL); return mio_mbsspltrn (s, delim, lquote, rquote, escape, MIO_NULL);
} }

View File

@ -850,15 +850,15 @@ int mio_loop (mio_t* mio)
mio->stopreq = MIO_STOPREQ_NONE; mio->stopreq = MIO_STOPREQ_NONE;
mio->renew_watch = 0; mio->renew_watch = 0;
if (mio_prologue (mio) <= -1) return -1; if (mio_prologue(mio) <= -1) return -1;
while (mio->stopreq == MIO_STOPREQ_NONE && mio->actdev.head) while (mio->stopreq == MIO_STOPREQ_NONE && mio->actdev.head)
{ {
if (mio_exec (mio) <= -1) break; if (mio_exec(mio) <= -1) break;
/* you can do other things here */ /* you can do other things here */
} }
mio_epilogue (mio); mio_epilogue(mio);
return 0; return 0;
} }
@ -872,7 +872,7 @@ mio_dev_t* mio_makedev (mio_t* mio, mio_oow_t dev_size, mio_dev_mth_t* dev_mth,
return MIO_NULL; return MIO_NULL;
} }
dev = MIO_MMGR_ALLOC (mio->mmgr, dev_size); dev = MIO_MMGR_ALLOC(mio->mmgr, dev_size);
if (!dev) if (!dev)
{ {
mio->errnum = MIO_ENOMEM; mio->errnum = MIO_ENOMEM;
@ -891,7 +891,7 @@ mio_dev_t* mio_makedev (mio_t* mio, mio_oow_t dev_size, mio_dev_mth_t* dev_mth,
/* call the callback function first */ /* call the callback function first */
mio->errnum = MIO_ENOERR; mio->errnum = MIO_ENOERR;
if (dev->dev_mth->make (dev, make_ctx) <= -1) if (dev->dev_mth->make(dev, make_ctx) <= -1)
{ {
if (mio->errnum == MIO_ENOERR) mio->errnum = MIO_EDEVMAKE; if (mio->errnum == MIO_ENOERR) mio->errnum = MIO_EDEVMAKE;
goto oops; goto oops;
@ -915,7 +915,7 @@ mio_dev_t* mio_makedev (mio_t* mio, mio_oow_t dev_size, mio_dev_mth_t* dev_mth,
goto oops_after_make; goto oops_after_make;
} }
#else #else
if (mio_dev_watch (dev, MIO_DEV_WATCH_START, 0) <= -1) goto oops_after_make; if (mio_dev_watch(dev, MIO_DEV_WATCH_START, 0) <= -1) goto oops_after_make;
#endif #endif
/* and place the new device object at the back of the active device list */ /* and place the new device object at the back of the active device list */
@ -925,17 +925,17 @@ mio_dev_t* mio_makedev (mio_t* mio, mio_oow_t dev_size, mio_dev_mth_t* dev_mth,
return dev; return dev;
oops_after_make: oops_after_make:
if (kill_and_free_device (dev, 0) <= -1) if (kill_and_free_device(dev, 0) <= -1)
{ {
/* schedule a timer job that reattempts to destroy the device */ /* schedule a timer job that reattempts to destroy the device */
if (schedule_kill_zombie_job (dev) <= -1) if (schedule_kill_zombie_job(dev) <= -1)
{ {
/* job scheduling failed. i have no choice but to /* job scheduling failed. i have no choice but to
* destroy the device now. * destroy the device now.
* *
* NOTE: this while loop can block the process * NOTE: this while loop can block the process
* if the kill method keep returning failure */ * if the kill method keep returning failure */
while (kill_and_free_device (dev, 1) <= -1) while (kill_and_free_device(dev, 1) <= -1)
{ {
if (mio->stopreq != MIO_STOPREQ_NONE) if (mio->stopreq != MIO_STOPREQ_NONE)
{ {
@ -996,12 +996,12 @@ static void kill_zombie_job_handler (mio_t* mio, const mio_ntime_t* now, mio_tmr
MIO_ASSERT (dev->dev_capa & MIO_DEV_CAPA_ZOMBIE); MIO_ASSERT (dev->dev_capa & MIO_DEV_CAPA_ZOMBIE);
if (kill_and_free_device (dev, 0) <= -1) if (kill_and_free_device(dev, 0) <= -1)
{ {
if (schedule_kill_zombie_job (dev) <= -1) if (schedule_kill_zombie_job(dev) <= -1)
{ {
/* i have to choice but to free up the devide by force */ /* i have to choice but to free up the devide by force */
while (kill_and_free_device (dev, 1) <= -1) while (kill_and_free_device(dev, 1) <= -1)
{ {
if (mio->stopreq != MIO_STOPREQ_NONE) if (mio->stopreq != MIO_STOPREQ_NONE)
{ {
@ -1292,7 +1292,7 @@ static int __dev_write (mio_dev_t* dev, const void* data, mio_iolen_t len, const
dev->dev_capa |= MIO_DEV_CAPA_OUT_CLOSED; dev->dev_capa |= MIO_DEV_CAPA_OUT_CLOSED;
} }
if (dev->dev_evcb->on_write (dev, len, wrctx, dstaddr) <= -1) return -1; if (dev->dev_evcb->on_write(dev, len, wrctx, dstaddr) <= -1) return -1;
} }
else else
{ {
@ -1355,7 +1355,7 @@ enqueue_data:
tmrjob.handler = on_write_timeout; tmrjob.handler = on_write_timeout;
tmrjob.idxptr = &q->tmridx; tmrjob.idxptr = &q->tmridx;
q->tmridx = mio_instmrjob (dev->mio, &tmrjob); q->tmridx = mio_instmrjob(dev->mio, &tmrjob);
if (q->tmridx == MIO_TMRIDX_INVALID) if (q->tmridx == MIO_TMRIDX_INVALID)
{ {
MIO_MMGR_FREE (dev->mio->mmgr, q); MIO_MMGR_FREE (dev->mio->mmgr, q);
@ -1367,7 +1367,7 @@ enqueue_data:
if (!dev->mio->in_exec && !(dev->dev_capa & MIO_DEV_CAPA_OUT_WATCHED)) if (!dev->mio->in_exec && !(dev->dev_capa & MIO_DEV_CAPA_OUT_WATCHED))
{ {
/* if output is not being watched, arrange to do so */ /* if output is not being watched, arrange to do so */
if (mio_dev_watch (dev, MIO_DEV_WATCH_RENEW, 0) <= -1) if (mio_dev_watch(dev, MIO_DEV_WATCH_RENEW, 0) <= -1)
{ {
unlink_wq (dev->mio, q); unlink_wq (dev->mio, q);
MIO_MMGR_FREE (dev->mio->mmgr, q); MIO_MMGR_FREE (dev->mio->mmgr, q);
@ -1384,12 +1384,12 @@ enqueue_data:
int mio_dev_write (mio_dev_t* dev, const void* data, mio_iolen_t len, void* wrctx, const mio_devaddr_t* dstaddr) int mio_dev_write (mio_dev_t* dev, const void* data, mio_iolen_t len, void* wrctx, const mio_devaddr_t* dstaddr)
{ {
return __dev_write (dev, data, len, MIO_NULL, wrctx, dstaddr); return __dev_write(dev, data, len, MIO_NULL, wrctx, dstaddr);
} }
int mio_dev_timedwrite (mio_dev_t* dev, const void* data, mio_iolen_t len, const mio_ntime_t* tmout, void* wrctx, const mio_devaddr_t* dstaddr) int mio_dev_timedwrite (mio_dev_t* dev, const void* data, mio_iolen_t len, const mio_ntime_t* tmout, void* wrctx, const mio_devaddr_t* dstaddr)
{ {
return __dev_write (dev, data, len, tmout, wrctx, dstaddr); return __dev_write(dev, data, len, tmout, wrctx, dstaddr);
} }
int mio_makesyshndasync (mio_t* mio, mio_syshnd_t hnd) int mio_makesyshndasync (mio_t* mio, mio_syshnd_t hnd)
@ -1397,8 +1397,8 @@ int mio_makesyshndasync (mio_t* mio, mio_syshnd_t hnd)
#if defined(F_GETFL) && defined(F_SETFL) && defined(O_NONBLOCK) #if defined(F_GETFL) && defined(F_SETFL) && defined(O_NONBLOCK)
int flags; int flags;
if ((flags = fcntl (hnd, F_GETFL)) <= -1 || if ((flags = fcntl(hnd, F_GETFL)) <= -1 ||
(flags = fcntl (hnd, F_SETFL, flags | O_NONBLOCK)) <= -1) (flags = fcntl(hnd, F_SETFL, flags | O_NONBLOCK)) <= -1)
{ {
mio->errnum = mio_syserrtoerrnum (errno); mio->errnum = mio_syserrtoerrnum (errno);
return -1; return -1;