茫茫網海中的冷日 - 對這文章發表回應
茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1675076 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_60D_00084.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

對這文章發表回應

發表限制: 非會員 可以發表

發表者: 冷日 發表時間: 2014/11/26 3:39:10

主服務器 Master Server

Date:2011-11-06 09:06

The Master Server is a meeting place for games that are actively seeking clients, and player clients who want to connect to them. Its purpose is also to hide IP address and port details and perform technical tasks around setting up network connections which might otherwise not be possible, like firewall handling and NAT punch-through.

Master Server 就像一個遊戲大廳,會主動掃瞄想要連接進來的客戶端。其目的是為了使隱藏的 IP 地址和端口也能夠建立網絡連接,像防火牆和網絡穿透。


Each individual running game server provide a Game Type to the Master Server. All games with a matching Game Type are gathered together so that compatible clients can easily view them. When a player connects and queries the Master Server for their matching Game Type, useful information on the server is displayed for the player to see. This helps the player decide which server to connect to. This includes the Game Name, player count and whether or not a password is needed. The two functions used to transmit this data are MasterServer.RegisterHost() for the Server, and MasterServer.RequestHostList() for the player client.


每個運行的遊戲服務器都需要向 Master Server 提供一個遊戲類型.具有相同類型的遊戲被集中在一起以至於客戶端可以很容易查看到,當一個玩家連接 Master Server 尋找遊戲時。Master Server 可以提供該遊戲的詳細訊息,幫助玩家決定連接哪個服務器。 這些訊息包括遊戲名,玩家數量,是否需要密碼。服器通過調用 MasterServer.RegisterHost()傳遞相應訊息,客戶端通過調用 MasterServer.RequestHostList()去獲取相應訊息。

When calling RegisterHost(), you need to pass three arguments - gameTypeName (which is the previously mentioned Game Type), gameName and comment - for the host being registered. RequestHostList() takes as an argument the gameTypeName
of the hosts you are interested in connecting to. All the registered hosts of that type will then be sent to the requesting client. This is an asynchronous operation and the actual host list can be retrieved with PollHostList() which returns the list when it has arrived.

當調用 RegisterHost() 進行註冊時,需要傳遞三個參數- 遊戲類型名(就是上文提到的遊戲類型),遊戲名和註釋。 RequestHostList() 函數將需要連接的遊戲類型名作為參數,所有已註冊主機的訊息將被發送到客戶端。 這是一個非同步操作,可以通過調用 PollHostList() 獲取所有的主機列表。

The NAT punchthrough portion of the Master Server is actually done by a separate entity called the Facilitator. The central Unity Master Server is also running a Facilitator in addition to being a game lobby so it is OK to think of these functionalities as simply Master Server functionalities in that context.

網絡穿透(NAT)部分的功能,是被另外一個從 Master Server 分割出來的叫 Facilitator 的實體所完成的。 Unity 中心服務器上也運行了一個 Facilitator,可以把 Facilitator 看做是 Master Server 的子功能。


The Game Type you set for your game should be unique for your game. This is to prevent clients running a completely different game from seeing your servers and trying to connect to them. So don't use "Game1" or anything generic like that as the gameTypeName. Be specific and unique. If you have different versions of your game out, you could use the comment field to relay that information. That way the client will know if their version is incompatible with some servers in the host list. The comment field can also be used to relay any information you desire. It is a binary data field and can store objects, you just need to cast them properly yourself.

為遊戲所設置的遊戲類型必須是獨一無二的。防止客戶端看到和實際連接上的遊戲不一樣的情況發生。 因此,不要使用"Game1"或者其他像 gameTypeName 這樣普遍的名字,名字一定是要特定的獨特的,如果遊戲有不同的版本,可以用註釋來表明這個信息,這樣的話客戶端可以知道他們的版本是否和主機相匹配。 這是一個二進制的可存儲對象,你需要自己去正確轉換它們。


More advanced functionality can be done by using the comment field and customizing the Master Server so that it will use that information itself. For example you could reserve the first 10 bytes of the comment field for a password and then extract the password in the Master Server when it receives the host update. It can compare and reject the host update if appropriate (password check fails). Of course this would require editing of the Master Server source code itself, but this can be downloaded and modified. For more information about this, skip down the page to the Advanced Section.

可以使用註釋域定制並實現更多高級功能,例如,你可以利用註釋域的前10字節來存儲密碼,Master Server 可以從主機獲取更新並提取密碼,可以進行比較並拒絕主機的更新(密碼檢查錯誤)。 當然,你需要下載並修改 Master Server 的源代碼,有關這方面更多的信息,請跳過該頁並前往 高級部分

