#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
+#include <fcntl.h>
#endif
#ifndef EWOULDBLOCK
descriptor->next->prev = descriptor->prev;
if(descriptor == timer_priority)
timer_priority = descriptor->next;
- if(timeout) {
+ if(timeout && timeout->tv_sec == 0 && descriptor->constant_timeout) {
+ descriptor->timeout.tv_usec += (descriptor->constant_timeout % 1000) * 1000;
+ descriptor->timeout.tv_sec += (descriptor->constant_timeout / 1000);
+ if(descriptor->timeout.tv_usec > 1000000) {
+ descriptor->timeout.tv_sec += (descriptor->timeout.tv_usec / 1000000);
+ descriptor->timeout.tv_usec %= 1000000;
+ }
+ } else if(timeout) {
descriptor->timeout = *timeout;
if(descriptor->timeout.tv_usec > 1000000) {
descriptor->timeout.tv_usec -= 1000000;
return descriptor;
}
+struct IODescriptor *iohandler_constant_timer(int msec, iohandler_callback *callback) {
+ struct IODescriptor *descriptor;
+ struct timeval timeout;
+ gettimeofday(&timeout, NULL);
+ timeout.tv_usec += (msec % 1000) * 1000;
+ timeout.tv_sec += (msec / 1000);
+ if(timeout.tv_usec > 1000000) {
+ timeout.tv_sec += (timeout.tv_usec / 1000000);
+ timeout.tv_usec %= 1000000;
+ }
+ descriptor = iohandler_add(-1, IOTYPE_TIMER, &timeout, callback);
+ if(!descriptor) {
+ iohandler_log(IOLOG_ERROR, "could not allocate memory for IODescriptor in %s:%d", __FILE__, __LINE__);
+ return NULL;
+ }
+ descriptor->constant_timeout = msec;
+ iohandler_log(IOLOG_DEBUG, "added timer descriptor (sec: %d; usec: %d)", timeout.tv_sec, timeout.tv_usec);
+ return descriptor;
+}
+
struct IODescriptor *iohandler_connect(const char *hostname, unsigned int port, int ssl, const char *bindhost, iohandler_callback *callback) {
return iohandler_connect_flags(hostname, port, ssl, bindhost, callback, IOHANDLER_CONNECT_IPV4 | IOHANDLER_CONNECT_IPV6);
}
//make sockfd unblocking
#if defined(F_GETFL)
{
- int flags;
- flags = fcntl(sockfd, F_GETFL);
- fcntl(sockfd, F_SETFL, flags|O_NONBLOCK);
- flags = fcntl(sockfd, F_GETFD);
- fcntl(sockfd, F_SETFD, flags|FD_CLOEXEC);
+ int fcntl_flags;
+ fcntl_flags = fcntl(sockfd, F_GETFL);
+ fcntl(sockfd, F_SETFL, fcntl_flags|O_NONBLOCK);
+ fcntl_flags = fcntl(sockfd, F_GETFD);
+ fcntl(sockfd, F_SETFD, fcntl_flags|FD_CLOEXEC);
}
#else
/* I hope you're using the Win32 backend or something else that
ip6->sin6_port = htons(port);
bind(sockfd, (struct sockaddr*)ip6, sizeof(*ip6));
- } else if(ip4 && (flags && IOHANDLER_LISTEN_IPV4)) {
+ } else if(ip4 && (flags & IOHANDLER_LISTEN_IPV4)) {
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd == -1) return NULL;