Changeset 12860


Ignore:
Timestamp:
Sep 12, 2011, 9:46:15 PM (10 years ago)
Author:
jordan
Message:

(trunk web) small refactoring of webseed's batch processing.

  1. move the callback structs' declarations next to the callback functions where they're used.
  2. rename the callback structs to clarify their link to the callback functions.
  3. constify some of the callback functions' local variables.
  4. clarify some of the comments.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/webseed.c

    r12859 r12860  
    6363};
    6464
    65 struct tr_blockwrite_info
    66 {
    67     struct tr_webseed  * webseed;
    68     struct evbuffer    * data;
    69     tr_piece_index_t     piece_index;
    70     tr_block_index_t     block_index;
    71     tr_block_index_t     count;
    72     uint32_t             block_offset;
    73 };
    74 
    75 struct tr_http_info
    76 {
    77     struct tr_webseed  * webseed;
    78     char               * redirect_url;
    79     tr_piece_index_t     piece_index;
    80     uint32_t             piece_offset;
    81 };
    82 
    8365enum
    8466{
     
    171153***/
    172154
    173 static void
    174 write_block_func( void * vblock )
    175 {
    176     struct tr_blockwrite_info * block = vblock;
    177     struct tr_webseed * w = block->webseed;
    178     struct evbuffer * buf = block->data;
     155struct write_block_data
     156{
     157    struct tr_webseed  * webseed;
     158    struct evbuffer    * content;
     159    tr_piece_index_t     piece_index;
     160    tr_block_index_t     block_index;
     161    tr_block_index_t     count;
     162    uint32_t             block_offset;
     163};
     164
     165static void
     166write_block_func( void * vdata )
     167{
     168    struct write_block_data * data = vdata;
     169    struct tr_webseed * w = data->webseed;
     170    struct evbuffer * buf = data->content;
    179171    struct tr_torrent * tor;
    180172
     
    184176        const uint32_t block_size = tor->blockSize;
    185177        uint32_t len = evbuffer_get_length( buf );
    186         uint32_t offset_end = block->block_offset + len;
     178        const uint32_t offset_end = data->block_offset + len;
    187179        tr_cache * cache = w->session->cache;
    188         tr_piece_index_t piece = block->piece_index;
    189 
    190         while( true )
     180        const tr_piece_index_t piece = data->piece_index;
     181
     182        while( len > 0 )
    191183        {
    192             if( len > block_size) {
    193                 tr_cacheWriteBlock( cache, tor, piece, offset_end - len,
    194                                     block_size, buf );
    195                 len -= block_size;
    196             }
    197             else {
    198                 tr_cacheWriteBlock( cache, tor, piece, offset_end - len,
    199                                     len, buf );
    200                 break;
    201             }
    202         }
    203         fire_client_got_blocks( tor, w, block->block_index, block->count );
     184            const uint32_t bytes_this_pass = MIN( len, block_size );
     185            tr_cacheWriteBlock( cache, tor, piece, offset_end - len, bytes_this_pass, buf );
     186            len -= bytes_this_pass;
     187        }
     188
     189        fire_client_got_blocks( tor, w, data->block_index, data->count );
    204190    }
    205191
    206192    evbuffer_free( buf );
    207     tr_free( block );
    208 }
    209 
    210 static void
    211 connection_succeeded( void * vinf )
    212 {
    213     struct tr_http_info * inf = vinf;
    214     struct tr_webseed * w = inf->webseed;
    215     struct tr_torrent * tor;
     193    tr_free( data );
     194}
     195
     196/***
     197****
     198***/
     199
     200struct connection_succeeded_data
     201{
     202    struct tr_webseed  * webseed;
     203    char               * real_url;
     204    tr_piece_index_t     piece_index;
     205    uint32_t             piece_offset;
     206};
     207
     208static void
     209connection_succeeded( void * vdata )
     210{
     211    tr_torrent * tor;
     212    struct connection_succeeded_data * data = vdata;
     213    struct tr_webseed * w = data->webseed;
    216214
    217215    if( ++w->active_transfers >= w->retry_challenge && w->retry_challenge )
     
    219217        w->consecutive_failures = w->retry_tickcount = w->retry_challenge = 0;
    220218
    221     if( inf->redirect_url &&
     219    if( data->real_url &&
    222220        (tor = tr_torrentFindFromId( w->session, w->torrent_id )))
    223221    {
     
    225223        tr_file_index_t file_index;
    226224
    227         tr_ioFindFileLocation( tor, inf->piece_index, inf->piece_offset,
     225        tr_ioFindFileLocation( tor, data->piece_index, data->piece_offset,
    228226                               &file_index, &file_offset );
    229227        tr_free( w->file_urls[file_index] );
    230         w->file_urls[file_index] = inf->redirect_url;
    231     }
    232 }
     228        w->file_urls[file_index] = data->real_url;
     229    }
     230}
     231
     232/***
     233****
     234***/
    233235
    234236static void
     
    237239                    void                           * vtask )
    238240{
     241    uint32_t len;
     242    const size_t n_added = info->n_added;
    239243    struct tr_webseed_task * task = vtask;
    240244    struct tr_webseed * w = task->webseed;
    241     uint32_t len;
    242 
    243     if( info->n_added <= 0 )
     245
     246    if( n_added <= 0 )
    244247        return;
    245248
    246249    if( !w->is_stopping )
    247250    {
    248         tr_bandwidthUsed( &w->bandwidth, TR_DOWN, info->n_added, true, tr_time_msec( ) );
    249         fire_client_got_data( w, info->n_added );
     251        tr_bandwidthUsed( &w->bandwidth, TR_DOWN, n_added, true, tr_time_msec( ) );
     252        fire_client_got_data( w, n_added );
    250253    }
    251254
    252255    len = evbuffer_get_length( buf );
    253256
    254     if( !task->response_code ) {
     257    if( !task->response_code )
     258    {
    255259        tr_webGetTaskInfo( task->web_task, TR_WEB_GET_CODE, &task->response_code );
    256260
    257         if( task->response_code == 206 ) {
    258             struct tr_http_info * inf = tr_new( struct tr_http_info, 1 );
    259             long redirects;
    260 
    261             inf->webseed = w;
    262             inf->piece_index = task->piece_index;
    263             inf->piece_offset = task->piece_offset
    264                               + (task->blocks_done * task->block_size)
    265                               + (len - 1);
    266             tr_webGetTaskInfo( task->web_task, TR_WEB_GET_REDIRECTS, &redirects );
    267             if( redirects ) {
    268                 char * redirect_url;
    269                 tr_webGetTaskInfo( task->web_task, TR_WEB_GET_REAL_URL, &redirect_url );
    270                 inf->redirect_url = tr_strdup( redirect_url );
    271             }
    272             else
    273                 inf->redirect_url = NULL;
    274             /* run this in the webseed thread to avoid tampering with mutexes and to
    275             not cost the web thread too much time */
    276             tr_runInEventThread( w->session, connection_succeeded, inf );
     261        if( task->response_code == 206 )
     262        {
     263            const char * url;
     264            struct connection_succeeded_data * data;
     265
     266            url = NULL;
     267            tr_webGetTaskInfo( task->web_task, TR_WEB_GET_REAL_URL, &url );
     268
     269            data = tr_new( struct connection_succeeded_data, 1 );
     270            data->webseed = w;
     271            data->real_url = tr_strdup( url );
     272            data->piece_index = task->piece_index;
     273            data->piece_offset = task->piece_offset
     274                               + (task->blocks_done * task->block_size)
     275                               + (len - 1);
     276
     277            /* processing this uses a tr_torrent pointer,
     278               so push the work to the libevent thread... */
     279            tr_runInEventThread( w->session, connection_succeeded, data );
    277280        }
    278281    }
    279282
    280     if( task->response_code == 206 && len >= task->block_size )
    281     {
    282         /* one (ore more) block(s) received. write to hd */
     283    if( ( task->response_code == 206 ) && ( len >= task->block_size ) )
     284    {
     285        /* once we've got at least one full block, save it */
     286
     287        struct write_block_data * data;
    283288        const uint32_t block_size = task->block_size;
    284289        const tr_block_index_t completed = len / block_size;
    285         struct tr_blockwrite_info * b = tr_new( struct tr_blockwrite_info, 1 );
    286 
    287         b->webseed = task->webseed;
    288         b->piece_index = task->piece_index;
    289         b->block_index = task->block + task->blocks_done;
    290         b->count = completed;
    291         b->block_offset = task->piece_offset + task->blocks_done * block_size;
    292         b->data = evbuffer_new( );
     290
     291        data = tr_new( struct write_block_data, 1 );
     292        data->webseed = task->webseed;
     293        data->piece_index = task->piece_index;
     294        data->block_index = task->block + task->blocks_done;
     295        data->count = completed;
     296        data->block_offset = task->piece_offset + task->blocks_done * block_size;
     297        data->content = evbuffer_new( );
    293298
    294299        /* we don't use locking on this evbuffer so we must copy out the data
    295300        that will be needed when writing the block in a different thread */
    296         evbuffer_remove_buffer( task->content, b->data,
     301        evbuffer_remove_buffer( task->content, data->content,
    297302                                block_size * completed );
    298303
    299         tr_runInEventThread( w->session, write_block_func, b );
     304        tr_runInEventThread( w->session, write_block_func, data );
    300305        task->blocks_done += completed;
    301306    }
Note: See TracChangeset for help on using the changeset viewer.