-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworkLevelLoad.js
executable file
·238 lines (206 loc) · 7.19 KB
/
NetworkLevelLoad.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
DontDestroyOnLoad(this);
var gameName = "regulargame";
var serverPort = 25002;
private var timeoutHostList = 0.0;
private var lastHostListRequest = -1000.0;
private var hostListRefreshTimeout = 10.0;
private var connectionTestResult : ConnectionTesterStatus = ConnectionTesterStatus.Undetermined;
private var filterNATHosts = false;
private var probingPublicIP = false;
private var doneTesting = false;
private var timer : float = 0.0;
private var useNat = false; // Should the server enabled NAT punchthrough feature
private var windowRect;
private var serverListRect;
private var hideTest = false;
private var testMessage = "Undetermined NAT capabilities";
//MasterServer.
// Enable this if not running a client on the server machine
//MasterServer.dedicatedServer = true;
function OnFailedToConnectToMasterServer(info: NetworkConnectionError)
{
Debug.Log(info);
}
function OnFailedToConnect(info: NetworkConnectionError)
{
Debug.Log(info);
}
function OnGUI ()
{
windowRect = GUILayout.Window (0, windowRect, MakeWindow, "Server Controls");
if (Network.peerType == NetworkPeerType.Disconnected && MasterServer.PollHostList().Length != 0)
serverListRect = GUILayout.Window(1, serverListRect, MakeClientWindow, "Server List");
}
function Awake ()
{
//MasterServer.ipAddress = "192.168.1.43";
windowRect = Rect(Screen.width-300,0,300,100);
serverListRect = Rect(0, 0, Screen.width - windowRect.width, 100);
// Start connection test
connectionTestResult = Network.TestConnection();
// What kind of IP does this machine have? TestConnection also indicates this in the
// test results
if (Network.HavePublicAddress())
Debug.Log("This machine has a public IP address");
else
Debug.Log("This machine has a private IP address");
}
function Update()
{
// If test is undetermined, keep running
if (!doneTesting)
TestConnection();
}
function TestConnection()
{
// Start/Poll the connection test, report the results in a label and react to the results accordingly
connectionTestResult = Network.TestConnection();
switch (connectionTestResult)
{
case ConnectionTesterStatus.Error:
testMessage = "Problem determining NAT capabilities";
doneTesting = true;
break;
case ConnectionTesterStatus.Undetermined:
testMessage = "Undetermined NAT capabilities";
doneTesting = false;
break;
case ConnectionTesterStatus.PublicIPIsConnectable:
testMessage = "Directly connectable public IP address.";
useNat = false;
doneTesting = true;
break;
// This case is a bit special as we now need to check if we can
// circumvent the blocking by using NAT punchthrough
case ConnectionTesterStatus.PublicIPPortBlocked:
testMessage = "Non-connectble public IP address (port " + serverPort +" blocked), running a server is impossible.";
useNat = false;
// If no NAT punchthrough test has been performed on this public IP, force a test
if (!probingPublicIP)
{
Debug.Log("Testing if firewall can be circumvented");
connectionTestResult = Network.TestConnectionNAT();
probingPublicIP = true;
timer = Time.time + 10;
}
// NAT punchthrough test was performed but we still get blocked
else if (Time.time > timer)
{
probingPublicIP = false; // reset
useNat = true;
doneTesting = true;
}
break;
case ConnectionTesterStatus.PublicIPNoServerStarted:
testMessage = "Public IP address but server not initialized, it must be started to check server accessibility. Restart connection test when ready.";
break;
case ConnectionTesterStatus.LimitedNATPunchthroughPortRestricted:
Debug.Log("LimitedNATPunchthroughPortRestricted");
testMessage = "Limited NAT punchthrough capabilities. Cannot connect to all types of NAT servers.";
useNat = true;
doneTesting = true;
break;
case ConnectionTesterStatus.LimitedNATPunchthroughSymmetric:
Debug.Log("LimitedNATPunchthroughSymmetric");
testMessage = "Limited NAT punchthrough capabilities. Cannot connect to all types of NAT servers. Running a server is ill adviced as not everyone can connect.";
useNat = true;
doneTesting = true;
break;
case ConnectionTesterStatus.NATpunchthroughAddressRestrictedCone:
case ConnectionTesterStatus.NATpunchthroughFullCone:
Debug.Log("NATpunchthroughAddressRestrictedCone || NATpunchthroughFullCone");
testMessage = "NAT punchthrough capable. Can connect to all servers and receive connections from all clients. Enabling NAT punchthrough functionality.";
useNat = true;
doneTesting = true;
break;
default:
testMessage = "Error in test routine, got " + connectionTestResult;
}
//Debug.Log(connectionTestResult + " " + probingPublicIP + " " + doneTesting);
}
function MakeWindow (id : int)
{
hideTest = GUILayout.Toggle(hideTest, "Hide test info");
if (!hideTest)
{
GUILayout.Label(testMessage);
if (GUILayout.Button ("Retest connection"))
{
Debug.Log("Redoing connection test");
probingPublicIP = false;
doneTesting = false;
connectionTestResult = Network.TestConnection(true);
}
}
if (Network.peerType == NetworkPeerType.Disconnected)
{
GUILayout.BeginHorizontal();
GUILayout.Space(10);
// Start a new server
if (GUILayout.Button ("Start Server"))
{
Network.InitializeServer(32, serverPort, useNat);
MasterServer.RegisterHost(gameName, "stuff", "l33t game for all");
}
// Refresh hosts
if (GUILayout.Button ("Refresh available Servers") || Time.realtimeSinceStartup > lastHostListRequest + hostListRefreshTimeout)
{
MasterServer.RequestHostList (gameName);
lastHostListRequest = Time.realtimeSinceStartup;
}
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
}
else
{
if (GUILayout.Button ("Disconnect"))
{
Network.Disconnect();
MasterServer.UnregisterHost();
}
GUILayout.FlexibleSpace();
}
GUI.DragWindow (Rect (0,0,1000,1000));
}
function MakeClientWindow(id : int)
{
GUILayout.Space(5);
var data : HostData[] = MasterServer.PollHostList();
var count = 0;
for (var element in data)
{
GUILayout.BeginHorizontal();
// Do not display NAT enabled games if we cannot do NAT punchthrough
if ( !(filterNATHosts && element.useNat) )
{
var connections = element.connectedPlayers + "/" + element.playerLimit;
GUILayout.Label(element.gameName);
GUILayout.Space(5);
GUILayout.Label(connections);
GUILayout.Space(5);
var hostInfo = "";
// Indicate if NAT punchthrough will be performed, omit showing GUID
if (element.useNat)
{
GUILayout.Label("NAT");
GUILayout.Space(5);
}
// Here we display all IP addresses, there can be multiple in cases where
// internal LAN connections are being attempted. In the GUI we could just display
// the first one in order not confuse the end user, but internally Unity will
// do a connection check on all IP addresses in the element.ip list, and connect to the
// first valid one.
for (var host in element.ip)
hostInfo = hostInfo + host + ":" + element.port + " ";
//GUILayout.Label("[" + element.ip + ":" + element.port + "]");
GUILayout.Label(hostInfo);
GUILayout.Space(5);
GUILayout.Label(element.comment);
GUILayout.Space(5);
GUILayout.FlexibleSpace();
if (GUILayout.Button("Connect"))
Network.Connect(element);
}
GUILayout.EndHorizontal();
}
}