Index: src/XMPPConnection.h ================================================================== --- src/XMPPConnection.h +++ src/XMPPConnection.h @@ -259,10 +259,16 @@ /** * \brief Adds the connection to the run loop. */ - (void)handleConnection; +/** + * \brief Asynchronously connects to the server and adds the connection to the + * run loop. + */ +- (void)asyncConnectAndHandle; + /** * \brief Parses the specified buffer. * * This is useful for handling multiple connections at once. * Index: src/XMPPConnection.m ================================================================== --- src/XMPPConnection.m +++ src/XMPPConnection.m @@ -55,10 +55,68 @@ #import "namespaces.h" #import #define BUFFER_LENGTH 512 + +@interface XMPPConnection_ConnectThread: OFThread +{ + OFThread *sourceThread; + XMPPConnection *connection; +} + +- initWithSourceThread: (OFThread*)sourceThread + connection: (XMPPConnection*)connection; +@end + +@implementation XMPPConnection_ConnectThread +- initWithSourceThread: (OFThread*)sourceThread_ + connection: (XMPPConnection*)connection_ +{ + self = [super init]; + + @try { + sourceThread = [sourceThread_ retain]; + connection = [connection_ retain]; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- (void)dealloc +{ + [sourceThread release]; + [connection release]; + + [super dealloc]; +} + +- (void)didConnect +{ + [self join]; + + [connection handleConnection]; +} + +- (id)main +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + + [connection connect]; + + [self performSelector: @selector(didConnect) + onThread: sourceThread + waitUntilDone: NO]; + + [pool release]; + + return nil; +} +@end @implementation XMPPConnection + connection { return [[[self alloc] init] autorelease]; @@ -305,10 +363,21 @@ length: BUFFER_LENGTH target: self selector: @selector(stream:didReadIntoBuffer:length: exception:)]; } + +- (void)asyncConnectAndHandle +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + + [[[[XMPPConnection_ConnectThread alloc] + initWithSourceThread: [OFThread currentThread] + connection: self] autorelease] start]; + + [pool release]; +} - (BOOL)XMPP_parseBuffer: (const void*)buffer length: (size_t)length { if ([sock isAtEndOfStream]) { Index: tests/test.m ================================================================== --- tests/test.m +++ tests/test.m @@ -115,16 +115,11 @@ [conn setDomain: [arguments objectAtIndex: 0]]; [conn setUsername: [arguments objectAtIndex: 1]]; [conn setPassword: [arguments objectAtIndex: 2]]; [conn setResource: @"ObjXMPP"]; - @try { - [conn connect]; - [conn handleConnection]; - } @catch (id e) { - of_log(@"%@", e); - } + [conn asyncConnectAndHandle]; } - (void)connection: (XMPPConnection*)conn didReceiveElement: (OFXMLElement*)element {