From b30bbf80630e23ebf8a0e6eead7aacaa7764d45c Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Fri, 11 Jan 2019 07:35:43 +0000 Subject: [PATCH] touched up code for renewal --- mio/configure | 12 ++++ mio/configure.ac | 1 + mio/lib/Makefile.am | 2 +- mio/lib/Makefile.in | 4 +- mio/lib/main.c | 83 +++++++++++++++++++----- mio/lib/mio-cfg.h.in | 15 +++++ mio/lib/mio-sck.c | 147 ++++++++++++++++++++++++++----------------- mio/lib/mio-sck.h | 21 ++----- mio/lib/mio-utl.c | 2 + mio/lib/mio.c | 42 ++++++------- 10 files changed, 214 insertions(+), 115 deletions(-) diff --git a/mio/configure b/mio/configure index a6ee0d3..4ed0c5d 100755 --- a/mio/configure +++ b/mio/configure @@ -17940,6 +17940,18 @@ _ACEOF fi 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" LIBS="$LIBM $LIBS" diff --git a/mio/configure.ac b/mio/configure.ac index c2f9f2b..301f765 100644 --- a/mio/configure.ac +++ b/mio/configure.ac @@ -160,6 +160,7 @@ AC_CHECK_FUNCS([backtrace backtrace_symbols]) AC_CHECK_FUNCS([fork vfork posix_spawn gettid nanosleep select]) AC_CHECK_FUNCS([makecontext swapcontext getcontext setcontext]) AC_CHECK_FUNCS([snprintf _vsnprintf _vsnwprintf]) +AC_CHECK_FUNCS([accept4 sendmsg recvmsg writev readv]) OLDLIBS="$LIBS" LIBS="$LIBM $LIBS" diff --git a/mio/lib/Makefile.am b/mio/lib/Makefile.am index dcd93c0..d3267a9 100644 --- a/mio/lib/Makefile.am +++ b/mio/lib/Makefile.am @@ -44,7 +44,7 @@ bin_PROGRAMS = mio mio_SOURCES = main.c mio_CPPFLAGS = $(CPPFLAGS_LIB_COMMON) mio_LDFLAGS = $(LDFLAGS_LIB_COMMON) -mio_LDADD = $(LIBADD_LIB_COMMON) -lmio +mio_LDADD = $(LIBADD_LIB_COMMON) -lmio $(SSL_LIBS) install-data-hook: @echo "#ifndef _MIO_CFG_H_" > "$(DESTDIR)$(includedir)/mio-cfg.h" diff --git a/mio/lib/Makefile.in b/mio/lib/Makefile.in index 9c27bea..ea7cfa4 100644 --- a/mio/lib/Makefile.in +++ b/mio/lib/Makefile.in @@ -154,7 +154,7 @@ libmio_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ PROGRAMS = $(bin_PROGRAMS) am_mio_OBJECTS = mio-main.$(OBJEXT) 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) \ $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ $(mio_LDFLAGS) $(LDFLAGS) -o $@ @@ -411,7 +411,7 @@ libmio_la_LIBADD = $(LIBADD_LIB_COMMON) $(SSL_LIBS) mio_SOURCES = main.c mio_CPPFLAGS = $(CPPFLAGS_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 $(MAKE) $(AM_MAKEFLAGS) all-am diff --git a/mio/lib/main.c b/mio/lib/main.c index daf4a71..111dfba 100644 --- a/mio/lib/main.c +++ b/mio/lib/main.c @@ -299,6 +299,11 @@ static int arp_sck_on_write (mio_dev_sck_t* dev, mio_iolen_t wrlen, void* wrctx, 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) { 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.on_write = arp_sck_on_write; 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 = mio_dev_sck_make (mio, 0, &sck_make); if (!sck) @@ -325,7 +331,9 @@ static int setup_arp_tester (mio_t* mio) } //mio_sckaddr_initforeth (ðdst, if_nametoindex("enp0s25.3"), (mio_ethaddr_t*)"\xFF\xFF\xFF\xFF\xFF\xFF"); - mio_sckaddr_initforeth (ðdst, if_nametoindex("enp0s25.3"), (mio_ethaddr_t*)"\xAA\xBB\xFF\xCC\xDD\xFF"); + //mio_sckaddr_initforeth (ðdst, if_nametoindex("enp0s25.3"), (mio_ethaddr_t*)"\xAA\xBB\xFF\xCC\xDD\xFF"); + + mio_sckaddr_initforeth (ðdst, if_nametoindex("wlan0"), (mio_ethaddr_t*)"\xAA\xBB\xFF\xCC\xDD\xFF"); memset (ðarp, 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); - if (mio_dev_sck_write (sck, ðarp, sizeof(etharp), NULL, ðdst) <= -1) + if (mio_dev_sck_write(sck, ðarp, sizeof(etharp), NULL, ðdst) <= -1) //if (mio_dev_sck_write (sck, ðarp.arphdr, sizeof(etharp) - sizeof(etharp.ethhdr), NULL, ðaddr) <= -1) { 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); } -int main () +int main (int argc, char* argv[]) { int i; @@ -590,7 +598,7 @@ int main () sin.sin_family = AF_INET; 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) { printf ("Cannot make udp\n"); @@ -602,8 +610,9 @@ int main () 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; - 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]) { printf ("Cannot make tcp\n"); @@ -621,7 +630,6 @@ int main () } mio_inittime (&tcp_conn.connect_tmout, 5, 0); - tcp_conn.on_connect = tcp_sck_on_connect; tcp_conn.options = MIO_DEV_SCK_CONNECT_SSL; 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.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; - 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]) { printf ("Cannot make tcp\n"); @@ -651,16 +660,15 @@ int main () 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; } tcp_lstn.backlogs = 100; - tcp_lstn.on_connect = tcp_sck_on_connect; 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; } @@ -669,6 +677,7 @@ int main () 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; 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"; 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; } 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; } @@ -706,7 +714,6 @@ int main () if (setup_arp_tester(mio) <= -1) goto oops; if (setup_ping4_tester(mio) <= -1) goto oops; - for (i = 0; i < 5; i++) { mio_dev_pro_t* pro; @@ -753,3 +760,47 @@ oops: #endif 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 diff --git a/mio/lib/mio-cfg.h.in b/mio/lib/mio-cfg.h.in index 87a601d..224f8d9 100644 --- a/mio/lib/mio-cfg.h.in +++ b/mio/lib/mio-cfg.h.in @@ -3,6 +3,9 @@ /* Define if building universal (internal helper macro) */ #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. */ #undef HAVE_ACOS @@ -385,6 +388,12 @@ /* Define to 1 if you have the `readdir64' function. */ #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. */ #undef HAVE_ROUND @@ -412,6 +421,9 @@ /* Define to 1 if you have the `sendfilev64' function. */ #undef HAVE_SENDFILEV64 +/* Define to 1 if you have the `sendmsg' function. */ +#undef HAVE_SENDMSG + /* Define to 1 if you have the `setcontext' function. */ #undef HAVE_SETCONTEXT @@ -687,6 +699,9 @@ /* Define to 1 if you have the header file. */ #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. */ #undef HAVE__VSNPRINTF diff --git a/mio/lib/mio-sck.c b/mio/lib/mio-sck.c index f6d1a58..2a29a26 100644 --- a/mio/lib/mio-sck.c +++ b/mio/lib/mio-sck.c @@ -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) { 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; + close (sck); +} -#if defined(_WIN32) - sck = WSASocket (domain, type, proto, NULL, 0, WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT); +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) { - /* 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) + #if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC) + if (errno == EINVAL && (type & (SOCK_NONBLOCK | SOCK_CLOEXEC))) { - mio->errnum = mio_syserrtoerrnum(errno); - return MIO_SCKHND_INVALID; + type &= ~(SOCK_NONBLOCK | SOCK_CLOEXEC); + goto open_socket; } + #endif + goto oops; } -#endif - - if (mio_makesckasync (mio, sck) <= -1) + else { - close (sck); - return MIO_SCKHND_INVALID; + #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 +#if defined(O_NONBLOCK) + flags |= O_NONBLOCK; +#endif + if (fcntl(sck, F_SETFD, flags) == -1) goto oops; +done: 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) @@ -421,12 +425,13 @@ static int dev_sck_make (mio_dev_t* dev, void* ctx) 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; 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_read = arg->on_read; + rdev->on_connect = arg->on_connect; rdev->on_disconnect = arg->on_disconnect; rdev->type = arg->type; rdev->tmrjob_index = MIO_TMRIDX_INVALID; @@ -436,7 +441,7 @@ static int dev_sck_make (mio_dev_t* dev, void* ctx) oops: if (rdev->sck != MIO_SCKHND_INVALID) { - mio_closeasyncsck (rdev->mio, rdev->sck); + close_async_socket (rdev->mio, rdev->sck); rdev->sck = MIO_SCKHND_INVALID; } 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_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 * after a client socket has been created. */ rdev->sck = *sck; 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) { - int flags = fcntl (rdev->sck, F_GETFD, 0); - if (fcntl (rdev->sck, F_SETFD, flags | FD_CLOEXEC) == -1) + int flags = fcntl(rdev->sck, F_GETFD, 0); + if (fcntl(rdev->sck, F_SETFD, flags | FD_CLOEXEC) == -1) { rdev->mio->errnum = mio_syserrtoerrnum(errno); return -1; @@ -512,7 +519,7 @@ static int dev_sck_kill (mio_dev_t* dev, int force) if (rdev->sck != MIO_SCKHND_INVALID) { - mio_closeasyncsck (rdev->mio, rdev->sck); + close_async_socket (rdev->mio, rdev->sck); rdev->sck = MIO_SCKHND_INVALID; } @@ -979,7 +986,7 @@ fcntl (rdev->sck, F_SETFL, flags & ~O_NONBLOCK); }*/ /* 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); 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->on_connect = conn->on_connect; #if defined(USE_SSL) rdev->ssl_ctx = ssl_ctx; #endif @@ -1040,10 +1046,9 @@ fcntl (rdev->sck, F_SETFL, flags | O_NONBLOCK); { /* connected immediately */ rdev->remoteaddr = conn->remoteaddr; - rdev->on_connect = conn->on_connect; 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 (ssl_ctx) @@ -1051,7 +1056,7 @@ fcntl (rdev->sck, F_SETFL, flags | O_NONBLOCK); int x; rdev->ssl_ctx = ssl_ctx; - x = connect_ssl (rdev); + x = connect_ssl(rdev); if (x <= -1) { SSL_CTX_free (rdev->ssl_ctx); @@ -1100,7 +1105,7 @@ fcntl (rdev->sck, F_SETFL, flags | O_NONBLOCK); ssl_connected: #endif 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) } #endif @@ -1134,7 +1139,6 @@ fcntl (rdev->sck, F_SETFL, flags | O_NONBLOCK); } MIO_DEV_SCK_SET_PROGRESS (rdev, MIO_DEV_SCK_LISTENING); - rdev->on_connect = lstn->on_connect; return 0; } } @@ -1274,11 +1278,36 @@ static int accept_incoming_connection (mio_dev_sck_t* rdev) mio_sckaddr_t remoteaddr; mio_scklen_t addrlen; mio_dev_sck_t* clidev; + int flags; /* 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); - 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 (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0; @@ -1288,11 +1317,13 @@ static int accept_incoming_connection (mio_dev_sck_t* rdev) return -1; } + +accept_done: /* use rdev->dev_size when instantiating a client sck device * instead of MIO_SIZEOF(mio_dev_sck_t). therefore, the * extension area as big as that of the master 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) { close (clisck); @@ -1336,7 +1367,7 @@ static int accept_incoming_connection (mio_dev_sck_t* rdev) clidev->state |= MIO_DEV_SCK_INTERCEPTED; } #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. * 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 */ { - rdev = (mio_dev_sck_t*)mio_makedev ( + rdev = (mio_dev_sck_t*)mio_makedev( mio, MIO_SIZEOF(mio_dev_sck_t) + xtnsize, &dev_sck_methods_stateful, &dev_sck_event_callbacks_stateful, (void*)info); } else { - rdev = (mio_dev_sck_t*)mio_makedev ( + rdev = (mio_dev_sck_t*)mio_makedev( mio, MIO_SIZEOF(mio_dev_sck_t) + xtnsize, &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) { - 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) { - 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) { - 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) { 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) { 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)); } diff --git a/mio/lib/mio-sck.h b/mio/lib/mio-sck.h index 1540572..cc21897 100644 --- a/mio/lib/mio-sck.h +++ b/mio/lib/mio-sck.h @@ -300,7 +300,7 @@ typedef struct mio_dev_sck_t mio_dev_sck_t; typedef int (*mio_dev_sck_on_read_t) ( mio_dev_sck_t* dev, - const void* data, + const void* data, mio_iolen_t dlen, const mio_sckaddr_t* srcaddr ); @@ -308,7 +308,7 @@ typedef int (*mio_dev_sck_on_read_t) ( typedef int (*mio_dev_sck_on_write_t) ( mio_dev_sck_t* dev, mio_iolen_t wrlen, - void* wrctx, + void* wrctx, const mio_sckaddr_t* dstaddr ); @@ -349,6 +349,7 @@ struct mio_dev_sck_make_t mio_dev_sck_type_t type; mio_dev_sck_on_write_t on_write; mio_dev_sck_on_read_t on_read; + mio_dev_sck_on_connect_t on_connect; mio_dev_sck_on_disconnect_t on_disconnect; }; @@ -390,14 +391,12 @@ struct mio_dev_sck_connect_t int options; mio_sckaddr_t remoteaddr; 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; struct mio_dev_sck_listen_t { 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; @@ -463,18 +462,6 @@ struct mio_dev_sck_t extern "C" { #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_t* mio, mio_sckhnd_t sck @@ -518,7 +505,7 @@ MIO_EXPORT void mio_sckaddr_initforip6 ( MIO_EXPORT void mio_sckaddr_initforeth ( mio_sckaddr_t* sckaddr, - int ifindex, + int ifindex, mio_ethaddr_t* ethaddr ); diff --git a/mio/lib/mio-utl.c b/mio/lib/mio-utl.c index 66eb598..c55a49d 100644 --- a/mio/lib/mio-utl.c +++ b/mio/lib/mio-utl.c @@ -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')) 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); } + diff --git a/mio/lib/mio.c b/mio/lib/mio.c index e06bcb4..3272915 100644 --- a/mio/lib/mio.c +++ b/mio/lib/mio.c @@ -850,15 +850,15 @@ int mio_loop (mio_t* mio) mio->stopreq = MIO_STOPREQ_NONE; 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) { - if (mio_exec (mio) <= -1) break; + if (mio_exec(mio) <= -1) break; /* you can do other things here */ } - mio_epilogue (mio); + mio_epilogue(mio); 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; } - dev = MIO_MMGR_ALLOC (mio->mmgr, dev_size); + dev = MIO_MMGR_ALLOC(mio->mmgr, dev_size); if (!dev) { 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 */ 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; 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; } #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 /* 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; 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 */ - if (schedule_kill_zombie_job (dev) <= -1) + if (schedule_kill_zombie_job(dev) <= -1) { /* job scheduling failed. i have no choice but to * destroy the device now. * * NOTE: this while loop can block the process * 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) { @@ -996,14 +996,14 @@ 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); - 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 */ - 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) { /* i can't wait until destruction attempt gets * fully successful. there is a chance that some @@ -1272,7 +1272,7 @@ static int __dev_write (mio_dev_t* dev, const void* data, mio_iolen_t len, const { /* [NOTE] * the write queue is empty at this moment. a zero-length - * request for a stream device can still get enqueued if the + * request for a stream device can still get enqueued if the * write callback returns 0 though i can't figure out if there * is a compelling reason to do so */ @@ -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; } - 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 { @@ -1355,7 +1355,7 @@ enqueue_data: tmrjob.handler = on_write_timeout; tmrjob.idxptr = &q->tmridx; - q->tmridx = mio_instmrjob (dev->mio, &tmrjob); + q->tmridx = mio_instmrjob(dev->mio, &tmrjob); if (q->tmridx == MIO_TMRIDX_INVALID) { 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 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); 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) { - 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) { - 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) @@ -1397,8 +1397,8 @@ int mio_makesyshndasync (mio_t* mio, mio_syshnd_t hnd) #if defined(F_GETFL) && defined(F_SETFL) && defined(O_NONBLOCK) int flags; - if ((flags = fcntl (hnd, F_GETFL)) <= -1 || - (flags = fcntl (hnd, F_SETFL, flags | O_NONBLOCK)) <= -1) + if ((flags = fcntl(hnd, F_GETFL)) <= -1 || + (flags = fcntl(hnd, F_SETFL, flags | O_NONBLOCK)) <= -1) { mio->errnum = mio_syserrtoerrnum (errno); return -1;