Example #1
0
 public void RequestPeers(int count)
 {
     if (Status == PeerStatus.Connected)
     {
         var message = new RequestPeersMessage(count);
         if (ConnectionMode == ConnectionMode.Direct)
         {
             //проверка на дисконнект
             try
             {
                 Connection.SendObject(message.GetType().Name, message);
             }
             catch
             {
                 Status = PeerStatus.Disconnected;
                 Connect();
             }
         }
         else
         {
             tracker.SendMessageToPeer(message, this);
         }
         PeersRequestsCount++;
     }
     else
     {
         Connect();
     }
 }
Example #2
0
 public void RequestPeersFromTracker(int count)
 {
     if (Status == TrackerStatus.Connected)
     {
         var message = new RequestPeersMessage(count);
         try
         {
             Connection.SendObject(message.GetType().Name, message);
         }
         catch
         {
             Status = TrackerStatus.Disconnected;
             Connect();
         }
     }
     else
     {
         Connect();
     }
 }
Example #3
0
        private void OnRequestPeersMessageDirect(RequestPeersMessage m)
        {
            var peers = allPeers.ToList();

            peers.Sort(peerComparer);

            var peersToSend = peers.Where(peer => peer.Status == PeerStatus.Connected && peer.Hash != Hash);

            peersToSend = peersToSend.Take(m.Count);

            var peersAddresses = peersToSend.Select(peer => peer.Address);
            var message        = new PeersMessage(peersAddresses.ToList());

            Connection.SendObject(typeof(PeersMessage).Name, message);


            foreach (var peer in peersToSend)
            {
                peer.SendToOthersCount++;
            }
        }