@@ -31,105 +31,110 @@ #import "autorelease.h" #import "macros.h" static OFRunLoop *mainRunLoop = nil; -@interface OFRunLoop_ReadQueueItem: OFObject -{ -@public - void *buffer; - size_t length; - id target; - SEL selector; -#ifdef OF_HAVE_BLOCKS - of_stream_async_read_block_t block; -#endif -} -@end - -@interface OFRunLoop_ExactReadQueueItem: OFObject -{ -@public - void *buffer; - size_t exactLength, readLength; - id target; - SEL selector; -#ifdef OF_HAVE_BLOCKS - of_stream_async_read_block_t block; -#endif -} -@end - -@interface OFRunLoop_ReadLineQueueItem: OFObject -{ -@public - of_string_encoding_t encoding; - id target; - SEL selector; -#ifdef OF_HAVE_BLOCKS - of_stream_async_read_line_block_t block; -#endif -} -@end - -@interface OFRunLoop_AcceptQueueItem: OFObject -{ -@public - id target; - SEL selector; -#ifdef OF_HAVE_BLOCKS - of_tcpsocket_async_accept_block_t block; -#endif +@interface OFRunLoop_QueueItem: OFObject +{ +@public + id _target; + SEL _selector; +} +@end + +@interface OFRunLoop_ReadQueueItem: OFRunLoop_QueueItem +{ +@public +#ifdef OF_HAVE_BLOCKS + of_stream_async_read_block_t _block; +#endif + void *_buffer; + size_t _length; +} +@end + +@interface OFRunLoop_ExactReadQueueItem: OFRunLoop_QueueItem +{ +@public +#ifdef OF_HAVE_BLOCKS + of_stream_async_read_block_t _block; +#endif + void *_buffer; + size_t _exactLength, _readLength; +} +@end + +@interface OFRunLoop_ReadLineQueueItem: OFRunLoop_QueueItem +{ +@public +#ifdef OF_HAVE_BLOCKS + of_stream_async_read_line_block_t _block; +#endif + of_string_encoding_t _encoding; +} +@end + +@interface OFRunLoop_AcceptQueueItem: OFRunLoop_QueueItem +{ +@public +#ifdef OF_HAVE_BLOCKS + of_tcpsocket_async_accept_block_t _block; +#endif +} +@end + +@implementation OFRunLoop_QueueItem +- (void)dealloc +{ + [_target release]; + + [super dealloc]; } @end @implementation OFRunLoop_ReadQueueItem +#ifdef OF_HAVE_BLOCKS - (void)dealloc { - [target release]; -#ifdef OF_HAVE_BLOCKS - [block release]; -#endif + [_block release]; [super dealloc]; } +#endif @end @implementation OFRunLoop_ExactReadQueueItem +#ifdef OF_HAVE_BLOCKS - (void)dealloc { - [target release]; -#ifdef OF_HAVE_BLOCKS - [block release]; -#endif + [_block release]; [super dealloc]; } +#endif @end @implementation OFRunLoop_ReadLineQueueItem +#ifdef OF_HAVE_BLOCKS - (void)dealloc { - [target release]; -#ifdef OF_HAVE_BLOCKS - [block release]; -#endif + [_block release]; [super dealloc]; } +#endif @end @implementation OFRunLoop_AcceptQueueItem +#ifdef OF_HAVE_BLOCKS - (void)dealloc { - [target release]; -#ifdef OF_HAVE_BLOCKS - [block release]; -#endif + [_block release]; [super dealloc]; } +#endif @end @implementation OFRunLoop + (OFRunLoop*)mainRunLoop { @@ -151,21 +156,21 @@ } #define ADD(type, code) \ void *pool = objc_autoreleasePoolPush(); \ OFRunLoop *runLoop = [self currentRunLoop]; \ - OFList *queue = [runLoop->readQueues objectForKey: stream]; \ + OFList *queue = [runLoop->_readQueues objectForKey: stream]; \ type *queueItem; \ \ if (queue == nil) { \ queue = [OFList list]; \ - [runLoop->readQueues setObject: queue \ - forKey: stream]; \ + [runLoop->_readQueues setObject: queue \ + forKey: stream]; \ } \ \ if ([queue count] == 0) \ - [runLoop->streamObserver addStreamForReading: stream]; \ + [runLoop->_streamObserver addStreamForReading: stream]; \ \ queueItem = [[[type alloc] init] autorelease]; \ code \ [queue appendObject: queueItem]; \ \ @@ -176,14 +181,14 @@ length: (size_t)length target: (id)target selector: (SEL)selector { ADD(OFRunLoop_ReadQueueItem, { - queueItem->buffer = buffer; - queueItem->length = length; - queueItem->target = [target retain]; - queueItem->selector = selector; + queueItem->_target = [target retain]; + queueItem->_selector = selector; + queueItem->_buffer = buffer; + queueItem->_length = length; }) } + (void)OF_addAsyncReadForStream: (OFStream*)stream buffer: (void*)buffer @@ -190,36 +195,36 @@ exactLength: (size_t)exactLength target: (id)target selector: (SEL)selector { ADD(OFRunLoop_ExactReadQueueItem, { - queueItem->buffer = buffer; - queueItem->exactLength = exactLength; - queueItem->target = [target retain]; - queueItem->selector = selector; + queueItem->_target = [target retain]; + queueItem->_selector = selector; + queueItem->_buffer = buffer; + queueItem->_exactLength = exactLength; }) } + (void)OF_addAsyncReadLineForStream: (OFStream*)stream encoding: (of_string_encoding_t)encoding target: (id)target selector: (SEL)selector { ADD(OFRunLoop_ReadLineQueueItem, { - queueItem->encoding = encoding; - queueItem->target = [target retain]; - queueItem->selector = selector; + queueItem->_target = [target retain]; + queueItem->_selector = selector; + queueItem->_encoding = encoding; }) } + (void)OF_addAsyncAcceptForTCPSocket: (OFTCPSocket*)stream target: (id)target selector: (SEL)selector { ADD(OFRunLoop_AcceptQueueItem, { - queueItem->target = [target retain]; - queueItem->selector = selector; + queueItem->_target = [target retain]; + queueItem->_selector = selector; }) } #ifdef OF_HAVE_BLOCKS + (void)OF_addAsyncReadForStream: (OFStream*)stream @@ -226,43 +231,43 @@ buffer: (void*)buffer length: (size_t)length block: (of_stream_async_read_block_t)block { ADD(OFRunLoop_ReadQueueItem, { - queueItem->buffer = buffer; - queueItem->length = length; - queueItem->block = [block copy]; + queueItem->_block = [block copy]; + queueItem->_buffer = buffer; + queueItem->_length = length; }) } + (void)OF_addAsyncReadForStream: (OFStream*)stream buffer: (void*)buffer exactLength: (size_t)exactLength block: (of_stream_async_read_block_t)block { ADD(OFRunLoop_ExactReadQueueItem, { - queueItem->buffer = buffer; - queueItem->exactLength = exactLength; - queueItem->block = [block copy]; + queueItem->_block = [block copy]; + queueItem->_buffer = buffer; + queueItem->_exactLength = exactLength; }) } + (void)OF_addAsyncReadLineForStream: (OFStream*)stream encoding: (of_string_encoding_t)encoding block: (of_stream_async_read_line_block_t)block { ADD(OFRunLoop_ReadLineQueueItem, { - queueItem->encoding = encoding; - queueItem->block = [block copy]; + queueItem->_block = [block copy]; + queueItem->_encoding = encoding; }) } + (void)OF_addAsyncAcceptForTCPSocket: (OFTCPSocket*)stream block: (of_tcpsocket_async_accept_block_t)block { ADD(OFRunLoop_AcceptQueueItem, { - queueItem->block = [block copy]; + queueItem->_block = [block copy]; }) } #endif #undef ADD @@ -271,15 +276,15 @@ { void *pool = objc_autoreleasePoolPush(); OFRunLoop *runLoop = [self currentRunLoop]; OFList *queue; - if ((queue = [runLoop->readQueues objectForKey: stream]) != nil) { + if ((queue = [runLoop->_readQueues objectForKey: stream]) != nil) { assert([queue count] > 0); - [runLoop->streamObserver removeStreamForReading: stream]; - [runLoop->readQueues removeObjectForKey: stream]; + [runLoop->_streamObserver removeStreamForReading: stream]; + [runLoop->_readQueues removeObjectForKey: stream]; } objc_autoreleasePoolPop(pool); } @@ -286,19 +291,19 @@ - init { self = [super init]; @try { - timersQueue = [[OFSortedList alloc] init]; + _timersQueue = [[OFSortedList alloc] init]; #ifdef OF_HAVE_THREADS - timersQueueLock = [[OFMutex alloc] init]; + _timersQueueLock = [[OFMutex alloc] init]; #endif - streamObserver = [[OFStreamObserver alloc] init]; - [streamObserver setDelegate: self]; + _streamObserver = [[OFStreamObserver alloc] init]; + [_streamObserver setDelegate: self]; - readQueues = [[OFMutableDictionary alloc] init]; + _readQueues = [[OFMutableDictionary alloc] init]; } @catch (id e) { [self release]; @throw e; } @@ -305,63 +310,63 @@ return self; } - (void)dealloc { - [timersQueue release]; + [_timersQueue release]; #ifdef OF_HAVE_THREADS - [timersQueueLock release]; + [_timersQueueLock release]; #endif - [streamObserver release]; - [readQueues release]; + [_streamObserver release]; + [_readQueues release]; [super dealloc]; } - (void)addTimer: (OFTimer*)timer { #ifdef OF_HAVE_THREADS - [timersQueueLock lock]; + [_timersQueueLock lock]; @try { #endif - [timersQueue insertObject: timer]; + [_timersQueue insertObject: timer]; #ifdef OF_HAVE_THREADS } @finally { - [timersQueueLock unlock]; + [_timersQueueLock unlock]; } #endif [timer OF_setInRunLoop: self]; - [streamObserver cancel]; + [_streamObserver cancel]; } - (void)OF_removeTimer: (OFTimer*)timer { #ifdef OF_HAVE_THREADS - [timersQueueLock lock]; + [_timersQueueLock lock]; @try { #endif of_list_object_t *iter; - for (iter = [timersQueue firstListObject]; iter != NULL; + for (iter = [_timersQueue firstListObject]; iter != NULL; iter = iter->next) { if ([iter->object isEqual: timer]) { - [timersQueue removeListObject: iter]; + [_timersQueue removeListObject: iter]; break; } } #ifdef OF_HAVE_THREADS } @finally { - [timersQueueLock unlock]; + [_timersQueueLock unlock]; } #endif } - (void)streamIsReadyForReading: (OFStream*)stream { - OFList *queue = [readQueues objectForKey: stream]; + OFList *queue = [_readQueues objectForKey: stream]; of_list_object_t *listObject; OF_ENSURE(queue != nil); listObject = [queue firstListObject]; @@ -371,45 +376,47 @@ OFRunLoop_ReadQueueItem *queueItem = listObject->object; size_t length; OFException *exception = nil; @try { - length = [stream readIntoBuffer: queueItem->buffer - length: queueItem->length]; + length = [stream readIntoBuffer: queueItem->_buffer + length: queueItem->_length]; } @catch (OFException *e) { length = 0; exception = e; } #ifdef OF_HAVE_BLOCKS - if (queueItem->block != NULL) { - if (!queueItem->block(stream, queueItem->buffer, + if (queueItem->_block != NULL) { + if (!queueItem->_block(stream, queueItem->_buffer, length, exception)) { [queue removeListObject: listObject]; if ([queue count] == 0) { - [streamObserver + [_streamObserver removeStreamForReading: stream]; - [readQueues removeObjectForKey: stream]; + [_readQueues + removeObjectForKey: stream]; } } } else { #endif BOOL (*func)(id, SEL, OFStream*, void*, size_t, OFException*) = (BOOL(*)(id, SEL, OFStream*, void*, size_t, OFException*)) - [queueItem->target methodForSelector: - queueItem->selector]; + [queueItem->_target methodForSelector: + queueItem->_selector]; - if (!func(queueItem->target, queueItem->selector, - stream, queueItem->buffer, length, exception)) { + if (!func(queueItem->_target, queueItem->_selector, + stream, queueItem->_buffer, length, exception)) { [queue removeListObject: listObject]; if ([queue count] == 0) { - [streamObserver + [_streamObserver removeStreamForReading: stream]; - [readQueues removeObjectForKey: stream]; + [_readQueues + removeObjectForKey: stream]; } } #ifdef OF_HAVE_BLOCKS } #endif @@ -419,59 +426,60 @@ size_t length; OFException *exception = nil; @try { length = [stream - readIntoBuffer: (char*)queueItem->buffer + - queueItem->readLength - length: queueItem->exactLength - - queueItem->readLength]; + readIntoBuffer: (char*)queueItem->_buffer + + queueItem->_readLength + length: queueItem->_exactLength - + queueItem->_readLength]; } @catch (OFException *e) { length = 0; exception = e; } - queueItem->readLength += length; - if (queueItem->readLength == queueItem->exactLength || + queueItem->_readLength += length; + if (queueItem->_readLength == queueItem->_exactLength || [stream isAtEndOfStream] || exception != nil) { #ifdef OF_HAVE_BLOCKS - if (queueItem->block != NULL) { - if (queueItem->block(stream, queueItem->buffer, - queueItem->readLength, exception)) - queueItem->readLength = 0; + if (queueItem->_block != NULL) { + if (queueItem->_block(stream, + queueItem->_buffer, queueItem->_readLength, + exception)) + queueItem->_readLength = 0; else { [queue removeListObject: listObject]; if ([queue count] == 0) { - [streamObserver + [_streamObserver removeStreamForReading: stream]; - [readQueues + [_readQueues removeObjectForKey: stream]; } } } else { #endif BOOL (*func)(id, SEL, OFStream*, void*, size_t, OFException*) = (BOOL(*)(id, SEL, OFStream*, void*, size_t, OFException*)) - [queueItem->target - methodForSelector: queueItem->selector]; + [queueItem->_target + methodForSelector: queueItem->_selector]; - if (func(queueItem->target, - queueItem->selector, stream, - queueItem->buffer, queueItem->readLength, + if (func(queueItem->_target, + queueItem->_selector, stream, + queueItem->_buffer, queueItem->_readLength, exception)) - queueItem->readLength = 0; + queueItem->_readLength = 0; else { [queue removeListObject: listObject]; if ([queue count] == 0) { - [streamObserver + [_streamObserver removeStreamForReading: stream]; - [readQueues + [_readQueues removeObjectForKey: stream]; } } #ifdef OF_HAVE_BLOCKS } @@ -483,50 +491,50 @@ OFString *line; OFException *exception = nil; @try { line = [stream - tryReadLineWithEncoding: queueItem->encoding]; + tryReadLineWithEncoding: queueItem->_encoding]; } @catch (OFException *e) { line = nil; exception = e; } if (line != nil || [stream isAtEndOfStream] || exception != nil) { #ifdef OF_HAVE_BLOCKS - if (queueItem->block != NULL) { - if (!queueItem->block(stream, line, + if (queueItem->_block != NULL) { + if (!queueItem->_block(stream, line, exception)) { [queue removeListObject: listObject]; if ([queue count] == 0) { - [streamObserver + [_streamObserver removeStreamForReading: stream]; - [readQueues + [_readQueues removeObjectForKey: stream]; } } } else { #endif BOOL (*func)(id, SEL, OFStream*, OFString*, OFException*) = (BOOL(*)(id, SEL, OFStream*, OFString*, OFException*)) - [queueItem->target methodForSelector: - queueItem->selector]; + [queueItem->_target methodForSelector: + queueItem->_selector]; - if (!func(queueItem->target, - queueItem->selector, stream, line, + if (!func(queueItem->_target, + queueItem->_selector, stream, line, exception)) { [queue removeListObject: listObject]; if ([queue count] == 0) { - [streamObserver + [_streamObserver removeStreamForReading: stream]; - [readQueues + [_readQueues removeObjectForKey: stream]; } } #ifdef OF_HAVE_BLOCKS } @@ -544,38 +552,40 @@ newSocket = nil; exception = e; } #ifdef OF_HAVE_BLOCKS - if (queueItem->block != NULL) { - if (!queueItem->block((OFTCPSocket*)stream, + if (queueItem->_block != NULL) { + if (!queueItem->_block((OFTCPSocket*)stream, newSocket, exception)) { [queue removeListObject: listObject]; if ([queue count] == 0) { - [streamObserver + [_streamObserver removeStreamForReading: stream]; - [readQueues removeObjectForKey: stream]; + [_readQueues + removeObjectForKey: stream]; } } } else { #endif BOOL (*func)(id, SEL, OFTCPSocket*, OFTCPSocket*, OFException*) = (BOOL(*)(id, SEL, OFTCPSocket*, OFTCPSocket*, OFException*)) - [queueItem->target methodForSelector: - queueItem->selector]; + [queueItem->_target methodForSelector: + queueItem->_selector]; - if (!func(queueItem->target, queueItem->selector, + if (!func(queueItem->_target, queueItem->_selector, (OFTCPSocket*)stream, newSocket, exception)) { [queue removeListObject: listObject]; if ([queue count] == 0) { - [streamObserver + [_streamObserver removeStreamForReading: stream]; - [readQueues removeObjectForKey: stream]; + [_readQueues + removeObjectForKey: stream]; } } #ifdef OF_HAVE_BLOCKS } #endif @@ -583,76 +593,76 @@ OF_ENSURE(0); } - (void)run { - running = YES; + _running = YES; - while (running) { + while (_running) { void *pool = objc_autoreleasePoolPush(); OFDate *now = [OFDate date]; OFTimer *timer; OFDate *nextTimer; #ifdef OF_HAVE_THREADS - [timersQueueLock lock]; + [_timersQueueLock lock]; @try { #endif of_list_object_t *listObject = - [timersQueue firstListObject]; + [_timersQueue firstListObject]; if (listObject != NULL && [[listObject->object fireDate] compare: now] != OF_ORDERED_DESCENDING) { timer = [[listObject->object retain] autorelease]; - [timersQueue removeListObject: listObject]; + [_timersQueue removeListObject: listObject]; [timer OF_setInRunLoop: nil]; } else timer = nil; #ifdef OF_HAVE_THREADS } @finally { - [timersQueueLock unlock]; + [_timersQueueLock unlock]; } #endif if ([timer isValid]) [timer fire]; #ifdef OF_HAVE_THREADS - [timersQueueLock lock]; + [_timersQueueLock lock]; @try { #endif - nextTimer = [[timersQueue firstObject] fireDate]; + nextTimer = [[_timersQueue firstObject] fireDate]; #ifdef OF_HAVE_THREADS } @finally { - [timersQueueLock unlock]; + [_timersQueueLock unlock]; } #endif /* Watch for stream events until the next timer is due */ if (nextTimer != nil) { double timeout = [nextTimer timeIntervalSinceNow]; if (timeout > 0) - [streamObserver observeWithTimeout: timeout]; + [_streamObserver observeWithTimeout: timeout]; } else { /* * No more timers: Just watch for streams until we get * an event. If a timer is added by another thread, it * cancels the observe. */ - [streamObserver observe]; + [_streamObserver observe]; } objc_autoreleasePoolPop(pool); } } - (void)stop { - running = NO; - [streamObserver cancel]; + _running = NO; + [_streamObserver cancel]; } @end