示例#1
0
        // Called by the Session class for peers connecting through the server and in StartPeerConnectionThread() method for peers
        // connected to directly
        private void AddPeerPrivate(Sockets.Socket socket, Sockets.NetworkStream netStream, PeerInformation peerInformation)
        {
            try
            {
                if (Config.ActiveConfig.MaxPeersConnected < this.mPeers.Count)
                {
                    socket.Close();
                    return;
                }

                bool connect = true;

                if (Config.ActiveConfig.OnlyOneConnectionFromEachIP)
                {
                    foreach (Peer connectedPeer in this.mPeers)
                    {
                        if (connectedPeer.Information.IP.Equals(peerInformation.IP))
                        {
                            connect = false;
                            break;
                        }
                    }
                }

                if (!connect)
                {
                    socket.Close();
                    return;
                }

                if (!this.downloadFile.Bitfield.AllFalse)
                {
                    PeerProtocol.SendBitfieldMessage(netStream, this.downloadFile);
                }

                Peer peer = new Peer(this.infofile, this.downloadFile, socket, netStream, peerInformation);
                peer.Disconnected += new PeerDisconnectedCallback(peer_Disconnected);

                Config.LogDebugMessage("Connection accepted from: " + peer.ToString());

                // add to download and upload manager
                this.mDownloadStrategy.HookPeerEvents(peer);
                this.uploadManager.AddPeer(peer);
                this.peerManager.AddPeer(peer);

                if (this.PeerConnected != null)
                {
                    this.PeerConnected(this, peer, true);
                }

                this.mPeers.Add(peer);
            }
            catch (System.Exception e)
            {
                Config.LogException(e);
            }
        }