Changeset 4356
- Timestamp:
- Dec 27, 2007, 9:48:41 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/tracker.c
r4324 r4356 662 662 snprintf( buf, sizeof(buf), "%s:%d", address->address, address->port ); 663 663 evhttp_add_header( req->output_headers, "Host", buf ); 664 evhttp_add_header( req->output_headers, "Connection", "close" );665 evhttp_add_header( req->output_headers, "Content-Length", "0" );666 664 evhttp_add_header( req->output_headers, "User-Agent", 667 665 TR_NAME "/" LONG_VERSION_STRING ); … … 763 761 struct tr_tracker_handle 764 762 { 765 int socketCount;766 763 unsigned int isShuttingDown : 1; 767 764 tr_timer * pulseTimer; 768 765 tr_list * requestQueue; 769 766 tr_list * scrapeQueue; 767 tr_list * connectionPool; 770 768 }; 771 769 … … 807 805 808 806 if( globalsExist 809 && ( handle->tracker-> socketCount < 1)807 && ( handle->tracker->connectionPool == NULL ) 810 808 && ( handle->tracker->requestQueue == NULL ) 811 809 && ( handle->tracker->scrapeQueue == NULL ) … … 832 830 return FALSE; 833 831 } 832 833 static int 834 connectionPoolCompare( const void * a, const void * b ) 835 { 836 return a != b; 837 } 838 834 839 static void 835 840 connectionClosedCB( struct evhttp_connection * evcon, void * vhandle ) 836 841 { 842 char * address; 843 unsigned short port; 837 844 tr_handle * handle = vhandle; 845 846 evhttp_connection_get_peer( evcon, &address, &port ); 847 dbgmsg( NULL, "%s:%d evcon [%s:%d] has closed... removing from pool", __FILE__, __LINE__, address, port ); 848 849 tr_list_remove( &handle->tracker->connectionPool, 850 evcon, 851 connectionPoolCompare ); 838 852 839 853 /* libevent references evcon right after calling this function, … … 844 858 845 859 static struct evhttp_connection* 860 getExistingConnection( tr_handle * handle, const char * address, int port ) 861 { 862 tr_list * l; 863 dbgmsg( NULL, "%s:%d looking for a connection to {%s:%d}\n", __FILE__, __LINE__, address, port ); 864 865 for( l=handle->tracker->connectionPool; l!=NULL; l=l->next ) 866 { 867 char * c_address; 868 unsigned short c_port; 869 evhttp_connection_get_peer( l->data, &c_address, &c_port ); 870 dbgmsg( NULL, "comparing with [%s:%d]...", c_address, c_port ); 871 if( ( port == c_port ) && !strcmp( address, c_address ) ) { 872 dbgmsg( NULL, "%s:%d reusing cached connection of [%s:%d]\n", __FILE__, __LINE__, address, port ); 873 return l->data; 874 } 875 } 876 877 dbgmsg( NULL, "%s:%d no match.\n", __FILE__, __LINE__ ); 878 return NULL; 879 } 880 881 static struct evhttp_connection* 846 882 getConnection( tr_handle * handle, const char * address, int port ) 847 883 { 848 struct evhttp_connection * c = evhttp_connection_new( address, port ); 849 evhttp_connection_set_closecb( c, connectionClosedCB, handle ); 884 struct evhttp_connection * c = getExistingConnection( handle, address, port ); 885 886 if( c == NULL ) 887 { 888 dbgmsg( NULL, "%s:%d creating new connection for [%s:%d]\n", __FILE__, __LINE__, address, port ); 889 c = evhttp_connection_new( address, port ); 890 evhttp_connection_set_closecb( c, connectionClosedCB, handle ); 891 tr_list_prepend( &handle->tracker->connectionPool, c ); 892 } 893 850 894 return c; 851 895 } … … 860 904 if( evhttp_make_request( evcon, req->req, EVHTTP_REQ_GET, req->uri )) 861 905 publishErrorMessageAndStop( t, "Tracker could not be reached." ); 862 else {863 ++handle->tracker->socketCount;864 dbgmsg( t, "incremented socket count to %d", handle->tracker->socketCount );865 }866 906 } 867 907 … … 872 912 invokeRequest( handle, req ); 873 913 freeRequest( req ); 874 }875 876 static int877 socketIsAvailable( tr_handle * handle )878 {879 const int max = handle->tracker->isShuttingDown880 ? MAX_TRACKER_SOCKETS_DURING_SHUTDOWN881 : MAX_TRACKER_SOCKETS;882 return handle->tracker->socketCount < max;883 914 } 884 915 … … 914 945 return FALSE; 915 946 916 if( handle->tracker->socketCount|| tr_list_size(th->requestQueue) || tr_list_size(th->scrapeQueue) )917 dbgmsg( NULL, "tracker pulse... %d sockets, %d reqs left, %d scrapes left", handle->tracker->socketCount, tr_list_size(th->requestQueue), tr_list_size(th->scrapeQueue) );947 if( tr_list_size(th->connectionPool) || tr_list_size(th->requestQueue) || tr_list_size(th->scrapeQueue) ) 948 dbgmsg( NULL, "tracker pulse... %d sockets, %d reqs left, %d scrapes left", tr_list_size(th->connectionPool), tr_list_size(th->requestQueue), tr_list_size(th->scrapeQueue) ); 918 949 919 950 /* upkeep: queue periodic rescrape / reannounce */ … … 933 964 } 934 965 935 if( handle->tracker->socketCount|| tr_list_size(th->requestQueue) || tr_list_size(th->scrapeQueue) )936 dbgmsg( NULL, "tracker pulse after upkeep... %d sockets, %d reqs left, %d scrapes left", handle->tracker->socketCount, tr_list_size(th->requestQueue), tr_list_size(th->scrapeQueue) );966 if( tr_list_size(th->connectionPool) || tr_list_size(th->requestQueue) || tr_list_size(th->scrapeQueue) ) 967 dbgmsg( NULL, "tracker pulse after upkeep... %d sockets, %d reqs left, %d scrapes left", tr_list_size(th->connectionPool), tr_list_size(th->requestQueue), tr_list_size(th->scrapeQueue) ); 937 968 938 969 /* look for things to do... process all the requests, then process all the scrapes */ 939 while( th->requestQueue && socketIsAvailable( handle ))970 while( th->requestQueue ) 940 971 invokeNextInQueue( handle, &th->requestQueue ); 941 while( th->scrapeQueue && socketIsAvailable( handle ))972 while( th->scrapeQueue ) 942 973 invokeNextInQueue( handle, &th->scrapeQueue ); 943 974 944 if( handle->tracker->socketCount|| tr_list_size(th->requestQueue) || tr_list_size(th->scrapeQueue) )945 dbgmsg( NULL, "tracker pulse done... %d sockets, %d reqs left, %d scrapes left", handle->tracker->socketCount, tr_list_size(th->requestQueue), tr_list_size(th->scrapeQueue) );975 if( tr_list_size(th->connectionPool) || tr_list_size(th->requestQueue) || tr_list_size(th->scrapeQueue) ) 976 dbgmsg( NULL, "tracker pulse done... %d sockets, %d reqs left, %d scrapes left", tr_list_size(th->connectionPool), tr_list_size(th->requestQueue), tr_list_size(th->scrapeQueue) ); 946 977 947 978 return maybeFreeGlobals( handle ); … … 954 985 { 955 986 pulse( handle ); 956 --handle->tracker->socketCount;957 dbgmsg( NULL, "decrementing socket count to %d", handle->tracker->socketCount );958 987 } 959 988 }
Note: See TracChangeset
for help on using the changeset viewer.