protected virtual void HandlePieceMessage(PeerId id, PieceMessage message, PeerMessage.Releaser releaser) { id.PiecesReceived++; if (Manager.PieceManager.PieceDataReceived(id, message, out bool _, out IList <IPeer> peersInvolved)) { WritePieceAsync(message, releaser, peersInvolved); }
public void HandleMessage(PeerId id, PeerMessage message, PeerMessage.Releaser releaser) { if (!CanHandleMessages) { return; } if (message is IFastPeerMessage && !id.SupportsFastPeer) { throw new MessageException("Peer shouldn't support fast peer messages"); } if (message is ExtensionMessage && !id.SupportsLTMessages && !(message is ExtendedHandshakeMessage)) { throw new MessageException("Peer shouldn't support extension messages"); } if (message is HaveMessage have) { HandleHaveMessage(id, have); } else if (message is RequestMessage request) { HandleRequestMessage(id, request); } else if (message is PortMessage port) { HandlePortMessage(id, port); } else if (message is PieceMessage piece) { HandlePieceMessage(id, piece, releaser); } else if (message is NotInterestedMessage notinterested) { HandleNotInterested(id, notinterested); } else if (message is KeepAliveMessage keepalive) { HandleKeepAliveMessage(id, keepalive); } else if (message is InterestedMessage interested) { HandleInterestedMessage(id, interested); } else if (message is ChokeMessage choke) { HandleChokeMessage(id, choke); } else if (message is CancelMessage cancel) { HandleCancelMessage(id, cancel); } else if (message is BitfieldMessage bitfield) { HandleBitfieldMessage(id, bitfield); } else if (message is UnchokeMessage unchoke) { HandleUnchokeMessage(id, unchoke); } else if (message is HashRejectMessage hashReject) { HandleHashRejectMessage(id, hashReject); } else if (message is HashesMessage hashes) { HandleHashesMessage(id, hashes); } else if (message is HashRequestMessage hashRequest) { HandleHashRequestMessage(id, hashRequest); } else if (message is HaveAllMessage haveall) { HandleHaveAllMessage(id, haveall); } else if (message is HaveNoneMessage havenone) { HandleHaveNoneMessage(id, havenone); } else if (message is RejectRequestMessage rejectrequest) { HandleRejectRequestMessage(id, rejectrequest); } else if (message is SuggestPieceMessage suggestpiece) { HandleSuggestedPieceMessage(id, suggestpiece); } else if (message is AllowedFastMessage allowedfast) { HandleAllowedFastMessage(id, allowedfast); } else if (message is ExtendedHandshakeMessage extendedhandshake) { HandleExtendedHandshakeMessage(id, extendedhandshake); } else if (message is LTMetadata metadata) { HandleLtMetadataMessage(id, metadata); } else if (message is LTChat chat) { HandleLtChat(id, chat); } else if (message is PeerExchangeMessage peerexchange) { HandlePeerExchangeMessage(id, peerexchange); } else if (message is HandshakeMessage handshake) { HandleHandshakeMessage(id, handshake); } else if (message is ExtensionMessage extension) { HandleGenericExtensionMessage(id, extension); } else { throw new MessageException($"Unsupported message found: {message.GetType ().Name}"); } if (!(message is PieceMessage)) { releaser.Dispose(); } ConnectionManager.TryProcessQueue(Manager, id); }
static async void HandleReceivedMessage(PeerId id, TorrentManager torrentManager, PeerMessage message, PeerMessage.Releaser releaser = default) { await ClientEngine.MainLoop; if (!id.Disposed) { id.LastMessageReceived.Restart(); torrentManager.Mode.HandleMessage(id, message, releaser); } else { releaser.Dispose(); } }