PlayWithReadyState.cs
6.73 KB
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
using System.Collections;
using System.Collections.Generic;
using System;
using NUnit.Framework;
using UnityEngine.TestTools;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.NetworkSystem;
#pragma warning disable 618
public class PlayWithReadyState
{
public bool isDone = false;
static NetworkHash128 playerHash = NetworkHash128.Parse("abcd1");
NetworkClient client1;
NetworkClient client2;
const short MsgId1 = 99;
const short MsgId2 = 98;
const short MsgId3 = 97;
const short FromClientMsg1 = 95;
const short FromClientMsg2 = 94;
const short FromClientMsg3 = 93;
public int numClientsConnected = 0;
public int msg1Count = 0;
public int msg3Count = 0;
public List<string> actualListOfCallbacks = new List<string>();
public List<string> resultListOfCallbacks = new List<string>()
{
"CheckClientsConnected:1",
"CheckClientsConnected:2",
"OnServerFromClientMsg1",
"Msg1",
"Msg1",
"OnServerFromClientMsg2",
"Msg3",
"Msg3",
"OnServerFromClientMsg3"
};
[UnityTest]
public IEnumerator PlayWithReadyStateTest()
{
NetworkClient.ShutdownAll();
NetworkServer.Reset();
GameObject nmObject = new GameObject();
PlayWithReadyStateNetworkManager nmanager = nmObject.AddComponent<PlayWithReadyStateNetworkManager>();
nmanager.networkAddress = "localhost";
ClientScene.RegisterSpawnHandler(playerHash, PlayWithReadyStateNetworkManager.OnSpawnPlayer, PlayWithReadyStateNetworkManager.OnUnSpawnPlayer);
NetworkServer.RegisterHandler(FromClientMsg1, OnServerFromClientMsg1);
NetworkServer.RegisterHandler(FromClientMsg2, OnServerFromClientMsg2);
NetworkServer.RegisterHandler(FromClientMsg3, OnServerFromClientMsg3);
nmanager.StartServer();
client1 = nmanager.StartClient();
client1.RegisterHandler(MsgType.Connect, OnClient1Connect);
client1.RegisterHandler(MsgId1, OnMsg1);
client1.RegisterHandler(MsgId2, OnMsg2);
client1.RegisterHandler(MsgId3, OnMsg3);
// client2 is never ready, so should not recieve msgs
client2 = new NetworkClient();
client2.RegisterHandler(MsgType.Connect, OnClient2Connect);
client2.RegisterHandler(MsgId1, OnMsg1);
client2.RegisterHandler(MsgId2, OnMsg2);
client2.RegisterHandler(MsgId3, OnMsg3);
client2.RegisterHandler(MsgType.NotReady, OnNotReady);
Assert.IsTrue(NetworkServer.active, "Server is not started");
Assert.IsTrue(NetworkClient.active, "Client is not started");
client2.Connect(NetworkManager.singleton.networkAddress, NetworkManager.singleton.networkPort);
yield return null;
while (!isDone)
{
yield return null;
}
CollectionAssert.AreEqual(resultListOfCallbacks, actualListOfCallbacks, "Wrong order of callbacks or some callback is missing");
nmanager.StopServer();
nmanager.StopClient();
NetworkClient.ShutdownAll();
UnityEngine.Object.Destroy(nmObject);
}
//need to handle this message as it is sent by NetworkManager,
//but it can appear with delay - so we can't guarantee order
void OnNotReady(NetworkMessage netMsg)
{
}
// Server Flow
// This block results in Msg1 printed twice (sent to same client twice)
void OnServerFromClientMsg1(NetworkMessage netMsg)
{
// both clients are connected now
actualListOfCallbacks.Add("OnServerFromClientMsg1");
NetworkServer.SetClientReady(netMsg.conn);
// this will go to only 1 client
NetworkServer.SendToReady(null, MsgId1, new EmptyMessage());
// this will go to only 1 client
var tm = NetworkManager.singleton as PlayWithReadyStateNetworkManager;
NetworkServer.SendToReady(tm.thePlayer, MsgId1, new EmptyMessage());
}
// This block results in Msg2 printed twice (sent to both clients)
void OnServerFromClientMsg2(NetworkMessage netMsg)
{
actualListOfCallbacks.Add("OnServerFromClientMsg2");
NetworkServer.SetAllClientsNotReady();
// clients should NOT receive this
NetworkServer.SendToReady(null, MsgId2, new EmptyMessage());
// both clients SHOULD receive this
NetworkServer.SendToAll(MsgId3, new EmptyMessage());
}
private void OnServerFromClientMsg3(NetworkMessage netMsg)
{
actualListOfCallbacks.Add("OnServerFromClientMsg3");
isDone = true;
}
// Client Flow
void OnClient1Connect(NetworkMessage netMsg)
{
numClientsConnected += 1;
CheckClientsConnected(netMsg.conn);
}
void OnClient2Connect(NetworkMessage netMsg)
{
numClientsConnected += 1;
CheckClientsConnected(netMsg.conn);
}
void CheckClientsConnected(NetworkConnection conn)
{
actualListOfCallbacks.Add("CheckClientsConnected:" + numClientsConnected);
if (numClientsConnected == 2)
{
conn.Send(FromClientMsg1, new EmptyMessage());
}
}
void OnMsg1(NetworkMessage netMsg)
{
actualListOfCallbacks.Add("Msg1");
msg1Count += 1;
if (msg1Count == 2)
{
netMsg.conn.Send(FromClientMsg2, new EmptyMessage());
}
}
void OnMsg2(NetworkMessage netMsg)
{
// should not ever be received!
Assert.Fail("This message should not be received: Msg2 " + netMsg.conn.connectionId);
}
void OnMsg3(NetworkMessage netMsg)
{
actualListOfCallbacks.Add("Msg3");
msg3Count += 1;
if (msg3Count == 2)
{
netMsg.conn.Send(FromClientMsg3, new EmptyMessage());
}
}
public class PlayWithReadyStateNetworkManager : NetworkManager
{
public GameObject thePlayer;
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
{
thePlayer = (GameObject)OnSpawnPlayer(Vector3.zero, playerHash);
NetworkServer.AddPlayerForConnection(conn, thePlayer, playerControllerId, playerHash);
}
public static GameObject OnSpawnPlayer(Vector3 pos, NetworkHash128 assetId)
{
try
{
GameObject thePlayer = new GameObject();
thePlayer.name = "PlayWithReadyStatePrefab";
thePlayer.AddComponent<NetworkIdentity>();
return thePlayer;
}
catch (Exception e)
{
Assert.Fail("Spawn exception " + e);
return null;
}
}
public static void OnUnSpawnPlayer(GameObject unspawned)
{
Destroy(unspawned);
}
}
}
#pragma warning restore 618