Changeset 11657


Ignore:
Timestamp:
Jan 9, 2011, 9:48:46 PM (11 years ago)
Author:
jch
Message:

Move handling of UDP I/O to tr-udp.c.

Location:
trunk/libtransmission
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/session.h

    r11654 r11657  
    131131    int                          udp6_socket;
    132132    unsigned char *              udp6_bound;
     133    struct event                 *udp_event;
     134    struct event                 *udp6_event;
    133135
    134136    /* The open port on the local machine for incoming peer requests */
  • trunk/libtransmission/tr-dht.c

    r11656 r11657  
    6060#include "utils.h"
    6161
    62 static struct event dht_event, dht6_event;
     62static struct event dht_timer;
    6363static unsigned char myid[20];
    6464static tr_session *session = NULL;
    6565
    66 static void event_callback(int s, short type, void *ignore);
     66static void timer_callback(int s, short type, void *ignore);
    6767
    6868struct bootstrap_closure {
     
    316316    tr_threadNew( dht_bootstrap, cl );
    317317
    318     event_set( &dht_event, ss->udp_socket, EV_READ, event_callback, NULL );
    319     tr_timerAdd( &dht_event, 0, tr_cryptoWeakRandInt( 1000000 ) );
    320 
    321     if( ss->udp6_socket >= 0 )
    322     {
    323         event_set( &dht6_event, ss->udp6_socket, EV_READ, event_callback, NULL );
    324         tr_timerAdd( &dht6_event, 0, tr_cryptoWeakRandInt( 1000000 ) );
    325     }
     318    evtimer_set( &dht_timer, timer_callback, session );
     319    tr_timerAdd( &dht_timer, 0, tr_cryptoWeakRandInt( 1000000 ) );
    326320
    327321    tr_ndbg( "DHT", "DHT initialized" );
     
    343337    tr_ndbg( "DHT", "Uninitializing DHT" );
    344338
    345     event_free( dht_event );
    346     dht_event = NULL;
    347 
    348     if( ss->udp6_socket >= 0 )
    349         event_del( &dht6_event );
     339    event_del( &dht_timer );
    350340
    351341    /* Since we only save known good nodes, avoid erasing older data if we
     
    591581}
    592582
    593 static void
    594 event_callback(int s, short type, void *ignore UNUSED )
    595 {
    596     struct event *event;
     583void
     584tr_dhtCallback(unsigned char *buf, int buflen,
     585               struct sockaddr *from, socklen_t fromlen,
     586               void *sv )
     587{
     588    tr_session *ss = (tr_session*)sv;
    597589    time_t tosleep;
    598     struct sockaddr_storage from;
    599     socklen_t fromlen;
    600     unsigned char *buf = NULL;
    601590    int rc;
    602591
    603     if (s == session->udp_socket)
    604         event =  &dht_event;
    605     else if(s == session->udp6_socket)
    606         event = &dht6_event;
    607     else {
    608         tr_nerr("DHT", "Event on unexpected socket");
    609         event = NULL;
    610     }
    611 
    612     if( type == EV_READ ) {
    613         buf = malloc(4096);
    614         if(buf != NULL) {
    615             fromlen = sizeof(from);
    616             rc = recvfrom(s, buf, 4096 - 1, 0,
    617                           (struct sockaddr*)&from, &fromlen);
    618             if(rc < 0)
    619                 rc = 0;
    620             else
    621                 buf[rc] = 0;
    622         }
    623     } else {
    624         rc = 0;
    625         fromlen = 0;
    626     }
    627 
    628     if( dht_periodic( buf, rc, (struct sockaddr*)&from, fromlen,
    629                       &tosleep, callback, NULL) < 0 ) {
     592    assert(tr_isSession(ss));
     593
     594    if(sv != session) {
     595        tr_nerr("DHT", "tr_dhtCallback called for unexpected session");
     596        return;
     597    }
     598
     599    rc = dht_periodic( buf, buflen, from, fromlen,
     600                       &tosleep, callback, NULL);
     601    if(rc < 0) {
    630602        if(errno == EINTR) {
    631603            tosleep = 0;
     
    638610    }
    639611
    640     if(buf) {
    641         free(buf);
    642         buf = NULL;
    643     }
    644 
    645 #ifdef NOTYET
    646     /* Only do this once in a while.  Counting rather than measuring time
    647        avoids a system call. */
    648     count++;
    649     if(count >= 20) {
    650         rebind_ipv6(FALSE);
    651         count = 0;
    652     }
    653 #endif
    654 
    655     if(event) {
    656         /* Being slightly late is fine,
    657            and has the added benefit of adding some jitter. */
    658         tr_timerAdd( event, tosleep, tr_cryptoWeakRandInt( 1000000 ) );
    659     }
    660 }
     612    /* Being slightly late is fine,
     613       and has the added benefit of adding some jitter. */
     614    tr_timerAdd( &dht_timer, tosleep, tr_cryptoWeakRandInt( 1000000 ) );
     615}
     616
     617static void
     618timer_callback(int s UNUSED, short type UNUSED, void *session )
     619{
     620    tr_dhtCallback(NULL, 0, NULL, 0, session);
     621}
     622
    661623
    662624void
  • trunk/libtransmission/tr-dht.h

    r11654 r11657  
    4545int tr_dhtAddNode( tr_session *, const tr_address *, tr_port, tr_bool bootstrap );
    4646int tr_dhtAnnounce( tr_torrent *, int af, tr_bool announce );
     47void tr_dhtCallback(unsigned char *buf, int buflen,
     48                    struct sockaddr *from, socklen_t fromlen,
     49                    void *sv);
  • trunk/libtransmission/tr-udp.c

    r11654 r11657  
    2424#include <unistd.h>
    2525#include <assert.h>
     26
     27#include <event.h>
    2628
    2729#include "transmission.h"
     
    104106}
    105107
     108static void
     109event_callback(int s, short type, void *sv)
     110{
     111    tr_session *ss = (tr_session*)sv;
     112    unsigned char *buf;
     113    struct sockaddr_storage from;
     114    socklen_t fromlen;
     115    int rc;
     116
     117    assert(tr_isSession(ss));
     118    assert(type == EV_READ);
     119
     120    buf = malloc(4096);
     121    if(buf == NULL) {
     122        tr_nerr("UDP", "Couldn't allocate buffer");
     123        return;
     124    }
     125
     126    fromlen = sizeof(from);
     127    rc = recvfrom(s, buf, 4096 - 1, 0,
     128                  (struct sockaddr*)&from, &fromlen);
     129    if(rc <= 0)
     130        return;
     131
     132    if(buf[0] == 'd') {
     133        /* DHT packet. */
     134        buf[rc] = '\0';
     135        tr_dhtCallback(buf, rc, (struct sockaddr*)&from, fromlen, sv);
     136    } else {
     137        /* Probably a UTP packet. */
     138        /* Nothing yet. */
     139    }
     140
     141    free(buf);
     142}   
     143
    106144void
    107145tr_udpInit(tr_session *ss, const tr_address * addr)
     
    120158    if(ss->udp_socket < 0) {
    121159        tr_nerr("UDP", "Couldn't create IPv4 socket");
     160        goto ipv6;
     161    }
     162
     163    ss->udp_event = tr_new0(struct event, 1);
     164    if(ss->udp_event == NULL) {
     165        tr_nerr("UDP", "Couldn't allocate IPv4 event");
     166        close(ss->udp_socket);
     167        ss->udp_socket = -1;
    122168        goto ipv6;
    123169    }
     
    135181    }
    136182
     183    event_set(ss->udp_event, ss->udp_socket, EV_READ | EV_PERSIST,
     184              event_callback, ss);
     185
    137186 ipv6:
    138     if(tr_globalIPv6())
    139         rebind_ipv6(ss, TRUE);
     187    ss->udp6_event = tr_new0(struct event, 1);
     188    if(ss->udp6_event == NULL) {
     189        tr_nerr("UDP", "Couldn't allocate IPv6 event");
     190    } else {
     191        if(tr_globalIPv6())
     192            rebind_ipv6(ss, TRUE);
     193        if(ss->udp6_socket >= 0)
     194            event_set(ss->udp6_event, ss->udp6_socket, EV_READ | EV_PERSIST,
     195                      event_callback, ss);
     196    }
    140197
    141198    if(ss->isDHTEnabled)
    142199        tr_dhtInit(ss);
     200
     201    if(ss->udp_event)
     202        event_add(ss->udp_event, NULL);
     203    if(ss->udp6_event)
     204        event_add(ss->udp6_event, NULL);
    143205}
    144206
     
    153215    }
    154216
     217    if(ss->udp_event) {
     218        event_del(ss->udp_event);
     219        free(ss->udp_event);
     220        ss->udp_event = NULL;
     221    }
     222
    155223    if(ss->udp6_socket >= 0) {
    156224        tr_netCloseSocket( ss->udp6_socket );
     
    158226    }
    159227
     228    if(ss->udp6_event) {
     229        event_del(ss->udp6_event);
     230        free(ss->udp6_event);
     231        ss->udp6_event = NULL;
     232    }
     233
    160234    if(ss->udp6_bound) {
    161235        free(ss->udp6_bound);
Note: See TracChangeset for help on using the changeset viewer.