1 /* IOHandler.h - TransparentIRC 0.1
2 * Copyright (C) 2011-2012 Philipp Kreil (pk910)
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.
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.
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/>.
24 #define IOHANDLER_CALLBACK(NAME) void NAME(UNUSED_ARG(struct IOEvent *io_event))
25 typedef IOHANDLER_CALLBACK(iohandler_callback);
28 IOTYPE_UNKNOWN, /* ignore descriptor (uninitialized) */
29 IOTYPE_SERVER, /* server socket */
30 IOTYPE_CLIENT, /* client socket */
31 IOTYPE_STDIN, /* stdin */
32 IOTYPE_TIMER /* timer */
36 IO_CLOSED, /* descriptor is dead (socket waiting for removal or timer) */
37 IO_LISTENING, /* descriptor is waiting for connections (server socket) */
38 IO_CONNECTING, /* descriptor is waiting for connection approval (connecting client socket) */
39 IO_CONNECTED /* descriptor is connected (connected client socket) */
44 IOEVENT_READABLE, /* socket is readable - not read anything yet, could also be disconnect notification */
45 IOEVENT_RECV, /* client socket received something (recv_str valid) */
46 IOEVENT_CONNECTED, /* client socket connected successful */
47 IOEVENT_NOTCONNECTED, /* client socket could not connect (errid valid) */
48 IOEVENT_CLOSED, /* client socket lost connection (errid valid) */
49 IOEVENT_ACCEPT, /* server socket accepted new connection (accept_fd valid) */
50 IOEVENT_TIMEOUT /* timer timed out */
55 size_t bufpos, buflen;
63 struct timeval timeout;
64 iohandler_callback *callback;
65 struct IOBuffer readbuf;
66 struct IOBuffer writebuf;
71 struct IODescriptor *next, *prev;
75 enum IOEventType type;
76 struct IODescriptor *iofd;
84 struct IODescriptor *iohandler_add(int sockfd, enum IOType type, iohandler_callback *callback);
85 struct IODescriptor *iohandler_file(FILE *file, iohandler_callback *callback);
86 struct IODescriptor *iohandler_timer(struct timeval timeout, iohandler_callback *callback);
87 struct IODescriptor *iohandler_connect(const char *hostname, unsigned int port, int ssl, const char *bind, iohandler_callback *callback);
88 struct IODescriptor *iohandler_listen(const char *hostname, unsigned int port, int ssl, iohandler_callback *callback);
89 void iohandler_write(struct IODescriptor *iofd, const char *line);
90 void iohandler_send(struct IODescriptor *iofd, const char *data, size_t datalen);
91 void iohandler_printf(struct IODescriptor *iofd, const char *text, ...);
92 void iohandler_close(struct IODescriptor *iofd);
93 void iohandler_update(struct IODescriptor *iofd);
95 void iohandler_poll();