jabberd2  2.2.16
mio/mio.h
Go to the documentation of this file.
00001 /*
00002  * jabberd - Jabber Open Source Server
00003  * Copyright (c) 2002 Jeremie Miller, Thomas Muldowney,
00004  *                    Ryan Eatmon, Robert Norris
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
00019  */
00020 
00021 #ifndef INCL_MIO_H
00022 #define INCL_MIO_H
00023 
00024 #ifdef HAVE_CONFIG_H
00025 # include <config.h>
00026 #endif
00027 #include "util/inaddr.h"
00028 #include "ac-stdint.h"
00029 
00030 /* jabberd2 Windows DLL */
00031 #ifndef JABBERD2_API
00032 # ifdef _WIN32
00033 #  ifdef JABBERD2_EXPORTS
00034 #   define JABBERD2_API  __declspec(dllexport)
00035 #  else /* JABBERD2_EXPORTS */
00036 #   define JABBERD2_API  __declspec(dllimport)
00037 #  endif /* JABBERD2_EXPORTS */
00038 # else /* _WIN32 */
00039 #  define JABBERD2_API extern
00040 # endif /* _WIN32 */
00041 #endif /* JABBERD2_API */
00042 
00043 #ifdef _WIN32
00044 # define MIO_MAXFD FD_SETSIZE
00045 #else
00046 # define MIO_MAXFD 1024
00047 #endif
00048 
00049 #include <stdio.h>
00050 #include <errno.h>
00051 #include <stdlib.h>
00052 #include <stdarg.h>
00053 
00054 #ifdef HAVE_UNISTD_H
00055 # include <unistd.h>
00056 #endif
00057 
00058 #ifdef HAVE_SYS_SOCKET_H
00059 # include <sys/socket.h>
00060 #endif
00061 
00062 #ifdef HAVE_FCNTL_H
00063 # include <fcntl.h>
00064 #endif
00065 
00066 #ifdef HAVE_SYS_IOCTL_H
00067 # include <sys/ioctl.h>
00068 #endif
00069 
00070 #ifdef HAVE_SYS_FILIO_H
00071 # include <sys/filio.h>
00072 #endif
00073 
00074 #ifdef __cplusplus
00075 extern "C" {
00076 #endif
00077 
00098 struct mio_st;
00099 
00100 typedef struct mio_fd_st
00101 {
00102     int fd;
00103 } *mio_fd_t;
00104 
00106 typedef enum { action_ACCEPT, action_READ, action_WRITE, action_CLOSE } mio_action_t;
00107 typedef int (*mio_handler_t) (struct mio_st **m, mio_action_t a, struct mio_fd_st *fd, void* data, void *arg);
00108 
00109 typedef struct mio_st
00110 {
00111   void (*mio_free)(struct mio_st **m);
00112 
00113   struct mio_fd_st *(*mio_listen)(struct mio_st **m, int port, char *sourceip,
00114                   mio_handler_t app, void *arg);
00115 
00116   struct mio_fd_st *(*mio_connect)(struct mio_st **m, int port, char *hostip,
00117                    char *srcip, mio_handler_t app, void *arg);
00118 
00119   struct mio_fd_st *(*mio_register)(struct mio_st **m, int fd,
00120                    mio_handler_t app, void *arg);
00121 
00122   void (*mio_app)(struct mio_st **m, struct mio_fd_st *fd,
00123           mio_handler_t app, void *arg);
00124 
00125   void (*mio_close)(struct mio_st **m, struct mio_fd_st *fd);
00126 
00127   void (*mio_write)(struct mio_st **m, struct mio_fd_st *fd);
00128 
00129   void (*mio_read)(struct mio_st **m, struct mio_fd_st *fd);
00130 
00131   void (*mio_run)(struct mio_st **m, int timeout);
00132 } **mio_t;
00133 
00135 JABBERD2_API mio_t mio_new(int maxfd); /* returns NULL if failed */
00136 
00137 #define mio_free(m) (*m)->mio_free(m)
00138 
00140 #define mio_listen(m, port, sourceip, app, arg) \
00141     (*m)->mio_listen(m, port, sourceip, app, arg)
00142 
00144 #define mio_connect(m, port, hostip, srcip, app, arg) \
00145     (*m)->mio_connect(m, port, hostip, srcip, app, arg)
00146 
00148 #define mio_register(m, fd, app, arg) \
00149     (*m)->mio_register(m, fd, app, arg)
00150 
00152 #define mio_app(m, fd, app, arg) (*m)->mio_app(m, fd, app, arg)
00153 
00155 #define mio_close(m, fd) (*m)->mio_close(m, fd)
00156 
00158 #define mio_write(m, fd) (*m)->mio_write(m, fd)
00159 
00161 #define mio_read(m, fd) (*m)->mio_read(m, fd)
00162 
00164 #define mio_run(m, timeout) (*m)->mio_run(m, timeout)
00165 
00167 #ifndef _WIN32
00168 # define MIO_ERROR       errno
00169 # define MIO_SETERROR(e) (errno = e)
00170 # define MIO_STRERROR(e) strerror(e)
00171 # define MIO_WOULDBLOCK  (errno == EWOULDBLOCK || errno == EINTR || errno == EAGAIN)
00172 #else /* _WIN32 */
00173 JABBERD2_API char *mio_strerror(int code);
00174 # define MIO_ERROR       WSAGetLastError()
00175 # define MIO_SETERROR(e) WSASetLastError(e)
00176 # define MIO_STRERROR(e) mio_strerror(e)
00177 # define MIO_WOULDBLOCK  (WSAGetLastError() == WSAEWOULDBLOCK)
00178 #endif /* _WIN32 */
00179 
00180 #ifdef __cplusplus
00181 }
00182 #endif
00183 
00184 #endif  /* INCL_MIO_H */
00185