public static void SendMessage(Socket socket, PeerMessage message, object state, MessageSentCallback callback) { byte[] buffer = message.ToBytes(); SendMessageState data = SendCache.Get().Init(socket, buffer, 0, buffer.Length, state, callback); SendMessageBase(data); }
protected virtual void SendMessage(PeerState peer, PeerMessage message) { if (Stopping) return; MessageIO.SendMessage(peer.Socket, message, peer, MessageSent); }
protected virtual void HandleMessage(PeerMessage message, PeerState peer) { //проверка на типа съобщение и извикване на съответния обработващ метод if (message is HandshakeMessage) HandleHandshake((HandshakeMessage)message, peer); else if (message is ChokeMessage) HandleChoke((ChokeMessage)message, peer); else if (message is UnchokeMessage) HandleUnchoke((UnchokeMessage)message, peer); else if (message is InterestedMessage) HandleInterested((InterestedMessage)message, peer); else if (message is NotInterestedMessage) HandleNotInterested((NotInterestedMessage)message, peer); else if (message is BitfieldMessage) HandleBitfield((BitfieldMessage)message, peer); else if (message is HaveMessage) HandleHave((HaveMessage)message, peer); else if (message is PieceMessage) HandlePiece((PieceMessage)message, peer); else if (message is RequestMessage) HandleRequest((RequestMessage)message, peer); //приемане на следващото съобщение от пиъра ReceiveMessage(peer); }
protected virtual void MessageReceived(bool success, PeerMessage message, object state) { if (Stopping) return; var peer = (PeerState)state; try { if (success) { Monitor.Received(message.MessageLength); HandleMessage(message, peer); } else { CloseSocket(peer); ConnectPeer(peer.EndPoint); } } catch(Exception e) { OnRaisedException(e); } }
private static void HandshakeReceived(bool success, PeerMessage message, object state) { var peer = (PeerState) state; var handshake = (HandshakeMessage) message; if (success) { peer.ReceivedHandshake = true; peer.ID = handshake.PeerID; if (peer.ID == Global.Instance.PeerId) return; PeerConnectedCallback callback; if (!InnerDictionary.TryGetValue(handshake.InfoHash, out callback)) ClosePeerSocket(peer); else callback(peer); } else ClosePeerSocket(peer); }