//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- static public void Update() { ProcessReceive(); if (!packetAvalable) return; packetBuffer.index = 0; ClientPacketTypes packetType = (ClientPacketTypes)packetBuffer.GetByte(); PacketProccessor[packetType](packetBuffer); packetBuffer.Reset(); packetAvalable = false; }
//Блокирует приложение до получения с сервера пакета определенного типа, уничтожает все пришедшие пакеты другого типа--------------------------------------------------------------------------------------------------------------------------------------------------------- private static bool WaitForPacket(ClientPacketTypes packetType, long maxWaitTime) { Stopwatch waitTimer = new Stopwatch(); waitTimer.Start(); while (true) { if (socketBuffer.length == 0) { if (waitTimer.ElapsedMilliseconds > maxWaitTime) { Client.Log("Ответ от сервера не пришел (socketBuffer.length == 0) " + packetType, LogTypes.Error, LogParts.UserApp); return(false); } } else { if (waitTimer.ElapsedMilliseconds > maxWaitTime * 2) { Client.Log("Ответ от сервера не пришел (socketBuffer.length == " + socketBuffer.length + " " + packetType, LogTypes.Error, LogParts.UserApp); return(false); } } ProcessReceive(); if (!packetAvalable) { continue; } packetBuffer.index = 0; ClientPacketTypes receivePacketType = (ClientPacketTypes)packetBuffer.GetByte(); if (packetType == receivePacketType) { return(true); } else { Client.Log("Пропущен пакет " + (ClientPacketTypes)receivePacketType, LogTypes.None, LogParts.UserApp); } packetBuffer.Reset(); packetAvalable = false; } }
/// <summary> /// Writes a packet to the server to figure out what to do. /// </summary> /// <param name="msg"></param> /// <param name="serverPacket"></param> private void writePackageInfo(NetOutgoingMessage msg, ClientPacketTypes clientPacket) { var packet = new PacketInfo(SenderPackets.Server, clientPacket, ServerPacketTypes.None_None); msg.WriteAllProperties(packet); }