source: trunk/libtransmission/torrent.c @ 1594

Last change on this file since 1594 was 1594, checked in by joshe, 15 years ago

Don't save or load cached peers for private torrents.

  • Property svn:keywords set to Date Rev Author Id
File size: 21.3 KB
Line 
1/******************************************************************************
2 * $Id: torrent.c 1594 2007-03-26 19:19:33Z joshe $
3 *
4 * Copyright (c) 2005-2007 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#include "transmission.h"
26#include "shared.h"
27
28/***********************************************************************
29 * Local prototypes
30 **********************************************************************/
31static tr_torrent_t * torrentRealInit( tr_handle_t *, tr_torrent_t * tor,
32                                       uint8_t *, int flags, int * error );
33static void torrentReallyStop( tr_torrent_t * );
34static void downloadLoop( void * );
35
36void tr_setUseCustomUpload( tr_torrent_t * tor, int limit )
37{
38    tor->customUploadLimit = limit;
39}
40
41void tr_setUseCustomDownload( tr_torrent_t * tor, int limit )
42{
43    tor->customDownloadLimit = limit;
44}
45
46void tr_setUploadLimit( tr_torrent_t * tor, int limit )
47{
48    tr_rcSetLimit( tor->upload, limit );
49}
50
51void tr_setDownloadLimit( tr_torrent_t * tor, int limit )
52{
53    tr_rcSetLimit( tor->download, limit );
54}
55
56tr_torrent_t * tr_torrentInit( tr_handle_t * h, const char * path,
57                               uint8_t * hash, int flags, int * error )
58{
59    tr_torrent_t  * tor = calloc( sizeof( tr_torrent_t ), 1 );
60    int             saveCopy = ( TR_FLAG_SAVE & flags );
61
62    /* Parse torrent file */
63    if( tr_metainfoParse( &tor->info, h->tag, path, NULL, saveCopy ) )
64    {
65        *error = TR_EINVALID;
66        free( tor );
67        return NULL;
68    }
69
70    return torrentRealInit( h, tor, hash, flags, error );
71}
72
73tr_torrent_t * tr_torrentInitSaved( tr_handle_t * h, const char * hashStr,
74                                    int flags, int * error )
75{
76    tr_torrent_t  * tor = calloc( sizeof( tr_torrent_t ), 1 );
77
78    /* Parse torrent file */
79    if( tr_metainfoParse( &tor->info, h->tag, NULL, hashStr, 0 ) )
80    {
81        *error = TR_EINVALID;
82        free( tor );
83        return NULL;
84    }
85
86    return torrentRealInit( h, tor, NULL, ( TR_FLAG_SAVE | flags ), error );
87}
88
89/***********************************************************************
90 * tr_torrentInit
91 ***********************************************************************
92 * Allocates a tr_torrent_t structure, then relies on tr_metainfoParse
93 * to fill it.
94 **********************************************************************/
95static tr_torrent_t * torrentRealInit( tr_handle_t * h, tr_torrent_t * tor,
96                                       uint8_t * hash, int flags, int * error )
97{
98    tr_torrent_t  * tor_tmp;
99    tr_info_t     * inf;
100    int             i;
101   
102    inf         = &tor->info;
103    inf->flags |= flags;
104
105    tr_sharedLock( h->shared );
106
107    /* Make sure this torrent is not already open */
108    for( tor_tmp = h->torrentList; tor_tmp; tor_tmp = tor_tmp->next )
109    {
110        if( !memcmp( tor->info.hash, tor_tmp->info.hash,
111                     SHA_DIGEST_LENGTH ) )
112        {
113            if( NULL != hash )
114            {
115                memcpy( hash, tor->info.hash, SHA_DIGEST_LENGTH );
116            }
117            *error = TR_EDUPLICATE;
118            tr_metainfoFree( &tor->info );
119            free( tor );
120            tr_sharedUnlock( h->shared );
121            return NULL;
122        }
123    }
124
125    tor->handle   = h;
126    tor->status   = TR_STATUS_PAUSE;
127    tor->id       = h->id;
128    tor->key      = h->key;
129    tor->azId     = h->azId;
130    tor->finished = 0;
131
132    /* Escaped info hash for HTTP queries */
133    for( i = 0; i < SHA_DIGEST_LENGTH; i++ )
134    {
135        snprintf( &tor->escapedHashString[3*i],
136                  sizeof( tor->escapedHashString ) - 3 * i,
137                  "%%%02x", inf->hash[i] );
138    }
139
140    tor->pexDisabled = 0;
141
142    /* Block size: usually 16 ko, or less if we have to */
143    tor->blockSize  = MIN( inf->pieceSize, 1 << 14 );
144    tor->blockCount = ( inf->totalSize + tor->blockSize - 1 ) /
145                        tor->blockSize;
146    tor->completion = tr_cpInit( tor );
147
148    tr_lockInit( &tor->lock );
149    tr_condInit( &tor->cond );
150
151    tor->upload         = tr_rcInit();
152    tor->download       = tr_rcInit();
153    tor->swarmspeed     = tr_rcInit();
154 
155    /* We have a new torrent */
156    tor->publicPort = tr_sharedGetPublicPort( h->shared );
157    tor->prev       = NULL;
158    tor->next       = h->torrentList;
159    if( tor->next )
160    {
161        tor->next->prev = tor;
162    }
163    h->torrentList = tor;
164    (h->torrentCount)++;
165
166    tr_sharedUnlock( h->shared );
167
168    if( !h->isPortSet )
169    {
170        tr_setBindPort( h, TR_DEFAULT_PORT );
171    }
172
173    return tor;
174}
175
176tr_info_t * tr_torrentInfo( tr_torrent_t * tor )
177{
178    return &tor->info;
179}
180
181/***********************************************************************
182 * tr_torrentScrape     
183 **********************************************************************/
184int tr_torrentScrape( tr_torrent_t * tor, int * s, int * l, int * d )
185{
186    return tr_trackerScrape( tor, s, l, d );
187}
188
189void tr_torrentSetFolder( tr_torrent_t * tor, const char * path )
190{
191    tor->destination = strdup( path );
192    tr_ioLoadResume( tor );
193}
194
195char * tr_torrentGetFolder( tr_torrent_t * tor )
196{
197    return tor->destination;
198}
199
200void tr_torrentStart( tr_torrent_t * tor )
201{
202    char name[32];
203
204    if( tor->status & ( TR_STATUS_STOPPING | TR_STATUS_STOPPED ) )
205    {
206        /* Join the thread first */
207        torrentReallyStop( tor );
208    }
209
210    tr_lockLock( &tor->lock );
211
212    tor->downloadedPrev += tor->downloadedCur;
213    tor->downloadedCur   = 0;
214    tor->uploadedPrev   += tor->uploadedCur;
215    tor->uploadedCur     = 0;
216
217    tor->status  = TR_STATUS_CHECK;
218    tor->error   = TR_OK;
219    tor->tracker = tr_trackerInit( tor );
220
221    tor->date = tr_date();
222    tor->die = 0;
223    snprintf( name, sizeof( name ), "torrent %p", tor );
224
225    tr_lockUnlock( &tor->lock );
226
227    tr_threadCreate( &tor->thread, downloadLoop, tor, name );
228}
229
230static void torrentStop( tr_torrent_t * tor )
231{
232    tr_trackerStopped( tor->tracker );
233    tr_rcReset( tor->download );
234    tr_rcReset( tor->upload );
235    tr_rcReset( tor->swarmspeed );
236    tor->status = TR_STATUS_STOPPING;
237    tor->stopDate = tr_date();
238}
239
240void tr_torrentStop( tr_torrent_t * tor )
241{
242    tr_lockLock( &tor->lock );
243    torrentStop( tor );
244
245    /* Don't return until the files are closed, so the UI can trash
246     * them if requested */
247    tr_condWait( &tor->cond, &tor->lock );
248    tr_lockUnlock( &tor->lock );
249}
250
251/***********************************************************************
252 * torrentReallyStop
253 ***********************************************************************
254 * Joins the download thread and frees/closes everything related to it.
255 **********************************************************************/
256static void torrentReallyStop( tr_torrent_t * tor )
257{
258    int i;
259
260    tor->die = 1;
261    tr_threadJoin( &tor->thread );
262
263    tr_trackerClose( tor->tracker );
264    tor->tracker = NULL;
265
266    tr_lockLock( &tor->lock );
267    for( i = 0; i < tor->peerCount; i++ )
268    {
269        tr_peerDestroy( tor->peers[i] );
270    }
271    tor->peerCount = 0;
272    tr_lockUnlock( &tor->lock );
273}
274
275void tr_torrentDisablePex( tr_torrent_t * tor, int disable )
276{
277    int ii;
278
279    if( TR_FLAG_PRIVATE & tor->info.flags )
280    {
281        return;
282    }
283
284    tr_lockLock( &tor->lock );
285
286    if( tor->pexDisabled == disable )
287    {
288        tr_lockUnlock( &tor->lock );
289        return;
290    }
291
292    tor->pexDisabled = disable;
293    for( ii = 0; ii < tor->peerCount; ii++ )
294    {
295        tr_peerSetPrivate( tor->peers[ii], disable );
296    }
297
298    tr_lockUnlock( &tor->lock );
299}
300
301int tr_getFinished( tr_torrent_t * tor )
302{
303    if( tor->finished )
304    {
305        tor->finished = 0;
306        return 1;
307    }
308    return 0;
309}
310
311void tr_manualUpdate( tr_torrent_t * tor )
312{
313    int peerCount;
314    uint8_t * peerCompact;
315
316    if( !( tor->status & TR_STATUS_ACTIVE ) )
317        return;
318   
319    tr_lockLock( &tor->lock );
320    tr_trackerAnnouncePulse( tor->tracker, &peerCount, &peerCompact, 1 );
321    if( peerCount > 0 )
322    {
323        tr_torrentAddCompact( tor, TR_PEER_FROM_TRACKER,
324                              peerCompact, peerCount );
325        free( peerCompact );
326    }
327    tr_lockUnlock( &tor->lock );
328}
329
330tr_stat_t * tr_torrentStat( tr_torrent_t * tor )
331{
332    tr_stat_t * s;
333    tr_peer_t * peer;
334    tr_info_t * inf = &tor->info;
335    tr_tracker_t * tc;
336    int i;
337
338    tor->statCur = ( tor->statCur + 1 ) % 2;
339    s = &tor->stats[tor->statCur];
340
341    if( ( tor->status & TR_STATUS_STOPPED ) ||
342        ( ( tor->status & TR_STATUS_STOPPING ) &&
343          tr_date() > tor->stopDate + 60000 ) )
344    {
345        torrentReallyStop( tor );
346        tor->status = TR_STATUS_PAUSE;
347    }
348
349    tr_lockLock( &tor->lock );
350
351    s->status = tor->status;
352    s->error  = tor->error;
353    memcpy( s->errorString, tor->errorString,
354            sizeof( s->errorString ) );
355
356    tc = tor->tracker;
357    s->cannotConnect = tr_trackerCannotConnect( tc );
358    s->tracker = ( tc ? tr_trackerGet( tc ) : &inf->trackerList[0].list[0] );
359
360    s->peersTotal       = 0;
361    memset( s->peersFrom, 0, sizeof( s->peersFrom ) );
362    s->peersUploading   = 0;
363    s->peersDownloading = 0;
364   
365    for( i = 0; i < tor->peerCount; i++ )
366    {
367        peer = tor->peers[i];
368   
369        if( tr_peerIsConnected( peer ) )
370        {
371            (s->peersTotal)++;
372            (s->peersFrom[ tr_peerIsFrom( peer ) ])++;
373            if( tr_peerAmInterested( peer ) && !tr_peerIsChoking( peer ) )
374            {
375                (s->peersUploading)++;
376            }
377            if( !tr_peerAmChoking( peer ) )
378            {
379                (s->peersDownloading)++;
380            }
381        }
382    }
383
384    s->progress = tr_cpCompletionAsFloat( tor->completion );
385    if( tor->status & TR_STATUS_DOWNLOAD )
386    {
387        s->rateDownload = tr_rcRate( tor->download );
388    }
389    else
390    {
391        /* tr_rcRate() doesn't make the difference between 'piece'
392           messages and other messages, which causes a non-zero
393           download rate even tough we are not downloading. So we
394           force it to zero not to confuse the user. */
395        s->rateDownload = 0.0;
396    }
397    s->rateUpload = tr_rcRate( tor->upload );
398   
399    s->seeders  = tr_trackerSeeders( tc );
400    s->leechers = tr_trackerLeechers( tc );
401    s->completedFromTracker = tr_trackerDownloaded( tc );
402
403    s->swarmspeed = tr_rcRate( tor->swarmspeed );
404
405    if( s->rateDownload < 0.1 )
406    {
407        s->eta = -1;
408    }
409    else
410    {
411        s->eta = ( 1.0 - s->progress ) *
412            (float) inf->totalSize / s->rateDownload / 1024.0;
413    }
414
415    s->downloaded = tor->downloadedCur + tor->downloadedPrev;
416    s->uploaded   = tor->uploadedCur   + tor->uploadedPrev;
417   
418    if( s->downloaded == 0 )
419    {
420        s->ratio = s->uploaded == 0 ? TR_RATIO_NA : TR_RATIO_INF;
421    }
422    else
423    {
424        s->ratio = (float)s->uploaded / (float)s->downloaded;
425    }
426   
427    tr_lockUnlock( &tor->lock );
428
429    return s;
430}
431
432tr_peer_stat_t * tr_torrentPeers( tr_torrent_t * tor, int * peerCount )
433{
434    tr_peer_stat_t * peers;
435
436    tr_lockLock( &tor->lock );
437
438    *peerCount = tor->peerCount;
439   
440    peers = (tr_peer_stat_t *) calloc( tor->peerCount, sizeof( tr_peer_stat_t ) );
441    if (peers != NULL)
442    {
443        tr_peer_t * peer;
444        struct in_addr * addr;
445        int i;
446        for( i = 0; i < tor->peerCount; i++ )
447        {
448            peer = tor->peers[i];
449           
450            addr = tr_peerAddress( peer );
451            if( NULL != addr )
452            {
453                tr_netNtop( addr, peers[i].addr,
454                           sizeof( peers[i].addr ) );
455            }
456           
457            peers[i].client = tr_clientForId(tr_peerId(peer));
458           
459            peers[i].isConnected   = tr_peerIsConnected( peer );
460            peers[i].from          = tr_peerIsFrom( peer );
461            peers[i].progress      = tr_peerProgress( peer );
462            peers[i].port          = tr_peerPort( peer );
463           
464            if( ( peers[i].isDownloading = !tr_peerAmChoking( peer ) ) )
465            {
466                peers[i].uploadToRate = tr_peerUploadRate( peer );
467            }
468            if( ( peers[i].isUploading = ( tr_peerAmInterested( peer ) &&
469                                           !tr_peerIsChoking( peer ) ) ) )
470            {
471                peers[i].downloadFromRate = tr_peerDownloadRate( peer );
472            }
473        }
474    }
475   
476    tr_lockUnlock( &tor->lock );
477   
478    return peers;
479}
480
481void tr_torrentPeersFree( tr_peer_stat_t * peers, int peerCount )
482{
483    int i;
484
485    if (peers == NULL)
486        return;
487
488    for (i = 0; i < peerCount; i++)
489        free( peers[i].client );
490
491    free( peers );
492}
493
494void tr_torrentAvailability( tr_torrent_t * tor, int8_t * tab, int size )
495{
496    int i, j, piece;
497    float interval;
498
499    tr_lockLock( &tor->lock );
500    interval = (float)tor->info.pieceCount / (float)size;
501    for( i = 0; i < size; i++ )
502    {
503        piece = i * interval;
504
505        if( tr_cpPieceIsComplete( tor->completion, piece ) )
506        {
507            tab[i] = -1;
508            continue;
509        }
510
511        tab[i] = 0;
512        for( j = 0; j < tor->peerCount; j++ )
513        {
514            if( tr_peerBitfield( tor->peers[j] ) &&
515                tr_bitfieldHas( tr_peerBitfield( tor->peers[j] ), piece ) )
516            {
517                (tab[i])++;
518            }
519        }
520    }
521    tr_lockUnlock( &tor->lock );
522}
523
524float * tr_torrentCompletion( tr_torrent_t * tor )
525{
526    tr_info_t * inf = &tor->info;
527    int         piece, file;
528    float     * ret, prog, weight;
529    uint64_t    piecemax, piecesize;
530    uint64_t    filestart, fileoff, filelen, blockend, blockused;
531
532    tr_lockLock( &tor->lock );
533
534    ret       = calloc( inf->fileCount, sizeof( float ) );
535    file      = 0;
536    piecemax  = inf->pieceSize;
537    filestart = 0;
538    fileoff   = 0;
539    piece     = 0;
540    while( inf->pieceCount > piece )
541    {
542        assert( file < inf->fileCount );
543        assert( filestart + fileoff < inf->totalSize );
544        filelen    = inf->files[file].length;
545        piecesize  = tr_pieceSize( piece );
546        blockend   = MIN( filestart + filelen, piecemax * piece + piecesize );
547        blockused  = blockend - ( filestart + fileoff );
548        weight     = ( filelen ? ( float )blockused / ( float )filelen : 1.0 );
549        prog       = tr_cpPercentBlocksInPiece( tor->completion, piece );
550        ret[file] += prog * weight;
551        fileoff   += blockused;
552        assert( -0.1 < prog   && 1.1 > prog );
553        assert( -0.1 < weight && 1.1 > weight );
554        if( fileoff == filelen )
555        {
556            ret[file] = MIN( 1.0, ret[file] );
557            ret[file] = MAX( 0.0, ret[file] );
558            filestart += fileoff;
559            fileoff    = 0;
560            file++;
561        }
562        if( filestart + fileoff >= piecemax * piece + piecesize )
563        {
564            piece++;
565        }
566    }
567
568    tr_lockUnlock( &tor->lock );
569
570    return ret;
571}
572
573void tr_torrentAmountFinished( tr_torrent_t * tor, float * tab, int size )
574{
575    int i, piece;
576    float interval;
577
578    tr_lockLock( &tor->lock );
579    interval = (float)tor->info.pieceCount / (float)size;
580    for( i = 0; i < size; i++ )
581    {
582        piece = i * interval;
583        tab[i] = tr_cpPercentBlocksInPiece( tor->completion, piece );
584    }
585    tr_lockUnlock( &tor->lock );
586}
587
588void tr_torrentRemoveSaved( tr_torrent_t * tor )
589{
590    tr_metainfoRemoveSaved( tor->info.hashString, tor->handle->tag );
591}
592
593/***********************************************************************
594 * tr_torrentClose
595 ***********************************************************************
596 * Frees memory allocated by tr_torrentInit.
597 **********************************************************************/
598void tr_torrentClose( tr_handle_t * h, tr_torrent_t * tor )
599{
600    tr_info_t * inf = &tor->info;
601
602    if( tor->status & ( TR_STATUS_STOPPING | TR_STATUS_STOPPED ) )
603    {
604        /* Join the thread first */
605        torrentReallyStop( tor );
606    }
607
608    tr_sharedLock( h->shared );
609
610    h->torrentCount--;
611
612    tr_lockClose( &tor->lock );
613    tr_condClose( &tor->cond );
614    tr_cpClose( tor->completion );
615
616    tr_rcClose( tor->upload );
617    tr_rcClose( tor->download );
618    tr_rcClose( tor->swarmspeed );
619
620    if( tor->destination )
621    {
622        free( tor->destination );
623    }
624
625    tr_metainfoFree( inf );
626
627    if( tor->prev )
628    {
629        tor->prev->next = tor->next;
630    }
631    else
632    {
633        h->torrentList = tor->next;
634    }
635    if( tor->next )
636    {
637        tor->next->prev = tor->prev;
638    }
639    free( tor );
640
641    tr_sharedUnlock( h->shared );
642}
643
644void tr_torrentAttachPeer( tr_torrent_t * tor, tr_peer_t * peer )
645{
646    int i;
647    tr_peer_t * otherPeer;
648
649    if( tor->peerCount >= TR_MAX_PEER_COUNT )
650    {
651        tr_peerDestroy(  peer );
652        return;
653    }
654
655    /* Don't accept two connections from the same IP */
656    for( i = 0; i < tor->peerCount; i++ )
657    {
658        otherPeer = tor->peers[i];
659        if( !memcmp( tr_peerAddress( peer ), tr_peerAddress( otherPeer ), 4 ) )
660        {
661            tr_peerDestroy(  peer );
662            return;
663        }
664    }
665
666    tr_peerSetPrivate( peer, tor->info.flags & TR_FLAG_PRIVATE ||
667                       tor->pexDisabled );
668    tr_peerSetTorrent( peer, tor );
669    tor->peers[tor->peerCount++] = peer;
670}
671
672void tr_torrentAddCompact( tr_torrent_t * tor, int from,
673                           uint8_t * buf, int count )
674{
675    struct in_addr addr;
676    in_port_t port;
677    int i;
678    tr_peer_t * peer;
679
680    for( i = 0; i < count; i++ )
681    {
682        memcpy( &addr, buf, 4 ); buf += 4;
683        memcpy( &port, buf, 2 ); buf += 2;
684
685        peer = tr_peerInit( addr, port, -1, from );
686        tr_torrentAttachPeer( tor, peer );
687    }
688}
689
690/***********************************************************************
691 * downloadLoop
692 **********************************************************************/
693static void downloadLoop( void * _tor )
694{
695    tr_torrent_t * tor = _tor;
696    int            i, ret;
697    int            peerCount;
698    uint8_t      * peerCompact;
699    tr_peer_t    * peer;
700
701    tr_lockLock( &tor->lock );
702
703    tr_cpReset( tor->completion );
704    tor->io     = tr_ioInit( tor );
705    tor->status = tr_cpIsSeeding( tor->completion ) ?
706                      TR_STATUS_SEED : TR_STATUS_DOWNLOAD;
707
708    while( !tor->die )
709    {
710        tr_lockUnlock( &tor->lock );
711        tr_wait( 20 );
712        tr_lockLock( &tor->lock );
713
714        /* Are we finished ? */
715        if( ( tor->status & TR_STATUS_DOWNLOAD ) &&
716            tr_cpIsSeeding( tor->completion ) )
717        {
718            /* Done */
719            tor->status = TR_STATUS_SEED;
720                        tor->finished = 1;
721            tr_trackerCompleted( tor->tracker );
722            tr_ioSync( tor->io );
723        }
724
725        /* Try to get new peers or to send a message to the tracker */
726        tr_trackerPulse( tor->tracker, &peerCount, &peerCompact );
727        if( peerCount > 0 )
728        {
729            tr_torrentAddCompact( tor, TR_PEER_FROM_TRACKER,
730                                  peerCompact, peerCount );
731            free( peerCompact );
732        }
733        if( tor->status & TR_STATUS_STOPPED )
734        {
735            break;
736        }
737
738        /* Stopping: make sure all files are closed and stop talking
739           to peers */
740        if( tor->status & TR_STATUS_STOPPING )
741        {
742            if( tor->io )
743            {
744                tr_ioClose( tor->io ); tor->io = NULL;
745                tr_condSignal( &tor->cond );
746            }
747            continue;
748        }
749
750        /* Shuffle peers */
751        if( tor->peerCount > 1 )
752        {
753            peer = tor->peers[0];
754            memmove( &tor->peers[0], &tor->peers[1],
755                    ( tor->peerCount - 1 ) * sizeof( void * ) );
756            tor->peers[tor->peerCount - 1] = peer;
757        }
758
759        /* Receive/send messages */
760        for( i = 0; i < tor->peerCount; )
761        {
762            peer = tor->peers[i];
763
764            ret = tr_peerPulse( peer );
765            if( ret & TR_ERROR_IO_MASK )
766            {
767                tr_err( "Fatal error, stopping download (%d)", ret );
768                torrentStop( tor );
769                tor->error = ret;
770                snprintf( tor->errorString, sizeof( tor->errorString ),
771                          "%s", tr_errorString( ret ) );
772                break;
773            }
774            if( ret )
775            {
776                tr_peerDestroy( peer );
777                tor->peerCount--;
778                memmove( &tor->peers[i], &tor->peers[i+1],
779                         ( tor->peerCount - i ) * sizeof( void * ) );
780                continue;
781            }
782            i++;
783        }
784    }
785
786    tr_lockUnlock( &tor->lock );
787
788    if( tor->io )
789    {
790        tr_ioClose( tor->io ); tor->io = NULL;
791        tr_condSignal( &tor->cond );
792    }
793
794    tor->status = TR_STATUS_STOPPED;
795}
796
Note: See TracBrowser for help on using the repository browser.