@@ -26,63 +26,73 @@ #import "autorelease.h" #import "macros.h" static OFRunLoop *mainRunLoop = nil; -@interface OFRunLoop_ReadQueueItem: OFObject +@interface OFRunLoop_QueueItem: OFObject +{ +@public + id target; + SEL selector; + id context; +} +@end + +@interface OFRunLoop_ReadQueueItem: OFRunLoop_QueueItem { @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 +@interface OFRunLoop_ExactReadQueueItem: OFRunLoop_QueueItem { @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 +@interface OFRunLoop_ReadLineQueueItem: OFRunLoop_QueueItem { @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 +@interface OFRunLoop_AcceptQueueItem: OFRunLoop_QueueItem { @public - id target; - SEL selector; #ifdef OF_HAVE_BLOCKS of_tcpsocket_async_accept_block_t block; #endif } @end -@implementation OFRunLoop_ReadQueueItem +@implementation OFRunLoop_QueueItem - (void)dealloc { [target release]; + [context release]; + + [super dealloc]; +} +@end + +@implementation OFRunLoop_ReadQueueItem +- (void)dealloc +{ #ifdef OF_HAVE_BLOCKS [block release]; #endif [super dealloc]; @@ -90,11 +100,10 @@ @end @implementation OFRunLoop_ExactReadQueueItem - (void)dealloc { - [target release]; #ifdef OF_HAVE_BLOCKS [block release]; #endif [super dealloc]; @@ -102,11 +111,10 @@ @end @implementation OFRunLoop_ReadLineQueueItem - (void)dealloc { - [target release]; #ifdef OF_HAVE_BLOCKS [block release]; #endif [super dealloc]; @@ -114,11 +122,10 @@ @end @implementation OFRunLoop_AcceptQueueItem - (void)dealloc { - [target release]; #ifdef OF_HAVE_BLOCKS [block release]; #endif [super dealloc]; @@ -167,52 +174,60 @@ + (void)OF_addAsyncReadForStream: (OFStream*)stream buffer: (void*)buffer length: (size_t)length target: (id)target selector: (SEL)selector + context: (id)context { ADD(OFRunLoop_ReadQueueItem, { queueItem->buffer = buffer; queueItem->length = length; queueItem->target = [target retain]; queueItem->selector = selector; + queueItem->context = [context retain]; }) } + (void)OF_addAsyncReadForStream: (OFStream*)stream buffer: (void*)buffer exactLength: (size_t)exactLength target: (id)target selector: (SEL)selector + context: (id)context { ADD(OFRunLoop_ExactReadQueueItem, { queueItem->buffer = buffer; queueItem->exactLength = exactLength; queueItem->target = [target retain]; queueItem->selector = selector; + queueItem->context = [context retain]; }) } + (void)OF_addAsyncReadLineForStream: (OFStream*)stream encoding: (of_string_encoding_t)encoding target: (id)target selector: (SEL)selector + context: (id)context { ADD(OFRunLoop_ReadLineQueueItem, { queueItem->encoding = encoding; queueItem->target = [target retain]; queueItem->selector = selector; + queueItem->context = [context retain]; }) } + (void)OF_addAsyncAcceptForTCPSocket: (OFTCPSocket*)stream target: (id)target selector: (SEL)selector + context: (id)context { ADD(OFRunLoop_AcceptQueueItem, { queueItem->target = [target retain]; queueItem->selector = selector; + queueItem->context = [context retain]; }) } #ifdef OF_HAVE_BLOCKS + (void)OF_addAsyncReadForStream: (OFStream*)stream @@ -331,18 +346,19 @@ [readQueues removeObjectForKey: stream]; } } } else { #endif - BOOL (*func)(id, SEL, OFStream*, void*, size_t, + BOOL (*func)(id, SEL, OFStream*, void*, size_t, id, OFException*) = (BOOL(*)(id, SEL, OFStream*, void*, - size_t, OFException*)) + size_t, id, OFException*)) [queueItem->target methodForSelector: queueItem->selector]; if (!func(queueItem->target, queueItem->selector, - stream, queueItem->buffer, length, exception)) { + stream, queueItem->buffer, length, + queueItem->context, exception)) { [queue removeListObject: listObject]; if ([queue count] == 0) { [streamObserver removeStreamForReading: stream]; @@ -388,20 +404,20 @@ removeObjectForKey: stream]; } } } else { #endif - BOOL (*func)(id, SEL, OFStream*, void*, - size_t, OFException*) = (BOOL(*)(id, SEL, - OFStream*, void*, size_t, OFException*)) + BOOL (*func)(id, SEL, OFStream*, void*, size_t, + id, OFException*) = (BOOL(*)(id, SEL, + OFStream*, void*, size_t, id, OFException*)) [queueItem->target methodForSelector: queueItem->selector]; if (func(queueItem->target, queueItem->selector, stream, queueItem->buffer, queueItem->readLength, - exception)) + queueItem->context, exception)) queueItem->readLength = 0; else { [queue removeListObject: listObject]; if ([queue count] == 0) { @@ -447,18 +463,18 @@ } } } else { #endif BOOL (*func)(id, SEL, OFStream*, OFString*, - OFException*) = (BOOL(*)(id, SEL, OFStream*, - OFString*, OFException*)) + id, OFException*) = (BOOL(*)(id, SEL, + OFStream*, OFString*, id, OFException*)) [queueItem->target methodForSelector: queueItem->selector]; if (!func(queueItem->target, queueItem->selector, stream, line, - exception)) { + queueItem->context, exception)) { [queue removeListObject: listObject]; if ([queue count] == 0) { [streamObserver removeStreamForReading: @@ -497,18 +513,19 @@ } } } else { #endif BOOL (*func)(id, SEL, OFTCPSocket*, OFTCPSocket*, - OFException*) = + id, OFException*) = (BOOL(*)(id, SEL, OFTCPSocket*, OFTCPSocket*, - OFException*)) + id, OFException*)) [queueItem->target methodForSelector: queueItem->selector]; if (!func(queueItem->target, queueItem->selector, - (OFTCPSocket*)stream, newSocket, exception)) { + (OFTCPSocket*)stream, newSocket, queueItem->context, + exception)) { [queue removeListObject: listObject]; if ([queue count] == 0) { [streamObserver removeStreamForReading: stream];