X-Git-Url: http://git.pk910.de/?p=NeonServV5.git;a=blobdiff_plain;f=src%2FIOHandler.c;h=e710ffe88022a21c233ead04551e9ba6fa168f6a;hp=07cd794e9be73afb33dba2eccb065369f4d5e5e1;hb=57d2d5a19c67112e2e416f534a6ac32618c8d0de;hpb=6b235c4b3e0ff537bff312c5f18104e657b02883 diff --git a/src/IOHandler.c b/src/IOHandler.c index 07cd794..e710ffe 100644 --- a/src/IOHandler.c +++ b/src/IOHandler.c @@ -34,6 +34,7 @@ #include #include #include +#include #endif #ifndef EWOULDBLOCK @@ -235,7 +236,14 @@ void iohandler_set_timeout(struct IODescriptor *descriptor, struct timeval *time 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; @@ -268,6 +276,26 @@ struct IODescriptor *iohandler_timer(struct timeval timeout, iohandler_callback 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); } @@ -381,11 +409,11 @@ struct IODescriptor *iohandler_connect_flags(const char *hostname, unsigned int //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