简体   繁体   English

游戏服务器TCP网络套接字-公平性

[英]Game server TCP networking sockets - fairness

I'm writing a game server for a turn-based game. 我正在为回合制游戏编写游戏服务器。 One criteria is that the game needs to be as fair for all players as possible. 一个标准是游戏必须对所有玩家尽可能公平。

So far it works like this: 到目前为止,它的工作方式如下:

  • Each client has a TCP connection. 每个客户端都有一个TCP连接。 (If relevant, the connection is opened via WebSockets) (如果相关,则通过WebSockets打开连接)
  • While running, continually check for incoming socket messages via epoll. 在运行时,通过epoll不断检查传入的套接字消息。
  • Iterate through clients with sockets ready to read: 使用准备好读取的套接字遍历客户端:
    • Read all messages from the client. 阅读来自客户端的所有消息。
    • Update the internal game state for each message. 更新每个消息的内部游戏状态。
    • Queue outgoing messages to affected clients. 将传出邮件排队到受影响的客户端。
  • At the end of each "window" (turn): 在每个“窗口”(转弯)的末尾:
    • Iterate through clients and write all queued outgoing messages to their sockets 遍历客户端并将所有排队的传出消息写入其套接字

My concern for fairness raises the following questions: 我对公平的关注提出了以下问题:

Does it matter in which order I send messages to the clients? 我以什么顺序向客户发送消息有关系吗?

  • Calling write() on all the sockets takes only a fraction of a second for my program, but somewhere in the underlying OS or networking would it make a difference if I sorted the client list? 对于我的程序,在所有套接字上调用write()只需一秒钟的时间,但是如果对客户端列表进行排序,在底层操作系统或网络中的某个地方会有所不同吗?
    • Perhaps I should be sending to the highest-latency clients first? 也许我应该先发送给延迟最高的客户?

Does it matter how I write the outgoing messages to the sockets? 我如何将传出消息写入套接字有关系吗?

  • Currently I'm writing them as one large chunk. 目前,我将它们写成一大块。 The size can exceed a single packet. 大小可以超过单个数据包。
    • Would it be faster for the client to begin its processing if I sent messages in smaller chunks than 1 packet? 如果我以小于1个数据包的小块发送消息,客户端开始处理的速度会更快吗?
    • Would it be better to write 1 packet worth to each client at a time, and iterate over the clients multiple times? 一次向每个客户端写入一个价值为1的数据包并多次遍历客户端会更好吗?

Are there any linux/networking configurations that would bear impact here? 是否有任何Linux /网络配置会在这里产生影响?

Thanks in advance for your feedback and tips. 在此先感谢您的反馈和提示。

Does it matter in which order I send messages to the clients? 我以什么顺序向客户发送消息有关系吗?

Yes, by fractions of milliseconds. 是的,以毫秒为单位。 If the network interface is available for sending the OS will immediately start sending. 如果网络接口可用于发送,则操作系统将立即开始发送。 Why would it wait? 为什么要等待?

Perhaps I should be sending to the highest-latency clients first? 也许我应该先发送给延迟最高的客户?

I think you should be sending in random order. 我认为您应该以随机顺序发送。 Shuffle the list prior to sending. 发送前先随机播放列表。 This makes it fair. 这很公平。 I think your question is valid and this should be addressed. 我认为您的问题是有效的,应该解决。

Currently I'm writing them as one large chunk. 目前,我将它们写成一大块。 [...] [...]

First, realize that TCP is stream-based and that there are no packets/messages at the protocol level. 首先,认识到TCP是基于流的,并且在协议级别上没有包/消息。 On a physical level data is indeed packetized. 在物理级别上,数据确实是打包的。

It is not necessary to manually split off packets because clients will read data as it arrives anyway. 无需手动拆分数据包,因为无论如何,客户端都会读取数据。 If a client issues a read, that read will complete immediately once the first packet has arrived. 如果客户端发出读取,则第一个数据包到达后,该读取将立即完成。 There is no artificial waiting in the OS. 操作系统中没有人为的等待。

Are there any linux/networking configurations that would bear impact here? 是否有任何Linux /网络配置会在这里产生影响?

I don't know. 我不知道。 Be sure to disable nagling. 确保禁用打na。

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

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