Ejemplo n.º 1
0
        private void ParseData(NetIncomingMessage mes)
        {
            MessageType messageType = (MessageType)mes.ReadByte();

            switch (messageType)
            {
            case MessageType.NewClient:
                // mes.SenderEndPoint.Address.GetHashCode() != Peer.Configuration.LocalAddress.GetHashCode()
                //    && mes.SenderEndPoint.GetHashCode() != Peer.Socket.RemoteEndPoint.GetHashCode()
                //    &&
                int        ipLength          = mes.ReadInt32();
                IPAddress  clientIP          = new IPAddress(mes.ReadBytes(ipLength));
                int        clientPort        = mes.ReadInt32();
                IPEndPoint newClientEndpoint = new IPEndPoint(clientIP, clientPort);

                if (newClientEndpoint != Peer.Socket.LocalEndPoint)
                {
                    Peer.Connect(newClientEndpoint, Peer.CreateMessage());
                }
                Logman.Log("Successfully connected to a new client.");

                break;

            case MessageType.UpdateTorrentList:
                Logman.Log($"Received new torrent list from {mes.SenderEndPoint.ToString()}");
                Torrent[] newTorrents = TorrentSerializer.Deserialize(mes.Data);
                if (TorrentListReceived != null)
                {
                    TorrentListReceived.Invoke(newTorrents);
                }
                break;
            }
        }
Ejemplo n.º 2
0
        public void SendTorrentListUpdate(Torrent[] torList, NetConnection target)
        {
            if (Peer.Connections.Count > 0)
            {
                NetOutgoingMessage newTorrentList = Peer.CreateMessage();
                byte[]             torBytes       = TorrentSerializer.Serialize(torList);
                newTorrentList.Write((byte)MessageType.UpdateTorrentList);
                newTorrentList.Write(torBytes);

                Peer.SendMessage(newTorrentList, target, NetDeliveryMethod.ReliableOrdered, 0);
            }
        }