1 /* IOHandler.c - IOMultiplexer
2 * Copyright (C) 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/>.
17 #include "IOHandler.h"
19 #include "IOHandler_SSL.h"
24 #define _WIN32_WINNT 0x501
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <netinet/tcp.h>
40 #define EWOULDBLOCK EAGAIN
44 iohandler_log_callback *iolog_backend = NULL;
46 struct IODescriptor *first_descriptor = NULL;
47 struct IODescriptor *timer_priority = NULL;
50 static pthread_mutex_t io_thread_sync;
51 static pthread_mutex_t io_poll_sync;
54 void iohandler_log(enum IOLogType type, char *text, ...) {
56 char logBuf[MAXLOG+1];
59 va_start(arg_list, text);
60 pos = vsnprintf(logBuf, MAXLOG - 1, text, arg_list);
62 if (pos < 0 || pos > (MAXLOG - 1)) pos = MAXLOG - 1;
67 iolog_backend(type, logBuf);
71 extern struct IOEngine engine_select; /* select system call (should always be useable) */
72 extern struct IOEngine engine_kevent;
73 extern struct IOEngine engine_epoll;
74 extern struct IOEngine engine_win32;
76 int iohandler_settings = 0;
77 struct IOEngine *engine = NULL;
79 void iohandler_set(int setting, int value) {
81 iohandler_settings |= setting;
83 iohandler_settings &= ~setting;
86 static void iohandler_init_engine() {
88 IOTHREAD_MUTEX_INIT(io_thread_sync);
89 IOTHREAD_MUTEX_INIT(io_poll_sync);
92 if(!(iohandler_settings & IOHANDLER_SETTING_HIGH_PRECISION_TIMER)) {
93 if(!engine && engine_kevent.init && engine_kevent.init())
94 engine = &engine_kevent;
95 if(!engine && engine_epoll.init && engine_epoll.init())
96 engine = &engine_epoll;
97 if(!engine && engine_win32.init && engine_win32.init())
98 engine = &engine_win32;
102 if(engine_select.init())
103 engine = &engine_select;
105 iohandler_log(IOLOG_FATAL, "found no useable IO engine");
109 iohandler_log(IOLOG_DEBUG, "using %s IO engine", engine->name);
110 iohandler_ssl_init();
113 static void iohandler_append(struct IODescriptor *descriptor) {
114 IOSYNCHRONIZE(io_thread_sync);
115 struct timeval *timeout = ((descriptor->timeout.tv_sec || descriptor->timeout.tv_usec) ? &descriptor->timeout : NULL);
117 struct IODescriptor *iofd;
118 int set_priority = 1;
119 descriptor->timeout = *timeout;
121 iofd = timer_priority;
123 iofd = first_descriptor;
125 for(;;iofd = iofd->next) {
126 if(timeval_is_smaler(timeout, (&iofd->timeout))) {
127 descriptor->prev = iofd->prev;
128 descriptor->next = iofd;
130 iofd->prev->next = descriptor;
131 iofd->prev = descriptor;
133 timer_priority = descriptor;
136 if(iofd == timer_priority)
138 if(iofd->next == NULL) {
139 descriptor->next = NULL;
140 descriptor->prev = iofd;
141 iofd->next = descriptor;
143 timer_priority = descriptor;
148 descriptor->prev = NULL;
149 descriptor->next = NULL;
150 first_descriptor = descriptor;
151 timer_priority = descriptor;
155 descriptor->prev = NULL;
156 descriptor->next = first_descriptor;
158 first_descriptor->prev = descriptor;
159 first_descriptor = descriptor;
161 IODESYNCHRONIZE(io_thread_sync);
164 static void iohandler_remove(struct IODescriptor *descriptor, int engine_remove) {
165 //remove IODescriptor from the list
166 IOSYNCHRONIZE(io_thread_sync);
168 descriptor->prev->next = descriptor->next;
170 first_descriptor = descriptor->next;
172 descriptor->next->prev = descriptor->prev;
173 if(descriptor == timer_priority)
174 timer_priority = descriptor->next;
177 engine->remove(descriptor);
178 if(descriptor->readbuf.buffer)
179 free(descriptor->readbuf.buffer);
180 if(descriptor->writebuf.buffer)
181 free(descriptor->writebuf.buffer);
182 iohandler_log(IOLOG_DEBUG, "removed IODescriptor (%d) of type `%s`", descriptor->fd, iohandler_iotype_name(descriptor->type));
184 IODESYNCHRONIZE(io_thread_sync);
187 struct IODescriptor *iohandler_add(int sockfd, enum IOType type, struct timeval *timeout, iohandler_callback *callback) {
188 //just add a new IODescriptor
189 struct IODescriptor *descriptor = calloc(1, sizeof(*descriptor));
191 iohandler_log(IOLOG_ERROR, "could not allocate memory for IODescriptor in %s:%d", __FILE__, __LINE__);
194 descriptor->fd = (type == IOTYPE_STDIN ? fileno(stdin) : sockfd);
195 descriptor->type = type;
196 descriptor->state = (type == IOTYPE_STDIN ? IO_CONNECTED : IO_CLOSED);
197 descriptor->callback = callback;
199 descriptor->timeout = *timeout;
200 if(descriptor->timeout.tv_usec > 1000000) {
201 descriptor->timeout.tv_usec -= 1000000;
202 descriptor->timeout.tv_sec++;
205 if(type != IOTYPE_TIMER) {
206 descriptor->readbuf.buffer = malloc(IO_READ_BUFLEN + 2);
207 descriptor->readbuf.bufpos = 0;
208 descriptor->readbuf.buflen = IO_READ_BUFLEN;
209 descriptor->writebuf.buffer = malloc(IO_READ_BUFLEN + 2);
210 descriptor->writebuf.bufpos = 0;
211 descriptor->writebuf.buflen = IO_READ_BUFLEN;
215 iohandler_init_engine();
220 engine->add(descriptor);
222 //add IODescriptor to the list
223 iohandler_append(descriptor);
225 iohandler_log(IOLOG_DEBUG, "added custom socket descriptor (%d) as type `%s`", sockfd, iohandler_iotype_name(type));
229 void iohandler_set_timeout(struct IODescriptor *descriptor, struct timeval *timeout) {
231 descriptor->prev->next = descriptor->next;
233 first_descriptor = descriptor->next;
235 descriptor->next->prev = descriptor->prev;
236 if(descriptor == timer_priority)
237 timer_priority = descriptor->next;
239 descriptor->timeout = *timeout;
240 if(descriptor->timeout.tv_usec > 1000000) {
241 descriptor->timeout.tv_usec -= 1000000;
242 descriptor->timeout.tv_sec++;
245 descriptor->timeout.tv_sec = 0;
246 descriptor->timeout.tv_usec = 0;
248 iohandler_append(descriptor);
251 static void iohandler_increase_iobuf(struct IOBuffer *iobuf, size_t required) {
252 if(iobuf->buflen >= required) return;
253 char *new_buf = realloc(iobuf->buffer, required + 2);
255 iobuf->buffer = new_buf;
256 iobuf->buflen = required;
260 struct IODescriptor *iohandler_timer(struct timeval timeout, iohandler_callback *callback) {
261 struct IODescriptor *descriptor;
262 descriptor = iohandler_add(-1, IOTYPE_TIMER, &timeout, callback);
264 iohandler_log(IOLOG_ERROR, "could not allocate memory for IODescriptor in %s:%d", __FILE__, __LINE__);
267 iohandler_log(IOLOG_DEBUG, "added timer descriptor (sec: %d; usec: %d)", timeout.tv_sec, timeout.tv_usec);
271 struct IODescriptor *iohandler_connect(const char *hostname, unsigned int port, int ssl, const char *bindhost, iohandler_callback *callback) {
272 return iohandler_connect_flags(hostname, port, ssl, bindhost, callback, IOHANDLER_CONNECT_IPV4 | IOHANDLER_CONNECT_IPV6);
275 struct IODescriptor *iohandler_connect_flags(const char *hostname, unsigned int port, int ssl, const char *bindhost, iohandler_callback *callback, int flags) {
276 //non-blocking connect
278 struct addrinfo hints, *res;
279 struct sockaddr_in *ip4 = NULL;
280 struct sockaddr_in6 *ip6 = NULL;
282 struct sockaddr *dstaddr = NULL;
283 struct IODescriptor *descriptor;
286 iohandler_init_engine();
287 if(!engine) return NULL;
289 memset (&hints, 0, sizeof (hints));
290 hints.ai_family = PF_UNSPEC;
291 hints.ai_socktype = SOCK_STREAM;
292 hints.ai_flags |= AI_CANONNAME;
293 if ((result = getaddrinfo (hostname, NULL, &hints, &res))) {
294 iohandler_log(IOLOG_ERROR, "could not resolve %s to an IP address (%d)", hostname, result);
298 switch (res->ai_family) {
300 ip4 = (struct sockaddr_in *) res->ai_addr;
303 ip6 = (struct sockaddr_in6 *) res->ai_addr;
310 if(ip6 && (flags & IOHANDLER_CONNECT_IPV6)) {
311 sockfd = socket(AF_INET6, SOCK_STREAM, 0);
313 iohandler_log(IOLOG_ERROR, "could not create socket in %s:%d", __FILE__, __LINE__);
317 ip6->sin6_family = AF_INET6;
318 ip6->sin6_port = htons(port);
320 struct sockaddr_in6 *ip6vhost = NULL;
321 if (bindhost && !getaddrinfo(bindhost, NULL, &hints, &res)) {
323 switch (res->ai_family) {
325 ip6vhost = (struct sockaddr_in6 *) res->ai_addr;
333 ip6vhost->sin6_family = AF_INET6;
334 ip6vhost->sin6_port = htons(0);
335 bind(sockfd, (struct sockaddr*)ip6vhost, sizeof(*ip6vhost));
337 dstaddr = (struct sockaddr*)ip6;
338 dstaddrlen = sizeof(*ip6);
339 } else if(ip4 && (flags & IOHANDLER_CONNECT_IPV4)) {
340 sockfd = socket(AF_INET, SOCK_STREAM, 0);
342 iohandler_log(IOLOG_ERROR, "could not create socket in %s:%d", __FILE__, __LINE__);
346 ip4->sin_family = AF_INET;
347 ip4->sin_port = htons(port);
349 struct sockaddr_in *ip4vhost = NULL;
350 if (bindhost && !getaddrinfo(bindhost, NULL, &hints, &res)) {
352 switch (res->ai_family) {
354 ip4vhost = (struct sockaddr_in *) res->ai_addr;
362 ip4vhost->sin_family = AF_INET;
363 ip4vhost->sin_port = htons(0);
364 bind(sockfd, (struct sockaddr*)ip4vhost, sizeof(*ip4vhost));
366 dstaddr = (struct sockaddr*)ip4;
367 dstaddrlen = sizeof(*ip4);
372 #if defined(SO_NOSIGPIPE)
375 setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, (void *)&set, sizeof(int));
378 signal(SIGPIPE, SIG_IGN);
381 //make sockfd unblocking
385 flags = fcntl(sockfd, F_GETFL);
386 fcntl(sockfd, F_SETFL, flags|O_NONBLOCK);
387 flags = fcntl(sockfd, F_GETFD);
388 fcntl(sockfd, F_SETFD, flags|FD_CLOEXEC);
391 /* I hope you're using the Win32 backend or something else that
392 * automatically marks the file descriptor non-blocking...
395 descriptor = iohandler_add(sockfd, IOTYPE_CLIENT, NULL, callback);
400 connect(sockfd, dstaddr, dstaddrlen); //returns EINPROGRESS here (nonblocking)
401 descriptor->state = IO_CONNECTING;
402 descriptor->ssl = (ssl ? 1 : 0);
403 descriptor->read_lines = 1;
404 engine->update(descriptor);
405 iohandler_log(IOLOG_DEBUG, "added client socket (%d) connecting to %s:%d", sockfd, hostname, port);
409 struct IODescriptor *iohandler_listen(const char *hostname, unsigned int port, iohandler_callback *callback) {
410 return iohandler_listen_flags(hostname, port, callback, IOHANDLER_LISTEN_IPV4 | IOHANDLER_LISTEN_IPV6);
413 struct IODescriptor *iohandler_listen_flags(const char *hostname, unsigned int port, iohandler_callback *callback, int flags) {
415 struct addrinfo hints, *res;
416 struct sockaddr_in *ip4 = NULL;
417 struct sockaddr_in6 *ip6 = NULL;
418 struct IODescriptor *descriptor;
422 iohandler_init_engine();
423 if(!engine) return NULL;
425 memset (&hints, 0, sizeof (hints));
426 hints.ai_family = PF_UNSPEC;
427 hints.ai_socktype = SOCK_STREAM;
428 hints.ai_flags |= AI_CANONNAME;
429 if (getaddrinfo (hostname, NULL, &hints, &res)) {
433 switch (res->ai_family) {
435 ip4 = (struct sockaddr_in *) res->ai_addr;
438 ip6 = (struct sockaddr_in6 *) res->ai_addr;
445 if(ip6 && (flags & IOHANDLER_LISTEN_IPV6)) {
446 sockfd = socket(AF_INET6, SOCK_STREAM, 0);
447 if(sockfd == -1) return NULL;
450 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof(opt));
452 ip6->sin6_family = AF_INET6;
453 ip6->sin6_port = htons(port);
455 bind(sockfd, (struct sockaddr*)ip6, sizeof(*ip6));
456 } else if(ip4 && (flags && IOHANDLER_LISTEN_IPV4)) {
457 sockfd = socket(AF_INET, SOCK_STREAM, 0);
458 if(sockfd == -1) return NULL;
461 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof(opt));
463 ip4->sin_family = AF_INET;
464 ip4->sin_port = htons(port);
466 bind(sockfd, (struct sockaddr*)ip4, sizeof(*ip4));
471 #if defined(SO_NOSIGPIPE)
474 setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, (void *)&set, sizeof(int));
477 signal(SIGPIPE, SIG_IGN);
480 //make sockfd unblocking
484 flag = fcntl(sockfd, F_GETFL);
485 fcntl(sockfd, F_SETFL, flag|O_NONBLOCK);
486 flag = fcntl(sockfd, F_GETFD);
487 fcntl(sockfd, F_SETFD, flag|FD_CLOEXEC);
490 /* I hope you're using the Win32 backend or something else that
491 * automatically marks the file descriptor non-blocking...
494 descriptor = iohandler_add(sockfd, IOTYPE_SERVER, NULL, callback);
500 descriptor->state = IO_LISTENING;
501 engine->update(descriptor);
502 iohandler_log(IOLOG_DEBUG, "added server socket (%d) listening on %s:%d", sockfd, hostname, port);
506 struct IODescriptor *iohandler_listen_ssl(const char *hostname, unsigned int port, const char *certfile, const char *keyfile, iohandler_callback *callback) {
507 return iohandler_listen_ssl_flags(hostname, port, certfile, keyfile, callback, IOHANDLER_LISTEN_IPV4 | IOHANDLER_LISTEN_IPV6);
510 struct IODescriptor *iohandler_listen_ssl_flags(const char *hostname, unsigned int port, const char *certfile, const char *keyfile, iohandler_callback *callback, int flags) {
511 struct IODescriptor *descriptor = iohandler_listen_flags(hostname, port, callback, flags);
515 iohandler_ssl_listen(descriptor, certfile, keyfile);
516 if(descriptor->sslnode)
521 void iohandler_write(struct IODescriptor *iofd, const char *line) {
522 size_t linelen = strlen(line);
523 iohandler_send(iofd, line, linelen);
526 void iohandler_send(struct IODescriptor *iofd, const char *data, size_t datalen) {
527 if(iofd->type == IOTYPE_TIMER || iofd->state == IO_CLOSED) {
528 iohandler_log(IOLOG_ERROR, "could not write to socket (%s)", (iofd->type == IOTYPE_TIMER ? "IOTYPE_TIMER" : "IO_CLOSED"));
531 iohandler_log(IOLOG_DEBUG, "add %d to writebuf (fd: %d): %s", datalen, iofd->fd, data);
532 if(iofd->writebuf.buflen < iofd->writebuf.bufpos + datalen) {
533 iohandler_log(IOLOG_DEBUG, "increase writebuf (curr: %d) to %d (+%d bytes)", iofd->writebuf.buflen, iofd->writebuf.bufpos + datalen, (iofd->writebuf.bufpos + datalen - iofd->writebuf.buflen));
534 iohandler_increase_iobuf(&iofd->writebuf, iofd->writebuf.bufpos + datalen);
535 if(iofd->writebuf.buflen < iofd->writebuf.bufpos + datalen) {
536 iohandler_log(IOLOG_ERROR, "increase writebuf (curr: %d) to %d (+%d bytes) FAILED", iofd->writebuf.buflen, iofd->writebuf.bufpos + datalen, (iofd->writebuf.bufpos + datalen - iofd->writebuf.buflen));
540 memcpy(iofd->writebuf.buffer + iofd->writebuf.bufpos, data, datalen);
541 iofd->writebuf.bufpos += datalen;
542 engine->update(iofd);
545 void iohandler_printf(struct IODescriptor *iofd, const char *text, ...) {
547 char sendBuf[IO_LINE_LEN];
550 va_start(arg_list, text);
551 pos = vsnprintf(sendBuf, IO_LINE_LEN - 2, text, arg_list);
553 if (pos < 0 || pos > (IO_LINE_LEN - 2)) pos = IO_LINE_LEN - 2;
555 sendBuf[pos+1] = '\0';
556 iohandler_send(iofd, sendBuf, pos+1);
559 static int iohandler_try_write(struct IODescriptor *iofd) {
560 if(!iofd->writebuf.bufpos) return 0;
561 iohandler_log(IOLOG_DEBUG, "write writebuf (%d bytes) to socket (fd: %d)", iofd->writebuf.bufpos, iofd->fd);
564 res = iohandler_ssl_write(iofd, iofd->writebuf.buffer, iofd->writebuf.bufpos);
566 res = send(iofd->fd, iofd->writebuf.buffer, iofd->writebuf.bufpos, 0);
568 if (errno != EAGAIN && errno != EWOULDBLOCK)
569 iohandler_log(IOLOG_ERROR, "could not write to socket (fd: %d): %d - %s", iofd->fd, errno, strerror(errno));
573 iofd->writebuf.bufpos -= res;
574 if(iofd->state != IO_CLOSED)
575 engine->update(iofd);
580 void iohandler_close(struct IODescriptor *iofd) {
581 int engine_remove = 1;
582 iofd->state = IO_CLOSED;
583 if(iofd->writebuf.bufpos) {
584 //try to send everything before closing
588 flags = fcntl(iofd->fd, F_GETFL);
589 fcntl(iofd->fd, F_SETFL, flags & ~O_NONBLOCK);
590 flags = fcntl(iofd->fd, F_GETFD);
591 fcntl(iofd->fd, F_SETFD, flags|FD_CLOEXEC);
595 engine->remove(iofd);
597 iohandler_try_write(iofd);
601 iohandler_ssl_disconnect(iofd);
602 if(iofd->type == IOTYPE_SERVER || iofd->type == IOTYPE_CLIENT || iofd->type == IOTYPE_STDIN)
604 iohandler_remove(iofd, engine_remove);
607 void iohandler_update(struct IODescriptor *iofd) {
608 iohandler_log(IOLOG_DEBUG, "external call to iohandler_update (fd: %d)", iofd->fd);
609 engine->update(iofd);
612 static void iohandler_trigger_event(struct IOEvent *event) {
613 if(!event->iofd->callback) return;
614 iohandler_log(IOLOG_DEBUG, "triggering event (%s) for %s (fd: %d)", iohandler_ioeventtype_name(event->type), iohandler_iotype_name(event->iofd->type), event->iofd->fd);
615 event->iofd->callback(event);
618 void iohandler_events(struct IODescriptor *iofd, int readable, int writeable) {
619 struct IOEvent callback_event;
620 callback_event.type = IOEVENT_IGNORE;
621 callback_event.iofd = iofd;
622 switch(iofd->state) {
624 if(!readable && !writeable) {
625 if(!iofd->ssl_server_hs) {
626 callback_event.type = IOEVENT_SSLFAILED;
627 iofd->state = IO_CLOSED;
628 engine->update(iofd);
630 iohandler_close(iofd);
631 } else if(iofd->ssl_server_hs) {
632 iohandler_log(IOLOG_DEBUG, "triggering iohandler_ssl_server_handshake for %s (fd: %d)", iohandler_iotype_name(iofd->type), iofd->fd);
633 iohandler_ssl_server_handshake(iofd);
635 iohandler_log(IOLOG_DEBUG, "triggering iohandler_ssl_client_handshake for %s (fd: %d)", iohandler_iotype_name(iofd->type), iofd->fd);
636 iohandler_ssl_client_handshake(iofd);
640 if(iofd->type == IOTYPE_TIMER)
641 callback_event.type = IOEVENT_TIMEOUT;
645 callback_event.data.accept_fd = accept(iofd->fd, NULL, 0);
646 if(callback_event.data.accept_fd < 0) {
647 iohandler_log(IOLOG_ERROR, "could not accept client (server fd: %d): %d - %s", iofd->fd, errno, strerror(errno));
648 } else if(iofd->ssl) {
649 struct IODescriptor *client_iofd = iohandler_add(callback_event.data.accept_fd, IOTYPE_CLIENT, NULL, NULL);
650 iohandler_ssl_client_accepted(iofd, client_iofd);
652 callback_event.type = IOEVENT_ACCEPT;
656 if(readable) { //could not connect
657 callback_event.type = IOEVENT_NOTCONNECTED;
659 //arglen = sizeof(callback_event.data.errid);
660 //if (getsockopt(iofd->fd, SOL_SOCKET, SO_ERROR, &callback_event.data.errid, &arglen) < 0)
661 // callback_event.data.errid = errno;
662 iofd->state = IO_CLOSED;
663 engine->update(iofd);
664 } else if(writeable) {
665 if(iofd->ssl && !iofd->ssl_active) {
666 iohandler_log(IOLOG_DEBUG, "triggering iohandler_ssl_connect for %s (fd: %d)", iohandler_iotype_name(iofd->type), iofd->fd);
667 iohandler_ssl_connect(iofd);
670 if(iofd->ssl && iofd->ssl_server_hs) {
671 callback_event.type = IOEVENT_SSLACCEPT;
672 callback_event.iofd = iofd->data;
673 callback_event.data.accept_iofd = iofd;
677 callback_event.type = IOEVENT_CONNECTED;
678 iofd->state = IO_CONNECTED;
679 engine->update(iofd);
684 if(iofd->read_lines) {
688 bytes = iohandler_ssl_read(iofd, iofd->readbuf.buffer + iofd->readbuf.bufpos, iofd->readbuf.buflen - iofd->readbuf.bufpos);
690 if(iofd->type == IOTYPE_STDIN)
694 bytes = read(iofd->fd, iofd->readbuf.buffer + iofd->readbuf.bufpos, iofd->readbuf.buflen - iofd->readbuf.bufpos);
697 bytes = recv(iofd->fd, iofd->readbuf.buffer + iofd->readbuf.bufpos, iofd->readbuf.buflen - iofd->readbuf.bufpos, 0);
700 if (errno != EAGAIN || errno != EWOULDBLOCK) {
701 iofd->state = IO_CLOSED;
702 engine->update(iofd);
703 callback_event.type = IOEVENT_CLOSED;
704 callback_event.data.errid = errno;
707 int i, used_bytes = 0;
708 iohandler_log(IOLOG_DEBUG, "received %d bytes (fd: %d). readbuf position: %d", bytes, iofd->fd, iofd->readbuf.bufpos);
709 iofd->readbuf.bufpos += bytes;
710 callback_event.type = IOEVENT_RECV;
711 for(i = 0; i < iofd->readbuf.bufpos; i++) {
712 if(iofd->readbuf.buffer[i] == '\r' && iofd->readbuf.buffer[i+1] == '\n')
713 iofd->readbuf.buffer[i] = 0;
714 else if(iofd->readbuf.buffer[i] == '\n' || iofd->readbuf.buffer[i] == '\r') {
715 iofd->readbuf.buffer[i] = 0;
716 callback_event.data.recv_str = iofd->readbuf.buffer + used_bytes;
717 iohandler_log(IOLOG_DEBUG, "parsed line (%d bytes): %s", i - used_bytes, iofd->readbuf.buffer + used_bytes);
719 iohandler_trigger_event(&callback_event);
720 } else if(i + 1 - used_bytes >= IO_LINE_LEN) { //512 max
721 iofd->readbuf.buffer[i] = 0;
722 callback_event.data.recv_str = iofd->readbuf.buffer + used_bytes;
723 iohandler_log(IOLOG_DEBUG, "parsed and stripped line (%d bytes): %s", i - used_bytes, iofd->readbuf.buffer + used_bytes);
724 for(; i < iofd->readbuf.bufpos; i++) { //skip the rest of the line
725 if(iofd->readbuf.buffer[i] == '\n' || (iofd->readbuf.buffer[i] == '\r' && iofd->readbuf.buffer[i+1] != '\n')) {
730 iohandler_trigger_event(&callback_event);
734 if(used_bytes == iofd->readbuf.bufpos) {
735 iofd->readbuf.bufpos = 0;
736 iohandler_log(IOLOG_DEBUG, "readbuf fully processed (set buffer position to 0)");
738 iohandler_log(IOLOG_DEBUG, "readbuf rest: %d bytes (used %d bytes)", iofd->readbuf.bufpos - used_bytes, used_bytes);
739 memmove(iofd->readbuf.buffer, iofd->readbuf.buffer + used_bytes, iofd->readbuf.bufpos - used_bytes);
740 iofd->readbuf.bufpos -= used_bytes;
743 callback_event.type = IOEVENT_IGNORE;
746 callback_event.type = IOEVENT_READABLE;
750 bytes = iohandler_try_write(iofd);
752 iofd->state = IO_CLOSED;
753 engine->update(iofd);
754 callback_event.type = IOEVENT_CLOSED;
755 callback_event.data.errid = errno;
760 if(callback_event.type == IOEVENT_IGNORE && !readable && !writeable)
761 callback_event.type = IOEVENT_TIMEOUT;
762 if(callback_event.type != IOEVENT_IGNORE)
763 iohandler_trigger_event(&callback_event);
766 void iohandler_poll() {
767 struct timeval timeout;
768 timeout.tv_sec = IO_MAX_TIMEOUT;
770 iohandler_poll_timeout(timeout);
773 void iohandler_poll_timeout(struct timeval timeout) {
775 IOSYNCHRONIZE(io_poll_sync); //quite senceless multithread support... better support will follow
776 engine->loop(&timeout);
777 IODESYNCHRONIZE(io_poll_sync);
781 //debugging functions
782 char *iohandler_iotype_name(enum IOType type) {
785 return "IOTYPE_UNKNOWN";
787 return "IOTYPE_SERVER";
789 return "IOTYPE_CLIENT";
791 return "IOTYPE_STDIN";
793 return "IOTYPE_TIMER";
795 return "(UNDEFINED)";
799 char *iohandler_iostatus_name(enum IOStatus status) {
804 return "IO_LISTENING";
806 return "IO_CONNECTING";
808 return "IO_CONNECTED";
812 return "(UNDEFINED)";
816 char *iohandler_ioeventtype_name(enum IOEventType type) {
819 return "IOEVENT_IGNORE";
820 case IOEVENT_READABLE:
821 return "IOEVENT_READABLE";
823 return "IOEVENT_RECV";
824 case IOEVENT_CONNECTED:
825 return "IOEVENT_CONNECTED";
826 case IOEVENT_NOTCONNECTED:
827 return "IOEVENT_NOTCONNECTED";
829 return "IOEVENT_CLOSED";
831 return "IOEVENT_ACCEPT";
832 case IOEVENT_SSLACCEPT:
833 return "IOEVENT_SSLACCEPT";
834 case IOEVENT_TIMEOUT:
835 return "IOEVENT_TIMEOUT";
837 return "(UNDEFINED)";