예제 #1
0
        /// <summary>
        /// Thread that will be processing incoming data.
        /// </summary>
        void ThreadFunction()
        {
            for (; ; )
            {
            mTime = DateTime.Now.Ticks / 10000;

            // Accept incoming connections
            while (mListener != null && mListener.Pending())
            {
                TcpProtocol tc = new TcpProtocol();
                tc.data = (long)(-1);
                tc.StartReceiving(mListener.AcceptSocket());
                mTcp.Add(tc);
            }

            Buffer buffer = null;

            // Process incoming TCP packets
            for (int i = 0; i < mTcp.size; ++i)
            {
                TcpProtocol tc = mTcp[i];

                while (tc.ReceivePacket(out buffer))
                {
                    try
                    {
                        if (!ProcessPacket(buffer, tc))
                        {
                            RemoveServer(tc);
                            tc.Disconnect();
                        }
                    }
            #if STANDALONE
                    catch (System.Exception ex)
                    {
                        Console.WriteLine("ERROR: " + ex.Message);
                        RemoveServer(tc);
                        tc.Disconnect();
                    }
            #else
                    catch (System.Exception)
                    {
                        RemoveServer(tc);
                        tc.Disconnect();
                    }
            #endif
                    if (buffer != null)
                    {
                        buffer.Recycle();
                        buffer = null;
                    }
                }
            }

            // We only want to send instant updates if the number of players is under a specific threshold
            if (mTcp.size > instantUpdatesClientLimit) mInstantUpdates = false;

            // Send the server list to all connected clients
            for (int i = 0; i < mTcp.size; ++i)
            {
                TcpProtocol tc = mTcp[i];
                long customTimestamp = (long)tc.data;

                // Timestamp of -1 means we don't want updates to be sent
                if (tc.stage != TcpProtocol.Stage.Connected || customTimestamp == -1) continue;

                // If timestamp was set then the list was already sent previously
                if (customTimestamp != 0)
                {
                    // List hasn't changed -- do nothing
                    if (customTimestamp >= mLastChange) continue;

                    // Too many clients: we want the updates to be infrequent
                    if (!mInstantUpdates && customTimestamp + 4000 > mTime) continue;
                }

                // Create the server list packet
                if (buffer == null)
                {
                    buffer = Buffer.Create();
                    BinaryWriter writer = buffer.BeginPacket(Packet.ResponseServerList);
                    mList.WriteTo(writer);
                    buffer.EndPacket();
                }
                tc.SendTcpPacket(buffer);
                tc.data = mTime;
            }

            if (buffer != null)
            {
                buffer.Recycle();
                buffer = null;
            }
            Thread.Sleep(1);
            }
        }