added additional iohandler_poll_timeout function
[NeonServV5.git] / src / IOHandler.h
1 /* IOHandler.h - IOMultiplexer
2  * Copyright (C) 2012  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17 #ifndef _IOHandler_h
18 #define _IOHandler_h
19 #include <stddef.h>
20 #include <sys/time.h> /* struct timeval */
21
22 #define IO_READ_BUFLEN 1024
23 #define IO_MAX_TIMEOUT 10
24 #define IO_LINE_LEN    1024
25
26 struct timeval;
27 struct IODescriptor;
28 struct IOEvent;
29 struct IOSSLNode;
30
31 enum IOLogType {
32     IOLOG_DEBUG,
33     IOLOG_WARNING,
34     IOLOG_ERROR,
35     IOLOG_FATAL
36 };
37
38 #define IOHANDLER_CALLBACK(NAME) void NAME(struct IOEvent *event)
39 typedef IOHANDLER_CALLBACK(iohandler_callback);
40
41 #define IOHANDLER_LOG_BACKEND(NAME) void NAME(enum IOLogType type, const char *line)
42 typedef IOHANDLER_LOG_BACKEND(iohandler_log_callback);
43
44 extern iohandler_log_callback *iolog_backend;
45
46 enum IOType {
47     IOTYPE_UNKNOWN, /* ignore descriptor (uninitialized) */
48     IOTYPE_SERVER, /* server socket */
49     IOTYPE_CLIENT, /* client socket */
50     IOTYPE_STDIN, /* stdin */
51     IOTYPE_TIMER /* timer */
52 };
53
54 enum IOStatus { 
55     IO_CLOSED, /* descriptor is dead (socket waiting for removal or timer) */
56     IO_LISTENING, /* descriptor is waiting for connections (server socket) */
57     IO_CONNECTING, /* descriptor is waiting for connection approval (connecting client socket) */
58     IO_CONNECTED, /* descriptor is connected (connected client socket) */
59     IO_SSLWAIT /* waiting for SSL backend (e.g. handshake) */
60 };
61
62 enum IOEventType {
63     IOEVENT_IGNORE,
64     IOEVENT_READABLE, /* socket is readable - not read anything yet, could also be disconnect notification */
65     IOEVENT_RECV, /* client socket received something (recv_str valid) */
66     IOEVENT_CONNECTED, /* client socket connected successful */
67     IOEVENT_NOTCONNECTED, /* client socket could not connect (errid valid) */
68     IOEVENT_CLOSED, /* client socket lost connection (errid valid) */
69     IOEVENT_ACCEPT, /* server socket accepted new connection (accept_fd valid) */
70     IOEVENT_TIMEOUT, /* timer timed out */
71     IOEVENT_SSLFAILED /* failed to initialize SSL session */
72 };
73
74 struct IOBuffer {
75     char *buffer;
76     size_t bufpos, buflen;
77 };
78
79 struct IODescriptor {
80     int fd;
81     enum IOType type;
82     enum IOStatus state;
83     struct timeval timeout;
84     iohandler_callback *callback;
85     struct IOBuffer readbuf;
86     struct IOBuffer writebuf;
87     void *data;
88     int read_lines : 1;
89     int ssl : 1;
90     int ssl_active : 1;
91     int ssl_hs_read : 1;
92     int ssl_hs_write : 1;
93     struct IOSSLNode *sslnode;
94     
95     struct IODescriptor *next, *prev;
96 };
97
98 struct IOEvent {
99     enum IOEventType type;
100     struct IODescriptor *iofd;
101     union {
102         char *recv_str;
103         int accept_fd;
104         int errid;
105     } data;
106 };
107
108 struct IODescriptor *iohandler_add(int sockfd, enum IOType type, struct timeval *timeout, iohandler_callback *callback);
109 struct IODescriptor *iohandler_timer(struct timeval timeout, iohandler_callback *callback);
110 struct IODescriptor *iohandler_connect(const char *hostname, unsigned int port, int ssl, const char *bind, iohandler_callback *callback);
111 struct IODescriptor *iohandler_listen(const char *hostname, unsigned int port, iohandler_callback *callback);
112 void iohandler_write(struct IODescriptor *iofd, const char *line);
113 void iohandler_send(struct IODescriptor *iofd, const char *data, size_t datalen);
114 void iohandler_printf(struct IODescriptor *iofd, const char *text, ...);
115 void iohandler_close(struct IODescriptor *iofd);
116 void iohandler_update(struct IODescriptor *iofd);
117 void iohandler_set_timeout(struct IODescriptor *iofd, struct timeval *timeout);
118
119 void iohandler_poll();
120 void iohandler_poll_timeout(struct timeval timeout);
121
122 #endif