简体   繁体   English

阅读来自Lidgren的某个客户的消息

[英]Read message from a certain client in Lidgren

I'm setting up a game with Lidgren, and I was wondering if there was a way to read a message from a certain client instead of just from the server as a whole, such as- 我正在与Lidgren一起玩游戏,我想知道是否有一种方法可以从某个客户端(而不是整个服务器)读取消息,例如-

//Why doesn't this exist?
NetIncomingMessage message = server.Connections[0].ReadMessage();

This way I would be able to split up reading data from each client into it's own thread and have a separate thread for sending data to each client. 这样,我将能够从每个客户端读取数据分成自己的线程,并有一个单独的线程向每个客户端发送数据。 Currently there is only one loop in my server, which reads packets and I fear is favoring one client more than others, as some movements made by players take a while to be received by other players. 当前,我的服务器中只有一个循环读取数据包,我担心一个客户端比另一个客户端更受青睐,因为玩家的某些动作需要一段时间才能被其他玩家接收。 I believe reading messages separately from each client would solve this issue. 我相信与每个客户分开阅读邮件可以解决此问题。 With simple TcpClients, you can read from each client's stream instead of the server as a whole, and I like this functionality but also rely on the simplicity of sending packets through Lidgren and was wondering if there was a similar functionality with Lidgren. 使用简单的TcpClients,您可以从每个客户端的流中读取而不是从整个服务器中读取,我喜欢此功能,但也依赖于通过Lidgren发送数据包的简单性,并且想知道Lidgren是否具有类似的功能。

You can read messages on one thread, then stuff them into a concurrent safe collection in the, for example, connection.Tag message. 您可以在一个线程上读取消息,然后将其填充到并发安全集合中,例如,connection.Tag消息。 Each individual thread would then dequeue messages for that connection. 然后,每个单独的线程将使该连接的消息出队。 However; 然而; it sounds like a really bad solution to a problem that I assume is a bug in the existing code... there's no reason reading messages on a single thread would favor any client over another; 对于我认为是现有代码中的错误的问题,这听起来像是一个非常糟糕的解决方案。没有理由在单个线程上读取消息会使任何客户端胜过另一个客户端。 messages are enqueued and dequeued in the exact order they arrive. 消息按照它们到达的确切顺序入队和出队。 Doing one thread per client will create a whole bunch of synchronization issues you must solve when acting on the content of your received messages; 对每个客户端执行一个线程将产生一堆同步问题,您必须根据接收到的消息的内容采取行动; I would advice against it unless you have a very compelling reason. 除非您有非常令人信服的理由,否则我将建议您这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM