Example #1
0
        private void serverTick()
        {
            double keepAlive = 0.0;

            while (isRunning)
            {
                double tickLength = 1.0 / tickrate;

                // send keep alive to clients 10 times per second
                keepAlive += tickLength;
                if (keepAlive >= 0.1)
                {
                    keepAlive = 0.0;
                    for (int i = 0; i < clientSlots.Length; i++)
                    {
                        if (clientSlots[i] != null)
                        {
                            sendKeepAlive(clientSlots[i]);
                        }
                    }
                }

                // disconnect any clients which have not responded for 10 seconds
                for (int i = 0; i < clientSlots.Length; i++)
                {
                    if (clientSlots[i] == null)
                    {
                        continue;
                    }

                    double timeRemaining = DateTime.Now.GetTotalSeconds() - clientSlots[i].lastResponseTime;
                    if ((DateTime.Now.GetTotalSeconds() - clientSlots[i].lastResponseTime) >= Defines.NETCODE_TIMEOUT_SECONDS)
                    {
                        if (OnClientDisconnected != null)
                        {
                            OnClientDisconnected(clientSlots[i]);
                        }

                        log("Client {0} timed out", NetcodeLogLevel.Debug, clientSlots[i].RemoteEndpoint.ToString());
                        disconnectClient(clientSlots[i]);
                    }
                }

                // process datagram queue
                while (datagramQueue.Count > 0)
                {
                    Datagram packet = datagramQueue.Dequeue();
                    processDatagram(packet.payload, packet.payloadSize, packet.sender);
                    packet.Release();
                }

                // sleep until next tick
                Thread.Sleep((int)(tickLength * 1000));
            }
        }