服务器套接字问题(示例代码注释)

发布于 2024-08-07 09:59:51 字数 2494 浏览 1 评论 0原文

我正在为游戏服务器开发一些额外的东西,但我不明白我收到的代码(但它有效)。如果有人可以向我解释为什么(在 receivedata 方法中)清除套接字列表然后再次填充,我会非常高兴。我就是看不出重点。

this.mReceiveDataThread = new System.Threading.Thread(this.ReceiveData);

           protected void ReceiveData()
        {
            List<System.Net.Sockets.Socket> sockets = new List<System.Net.Sockets.Socket>();
            bool noClients = false;
            while (true)
            {
                if (noClients)
                {
                    System.Threading.Thread.Sleep(RECEIVE_DATA_NO_CLIENT_WAIT_TIME);
                    noClients = false;
                }
                this.mClientsSynchronization.AcquireReaderLock(-1);
                try
                {
                    if (this.mClients == null || this.mClients.Count == 0)
                    {
                        noClients = true;
                        continue;
                    }
                    sockets.Clear(); 
                    for (int i = 0; i < this.mClientsValues.Count; i++)
                    {
                        Client c = this.mClientsValues[i];
                        if (!c.IsDisconnected && !c.ReadingInProgress)
                        {
                            sockets.Add(c.Socket);
                        }
                    }
                    if (sockets.Count == 0)
                    {
                        // noClients = true;
                        continue;
                    }
                }
                finally
                {
                    this.mClientsSynchronization.ReleaseReaderLock();
                }
                try
                {
                    //System.Net.Sockets.Socket.Select(sockets, null, null, RECEIVE_DATA_TIMEOUT);
                    SocketSelect.Select(sockets, null, null, RECEIVE_DATA_TIMEOUT);
                    foreach (System.Net.Sockets.Socket s in sockets)
                    {
                        Client c = this.mClients[s]; //mClients-dictionary<socket,client>
                        if (!c.SetReadingInProgress())
                        {
                            System.Threading.ThreadPool.QueueUserWorkItem(c.ReadData);
                        }
                    }
                }
                catch (Exception e)
                {
                    this.OnExceptionCaught(this, new ExceptionCaughtEventArgs(e, "Exception when reading data."));
                }
            }
        }

I am developing some additional stuff to the game server but I dont understand the code I recieved (however it works). I would be really happy if somebody could explain to me, why (in the recievedata method) there is socket list clearing and then again filling. I just cant see the point.

this.mReceiveDataThread = new System.Threading.Thread(this.ReceiveData);

           protected void ReceiveData()
        {
            List<System.Net.Sockets.Socket> sockets = new List<System.Net.Sockets.Socket>();
            bool noClients = false;
            while (true)
            {
                if (noClients)
                {
                    System.Threading.Thread.Sleep(RECEIVE_DATA_NO_CLIENT_WAIT_TIME);
                    noClients = false;
                }
                this.mClientsSynchronization.AcquireReaderLock(-1);
                try
                {
                    if (this.mClients == null || this.mClients.Count == 0)
                    {
                        noClients = true;
                        continue;
                    }
                    sockets.Clear(); 
                    for (int i = 0; i < this.mClientsValues.Count; i++)
                    {
                        Client c = this.mClientsValues[i];
                        if (!c.IsDisconnected && !c.ReadingInProgress)
                        {
                            sockets.Add(c.Socket);
                        }
                    }
                    if (sockets.Count == 0)
                    {
                        // noClients = true;
                        continue;
                    }
                }
                finally
                {
                    this.mClientsSynchronization.ReleaseReaderLock();
                }
                try
                {
                    //System.Net.Sockets.Socket.Select(sockets, null, null, RECEIVE_DATA_TIMEOUT);
                    SocketSelect.Select(sockets, null, null, RECEIVE_DATA_TIMEOUT);
                    foreach (System.Net.Sockets.Socket s in sockets)
                    {
                        Client c = this.mClients[s]; //mClients-dictionary<socket,client>
                        if (!c.SetReadingInProgress())
                        {
                            System.Threading.ThreadPool.QueueUserWorkItem(c.ReadData);
                        }
                    }
                }
                catch (Exception e)
                {
                    this.OnExceptionCaught(this, new ExceptionCaughtEventArgs(e, "Exception when reading data."));
                }
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无所谓啦 2024-08-14 09:59:51

循环中使用套接字列表从 mClient 中提取仍然连接的 Client 对象。套接字列表在每次循环迭代时都会被清除,以便可以检查 mClient 以排除任何断开连接的客户端或已在进行读取的客户端。

去掉线程锁定机制和SocketSelect调用循环,归结为下面的代码可能更容易理解。

mClients.Where(c => !c.IsDisconnected && !c.ReadingInProgress).ToList().ForEach(c=> {
    if (!c.SetReadingInProgress())  {
        ThreadPool.QueueUserWorkItem(c.ReadData);                        
    }
});

The sockets list is being used within the loop to extract the still connected Client objects from mClients. The sockets list gets cleared on each loop iteration so that mClients can be checked to exclude any disconnected clients or clients with a read already in progress.

Taking out the thread locking mechanism and SocketSelect call the loop boils down to the code below which may be easier to understand.

mClients.Where(c => !c.IsDisconnected && !c.ReadingInProgress).ToList().ForEach(c=> {
    if (!c.SetReadingInProgress())  {
        ThreadPool.QueueUserWorkItem(c.ReadData);                        
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文