/// <summary> /// Initializes a new instance of the <see cref="Peer" /> class. /// </summary> /// <param name="communicator">The communicator.</param> /// <param name="pieceManager">The piece manager.</param> /// <param name="localPeerId">The local peer identifier.</param> /// <param name="peerId">The peer identifier.</param> public Peer(PeerCommunicator communicator, PieceManager pieceManager, string localPeerId, string peerId = null) { communicator.CannotBeNull(); pieceManager.CannotBeNull(); localPeerId.CannotBeNullOrEmpty(); this.PeerId = peerId; this.localPeerId = localPeerId; this.BitField = new bool[pieceManager.PieceCount]; this.HandshakeState = peerId == null ? HandshakeState.SentButNotReceived : HandshakeState.SendAndReceived; this.SeedingState = SeedingState.Choked; this.LeechingState = LeechingState.Uninterested; this.Downloaded = 0; this.Uploaded = 0; this.communicator = communicator; this.communicator.MessageReceived += this.Communicator_MessageReceived; this.communicator.CommunicationError += this.Communicator_CommunicationError; this.pieceManager = pieceManager; this.pieceManager.PieceCompleted += this.PieceManager_PieceCompleted; this.Endpoint = this.communicator.Endpoint; this.StartSending(); this.StartDownloading(); this.StartUploading(); this.StartKeepingConnectionAlive(); // send handshake this.EnqueueSendMessage(new HandshakeMessage(this.pieceManager.TorrentInfoHash, localPeerId, HandshakeMessage.ProtocolName)); }