Index: src/OFStream.h ================================================================== --- src/OFStream.h +++ src/OFStream.h @@ -31,11 +31,16 @@ char *cache; @protected char *wBuffer; size_t cacheLen, wBufferLen; BOOL buffersWrites; + BOOL isBlocking; } + +#ifdef OF_HAVE_PROPERTIES +@property (assign, setter=setBlocking) BOOL isBlocking; +#endif /** * Returns a boolean whether the end of the stream has been reached. * * \return A boolean whether the end of the stream has been reached @@ -313,10 +318,25 @@ * \return The number of bytes written */ - (size_t)writeFormat: (OFString*)fmt withArguments: (va_list)args; +/** + * \return Whether the stream is in blocking mode + */ +- (BOOL)isBlocking; + +/** + * Enables or disables non-blocking I/O. + * + * By default, a stream is in blocking mode. + * On Win32, this currently only works for sockets! + * + * \param enable Whether the stream should be blocking + */ +- (void)setBlocking: (BOOL)enable; + /** * \return The file descriptor for the stream. */ - (int)fileDescriptor; Index: src/OFStream.m ================================================================== --- src/OFStream.m +++ src/OFStream.m @@ -16,10 +16,11 @@ #include #include #include #include +#include #import "OFStream.h" #import "OFString.h" #import "OFDataArray.h" #import "OFExceptions.h" @@ -39,10 +40,11 @@ self = [super init]; cache = NULL; wBuffer = NULL; + isBlocking = YES; return self; } - (BOOL)_isAtEndOfStream @@ -670,10 +672,38 @@ } /* Get rid of a warning, never reached anyway */ assert(0); } + +- (BOOL)isBlocking +{ + return isBlocking; +} + +- (void)setBlocking: (BOOL)enable +{ +#ifndef _WIN32 + int flags; + + isBlocking = enable; + + if ((flags = fcntl([self fileDescriptor], F_GETFL)) == -1) + @throw [OFSetOptionFailedException newWithClass: isa]; + + if (enable) + flags &= ~O_NONBLOCK; + else + flags |= O_NONBLOCK; + + if (fcntl([self fileDescriptor], F_SETFL, flags) == -1) + @throw [OFSetOptionFailedException newWithClass: isa]; +#else + @throw [OFNotImplementedException newWithClass: isa + selector: _cmd]; +#endif +} - (int)fileDescriptor { @throw [OFNotImplementedException newWithClass: isa selector: _cmd]; Index: src/OFStreamSocket.h ================================================================== --- src/OFStreamSocket.h +++ src/OFStreamSocket.h @@ -36,11 +36,6 @@ /** * \return A new autoreleased OFTCPSocket */ + socket; - -/** - * Enables/disables non-blocking I/O. - */ -- (void)setBlocking: (BOOL)enable; @end Index: src/OFStreamSocket.m ================================================================== --- src/OFStreamSocket.m +++ src/OFStreamSocket.m @@ -11,11 +11,10 @@ #include "config.h" #include #include -#include #include #ifndef _WIN32 # include # include @@ -100,32 +99,20 @@ /* This is safe, as we already checked for -1 */ return ret; } +#ifdef _WIN32 - (void)setBlocking: (BOOL)enable { -#ifndef _WIN32 - int flags; - - if ((flags = fcntl(sock, F_GETFL)) == -1) - @throw [OFSetOptionFailedException newWithClass: isa]; - - if (enable) - flags &= ~O_NONBLOCK; - else - flags |= O_NONBLOCK; - - if (fcntl(sock, F_SETFL, flags) == -1) - @throw [OFSetOptionFailedException newWithClass: isa]; -#else u_long v = enable; + isBlocking = enable; if (ioctlsocket(sock, FIONBIO, &v) == SOCKET_ERROR) @throw [OFSetOptionFailedException newWithClass: isa]; -#endif } +#endif - (int)fileDescriptor { return sock; }