Exemplo n.º 1
0
        // Adds a new peer to the peer list
        private void AddPeer(P2pPeer P2pPeer)
        {
            // Do not add peer if not running
            if (Stopped)
            {
                RemovePeer(P2pPeer);
                return;
            }

            // Lock the peer list to prevent race conditions
            lock (PeerList)
            {
                // Check if this peer already exists
                if (PeerList.Any(x => x.P2pPeer == P2pPeer))
                {
                    return;
                }

                // Create a new local peer
                var Peer = new Peer(this, P2pPeer);

                // Add the peer to our peer list
                PeerList.Add(Peer);

                // Peer is outgoing
                if (P2pPeer.Direction == PeerDirection.OUT)
                {
                    // Request handshake
                    RequestHandshake(Peer);
                }
            }
        }
Exemplo n.º 2
0
        internal Peer(Node Node, P2pPeer P2pPeer)
        {
            // Assign variable references
            this.Node    = Node;
            this.P2pPeer = P2pPeer;

            // Set peer values
            Validated = false;
            State     = PeerState.BEFORE_HANDSHAKE;
            LastSeen  = GetTimestamp();
            Address   = P2pPeer.Address;
            if (P2pPeer.Direction == PeerDirection.OUT)
            {
                Port = (uint)P2pPeer.Port;
            }

            // Setup packet buffer variables
            Buffer    = new byte[0];
            ReadState = PacketReadState.HEADER;
            Header    = new PacketHeader();

            // Ready our threading objects
            IncomingBufferQueue = new Queue <byte[]>();
            ReadyEvent          = new ManualResetEvent(false);
            StopEvent           = new ManualResetEvent(false);
            PacketBufferThread  = new Thread(ReadPacketBuffer);

            // Start thread
            PacketBufferThread.Start();
        }
Exemplo n.º 3
0
 // Removes a peer based on a P2P peer object reference
 private void RemovePeer(P2pPeer P2pPeer)
 {
     lock (PeerList)
     {
         OnPeerDisconnected(PeerList.First(x => x.P2pPeer == P2pPeer));
         PeerList.RemoveAll(x => x.P2pPeer == P2pPeer);
     }
 }
Exemplo n.º 4
0
        // Disposes of this peer, stopping any threads
        internal void Dispose()
        {
            // Stop packet buffer thread
            StopEvent.Set();
            PacketBufferThread.Join();

            // Kill underlying P2P peer
            P2pPeer.Stop();
        }
Exemplo n.º 5
0
 // Adds a new peer based on a P2P peer object reference
 private Peer AddPeer(P2pPeer P2pPeer)
 {
     lock (PeerList)
     {
         var Peer = new Peer(this, P2pPeer);
         PeerList.Add(Peer);
         return(Peer);
     }
 }
Exemplo n.º 6
0
 // Removes a peer from the peer list based on a P2pPeer object
 private void RemovePeer(P2pPeer Peer)
 {
     // Lock peer list to prevent race conditions
     lock (PeerList)
     {
         // Check if this peer exists
         if (PeerList.Any(x => x.P2pPeer == Peer))
         {
             // Remove peer from the peer list
             RemovePeer(PeerList.First(x => x.P2pPeer == Peer));
         }
     }
 }
Exemplo n.º 7
0
 // Gets a peer based on a P2P peer object reference
 private Peer GetPeer(P2pPeer P2pPeer)
 {
     lock (PeerList)
     {
         try
         {
             return(PeerList.First(x => x.P2pPeer == P2pPeer));
         }
         catch
         {
             return(AddPeer(P2pPeer));
         }
     }
 }
Exemplo n.º 8
0
        // Adds a packet of data to a peer's buffer
        private void AddData(P2pPeer P2pPeer, byte[] Buffer)
        {
            // Do nothing if not running
            if (Stopped)
            {
                return;
            }

            // Lock peer list to prevent race conditions
            lock (PeerList)
            {
                // Check if peer list containers this peer
                if (!PeerList.Any(x => x.P2pPeer == P2pPeer))
                {
                    AddPeer(P2pPeer);
                }

                // Add data to this peer's buffer
                PeerList.First(x => x.P2pPeer == P2pPeer).AddData(Buffer);
            }
        }
Exemplo n.º 9
0
        internal Peer(Node Node, P2pPeer P2pPeer)
        {
            // Assign variable references
            this.Node    = Node;
            this.P2pPeer = P2pPeer;

            // Set last seen time to now
            LastSeen = GetTimestamp();

            // Setup packet buffer variables
            Buffer    = new byte[0];
            State     = ReadState.Header;
            Header    = new PacketHeader();
            Validated = false;

            // Ready our threading objects
            IncomingBufferQueue = new Queue <byte[]>();
            ReadyEvent          = new ManualResetEvent(false);
            StopEvent           = new ManualResetEvent(false);
            PacketBufferThread  = new Thread(ReadPacketBuffer);

            // Start thread
            PacketBufferThread.Start();
        }
Exemplo n.º 10
0
 // Sends a message to our underlying P2P peer object
 internal void SendMessage(Packet Packet)
 {
     P2pPeer.SendMessage(Packet.Serialize());
     Node.OnPacketSent(this, Packet);
 }