From 4978c42916c7ae90c367aff86ba0fd65af2c9f6a Mon Sep 17 00:00:00 2001 From: Jonathan Schleifer Date: Thu, 13 Dec 2012 23:29:12 +0100 Subject: [PATCH] Add -[asyncConnectAndHandle]. --- src/XMPPConnection.h | 6 ++++ src/XMPPConnection.m | 69 ++++++++++++++++++++++++++++++++++++++++++++ tests/test.m | 7 +---- 3 files changed, 76 insertions(+), 6 deletions(-) diff --git a/src/XMPPConnection.h b/src/XMPPConnection.h index b566620..8d5caa0 100644 --- a/src/XMPPConnection.h +++ b/src/XMPPConnection.h @@ -261,6 +261,12 @@ */ - (void)handleConnection; +/** + * \brief Asynchronously connects to the server and adds the connection to the + * run loop. + */ +- (void)asyncConnectAndHandle; + /** * \brief Parses the specified buffer. * diff --git a/src/XMPPConnection.m b/src/XMPPConnection.m index 08e82a3..b092e00 100644 --- a/src/XMPPConnection.m +++ b/src/XMPPConnection.m @@ -58,6 +58,64 @@ #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 { @@ -308,6 +366,17 @@ 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 { diff --git a/tests/test.m b/tests/test.m index bdc43f1..70f7d45 100644 --- a/tests/test.m +++ b/tests/test.m @@ -117,12 +117,7 @@ OF_APPLICATION_DELEGATE(AppDelegate) [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