void ReadPacket(NetClient client) { bool success = false; int started = p.TimeMillisecondsFromStart(); int timeout = 2000; while (p.TimeMillisecondsFromStart() < started + timeout) { if (success) { //Return if already received answer from server querySuccess = true; return; } NetIncomingMessage msg; msg = client.ReadMessage(); if (msg == null) { //Message empty - skip processing. continue; } Packet_Server packet = new Packet_Server(); Packet_ServerSerializer.DeserializeBuffer(msg.message, msg.messageLength, packet); switch (packet.Id) { case Packet_ServerIdEnum.QueryAnswer: //Got answer from server. Process it. result.Name = packet.QueryAnswer.Name; result.MOTD = packet.QueryAnswer.MOTD; result.PlayerCount = packet.QueryAnswer.PlayerCount; result.MaxPlayers = packet.QueryAnswer.MaxPlayers; result.PlayerList = packet.QueryAnswer.PlayerList; result.Port = packet.QueryAnswer.Port; result.GameMode = packet.QueryAnswer.GameMode; result.Password = packet.QueryAnswer.Password; result.PublicHash = packet.QueryAnswer.PublicHash; result.ServerVersion = packet.QueryAnswer.ServerVersion; result.MapSizeX = packet.QueryAnswer.MapSizeX; result.MapSizeY = packet.QueryAnswer.MapSizeY; result.MapSizeZ = packet.QueryAnswer.MapSizeZ; result.ServerThumbnail = packet.QueryAnswer.ServerThumbnail; success = true; continue; case Packet_ServerIdEnum.DisconnectPlayer: serverMessage = packet.DisconnectPlayer.DisconnectReason; //End method as server kicked us out return; default: //Drop all other packets sent by server (not relevant) continue; } } //Set timeout message if query did not finish in time serverMessage = "Timeout while querying server!"; }
public static byte[] Serialize(Packet_Server packet, IntRef retLength) { CitoMemoryStream ms = new CitoMemoryStream(); Packet_ServerSerializer.Serialize(ms, packet); byte[] data = ms.ToArray(); retLength.value = ms.Length(); return(data); }
public void TryReadPacket(byte[] data, int dataLength) { Packet_Server packet = new Packet_Server(); Packet_ServerSerializer.DeserializeBuffer(data, dataLength, packet); ProcessInBackground(packet); ProcessPacketTask task = new ProcessPacketTask(); task.game = game; task.packet_ = packet; game.QueueActionCommit(task); game.LastReceivedMilliseconds = game.currentTimeMilliseconds; //return lengthPrefixLength + packetLength; }