File: downloader.cpp

package info (click to toggle)
ctorrent 1.3.4.dnh3.3.2-5
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,240 kB
  • sloc: cpp: 13,161; sh: 675; ansic: 383; makefile: 6
file content (138 lines) | stat: -rw-r--r-- 3,674 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "def.h"

#include <sys/types.h>

#include <sys/time.h>
#include <time.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include "peerlist.h"
#include "tracker.h"
#include "btcontent.h"
#include "ctcs.h"
#include "btconfig.h"
#include "bttime.h"
#include "console.h"

#define MAX_SLEEP 1

time_t now = time((time_t *)0);

void Downloader()
{
  int nfds = 0, maxfd, r;
  struct timeval timeout;
  fd_set rfd, rfdnext;
  fd_set wfd, wfdnext;
  int stopped = 0, f_idleused = 0, f_poll = 0;
  struct timespec nowspec;
  double maxsleep;
  time_t then, concheck = (time_t)0;

  FD_ZERO(&rfdnext); FD_ZERO(&wfdnext);

  do{
    time(&now);
    if( !stopped ){
      if( !Tracker.IsQuitting() && BTCONTENT.SeedTimeout() )
        Tracker.SetStoped();
      if( Tracker.IsQuitting() && !Tracker.IsRestarting() ){
        stopped = 1;
        WORLD.Pause();
        if( arg_ctcs ) CTCS.Send_Status();
      }
    }

    maxfd = -1;
    maxsleep = -1;
    rfd = rfdnext;
    wfd = wfdnext;

    if( f_poll ){
      FD_ZERO(&rfd); FD_ZERO(&wfd);  // remove non-peers from sets
      maxsleep = 0;  // waited for bandwidth--poll now
    }else{
      WORLD.DontWaitBW();
      if( WORLD.IsIdle() ){
        f_idleused = 0;
        if( BTCONTENT.CheckedPieces() < BTCONTENT.GetNPieces() &&
            !BTCONTENT.NeedFlush() ){
          if( BTCONTENT.CheckNextPiece() < 0 ){
            CONSOLE.Warning(1, "Error while checking piece %d of %d",
              (int)(BTCONTENT.CheckedPieces()), (int)(BTCONTENT.GetNPieces()));
            Tracker.SetStoped();
            maxsleep = 2;
          }else maxsleep = 0;
          f_idleused = 1;
          time(&now);
        }
        r = Tracker.IntervalCheck(&rfd, &wfd);
        if( r > maxfd ) maxfd = r;
        if( arg_ctcs ){
          r = CTCS.IntervalCheck(&rfd, &wfd);
          if( r > maxfd ) maxfd = r;
        }
        if( !f_idleused || concheck <= now-2 || WORLD.IsIdle() ){
          concheck = now;
          r = CONSOLE.IntervalCheck(&rfd, &wfd);
          if( r > maxfd ) maxfd = r;
        }
      }
    }
    r = WORLD.IntervalCheck(&rfd, &wfd);
    if( r > maxfd ) maxfd = r;

    if( !f_poll ){
      time(&now);
      while( BTCONTENT.NeedFlush() && WORLD.IsIdle() ){
        BTCONTENT.FlushQueue();
        maxsleep = 0;
        time(&now);
      }
    }

    rfdnext = rfd;
    wfdnext = wfd;

    if( maxsleep < 0 ){  //not yet set
      maxsleep = WORLD.WaitBW();  // must do after intervalchecks!
      if( maxsleep <= -100 ) maxsleep = 0;
      else if( maxsleep <= 0 || maxsleep > MAX_SLEEP ) maxsleep = MAX_SLEEP;
    }

    timeout.tv_sec = (long)maxsleep;
    timeout.tv_usec = (long)( (maxsleep-(long)maxsleep) * 1000000 );

    WORLD.UnLate();
    nfds = select(maxfd + 1,&rfd,&wfd,(fd_set*) 0,&timeout);
    if( nfds < 0 ){
      CONSOLE.Debug("Error from select:  %s", strerror(errno));
      FD_ZERO(&rfdnext); FD_ZERO(&wfdnext);
      nfds = 0;
    }

    if( f_poll ) f_poll = 0;
    else if( nfds > 0 ) WORLD.DontWaitBW();
    else if( maxsleep > 0 && maxsleep < MAX_SLEEP ) f_poll = 1;

    then = now;
    time(&now);
    if( now == then-1 ) now = then;

    if( !f_poll && nfds > 0 ){
      if(T_FREE != Tracker.GetStatus())
        Tracker.SocketReady(&rfd,&wfd,&nfds,&rfdnext,&wfdnext);
      if(nfds > 0 && T_FREE != CTCS.GetStatus())
        CTCS.SocketReady(&rfd,&wfd,&nfds,&rfdnext,&wfdnext);
      if(nfds > 0)
        CONSOLE.User(&rfd,&wfd,&nfds,&rfdnext,&wfdnext);
    }
    if(nfds > 0)
      WORLD.AnyPeerReady(&rfd,&wfd,&nfds,&rfdnext,&wfdnext);
  } while(Tracker.GetStatus() != T_FINISHED || Tracker.IsRestarting());
}