CommandsAndRPCCallsWork.cs
6.16 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
217
218
219
220
221
222
223
224
225
using System;
using System.Collections;
using NUnit.Framework;
using UnityEngine.TestTools;
using UnityEngine;
using UnityEngine.Networking;
using Object = UnityEngine.Object;
#pragma warning disable 618
public class CommandsAndRPCCallsWork
{
int kListenPort = 7073;
NetworkClient myClient;
static bool isTestDone;
static NetworkHash128 playerHash = NetworkHash128.Parse("abcd1");
public static GameObject OnSpawnPlayer(Vector3 pos, NetworkHash128 assetId)
{
try
{
GameObject serverPlayer = new GameObject();
serverPlayer.name = "MyPlayer";
serverPlayer.AddComponent<CommandTestPlayerBehaviourExtra>();
serverPlayer.AddComponent<CommandTestPlayerBehaviour>();
return serverPlayer;
}
catch (Exception e)
{
Assert.Fail("Spawn exception " + e);
return null;
}
}
public static void OnUnSpawnPlayer(GameObject unspawned)
{
Object.Destroy(unspawned);
}
[UnityTest]
public IEnumerator CommandsAndRPCCallsWorkTest()
{
NetworkClient.ShutdownAll();
NetworkServer.Reset();
ConnectionConfig config = new ConnectionConfig();
config.AddChannel(QosType.ReliableSequenced);// this test requires correct sequence of packets.
config.AddChannel(QosType.Unreliable);
myClient = new NetworkClient();
if (!myClient.Configure(config, 10))
{
Assert.Fail("Client configure failed");
}
NetworkServer.RegisterHandler(MsgType.AddPlayer, OnAddPlayer);
myClient.RegisterHandler(MsgType.Connect, OnClientConnected);
myClient.RegisterHandler(MsgType.Error, OnClientError);
ClientScene.RegisterSpawnHandler(playerHash, OnSpawnPlayer, OnUnSpawnPlayer);
int retries = 0;
while (!NetworkServer.Listen("127.0.0.1", ++kListenPort))
{
Assert.IsTrue(retries++ < 10, "Couldn't Listen for more than 10 retries");
}
myClient.Connect("127.0.0.1", kListenPort);
while (!isTestDone)
{
yield return null;
}
}
public void OnAddPlayer(NetworkMessage netMsg)
{
var msg = netMsg.ReadMessage<UnityEngine.Networking.NetworkSystem.AddPlayerMessage>();
GameObject obj = OnSpawnPlayer(Vector3.zero, playerHash);
NetworkServer.AddPlayerForConnection(netMsg.conn, obj, msg.playerControllerId, playerHash);
CommandTestPlayerBehaviour beh = obj.GetComponent<CommandTestPlayerBehaviour>();
Assert.IsNotNull(beh, "No component CommandTestPlayerBehaviour");
string args = "foo";
beh.RpcTestOnClient(args);
beh.TargetTestOnOne(netMsg.conn, "one");
}
public void OnClientConnected(NetworkMessage netMsg)
{
ClientScene.AddPlayer(netMsg.conn, 1);
}
public void OnClientError(NetworkMessage netMsg)
{
Assert.Fail("Connect Error");
}
// extra NetworkBehaviour component on the player, to check for multiple NetworkBehaviour issues
public class CommandTestPlayerBehaviourExtra : NetworkBehaviour
{
[SyncVar]
int extraData;
}
public class CommandTestPlayerBehaviour : NetworkBehaviour
{
public struct Inner
{
public string aString;
public double aDouble;
}
public struct Outer
{
public float aFloat;
public int aInt;
public Inner aInner;
}
public static int numEvents = 0;
public delegate void IntegerEventDelegate(int value);
private int testInt = 77;
private float testFloat = 55.5f;
private int testValue = 100;
private int testCmdCount = 0;
private int testCmdValidate = 0;
[SyncEvent]
public event IntegerEventDelegate EventDoInt1;
[SyncEvent]
public event IntegerEventDelegate EventDoInt2;
void Awake()
{
// test multiple events in this script
EventDoInt1 += OnIntegerEvent1;
EventDoInt1 += OnIntegerEvent2;
EventDoInt2 += OnIntegerEvent2;
}
void OnIntegerEvent1(int value)
{
Debug.Log("OnIntegerEvent1");
Assert.AreEqual(testValue, value);
numEvents += 1;
}
void OnIntegerEvent2(int value)
{
Debug.Log("OnIntegerEvent2");
Assert.AreEqual(testValue, value);
numEvents += 1;
}
private void Update()
{
// 3 = 2 events from EventDo1 + 1 event from EventDo2
if (numEvents == 3 && isClient)
{
// this tests that all commands arrive in the correct order
CmdCount(testCmdCount++);
if (testCmdCount == 100)
{
isTestDone = true;
}
Outer outer = new Outer();
outer.aInt = 99;
outer.aInner = new Inner();
outer.aInner.aDouble = 1.2;
CmdDoOuter(outer);
}
}
[Command]
void CmdCount(int count)
{
Assert.AreEqual(count, testCmdValidate++);
}
[Command]
void CmdDoOuter(Outer outer)
{
Assert.AreEqual(99, outer.aInt);
Assert.AreEqual(1.2, outer.aInner.aDouble, 0.001);
}
[Command]
public void CmdTestCommandOnServer(int arg1, float arg2)
{
Assert.AreEqual(testInt, arg1);
Assert.AreEqual(testFloat, arg2);
if (EventDoInt1 != null)
{
EventDoInt1(testValue);
}
if (EventDoInt2 != null)
{
EventDoInt2(testValue);
}
}
[TargetRpc]
public void TargetTestOnOne(NetworkConnection target, string arg)
{
Assert.AreEqual(arg, "one");
}
[ClientRpc]
public void RpcTestOnClient(string arg)
{
Assert.AreEqual(arg, "foo");
CmdTestCommandOnServer(testInt, testFloat);
}
}
}
#pragma warning restore 618