Changeset 14108


Ignore:
Timestamp:
Jul 8, 2013, 4:41:12 PM (9 years ago)
Author:
jordan
Message:

fix bug #5372, 'wrong peer states displayed'.

Location:
trunk/libtransmission
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/peer-common.h

    r13954 r14108  
    150150void tr_peerUpdateProgress (tr_torrent * tor, struct tr_peer *);
    151151
     152bool tr_peerIsSeed (const struct tr_peer * peer);
     153
     154/***
     155****
     156***/
     157
     158typedef struct tr_swarm_stats
     159{
     160  int activePeerCount[2];
     161  int activeWebseedCount;
     162  int peerCount;
     163  int peerFromCount[TR_PEER_FROM__MAX];
     164}
     165tr_swarm_stats;
     166
     167extern const tr_swarm_stats TR_SWARM_STATS_INIT;
     168
     169void tr_swarmGetStats (const struct tr_swarm * swarm, tr_swarm_stats * setme);
     170
     171void tr_swarmIncrementActivePeers (struct tr_swarm * swarm, tr_direction direction, bool is_active);
     172
     173
     174/***
     175****
     176***/
     177
    152178
    153179#ifdef WIN32
  • trunk/libtransmission/peer-mgr.c

    r14089 r14108  
    104104const tr_peer_event TR_PEER_EVENT_INIT = { 0, 0, NULL, 0, 0, 0, 0 };
    105105
     106const tr_swarm_stats TR_SWARM_STATS_INIT = { { 0, 0 }, 0, 0, { 0, 0, 0, 0, 0, 0, 0 } };
     107
    106108/**
    107109***
     
    185187typedef struct tr_swarm
    186188{
     189  tr_swarm_stats             stats;
     190
    187191  tr_ptrArray                outgoingHandshakes; /* tr_handshake */
    188192  tr_ptrArray                pool; /* struct peer_atom */
     
    487491
    488492static void
    489 resetTorrentStats (tr_torrent * tor)
    490 {
    491   int i;
    492 
    493   tor->peerCount = 0;
    494   tor->activePeerCount[TR_UP] = 0;
    495   tor->activePeerCount[TR_DOWN] = 0;
    496   tor->activeWebseedCount = 0;
    497   for (i=0; i<TR_PEER_FROM__MAX; i++)
    498     tor->peerFromCount[i] = 0;
    499 }
    500 
    501 static void
    502493swarmFree (void * vs)
    503494{
     
    514505  tr_ptrArrayDestruct (&s->outgoingHandshakes, NULL);
    515506  tr_ptrArrayDestruct (&s->peers, NULL);
    516   resetTorrentStats (s->tor);
     507  s->stats = TR_SWARM_STATS_INIT;
    517508
    518509  replicationFree (s);
     
    534525  tr_ptrArrayDestruct (&s->webseeds, (PtrArrayForeachFunc)tr_peerFree);
    535526  s->webseeds = TR_PTR_ARRAY_INIT;
    536   s->tor->activeWebseedCount = 0;
     527  s->stats.activeWebseedCount = 0;
    537528
    538529  /* repopulate it */
     
    19361927
    19371928  tr_ptrArrayInsertSorted (&swarm->peers, peer, peerCompare);
    1938   ++tor->peerCount;
    1939   ++tor->peerFromCount[atom->fromFirst];
    1940 
    1941   assert (tor->peerCount == tr_ptrArraySize (&swarm->peers));
    1942   assert (tor->peerFromCount[atom->fromFirst] <= tor->peerCount);
     1929  ++swarm->stats.peerCount;
     1930  ++swarm->stats.peerFromCount[atom->fromFirst];
     1931
     1932  assert (swarm->stats.peerCount == tr_ptrArraySize (&swarm->peers));
     1933  assert (swarm->stats.peerFromCount[atom->fromFirst] <= swarm->stats.peerCount);
    19431934}
    19441935
     
    25762567}
    25772568
    2578 static bool
    2579 peerIsSeed (const tr_peer * peer)
     2569void
     2570tr_swarmGetStats (const tr_swarm * swarm, tr_swarm_stats * setme)
     2571{
     2572  assert (swarm != NULL);
     2573  assert (setme != NULL);
     2574
     2575  *setme = swarm->stats;
     2576}
     2577
     2578void
     2579tr_swarmIncrementActivePeers (tr_swarm * swarm, tr_direction direction, bool is_active)
     2580{
     2581  int n = swarm->stats.activePeerCount[direction];
     2582
     2583  if (is_active)
     2584    ++n;
     2585  else
     2586    --n;
     2587
     2588  assert (0 <= n);
     2589  assert (n <= swarm->stats.peerCount);
     2590
     2591  swarm->stats.activePeerCount[direction] = n;
     2592}
     2593
     2594bool
     2595tr_peerIsSeed (const tr_peer * peer)
    25802596{
    25812597  if (peer->progress >= 1.0)
     
    27052721      stat->isDownloadingFrom   = tr_peerMsgsIsActive (msgs, TR_PEER_TO_CLIENT);
    27062722      stat->isUploadingTo       = tr_peerMsgsIsActive (msgs, TR_CLIENT_TO_PEER);
    2707       stat->isSeed              = peerIsSeed (peer);
     2723      stat->isSeed              = tr_peerIsSeed (peer);
    27082724
    27092725      stat->blocksToPeer        = tr_historyGet (&peer->blocksSentToPeer,    now, CANCEL_HISTORY_SEC);
     
    27662782  assert (tr_torrentIsPieceTransferAllowed (tor, TR_PEER_TO_CLIENT));
    27672783
    2768   if (peerIsSeed (peer))
     2784  if (tr_peerIsSeed (peer))
    27692785    return true;
    27702786
     
    30563072      struct peer_atom * atom = peer->atom;
    30573073
    3058       if (peerIsSeed (peer)) /* choke seeds and partial seeds */
     3074      if (tr_peerIsSeed (peer)) /* choke seeds and partial seeds */
    30593075        {
    30603076          tr_peerMsgsSetChoke (PEER_MSGS(peer), true);
     
    31493165  while ((tor = tr_torrentNext (mgr->session, tor)))
    31503166    {
    3151       if (tor->isRunning && tor->peerCount)
     3167      if (tor->isRunning)
    31523168        {
    31533169          tr_swarm * s = tor->swarm;
    3154           rechokeUploads (s, now);
    3155           rechokeDownloads (s);
     3170
     3171          if (s->stats.peerCount > 0)
     3172            {
     3173              rechokeUploads (s, now);
     3174              rechokeDownloads (s);
     3175            }
    31563176        }
    31573177    }
     
    31853205
    31863206  /* disconnect if we're both seeds and enough time has passed for PEX */
    3187   if (tr_torrentIsSeed (tor) && peerIsSeed (peer))
     3207  if (tr_torrentIsSeed (tor) && tr_peerIsSeed (peer))
    31883208    return !tr_torrentAllowsPex (tor) || (now-atom->time>=30);
    31893209
     
    32863306
    32873307  removed = tr_ptrArrayRemoveSorted (&s->peers, peer, peerCompare);
    3288   --s->tor->peerCount;
    3289   --s->tor->peerFromCount[atom->fromFirst];
     3308  --s->stats.peerCount;
     3309  --s->stats.peerFromCount[atom->fromFirst];
    32903310
    32913311  if (replicationExists (s))
     
    32933313
    32943314  assert (removed == peer);
    3295   assert (s->tor->peerCount == tr_ptrArraySize (&s->peers));
    3296   assert (s->tor->peerFromCount[atom->fromFirst] >= 0);
     3315  assert (s->stats.peerCount == tr_ptrArraySize (&s->peers));
     3316  assert (s->stats.peerFromCount[atom->fromFirst] >= 0);
    32973317
    32983318  tr_peerFree (removed);
     
    33333353    removePeer (s, tr_ptrArrayNth (&s->peers, 0));
    33343354
    3335   assert (!s->tor->peerCount);
     3355  assert (!s->stats.peerCount);
    33363356}
    33373357
     
    36193639
    36203640      /* update the torrent's stats */
    3621       tor->activeWebseedCount = countActiveWebseeds (tor->swarm);
     3641      tor->swarm->stats.activeWebseedCount = countActiveWebseeds (tor->swarm);
    36223642    }
    36233643
  • trunk/libtransmission/peer-msgs.c

    r14083 r14108  
    690690***/
    691691
     692static bool
     693tr_peerMsgsCalculateActive (const tr_peerMsgs * msgs, tr_direction direction)
     694{
     695  bool is_active;
     696
     697  assert (tr_isPeerMsgs (msgs));
     698  assert (tr_isDirection (direction));
     699
     700  if (direction == TR_CLIENT_TO_PEER)
     701    {
     702      is_active = tr_peerMsgsIsPeerInterested (msgs)
     703              && !tr_peerMsgsIsPeerChoked (msgs);
     704
     705      if (is_active)
     706        assert (!tr_peerIsSeed (&msgs->peer));
     707    }
     708  else /* TR_PEER_TO_CLIENT */
     709    {
     710      if (!tr_torrentHasMetadata (msgs->torrent))
     711        is_active = true;
     712      else
     713        is_active = tr_peerMsgsIsClientInterested (msgs)
     714                && !tr_peerMsgsIsClientChoked (msgs);
     715
     716      if (is_active)
     717        assert (!tr_torrentIsSeed (msgs->torrent));
     718    }
     719
     720  return is_active;
     721}
     722
    692723bool
    693724tr_peerMsgsIsActive (const tr_peerMsgs  * msgs, tr_direction direction)
    694725{
     726  bool is_active;
     727
    695728  assert (tr_isPeerMsgs (msgs));
    696729  assert (tr_isDirection (direction));
    697730
    698   return msgs->is_active[direction];
     731  is_active = msgs->is_active[direction];
     732
     733  assert (is_active == tr_peerMsgsCalculateActive (msgs, direction));
     734
     735  return is_active;
    699736}
    700737
     
    704741                      bool           is_active)
    705742{
     743  dbgmsg (msgs, "direction [%d] is_active [%d]", (int)direction, (int)is_active);
     744
    706745  if (msgs->is_active[direction] != is_active)
    707746    {
    708       int n = msgs->torrent->activePeerCount[direction];
    709 
    710747      msgs->is_active[direction] = is_active;
    711748
    712       if (is_active)
    713         ++n;
    714       else
    715         --n;
    716       assert (0 <= n);
    717       assert (n <= msgs->torrent->peerCount);
    718 
    719       msgs->torrent->activePeerCount[direction] = n;
     749      tr_swarmIncrementActivePeers (msgs->torrent->swarm, direction, is_active);
    720750    }
    721751}
     
    724754tr_peerMsgsUpdateActive (tr_peerMsgs * msgs, tr_direction direction)
    725755{
    726   bool active;
    727 
    728   assert (tr_isPeerMsgs (msgs));
    729   assert (tr_isDirection (direction));
    730 
    731   if (direction == TR_CLIENT_TO_PEER)
    732     {
    733       active = tr_peerMsgsIsPeerInterested (msgs)
    734            && !tr_peerMsgsIsPeerChoked (msgs);
    735     }
    736   else /* TR_PEER_TO_CLIENT */
    737     {
    738       if (!tr_torrentHasMetadata (msgs->torrent))
    739         active = true;
    740       else
    741         active = tr_peerMsgsIsClientInterested (msgs)
    742              && !tr_peerMsgsIsClientChoked (msgs);
    743     }
    744 
    745   tr_peerMsgsSetActive (msgs, direction, active);
     756  const bool is_active = tr_peerMsgsCalculateActive (msgs, direction);
     757
     758  tr_peerMsgsSetActive (msgs, direction, is_active);
    746759}
    747760
     
    847860      protocolSendChoke (msgs, peer_is_choked);
    848861      msgs->chokeChangedAt = now;
     862      tr_peerMsgsUpdateActive (msgs, TR_CLIENT_TO_PEER);
    849863    }
    850864}
  • trunk/libtransmission/torrent.c

    r14083 r14108  
    12561256  unsigned int pieceUploadSpeed_Bps;
    12571257  unsigned int pieceDownloadSpeed_Bps;
     1258  struct tr_swarm_stats swarm_stats;
    12581259  int i;
    12591260
     
    12611262
    12621263  tor->lastStatTime = tr_time ();
     1264
     1265  if (tor->swarm != NULL)
     1266    tr_swarmGetStats (tor->swarm, &swarm_stats);
     1267  else
     1268    swarm_stats = TR_SWARM_STATS_INIT;
    12631269
    12641270  s = &tor->stats;
     
    12711277
    12721278  s->manualAnnounceTime = tr_announcerNextManualAnnounce (tor);
    1273 
    1274   s->peersConnected      = tor->peerCount;
    1275   s->peersSendingToUs    = tor->activePeerCount[TR_DOWN];
    1276   s->peersGettingFromUs  = tor->activePeerCount[TR_UP];
    1277   s->webseedsSendingToUs = tor->activeWebseedCount;
     1279  s->peersConnected      = swarm_stats.peerCount;
     1280  s->peersSendingToUs    = swarm_stats.activePeerCount[TR_DOWN];
     1281  s->peersGettingFromUs  = swarm_stats.activePeerCount[TR_UP];
     1282  s->webseedsSendingToUs = swarm_stats.activeWebseedCount;
    12781283  for (i=0; i<TR_PEER_FROM__MAX; i++)
    1279     s->peersFrom[i] = tor->peerFromCount[i];
     1284    s->peersFrom[i] = swarm_stats.peerFromCount[i];
    12801285
    12811286  s->rawUploadSpeed_KBps     = toSpeedKBps (tr_bandwidthGetRawSpeed_Bps (&tor->bandwidth, now, TR_UP));
  • trunk/libtransmission/torrent.h

    r14083 r14108  
    226226    int                        secondsDownloading;
    227227    int                        secondsSeeding;
    228 
    229     int                        peerCount;
    230     int                        peerFromCount[TR_PEER_FROM__MAX];
    231     int                        activePeerCount[2];
    232     int                        activeWebseedCount;
    233228
    234229    int                        queuePosition;
Note: See TracChangeset for help on using the changeset viewer.