30 #define close(x) closesocket(x)
33 static char buff[1024];
34 if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, code, 0, buff,
sizeof(buff), NULL))
36 return strerror(code);
71 #define MIO(m) ((mio_priv_t) m)
72 #define FD(m,f) ((mio_priv_fd_t) f)
73 #define ACT(m,f,a,d) (*(FD(m,f)->app))(m,a,&FD(m,f)->mio_fd,d,FD(m,f)->arg)
80 #define mio_debug if(MIO_DEBUG) _mio_debug
81 static void _mio_debug(
int line,
const char *msgfmt, ...)
85 fprintf(stderr,
"mio.c#%d: ",line);
86 vfprintf(stderr,msgfmt,ap);
102 if (mio_fd == NULL)
return NULL;
106 FD(m,mio_fd)->app = app;
107 FD(m,mio_fd)->arg = arg;
110 #if defined(HAVE_FCNTL)
111 flags = fcntl(fd, F_GETFL);
113 fcntl(fd, F_SETFL, flags);
114 #elif defined(HAVE_IOCTL)
116 ioctl(fd, FIONBIO, &flags);
134 if (
FD(m,fd)->app != NULL)
140 FD(m,fd)->app = NULL;
141 FD(m,fd)->arg = NULL;
153 socklen_t addrlen = (socklen_t)
sizeof(serv_addr);
161 newfd = accept(fd->
fd, (
struct sockaddr*)&serv_addr, &addrlen);
162 if(newfd <= 0)
return;
228 if(retval == 0)
return;
238 mio_debug(
ZONE,
"mio processing %d file descriptors", retval);
244 if (fd == NULL)
continue;
293 if(m == NULL || fd == NULL)
return;
308 if(m == NULL || fd == NULL)
return;
333 if(m == NULL)
return NULL;
337 memset(&sa, 0,
sizeof(sa));
340 if(sourceip != NULL && !
j_inet_pton(sourceip, &sa))
347 if((fd = socket(sa.
ss_family,SOCK_STREAM,0)) < 0)
return NULL;
348 if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (
char*)&flag,
sizeof(flag)) < 0)
return NULL;
359 if(listen(fd, -1) < 0)
386 memset(&sa, 0,
sizeof(sa));
388 if(m == NULL || port <= 0 || hostip == NULL)
return NULL;
390 mio_debug(
ZONE,
"mio connecting to %s, port=%d",hostip,port);
401 if((fd = socket(sa.
ss_family,SOCK_STREAM,0)) < 0)
return NULL;
419 #if defined(HAVE_FCNTL)
420 flags = fcntl(fd, F_GETFL);
422 fcntl(fd, F_SETFL, flags);
423 #elif defined(HAVE_IOCTL)
425 ioctl(fd, FIONBIO, &flags);
440 if(mio_fd != NULL)
return mio_fd;
445 if(flag == -1 && WSAGetLastError() == WSAEWOULDBLOCK)
447 if(flag == -1 && errno == EINPROGRESS)
478 static struct mio_st mio_impl = {
491 if (WSAStartup(MAKEWORD( 1, 1 ), &wsaData))
496 if((m = calloc(1,
sizeof(
struct mio_priv_st))) == NULL) {
497 fprintf(stderr,
"Cannot allocate MIO memory! Exiting.\n");
503 MIO(m)->maxfd = maxfd;
static void _mio_write(mio_t m, mio_fd_t fd)
try writing to the socket via the app
#define INET6_ADDRSTRLEN
maximum length of the string representation of an IPv6 address
socklen_t j_inet_addrlen(struct sockaddr_storage *sa)
calculate the size of an address structure (on some unices the stdlibc functions for socket handling ...
int j_inet_pton(const char *src, struct sockaddr_storage *dst)
set the address of a struct sockaddr_storage (modeled after the stdlib function inet_pton) ...
static void _mio__connect(mio_t m, mio_fd_t fd)
internally change a connecting socket to a normal one
static void _mio_debug(int line, const char *msgfmt,...)
#define MIO_SET_WRITE(m, mfd)
static mio_fd_t _mio_listen(mio_t m, int port, const char *sourceip, mio_handler_t app, void *arg)
set up a listener in this mio w/ this default app/arg
static void _mio_run(mio_t m, int timeout)
main select loop runner
static void _mio_close(mio_t m, mio_fd_t fd)
internal close function
#define MIO_INIT_ITERATOR(iter)
sa_family_t ss_family
address family
#define MIO_SET_READ(m, mfd)
static mio_t _mio_new(int maxfd)
eve
#define mio_read(m, fd)
process read events for this fd
#define MIO_UNSET_WRITE(m, mfd)
#define MIO_ERROR
all MIO related routines should use those for error reporting
#define MIO_REMOVE_FD(m, mfd)
mio_type_t
our internal wrapper around a fd
#define MIO_FREE_FD(m, mfd)
struct mio_priv_st * mio_priv_t
now define our master data type
int j_inet_getport(struct sockaddr_storage *sa)
get the port number out of a struct sockaddr_storage
static void _mio_read(mio_t m, mio_fd_t fd)
start processing read events
static mio_fd_t _mio_connect(mio_t m, int port, const char *hostip, const char *srcip, mio_handler_t app, void *arg)
create an fd and connect to the given ip/port
static void _mio_free(mio_t m)
adam
now define our master data type
struct mio_priv_fd_st * mio_priv_fd_t
int(* mio_handler_t)(struct mio_st **m, mio_action_t a, struct mio_fd_st *fd, void *data, void *arg)
#define MIO_ITERATOR_FD(m, iter)
static void _mio_app(mio_t m, mio_fd_t fd, mio_handler_t app, void *arg)
reset app stuff for this fd
#define mio_write(m, fd)
mio should try the write action on this fd now
#define MIO_ALLOC_FD(m, rfd)
#define MIO_CAN_READ(m, iter)
static MIO_FUNCS mio_fd_t _mio_setup_fd(mio_t m, int fd, mio_handler_t app, void *arg)
add and set up this fd to this mio
const char * j_inet_ntop(struct sockaddr_storage *src, char *dst, size_t size)
get the string representation of an address in struct sockaddr_storage (modeled after the stdlib func...
#define MIO_UNSET_READ(m, mfd)
static void _mio_accept(mio_t m, mio_fd_t fd)
internally accept an incoming connection from a listen sock
#define MIO_CAN_WRITE(m, iter)
int j_inet_setport(struct sockaddr_storage *sa, in_port_t port)
set the port number in a struct sockaddr_storage
#define MIO_ITERATE_RESULTS(m, retval, iter)