Registering a game 註冊遊戲


Before registering a game it is important to set the NAT functionality properly by setting useNat parameter for Network.InitializeServer to true or false depending on the networking capabilities of the host. The user hosting the game should know if he has a NAT address or not and if the game port is connectable on the public internet, so before hosting the game he will indicate if NAT assistance is needed or not.

A server might be started with code similar to this:

根據主機的網絡情況,在註冊遊戲前,最重要的是通過設置 Network.InitializeServer 的參數 useNat 為 true 或者 false 來正確設置 NAT(網絡地址轉換)功能。 在連接遊戲之前,用戶主機應該知道他是否是一個 NAT 網絡地址或者可連接的遊戲端口是否在公網上,他需要知道是否需要 NAT 的協助。

服務器端的啟動代碼看起來像這樣:


function OnGUI() {
if (GUILayout.Button ("Start Server"))
{
// Use NAT punchthrough if no public IP present
Network.InitializeServer(32, 25002, !Network.HavePublicAddress());
MasterServer.RegisterHost("MyUniqueGameType", "JohnDoes game", "l33t game for all");
}
}

Here we just decide if NAT punchthrough is needed based on if the machine has a public address or not. There is a more involved function available called
Network.TestConnection() which can tell you if the host machine can do NAT or not. It also does connectivity testing for public IP addresses to see if a firewall is blocking the game port. Machines which have public IP addresses always pass the NAT test but if the test fails then the host will NOT be connectable to NAT clients. In such a case the user needs to be informed that in order to host a game he needs to set up port forwarding if possible. Usually people with cheap broadband connections will have a NAT address and not be able to set up port forwarding (as they don't have a personal public IP address at all). In these cases, if the NAT test fails, the user should be informed that running a server is inadvisable as no one but clients on the same local network will be able to connect.


這裡我們根據機器是否具有一個公共地址來決定是否需要使用 NAT 穿透,有個函數 Network.TestConnection(),可以告訴我們主機是否可以 NAT。 它可以連接公網 IP,測試是否有防火牆阻止了遊戲端口。 有公網 IP 的主機通常可以通過 NAT 測試,但是如果測試失敗主機將不能連接 NAT 客戶端。 在這種情況下,用戶需要知道,他可能需要建立端口轉發。 通常人們會有一個 NAT 地址,不能設置端口轉發(因為沒有一個專有的公網 IP)。 這種情況下,如果 NAT 測試失敗,用戶會被告知,運行服務器是不行的,但在同一個局域網的客戶端是可以連接的。

If a host enables NAT functionality without needing it, no harm is done and it will still be connectable, however, client which cannot do NAT punchthrough will think they cannot connect to him as he has NAT enabled.

如果一個主機開啟了 NAT 功能但卻不能用,這樣也沒什麼危害,它仍然可以連接。 但是,不能實現 NAT 穿透的客戶端將不能連接到主機,如果它開啟 NAT 的話。

Connecting to a game 連接到遊戲

The host information, HostData, object sent during host registrations or queries contains the following information:

主機信息,主機數據,對像在登記或查詢時包括如下信息:




booleanuseNat Indicates if the host uses NAT punchthrough. // 表示主機是否使用NAT穿透
String gameType The game type of the host. // 主機類型
String gameName The game name of the host. // 主機名稱
int connectedPlayers The amount of currently connected players/clients. // 當前連接的用戶/客戶端數量
int playerLimit The maximum amount of allowed concurrent players/clients. // 最大連接數量
String[]IP The internal IP address of the host. On a server with a public address the external and internal addresses are the same. This is an array as when connecting internally, all the IP addresses associated with all the active interfaces of the machine need to be checked.
主機的內部IP地址。在具有公網IP地址的服務器的外部和內部地址是相同的。內部連接時他是一個數組,與IP地址相關的所有機器的活動接口都需要被查閱。
int port The port of the host. // 主機的端口
boolean passwordProtected Indicates if you need to supply a password to be able to connect to this host.
提示是否需要提供密碼才能鏈接到這台主機。
String comment Any comment which was set during host registration.
主機註冊時的註釋
String guid The network GUID of the host. This is needed when connecting using NAT punchthrough.
主機的GUID。當進行NAT穿透時需要使用上。

This information can be used by clients to see the connection capabilities of the hosts. When NAT is enabled you need to use the GUID of the host when connecting. This is automatically handled for you when you connect to the HostData struct directly. The connect routine might look something like this:

這些信息可以被客戶端用來查看網絡連接能力,當使用 NAT 連接時,需要使用到主機的 GUID。 當你直接連接到主機數據時,這些信息都會被自動使用,這裡有個大致的連接的範例:


function Awake() {
MasterServer.RequestHostList("MadBubbleSmashGame");
}
function OnGUI() {
var data : HostData[] = MasterServer.PollHostList();
// Go through all the hosts in the host list
for (var element in data)
{
GUILayout.BeginHorizontal();
var name = element.gameName + " " + element.connectedPlayers + " / " + element.playerLimit;
GUILayout.Label(name);
GUILayout.Space(5);
var hostInfo;
hostInfo = "[";
for (var host in element.ip)
hostInfo = hostInfo + host + ":" + element.port + " ";
hostInfo = hostInfo + "]";
GUILayout.Label(hostInfo);
GUILayout.Space(5);
GUILayout.Label(element.comment);
GUILayout.Space(5);
GUILayout.FlexibleSpace();
if (GUILayout.Button("Connect"))
{
// Connect to HostData struct, internally the correct method is used (GUID when using NAT).
Network.Connect(element);
}
GUILayout.EndHorizontal();
}
}

This example code prints out all the relevant information of the hosts returned by the Master Server. Other useful data like ping information or geographic location of hosts can be added to this.

這個例子列印出所有主服務器返回主機的相關訊息。 其他的有用的數據,像 ping 訊息或者主機的地理位置可以被加載到這裡。

NAT punchthrough (NAT 穿透)

Bear in mind that NAT punchthrough will not work 100% of the time so certain machines are not suited to run as servers as some clients can connect to them and certain client might have trouble connecting to any NAT server.

要知道 NAT 穿透並不是 100% 都能工作,所以某些機器並不適合做服務器或客戶端。


By default NAT punch through is done with the help of the Master Server but this functionality does not need to rely on the Master Server. The Facilitator is the one who is actually used for the NAT punchthrough routine. If two machines are connected to the Facilitator, one can directly connect to the other if as long as it uses the external IP and port. The Master Server is used to provide this external IP and port information which is otherwise hard to determine. That is why the Master Server and Facilitator are so tightly integrated. The Master Server and Facilitator have the same IP address by default, to change either one use the MasterServer.ipAddress, MasterServer.port,
Network.natFacilitatorIP and Network.natFacilitatorPort

默認情況下 NAT 穿透是通過主服務器的幫助來完成的,但這個功能並不需要依靠 Master Server。 Facilitator 是一個真正用於NAT穿透的程式。只要使用外部 IP 和端口就可以直接連接。 主服務器是用來提供這種外部難以定位的 IP 和端口訊息的。 這就是為什麼主服務器和調解器要如此緊密的結合起來。 主服和調解器有相同的默認 IP,改變它用 MasterServer.ipAddress, MasterServer.port, Network.natFacilitatorIP
Network.natFacilitatorPort

Advanced 高級

The Master Server is a completely separate entity from the Unity editor itself. It can be deployed on Windows, Linux and Mac OS, and anyone can have their own Master Server. Unity Technologies has a dedicated Master Server available for anyone to use which is the default server used.

Master Server 是一個完全獨立的實體。它可以部署到 windows、Linux、macOS。 unity 技術提供一個專門的主服給所有人作為默認的服務器使用。


There might be cases where you want to modify how the Master Server handles information and how it communicates. Maybe you need to do some optimizations of how the Master Server handles host data information from the individual game servers. Maybe you need to limit how many hosts the clients will receive when they request the host list, either numerically or through some kind of filter like location. This would mostly be bandwidth optimizations but anything can be done as long as it communicates properly with the Unity player.

有些情況下,你可能想知道如何修改主服的處理信息,如何進行通信。 或許你需要去做一些優化,如何從主服向個別遊戲服務器處理主機數據。 也許你需要限制主機收到的列表,無論是數字或通過一些實物,如過濾器的位置。 主要是頻寬的優化。只要大家相互團結,妥善溝通,所有事情都可以做到。

To perform any of these modifications, you must build, deploy, and host your own Master Server. Information about how to do this can be found on the Master Server Build page.

要執行任何這些修改,您必須建立,部署和托管您自己的主服務器。有關如何執行此操作可以在
Master Server Build page找到。

頁面最後更新:2011-05-24


原文出處: Master Server 主服务器 - Unity圣典
內容圖示
url email imgsrc image code quote
樣本
bold italic underline linethrough   












 [詳情...]
validation picture

注意事項:
預覽不需輸入認證碼,僅真正發送文章時才會檢查驗證碼。
認證碼有效期10分鐘,若輸入資料超過10分鐘,請您備份內容後,重新整理本頁並貼回您的內容,再輸入驗證碼送出。

選項

Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|