Changeset 3093

Show
Ignore:
Timestamp:
12/27/05 01:51:20 (3 years ago)
Author:
timothy
Message:
  • Make the new connection code run in a thread.
  • Adding the InterThreadMessaging? code to let use call methods on the connection thread.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/cocoa-networking/Chat Core.xcodeproj/project.pbxproj

    r3081 r3093  
    4545                1CD020B505759347000F93B4 /* NSStringAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 1CD020B305759347000F93B4 /* NSStringAdditions.m */; }; 
    4646                1CD020C0057593F9000F93B4 /* NSColorAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 1CD020BD057593A9000F93B4 /* NSColorAdditions.m */; }; 
     47                1CD6296709611B9100BD1DD2 /* InterThreadMessaging.m in Sources */ = {isa = PBXBuildFile; fileRef = 1CD6290D096118B900BD1DD2 /* InterThreadMessaging.m */; }; 
    4748                1CDDD9AD0718BD2E00FE11C3 /* MVChatRoom.h in Headers */ = {isa = PBXBuildFile; fileRef = 1C5CC07A06FFCB0B0006C5B1 /* MVChatRoom.h */; settings = {ATTRIBUTES = (Public, ); }; }; 
    4849                1CE4630F095ECCFE00AB7732 /* MVChatUserPrivate.h in Headers */ = {isa = PBXBuildFile; fileRef = 1CE4630E095ECCFE00AB7732 /* MVChatUserPrivate.h */; }; 
     
    323324                1CD020BC057593A9000F93B4 /* NSColorAdditions.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = NSColorAdditions.h; path = Additions/NSColorAdditions.h; sourceTree = "<group>"; }; 
    324325                1CD020BD057593A9000F93B4 /* NSColorAdditions.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; name = NSColorAdditions.m; path = Additions/NSColorAdditions.m; sourceTree = "<group>"; }; 
     326                1CD6290C096118B900BD1DD2 /* InterThreadMessaging.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = InterThreadMessaging.h; path = "Chat Core/InterThreadMessaging.h"; sourceTree = "<group>"; }; 
     327                1CD6290D096118B900BD1DD2 /* InterThreadMessaging.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = InterThreadMessaging.m; path = "Chat Core/InterThreadMessaging.m"; sourceTree = "<group>"; }; 
    325328                1CE4630E095ECCFE00AB7732 /* MVChatUserPrivate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = MVChatUserPrivate.h; path = "Chat Core/MVChatUserPrivate.h"; sourceTree = "<group>"; }; 
    326329                1CE4631D095ECD7300AB7732 /* MVChatRoomPrivate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = MVChatRoomPrivate.h; path = "Chat Core/MVChatRoomPrivate.h"; sourceTree = "<group>"; }; 
     
    738741                                1CEEB164095BE36700166A54 /* AsyncSocket.m */, 
    739742                                1CEEB163095BE36700166A54 /* AsyncSocket.h */, 
     743                                1CD6290D096118B900BD1DD2 /* InterThreadMessaging.m */, 
     744                                1CD6290C096118B900BD1DD2 /* InterThreadMessaging.h */, 
    740745                        ); 
    741746                        name = "Sources & Headers"; 
     
    974979                                1C7C776A07DBBA4500FB5F83 /* NSScriptCommandAdditions.m in Sources */, 
    975980                                1CEEB166095BE36700166A54 /* AsyncSocket.m in Sources */, 
     981                                1CD6296709611B9100BD1DD2 /* InterThreadMessaging.m in Sources */, 
    976982                        ); 
    977983                        runOnlyForDeploymentPostprocessing = 0; 
  • branches/cocoa-networking/Chat Core/MVChatConnection.m

    r3088 r3093  
    759759 
    760760- (void) _didDisconnect { 
    761         BOOL wasConnected = ( _status == MVChatConnectionConnectedStatus || _status == MVChatConnectionSuspendedStatus ); 
     761        BOOL wasConnected = ( _status == MVChatConnectionConnectedStatus || _status == MVChatConnectionSuspendedStatus || _status == MVChatConnectionServerDisconnectedStatus ); 
    762762 
    763763        [[self localUser] _setStatus:MVChatUserOfflineStatus]; 
  • branches/cocoa-networking/Chat Core/MVIRCChatConnection.h

    r3084 r3093  
    77@private 
    88        AsyncSocket *_chatConnection; 
     9        NSThread *_connectionThread; 
    910        NSMutableDictionary *_knownUsers; 
    1011        NSString *_server; 
  • branches/cocoa-networking/Chat Core/MVIRCChatConnection.m

    r3088 r3093  
     1#import <sched.h> 
     2 
    13#import "MVIRCChatConnection.h" 
    24#import "MVIRCChatRoom.h" 
     
    57 
    68#import "AsyncSocket.h" 
     9#import "InterThreadMessaging.h" 
    710#import "MVChatPluginManager.h" 
    811#import "NSAttributedStringAdditions.h" 
     
    717720        [self disconnect]; 
    718721 
     722        [_chatConnection release]; 
    719723        [_knownUsers release]; 
     724        [_proxyUsername release]; 
     725        [_proxyPassword release]; 
     726 
     727        _chatConnection = nil; 
     728        _connectionThread = nil; 
    720729        _knownUsers = nil; 
    721  
    722         [_proxyUsername release]; 
    723730        _proxyUsername = nil; 
    724  
    725         [_proxyPassword release]; 
    726731        _proxyPassword = nil; 
    727732 
     
    764769        [self _willConnect]; // call early so other code has a chance to change our info 
    765770 
    766         if( ! [_chatConnection connectToHost:[self server] onPort:[self serverPort] error:NULL] ) 
    767                 [self _didNotConnect]; 
     771        if( ! _connectionThread ) { 
     772                [NSThread prepareForInterThreadMessages]; 
     773                [NSThread detachNewThreadSelector:@selector( _ircRunloop ) toTarget:self withObject:nil]; 
     774                while( ! _connectionThread ) sched_yield(); 
     775        } 
     776 
     777        [self performSelector:@selector( _connect ) inThread:_connectionThread]; 
    768778 
    769779/* 
     
    795805        } 
    796806 
    797         [_chatConnection disconnectAfterWriting]; 
     807        _status = MVChatConnectionDisconnectedStatus; 
     808        [_chatConnection performSelector:@selector( disconnectAfterWriting ) inThread:_connectionThread]; 
    798809} 
    799810 
     
    988999        [data appendBytes:"\x0D\x0A" length:2]; 
    9891000 
    990         [_chatConnection writeData:data withTimeout:-1. tag:0]; 
     1001        [self performSelector:@selector( _writeDataToServer: ) withObject:data inThread:_connectionThread]; 
    9911002 
    9921003        [[NSNotificationCenter defaultCenter] postNotificationName:MVChatConnectionGotRawMessageNotification object:self userInfo:[NSDictionary dictionaryWithObjectsAndKeys:string, @"message", [NSNumber numberWithBool:YES], @"outbound", nil]]; 
     
    11231134 
    11241135@implementation MVIRCChatConnection (MVIRCChatConnectionPrivate) 
    1125 - (oneway void) _connectionRunloop { 
     1136- (void) _connect { 
     1137        [_chatConnection disconnect]; 
     1138        if( ! [_chatConnection connectToHost:[self server] onPort:[self serverPort] error:NULL] ) 
     1139                [self _didNotConnect]; 
     1140
     1141 
     1142- (oneway void) _ircRunloop { 
     1143        NSAutoreleasePool *pool = [[NSAutoreleasePool allocWithZone:nil] init]; 
     1144 
     1145        _connectionThread = [NSThread currentThread]; 
     1146        [NSThread prepareForInterThreadMessages]; 
     1147 
     1148//      BOOL active = YES; 
     1149//      while( active && ( _status == MVChatConnectionConnectedStatus || _status == MVChatConnectionConnectingStatus ) ) 
     1150//              active = [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; 
    11261151        [[NSRunLoop currentRunLoop] run]; 
     1152 
     1153        _connectionThread = nil; 
     1154 
     1155        [pool release]; 
     1156} 
     1157 
     1158- (void) _didDisconnect { 
     1159        if( ABS( [_lastConnectAttempt timeIntervalSinceNow] ) > 300. ) 
     1160                [self performSelector:@selector( connect ) withObject:nil afterDelay:5.]; 
     1161        [self scheduleReconnectAttemptEvery:30.]; 
     1162        [super _didDisconnect]; 
    11271163} 
    11281164 
     
    11301166        NSLog(@"willDisconnectWithError: %@", error ); 
    11311167        _status = MVChatConnectionServerDisconnectedStatus; 
    1132         if( ABS( [_lastConnectAttempt timeIntervalSinceNow] ) > 300. ) 
    1133                 [self performSelector:@selector( connect ) withObject:nil afterDelay:5.]; 
    1134         [self scheduleReconnectAttemptEvery:30.]; 
    11351168} 
    11361169 
    11371170- (void) socketDidDisconnect:(AsyncSocket *) sock { 
    1138         if( _status != MVChatConnectionServerDisconnectedStatus ) 
    1139                 _status = MVChatConnectionDisconnectedStatus; 
    1140  
    11411171        id old = _localUser; 
    11421172        _localUser = nil; 
    11431173        [old release]; 
    11441174 
    1145         [self _didDisconnect]; 
     1175        [self performSelectorOnMainThread:@selector( _didDisconnect ) withObject:nil waitUntilDone:NO]; 
    11461176} 
    11471177 
     
    11551185        [old release]; 
    11561186 
    1157         [self _didConnect]; 
     1187        [self performSelectorOnMainThread:@selector( _didConnect ) withObject:nil waitUntilDone:NO]; 
    11581188        [self _readNextMessageFromServer]; 
    11591189} 
     
    12911321 
    12921322        [self _readNextMessageFromServer]; 
     1323} 
     1324 
     1325- (void) _writeDataToServer:(NSData *) data { 
     1326        [_chatConnection writeData:data withTimeout:-1. tag:0]; 
    12931327} 
    12941328 
     
    15101544                                                if( ! [self nicknamePassword] ) { 
    15111545                                                        [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadWithName:MVChatConnectionNeedNicknamePasswordNotification object:self userInfo:nil]; 
    1512                                                 } else [self sendRawMessageWithFormat:@"PRIVMSG %@ :IDENTIFY %@", [self nickname], [self nicknamePassword]]; 
     1546                                                } else [self sendRawMessageWithFormat:@"PRIVMSG NickServ :IDENTIFY %@", [self nicknamePassword]]; 
    15131547                                        } else if( [msg rangeOfString:@"Password accepted"].location != NSNotFound ) { 
    15141548                                                [[self localUser] _setIdentified:YES];