forked from hunterzonewu/unity-decompiled
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkServer.cs
More file actions
1900 lines (1777 loc) · 74 KB
/
NetworkServer.cs
File metadata and controls
1900 lines (1777 loc) · 74 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Decompiled with JetBrains decompiler
// Type: UnityEngine.Networking.NetworkServer
// Assembly: UnityEngine.Networking, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 8B34E19C-EF53-416E-AE36-35C45BAFD2DE
// Assembly location: C:\Users\Blake\sandbox\unity\test-project\Library\UnityAssemblies\UnityEngine.Networking.dll
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using UnityEditor;
using UnityEngine.Networking.Match;
using UnityEngine.Networking.NetworkSystem;
using UnityEngine.Networking.Types;
namespace UnityEngine.Networking
{
/// <summary>
/// <para>High level network server.</para>
/// </summary>
public sealed class NetworkServer
{
private static object s_Sync = (object) new UnityEngine.Object();
private static RemovePlayerMessage s_RemovePlayerMessage = new RemovePlayerMessage();
private List<NetworkConnection> m_LocalConnectionsFakeList = new List<NetworkConnection>();
private float m_MaxDelay = 0.1f;
private const int k_RemoveListInterval = 100;
private static bool s_Active;
private static volatile NetworkServer s_Instance;
private static bool m_DontListen;
private bool m_LocalClientActive;
private ULocalConnectionToClient m_LocalConnection;
private NetworkScene m_NetworkScene;
private HashSet<int> m_ExternalConnections;
private NetworkServer.ServerSimpleWrapper m_SimpleServerSimple;
private HashSet<NetworkInstanceId> m_RemoveList;
private int m_RemoveListCount;
internal static ushort maxPacketSize;
/// <summary>
/// <para>A list of local connections on the server.</para>
/// </summary>
public static List<NetworkConnection> localConnections
{
get
{
return NetworkServer.instance.m_LocalConnectionsFakeList;
}
}
/// <summary>
/// <para>The port that the server is listening on.</para>
/// </summary>
public static int listenPort
{
get
{
return NetworkServer.instance.m_SimpleServerSimple.listenPort;
}
}
/// <summary>
/// <para>The transport layer hostId used by this server.</para>
/// </summary>
public static int serverHostId
{
get
{
return NetworkServer.instance.m_SimpleServerSimple.serverHostId;
}
}
/// <summary>
/// <para>A list of all the current connections from clients.</para>
/// </summary>
public static ReadOnlyCollection<NetworkConnection> connections
{
get
{
return NetworkServer.instance.m_SimpleServerSimple.connections;
}
}
/// <summary>
/// <para>Dictionary of the message handlers registered with the server.</para>
/// </summary>
public static Dictionary<short, NetworkMessageDelegate> handlers
{
get
{
return NetworkServer.instance.m_SimpleServerSimple.handlers;
}
}
/// <summary>
/// <para>The host topology that the server is using.</para>
/// </summary>
public static HostTopology hostTopology
{
get
{
return NetworkServer.instance.m_SimpleServerSimple.hostTopology;
}
}
/// <summary>
/// <para>This is a dictionary of networked objects that have been spawned on the server.</para>
/// </summary>
public static Dictionary<NetworkInstanceId, NetworkIdentity> objects
{
get
{
return NetworkServer.instance.m_NetworkScene.localObjects;
}
}
/// <summary>
/// <para>Setting this true will make the server send peer info to all participants of the network.</para>
/// </summary>
[Obsolete("Moved to NetworkMigrationManager")]
public static bool sendPeerInfo
{
get
{
return false;
}
set
{
}
}
/// <summary>
/// <para>If you enable this, the server will not listen for incoming connections on the regular network port.</para>
/// </summary>
public static bool dontListen
{
get
{
return NetworkServer.m_DontListen;
}
set
{
NetworkServer.m_DontListen = value;
}
}
/// <summary>
/// <para>This makes the server listen for WebSockets connections instead of normal transport layer connections.</para>
/// </summary>
public static bool useWebSockets
{
get
{
return NetworkServer.instance.m_SimpleServerSimple.useWebSockets;
}
set
{
NetworkServer.instance.m_SimpleServerSimple.useWebSockets = value;
}
}
internal static NetworkServer instance
{
get
{
if (NetworkServer.s_Instance == null)
{
lock (NetworkServer.s_Sync)
{
if (NetworkServer.s_Instance == null)
NetworkServer.s_Instance = new NetworkServer();
}
}
return NetworkServer.s_Instance;
}
}
/// <summary>
/// <para>Checks if the server has been started.</para>
/// </summary>
public static bool active
{
get
{
return NetworkServer.s_Active;
}
}
/// <summary>
/// <para>True is a local client is currently active on the server.</para>
/// </summary>
public static bool localClientActive
{
get
{
return NetworkServer.instance.m_LocalClientActive;
}
}
/// <summary>
/// <para>The number of channels the network is configure with.</para>
/// </summary>
public static int numChannels
{
get
{
return NetworkServer.instance.m_SimpleServerSimple.hostTopology.DefaultConfig.ChannelCount;
}
}
/// <summary>
/// <para>The maximum delay before sending packets on connections.</para>
/// </summary>
public static float maxDelay
{
get
{
return NetworkServer.instance.m_MaxDelay;
}
set
{
NetworkServer.instance.InternalSetMaxDelay(value);
}
}
/// <summary>
/// <para>The class to be used when creating new network connections.</para>
/// </summary>
public static System.Type networkConnectionClass
{
get
{
return NetworkServer.instance.m_SimpleServerSimple.networkConnectionClass;
}
}
private NetworkServer()
{
NetworkTransport.Init();
if (LogFilter.logDev)
Debug.Log((object) ("NetworkServer Created version " + (object) Version.Current));
this.m_RemoveList = new HashSet<NetworkInstanceId>();
this.m_ExternalConnections = new HashSet<int>();
this.m_NetworkScene = new NetworkScene();
this.m_SimpleServerSimple = new NetworkServer.ServerSimpleWrapper(this);
}
public static void SetNetworkConnectionClass<T>() where T : NetworkConnection
{
NetworkServer.instance.m_SimpleServerSimple.SetNetworkConnectionClass<T>();
}
/// <summary>
/// <para>This configures the transport layer settings for the server.</para>
/// </summary>
/// <param name="config">Transport layer confuration object.</param>
/// <param name="maxConnections">The maximum number of client connections to allow.</param>
/// <param name="topology">Transport layer topology object to use.</param>
/// <returns>
/// <para>True if successfully configured.</para>
/// </returns>
public static bool Configure(ConnectionConfig config, int maxConnections)
{
return NetworkServer.instance.m_SimpleServerSimple.Configure(config, maxConnections);
}
/// <summary>
/// <para>This configures the transport layer settings for the server.</para>
/// </summary>
/// <param name="config">Transport layer confuration object.</param>
/// <param name="maxConnections">The maximum number of client connections to allow.</param>
/// <param name="topology">Transport layer topology object to use.</param>
/// <returns>
/// <para>True if successfully configured.</para>
/// </returns>
public static bool Configure(HostTopology topology)
{
return NetworkServer.instance.m_SimpleServerSimple.Configure(topology);
}
/// <summary>
/// <para>Reset the NetworkServer singleton.</para>
/// </summary>
public static void Reset()
{
NetworkDetailStats.ResetAll();
NetworkTransport.Shutdown();
NetworkTransport.Init();
NetworkServer.s_Instance = (NetworkServer) null;
NetworkServer.s_Active = false;
}
/// <summary>
/// <para>This shuts down the server and disconnects all clients.</para>
/// </summary>
public static void Shutdown()
{
if (NetworkServer.s_Instance != null)
{
NetworkServer.s_Instance.InternalDisconnectAll();
if (!NetworkServer.m_DontListen)
NetworkServer.s_Instance.m_SimpleServerSimple.Stop();
NetworkServer.s_Instance = (NetworkServer) null;
}
NetworkServer.m_DontListen = false;
NetworkServer.s_Active = false;
}
public static bool Listen(MatchInfo matchInfo, int listenPort)
{
if (!matchInfo.usingRelay)
return NetworkServer.instance.InternalListen((string) null, listenPort);
NetworkServer.instance.InternalListenRelay(matchInfo.address, matchInfo.port, matchInfo.networkId, Utility.GetSourceID(), matchInfo.nodeId);
return true;
}
internal void RegisterMessageHandlers()
{
this.m_SimpleServerSimple.RegisterHandlerSafe((short) 35, new NetworkMessageDelegate(NetworkServer.OnClientReadyMessage));
this.m_SimpleServerSimple.RegisterHandlerSafe((short) 5, new NetworkMessageDelegate(NetworkServer.OnCommandMessage));
this.m_SimpleServerSimple.RegisterHandlerSafe((short) 6, new NetworkMessageDelegate(NetworkTransform.HandleTransform));
this.m_SimpleServerSimple.RegisterHandlerSafe((short) 16, new NetworkMessageDelegate(NetworkTransformChild.HandleChildTransform));
this.m_SimpleServerSimple.RegisterHandlerSafe((short) 38, new NetworkMessageDelegate(NetworkServer.OnRemovePlayerMessage));
this.m_SimpleServerSimple.RegisterHandlerSafe((short) 40, new NetworkMessageDelegate(NetworkAnimator.OnAnimationServerMessage));
this.m_SimpleServerSimple.RegisterHandlerSafe((short) 41, new NetworkMessageDelegate(NetworkAnimator.OnAnimationParametersServerMessage));
this.m_SimpleServerSimple.RegisterHandlerSafe((short) 42, new NetworkMessageDelegate(NetworkAnimator.OnAnimationTriggerServerMessage));
NetworkServer.maxPacketSize = NetworkServer.hostTopology.DefaultConfig.PacketSize;
}
/// <summary>
/// <para>Starts a server using a relay server. This is the manual way of using the relay server, as the regular NetworkServer.Connect() will automatically use the relay server if a match exists.</para>
/// </summary>
/// <param name="relayIp">Relay server IP Address.</param>
/// <param name="relayPort">Relay server port.</param>
/// <param name="netGuid">GUID of the network to create.</param>
/// <param name="sourceId">This server's sourceId.</param>
/// <param name="nodeId">The node to join the network with.</param>
public static void ListenRelay(string relayIp, int relayPort, NetworkID netGuid, SourceID sourceId, NodeID nodeId)
{
NetworkServer.instance.InternalListenRelay(relayIp, relayPort, netGuid, sourceId, nodeId);
}
private void InternalListenRelay(string relayIp, int relayPort, NetworkID netGuid, SourceID sourceId, NodeID nodeId)
{
this.m_SimpleServerSimple.ListenRelay(relayIp, relayPort, netGuid, sourceId, nodeId);
NetworkServer.s_Active = true;
this.RegisterMessageHandlers();
}
/// <summary>
/// <para>Start the server on the given port number. Note that if a match has been created, this will listen using the relay server instead of a local socket.</para>
/// </summary>
/// <param name="ipAddress">The IP address to bind to (optional).</param>
/// <param name="serverPort">Listen port number.</param>
/// <returns>
/// <para>True if listen succeeded.</para>
/// </returns>
public static bool Listen(int serverPort)
{
return NetworkServer.instance.InternalListen((string) null, serverPort);
}
/// <summary>
/// <para>Start the server on the given port number. Note that if a match has been created, this will listen using the relay server instead of a local socket.</para>
/// </summary>
/// <param name="ipAddress">The IP address to bind to (optional).</param>
/// <param name="serverPort">Listen port number.</param>
/// <returns>
/// <para>True if listen succeeded.</para>
/// </returns>
public static bool Listen(string ipAddress, int serverPort)
{
return NetworkServer.instance.InternalListen(ipAddress, serverPort);
}
internal bool InternalListen(string ipAddress, int serverPort)
{
if (NetworkServer.m_DontListen)
this.m_SimpleServerSimple.Initialize();
else if (!this.m_SimpleServerSimple.Listen(ipAddress, serverPort))
return false;
NetworkServer.maxPacketSize = NetworkServer.hostTopology.DefaultConfig.PacketSize;
NetworkServer.s_Active = true;
this.RegisterMessageHandlers();
return true;
}
public static NetworkClient BecomeHost(NetworkClient oldClient, int port, MatchInfo matchInfo, int oldConnectionId, PeerInfoMessage[] peers)
{
return NetworkServer.instance.BecomeHostInternal(oldClient, port, matchInfo, oldConnectionId, peers);
}
internal NetworkClient BecomeHostInternal(NetworkClient oldClient, int port, MatchInfo matchInfo, int oldConnectionId, PeerInfoMessage[] peers)
{
if (NetworkServer.s_Active)
{
if (LogFilter.logError)
Debug.LogError((object) "BecomeHost already a server.");
return (NetworkClient) null;
}
if (!NetworkClient.active)
{
if (LogFilter.logError)
Debug.LogError((object) "BecomeHost NetworkClient not active.");
return (NetworkClient) null;
}
NetworkServer.Configure(NetworkServer.hostTopology);
if (matchInfo == null)
{
if (LogFilter.logDev)
Debug.Log((object) ("BecomeHost Listen on " + (object) port));
if (!NetworkServer.Listen(port))
{
if (LogFilter.logError)
Debug.LogError((object) "BecomeHost bind failed.");
return (NetworkClient) null;
}
}
else
{
if (LogFilter.logDev)
Debug.Log((object) ("BecomeHost match:" + (object) matchInfo.networkId));
NetworkServer.ListenRelay(matchInfo.address, matchInfo.port, matchInfo.networkId, Utility.GetSourceID(), matchInfo.nodeId);
}
using (Dictionary<NetworkInstanceId, NetworkIdentity>.ValueCollection.Enumerator enumerator = ClientScene.objects.Values.GetEnumerator())
{
while (enumerator.MoveNext())
{
NetworkIdentity current = enumerator.Current;
if (!((UnityEngine.Object) current == (UnityEngine.Object) null) && !((UnityEngine.Object) current.gameObject == (UnityEngine.Object) null))
{
NetworkIdentity.AddNetworkId(current.netId.Value);
this.m_NetworkScene.SetLocalObject(current.netId, current.gameObject, false, false);
current.OnStartServer(true);
}
}
}
if (LogFilter.logDev)
Debug.Log((object) ("NetworkServer BecomeHost done. oldConnectionId:" + (object) oldConnectionId));
this.RegisterMessageHandlers();
if (!NetworkClient.RemoveClient(oldClient) && LogFilter.logError)
Debug.LogError((object) "BecomeHost failed to remove client");
if (LogFilter.logDev)
Debug.Log((object) "BecomeHost localClient ready");
NetworkClient networkClient = ClientScene.ReconnectLocalServer();
ClientScene.Ready(networkClient.connection);
ClientScene.SetReconnectId(oldConnectionId, peers);
ClientScene.AddPlayer(ClientScene.readyConnection, (short) 0);
return networkClient;
}
private void InternalSetMaxDelay(float seconds)
{
for (int index = 0; index < NetworkServer.connections.Count; ++index)
{
NetworkConnection connection = NetworkServer.connections[index];
if (connection != null)
connection.SetMaxDelay(seconds);
}
this.m_MaxDelay = seconds;
}
internal int AddLocalClient(LocalClient localClient)
{
if (this.m_LocalConnectionsFakeList.Count != 0)
{
Debug.LogError((object) "Local Connection already exists");
return -1;
}
this.m_LocalConnection = new ULocalConnectionToClient(localClient);
this.m_LocalConnection.connectionId = 0;
this.m_SimpleServerSimple.SetConnectionAtIndex((NetworkConnection) this.m_LocalConnection);
this.m_LocalConnectionsFakeList.Add((NetworkConnection) this.m_LocalConnection);
this.m_LocalConnection.InvokeHandlerNoData((short) 32);
return 0;
}
internal void RemoveLocalClient(NetworkConnection localClientConnection)
{
for (int index = 0; index < this.m_LocalConnectionsFakeList.Count; ++index)
{
if (this.m_LocalConnectionsFakeList[index].connectionId == localClientConnection.connectionId)
{
this.m_LocalConnectionsFakeList.RemoveAt(index);
break;
}
}
if (this.m_LocalConnection != null)
{
this.m_LocalConnection.Disconnect();
this.m_LocalConnection.Dispose();
this.m_LocalConnection = (ULocalConnectionToClient) null;
}
this.m_LocalClientActive = false;
this.m_SimpleServerSimple.RemoveConnectionAtIndex(0);
}
internal void SetLocalObjectOnServer(NetworkInstanceId netId, GameObject obj)
{
if (LogFilter.logDev)
Debug.Log((object) ("SetLocalObjectOnServer " + (object) netId + " " + (object) obj));
this.m_NetworkScene.SetLocalObject(netId, obj, false, true);
}
internal void ActivateLocalClientScene()
{
if (this.m_LocalClientActive)
return;
this.m_LocalClientActive = true;
using (Dictionary<NetworkInstanceId, NetworkIdentity>.ValueCollection.Enumerator enumerator = NetworkServer.objects.Values.GetEnumerator())
{
while (enumerator.MoveNext())
{
NetworkIdentity current = enumerator.Current;
if (!current.isClient)
{
if (LogFilter.logDev)
Debug.Log((object) ("ActivateClientScene " + (object) current.netId + " " + (object) current.gameObject));
ClientScene.SetLocalObject(current.netId, current.gameObject);
current.OnStartClient();
}
}
}
}
public static bool SendToAll(short msgType, MessageBase msg)
{
if (LogFilter.logDev)
Debug.Log((object) ("Server.SendToAll msgType:" + (object) msgType));
bool flag = true;
for (int index = 0; index < NetworkServer.connections.Count; ++index)
{
NetworkConnection connection = NetworkServer.connections[index];
if (connection != null)
flag &= connection.Send(msgType, msg);
}
return flag;
}
private static bool SendToObservers(GameObject contextObj, short msgType, MessageBase msg)
{
if (LogFilter.logDev)
Debug.Log((object) ("Server.SendToObservers id:" + (object) msgType));
bool flag = true;
NetworkIdentity component = contextObj.GetComponent<NetworkIdentity>();
if ((UnityEngine.Object) component == (UnityEngine.Object) null || component.observers == null)
return false;
int count = component.observers.Count;
for (int index = 0; index < count; ++index)
{
NetworkConnection observer = component.observers[index];
flag &= observer.Send(msgType, msg);
}
return flag;
}
public static bool SendToReady(GameObject contextObj, short msgType, MessageBase msg)
{
if (LogFilter.logDev)
Debug.Log((object) ("Server.SendToReady id:" + (object) msgType));
if ((UnityEngine.Object) contextObj == (UnityEngine.Object) null)
{
for (int index = 0; index < NetworkServer.connections.Count; ++index)
{
NetworkConnection connection = NetworkServer.connections[index];
if (connection != null && connection.isReady)
connection.Send(msgType, msg);
}
return true;
}
bool flag = true;
NetworkIdentity component = contextObj.GetComponent<NetworkIdentity>();
if ((UnityEngine.Object) component == (UnityEngine.Object) null || component.observers == null)
return false;
int count = component.observers.Count;
for (int index = 0; index < count; ++index)
{
NetworkConnection observer = component.observers[index];
if (observer.isReady)
flag &= observer.Send(msgType, msg);
}
return flag;
}
public static void SendWriterToReady(GameObject contextObj, NetworkWriter writer, int channelId)
{
if (writer.AsArraySegment().Count > (int) short.MaxValue)
throw new UnityException("NetworkWriter used buffer is too big!");
NetworkServer.SendBytesToReady(contextObj, writer.AsArraySegment().Array, writer.AsArraySegment().Count, channelId);
}
public static void SendBytesToReady(GameObject contextObj, byte[] buffer, int numBytes, int channelId)
{
if ((UnityEngine.Object) contextObj == (UnityEngine.Object) null)
{
bool flag = true;
for (int index = 0; index < NetworkServer.connections.Count; ++index)
{
NetworkConnection connection = NetworkServer.connections[index];
if (connection != null && connection.isReady && !connection.SendBytes(buffer, numBytes, channelId))
flag = false;
}
if (flag || !LogFilter.logWarn)
return;
Debug.LogWarning((object) "SendBytesToReady failed");
}
else
{
NetworkIdentity component = contextObj.GetComponent<NetworkIdentity>();
try
{
bool flag = true;
int count = component.observers.Count;
for (int index = 0; index < count; ++index)
{
NetworkConnection observer = component.observers[index];
if (observer.isReady && !observer.SendBytes(buffer, numBytes, channelId))
flag = false;
}
if (flag || !LogFilter.logWarn)
return;
Debug.LogWarning((object) ("SendBytesToReady failed for " + (object) contextObj));
}
catch (NullReferenceException ex)
{
if (!LogFilter.logWarn)
return;
Debug.LogWarning((object) ("SendBytesToReady object " + (object) contextObj + " has not been spawned"));
}
}
}
/// <summary>
/// <para>This sends an array of bytes to a specific player.</para>
/// </summary>
/// <param name="player">The player to send he bytes to.</param>
/// <param name="buffer">Array of bytes to send.</param>
/// <param name="numBytes">Size of array.</param>
/// <param name="channelId">Transport layer channel id to send bytes on.</param>
public static void SendBytesToPlayer(GameObject player, byte[] buffer, int numBytes, int channelId)
{
for (int index1 = 0; index1 < NetworkServer.connections.Count; ++index1)
{
NetworkConnection connection = NetworkServer.connections[index1];
if (connection != null)
{
for (int index2 = 0; index2 < connection.playerControllers.Count; ++index2)
{
if (connection.playerControllers[index2].IsValid && (UnityEngine.Object) connection.playerControllers[index2].gameObject == (UnityEngine.Object) player)
{
connection.SendBytes(buffer, numBytes, channelId);
break;
}
}
}
}
}
public static bool SendUnreliableToAll(short msgType, MessageBase msg)
{
if (LogFilter.logDev)
Debug.Log((object) ("Server.SendUnreliableToAll msgType:" + (object) msgType));
bool flag = true;
for (int index = 0; index < NetworkServer.connections.Count; ++index)
{
NetworkConnection connection = NetworkServer.connections[index];
if (connection != null)
flag &= connection.SendUnreliable(msgType, msg);
}
return flag;
}
public static bool SendUnreliableToReady(GameObject contextObj, short msgType, MessageBase msg)
{
if (LogFilter.logDev)
Debug.Log((object) ("Server.SendUnreliableToReady id:" + (object) msgType));
if ((UnityEngine.Object) contextObj == (UnityEngine.Object) null)
{
for (int index = 0; index < NetworkServer.connections.Count; ++index)
{
NetworkConnection connection = NetworkServer.connections[index];
if (connection != null && connection.isReady)
connection.SendUnreliable(msgType, msg);
}
return true;
}
bool flag = true;
NetworkIdentity component = contextObj.GetComponent<NetworkIdentity>();
int count = component.observers.Count;
for (int index = 0; index < count; ++index)
{
NetworkConnection observer = component.observers[index];
if (observer.isReady)
flag &= observer.SendUnreliable(msgType, msg);
}
return flag;
}
/// <summary>
/// <para>Sends a network message to all connected clients on a specified transport layer QoS channel.</para>
/// </summary>
/// <param name="msgType">The message id.</param>
/// <param name="msg">The message to send.</param>
/// <param name="channelId">The transport layer channel to use.</param>
/// <returns>
/// <para>True if the message was sent.</para>
/// </returns>
public static bool SendByChannelToAll(short msgType, MessageBase msg, int channelId)
{
if (LogFilter.logDev)
Debug.Log((object) ("Server.SendByChannelToAll id:" + (object) msgType));
bool flag = true;
for (int index = 0; index < NetworkServer.connections.Count; ++index)
{
NetworkConnection connection = NetworkServer.connections[index];
if (connection != null)
flag &= connection.SendByChannel(msgType, msg, channelId);
}
return flag;
}
/// <summary>
/// <para>Sends a network message to all connected clients that are "ready" on a specified transport layer QoS channel.</para>
/// </summary>
/// <param name="contextObj">An object to use for context when calculating object visibility. If null, then the message is sent to all ready clients.</param>
/// <param name="msgType">The message id.</param>
/// <param name="msg">The message to send.</param>
/// <param name="channelId">The transport layer channel to send on.</param>
/// <returns>
/// <para>True if the message was sent.</para>
/// </returns>
public static bool SendByChannelToReady(GameObject contextObj, short msgType, MessageBase msg, int channelId)
{
if (LogFilter.logDev)
Debug.Log((object) ("Server.SendByChannelToReady msgType:" + (object) msgType));
if ((UnityEngine.Object) contextObj == (UnityEngine.Object) null)
{
for (int index = 0; index < NetworkServer.connections.Count; ++index)
{
NetworkConnection connection = NetworkServer.connections[index];
if (connection != null && connection.isReady)
connection.SendByChannel(msgType, msg, channelId);
}
return true;
}
bool flag = true;
NetworkIdentity component = contextObj.GetComponent<NetworkIdentity>();
int count = component.observers.Count;
for (int index = 0; index < count; ++index)
{
NetworkConnection observer = component.observers[index];
if (observer.isReady)
flag &= observer.SendByChannel(msgType, msg, channelId);
}
return flag;
}
/// <summary>
/// <para>Disconnect all currently connected clients.</para>
/// </summary>
public static void DisconnectAll()
{
NetworkServer.instance.InternalDisconnectAll();
}
internal void InternalDisconnectAll()
{
this.m_SimpleServerSimple.DisconnectAllConnections();
if (this.m_LocalConnection != null)
{
this.m_LocalConnection.Disconnect();
this.m_LocalConnection.Dispose();
this.m_LocalConnection = (ULocalConnectionToClient) null;
}
NetworkServer.s_Active = false;
this.m_LocalClientActive = false;
}
internal static void Update()
{
if (NetworkServer.s_Instance == null)
return;
NetworkServer.s_Instance.InternalUpdate();
}
private void UpdateServerObjects()
{
using (Dictionary<NetworkInstanceId, NetworkIdentity>.ValueCollection.Enumerator enumerator = NetworkServer.objects.Values.GetEnumerator())
{
while (enumerator.MoveNext())
{
NetworkIdentity current = enumerator.Current;
try
{
current.UNetUpdate();
}
catch (NullReferenceException ex)
{
}
catch (MissingReferenceException ex)
{
}
}
}
if (this.m_RemoveListCount++ % 100 != 0)
return;
this.CheckForNullObjects();
}
private void CheckForNullObjects()
{
using (Dictionary<NetworkInstanceId, NetworkIdentity>.KeyCollection.Enumerator enumerator = NetworkServer.objects.Keys.GetEnumerator())
{
while (enumerator.MoveNext())
{
NetworkInstanceId current = enumerator.Current;
NetworkIdentity networkIdentity = NetworkServer.objects[current];
if ((UnityEngine.Object) networkIdentity == (UnityEngine.Object) null || (UnityEngine.Object) networkIdentity.gameObject == (UnityEngine.Object) null)
this.m_RemoveList.Add(current);
}
}
if (this.m_RemoveList.Count <= 0)
return;
using (HashSet<NetworkInstanceId>.Enumerator enumerator = this.m_RemoveList.GetEnumerator())
{
while (enumerator.MoveNext())
NetworkServer.objects.Remove(enumerator.Current);
}
this.m_RemoveList.Clear();
}
internal void InternalUpdate()
{
this.m_SimpleServerSimple.Update();
if (NetworkServer.m_DontListen)
this.m_SimpleServerSimple.UpdateConnections();
this.UpdateServerObjects();
}
private void OnConnected(NetworkConnection conn)
{
if (LogFilter.logDebug)
Debug.Log((object) ("Server accepted client:" + (object) conn.connectionId));
conn.SetMaxDelay(this.m_MaxDelay);
conn.InvokeHandlerNoData((short) 32);
NetworkServer.SendCrc(conn);
}
private void OnDisconnected(NetworkConnection conn)
{
conn.InvokeHandlerNoData((short) 33);
for (int index = 0; index < conn.playerControllers.Count; ++index)
{
if ((UnityEngine.Object) conn.playerControllers[index].gameObject != (UnityEngine.Object) null && LogFilter.logWarn)
Debug.LogWarning((object) "Player not destroyed when connection disconnected.");
}
if (LogFilter.logDebug)
Debug.Log((object) ("Server lost client:" + (object) conn.connectionId));
conn.RemoveObservers();
conn.Dispose();
}
private void OnData(NetworkConnection conn, int receivedSize, int channelId)
{
NetworkDetailStats.IncrementStat(NetworkDetailStats.NetworkDirection.Incoming, (short) 29, "msg", 1);
conn.TransportRecieve(this.m_SimpleServerSimple.messageBuffer, receivedSize, channelId);
}
private void GenerateConnectError(int error)
{
if (LogFilter.logError)
Debug.LogError((object) ("UNet Server Connect Error: " + (object) error));
this.GenerateError((NetworkConnection) null, error);
}
private void GenerateDataError(NetworkConnection conn, int error)
{
NetworkError networkError = (NetworkError) error;
if (LogFilter.logError)
Debug.LogError((object) ("UNet Server Data Error: " + (object) networkError));
this.GenerateError(conn, error);
}
private void GenerateDisconnectError(NetworkConnection conn, int error)
{
NetworkError networkError = (NetworkError) error;
if (LogFilter.logError)
Debug.LogError((object) ("UNet Server Disconnect Error: " + (object) networkError + " conn:[" + (object) conn + "]:" + (object) conn.connectionId));
this.GenerateError(conn, error);
}
private void GenerateError(NetworkConnection conn, int error)
{
if (!NetworkServer.handlers.ContainsKey((short) 34))
return;
ErrorMessage errorMessage = new ErrorMessage();
errorMessage.errorCode = error;
NetworkWriter writer = new NetworkWriter();
errorMessage.Serialize(writer);
NetworkReader reader = new NetworkReader(writer);
conn.InvokeHandler((short) 34, reader, 0);
}
/// <summary>
/// <para>Register a handler for a particular message type.</para>
/// </summary>
/// <param name="msgType">Message type number.</param>
/// <param name="handler">Function handler which will be invoked for when this message type is received.</param>
public static void RegisterHandler(short msgType, NetworkMessageDelegate handler)
{
NetworkServer.instance.m_SimpleServerSimple.RegisterHandler(msgType, handler);
}
/// <summary>
/// <para>Unregisters a handler for a particular message type.</para>
/// </summary>
/// <param name="msgType">The message type to remove the handler for.</param>
public static void UnregisterHandler(short msgType)
{
NetworkServer.instance.m_SimpleServerSimple.UnregisterHandler(msgType);
}
/// <summary>
/// <para>Clear all registered callback handlers.</para>
/// </summary>
public static void ClearHandlers()
{
NetworkServer.instance.m_SimpleServerSimple.ClearHandlers();
}
/// <summary>
/// <para>Clears all registered spawn prefab and spawn handler functions for this server.</para>
/// </summary>
public static void ClearSpawners()
{
NetworkScene.ClearSpawners();
}
public static void GetStatsOut(out int numMsgs, out int numBufferedMsgs, out int numBytes, out int lastBufferedPerSecond)
{
numMsgs = 0;
numBufferedMsgs = 0;
numBytes = 0;
lastBufferedPerSecond = 0;
for (int index = 0; index < NetworkServer.connections.Count; ++index)
{
NetworkConnection connection = NetworkServer.connections[index];
if (connection != null)
{
int numMsgs1;
int numBufferedMsgs1;
int numBytes1;
int lastBufferedPerSecond1;
connection.GetStatsOut(out numMsgs1, out numBufferedMsgs1, out numBytes1, out lastBufferedPerSecond1);
numMsgs = numMsgs + numMsgs1;
numBufferedMsgs = numBufferedMsgs + numBufferedMsgs1;
numBytes = numBytes + numBytes1;
lastBufferedPerSecond = lastBufferedPerSecond + lastBufferedPerSecond1;
}
}
}
public static void GetStatsIn(out int numMsgs, out int numBytes)
{
numMsgs = 0;
numBytes = 0;
for (int index = 0; index < NetworkServer.connections.Count; ++index)
{
NetworkConnection connection = NetworkServer.connections[index];
if (connection != null)
{
int numMsgs1;
int numBytes1;
connection.GetStatsIn(out numMsgs1, out numBytes1);
numMsgs = numMsgs + numMsgs1;
numBytes = numBytes + numBytes1;
}
}
}
public static void SendToClientOfPlayer(GameObject player, short msgType, MessageBase msg)
{
for (int index1 = 0; index1 < NetworkServer.connections.Count; ++index1)
{
NetworkConnection connection = NetworkServer.connections[index1];
if (connection != null)
{
for (int index2 = 0; index2 < connection.playerControllers.Count; ++index2)
{
if (connection.playerControllers[index2].IsValid && (UnityEngine.Object) connection.playerControllers[index2].gameObject == (UnityEngine.Object) player)
{
connection.Send(msgType, msg);
return;
}
}
}
}
if (!LogFilter.logError)
return;
Debug.LogError((object) ("Failed to send message to player object '" + player.name + ", not found in connection list"));
}