标题: [Unity 组件参考手册]桌面:网络参考指南之主服务器“构建自己的Unity网络服务器” [打印本页] 作者: . 时间: 2013-2-19 17:06 标题: [Unity 组件参考手册]桌面:网络参考指南之主服务器“构建自己的Unity网络服务器” The source code for all the individual networking servers can be downloaded from the Unity website. This includes the connection tester, facilitator, master server and proxy server.可以从Unity 网站下载创建服务器所需的源代码,包含连接测试器、facilitator、主服务器代和理服务器。All source packages include the RakNet 3.732 networking library which handles the basic networking functions and provides plugins used by the networking servers.所有源码包包含了可以处理基本网络功能的RakNet 3.732库文件,也提供了可以被网络服务器所使用的插件。The packages include three different types of project files, ready for compilation:这个包,包含了三种不同类型的可编译的项目文件: An Xcode 3.0 project for Mac OS X
A Makefile for Linux and Mac OS X
A Visual Studio 2008 solutionThe Xcode and Visual Studio projects can just be opened, compiled and built. To build with the Makefile just ***n "make". It should work with a standard compilation setup on Linux and Mac OS X, if you have gcc then it should work. On Linux you might need to install the ncurses library.The Xcode 和 Visual Studio 项目可以被打开,编译并创建.带有Makefile的项目,需要运行"make"指令进行创建.这些都可以在Linux和Mac OS X上的标准编译平台上正常工作.如果采用gcc进行编译,Linux平台需要安装ncurses库。
S***cture 结构
The Master Server 主服务器The Master Server uses an internal database s***cture to keep track of host information.主服务器使用内部数据结构来跟踪主机的信息。Hosts send messages with the RUM_UPDATE_OR_ADD_ROW message identifier and all their host information embedded. This is processed in the OnReceive() function in the LightweightDatabaseServer.cpp file. This is where all message initially appear and therefore a good place to start if you want to trace how a message is processed. A table is created within the database s***cture for each game type which is set when you use MasterServer.RegisterHost function. All game types are grouped together in a table, if the table does not exist it is dynamically created in the CreateDefaultTable() function.主机发送带有RUM_UPDATE_OR_ADD_ROW标识符的消息并嵌入主机信息.这些消息在LightweightDatabaseServer.cpp 文件中的OnReceive()函数中被处理.所有的消息被处理的地方,也是开始跟踪消息如何被处理的好地方,当使用MasterServer.RegisterHost函数,将会为每一个游戏类型创建一个数据库表格。所有类型的游戏被组织在这个表格里,如果表格不存在,可以在调用CreateDefaultTable() 函数时动态生成。The host information data is modified by the master server. The IP and port of the game which is registering, as seen by the master server, is injected into the host data. This way we can for sure detect the correct external IP and port in cases where the host has a private address (NAT address). The IP and port in the host data sent by the game server is the private address and port and this is stored for later use. If the master server detects that a client is requesting the host data for a game server and the server has the same IP address then he uses the private address of the server instead of the external one. This is to handle cases where the client and server are on the same local network, using the same router with NAT addresses. Thus they will have the same external address and cannot connect to each other through it, they need to use the private addresses and those will work in this case.主服务器可以修改主机的信息数据.被主服务器所看到的已注册的游戏的IP和端口被灌入到主机数据里.这样我们可以确保,当主机使用私有地址(NAT 地址),也可以检测其正确的外部IP和端口.被主服务器所发送的IP和端口将会被存储以备后用. 如果主服务器检测到客户端,正在请求一个游戏服务器的主机数据,而这个服务器同Master Server具有相同的IP地址,这时就会转而使用服务器的私有地址,而不是外部的那个。这是处理客户端和服务器在同一个本地网络的,使用相同的带NAT地址的路由器情况。他们具有相同的外部地址,因此,无法通过它来连接对方,在这种情况下他们需要使用私有地址。Clients send messages with the ID_DATABASE_QUERY_REQUEST message identifier and what game type they are looking for. The table or host list is fetched from the database s***cture and sent to the client. If it isn't found and empty host list is sent.客户端发送带有ID_DATABASE_QUERY_REQUEST标识符的消息来请求查找的游戏类型。从数据库结构中取出表格或者主机列表并发送到客户端,如果没有找到,将发送空的主机表格。All messages sent to the master server must contain version information which is checked in the CheckVersion() function. At the moment each version of Unity will set a new master server version internally and this is checked here. So if the master server communication routine will change at any point it will be able to detect older versions here and possibly refer to another version of the master server (if at all needed) or modify the processing of that message to account for differences.所有发送到主机的消息包必须包含版本信息,并在CheckVersion()函数中被检查。每一个Unity版本都会设置一个新的内部主服务器版本并在这里被检查。因此。因此,如果主服务器通信程序在任何时候被改变,它将能够检测到旧版本同时可能会参考主服务器的其他版本(如果有必要的话)或者修改消息的处理过程去应对差异。
The FacilitatorThe facilitator uses the NAT punchthrough plugin from RakNet directly with no modifications. This is essentially just a peer listening on a port with the NAT punchthrough plugin loaded. When a server and a client with NAT addresses are both connected to this peer, they will be able to perform NAT punchthrough to connect to each other. When the Network.InitializeServer uses NAT, the connection is set up automatically for you.Facilitator 直接使用了 RakNet的NAT 网络穿透插件.本质上就是加载NAT 网络穿透插件来侦听一个端口。当一个带有NAT地址的服务器和客户端都在连接这个端口时,他们可以执行网络穿透去连接到对方.当调用Network.InitializeServer 使用 NAT,连接会被自动设置。
【来源:互联网】
更多精彩教程,尽在web3D纳金网http://www.narkii.com/college/