Changeset 3018


Ignore:
Timestamp:
Sep 10, 2007, 3:12:11 AM (14 years ago)
Author:
charles
Message:

BANG! another thread, and 300 lines of code, go away and are replaced by libevent's DNS resolution.

Location:
branches/encryption/libtransmission
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/encryption/libtransmission/fdlimit.c

    r2596 r3018  
    391391void tr_fdSocketClose( int s )
    392392{
    393     tr_lockLock( gFd->lock );
     393    if( s >= 0 )
     394    {
     395        tr_lockLock( gFd->lock );
    394396#ifdef BEOS_NETSERVER
    395     closesocket( s );
     397        closesocket( s );
    396398#else
    397     close( s );
     399        close( s );
    398400#endif
    399     if( SocketGetPriority( s ) )
    400         gFd->reserved--;
    401     else
    402         gFd->normal--;
    403     tr_lockUnlock( gFd->lock );
     401        if( SocketGetPriority( s ) )
     402            gFd->reserved--;
     403        else
     404            gFd->normal--;
     405        tr_lockUnlock( gFd->lock );
     406    }
    404407}
    405408
  • branches/encryption/libtransmission/http.c

    r2686 r3018  
    3232#include <sys/types.h>
    3333
     34#include "evdns.h"
     35
    3436#ifdef __BEOS__
    3537extern int vasprintf( char **, const char *, va_list );
     
    8587#define HTTP_LENGTH_CHUNKED     4
    8688    char           lengthtype;
    87     tr_resolve_t * resolve;
    8889    char         * host;
    8990    int            port;
     
    568569}
    569570
     571static void
     572resolve_cb( int result, char type, int count, int ttl, void *addresses, void *arg)
     573{
     574    tr_http_t * http = (tr_http_t *) arg;
     575
     576    if( (result!=DNS_ERR_NONE) || (type!=DNS_IPv4_A) || (ttl<0) || (count<1) )
     577        http->state = HTTP_STATE_ERROR;
     578    else {
     579         struct in_addr *in_addrs = addresses;
     580         http->sock = tr_netOpenTCP( &in_addrs[0], htons(http->port), 1 );
     581         http->state = HTTP_STATE_CONNECT;
     582    }
     583}
     584
    570585tr_tristate_t
    571586tr_httpPulse( tr_http_t * http, const char ** data, int * len )
     
    587602                break;
    588603            }
    589             http->resolve = tr_netResolveInit( http->host );
    590             if( NULL == http->resolve )
     604            if( evdns_resolve_ipv4( http->host, 0, resolve_cb, http ) )
    591605            {
    592606                goto err;
     
    596610
    597611        case HTTP_STATE_RESOLVE:
    598             switch( tr_netResolvePulse( http->resolve, &addr ) )
    599             {
    600                 case TR_NET_WAIT:
    601                     return TR_NET_WAIT;
    602                 case TR_NET_ERROR:
    603                     goto err;
    604                 case TR_NET_OK:
    605                     tr_netResolveClose( http->resolve );
    606                     http->resolve = NULL;
    607                     http->sock = tr_netOpenTCP( &addr, htons( http->port ), 1 );
    608                     http->state = HTTP_STATE_CONNECT;
    609             }
    610             /* fallthrough */
     612            return TR_NET_WAIT;
    611613
    612614        case HTTP_STATE_CONNECT:
     
    898900tr_httpClose( tr_http_t * http )
    899901{
    900     if( NULL != http->resolve )
    901     {
    902         tr_netResolveClose( http->resolve );
    903     }
    904902    free( http->host );
    905     if( 0 <= http->sock )
    906     {
    907         tr_netClose( http->sock );
    908     }
     903    tr_netClose( http->sock );
    909904    free( http->header.buf );
    910905    free( http->body.buf );
  • branches/encryption/libtransmission/net.c

    r2614 r3018  
    5858/***********************************************************************
    5959 * DNS resolution
    60  **********************************************************************/
    61 
    62 /***********************************************************************
    63  * tr_netResolve
    64  ***********************************************************************
     60 *
    6561 * Synchronous "resolution": only works with character strings
    6662 * representing numbers expressed in the Internet standard `.' notation.
     
    7167    addr->s_addr = inet_addr( address );
    7268    return ( addr->s_addr == 0xFFFFFFFF );
    73 }
    74 
    75 static tr_thread_t  * resolveThread;
    76 static tr_lock_t    * resolveLock;
    77 static tr_cond_t    * resolveCond;
    78 static volatile int   resolveDie;
    79 static tr_resolve_t * resolveQueue;
    80 
    81 static void resolveRelease ( tr_resolve_t * );
    82 static void resolveFunc    ( void * );
    83 
    84 struct tr_resolve_s
    85 {
    86     tr_tristate_t  status;
    87     char           * address;
    88     struct in_addr addr;
    89 
    90     int            refcount;
    91     tr_resolve_t   * next;
    92 };
    93 
    94 /***********************************************************************
    95  * tr_netResolveThreadInit
    96  ***********************************************************************
    97  * Initializes the static variables used for resolution and launch the
    98  * gethostbyname thread.
    99  **********************************************************************/
    100 void tr_netResolveThreadInit( void )
    101 {
    102     resolveDie   = 0;
    103     resolveQueue = NULL;
    104     resolveLock = tr_lockNew( );
    105     resolveCond = tr_condNew( );
    106     resolveThread = tr_threadNew( resolveFunc, NULL, "resolve" );
    107 }
    108 
    109 /***********************************************************************
    110  * tr_netResolveThreadClose
    111  ***********************************************************************
    112  * Notices the gethostbyname thread that is should terminate. Doesn't
    113  * wait until it does, in case it is stuck in a resolution: we let it
    114  * die and clean itself up.
    115  **********************************************************************/
    116 void tr_netResolveThreadClose( void )
    117 {
    118     tr_lockLock( resolveLock );
    119     resolveDie = 1;
    120     tr_lockUnlock( resolveLock );
    121     tr_condSignal( resolveCond );
    122     tr_wait( 200 );
    123 }
    124 
    125 /***********************************************************************
    126  * tr_netResolveInit
    127  ***********************************************************************
    128  * Adds an address to the resolution queue.
    129  **********************************************************************/
    130 tr_resolve_t * tr_netResolveInit( const char * address )
    131 {
    132     tr_resolve_t * r = tr_new0( tr_resolve_t, 1 );
    133     r->status   = TR_NET_WAIT;
    134     r->address  = strdup( address );
    135     r->refcount = 2;
    136     r->next     = NULL;
    137 
    138     tr_lockLock( resolveLock );
    139     if( !resolveQueue )
    140     {
    141         resolveQueue = r;
    142     }
    143     else
    144     {
    145         tr_resolve_t * iter;
    146         for( iter = resolveQueue; iter->next; iter = iter->next );
    147         iter->next = r;
    148     }
    149     tr_lockUnlock( resolveLock );
    150     tr_condSignal( resolveCond );
    151 
    152     return r;
    153 }
    154 
    155 /***********************************************************************
    156  * tr_netResolvePulse
    157  ***********************************************************************
    158  * Checks the current status of a resolution.
    159  **********************************************************************/
    160 tr_tristate_t tr_netResolvePulse( tr_resolve_t * r, struct in_addr * addr )
    161 {
    162     tr_tristate_t ret;
    163 
    164     tr_lockLock( resolveLock );
    165     ret = r->status;
    166     if( ret == TR_NET_OK )
    167     {
    168         *addr = r->addr;
    169     }
    170     tr_lockUnlock( resolveLock );
    171 
    172     return ret;
    173 }
    174 
    175 /***********************************************************************
    176  * tr_netResolveClose
    177  ***********************************************************************
    178  *
    179  **********************************************************************/
    180 void tr_netResolveClose( tr_resolve_t * r )
    181 {
    182     resolveRelease( r );
    183 }
    184 
    185 /***********************************************************************
    186  * resolveRelease
    187  ***********************************************************************
    188  * The allocated tr_resolve_t structures should be freed when
    189  * tr_netResolveClose was called *and* it was removed from the queue.
    190  * This can happen in any order, so we use a refcount to know we can
    191  * take it out.
    192  **********************************************************************/
    193 static void resolveRelease( tr_resolve_t * r )
    194 {
    195     if( --r->refcount < 1 )
    196     {
    197         free( r->address );
    198         free( r );
    199     }
    200 }
    201 
    202 /***********************************************************************
    203  * resolveFunc
    204  ***********************************************************************
    205  * Keeps waiting for addresses to resolve, and removes them from the
    206  * queue once resolution is done.
    207  **********************************************************************/
    208 static void resolveFunc( void * arg UNUSED )
    209 {
    210     tr_resolve_t * r;
    211     struct hostent * host;
    212 
    213     tr_lockLock( resolveLock );
    214 
    215     while( !resolveDie )
    216     {
    217         if( !( r = resolveQueue ) )
    218         {
    219             tr_condWait( resolveCond, resolveLock );
    220             continue;
    221         }
    222 
    223         /* Blocking resolution */
    224         tr_lockUnlock( resolveLock );
    225         host = gethostbyname( r->address );
    226         tr_lockLock( resolveLock );
    227 
    228         if( host )
    229         {
    230             memcpy( &r->addr, host->h_addr, host->h_length );
    231             r->status = TR_NET_OK;
    232         }
    233         else
    234         {
    235             r->status = TR_NET_ERROR;
    236         }
    237        
    238         resolveQueue = r->next;
    239         resolveRelease( r );
    240     }
    241 
    242     /* Clean up  */
    243     tr_lockUnlock( resolveLock );
    244     tr_lockFree( resolveLock );
    245     resolveLock = NULL;
    246     while( ( r = resolveQueue ) )
    247     {
    248         resolveQueue = r->next;
    249         resolveRelease( r );
    250     }
    25169}
    25270
  • branches/encryption/libtransmission/net.h

    r2615 r3018  
    6868int tr_netResolve( const char *, struct in_addr * );
    6969
    70 typedef struct tr_resolve_s tr_resolve_t;
    71 void           tr_netResolveThreadInit( void );
    72 void           tr_netResolveThreadClose( void );
    73 tr_resolve_t * tr_netResolveInit( const char * address );
    74 tr_tristate_t  tr_netResolvePulse( tr_resolve_t *, struct in_addr * );
    75 void           tr_netResolveClose( tr_resolve_t * );
    76 
    7770
    7871/***********************************************************************
  • branches/encryption/libtransmission/transmission.c

    r2985 r3018  
    103103    tr_eventInit( h );
    104104    tr_netInit();
    105     tr_netResolveThreadInit();
    106105
    107106    h->tag = strdup( tag );
     
    256255    free( h->tag );
    257256    free( h );
    258 
    259     tr_netResolveThreadClose();
    260257}
    261258
  • branches/encryption/libtransmission/trevent.c

    r2977 r3018  
    2828
    2929#include <event.h>
     30#include <evdns.h>
    3031#include <evhttp.h>
    3132
     
    163164    eh->base = event_init( );
    164165    event_set_log_callback( logFunc );
     166    evdns_init( );
    165167
    166168    /* listen to the pipe's read fd */
     
    170172    event_dispatch( );
    171173
     174    evdns_shutdown( FALSE );
    172175    event_del( &eh->pipeEvent );
    173176    tr_lockFree( eh->lock );
  • branches/encryption/libtransmission/upnp.c

    r2898 r3018  
    211211        stupid_api = time( NULL );
    212212        fprintf( vlog, "opened log at %s\n\n", ctime( &stupid_api ) );
     213        fflush( vlog );
    213214    }
    214215#endif
     
    307308    if( NULL != vlog )
    308309    {
    309         fflush( vlog );
    310     }
    311 #endif
    312 
     310        fclose( vlog );
     311    }
     312#endif
    313313}
    314314
     
    404404    fwrite( buf, 1, len, vlog );
    405405    fputs( "\n\n", vlog );
     406    fflush( vlog );
    406407#endif
    407408
     
    525526        fwrite( buf, 1, *len, vlog );
    526527        fputs( "\n\n", vlog );
     528        fflush( vlog );
    527529#endif
    528530        return TR_NET_OK;
     
    965967        fwrite( body, 1, len, vlog );
    966968        fputs( "\n\n", vlog );
     969        fflush( vlog );
    967970    }
    968971#endif
     
    10081011    fwrite( headers, 1, hlen, vlog );
    10091012    fputs( "\n\n", vlog );
     1013    fflush( vlog );
    10101014#endif
    10111015            code = tr_httpResponseCode( headers, hlen );
Note: See TracChangeset for help on using the changeset viewer.