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;
            }
        }