简体   繁体   English

服务器 - C#,客户端 - 闪存,数据交换协议

[英]Server - C#, Client - Flash, data exchange protocol

We are writing online multiplayer game in Flash wish .Net server side, we need duplex channel so communication is done using sockets. 我们正在Flash愿望.Net服务器端编写在线多人游戏,我们需要双工通道,所以使用套接字进行通信。

Can you tell me some best practices about protocol/format of exchanging data between server and client? 你能告诉我一些关于在服务器和客户端之间交换数据的协议/格式的最佳实践吗? IMO one of good choise may be using JSON serialization. IMO的一个好选择可能是使用JSON序列化。

I think, JSON can be the best choice and let me explain why. 我认为,JSON可能是最好的选择,让我解释一下原因。

Firstly, JSON-serialized game traffic is ligth-weighted, because JSON notation is like "name : val", not like "<name> val </name>" . 首先,JSON序列化的游戏流量是加权加权的,因为JSON符号类似于“name:val”,而不像"<name> val </name>"

Secondly, JSON serialization is the most intuitive one for games. 其次,JSON序列化是游戏中最直观的序列化。 XML and XML-like notations are good for multiline data, but in-game variables' values are usually rather short (HP, mana, AP, damage, etc.) and can be discribed with one line. XML和类似XML的符号适用于多行数据,但游戏中变量的值通常很短(HP,法力,AP,伤害等),并且可以用一行描述。

Thirdly, Actionscript 3 (I hope, you won't write MMO in AS2) has got perfect tools for JSON decoding/encoding. 第三,Actionscript 3(我希望你不会在AS2中编写MMO)已经有了完美的JSON解码/编码工具。 XML-parsing tools are good too, but IMO, JSON's are better. XML解析工具也很好,但IMO,JSON更好。 Also, in Flash 11+, JSON operations are extremely optimized. 此外,在Flash 11+中,JSON操作得到了极大的优化。

Fourthly, many high-load projects use cache-servers with MongoDB, and it's main format is BSON (Binary JSON), so using JSON in this case will simplify data communication between DB and everything else. 第四,许多高负载项目使用带有MongoDB的缓存服务器,它的主要格式是BSON(二进制JSON),因此在这种情况下使用JSON将简化数据库与其他所有内容之间的数据通信。

The good solution is a proprietary protocol because it will provide perfect performance and traffic optimization. 好的解决方案是专有协议,因为它将提供完美的性能和流量优化。

We have tried the different protocols to search a best one for our online poker. 我们尝试了不同的协议来搜索我们在线扑克的最佳协议。

The result of research is the next protocol: 研究结果是下一个协议:

The first two bytes is a package type. 前两个字节是包类型。 The second two bytes of package is size of package. 包的后两个字节是包的大小。 The N bytes package body N字节包体

  1. Load the leading 4 bytes from the socket to get size and type of package. 从套接字加载前4个字节以获取包的大小和类型。
  2. Load package body 加载包体
  3. Send package body to process to the message type handler 将包体发送到消息类型处理程序

It depends. 这取决于。 Forget the involved technologies for a while and focus on your specific comm needs. 暂时忘掉所涉及的技术,并专注于您的特定通信需求。 Hope you don't mind if I explore a few related points as well. 希望你不介意我也探讨一些相关的观点。

  • Point-to-point (p2p) flow : We know this one - Duplex, as you mentioned. 点对点(p2p)流程 :正如您所提到的,我们知道这一点 - Duplex。
  • p2p mode : Half- or Full-duplex? p2p模式 :半双工或全双工? Will data be exchanged simultaneously, or it is always a message-response mechanism? 数据是否会同时交换,还是始终是消息响应机制?
  • State : You may have code that will wait for a response, even if the major part of your exchange is asynchronous. 状态 :即使交换的主要部分是异步的,您也可能拥有等待响应的代码。 Is the exchange absolutely async? 交换绝对是异步的吗? sync? 同步? mixed? 混合使用吗? This point is tightly related to the previous one. 这一点与前一个密切相关。
  • Transmission : Data bursts, or constant streams? 传输 :数据突发或恒定流?
  • Bandwidth : How many bytes/sec do you expect to traffic around? 带宽 :您期望流量多少字节/秒? Is it a considerable percent of your target network (wireless? mobile? hi-speed?) 它是您目标网络的一个相当大的百分比(无线?移动?高速?)
  • Reliable/unreliable content : Does the loss of a data package tosses your client state in disarray? 可靠/不可靠的内容 :数据包的丢失是否会使您的客户状态陷入混乱? (This isn't a bad thing per se, just a spec.) (这本身并不是一件坏事,只是一个规范。)

If your code needs any kind of synchronous content, then you may need a reply flow control on you code, and a Messaging structure of some sort where Messages have an ID and may have a Original Message ID to which they are a reply. 如果您的代码需要任何类型的同步内容,那么您可能需要对代码进行回复流控制,以及某种类型的消息传递结构,其中消息具有ID并且可能具有作为回复的原始消息ID。 This will help you control content even on async protocols. 这将有助于您甚至在异步协议上控制内容。 In this case, an easily maintanable protocol like JSON may be better, as @ JustLogin mentioned. 在这种情况下,正如@ JustLogin所提到的,像JSON这样易于管理的协议可能会更好。

Full-Duplex mean you be staging more data on your network/interpreter layer, and it probably will have to handle asynchronous communications. 全双工意味着您要在网络/解释器层上升级更多数据,它可能必须处理异步通信。

If performance is a concern, and depending on your specifics, then proposals like the one made by @ Viacheslav can be attractive. 如果表现是一个问题,并根据您的具体情况,那么像@ Viacheslav制作的提案可能很有吸引力。 A custom protocol can be heavily optimized, at the cost of maintanability and portability (several languages have their own JSON interpreter, while custom interpreters may need to be implemented or encapsuled, adding layers to the process and thus lowering performance, or requiring code conversion.) 自定义协议可以以可维护性和可移植性为代价进行大量优化(多种语言都有自己的JSON解释器,而自定义解释器可能需要实现或封装,为进程添加层,从而降低性能或需要代码转换。 )

If you couldn't care less if a packet or two are lost, then UDP may be a viable solution. 如果你不小心丢失一两个数据包,那么UDP可能是一个可行的解决方案。 Less control than TCP means a lighter protocol, but you're never sure that the data reached the other side unless you implement your own control. 比TCP更少的控制意味着更轻的协议,但除非您实现自己的控制,否则您永远不会确定数据到达另一侧。

TL;DR version: I would stick to standard low-level protocols like JSON, even if they add some overhead. TL; DR版本:我会坚持使用像JSON这样的标准低级协议,即使它们增加了一些开销。

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

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