Changeset 12378


Ignore:
Timestamp:
Apr 24, 2011, 7:12:28 PM (11 years ago)
Author:
jch
Message:

Optimise UDP dispatch (fixes #4209).

Make quick inline tests for the DHT and UDP tracker protocols, which
avoids calling tau_handle_message for each µTP packet.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/tr-udp.c

    r12274 r12378  
    206206    rc = recvfrom(s, buf, 4096 - 1, 0,
    207207                  (struct sockaddr*)&from, &fromlen);
     208
     209    /* Since most packets we receive here are µTP, make quick inline
     210       checks for the other protocols.  The logic is as follows:
     211       - all DHT packets start with 'd';
     212       - all UDP tracker packets start with a 32-bit (!) "action", which
     213         is between 0 and 3;
     214       - the above cannot be µTP packets, since these start with a 4-bit
     215         version number (1). */
    208216    if(rc > 0) {
    209         if( tau_handle_message( ss, buf, rc ) ) {
    210             tr_ndbg("UDP", "Received UDP Tracker packet");
    211         }
    212         else if( buf[0] == 'd' ) {
    213             /* DHT packet. */
    214             buf[rc] = '\0';
     217        if( buf[0] == 'd' ) {
     218            buf[rc] = '\0';     /* required by the DHT code */
    215219            tr_dhtCallback(buf, rc, (struct sockaddr*)&from, fromlen, sv);
     220        } else if( rc >= 8 &&
     221                   buf[0] == 0 && buf[1] == 0 && buf[2] == 0 && buf[3] <= 3 ) {
     222            rc = tau_handle_message( ss, buf, rc );
     223            if( !rc )
     224                tr_ndbg("UDP", "Couldn't parse UDP tracker packet.");
    216225        } else {
    217226            rc = tr_utpPacket(buf, rc, (struct sockaddr*)&from, fromlen, ss);
    218             if(!rc)
     227            if( !rc )
    219228                tr_ndbg("UDP", "Unexpected UDP packet");
    220229        }
Note: See TracChangeset for help on using the changeset viewer.