Index: configure.ac ================================================================== --- configure.ac +++ configure.ac @@ -273,10 +273,15 @@ AC_MSG_RESULT($atomic_ops) AC_CHECK_LIB(socket, socket, LIBS="$LIBS -lsocket") AC_CHECK_LIB(ws2_32, main, LIBS="$LIBS -lws2_32") +dnl AC_CHECK_HEADER(poll.h, +dnl [AC_DEFINE(OF_HAVE_POLL, 1, [Whether poll is supported])]) +AC_CHECK_HEADERS(sys/select.h, + [AC_DEFINE(OF_HAVE_SYS_SELECT_H, 1, [Whether we have sys/select.h])]) + AC_MSG_CHECKING(for getaddrinfo) AC_TRY_COMPILE([ #include #ifndef _WIN32 #include Index: src/OFSocketObserver.h ================================================================== --- src/OFSocketObserver.h +++ src/OFSocketObserver.h @@ -6,16 +6,27 @@ * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE included in * the packaging of this file. */ + +#if !defined(OF_HAVE_POLL) && defined(OF_HAVE_SYS_SELECT_H) +# include +#endif #import "OFObject.h" + +#ifdef _WIN32 +# define _WIN32_WINNT 0x0501 +# include +#endif @class OFSocket; @class OFTCPSocket; +#ifdef OF_HAVE_POLL @class OFDataArray; +#endif @class OFMutableDictionary; /** * \brief A protocol that needs to be implemented by delegates for * OFSocketObserver. @@ -48,11 +59,17 @@ * \brief A class that can observe multiple sockets at once. */ @interface OFSocketObserver: OFObject { OFObject *delegate; +#ifdef OF_HAVE_POLL OFDataArray *fds; +#else + fd_set readfds; + fd_set writefds; + int nfds; +#endif OFMutableDictionary *fdToSocket; } #ifdef OF_HAVE_PROPERTIES @property (retain) OFObject *delegate; @@ -117,21 +134,20 @@ */ - (void)removeSocketToObserveForWriting: (OFSocket*)sock; /** * Observes all sockets and blocks until an event happens on a socket. - * - * \return The number of sockets that have pending events */ -- (int)observe; +- (void)observe; /** * Observes all sockets until an event happens on a socket or the timeout is * reached. * - * \return The number of sockets that have pending events + * \param timeout The time to wait for an event, in milliseconds + * \return A boolean whether events occurred during the timeinterval */ -- (int)observeWithTimeout: (int)timeout; +- (BOOL)observeWithTimeout: (int)timeout; @end @interface OFObject (OFSocketObserverDelegate) @end Index: src/OFSocketObserver.m ================================================================== --- src/OFSocketObserver.m +++ src/OFSocketObserver.m @@ -9,19 +9,22 @@ * the packaging of this file. */ #include "config.h" -#include +#ifdef OF_HAVE_POLL +# include +#endif #import "OFSocketObserver.h" #import "OFDataArray.h" #import "OFDictionary.h" #import "OFSocket.h" #import "OFTCPSocket.h" #import "OFNumber.h" #import "OFAutoreleasePool.h" +#import "OFExceptions.h" @implementation OFSocketObserver + socketObserver { return [[[self alloc] init] autorelease]; @@ -29,20 +32,27 @@ - init { self = [super init]; +#ifdef OF_HAVE_POLL fds = [[OFDataArray alloc] initWithItemSize: sizeof(struct pollfd)]; +#else + FD_ZERO(&readfds); + FD_ZERO(&writefds); +#endif fdToSocket = [[OFMutableDictionary alloc] init]; return self; } - (void)dealloc { [delegate release]; +#ifdef OF_HAVE_POLL [fds release]; +#endif [fdToSocket release]; [super dealloc]; } @@ -56,10 +66,11 @@ [delegate_ retain]; [delegate release]; delegate = delegate_; } +#ifdef OF_HAVE_POLL - (void)_addSocket: (OFSocket*)sock withEvents: (short)events { struct pollfd *fds_c = [fds cArray]; size_t i, count = [fds count]; @@ -105,61 +116,130 @@ [pool release]; } } } +#else +- (void)_addSocket: (OFSocket*)sock + withFDSet: (fd_set*)fdset +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + + if (sock->sock >= FD_SETSIZE) + @throw [OFOutOfRangeException newWithClass: isa]; + + FD_SET(sock->sock, fdset); + + if (sock->sock >= nfds) + nfds = sock->sock + 1; + + [fdToSocket setObject: sock + forKey: [OFNumber numberWithInt: sock->sock]]; + + [pool release]; +} + +- (void)_removeSocket: (OFSocket*)sock + withFDSet: (fd_set*)fdset +{ + if (sock->sock >= FD_SETSIZE) + @throw [OFOutOfRangeException newWithClass: isa]; + + FD_CLR(sock->sock, fdset); + + if (!FD_ISSET(sock->sock, &readfds) && + !FD_ISSET(sock->sock, &writefds)) { + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + + [fdToSocket removeObjectForKey: + [OFNumber numberWithInt: sock->sock]]; + + [pool release]; + } +} +#endif - (void)addSocketToObserveForIncomingConnections: (OFTCPSocket*)sock { +#ifdef OF_HAVE_POLL [self _addSocket: sock withEvents: POLLIN]; +#else + [self _addSocket: sock + withFDSet: &readfds]; +#endif } - (void)addSocketToObserveForReading: (OFSocket*)sock { +#ifdef OF_HAVE_POLL [self _addSocket: sock withEvents: POLLIN]; +#else + [self _addSocket: sock + withFDSet: &readfds]; +#endif } - (void)addSocketToObserveForWriting: (OFSocket*)sock { +#ifdef OF_HAVE_POLL [self _addSocket: sock withEvents: POLLOUT]; +#else + [self _addSocket: sock + withFDSet: &writefds]; +#endif } - (void)removeSocketToObserveForIncomingConnections: (OFTCPSocket*)sock { +#ifdef OF_HAVE_POLL [self _removeSocket: sock withEvents: POLLIN]; +#else + [self _removeSocket: sock + withFDSet: &readfds]; +#endif } - (void)removeSocketToObserveForReading: (OFSocket*)sock { +#ifdef OF_HAVE_POLL [self _removeSocket: sock withEvents: POLLIN]; +#else + [self _removeSocket: sock + withFDSet: &readfds]; +#endif } - (void)removeSocketToObserveForWriting: (OFSocket*)sock { - [self _removeSocket: sock - withEvents: POLLOUT]; -} - -- (int)observe -{ - return [self observeWithTimeout: -1]; -} - -- (int)observeWithTimeout: (int)timeout -{ - OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; - struct pollfd *fds_c = [fds cArray]; - size_t i, nfds = [fds count]; - int ret = poll(fds_c, nfds, timeout); - - if (ret <= 0) - return ret; +#ifdef OF_HAVE_POLL + [self _removeSocket: sock + withEvents: POLLOUT]; +#else + [self _removeSocket: sock + withFDSet: &writefds]; +#endif +} + +- (void)observe +{ + [self observeWithTimeout: -1]; +} + +- (BOOL)observeWithTimeout: (int)timeout +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; +#ifdef OF_HAVE_POLL + struct pollfd *fds_c = [fds cArray]; + size_t i, nfds = [fds count]; + + if (poll(fds_c, nfds, timeout) < 1) + return NO; for (i = 0; i < nfds; i++) { OFNumber *num; OFSocket *sock; @@ -180,14 +260,44 @@ [delegate socketDidBecomeReadyForReading: sock]; } fds_c[i].revents = 0; } +#else + fd_set readfds_; + fd_set writefds_; + fd_set exceptfds_; + struct timeval tv; + OFEnumerator *enumerator; + OFSocket *sock; + + readfds_ = readfds; + writefds_ = writefds; + FD_ZERO(&exceptfds_); + + if (select(nfds, &readfds_, &writefds_, &exceptfds_, + (timeout != -1 ? &tv : NULL)) < 1) + return NO; + + enumerator = [[[fdToSocket copy] autorelease] objectEnumerator]; + + while ((sock = [enumerator nextObject]) != nil) { + if (FD_ISSET(sock->sock, &readfds_)) { + if (sock->listening) + [delegate socketDidReceiveIncomingConnection: + (OFTCPSocket*)sock]; + else + [delegate socketDidBecomeReadyForReading: sock]; + } + if (FD_ISSET(sock->sock, &writefds_)) + [delegate socketDidBecomeReadyForWriting: sock]; + } +#endif [pool release]; - return ret; + return YES; } @end @implementation OFObject (OFSocketObserverDelegate) - (void)socketDidReceiveIncomingConnection: (OFTCPSocket*)sock Index: src/OFTCPSocket.m ================================================================== --- src/OFTCPSocket.m +++ src/OFTCPSocket.m @@ -36,10 +36,14 @@ # import "OFThread.h" # import "OFDataArray.h" static OFMutex *mutex = nil; #endif + +#ifdef _WIN32 +# define close(sock) closesocket(sock) +#endif @implementation OFTCPSocket #if defined(OF_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO) + (void)initialize { Index: src/objfw-defs.h.in ================================================================== --- src/objfw-defs.h.in +++ src/objfw-defs.h.in @@ -5,13 +5,15 @@ #undef OF_GNU_RUNTIME #undef OF_HAVE_ASPRINTF #undef OF_HAVE_FAST_ENUMERATION #undef OF_HAVE_GCC_ATOMIC_OPS #undef OF_HAVE_LIBKERN_OSATOMIC_H +#undef OF_HAVE_POLL #undef OF_HAVE_PROPERTIES #undef OF_HAVE_PTHREADS #undef OF_HAVE_PTHREAD_SPINLOCKS #undef OF_HAVE_SCHED_YIELD +#undef OF_HAVE_SYS_SELECT_H #undef OF_OBJFW_RUNTIME #undef OF_PLUGINS #undef OF_THREADS #undef SIZE_MAX