查看: 2631|回复: 1
打印 上一主题 下一主题

[Unity 组件参考手册]桌面:网络参考指南之高级网络概念

[复制链接]
.    

3797

主题

11

听众

5万

积分

首席设计师

Rank: 8Rank: 8

纳金币
32328
精华
41

活跃会员 优秀版主 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2013-2-19 17:00:17 |只看该作者 |倒序浏览
The following information is a collection of concepts that are relevant to any kind of game network activity, and are not specific to Unity. It will be very helpful for you to understand these concepts before you begin using Unity networking.接下来你将看到的是一系列和所有类型的游戏中网络使用都会碰到,同时也不是特定于Unity中的知识。在你开始使用Unity网络前理解这些内容会对你有很大帮助。
What is Networking? 什么是网络The basic concept of networking is communication between multiple computers. To do this you need a client and a server. The server can either be a dedicated host machine for everyone, or a player&nbsp***nning the game acting as the server for other players. Once a server has been established and a client has connected to it, the two computers can exchange data necessary for a multiplayer network game.对网络的基本理解是多台计算机之间的通信。为了完成这件事你需要安排一个客户端和一个服务器。服务器或是专用于扮演主机角色,或运行游戏的某个用户游戏的同时也为其他游戏用户提供主机的服务。当一个服务器被发布并且一个客户端连接到这个服务器时,两台电脑间就能够为一个多人网络游戏交换必要的数据了。Creating a network game requires a lot of attention to some very specific details. Even in Unity, where most actions are easy to design and create, networking remains extremely complex. We chose to make networking in Unity very robust and flexible. This means that you as the game creator need to make a lot of choices which you may not otherwise have to think about. These choices will have a big effect on how you build your game, so it is best to make them as early as possible. This means learning about these concepts and planning them as much as possible, keeping long-term implications in mind.创建一个网络游戏需要对一些非常特别的细节有大量的关注。甚至在Unity中,许多功能都可以轻易的设计和创建,但网络仍然是非常复杂。在Unity中的网络是健壮齐备的,我们可以灵活的选择如何使用网络。这意味着你作为一个游戏创建人员需要做出大量的你不得不仔细思考的选择。这些选择将对你如何创建这个游戏带来巨大影响。因此最好尽可能早些开始这部分的工作,这些工作包括了学习,尽可能的设计和长时间的动脑思考。
Networking Approaches 网络使用方法There are two common and proven approaches to s***cturing a network game. These approaches are called Authoritative Server and Non-Authoritative Server. Both approaches rely on a server connecting clients and passing information between them. Incidentally, they offer more privacy for end users since clients never actually connect directly with each other or have their IP addresses revealed to other clients.有两种通用的被验证的用于创建网络游戏的方案。这些方案被称作授权服务器和非授权服务器。这两种方案依赖于连接着客户端的服务器和在两者之间的信息传送机制。顺便说一句,当客户端之间并不是直接相互连接或是他们的本地IP没有相互知晓的情况下,这些方案能提供更多的隐私保密。
Authoritative Server 授权服务器The authoritative server approach requires the server to perform all world simulation, application of game&nbsp***les, and processing of client player input. Each client sends their input (in the form of key presses or requested functions to perform) to the server and continuously receives the current state of the game from the server. The client never performs any game logic changes itself. Instead, it tells the server what it wants to do, and the server handles internal logic itself and informs the client exactly what has happened.授权服务器方案需要包含一个提供世界仿真计算的服务器,这个服务器包含了所有游戏的规则,并负责处理客户端用户的输入。每一个客户端发送他们的输入(键盘按键码,或者是函数调用)到服务器,并且持续的从服务器接收游戏当前的状态。客户端不能执行任何游戏逻辑的修改。相反,他会告诉服务器他想做什么。然后服务器会根据自身内部的逻辑修改状态,然后再回复客户端什么情况正在发生。Fundamentally, this is a layer of separation between what the player wants to do and what actually happens. This allows the server to listen to what everyone wants to do, apply the logic of game&nbsp***les (which vary from game to game) and tell each client exactly what to do. Think of this as "layer tells Server what they want to do --> Server determines what happens --> Server tells client players how to update".从根本上讲,只是一个在游戏玩家想发生的事情和实际发生的事情之间的一个隔层。这允许服务器侦听到每个人想做什么,执行游戏规则的逻辑(与游戏到游戏的有所不同)然后告诉每一个客户端当前正发生了的事情。我们可以把这个过程看作是"玩家告诉服务器他们想做什么--> 服务器处理什么发生--> 服务器告诉客户端更新世界所需的数据"An advantage of using this approach is that it makes cheating much harder for clients. For example, they do not have the capabilities to tell the server (and thus all clients) "I killed your character" because of the technical limitations of the network communication. They can only tell the server "I fired my weapon" and the server determines whether or not a kill was made.使用这种方案的优势是使得游戏难以出现客户端的欺诈行为。例如,客户端不能告诉服务器(或者其他客户端)"我搞定了你的角色"应为网络通信的技术限制。他们只能告诉服务器"我开火了"然后服务器决定是否或者完成了一次击杀。Another example of an authoritative server would be the following for a physics game: "layer client sends input to the server --> Server&nbsp***ns physics simulation and determines all collisions & effects of collisions --> Server sends updates of object positions to the player client". This means that while each player client appears to be&nbsp***nning a full physics simulation, they are not. They are only receiving the result of the physics simulation that was performed by the server.授权服务器的另一个例子是体现在接下来的一个物理游戏中:“用户客户端发送输入到服务器->服务器完成物理计算然后检测所有碰撞和碰撞的效果->服务器发送物体的更新到用户客户端”。这意味着每一个用户客户端看起来正执行着完整的物理计算,但实际上他们没有,他们只是接收由服务器计算后的物理计算的结果With an authoritative server local movement has no effect until the server has reacted to it, which can look unnatural to the player. So if you press the go forward button, nothing will happen for possibly 100 ms if there is a 50 ms trip time in each direction. To combat this a technique called Client Side Prediction is used. This guide will not explain Client Side Prediction in depth, as it is a very technical and specialized technique. Essentially, it is a method of allowing each client to operate in the way that it believes it should, and receive corrections from the Server only if it calculates something incorrectly. The server can then override the actions of a client when it predicts something incorrectly. For more information on Client Side Prediction, simply Google the term.使用一个授权服务器,直到服务器做出反应,本地的移动才会真正生效,在玩家看起来这可能不可思议。因此如果你键入一个向前的按钮,可能100ms内不会发生任何事情,因为单边的数据传输可能超过了50ms.用来解决这个问题的技术被称作客户端预告。本指南不会深入解释客户端预告本身,这是一门特别而且非常技术性的工作。简单来说,这是一种允许客户端以它假定自己可以完成操作的方式行为。如果它做出了错误的预测,可以从服务器端得到纠正。服务器能够更正客户端的行为当它错误的预测某些事情的时候。关于客户端预测技术的更多信息,请在Google上搜索.Making the server authoritative requires a greater processing load on the server. Imagine the server receiving a variety of different processes and commands from 32 different players in an action game. He needs to process each user input and decide what is legal and not legal sometimes resolving conflicts between clients.授权服务器还必须在服务器具备更强大的处理能力。请设想在一个动作游戏中通常服务器会同时超过32个不同用户的命令和各种各样不同的处理。服务器需要处理每个用户的输入和有时还要在客户端之间处理冲突,然后决定什么是合理的,什么是不合理的。
Non-Authoritative Server 非授权服务器A Non-Authoritative Server does not control the outcome of every user input. The clients themselves process user input and game logic locally, then send the result of any determined actions to the server. The server then synchronizes all actions with the world state. This is easier to implement from a design perspective, as the server really just relays messages between the clients and does no extra processing beyond what the clients do.非授权服务器并不控制每个用户的输入的输出。客户端自己处理用户输入和本地的游戏逻辑,然后发送任何确定的行为的结果给服务器,服务器然后同步这些操作到世界状态中。服务器只是在客户端中转发消息,并不对客户端做些什么做更多处理,因此更容易从设计视图中实现。There is no need for any kind of prediction methods as the clients handle all physics and events themselves and relay what happened to the server. They are the owners of their objects and are the only agents permitted to send local modifications of those objects over the network.不需要任何类型的客户端预言方法,因为任何物理操作或者事件本身和包括决定将什么内容汇报到服务器均由客户端完成。他们是对象的主宰,只是通过网络发送本地所做出的物体改变的一种被许可的代理机制
Methods of Network Communication 网络通信的方法Now that we've covered the basic architectures of networked games, we will explore the lower-levels of how clients and servers can talk to each other.现在我们来了解网络游戏的基础架构,我们会浏览在底层客户端和服务器是如何相互对话。There are two relevant methods: Remote Procedure Calls and State Synchronization. It is not uncommon to use both methods at different points in any particular game.有两种重要的方式:远程函数调用和状态同步。在少许特定的游戏中在不同地方同时使用两种方法的情况并不常见。
Remote Procedure Calls 远程过程调用Also called RPCs, Remote Procedure Calls are used to invoke functions on other machines across the network. This can include the player's local game instance as well. Clients can send RPCs to the server, and the server can send RPCs to one or more clients. Most commonly, they are used for actions that happen infrequently. For example, if a client flips a switch to open a door, it can send an RPC to the server telling it that the door has been opened. The server can then send another RPC to all clients, invoking their local functions to open that same door. They are used for managing and&nbsp***cuting individual events.正如其名,远程函数调用被用于在跨越网络的不同机器上执行函数。这包含用户的本地游戏实例。客户端能发送RPCs到服务器,服务器也能发送RPCs到一个或同时多个客户端。通常情况下,他们很少被应用于产生行为。例如,如果一个客户端敲击一个开关来打开一扇门,它可以发送一个RPC到服务器告诉它们被打开了。服务器能发送另一个 RPC到所有其他客户端执行他们开启同一扇门得本地函数。RPC被用于管理和执行单个的事件
State Synchronization 状态同步State Synchronization is used to share data that is constantly changing. The best example of this would be a player's position in an action game. The player is always moving,&nbsp***nning around, jumping, etc. All the other players on the network, even the ones that are not controlling this player locally, need to know where he is and what he is doing. By constantly relaying data about this player's position, the game can accurately represent that position to the other players.状态同步被用于共享不断改变的数据。最好的例子是一个动作游戏中的玩家位置。玩家总是移动,跑动,跳跃等等。所有其他网络上的玩家,甚至没有本地控制这个角色的玩家,都需要知道该玩家在哪里,该玩家在做什么。不断的分发关于这个玩家的位置数据,游戏能够精确的再现出该玩家的位置给其他玩家This kind of data is regularly and frequently sent across the network. Because this data is time-sensitive, and it requires time to travel across the internet tubes from one machine to another, it is important to reduce the amount of data that is sent as much as possible. In simpler terms, state synchronization naturally requires a lot of bandwidth, so you should do everything you can to use the least amount of bandwidth that you can.这种类型的数据定期和顺序的通过网络发送,因为这种数据是具有时间敏感性的,并且他甚至需要时间来跨越互联网从一台机器传递到另一台机器,尽可能减少发送数据的数量变得非常重要。换个更简单的说法,状态同步通常需要大量的带宽,因此你应该不惜余力的减少必要工作所需要的带宽数量。
Connecting servers and clients together 把服务器和客户端连接起来Connecting servers and clients together can be complex if all factors are considered. Machines can have private or public IP addresses and they can have local or external firewalls blocking access. Unity networking tries to provide methods for handling all situations but there is no "magic bullet" for this.如果把所有的因素都考虑进去,比如,发现机器是否具备开放的IP地址,了解他们是否在外在或者内在的防火墙在阻止通信,等等问题。那么连接服务器和客户端在一起这个问题会变得很复杂。Unity网络模块尽可能为你提供解决所有问题的方案。但请记住,网络没有"银弹"。Private addresses are IP addresses which are not accessible directly from the internet, they are also called NAT addresses because of the method used to connect them to the internet (Network Address Translation). To explain this in greatly simplified terms, the private address goes through a local router which translates the address to a public address. This way a great number of private addresses can use a single public IP address to communicate with the internet. This is fine until someone from across the internet wants to initiate contact with a private address. He needs to go through the public address of the router which handles the private address and there is no way to do that except with a concept called NAT punchthrough. With NAT punchthrough a common server, called a facilitator, mediates communication in a way which allows a public address to contact a private address. This works by having the private address first contact the facilitator, which "punches" a hole through the local router. The facilitator can now see the public IP address and port which the private address is using. Using this information any machine on the internet can now connect directly with the previously unreachable private address. Bear in mind that this is a simplified explanation just to cover the basics.私有地址是一种不能被互联网直接访问的IP地址,他们也被称作NAT(转换)地址,因为这种方法只用来将他们连接到互联网,而非相反。用非常简单的话来说,私有地址是通过一个本地路由器转换到公共IP地址,这种方法帮助大量的私有地址同时使用一个公共IP地址来和互联网通信。除了当有人想从外部想访问内部而初始化一条到内部私有地址的通路前,这种方案工作的很好。从外到内访问内部私有地址需要跨越具有公共地址的并知道私有地址的路由器。除了一种被称作NAT punchthrough(穿透)的技术没有别的办法。通常允许穿透的服务器,被称作服务商,在某种程度上允许公共地址和某个私有地址间接的通信。首先,私有地址联系服务商,在本地路由器上"穿透"一个洞。服务器现在看见了私有地址正使用的公共地址和端口。使用这些信息,其他互联网的机器现在可以直接连接到穿透前不能连接的私有地址。不过请注意,这些描述只是非常笼统和抽象的简单解释Public addresses are more simple. Here the primary danger is that connectivity is blocked by an internal or external firewall. An internal firewall is a firewall&nbsp***nning locally on the machine. In this case the user can be asked to open a port to make the game server accessible. An external firewall is understood as a firewall&nbsp***nning on the network router or something similar which is outside the control of the users. In this case there might be nothing to do about connectivity except attempt to use the NAT punchthrough method to punch a hole through the firewall. During testing this did work but there is no research available to indicate how successful this is.公共地址就更加简单了,但第一个你可能碰到的风险是连接可能被一个内部或者外部的防火墙所隔断。内部防火墙运行于本机。这种情况下可以通知用户开启一个游戏服务器能访问的端口。而外部防火墙有可能是运行于本地网络的路由器或者根本就超出用户控制范围。这种情况下除了使用NAT穿透技术在防火墙上打洞这个方法以外没有别的办法可以建立连接。而且在实际测试前没有办法知道这种方法是否可行。The above mentioned connectivity issues affect servers and clients differently. Clients are simpler as they just need to contact a server, and such contact involves only outgoing traffic. If the client has a public address this almost always works, outgoing traffic is seldom blocked except on corporate networks where access is very limited. If the client has a private address it can connect to all servers except servers with private addresses which cannot do NAT punchthrough. (More on this later.) Servers are more complicated as they need to be able to accept incoming connections from unknown sources. With a public address, the server needs to have the game port open to the internet, not blocked by a firewall. If a port is not open and accessible to the internet the server cannot accept any connections from clients and is thus unusable. If the server has a private address it must be able to do NAT punchthrough, or else no one can connect to it. Clients which cannot do NAT punchthrough will regardless not be able to connect to the server.上述内容分别描述了影响服务器和客户端连通性的问题。客户端更简单,他们只需要连接到服务器,对客户端的讨论只包含向外通信。如果客户端有总是连接的公共地址。向外通信很少被中断,除非所访问的网络流量非常有限。如果客户端是私有地址那么可以访问所有的服务器,除非服务器也是不支持NAT穿透的私有地址。服务器常常会接收很多来至未知地址的连接,所以实现上变得复杂。在公共地址环境下,服务器需要向互联网开放一个并未被防火墙阻止的端口。如果端口没有被开放到互联网,服务器将不能接收任何来至客户端的连接,因此而变得不可访问。如果服务器处在私有地址的环境下,那必须能做NAT穿透,否则而不管客户端能不能做NAT穿透,没有客户端能连接到服务器。Unity provides tools to test all these different connectivity situations. When everything is resolved and you want to start connecting servers and clients together, there are two methods: direct connections by which a client needs to know the DNS name or IP address of the server and connections with help from the Master Server. The Master Server allows servers to advertise their presence to clients. The clients in that case do not need to know about any servers beforehand.Unity提供了工具用于测试所有可能的连接方案。当每个困难都被解决然后你开始连接服务器和客户端时,有两个方法实现连接:直接连接(客户端需要知道服务器的DNS名称或者IP地址)或者通过连接主服务器来寻求帮助。主服务器允许服务器注册他们的地址,并公开给客户端。客户端不需要预先知道任何服务器的信息。
Minimizing Network Bandwidth 最小化网络带宽When working with State Synchronization across multiple clients, you don't necessarily need to synchronize every single detail in order to make objects appear synchronized. For example, when synchronizing a character avatar you only need to send its position and rotation between clients. Even though the character itself is much more complex and might contain a deep Transform hierarchy, data about the entire hierarchy does not need to be shared.当在多个客户端之间使用状态同步时,为了使对象看起来同步而使每一个单一细节同步是没有必要的,例如,同步一个阿凡达角色你只需要在客户端之间传递他的位置和角度。甚至于角色本身更加的复杂,可以限定在某个特定深度的层次。关于整个层次的数据则不需要共享。A lot of data in your game can be viewed as static, and clients neither have to transfer it initially nor synchronize it. Using infrequent or one-time RPC calls should be sufficient to make a lot of your functionality work. Take advantage of the data you know will exist in every installation of your game and keep the client working by itself as much as possible. For example, you know that assets like textures and meshes exist on all installations and they usually don't change, so they will never have to be synchronized. This is a simple example but it should get you thinking about what data is absolutely critical to share from one client to another. This is the only data that you should ever share.在你的游戏中许多数据可以看成是静止的,客户端没有必要在初始化的时候传递或者后期同步这些内容。很少的或者一次性的RPC调用足以完成这些功能。存在于游戏的每一次安装过程中的数据会给你带来好处并尽可能让客户端能尽可能的自行工作。例如,你知道像纹理或者网格等存在于所有安装中的资源,他们常常不会被改变,因此他们也没有必要同步。这是一个简单的例子但它能说明什么数据是必须在客户端之间共享的,这些数据也是你唯一需要共享的。This is not an easy thing to do, especially if you have never made a network game before. Consider this: you can use a single RPC call with a level name to make all clients load the entire specified level and add their own networked elements automatically. S***cturing your game to make each client as self-sufficient as possible will result in reduced bandwidth.没有简单的方法。除非你不做网络游戏,请仔细思考,你能使用一个带有关卡名的单一的RPC调用使所有客户端加载整个特定的关卡,然后自动添加他们自己的网络元素。结构化你的游戏来使得每一个客户端都能尽可能自给自足将会让你达到减少网络带宽使用的目标。
Multiplayer Game Performance 多人游戏性能The physical location and performance of the server itself can greatly affect the playability of a game&nbsp***nning on it. Clients which are located a continent away from the server may experience a great amount of lag. That's just the way it is and there is really nothing one can do about that. No matter how powerful the server hardware is, the distance data has to travel to and from the server will always be a factor in your game. It's best if the server and clients are located on the same continent.物理位置和服务器自身的性能巨大的影响一个游戏的可玩性。和服务器相隔大陆的客户端将感受到巨大的延迟。这真的让人无能为力。不管服务器硬件如何强大,往返于服务器和客户端的数据传递的距离总是会非常难以想象。最好的办法,是把服务器和客户端搬到相同的大陆。
Extra Resources 其他资源We've collected some additional helpful links for learning about networking concepts. We recommend that you read as many networking resources as possible.我们收集了一些额外的可以帮助你学习网络的链接。我们希望你能尽可能阅读更多的网络资源:    http://developer.valvesoftware.com/wiki/Source_Multiplayer_Networking
    http://developer.valvesoftware.com/wiki/Lag_Compensation
    http://developer.valvesoftware.com/wiki/Working_With_Prediction
    http://www.gamasutra.com/resource_guide/20020916/lambright_01.htm【来源:互联网】
更多精彩教程,尽在web3D纳金网http://www.narkii.com/college/
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

nts    

3

主题

1

听众

743

积分

初级设计师

Rank: 3Rank: 3

纳金币
7
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2013-10-20 13:07:51 |只看该作者
不管服务器硬件如何强大,往返于服务器和客户端的数据传递的距离总是会非常难以想象。最好的办法,是把服务器和客户端搬到相同的大陆。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-4-28 08:10 , Processed in 0.106567 second(s), 32 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部