Exemple #1
0
        public sealed override void Update()
        {
            UpdateLock.Reset(); // Signal that the UpdateThread is alive.
            try
            {
                while (!UpdateToken.IsCancellationRequested && Stream.IsConnected)
                {
                    ConnectionLock.Reset(); // Signal that we are handling pending client data.
                    try
                    {
                        while (Stream.TryReadPacket(out var packetToReceive))
                        {
                            HandlePacket(packetToReceive);

#if DEBUG
                            Received.Enqueue(packetToReceive);
                            if (Received.Count >= QueueSize)
                            {
                                Received.Dequeue();
                            }
#endif
                        }
                        while (PacketsToSend.TryDequeue(out var packetToSend))
                        {
                            Stream.SendPacket(packetToSend);

#if DEBUG
                            Sended.Enqueue(packetToSend);
                            if (Sended.Count >= QueueSize)
                            {
                                Sended.Dequeue();
                            }
#endif
                        }
                    }
                    finally
                    {
                        ConnectionLock.Set(); // Signal that we are not handling anymore pending client data.
                    }

                    Thread.Sleep(100); // 100 calls per second should not be too often?
                }
            }
            finally
            {
                UpdateLock.Set();                                                // Signal that the UpdateThread is finished

                if (!UpdateToken.IsCancellationRequested && !Stream.IsConnected) // Leave() if the update cycle stopped unexpectedly
                {
                    LeaveAsync();
                }
            }
        }