Changeset 3104
- Timestamp:
- 12/31/05 17:14:12 (3 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/cocoa-networking/Chat Core/MVIRCFileTransfer.m
r3103 r3104 10 10 11 11 #define DCCPacketSize 4096 12 13 static 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 31 static 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 } 12 37 13 38 /*static void MVFileTransferClosed( FILE_DCC_REC *dcc ) { … … 88 113 @implementation MVIRCUploadFileTransfer 89 114 + (void) initialize { 90 portRange = NSMakeRange( 1024, 2 0);115 portRange = NSMakeRange( 1024, 24 ); 91 116 } 92 117 … … 164 189 165 190 - (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 166 196 if( [self status] != MVFileTransferDoneStatus && [self status] != MVFileTransferStoppedStatus ) 167 197 [self _setStatus:MVFileTransferErrorStatus]; … … 191 221 unsigned long long progress = [self transfered] + tag; 192 222 [self _setTransfered:progress]; 193 if( progress == [self finalSize] ) {194 [self _setStatus:MVFileTransferDoneStatus];195 [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadWithName:MVFileTransferFinishedNotification object:self];196 }197 223 } 198 224 … … 202 228 - (void) socket:(AsyncSocket *) sock didReadData:(NSData *) data withTag:(long) tag { 203 229 unsigned long bytes = ntohl( *( (unsigned long *) [data bytes] ) ); 204 205 [self _setTransfered:bytes]; 230 if( bytes > [self transfered] ) [self _setTransfered:bytes]; 231 206 232 [_connection readDataToLength:4 withTimeout:-1. tag:0]; 207 233 … … 241 267 _acceptConnection = [[AsyncSocket allocWithZone:nil] initWithDelegate:self]; 242 268 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 ); 255 270 256 271 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] ); 260 273 [self _setPort:[_acceptConnection localPort]]; 261 274 … … 491 504 _acceptConnection = [[AsyncSocket allocWithZone:nil] initWithDelegate:self]; 492 505 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 ); 505 507 506 508 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] ); 510 510 [self _setPort:[_acceptConnection localPort]]; 511 511
