Changeset 1425
- Timestamp:
- Jan 21, 2007, 7:42:11 PM (15 years ago)
- Location:
- trunk/libtransmission
- Files:
-
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/fdlimit.c
r1420 r1425 102 102 for( j = 0; j < i; j++ ) 103 103 { 104 tr_netClose( s[j] ); 104 #ifdef BEOS_NETSERVER 105 closesocket( s[j] ); 106 #else 107 close( s[j] ); 108 #endif 105 109 } 106 110 … … 261 265 } 262 266 263 /*********************************************************************** 264 * tr_fdSocketWillCreate 265 **********************************************************************/ 266 int tr_fdSocketWillCreate( int reserved ) 267 { 268 int ret; 269 270 tr_lockLock( &gFd->lock ); 271 272 if( reserved ) 273 { 274 if( gFd->reserved < TR_RESERVED_FDS ) 275 { 276 ret = 0; 277 (gFd->reserved)++; 278 } 267 268 /*********************************************************************** 269 * Sockets 270 **********************************************************************/ 271 typedef struct 272 { 273 int socket; 274 int priority; 275 } 276 tr_socket_t; 277 278 /* Remember the priority of every socket we open, so that we can keep 279 * track of how many reserved file descriptors we are using */ 280 static tr_socket_t * gSockets = NULL; 281 static int gSocketsSize = 0; 282 static int gSocketsCount = 0; 283 static void SocketSetPriority( int s, int priority ) 284 { 285 if( gSocketsSize < 1 ) 286 { 287 gSocketsSize = 256; 288 gSockets = malloc( gSocketsSize * sizeof( tr_socket_t ) ); 289 } 290 if( gSocketsSize <= gSocketsCount ) 291 { 292 gSocketsSize *= 2; 293 gSockets = realloc( gSockets, gSocketsSize * sizeof( tr_socket_t ) ); 294 } 295 gSockets[gSocketsCount].socket = s; 296 gSockets[gSocketsCount].priority = priority; 297 gSocketsCount++; 298 } 299 static int SocketGetPriority( int s ) 300 { 301 int i, ret; 302 for( i = 0; i < gSocketsCount; i++ ) 303 if( gSockets[i].socket == s ) 304 break; 305 if( i >= gSocketsCount ) 306 { 307 tr_err( "could not find that socket (%d)!", s ); 308 return -1; 309 } 310 ret = gSockets[i].priority; 311 gSocketsCount--; 312 memmove( &gSockets[i], &gSockets[i+1], 313 ( gSocketsCount - i ) * sizeof( tr_socket_t ) ); 314 } 315 316 /*********************************************************************** 317 * tr_fdSocketCreate 318 **********************************************************************/ 319 int tr_fdSocketCreate( int type, int priority ) 320 { 321 int s = -1; 322 323 tr_lockLock( &gFd->lock ); 324 if( ( priority && gFd->reserved < TR_RESERVED_FDS ) || 325 ( !priority && gFd->normal < gFd->normalMax ) ) 326 { 327 if( ( s = socket( AF_INET, type, 0 ) ) < 0 ) 328 { 329 tr_err( "Could not create socket (%s)", strerror( errno ) ); 330 } 331 } 332 if( s > -1 ) 333 { 334 SocketSetPriority( s, priority ); 335 if( priority ) 336 gFd->reserved++; 279 337 else 280 { 281 ret = 1; 282 } 283 } 338 gFd->normal++; 339 } 340 tr_lockUnlock( &gFd->lock ); 341 342 return s; 343 } 344 345 int tr_fdSocketAccept( int b, struct in_addr * addr, in_port_t * port ) 346 { 347 int s = -1; 348 unsigned len; 349 struct sockaddr_in sock; 350 351 tr_lockLock( &gFd->lock ); 352 if( gFd->normal < gFd->normalMax ) 353 { 354 len = sizeof( sock ); 355 s = accept( b, (struct sockaddr *) &sock, &len ); 356 } 357 if( s > -1 ) 358 { 359 SocketSetPriority( s, 0 ); 360 *addr = sock.sin_addr; 361 *port = sock.sin_port; 362 gFd->normal++; 363 } 364 tr_lockUnlock( &gFd->lock ); 365 366 return s; 367 } 368 369 /*********************************************************************** 370 * tr_fdSocketClose 371 **********************************************************************/ 372 void tr_fdSocketClose( int s ) 373 { 374 tr_lockLock( &gFd->lock ); 375 #ifdef BEOS_NETSERVER 376 closesocket( s ); 377 #else 378 close( s ); 379 #endif 380 if( SocketGetPriority( s ) ) 381 gFd->reserved--; 284 382 else 285 { 286 if( gFd->normal < gFd->normalMax ) 287 { 288 ret = 0; 289 (gFd->normal)++; 290 } 291 else 292 { 293 ret = 1; 294 } 295 } 296 297 tr_lockUnlock( &gFd->lock ); 298 299 return ret; 300 } 301 302 /*********************************************************************** 303 * tr_fdSocketClosed 304 **********************************************************************/ 305 void tr_fdSocketClosed( int reserved ) 306 { 307 tr_lockLock( &gFd->lock ); 308 309 if( reserved ) 310 { 311 (gFd->reserved)--; 312 } 313 else 314 { 315 (gFd->normal)--; 316 } 317 383 gFd->normal--; 318 384 tr_lockUnlock( &gFd->lock ); 319 385 } -
trunk/libtransmission/fdlimit.h
r1420 r1425 59 59 60 60 /*********************************************************************** 61 * tr_fdSocketWillCreate61 * Sockets 62 62 **********************************************************************/ 63 int tr_fdSocketWillCreate( int ); 64 65 /*********************************************************************** 66 * tr_fdSocketClosed 67 **********************************************************************/ 68 void tr_fdSocketClosed( int ); 69 63 int tr_fdSocketCreate( int type, int priority ); 64 int tr_fdSocketAccept( int b, struct in_addr * addr, in_port_t * port ); 65 void tr_fdSocketClose( int s ); 70 66 71 67 /*********************************************************************** -
trunk/libtransmission/http.c
r1356 r1425 546 546 if( !tr_netResolve( http->host, &addr ) ) 547 547 { 548 http->sock = tr_netOpenTCP( addr, htons( http->port ) );548 http->sock = tr_netOpenTCP( addr, htons( http->port ), 1 ); 549 549 http->state = HTTP_STATE_CONNECT; 550 550 break; … … 568 568 tr_netResolveClose( http->resolve ); 569 569 http->resolve = NULL; 570 http->sock = tr_netOpenTCP( addr, htons( http->port ) );570 http->sock = tr_netOpenTCP( addr, htons( http->port ), 1 ); 571 571 http->state = HTTP_STATE_CONNECT; 572 572 } -
trunk/libtransmission/natpmp.c
r1420 r1425 472 472 tr_netClose( *fd ); 473 473 *fd = -1; 474 tr_fdSocketClosed( 0 );475 474 } 476 475 } … … 489 488 } 490 489 ret->fd = -1; 491 if( tr_fdSocketWillCreate( 0 ) ) 492 { 493 goto err; 494 } 495 ret->fd = tr_netOpenUDP( addr, htons( PMP_PORT ) ); 490 ret->fd = tr_netOpenUDP( addr, htons( PMP_PORT ), 1 ); 496 491 if( 0 > ret->fd ) 497 492 { … … 584 579 struct in_addr addr; 585 580 586 if( tr_fdSocketWillCreate( 0 ) )587 {588 return -1;589 }590 591 581 addr.s_addr = inet_addr( PMP_MCAST_ADDR ); 592 582 fd = tr_netMcastOpen( PMP_PORT, addr ); 593 583 if( 0 > fd ) 594 584 { 595 tr_fdSocketClosed( 0 );596 585 return -1; 597 586 } -
trunk/libtransmission/net.c
r1420 r1425 223 223 224 224 /*********************************************************************** 225 * TCP sockets225 * TCP/UDP sockets 226 226 **********************************************************************/ 227 227 … … 248 248 } 249 249 250 static int createSocket( int type )250 static int createSocket( int type, int priority ) 251 251 { 252 252 int s; 253 254 s = socket( AF_INET, type, 0 ); 255 if( s < 0 ) 256 { 257 tr_err( "Could not create socket (%s)", strerror( errno ) ); 258 return -1; 259 } 260 253 if( ( s = tr_fdSocketCreate( type, priority ) ) < 0 ) 254 { 255 return -1; 256 } 261 257 return makeSocketNonBlocking( s ); 262 258 } 263 259 264 int tr_netOpen( struct in_addr addr, in_port_t port, int type )260 int tr_netOpen( struct in_addr addr, in_port_t port, int type, int priority ) 265 261 { 266 262 int s; 267 263 struct sockaddr_in sock; 268 264 269 s = createSocket( type ); 270 if( s < 0 ) 265 if( ( s = createSocket( type, priority ) ) < 0 ) 271 266 { 272 267 return -1; … … 329 324 #endif 330 325 331 s = createSocket( type ); 332 if( s < 0 ) 326 if( ( s = createSocket( type, 1 ) ) < 0 ) 333 327 { 334 328 return -1; … … 364 358 } 365 359 366 int tr_netAccept( int s, struct in_addr * addr, in_port_t * port ) 367 { 368 int t; 369 unsigned len; 370 struct sockaddr_in sock; 371 372 len = sizeof( sock ); 373 t = accept( s, (struct sockaddr *) &sock, &len ); 374 375 if( t < 0 ) 376 { 377 return -1; 378 } 379 380 *addr = sock.sin_addr; 381 *port = sock.sin_port; 382 383 return makeSocketNonBlocking( t ); 360 int tr_netAccept( int b, struct in_addr * addr, in_port_t * port ) 361 { 362 int s; 363 if( ( s = tr_fdSocketAccept( b, addr, port ) ) < 0 ) 364 { 365 return -1; 366 } 367 return makeSocketNonBlocking( s ); 384 368 } 385 369 … … 432 416 void tr_netClose( int s ) 433 417 { 434 #ifdef BEOS_NETSERVER 435 closesocket( s ); 436 #else 437 close( s ); 438 #endif 418 tr_fdSocketClose( s ); 439 419 } 440 420 -
trunk/libtransmission/net.h
r920 r1425 38 38 39 39 /*********************************************************************** 40 * TCP sockets40 * TCP and UDP sockets 41 41 **********************************************************************/ 42 #define tr_netOpenTCP( addr, port ) tr_netOpen( (addr), (port), SOCK_STREAM ) 43 #define tr_netOpenUDP( addr, port ) tr_netOpen( (addr), (port), SOCK_DGRAM ) 44 int tr_netOpen ( struct in_addr addr, in_port_t port, int type ); 42 #define tr_netOpenTCP( addr, port, priority ) \ 43 tr_netOpen( (addr), (port), SOCK_STREAM, (priority) ) 44 #define tr_netOpenUDP( addr, port, priority ) \ 45 tr_netOpen( (addr), (port), SOCK_DGRAM, (priority) ) 46 int tr_netOpen ( struct in_addr addr, in_port_t port, int type, 47 int priority ); 45 48 int tr_netMcastOpen( int port, struct in_addr addr ); 46 49 #define tr_netBindTCP( port ) tr_netBind( (port), SOCK_STREAM ) -
trunk/libtransmission/peer.c
r1424 r1425 186 186 { 187 187 tr_netClose( peer->socket ); 188 tr_fdSocketClosed( 0 );189 188 } 190 189 tr_rcClose( peer->download ); … … 315 314 316 315 /* Connect */ 317 if( ( peer->status & PEER_STATUS_IDLE ) && 318 !tr_fdSocketWillCreate( 0 ) ) 319 { 320 peer->socket = tr_netOpenTCP( peer->addr, peer->port ); 316 if( peer->status & PEER_STATUS_IDLE ) 317 { 318 peer->socket = tr_netOpenTCP( peer->addr, peer->port, 0 ); 321 319 if( peer->socket < 0 ) 322 320 { 323 peer_dbg( "connection failed" );324 tr_fdSocketClosed( 0 );325 321 return TR_ERROR; 326 322 } … … 333 329 uint8_t buf[68]; 334 330 tr_info_t * inf = &tor->info; 335 int ret;336 331 337 332 buf[0] = 19; -
trunk/libtransmission/peerutils.h
r1424 r1425 59 59 static int checkPeer( tr_peer_t * peer ) 60 60 { 61 tr_torrent_t * tor = peer->tor;62 63 61 if( peer->status < PEER_STATUS_CONNECTED && 64 62 tr_date() > peer->date + 8000 ) -
trunk/libtransmission/shared.c
r1420 r1425 105 105 { 106 106 tr_netClose( s->bindSocket ); 107 tr_fdSocketClosed( 0 );108 107 } 109 108 tr_lockClose( &s->lock ); … … 157 156 { 158 157 tr_netClose( s->bindSocket ); 159 tr_fdSocketClosed( 0 );160 158 } 161 159 162 160 /* Create the new one */ 163 if( !tr_fdSocketWillCreate( 0 ) ) 164 { 165 /* XXX should handle failure here in a better way */ 166 s->bindSocket = tr_netBindTCP( port ); 167 if( 0 > s->bindSocket ) 168 { 169 tr_fdSocketClosed( 0 ); 170 } 171 else 172 { 173 tr_inf( "Bound listening port %d", port ); 174 listen( s->bindSocket, 5 ); 175 } 161 /* XXX should handle failure here in a better way */ 162 s->bindSocket = tr_netBindTCP( port ); 163 if( s->bindSocket >= 0 ) 164 { 165 tr_inf( "Bound listening port %d", port ); 166 listen( s->bindSocket, 5 ); 176 167 } 177 168 … … 312 303 for( ;; ) 313 304 { 314 if( s->bindSocket < 0 || s->peerCount >= MAX_PEER_COUNT || 315 tr_fdSocketWillCreate( 0 ) ) 316 { 317 break;; 305 if( s->bindSocket < 0 || s->peerCount >= MAX_PEER_COUNT ) 306 { 307 break; 318 308 } 319 309 … … 321 311 if( socket < 0 ) 322 312 { 323 tr_fdSocketClosed( 0 );324 313 break; 325 314 } -
trunk/libtransmission/tracker.c
r1420 r1425 346 346 tc->randOffset = tr_rand( 60000 ); 347 347 348 if( tr_fdSocketWillCreate( 1 ) )349 {350 return;351 }352 348 tc->dateTry = tr_date(); 353 349 … … 460 456 if( ( NULL == tc->httpScrape ) && shouldScrape( tc ) ) 461 457 { 462 if( tr_fdSocketWillCreate( 1 ) )463 {464 return;465 }466 458 tc->dateScrape = tr_date(); 467 459 … … 1166 1158 { 1167 1159 tr_httpClose( *http ); 1168 tr_fdSocketClosed( 1 );1169 1160 *http = NULL; 1170 1161 } -
trunk/libtransmission/upnp.c
r1420 r1425 335 335 if( 0 > fd ) 336 336 { 337 if( tr_fdSocketWillCreate( 0 ) )338 {339 return -1;340 }341 337 fd = tr_netBindUDP( 0 ); 342 338 if( 0 > fd ) 343 339 { 344 tr_fdSocketClosed( 0 );345 340 return -1; 346 341 } … … 387 382 struct in_addr addr; 388 383 389 if( tr_fdSocketWillCreate( 0 ) )390 {391 return -1;392 }393 394 384 addr.s_addr = inet_addr( SSDP_ADDR ); 395 385 fd = tr_netMcastOpen( SSDP_PORT, addr ); 396 386 if( 0 > fd ) 397 387 { 398 tr_fdSocketClosed( 0 );399 388 return -1; 400 389 } … … 410 399 tr_netClose( *sock ); 411 400 *sock = -1; 412 tr_fdSocketClosed( 0 );413 401 } 414 402 } … … 419 407 tr_httpClose( *http ); 420 408 *http = NULL; 421 tr_fdSocketClosed( 0 );422 409 } 423 410 … … 850 837 tr_http_t * ret; 851 838 char numstr[6]; 852 853 if( tr_fdSocketWillCreate( 0 ) )854 {855 return NULL;856 }857 839 858 840 ret = NULL; … … 914 896 } 915 897 916 if( NULL == ret )917 {918 tr_fdSocketClosed( 0 );919 }920 921 898 return ret; 922 899 }
Note: See TracChangeset
for help on using the changeset viewer.