Changeset 3119

Show
Ignore:
Timestamp:
02/07/06 01:50:28 (3 years ago)
Author:
timothy
Message:

More fixes to how connections are setup and makes sure they are added to the correct runloop on the connection thread.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/cocoa-networking/Chat Core/MVIRCChatConnection.h

    r3117 r3119  
    1717        NSString *_password; 
    1818        NSString *_realName; 
     19        NSConditionLock *_threadWaitLock; 
    1920        unsigned short _serverPort; 
    2021} 
  • branches/cocoa-networking/Chat Core/MVIRCChatConnection.m

    r3118 r3119  
    302302                _currentNickname = [_nickname retain]; 
    303303                _realName = [NSFullUserName() retain]; 
     304                _threadWaitLock = [[NSConditionLock allocWithZone:nil] initWithCondition:0]; 
    304305 
    305306                _knownUsers = [[NSMutableDictionary allocWithZone:nil] initWithCapacity:200]; 
     
    324325        [_password release]; 
    325326        [_realName release]; 
     327        [_threadWaitLock release]; 
    326328 
    327329        _chatConnection = nil; 
     
    335337        _password = nil; 
    336338        _realName = nil; 
     339        _threadWaitLock = nil; 
    337340 
    338341        [super dealloc]; 
     
    369372        } 
    370373 
    371         id old = _chatConnection; 
    372         _chatConnection = [[AsyncSocket allocWithZone:nil] initWithDelegate:self]; 
    373         [old setDelegate:nil]; 
    374         [old release]; 
    375  
    376         old = _lastConnectAttempt; 
     374        id old = _lastConnectAttempt; 
    377375        _lastConnectAttempt = [[NSDate allocWithZone:nil] init]; 
    378376        [old release]; 
     
    380378        [self _willConnect]; // call early so other code has a chance to change our info 
    381379 
    382         if( ! _connectionThread ) { 
    383                 [NSThread prepareForInterThreadMessages]; 
    384                 [NSThread detachNewThreadSelector:@selector( _ircRunloop ) toTarget:self withObject:nil]; 
    385                 while( ! _connectionThread ) sched_yield(); 
    386         } 
     380        _connectionThread = nil; 
     381        [NSThread prepareForInterThreadMessages]; 
     382        [NSThread detachNewThreadSelector:@selector( _ircRunloop ) toTarget:self withObject:nil]; 
     383 
     384        [_threadWaitLock lockWhenCondition:1]; 
     385        [_threadWaitLock unlockWithCondition:0]; 
    387386 
    388387        [self performSelector:@selector( _connect ) inThread:_connectionThread]; 
     
    539538        [self performSelector:@selector( _writeDataToServer: ) withObject:data inThread:_connectionThread]; 
    540539 
    541         [[NSNotificationCenter defaultCenter] postNotificationName:MVChatConnectionGotRawMessageNotification object:self userInfo:[NSDictionary dictionaryWithObjectsAndKeys:string, @"message", [NSNumber numberWithBool:YES], @"outbound", nil]]; 
     540        [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadWithName:MVChatConnectionGotRawMessageNotification object:self userInfo:[NSDictionary dictionaryWithObjectsAndKeys:string, @"message", [NSNumber numberWithBool:YES], @"outbound", nil]]; 
    542541 
    543542        [string release]; 
     
    679678 
    680679- (void) _connect { 
    681         [_chatConnection disconnect]; 
     680        id old = _chatConnection; 
     681        _chatConnection = [[AsyncSocket allocWithZone:nil] initWithDelegate:self]; 
     682        [old setDelegate:nil]; 
     683        [old disconnect]; 
     684        [old release]; 
     685 
    682686        if( ! [_chatConnection connectToHost:[self server] onPort:[self serverPort] error:NULL] ) 
    683687                [self _didNotConnect]; 
     
    687691        NSAutoreleasePool *pool = [[NSAutoreleasePool allocWithZone:nil] init]; 
    688692 
     693        [_threadWaitLock lockWhenCondition:0]; 
     694 
    689695        _connectionThread = [NSThread currentThread]; 
    690696        [NSThread prepareForInterThreadMessages]; 
     697 
     698        [_threadWaitLock unlockWithCondition:1]; 
    691699 
    692700        BOOL active = YES; 
     
    753761        id old = _localUser; 
    754762        _localUser = nil; 
    755         [old release]; 
    756  
    757         old = _chatConnection; 
    758         _chatConnection = nil; 
    759         [old setDelegate:nil]; 
    760763        [old release]; 
    761764 
     
    869872 
    870873end: 
    871         [[NSNotificationCenter defaultCenter] postNotificationName:MVChatConnectionGotRawMessageNotification object:self userInfo:[NSDictionary dictionaryWithObjectsAndKeys:rawString, @"message", [NSNumber numberWithBool:NO], @"outbound", nil]]; 
     874        [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadWithName:MVChatConnectionGotRawMessageNotification object:self userInfo:[NSDictionary dictionaryWithObjectsAndKeys:rawString, @"message", [NSNumber numberWithBool:NO], @"outbound", nil]]; 
    872875 
    873876        if( command && commandLength ) { 
  • branches/cocoa-networking/Chat Core/MVIRCFileTransfer.h

    r3103 r3119  
    1010        NSThread *_connectionThread; 
    1111        NSFileHandle *_fileHandle; 
     12        NSConditionLock *_threadWaitLock; 
    1213        BOOL _fileNameQuoted; 
    1314        BOOL _readData; 
     
    2930        NSThread *_connectionThread; 
    3031        NSFileHandle *_fileHandle; 
     32        NSConditionLock *_threadWaitLock; 
    3133        BOOL _fileNameQuoted; 
    3234        BOOL _done; 
  • branches/cocoa-networking/Chat Core/MVIRCFileTransfer.m

    r3104 r3119  
    251251        if( directory || ! fileExists ) return; 
    252252 
    253         [_fileHandle release]; 
    254253        _fileHandle = [[NSFileHandle fileHandleForReadingAtPath:[self source]] retain]; 
    255254        if( ! _fileHandle ) return; 
    256255        [_fileHandle seekToFileOffset:[self startOffset]]; 
    257256 
     257        _threadWaitLock = [[NSConditionLock allocWithZone:nil] initWithCondition:0]; 
     258 
    258259        [NSThread prepareForInterThreadMessages]; 
    259260        [NSThread detachNewThreadSelector:@selector( _dccRunloop ) toTarget:self withObject:nil]; 
    260         while( ! _connectionThread ) sched_yield(); 
     261 
     262        [_threadWaitLock lockWhenCondition:1]; 
     263        [_threadWaitLock unlockWithCondition:0]; 
    261264 
    262265        if( ! [self isPassive] ) [self performSelector:@selector( _waitForConnection ) inThread:_connectionThread]; 
     
    316319        [self retain]; 
    317320 
     321        [_threadWaitLock lockWhenCondition:0]; 
     322 
    318323        _connectionThread = [NSThread currentThread]; 
    319324        [NSThread prepareForInterThreadMessages]; 
    320325        [NSThread setThreadPriority:0.75]; 
     326 
     327        [_threadWaitLock unlockWithCondition:1]; 
    321328 
    322329        BOOL active = YES; 
     
    479486        if( ! fileExists ) return; 
    480487 
    481         [_fileHandle release]; 
    482488        _fileHandle = [[NSFileHandle fileHandleForWritingAtPath:[self destination]] retain]; 
    483489        if( ! _fileHandle ) return; 
    484490        [_fileHandle truncateFileAtOffset:[self startOffset]]; 
    485491 
     492        _threadWaitLock = [[NSConditionLock allocWithZone:nil] initWithCondition:0]; 
     493 
    486494        [NSThread prepareForInterThreadMessages]; 
    487495        [NSThread detachNewThreadSelector:@selector( _dccRunloop ) toTarget:self withObject:nil]; 
    488         while( ! _connectionThread ) sched_yield(); 
     496 
     497        [_threadWaitLock lockWhenCondition:1]; 
     498        [_threadWaitLock unlockWithCondition:0]; 
    489499 
    490500        if( ! [self isPassive] ) [self performSelector:@selector( _connect ) inThread:_connectionThread]; 
     
    537547        [self retain]; 
    538548 
     549        [_threadWaitLock lockWhenCondition:0]; 
     550 
    539551        _connectionThread = [NSThread currentThread]; 
    540552        [NSThread prepareForInterThreadMessages]; 
    541553        [NSThread setThreadPriority:0.75]; 
     554 
     555        [_threadWaitLock unlockWithCondition:1]; 
    542556 
    543557        BOOL active = YES;