(trunk libT) #4209 "Shortcut UDP tracker test" -- prioritize the UDP handler functions based on frequency (uTP, DHT, UTP tracker) as outlined by jch in comment:5. Also, don't call the uTP or DHT handlers when uTP or DHT is disabled in the system preferences.

To avoid the function call overhead described by jch, instead of calling tr_sessionIsUTPEnabled() and tr_sessionIsDHTEnabled(), we test for WITH_UTP to be defined and test the tr_session.isUTPEnabled and tr_session.isDHTEnabled flags directly.
This commit is contained in:
Jordan Lee 2011-04-27 05:29:05 +00:00
parent ea3fe42ca3
commit 018b673acd
1 changed files with 27 additions and 23 deletions

View File

@ -187,11 +187,11 @@ rebind_ipv6(tr_session *ss, bool force)
static void
event_callback(int s, short type UNUSED, void *sv)
{
tr_session *ss = sv;
int rc;
socklen_t fromlen;
unsigned char buf[4096];
struct sockaddr_storage from;
socklen_t fromlen;
int rc;
tr_session *ss = sv;
assert(tr_isSession(sv));
assert(type == EV_READ);
@ -200,26 +200,30 @@ event_callback(int s, short type UNUSED, void *sv)
rc = recvfrom(s, buf, 4096 - 1, 0,
(struct sockaddr*)&from, &fromlen);
/* Since most packets we receive here are µTP, make quick inline
checks for the other protocols. The logic is as follows:
- all DHT packets start with 'd';
- all UDP tracker packets start with a 32-bit (!) "action", which
is between 0 and 3;
- the above cannot be µTP packets, since these start with a 4-bit
version number (1). */
if(rc > 0) {
if( buf[0] == 'd' ) {
buf[rc] = '\0'; /* required by the DHT code */
tr_dhtCallback(buf, rc, (struct sockaddr*)&from, fromlen, sv);
} else if( rc >= 8 &&
buf[0] == 0 && buf[1] == 0 && buf[2] == 0 && buf[3] <= 3 ) {
rc = tau_handle_message( ss, buf, rc );
if( !rc )
tr_ndbg("UDP", "Couldn't parse UDP tracker packet.");
} else {
rc = tr_utpPacket(buf, rc, (struct sockaddr*)&from, fromlen, ss);
if( !rc )
tr_ndbg("UDP", "Unexpected UDP packet");
/* Test for the different packet types based on frequency:
µTP will be the most common, then DHT, then UDP trackers. */
if( rc > 0 )
{
#ifdef WITH_UTP
if( ss->isUTPEnabled && tr_utpPacket( buf, rc, (struct sockaddr*)&from, fromlen, ss ) )
{
tr_ndbg( "UDP", "Handled uTP packet" );
}
else
#endif
if( ss->isDHTEnabled && ( *buf == 'd' ) ) /* DHT */
{
buf[rc] = '\0'; /* required by the DHT code */
tr_dhtCallback( buf, rc, (struct sockaddr*)&from, fromlen, sv );
tr_ndbg( "UDP", "Handled DHT packet" );
}
else if( tau_handle_message( ss, buf, rc ) )
{
tr_ndbg( "UDP", "Handled UDP tracker packet" );
}
else
{
tr_ndbg( "UDP", "Unexpected UDP packet" );
}
}
}