/// <summary> /// Processes the received message. /// </summary> /// <param name="message">The message.</param> private void ProcessRecievedMessage(HandshakeMessage message) { message.CannotBeNull(); if (this.HandshakeState == HandshakeState.None || this.HandshakeState == HandshakeState.SentButNotReceived) { if (message.InfoHash == this.pieceManager.TorrentInfoHash && message.ProtocolString == HandshakeMessage.ProtocolName && message.PeerId.IsNotNullOrEmpty() && message.PeerId != this.localPeerId) { if (this.HandshakeState == HandshakeState.None) { this.HandshakeState = HandshakeState.ReceivedButNotSent; this.PeerId = message.PeerId; // send a handshake this.EnqueueSendMessage(new HandshakeMessage(this.pieceManager.TorrentInfoHash, this.localPeerId)); // send a bit field this.EnqueueSendMessage(new BitFieldMessage(this.pieceManager.BitField.Select(x => x == PieceStatus.Present).ToArray())); } else if (this.HandshakeState == HandshakeState.SentButNotReceived) { this.HandshakeState = HandshakeState.SendAndReceived; this.PeerId = message.PeerId; // send a bit field this.EnqueueSendMessage(new BitFieldMessage(this.pieceManager.BitField.Select(x => x == PieceStatus.Present).ToArray())); } } else { this.OnCommunicationErrorOccurred(this, new PeerCommunicationErrorEventArgs("Invalid handshake message.", true)); } } else { this.OnCommunicationErrorOccurred(this, new PeerCommunicationErrorEventArgs("Invalid message sequence.", true)); } }