Changeset 3104

Show
Ignore:
Timestamp:
12/31/05 17:14:12 (3 years ago)
Author:
timothy
Message:

Share more code.
The transfer is done in turbo mode when the connection is disconnected and all bytes are transfered.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/cocoa-networking/Chat Core/MVIRCFileTransfer.m

    r3103 r3104  
    1010 
    1111#define DCCPacketSize 4096 
     12 
     13static BOOL acceptConnectionOnFirstPortInRange( AsyncSocket *connection, NSRange ports ) { 
     14        unsigned int port = ports.location; 
     15        BOOL success = NO; 
     16        while( ! success ) { 
     17                if( [connection acceptOnPort:port error:NULL] ) { 
     18                        success = YES; 
     19                        break; 
     20                } else { 
     21                        if( port == 0 ) break; 
     22                        [connection disconnect]; 
     23                        if( ++port > NSMaxRange( ports ) ) 
     24                                port = 0; // just use a random port since the user defined range is in use 
     25                } 
     26        } 
     27 
     28        return success; 
     29} 
     30 
     31static id dccFriendlyAddress( AsyncSocket *connection ) { 
     32        id address = [connection localHost]; 
     33        if( [address rangeOfString:@"."].location != NSNotFound ) 
     34                address = [NSNumber numberWithUnsignedLong:ntohl( inet_addr( [address UTF8String] ) )]; 
     35        return address; 
     36} 
    1237 
    1338/*static void MVFileTransferClosed( FILE_DCC_REC *dcc ) { 
     
    88113@implementation MVIRCUploadFileTransfer 
    89114+ (void) initialize { 
    90         portRange = NSMakeRange( 1024, 20 ); 
     115        portRange = NSMakeRange( 1024, 24 ); 
    91116} 
    92117 
     
    164189 
    165190- (void) socketDidDisconnect:(AsyncSocket *) sock { 
     191        if( [self status] != MVFileTransferDoneStatus && [self transfered] == [self finalSize] ) { 
     192                [self _setStatus:MVFileTransferDoneStatus]; 
     193                [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadWithName:MVFileTransferFinishedNotification object:self]; 
     194        } 
     195 
    166196        if( [self status] != MVFileTransferDoneStatus && [self status] != MVFileTransferStoppedStatus ) 
    167197                [self _setStatus:MVFileTransferErrorStatus]; 
     
    191221                unsigned long long progress = [self transfered] + tag; 
    192222                [self _setTransfered:progress]; 
    193                 if( progress == [self finalSize] ) { 
    194                         [self _setStatus:MVFileTransferDoneStatus]; 
    195                         [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadWithName:MVFileTransferFinishedNotification object:self]; 
    196                 } 
    197223        } 
    198224 
     
    202228- (void) socket:(AsyncSocket *) sock didReadData:(NSData *) data withTag:(long) tag { 
    203229        unsigned long bytes = ntohl( *( (unsigned long *) [data bytes] ) ); 
    204  
    205         [self _setTransfered:bytes]; 
     230        if( bytes > [self transfered] ) [self _setTransfered:bytes]; 
     231 
    206232        [_connection readDataToLength:4 withTimeout:-1. tag:0]; 
    207233 
     
    241267        _acceptConnection = [[AsyncSocket allocWithZone:nil] initWithDelegate:self]; 
    242268 
    243         unsigned int port = portRange.location; 
    244         BOOL success = NO; 
    245         while( ! success ) { 
    246                 if( [_acceptConnection acceptOnPort:port error:NULL] ) { 
    247                         success = YES; 
    248                         break; 
    249                 } else { 
    250                         [_acceptConnection disconnect]; 
    251                         if( ++port > NSMaxRange( portRange ) ) 
    252                                 port = 0; // just use a random port since the user defined range is in use 
    253                 } 
    254         } 
     269        BOOL success = acceptConnectionOnFirstPortInRange( _acceptConnection, portRange ); 
    255270 
    256271        if( success ) { 
    257                 id address = [[(MVIRCChatConnection *)[[self user] connection] _chatConnection] localHost]; 
    258                 if( [address rangeOfString:@"."].location != NSNotFound ) 
    259                         address = [NSNumber numberWithUnsignedLong:ntohl( inet_addr( [address UTF8String] ) )]; 
     272                id address = dccFriendlyAddress( [(MVIRCChatConnection *)[[self user] connection] _chatConnection] ); 
    260273                [self _setPort:[_acceptConnection localPort]]; 
    261274 
     
    491504        _acceptConnection = [[AsyncSocket allocWithZone:nil] initWithDelegate:self]; 
    492505 
    493         unsigned int port = portRange.location; 
    494         BOOL success = NO; 
    495         while( ! success ) { 
    496                 if( [_acceptConnection acceptOnPort:port error:NULL] ) { 
    497                         success = YES; 
    498                         break; 
    499                 } else { 
    500                         [_acceptConnection disconnect]; 
    501                         if( ++port > NSMaxRange( portRange ) ) 
    502                                 port = 0; // just use a random port since the user defined range is in use 
    503                 } 
    504         } 
     506        BOOL success = acceptConnectionOnFirstPortInRange( _acceptConnection, portRange ); 
    505507 
    506508        if( success ) { 
    507                 id address = [[(MVIRCChatConnection *)[[self user] connection] _chatConnection] localHost]; 
    508                 if( [address rangeOfString:@"."].location != NSNotFound ) 
    509                         address = [NSNumber numberWithUnsignedLong:ntohl( inet_addr( [address UTF8String] ) )]; 
     509                id address = dccFriendlyAddress( [(MVIRCChatConnection *)[[self user] connection] _chatConnection] ); 
    510510                [self _setPort:[_acceptConnection localPort]]; 
    511511