public void ShowResponse(Packet.Packet packet) { if (packet == null) { return; } Console.WriteLine("Response type: " + packet.Type); switch (packet.Type) { case PacketType.ConnectResponse: var connectResponse = (ConnectResponse)packet; Console.WriteLine("Connect response type: " + connectResponse.ConnectResponseType); Console.WriteLine("Reason: " + connectResponse.Reason); break; case PacketType.PingRequest: var pingRequest = (PingRequest)packet; Console.WriteLine("Ping request: " + pingRequest.Random); break; case PacketType.PingResponse: var pingResponse = (PingResponse)packet; Console.WriteLine("Ping response: " + pingResponse.Verify); break; } }
/// <summary> /// Parses the payload data in a request packet /// </summary> /// <param name="data">The request payload data</param> /// <returns>The response packet, or null if no response</returns> public Packet.Packet ParsePacket(byte[] data, IPEndPoint sender) { // first byte is packet type var packetType = (PacketType)data[0]; Packet.Packet packet = null; switch (packetType) { case PacketType.ConnectRequest: Console.WriteLine("Packet received: " + packetType); packet = new ConnectRequest(data, sender); break; case PacketType.PingResponse: Console.WriteLine("Packet received: " + packetType); packet = new PingResponse(data, sender); break; } if (packet == null) { return(null); } return(packet.Response); }
/// <summary> /// Sends a packet to the server /// </summary> /// <param name="packet">The packet to send, usually the InputState</param> public void SendPacket(Packet.Packet packet) { var packetBytes = packet.Serialize(); //UdpClient.Send(packetBytes, packetBytes.Length); UdpClient.BeginSend(packetBytes, packetBytes.Length, SentData, UdpClient); }
/// <summary> /// Only handles response packets atm? /// </summary> /// <param name="responseBytes">The server packet sent as a response from a previous request</param> /// <returns>The parsed packet object</returns> public Packet.Packet GetPacket(byte[] responseBytes) { var packetType = (PacketType)responseBytes[0]; Packet.Packet packet = null; switch (packetType) { case PacketType.ConnectResponse: packet = new ConnectResponse(responseBytes); break; case PacketType.PingRequest: packet = new PingRequest(responseBytes); break; case PacketType.GameState: packet = new Packet.GameState(responseBytes); break; case PacketType.GameStateDiff: packet = new GameStateDiff(responseBytes); break; } ShowResponse(responseBytes); return(packet); }
/// <summary> /// Parses the data sent from the server into a packet /// </summary> /// <param name="data">The request payload data</param> /// <returns>The response packet, or null if no response</returns> public Packet.Packet ParsePacket(byte[] data, IPEndPoint sender) { // first byte is packet type var packetType = (PacketType)data[0]; Packet.Packet packet = null; //Console.WriteLine("Packet received: " + packetType); switch (packetType) { case PacketType.GameStateDiff: var gameStateDiff = new GameStateDiff(data); ProcessUpdate(gameStateDiff); break; } if (packet == null) { return(null); } return(packet.Response); }
/// <summary> /// Serializes a packet and sends it over the wire to a client /// </summary> /// <param name="packet">The packet to convert to serialized bytes</param> /// <param name="sender">The remote client to send the data to</param> public void SendData(Packet.Packet packet, IPEndPoint sender) { var responseBytes = packet.Serialize(); UdpServer.BeginSend(responseBytes, responseBytes.Length, sender, SentCallback, UdpServer); }