Tom*_*Tom 3 network-programming tcp client-server real-time network-protocols
我正在编写套接字服务器和Flash游戏客户端.游戏需要实时命令,如移动和转动.这些命令必须尽快由服务器发送到客户端,因为否则其他客户端将与移动/转向客户端进行大量的同步.
这是Nagle算法引起的问题的一个例子:
注意:如果您想了解这些命令的含义,请参阅下面的命令表.
第一个是我移动的船(向前移动+向右移动,向前移动但是没有移动)
客户端发送命令:
84796: Sending data: 2#4
84796: Sending data: 2#2
84904: Sending data: 2#3
84904: Sending data: 2#0
86187: Sending data: 2#4
86188: Sending data: 2#2
86374: Sending data: 2#3
86404: Sending data: 2#0
Run Code Online (Sandbox Code Playgroud)
客户端接收命令:
79244: Raw receive: 3#3#4$
79244: New command: 3#3#4
79398: Raw receive: 3#3#2$3#3#3$3#3#0$
79399: New command: 3#3#2
79399: New command: 3#3#3
79399: New command: 3#3#0
80635: Raw receive: 3#3#4$
80635: New command: 3#3#4
80908: Raw receive: 3#3#2$3#3#3$3#3#0$
80908: New command: 3#3#2
80908: New command: 3#3#3
80908: New command: 3#3#0
Run Code Online (Sandbox Code Playgroud)
"时刻"是一个奇怪的术语,并不意味着我想说的话,但这里似乎是前一个命令之后的毫秒时间
由客户端A(时刻:0)向前发送,由客户端B接收(时刻:0)
右转由客户A发送(时刻:0),由客户B收到(时刻:155)
停止移动客户端A发送(时刻:108),由客户端B接收(时刻:0)
停止由客户端A发送发送(时刻:0),由客户端B接收(时刻:0)
由客户B发送(时刻:1283),由客户B收到(时刻:1236)
右转由客户A发送(时刻:1),由客户B收到(时刻:273)
停止客户A发送的动作(时刻:186),由客户B接收(时刻:0)
停止转动客户A发送(时刻:30),客户B收到(时刻:0)
这是与命令对应的命令表:
客户端 - >服务器
2# (movement info)
0) now not turning
1) now turning left
2) now turning right
3) now not moving
4) now moving forward
Run Code Online (Sandbox Code Playgroud)
服务器 - >客户端
3# (movement info)
[shipId]#
0) now not turning
1) now turning left
2) now turning right
3) now not moving
4) now moving forward
Run Code Online (Sandbox Code Playgroud)
所以,你可以看到,由于"Nagle",这些命令完全被废弃了.这导致停止移动命令被其他客户端与开始移动命令同时接收,导致该玩家根本不移动.
这就是为什么我需要TCP服务器尽可能快地实时发送这些命令的原因.一个简单的解决方法是简单地禁用Nagle.但是,我用google搜索(注意他关于tcp消息部分的建议是在我的系统中实现但与时间无关),并注意到人们绝对不建议禁用Nagle.
我不应该为此原因禁用Nagle算法,而应该寻找其他解决方案吗?为什么不)?
提前致谢. - 汤姆
您希望使用TCP_NODELAY关闭该套接字的nagle算法.在现代操作系统中有一些设置可以在系统范围内禁用它,并且这是不受欢迎的,但是没有理由不应该使用你知道需要低延迟的套接字.
归档时间: |
|
查看次数: |
914 次 |
最近记录: |