Example #1
0
        /// <summary>
        /// Removes a peer from the peer list
        /// </summary>
        /// <param name="Peer">The peer to remove</param>
        public void RemovePeer(P2pPeer Peer)
        {
            // Lock peer list to prevent race conditions
            lock (Peers)
            {
                // Stop this peer
                Peer.Stop();

                // Remove this peer from our peer list
                Peers.RemoveAll(x => x == Peer);
            }
        }
Example #2
0
        // Handles a new peer client
        private void AddPeer(P2pPeer Peer)
        {
            // Lock peer list to prevent race conditions
            lock (Peers)
            {
                // Check that we have space for this peer
                if (Peers.Count < MaxConnections)
                {
                    Peer.Start();
                    Peers.Add(Peer);
                }

                // No space available, close connection
                else
                {
                    Peer.Stop();
                }
            }
        }
Example #3
0
        // Accepts incoming connections if we are able
        private void AcceptPeerConnection()
        {
            // Create a wait handle array so we can cancel this thread if need be
            WaitHandle[] Wait = new[] { ReadyEvent, StopEvent };
            while (0 == WaitHandle.WaitAny(Wait))
            {
                // Check if stopped
                if (StopEvent.WaitOne(0))
                {
                    break;
                }

                // Lock our connection queue to prevent any race conditions
                lock (ConnectionQueue)
                {
                    // Connection queue has entries, accept one
                    if (ConnectionQueue.Count > 0)
                    {
                        // Dequeue the new peer in line
                        var Connection = ConnectionQueue.Dequeue();

                        // Create a peer instance
                        var Peer = new P2pPeer(this, Connection, PeerDirection.IN);

                        // Handle this connection
                        AddPeer(Peer);
                    }

                    // There are no entries in the connection queue
                    else
                    {
                        // No peers in line, reset ready event
                        ReadyEvent.Reset();
                        continue;
                    }
                }
            }
        }