Esempio n. 1
0
        public static void HandleEchoPacket(byte[] packet_buf)
        {
            EchoPacket packet = EchoPacket.CreateBuilder().MergeFrom(packet_buf).Build();

            Console.WriteLine(packet.ToString());
            Console.WriteLine(Util.ByteStringToString(packet.Name, Encoding.Default));
        }
Esempio n. 2
0
        private static void SendEcho(IPAddress ip)
        {
            try
            {
                Console.WriteLine($"Sending echo to {ip}...");

                var packet = new EchoPacket(1, EchoPacketType.Request, new byte[] { 0x01, 0x02, 0x03 });
                var bytes  = BinarySerializer.Serialize(packet);

                var sw = Stopwatch.StartNew();
                socket.SendTo(bytes, new IPEndPoint(ip, 50000));
                int len = socket.Receive(recvBuffer);
                sw.Stop();

                var recvPacket = BinarySerializer.Deserialize <EchoPacket>(recvBuffer);
                if (recvPacket.Id == packet.Id && recvPacket.Type == EchoPacketType.Response)
                {
                    Console.WriteLine($"Received echo. Time: {sw.Elapsed.TotalMilliseconds} ms");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    public void TestSend()
    {
        var origin = new EchoPacket
        {
            Arg1 = UnityEngine.Random.Range(1000, 10000),
            Arg2 = Guid.NewGuid().
                   ToString(),
            Args = new List <int>
            {
                UnityEngine.Random.Range(10, 100),
                UnityEngine.Random.Range(101, 200),
            }
        };


        using (var stream = new MemoryStream())
        {
            using (var writer = new BinaryWriter(stream))
            {
                var formatter = new BinaryFormatter();
                formatter.Serialize(stream, origin);

                UInt16       size   = (ushort)((ushort)stream.Length - (ushort)ClientCommon.BODY_LENGTH_SIZE);
                PacketStream packet = new PacketStream((int)(ClientCommon.BODY_LENGTH_SIZE + (int)stream.Length));
                packet.Push(size);
                packet.Push(stream.GetBuffer(), (int)stream.Length);

                mNetworkManager.Send(packet);
            }
        }
    }
Esempio n. 4
0
 public void EchoHandler(EchoPacket echo)
 {
     if (echo != null)
     {
         Console.WriteLine($"Server Echoed: {echo.ClientDirection}");
     }
 }
Esempio n. 5
0
        protected override void ConstructeResponse()
        {
            EchoPacket echo = new EchoPacket();

            echo.Parse(_recv);
            // We send the echo packet to check the ping
            _sendingBuffer = echo.GenerateResponse();

            QRSession client = (QRSession)_session.GetInstance();

            GameServer.UpdateServer(
                client.RemoteEndPoint,
                _gameServer.ServerData.KeyValue["gamename"],
                _gameServer
                );
        }
Esempio n. 6
0
        private async Task Accept()
        {
            ListenerFactory listenerFactory = new ListenerFactory();

            listenerFactory.Uri = new Uri(webSockets ? "http://localhost:5001/" : "tcp://127.0.0.1:7000");
            Listener listener = listenerFactory.Create();

            for (int i = 0; i < Connections; i++)
            {
                Transporter transporter = await listener.Execute(cancellationTokenSource.Token);

                EchoPacket echoPacket = new EchoPacket(Bytes, Iterations);
                tasks.Add(ExecuteRead(transporter, echoPacket));
                tasks.Add(ExecuteWrite(transporter, echoPacket));
                transporters.Add(transporter);
            }
        }
        protected override void ConstructeResponse()
        {
            EchoPacket packet = new EchoPacket();

            packet.Parse(_recv);
            if (_session.InstantKey != packet.InstantKey)
            {
                _session.SetInstantKey(packet.InstantKey);
            }

            // We send the echo packet to check the ping
            _sendingBuffer = packet.BuildResponse();

            QRSession client = (QRSession)_session.GetInstance();

            GameServer.UpdateServer(
                client.RemoteEndPoint,
                _gameServer.ServerData.KeyValue["gamename"],
                _gameServer
                );
        }
Esempio n. 8
0
        private void HandleClientUpdate(UpdatePacket update, NetPeer peer)
        {
            //process and echo it back..
            // let's echo back the fact we received the message

            if (update != null)
            {
                //we got a client update so let's go ahead and update them...
                //first find them
                string key          = peer.EndPoint.ToString();
                var    playerRecord = _knownPlayers[key]; // temp record
                if (update.PlayerAction == 0)
                {
                    playerRecord.PlayerLocation.Y += 5; // = new Point(playerRecord.PlayerLocation.X, playerRecord.PlayerLocation.Y - 5);
                }
                if (update.PlayerAction == 1)
                {
                    playerRecord.PlayerLocation.Y -= 5;
                }
                if (update.PlayerAction == 2)
                {
                    playerRecord.PlayerLocation.X -= 5;
                }
                if (update.PlayerAction == 3)
                {
                    playerRecord.PlayerLocation.X += 5;
                }

                Console.WriteLine($"Updated Player {playerRecord.PlayerNumber} Location to { playerRecord.PlayerLocation.X},{playerRecord.PlayerLocation.Y}");
                Console.WriteLine($"Echo'd Peer:{peer.EndPoint.ToString()} with {update.PlayerAction}");
                EchoPacket ep = new EchoPacket()
                {
                    ClientDirection = update.PlayerAction
                };
                _packetProcessor.Send <EchoPacket>(peer, ep, DeliveryMethod.ReliableOrdered);
            }
        }