NetworkServer.cs 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Mirror.RemoteCalls;
  5. using UnityEngine;
  6. namespace Mirror
  7. {
  8. /// <summary>NetworkServer handles remote connections and has a local connection for a local client.</summary>
  9. public static class NetworkServer
  10. {
  11. static bool initialized;
  12. public static int maxConnections;
  13. /// <summary>Connection to host mode client (if any)</summary>
  14. public static NetworkConnectionToClient localConnection { get; private set; }
  15. /// <summary>True is a local client is currently active on the server</summary>
  16. public static bool localClientActive => localConnection != null;
  17. /// <summary>Dictionary of all server connections, with connectionId as key</summary>
  18. public static Dictionary<int, NetworkConnectionToClient> connections =
  19. new Dictionary<int, NetworkConnectionToClient>();
  20. /// <summary>Message Handlers dictionary, with mesageId as key</summary>
  21. internal static Dictionary<ushort, NetworkMessageDelegate> handlers =
  22. new Dictionary<ushort, NetworkMessageDelegate>();
  23. /// <summary>Single player mode can use dontListen to not accept incoming connections</summary>
  24. // see also: https://github.com/vis2k/Mirror/pull/2595
  25. public static bool dontListen;
  26. /// <summary>active checks if the server has been started</summary>
  27. public static bool active { get; internal set; }
  28. // scene loading
  29. public static bool isLoadingScene;
  30. // interest management component (optional)
  31. // by default, everyone observes everyone
  32. public static InterestManagement aoi;
  33. // Deprecated 2021-05-10
  34. [Obsolete("Transport is responsible for timeouts.")]
  35. public static bool disconnectInactiveConnections;
  36. // Deprecated 2021-05-10
  37. [Obsolete("Transport is responsible for timeouts. Configure the Transport's timeout setting instead.")]
  38. public static float disconnectInactiveTimeout = 60f;
  39. // OnConnected / OnDisconnected used to be NetworkMessages that were
  40. // invoked. this introduced a bug where external clients could send
  41. // Connected/Disconnected messages over the network causing undefined
  42. // behaviour.
  43. // => public so that custom NetworkManagers can hook into it
  44. public static Action<NetworkConnection> OnConnectedEvent;
  45. public static Action<NetworkConnection> OnDisconnectedEvent;
  46. public static Action<NetworkConnection, Exception> OnErrorEvent;
  47. // initialization / shutdown ///////////////////////////////////////////
  48. static void Initialize()
  49. {
  50. if (initialized)
  51. return;
  52. initialized = true;
  53. // Debug.Log("NetworkServer Created version " + Version.Current);
  54. //Make sure connections are cleared in case any old connections references exist from previous sessions
  55. connections.Clear();
  56. // reset NetworkTime
  57. NetworkTime.Reset();
  58. Debug.Assert(Transport.activeTransport != null, "There was no active transport when calling NetworkServer.Listen, If you are calling Listen manually then make sure to set 'Transport.activeTransport' first");
  59. AddTransportHandlers();
  60. }
  61. static void AddTransportHandlers()
  62. {
  63. Transport.activeTransport.OnServerConnected = OnTransportConnected;
  64. Transport.activeTransport.OnServerDataReceived = OnTransportData;
  65. Transport.activeTransport.OnServerDisconnected = OnTransportDisconnected;
  66. Transport.activeTransport.OnServerError = OnError;
  67. }
  68. // calls OnStartClient for all SERVER objects in host mode once.
  69. // client doesn't get spawn messages for those, so need to call manually.
  70. public static void ActivateHostScene()
  71. {
  72. foreach (NetworkIdentity identity in NetworkIdentity.spawned.Values)
  73. {
  74. if (!identity.isClient)
  75. {
  76. // Debug.Log("ActivateHostScene " + identity.netId + " " + identity);
  77. identity.OnStartClient();
  78. }
  79. }
  80. }
  81. internal static void RegisterMessageHandlers()
  82. {
  83. RegisterHandler<ReadyMessage>(OnClientReadyMessage);
  84. RegisterHandler<CommandMessage>(OnCommandMessage);
  85. RegisterHandler<NetworkPingMessage>(NetworkTime.OnServerPing, false);
  86. }
  87. /// <summary>Starts server and listens to incoming connections with max connections limit.</summary>
  88. public static void Listen(int maxConns)
  89. {
  90. Initialize();
  91. maxConnections = maxConns;
  92. // only start server if we want to listen
  93. if (!dontListen)
  94. {
  95. Transport.activeTransport.ServerStart();
  96. Debug.Log("Server started listening");
  97. }
  98. active = true;
  99. RegisterMessageHandlers();
  100. }
  101. // Note: NetworkClient.DestroyAllClientObjects does the same on client.
  102. static void CleanupNetworkIdentities()
  103. {
  104. // iterate a COPY of NetworkIdentity.spawned.
  105. // DestroyObject removes them from the original collection.
  106. // removing while iterating is not allowed.
  107. foreach (NetworkIdentity identity in NetworkIdentity.spawned.Values.ToList())
  108. {
  109. if (identity != null)
  110. {
  111. // scene object
  112. if (identity.sceneId != 0)
  113. {
  114. // spawned scene objects are unspawned and reset.
  115. // afterwards we disable them again.
  116. // (they always stay in the scene, we don't destroy them)
  117. DestroyObject(identity, DestroyMode.Reset);
  118. identity.gameObject.SetActive(false);
  119. }
  120. // spawned prefabs
  121. else
  122. {
  123. // spawned prefabs are unspawned and destroyed.
  124. DestroyObject(identity, DestroyMode.Destroy);
  125. }
  126. }
  127. }
  128. NetworkIdentity.spawned.Clear();
  129. }
  130. /// <summary>Shuts down the server and disconnects all clients</summary>
  131. public static void Shutdown()
  132. {
  133. if (initialized)
  134. {
  135. DisconnectAll();
  136. // stop the server.
  137. // we do NOT call Transport.Shutdown, because someone only
  138. // called NetworkServer.Shutdown. we can't assume that the
  139. // client is supposed to be shut down too!
  140. //
  141. // NOTE: stop no matter what, even if 'dontListen':
  142. // someone might enabled dontListen at runtime.
  143. // but we still need to stop the server.
  144. // fixes https://github.com/vis2k/Mirror/issues/2536
  145. Transport.activeTransport.ServerStop();
  146. initialized = false;
  147. }
  148. dontListen = false;
  149. active = false;
  150. handlers.Clear();
  151. CleanupNetworkIdentities();
  152. NetworkIdentity.ResetNextNetworkId();
  153. // clear events. someone might have hooked into them before, but
  154. // we don't want to use those hooks after Shutdown anymore.
  155. OnConnectedEvent = null;
  156. OnDisconnectedEvent = null;
  157. }
  158. // connections /////////////////////////////////////////////////////////
  159. /// <summary>Add a connection and setup callbacks. Returns true if not added yet.</summary>
  160. public static bool AddConnection(NetworkConnectionToClient conn)
  161. {
  162. if (!connections.ContainsKey(conn.connectionId))
  163. {
  164. // connection cannot be null here or conn.connectionId
  165. // would throw NRE
  166. connections[conn.connectionId] = conn;
  167. return true;
  168. }
  169. // already a connection with this id
  170. return false;
  171. }
  172. /// <summary>Removes a connection by connectionId. Returns true if removed.</summary>
  173. public static bool RemoveConnection(int connectionId)
  174. {
  175. return connections.Remove(connectionId);
  176. }
  177. // called by LocalClient to add itself. don't call directly.
  178. // TODO consider internal setter instead?
  179. internal static void SetLocalConnection(LocalConnectionToClient conn)
  180. {
  181. if (localConnection != null)
  182. {
  183. Debug.LogError("Local Connection already exists");
  184. return;
  185. }
  186. localConnection = conn;
  187. }
  188. // removes local connection to client
  189. internal static void RemoveLocalConnection()
  190. {
  191. if (localConnection != null)
  192. {
  193. localConnection.Disconnect();
  194. localConnection = null;
  195. }
  196. RemoveConnection(0);
  197. }
  198. /// <summary>True if we have no external connections (host is allowed)</summary>
  199. public static bool NoExternalConnections()
  200. {
  201. return connections.Count == 0 ||
  202. (connections.Count == 1 && localConnection != null);
  203. }
  204. // Deprecated 2021-03-07
  205. [Obsolete("NoConnections was renamed to NoExternalConnections because that's what it checks for.")]
  206. public static bool NoConnections() => NoExternalConnections();
  207. // send ////////////////////////////////////////////////////////////////
  208. /// <summary>Send a message to all clients, even those that haven't joined the world yet (non ready)</summary>
  209. public static void SendToAll<T>(T message, int channelId = Channels.Reliable, bool sendToReadyOnly = false)
  210. where T : struct, NetworkMessage
  211. {
  212. if (!active)
  213. {
  214. Debug.LogWarning("Can not send using NetworkServer.SendToAll<T>(T msg) because NetworkServer is not active");
  215. return;
  216. }
  217. // Debug.Log("Server.SendToAll id:" + typeof(T));
  218. using (PooledNetworkWriter writer = NetworkWriterPool.GetWriter())
  219. {
  220. // pack message only once
  221. MessagePacking.Pack(message, writer);
  222. ArraySegment<byte> segment = writer.ToArraySegment();
  223. // filter and then send to all internet connections at once
  224. // -> makes code more complicated, but is HIGHLY worth it to
  225. // avoid allocations, allow for multicast, etc.
  226. int count = 0;
  227. foreach (NetworkConnectionToClient conn in connections.Values)
  228. {
  229. if (sendToReadyOnly && !conn.isReady)
  230. continue;
  231. count++;
  232. conn.Send(segment, channelId);
  233. }
  234. NetworkDiagnostics.OnSend(message, channelId, segment.Count, count);
  235. }
  236. }
  237. /// <summary>Send a message to all clients which have joined the world (are ready).</summary>
  238. // TODO put rpcs into NetworkServer.Update WorldState packet, then finally remove SendToReady!
  239. public static void SendToReady<T>(T message, int channelId = Channels.Reliable)
  240. where T : struct, NetworkMessage
  241. {
  242. if (!active)
  243. {
  244. Debug.LogWarning("Can not send using NetworkServer.SendToReady<T>(T msg) because NetworkServer is not active");
  245. return;
  246. }
  247. SendToAll(message, channelId, true);
  248. }
  249. /// <summary>Send a message to only clients which are ready with option to include the owner of the object identity</summary>
  250. // TODO put rpcs into NetworkServer.Update WorldState packet, then finally remove SendToReady!
  251. public static void SendToReady<T>(NetworkIdentity identity, T message, bool includeOwner = true, int channelId = Channels.Reliable)
  252. where T : struct, NetworkMessage
  253. {
  254. // Debug.Log("Server.SendToReady msgType:" + typeof(T));
  255. if (identity == null || identity.observers == null || identity.observers.Count == 0)
  256. return;
  257. using (PooledNetworkWriter writer = NetworkWriterPool.GetWriter())
  258. {
  259. // pack message only once
  260. MessagePacking.Pack(message, writer);
  261. ArraySegment<byte> segment = writer.ToArraySegment();
  262. int count = 0;
  263. foreach (NetworkConnection conn in identity.observers.Values)
  264. {
  265. bool isOwner = conn == identity.connectionToClient;
  266. if ((!isOwner || includeOwner) && conn.isReady)
  267. {
  268. count++;
  269. conn.Send(segment, channelId);
  270. }
  271. }
  272. NetworkDiagnostics.OnSend(message, channelId, segment.Count, count);
  273. }
  274. }
  275. /// <summary>Send a message to only clients which are ready including the owner of the NetworkIdentity</summary>
  276. // TODO put rpcs into NetworkServer.Update WorldState packet, then finally remove SendToReady!
  277. public static void SendToReady<T>(NetworkIdentity identity, T message, int channelId)
  278. where T : struct, NetworkMessage
  279. {
  280. SendToReady(identity, message, true, channelId);
  281. }
  282. // this is like SendToReady - but it doesn't check the ready flag on the connection.
  283. // this is used for ObjectDestroy messages.
  284. static void SendToObservers<T>(NetworkIdentity identity, T message, int channelId = Channels.Reliable)
  285. where T : struct, NetworkMessage
  286. {
  287. // Debug.Log("Server.SendToObservers id:" + typeof(T));
  288. if (identity == null || identity.observers == null || identity.observers.Count == 0)
  289. return;
  290. using (PooledNetworkWriter writer = NetworkWriterPool.GetWriter())
  291. {
  292. // pack message into byte[] once
  293. MessagePacking.Pack(message, writer);
  294. ArraySegment<byte> segment = writer.ToArraySegment();
  295. foreach (NetworkConnection conn in identity.observers.Values)
  296. {
  297. conn.Send(segment, channelId);
  298. }
  299. NetworkDiagnostics.OnSend(message, channelId, segment.Count, identity.observers.Count);
  300. }
  301. }
  302. /// <summary>Send this message to the player only</summary>
  303. // Deprecated 2021-03-04
  304. [Obsolete("Use identity.connectionToClient.Send() instead! Previously Mirror needed this function internally, but not anymore.")]
  305. public static void SendToClientOfPlayer<T>(NetworkIdentity identity, T msg, int channelId = Channels.Reliable)
  306. where T : struct, NetworkMessage
  307. {
  308. if (identity != null)
  309. {
  310. identity.connectionToClient.Send(msg, channelId);
  311. }
  312. else
  313. {
  314. Debug.LogError("SendToClientOfPlayer: player has no NetworkIdentity: " + identity);
  315. }
  316. }
  317. // transport events ////////////////////////////////////////////////////
  318. // called by transport
  319. static void OnTransportConnected(int connectionId)
  320. {
  321. // Debug.Log("Server accepted client:" + connectionId);
  322. // connectionId needs to be != 0 because 0 is reserved for local player
  323. // note that some transports like kcp generate connectionId by
  324. // hashing which can be < 0 as well, so we need to allow < 0!
  325. if (connectionId == 0)
  326. {
  327. Debug.LogError("Server.HandleConnect: invalid connectionId: " + connectionId + " . Needs to be != 0, because 0 is reserved for local player.");
  328. Transport.activeTransport.ServerDisconnect(connectionId);
  329. return;
  330. }
  331. // connectionId not in use yet?
  332. if (connections.ContainsKey(connectionId))
  333. {
  334. Transport.activeTransport.ServerDisconnect(connectionId);
  335. // Debug.Log("Server connectionId " + connectionId + " already in use. kicked client:" + connectionId);
  336. return;
  337. }
  338. // are more connections allowed? if not, kick
  339. // (it's easier to handle this in Mirror, so Transports can have
  340. // less code and third party transport might not do that anyway)
  341. // (this way we could also send a custom 'tooFull' message later,
  342. // Transport can't do that)
  343. if (connections.Count < maxConnections)
  344. {
  345. // add connection
  346. NetworkConnectionToClient conn = new NetworkConnectionToClient(connectionId);
  347. OnConnected(conn);
  348. }
  349. else
  350. {
  351. // kick
  352. Transport.activeTransport.ServerDisconnect(connectionId);
  353. // Debug.Log("Server full, kicked client:" + connectionId);
  354. }
  355. }
  356. internal static void OnConnected(NetworkConnectionToClient conn)
  357. {
  358. // Debug.Log("Server accepted client:" + conn);
  359. // add connection and invoke connected event
  360. AddConnection(conn);
  361. OnConnectedEvent?.Invoke(conn);
  362. }
  363. static bool UnpackAndInvoke(NetworkConnectionToClient connection, NetworkReader reader, int channelId)
  364. {
  365. if (MessagePacking.Unpack(reader, out ushort msgType))
  366. {
  367. // try to invoke the handler for that message
  368. if (handlers.TryGetValue(msgType, out NetworkMessageDelegate handler))
  369. {
  370. handler.Invoke(connection, reader, channelId);
  371. connection.lastMessageTime = Time.time;
  372. return true;
  373. }
  374. else
  375. {
  376. // Debug.Log("Unknown message ID " + msgType + " " + this + ". May be due to no existing RegisterHandler for this message.");
  377. return false;
  378. }
  379. }
  380. else
  381. {
  382. Debug.LogError("Closed connection: " + connection + ". Invalid message header.");
  383. connection.Disconnect();
  384. return false;
  385. }
  386. }
  387. // called by transport
  388. internal static void OnTransportData(int connectionId, ArraySegment<byte> data, int channelId)
  389. {
  390. if (connections.TryGetValue(connectionId, out NetworkConnectionToClient connection))
  391. {
  392. // client might batch multiple messages into one packet.
  393. // feed it to the Unbatcher.
  394. // NOTE: we don't need to associate a channelId because we
  395. // always process all messages in the batch.
  396. if (!connection.unbatcher.AddBatch(data))
  397. {
  398. Debug.LogWarning($"NetworkServer: received Message was too short (messages should start with message id)");
  399. connection.Disconnect();
  400. return;
  401. }
  402. // process all messages in the batch.
  403. // only while NOT loading a scene.
  404. // if we get a scene change message, then we need to stop
  405. // processing. otherwise we might apply them to the old scene.
  406. // => fixes https://github.com/vis2k/Mirror/issues/2651
  407. //
  408. // NOTE: is scene starts loading, then the rest of the batch
  409. // would only be processed when OnTransportData is called
  410. // the next time.
  411. // => consider moving processing to NetworkEarlyUpdate.
  412. while (!isLoadingScene &&
  413. connection.unbatcher.GetNextMessage(out NetworkReader reader, out double remoteTimestamp))
  414. {
  415. // enough to read at least header size?
  416. if (reader.Remaining >= MessagePacking.HeaderSize)
  417. {
  418. // make remoteTimeStamp available to the user
  419. connection.remoteTimeStamp = remoteTimestamp;
  420. // handle message
  421. if (!UnpackAndInvoke(connection, reader, channelId))
  422. break;
  423. }
  424. // otherwise disconnect
  425. else
  426. {
  427. Debug.LogError($"NetworkServer: received Message was too short (messages should start with message id). Disconnecting {connectionId}");
  428. connection.Disconnect();
  429. return;
  430. }
  431. }
  432. }
  433. else Debug.LogError("HandleData Unknown connectionId:" + connectionId);
  434. }
  435. // called by transport
  436. // IMPORTANT: often times when disconnecting, we call this from Mirror
  437. // too because we want to remove the connection and handle
  438. // the disconnect immediately.
  439. // => which is fine as long as we guarantee it only runs once
  440. // => which we do by removing the connection!
  441. internal static void OnTransportDisconnected(int connectionId)
  442. {
  443. // Debug.Log("Server disconnect client:" + connectionId);
  444. if (connections.TryGetValue(connectionId, out NetworkConnectionToClient conn))
  445. {
  446. RemoveConnection(connectionId);
  447. // Debug.Log("Server lost client:" + connectionId);
  448. // NetworkManager hooks into OnDisconnectedEvent to make
  449. // DestroyPlayerForConnection(conn) optional, e.g. for PvP MMOs
  450. // where players shouldn't be able to escape combat instantly.
  451. if (OnDisconnectedEvent != null)
  452. {
  453. OnDisconnectedEvent.Invoke(conn);
  454. }
  455. // if nobody hooked into it, then simply call DestroyPlayerForConnection
  456. else
  457. {
  458. DestroyPlayerForConnection(conn);
  459. }
  460. }
  461. }
  462. static void OnError(int connectionId, Exception exception)
  463. {
  464. Debug.LogException(exception);
  465. // try get connection. passes null otherwise.
  466. connections.TryGetValue(connectionId, out NetworkConnectionToClient conn);
  467. OnErrorEvent?.Invoke(conn, exception);
  468. }
  469. // message handlers ////////////////////////////////////////////////////
  470. /// <summary>Register a handler for message type T. Most should require authentication.</summary>
  471. public static void RegisterHandler<T>(Action<NetworkConnection, T> handler, bool requireAuthentication = true)
  472. where T : struct, NetworkMessage
  473. {
  474. ushort msgType = MessagePacking.GetId<T>();
  475. if (handlers.ContainsKey(msgType))
  476. {
  477. Debug.LogWarning($"NetworkServer.RegisterHandler replacing handler for {typeof(T).FullName}, id={msgType}. If replacement is intentional, use ReplaceHandler instead to avoid this warning.");
  478. }
  479. handlers[msgType] = MessagePacking.WrapHandler(handler, requireAuthentication);
  480. }
  481. /// <summary>Register a handler for message type T. Most should require authentication.</summary>
  482. // Deprecated 2021-02-24
  483. [Obsolete("Use RegisterHandler(Action<NetworkConnection, T), requireAuthentication instead.")]
  484. public static void RegisterHandler<T>(Action<T> handler, bool requireAuthentication = true)
  485. where T : struct, NetworkMessage
  486. {
  487. RegisterHandler<T>((_, value) => { handler(value); }, requireAuthentication);
  488. }
  489. /// <summary>Replace a handler for message type T. Most should require authentication.</summary>
  490. public static void ReplaceHandler<T>(Action<NetworkConnection, T> handler, bool requireAuthentication = true)
  491. where T : struct, NetworkMessage
  492. {
  493. ushort msgType = MessagePacking.GetId<T>();
  494. handlers[msgType] = MessagePacking.WrapHandler(handler, requireAuthentication);
  495. }
  496. /// <summary>Replace a handler for message type T. Most should require authentication.</summary>
  497. public static void ReplaceHandler<T>(Action<T> handler, bool requireAuthentication = true)
  498. where T : struct, NetworkMessage
  499. {
  500. ReplaceHandler<T>((_, value) => { handler(value); }, requireAuthentication);
  501. }
  502. /// <summary>Unregister a handler for a message type T.</summary>
  503. public static void UnregisterHandler<T>()
  504. where T : struct, NetworkMessage
  505. {
  506. ushort msgType = MessagePacking.GetId<T>();
  507. handlers.Remove(msgType);
  508. }
  509. /// <summary>Clears all registered message handlers.</summary>
  510. public static void ClearHandlers() => handlers.Clear();
  511. internal static bool GetNetworkIdentity(GameObject go, out NetworkIdentity identity)
  512. {
  513. identity = go.GetComponent<NetworkIdentity>();
  514. if (identity == null)
  515. {
  516. Debug.LogError($"GameObject {go.name} doesn't have NetworkIdentity.");
  517. return false;
  518. }
  519. return true;
  520. }
  521. // disconnect //////////////////////////////////////////////////////////
  522. /// <summary>Disconnect all connections, including the local connection.</summary>
  523. // synchronous: handles disconnect events and cleans up fully before returning!
  524. public static void DisconnectAll()
  525. {
  526. // disconnect and remove all connections.
  527. // we can not use foreach here because if
  528. // conn.Disconnect -> Transport.ServerDisconnect calls
  529. // OnDisconnect -> NetworkServer.OnDisconnect(connectionId)
  530. // immediately then OnDisconnect would remove the connection while
  531. // we are iterating here.
  532. // see also: https://github.com/vis2k/Mirror/issues/2357
  533. // this whole process should be simplified some day.
  534. // until then, let's copy .Values to avoid InvalidOperatinException.
  535. // note that this is only called when stopping the server, so the
  536. // copy is no performance problem.
  537. foreach (NetworkConnectionToClient conn in connections.Values.ToList())
  538. {
  539. // disconnect via connection->transport
  540. conn.Disconnect();
  541. // we want this function to be synchronous: handle disconnect
  542. // events and clean up fully before returning.
  543. // -> OnTransportDisconnected can safely be called without
  544. // waiting for the Transport's callback.
  545. // -> it has checks to only run once.
  546. // call OnDisconnected unless local player in host mod
  547. // TODO unnecessary check?
  548. if (conn.connectionId != NetworkConnection.LocalConnectionId)
  549. OnTransportDisconnected(conn.connectionId);
  550. }
  551. // cleanup
  552. connections.Clear();
  553. localConnection = null;
  554. active = false;
  555. }
  556. // Deprecated 2021-05-11
  557. [Obsolete("Call NetworkClient.DisconnectAll() instead")]
  558. public static void DisconnectAllExternalConnections() => DisconnectAll();
  559. // Deprecated 2021-05-11
  560. [Obsolete("Call NetworkClient.DisconnectAll() instead")]
  561. public static void DisconnectAllConnections() => DisconnectAll();
  562. // add/remove/replace player ///////////////////////////////////////////
  563. /// <summary>Called by server after AddPlayer message to add the player for the connection.</summary>
  564. // When a player is added for a connection, the client for that
  565. // connection is made ready automatically. The player object is
  566. // automatically spawned, so you do not need to call NetworkServer.Spawn
  567. // for that object. This function is used for "adding" a player, not for
  568. // "replacing" the player on a connection. If there is already a player
  569. // on this playerControllerId for this connection, this will fail.
  570. public static bool AddPlayerForConnection(NetworkConnection conn, GameObject player)
  571. {
  572. NetworkIdentity identity = player.GetComponent<NetworkIdentity>();
  573. if (identity == null)
  574. {
  575. Debug.LogWarning("AddPlayer: playerGameObject has no NetworkIdentity. Please add a NetworkIdentity to " + player);
  576. return false;
  577. }
  578. // cannot have a player object in "Add" version
  579. if (conn.identity != null)
  580. {
  581. Debug.Log("AddPlayer: player object already exists");
  582. return false;
  583. }
  584. // make sure we have a controller before we call SetClientReady
  585. // because the observers will be rebuilt only if we have a controller
  586. conn.identity = identity;
  587. // Set the connection on the NetworkIdentity on the server, NetworkIdentity.SetLocalPlayer is not called on the server (it is on clients)
  588. identity.SetClientOwner(conn);
  589. // special case, we are in host mode, set hasAuthority to true so that all overrides see it
  590. if (conn is LocalConnectionToClient)
  591. {
  592. identity.hasAuthority = true;
  593. NetworkClient.InternalAddPlayer(identity);
  594. }
  595. // set ready if not set yet
  596. SetClientReady(conn);
  597. // Debug.Log("Adding new playerGameObject object netId: " + identity.netId + " asset ID " + identity.assetId);
  598. Respawn(identity);
  599. return true;
  600. }
  601. /// <summary>Called by server after AddPlayer message to add the player for the connection.</summary>
  602. // When a player is added for a connection, the client for that
  603. // connection is made ready automatically. The player object is
  604. // automatically spawned, so you do not need to call NetworkServer.Spawn
  605. // for that object. This function is used for "adding" a player, not for
  606. // "replacing" the player on a connection. If there is already a player
  607. // on this playerControllerId for this connection, this will fail.
  608. public static bool AddPlayerForConnection(NetworkConnection conn, GameObject player, Guid assetId)
  609. {
  610. if (GetNetworkIdentity(player, out NetworkIdentity identity))
  611. {
  612. identity.assetId = assetId;
  613. }
  614. return AddPlayerForConnection(conn, player);
  615. }
  616. /// <summary>Replaces connection's player object. The old object is not destroyed.</summary>
  617. // This does NOT change the ready state of the connection, so it can
  618. // safely be used while changing scenes.
  619. public static bool ReplacePlayerForConnection(NetworkConnection conn, GameObject player, bool keepAuthority = false)
  620. {
  621. NetworkIdentity identity = player.GetComponent<NetworkIdentity>();
  622. if (identity == null)
  623. {
  624. Debug.LogError("ReplacePlayer: playerGameObject has no NetworkIdentity. Please add a NetworkIdentity to " + player);
  625. return false;
  626. }
  627. if (identity.connectionToClient != null && identity.connectionToClient != conn)
  628. {
  629. Debug.LogError("Cannot replace player for connection. New player is already owned by a different connection" + player);
  630. return false;
  631. }
  632. //NOTE: there can be an existing player
  633. //Debug.Log("NetworkServer ReplacePlayer");
  634. NetworkIdentity previousPlayer = conn.identity;
  635. conn.identity = identity;
  636. // Set the connection on the NetworkIdentity on the server, NetworkIdentity.SetLocalPlayer is not called on the server (it is on clients)
  637. identity.SetClientOwner(conn);
  638. // special case, we are in host mode, set hasAuthority to true so that all overrides see it
  639. if (conn is LocalConnectionToClient)
  640. {
  641. identity.hasAuthority = true;
  642. NetworkClient.InternalAddPlayer(identity);
  643. }
  644. // add connection to observers AFTER the playerController was set.
  645. // by definition, there is nothing to observe if there is no player
  646. // controller.
  647. //
  648. // IMPORTANT: do this in AddPlayerForConnection & ReplacePlayerForConnection!
  649. SpawnObserversForConnection(conn);
  650. // Debug.Log("Replacing playerGameObject object netId: " + player.GetComponent<NetworkIdentity>().netId + " asset ID " + player.GetComponent<NetworkIdentity>().assetId);
  651. Respawn(identity);
  652. if (!keepAuthority)
  653. previousPlayer.RemoveClientAuthority();
  654. return true;
  655. }
  656. /// <summary>Replaces connection's player object. The old object is not destroyed.</summary>
  657. // This does NOT change the ready state of the connection, so it can
  658. // safely be used while changing scenes.
  659. public static bool ReplacePlayerForConnection(NetworkConnection conn, GameObject player, Guid assetId, bool keepAuthority = false)
  660. {
  661. if (GetNetworkIdentity(player, out NetworkIdentity identity))
  662. {
  663. identity.assetId = assetId;
  664. }
  665. return ReplacePlayerForConnection(conn, player, keepAuthority);
  666. }
  667. // ready ///////////////////////////////////////////////////////////////
  668. /// <summary>Flags client connection as ready (=joined world).</summary>
  669. // When a client has signaled that it is ready, this method tells the
  670. // server that the client is ready to receive spawned objects and state
  671. // synchronization updates. This is usually called in a handler for the
  672. // SYSTEM_READY message. If there is not specific action a game needs to
  673. // take for this message, relying on the default ready handler function
  674. // is probably fine, so this call wont be needed.
  675. public static void SetClientReady(NetworkConnection conn)
  676. {
  677. // Debug.Log("SetClientReadyInternal for conn:" + conn);
  678. // set ready
  679. conn.isReady = true;
  680. // client is ready to start spawning objects
  681. if (conn.identity != null)
  682. SpawnObserversForConnection(conn);
  683. }
  684. /// <summary>Marks the client of the connection to be not-ready.</summary>
  685. // Clients that are not ready do not receive spawned objects or state
  686. // synchronization updates. They client can be made ready again by
  687. // calling SetClientReady().
  688. public static void SetClientNotReady(NetworkConnection conn)
  689. {
  690. if (conn.isReady)
  691. {
  692. // Debug.Log("PlayerNotReady " + conn);
  693. conn.isReady = false;
  694. conn.RemoveFromObservingsObservers();
  695. conn.Send(new NotReadyMessage());
  696. }
  697. }
  698. /// <summary>Marks all connected clients as no longer ready.</summary>
  699. // All clients will no longer be sent state synchronization updates. The
  700. // player's clients can call ClientManager.Ready() again to re-enter the
  701. // ready state. This is useful when switching scenes.
  702. public static void SetAllClientsNotReady()
  703. {
  704. foreach (NetworkConnectionToClient conn in connections.Values)
  705. {
  706. SetClientNotReady(conn);
  707. }
  708. }
  709. // default ready handler.
  710. static void OnClientReadyMessage(NetworkConnection conn, ReadyMessage msg)
  711. {
  712. // Debug.Log("Default handler for ready message from " + conn);
  713. SetClientReady(conn);
  714. }
  715. // show / hide for connection //////////////////////////////////////////
  716. internal static void ShowForConnection(NetworkIdentity identity, NetworkConnection conn)
  717. {
  718. if (conn.isReady)
  719. SendSpawnMessage(identity, conn);
  720. }
  721. internal static void HideForConnection(NetworkIdentity identity, NetworkConnection conn)
  722. {
  723. ObjectHideMessage msg = new ObjectHideMessage
  724. {
  725. netId = identity.netId
  726. };
  727. conn.Send(msg);
  728. }
  729. /// <summary>Removes the player object from the connection</summary>
  730. // destroyServerObject: Indicates whether the server object should be destroyed
  731. public static void RemovePlayerForConnection(NetworkConnection conn, bool destroyServerObject)
  732. {
  733. if (conn.identity != null)
  734. {
  735. if (destroyServerObject)
  736. Destroy(conn.identity.gameObject);
  737. else
  738. UnSpawn(conn.identity.gameObject);
  739. conn.identity = null;
  740. }
  741. //else Debug.Log($"Connection {conn} has no identity");
  742. }
  743. // remote calls ////////////////////////////////////////////////////////
  744. // Handle command from specific player, this could be one of multiple
  745. // players on a single client
  746. static void OnCommandMessage(NetworkConnection conn, CommandMessage msg)
  747. {
  748. if (!NetworkIdentity.spawned.TryGetValue(msg.netId, out NetworkIdentity identity))
  749. {
  750. Debug.LogWarning("Spawned object not found when handling Command message [netId=" + msg.netId + "]");
  751. return;
  752. }
  753. CommandInfo commandInfo = identity.GetCommandInfo(msg.componentIndex, msg.functionHash);
  754. // Commands can be for player objects, OR other objects with client-authority
  755. // -> so if this connection's controller has a different netId then
  756. // only allow the command if clientAuthorityOwner
  757. bool requiresAuthority = commandInfo.requiresAuthority;
  758. if (requiresAuthority && identity.connectionToClient != conn)
  759. {
  760. Debug.LogWarning("Command for object without authority [netId=" + msg.netId + "]");
  761. return;
  762. }
  763. // Debug.Log("OnCommandMessage for netId=" + msg.netId + " conn=" + conn);
  764. using (PooledNetworkReader networkReader = NetworkReaderPool.GetReader(msg.payload))
  765. identity.HandleRemoteCall(msg.componentIndex, msg.functionHash, MirrorInvokeType.Command, networkReader, conn as NetworkConnectionToClient);
  766. }
  767. // spawning ////////////////////////////////////////////////////////////
  768. static ArraySegment<byte> CreateSpawnMessagePayload(bool isOwner, NetworkIdentity identity, PooledNetworkWriter ownerWriter, PooledNetworkWriter observersWriter)
  769. {
  770. // Only call OnSerializeAllSafely if there are NetworkBehaviours
  771. if (identity.NetworkBehaviours.Length == 0)
  772. {
  773. return default;
  774. }
  775. // serialize all components with initialState = true
  776. // (can be null if has none)
  777. identity.OnSerializeAllSafely(true, ownerWriter, observersWriter);
  778. // convert to ArraySegment to avoid reader allocations
  779. // if nothing was written, .ToArraySegment returns an empty segment.
  780. ArraySegment<byte> ownerSegment = ownerWriter.ToArraySegment();
  781. ArraySegment<byte> observersSegment = observersWriter.ToArraySegment();
  782. // use owner segment if 'conn' owns this identity, otherwise
  783. // use observers segment
  784. ArraySegment<byte> payload = isOwner ? ownerSegment : observersSegment;
  785. return payload;
  786. }
  787. internal static void SendSpawnMessage(NetworkIdentity identity, NetworkConnection conn)
  788. {
  789. if (identity.serverOnly) return;
  790. // Debug.Log("Server SendSpawnMessage: name=" + identity.name + " sceneId=" + identity.sceneId.ToString("X") + " netid=" + identity.netId);
  791. // one writer for owner, one for observers
  792. using (PooledNetworkWriter ownerWriter = NetworkWriterPool.GetWriter(), observersWriter = NetworkWriterPool.GetWriter())
  793. {
  794. bool isOwner = identity.connectionToClient == conn;
  795. ArraySegment<byte> payload = CreateSpawnMessagePayload(isOwner, identity, ownerWriter, observersWriter);
  796. SpawnMessage message = new SpawnMessage
  797. {
  798. netId = identity.netId,
  799. isLocalPlayer = conn.identity == identity,
  800. isOwner = isOwner,
  801. sceneId = identity.sceneId,
  802. assetId = identity.assetId,
  803. // use local values for VR support
  804. position = identity.transform.localPosition,
  805. rotation = identity.transform.localRotation,
  806. scale = identity.transform.localScale,
  807. payload = payload
  808. };
  809. conn.Send(message);
  810. }
  811. }
  812. static void SpawnObject(GameObject obj, NetworkConnection ownerConnection)
  813. {
  814. // verify if we an spawn this
  815. if (Utils.IsPrefab(obj))
  816. {
  817. Debug.LogError($"GameObject {obj.name} is a prefab, it can't be spawned. Instantiate it first.");
  818. return;
  819. }
  820. if (!active)
  821. {
  822. Debug.LogError("SpawnObject for " + obj + ", NetworkServer is not active. Cannot spawn objects without an active server.");
  823. return;
  824. }
  825. NetworkIdentity identity = obj.GetComponent<NetworkIdentity>();
  826. if (identity == null)
  827. {
  828. Debug.LogError("SpawnObject " + obj + " has no NetworkIdentity. Please add a NetworkIdentity to " + obj);
  829. return;
  830. }
  831. if (identity.SpawnedFromInstantiate)
  832. {
  833. // Using Instantiate on SceneObject is not allowed, so stop spawning here
  834. // NetworkIdentity.Awake already logs error, no need to log a second error here
  835. return;
  836. }
  837. identity.connectionToClient = (NetworkConnectionToClient)ownerConnection;
  838. // special case to make sure hasAuthority is set
  839. // on start server in host mode
  840. if (ownerConnection is LocalConnectionToClient)
  841. identity.hasAuthority = true;
  842. identity.OnStartServer();
  843. // Debug.Log("SpawnObject instance ID " + identity.netId + " asset ID " + identity.assetId);
  844. if (aoi)
  845. {
  846. // This calls user code which might throw exceptions
  847. // We don't want this to leave us in bad state
  848. try
  849. {
  850. aoi.OnSpawned(identity);
  851. }
  852. catch (Exception e)
  853. {
  854. Debug.LogException(e);
  855. }
  856. }
  857. RebuildObservers(identity, true);
  858. }
  859. /// <summary>Spawn the given game object on all clients which are ready.</summary>
  860. // This will cause a new object to be instantiated from the registered
  861. // prefab, or from a custom spawn function.
  862. public static void Spawn(GameObject obj, NetworkConnection ownerConnection = null)
  863. {
  864. SpawnObject(obj, ownerConnection);
  865. }
  866. /// <summary>Spawns an object and also assigns Client Authority to the specified client.</summary>
  867. // This is the same as calling NetworkIdentity.AssignClientAuthority on the spawned object.
  868. public static void Spawn(GameObject obj, GameObject ownerPlayer)
  869. {
  870. NetworkIdentity identity = ownerPlayer.GetComponent<NetworkIdentity>();
  871. if (identity == null)
  872. {
  873. Debug.LogError("Player object has no NetworkIdentity");
  874. return;
  875. }
  876. if (identity.connectionToClient == null)
  877. {
  878. Debug.LogError("Player object is not a player.");
  879. return;
  880. }
  881. Spawn(obj, identity.connectionToClient);
  882. }
  883. /// <summary>Spawns an object and also assigns Client Authority to the specified client.</summary>
  884. // This is the same as calling NetworkIdentity.AssignClientAuthority on the spawned object.
  885. public static void Spawn(GameObject obj, Guid assetId, NetworkConnection ownerConnection = null)
  886. {
  887. if (GetNetworkIdentity(obj, out NetworkIdentity identity))
  888. {
  889. identity.assetId = assetId;
  890. }
  891. SpawnObject(obj, ownerConnection);
  892. }
  893. internal static bool ValidateSceneObject(NetworkIdentity identity)
  894. {
  895. if (identity.gameObject.hideFlags == HideFlags.NotEditable ||
  896. identity.gameObject.hideFlags == HideFlags.HideAndDontSave)
  897. return false;
  898. #if UNITY_EDITOR
  899. if (UnityEditor.EditorUtility.IsPersistent(identity.gameObject))
  900. return false;
  901. #endif
  902. // If not a scene object
  903. return identity.sceneId != 0;
  904. }
  905. /// <summary>Spawns NetworkIdentities in the scene on the server.</summary>
  906. // NetworkIdentity objects in a scene are disabled by default. Calling
  907. // SpawnObjects() causes these scene objects to be enabled and spawned.
  908. // It is like calling NetworkServer.Spawn() for each of them.
  909. public static bool SpawnObjects()
  910. {
  911. // only if server active
  912. if (!active)
  913. return false;
  914. NetworkIdentity[] identities = Resources.FindObjectsOfTypeAll<NetworkIdentity>();
  915. // first pass: activate all scene objects
  916. foreach (NetworkIdentity identity in identities)
  917. {
  918. if (ValidateSceneObject(identity))
  919. {
  920. // Debug.Log("SpawnObjects sceneId:" + identity.sceneId.ToString("X") + " name:" + identity.gameObject.name);
  921. identity.gameObject.SetActive(true);
  922. // fix https://github.com/vis2k/Mirror/issues/2778:
  923. // -> SetActive(true) does NOT call Awake() if the parent
  924. // is inactive
  925. // -> we need Awake() to initialize NetworkBehaviours[] etc.
  926. // because our second pass below spawns and works with it
  927. // => detect this situation and manually call Awake for
  928. // proper initialization
  929. if (!identity.gameObject.activeInHierarchy)
  930. identity.Awake();
  931. }
  932. }
  933. // second pass: spawn all scene objects
  934. foreach (NetworkIdentity identity in identities)
  935. {
  936. if (ValidateSceneObject(identity))
  937. Spawn(identity.gameObject);
  938. }
  939. return true;
  940. }
  941. static void Respawn(NetworkIdentity identity)
  942. {
  943. if (identity.netId == 0)
  944. {
  945. // If the object has not been spawned, then do a full spawn and update observers
  946. Spawn(identity.gameObject, identity.connectionToClient);
  947. }
  948. else
  949. {
  950. // otherwise just replace his data
  951. SendSpawnMessage(identity, identity.connectionToClient);
  952. }
  953. }
  954. static void SpawnObserversForConnection(NetworkConnection conn)
  955. {
  956. // Debug.Log("Spawning " + NetworkIdentity.spawned.Count + " objects for conn " + conn);
  957. if (!conn.isReady)
  958. {
  959. // client needs to finish initializing before we can spawn objects
  960. // otherwise it would not find them.
  961. return;
  962. }
  963. // let connection know that we are about to start spawning...
  964. conn.Send(new ObjectSpawnStartedMessage());
  965. // add connection to each nearby NetworkIdentity's observers, which
  966. // internally sends a spawn message for each one to the connection.
  967. foreach (NetworkIdentity identity in NetworkIdentity.spawned.Values)
  968. {
  969. // try with far away ones in ummorpg!
  970. if (identity.gameObject.activeSelf) //TODO this is different
  971. {
  972. // Debug.Log("Sending spawn message for current server objects name='" + identity.name + "' netId=" + identity.netId + " sceneId=" + identity.sceneId.ToString("X"));
  973. // we need to support three cases:
  974. // - legacy system (identity has .visibility)
  975. // - new system (networkserver has .aoi)
  976. // - default case: no .visibility and no .aoi means add all
  977. // connections by default)
  978. //
  979. // ForceHidden/ForceShown overwrite all systems so check it
  980. // first!
  981. // ForceShown: add no matter what
  982. if (identity.visible == Visibility.ForceShown)
  983. {
  984. identity.AddObserver(conn);
  985. }
  986. // ForceHidden: don't show no matter what
  987. else if (identity.visible == Visibility.ForceHidden)
  988. {
  989. // do nothing
  990. }
  991. // default: legacy system / new system / no system support
  992. else if (identity.visible == Visibility.Default)
  993. {
  994. // legacy system
  995. #pragma warning disable 618
  996. if (identity.visibility != null)
  997. {
  998. // call OnCheckObserver
  999. if (identity.visibility.OnCheckObserver(conn))
  1000. identity.AddObserver(conn);
  1001. }
  1002. #pragma warning restore 618
  1003. // new system
  1004. else if (aoi != null)
  1005. {
  1006. // call OnCheckObserver
  1007. if (aoi.OnCheckObserver(identity, conn))
  1008. identity.AddObserver(conn);
  1009. }
  1010. // no system: add all observers by default
  1011. else
  1012. {
  1013. identity.AddObserver(conn);
  1014. }
  1015. }
  1016. }
  1017. }
  1018. // let connection know that we finished spawning, so it can call
  1019. // OnStartClient on each one (only after all were spawned, which
  1020. // is how Unity's Start() function works too)
  1021. conn.Send(new ObjectSpawnFinishedMessage());
  1022. }
  1023. /// <summary>This takes an object that has been spawned and un-spawns it.</summary>
  1024. // The object will be removed from clients that it was spawned on, or
  1025. // the custom spawn handler function on the client will be called for
  1026. // the object.
  1027. // Unlike when calling NetworkServer.Destroy(), on the server the object
  1028. // will NOT be destroyed. This allows the server to re-use the object,
  1029. // even spawn it again later.
  1030. public static void UnSpawn(GameObject obj) => DestroyObject(obj, DestroyMode.Reset);
  1031. // destroy /////////////////////////////////////////////////////////////
  1032. /// <summary>Destroys all of the connection's owned objects on the server.</summary>
  1033. // This is used when a client disconnects, to remove the players for
  1034. // that client. This also destroys non-player objects that have client
  1035. // authority set for this connection.
  1036. public static void DestroyPlayerForConnection(NetworkConnection conn)
  1037. {
  1038. // destroy all objects owned by this connection, including the player object
  1039. conn.DestroyOwnedObjects();
  1040. // remove connection from all of its observing entities observers
  1041. // fixes https://github.com/vis2k/Mirror/issues/2737
  1042. // -> cleaning those up in NetworkConnection.Disconnect is NOT enough
  1043. // because voluntary disconnects from the other end don't call
  1044. // NetworkConnectionn.Disconnect()
  1045. conn.RemoveFromObservingsObservers();
  1046. conn.identity = null;
  1047. }
  1048. // sometimes we want to GameObject.Destroy it.
  1049. // sometimes we want to just unspawn on clients and .Reset() it on server.
  1050. // => 'bool destroy' isn't obvious enough. it's really destroy OR reset!
  1051. enum DestroyMode { Destroy, Reset }
  1052. static void DestroyObject(NetworkIdentity identity, DestroyMode mode)
  1053. {
  1054. if (aoi)
  1055. {
  1056. // This calls user code which might throw exceptions
  1057. // We don't want this to leave us in bad state
  1058. try
  1059. {
  1060. aoi.OnDestroyed(identity);
  1061. }
  1062. catch (Exception e)
  1063. {
  1064. Debug.LogException(e);
  1065. }
  1066. }
  1067. // Debug.Log("DestroyObject instance:" + identity.netId);
  1068. NetworkIdentity.spawned.Remove(identity.netId);
  1069. identity.connectionToClient?.RemoveOwnedObject(identity);
  1070. // send object destroy message to all observers, clear observers
  1071. SendToObservers(identity, new ObjectDestroyMessage{netId = identity.netId});
  1072. identity.ClearObservers();
  1073. // in host mode, call OnStopClient manually
  1074. if (NetworkClient.active && localClientActive)
  1075. {
  1076. identity.OnStopClient();
  1077. // The object may have been spawned with host client ownership,
  1078. // e.g. a pet so we need to clear hasAuthority and call
  1079. // NotifyAuthority which invokes OnStopAuthority if hasAuthority.
  1080. identity.hasAuthority = false;
  1081. identity.NotifyAuthority();
  1082. }
  1083. // we are on the server. call OnStopServer.
  1084. identity.OnStopServer();
  1085. // are we supposed to GameObject.Destroy() it completely?
  1086. if (mode == DestroyMode.Destroy)
  1087. {
  1088. identity.destroyCalled = true;
  1089. UnityEngine.Object.Destroy(identity.gameObject);
  1090. }
  1091. // otherwise simply .Reset() and set inactive again
  1092. else if (mode == DestroyMode.Reset)
  1093. {
  1094. identity.Reset();
  1095. }
  1096. }
  1097. static void DestroyObject(GameObject obj, DestroyMode mode)
  1098. {
  1099. if (obj == null)
  1100. {
  1101. Debug.Log("NetworkServer DestroyObject is null");
  1102. return;
  1103. }
  1104. if (GetNetworkIdentity(obj, out NetworkIdentity identity))
  1105. {
  1106. DestroyObject(identity, mode);
  1107. }
  1108. }
  1109. /// <summary>Destroys this object and corresponding objects on all clients.</summary>
  1110. // In some cases it is useful to remove an object but not delete it on
  1111. // the server. For that, use NetworkServer.UnSpawn() instead of
  1112. // NetworkServer.Destroy().
  1113. public static void Destroy(GameObject obj) => DestroyObject(obj, DestroyMode.Destroy);
  1114. // interest management /////////////////////////////////////////////////
  1115. // Helper function to add all server connections as observers.
  1116. // This is used if none of the components provides their own
  1117. // OnRebuildObservers function.
  1118. internal static void AddAllReadyServerConnectionsToObservers(NetworkIdentity identity)
  1119. {
  1120. // add all server connections
  1121. foreach (NetworkConnectionToClient conn in connections.Values)
  1122. {
  1123. // only if authenticated (don't send to people during logins)
  1124. if (conn.isReady)
  1125. identity.AddObserver(conn);
  1126. }
  1127. // add local host connection (if any)
  1128. if (localConnection != null && localConnection.isReady)
  1129. {
  1130. identity.AddObserver(localConnection);
  1131. }
  1132. }
  1133. // allocate newObservers helper HashSet only once
  1134. static readonly HashSet<NetworkConnection> newObservers = new HashSet<NetworkConnection>();
  1135. // rebuild observers default method (no AOI) - adds all connections
  1136. static void RebuildObserversDefault(NetworkIdentity identity, bool initialize)
  1137. {
  1138. // only add all connections when rebuilding the first time.
  1139. // second time we just keep them without rebuilding anything.
  1140. if (initialize)
  1141. {
  1142. // not force hidden?
  1143. if (identity.visible != Visibility.ForceHidden)
  1144. {
  1145. AddAllReadyServerConnectionsToObservers(identity);
  1146. }
  1147. }
  1148. }
  1149. // rebuild observers via interest management system
  1150. static void RebuildObserversCustom(NetworkIdentity identity, bool initialize)
  1151. {
  1152. // clear newObservers hashset before using it
  1153. newObservers.Clear();
  1154. // not force hidden?
  1155. if (identity.visible != Visibility.ForceHidden)
  1156. {
  1157. // obsolete legacy system support (for now)
  1158. #pragma warning disable 618
  1159. if (identity.visibility != null)
  1160. identity.visibility.OnRebuildObservers(newObservers, initialize);
  1161. #pragma warning restore 618
  1162. else
  1163. aoi.OnRebuildObservers(identity, newObservers, initialize);
  1164. }
  1165. // IMPORTANT: AFTER rebuilding add own player connection in any case
  1166. // to ensure player always sees himself no matter what.
  1167. // -> OnRebuildObservers might clear observers, so we need to add
  1168. // the player's own connection AFTER. 100% fail safe.
  1169. // -> fixes https://github.com/vis2k/Mirror/issues/692 where a
  1170. // player might teleport out of the ProximityChecker's cast,
  1171. // losing the own connection as observer.
  1172. if (identity.connectionToClient != null)
  1173. {
  1174. newObservers.Add(identity.connectionToClient);
  1175. }
  1176. bool changed = false;
  1177. // add all newObservers that aren't in .observers yet
  1178. foreach (NetworkConnection conn in newObservers)
  1179. {
  1180. // only add ready connections.
  1181. // otherwise the player might not be in the world yet or anymore
  1182. if (conn != null && conn.isReady)
  1183. {
  1184. if (initialize || !identity.observers.ContainsKey(conn.connectionId))
  1185. {
  1186. // new observer
  1187. conn.AddToObserving(identity);
  1188. // Debug.Log("New Observer for " + gameObject + " " + conn);
  1189. changed = true;
  1190. }
  1191. }
  1192. }
  1193. // remove all old .observers that aren't in newObservers anymore
  1194. foreach (NetworkConnection conn in identity.observers.Values)
  1195. {
  1196. if (!newObservers.Contains(conn))
  1197. {
  1198. // removed observer
  1199. conn.RemoveFromObserving(identity, false);
  1200. // Debug.Log("Removed Observer for " + gameObject + " " + conn);
  1201. changed = true;
  1202. }
  1203. }
  1204. // copy new observers to observers
  1205. if (changed)
  1206. {
  1207. identity.observers.Clear();
  1208. foreach (NetworkConnection conn in newObservers)
  1209. {
  1210. if (conn != null && conn.isReady)
  1211. identity.observers.Add(conn.connectionId, conn);
  1212. }
  1213. }
  1214. // special case for host mode: we use SetHostVisibility to hide
  1215. // NetworkIdentities that aren't in observer range from host.
  1216. // this is what games like Dota/Counter-Strike do too, where a host
  1217. // does NOT see all players by default. they are in memory, but
  1218. // hidden to the host player.
  1219. //
  1220. // this code is from UNET, it's a bit strange but it works:
  1221. // * it hides newly connected identities in host mode
  1222. // => that part was the intended behaviour
  1223. // * it hides ALL NetworkIdentities in host mode when the host
  1224. // connects but hasn't selected a character yet
  1225. // => this only works because we have no .localConnection != null
  1226. // check. at this stage, localConnection is null because
  1227. // StartHost starts the server first, then calls this code,
  1228. // then starts the client and sets .localConnection. so we can
  1229. // NOT add a null check without breaking host visibility here.
  1230. // * it hides ALL NetworkIdentities in server-only mode because
  1231. // observers never contain the 'null' .localConnection
  1232. // => that was not intended, but let's keep it as it is so we
  1233. // don't break anything in host mode. it's way easier than
  1234. // iterating all identities in a special function in StartHost.
  1235. if (initialize)
  1236. {
  1237. if (!newObservers.Contains(localConnection))
  1238. {
  1239. // obsolete legacy system support (for now)
  1240. #pragma warning disable 618
  1241. if (identity.visibility != null)
  1242. identity.visibility.OnSetHostVisibility(false);
  1243. #pragma warning restore 618
  1244. else if (aoi != null)
  1245. aoi.SetHostVisibility(identity, false);
  1246. }
  1247. }
  1248. }
  1249. // RebuildObservers does a local rebuild for the NetworkIdentity.
  1250. // This causes the set of players that can see this object to be rebuild.
  1251. //
  1252. // IMPORTANT:
  1253. // => global rebuild would be more simple, BUT
  1254. // => local rebuild is way faster for spawn/despawn because we can
  1255. // simply rebuild a select NetworkIdentity only
  1256. // => having both .observers and .observing is necessary for local
  1257. // rebuilds
  1258. //
  1259. // in other words, this is the perfect solution even though it's not
  1260. // completely simple (due to .observers & .observing)
  1261. //
  1262. // Mirror maintains .observing automatically in the background. best of
  1263. // both worlds without any worrying now!
  1264. public static void RebuildObservers(NetworkIdentity identity, bool initialize)
  1265. {
  1266. // observers are null until OnStartServer creates them
  1267. if (identity.observers == null)
  1268. return;
  1269. // legacy proximitychecker support:
  1270. // make sure user doesn't use both new and old system.
  1271. #pragma warning disable 618
  1272. if (aoi != null & identity.visibility != null)
  1273. {
  1274. Debug.LogError($"RebuildObservers: {identity.name} has {identity.visibility.GetType()} component but there is also a global {aoi.GetType()} component. Can't use both systems at the same time!");
  1275. return;
  1276. }
  1277. #pragma warning restore 618
  1278. // if there is no interest management system,
  1279. // or if 'force shown' then add all connections
  1280. #pragma warning disable 618
  1281. if ((aoi == null && identity.visibility == null) ||
  1282. identity.visible == Visibility.ForceShown)
  1283. #pragma warning restore 618
  1284. {
  1285. RebuildObserversDefault(identity, initialize);
  1286. }
  1287. // otherwise let interest management system rebuild
  1288. else
  1289. {
  1290. RebuildObserversCustom(identity, initialize);
  1291. }
  1292. }
  1293. // broadcasting ////////////////////////////////////////////////////////
  1294. // helper function to get the right serialization for a connection
  1295. static NetworkWriter GetEntitySerializationForConnection(NetworkIdentity identity, NetworkConnectionToClient connection)
  1296. {
  1297. // get serialization for this entity (cached)
  1298. // IMPORTANT: int tick avoids floating point inaccuracy over days/weeks
  1299. NetworkIdentitySerialization serialization = identity.GetSerializationAtTick(Time.frameCount);
  1300. // is this entity owned by this connection?
  1301. bool owned = identity.connectionToClient == connection;
  1302. // send serialized data
  1303. // owner writer if owned
  1304. if (owned)
  1305. {
  1306. // was it dirty / did we actually serialize anything?
  1307. if (serialization.ownerWriter.Position > 0)
  1308. return serialization.ownerWriter;
  1309. }
  1310. // observers writer if not owned
  1311. else
  1312. {
  1313. // was it dirty / did we actually serialize anything?
  1314. if (serialization.observersWriter.Position > 0)
  1315. return serialization.observersWriter;
  1316. }
  1317. // nothing was serialized
  1318. return null;
  1319. }
  1320. // helper function to clear dirty bits of all spawned entities
  1321. static void ClearSpawnedDirtyBits()
  1322. {
  1323. // TODO clear dirty bits when removing the last observer instead!
  1324. // no need to do it for ALL entities ALL the time.
  1325. //
  1326. // for each spawned:
  1327. // clear dirty bits if it has no observers.
  1328. // we did this before push->pull broadcasting so let's keep
  1329. // doing this for now.
  1330. foreach (NetworkIdentity identity in NetworkIdentity.spawned.Values)
  1331. {
  1332. if (identity.observers == null || identity.observers.Count == 0)
  1333. {
  1334. // clear all component's dirty bits.
  1335. // it would be spawned on new observers anyway.
  1336. identity.ClearAllComponentsDirtyBits();
  1337. }
  1338. }
  1339. }
  1340. // helper function to broadcast the world to a connection
  1341. static void BroadcastToConnection(NetworkConnectionToClient connection)
  1342. {
  1343. // for each entity that this connection is seeing
  1344. foreach (NetworkIdentity identity in connection.observing)
  1345. {
  1346. // make sure it's not null or destroyed.
  1347. // (which can happen if someone uses
  1348. // GameObject.Destroy instead of
  1349. // NetworkServer.Destroy)
  1350. if (identity != null)
  1351. {
  1352. // get serialization for this entity viewed by this connection
  1353. // (if anything was serialized this time)
  1354. NetworkWriter serialization = GetEntitySerializationForConnection(identity, connection);
  1355. if (serialization != null)
  1356. {
  1357. EntityStateMessage message = new EntityStateMessage
  1358. {
  1359. netId = identity.netId,
  1360. payload = serialization.ToArraySegment()
  1361. };
  1362. connection.Send(message);
  1363. }
  1364. // clear dirty bits only for the components that we serialized
  1365. // DO NOT clean ALL component's dirty bits, because
  1366. // components can have different syncIntervals and we don't
  1367. // want to reset dirty bits for the ones that were not
  1368. // synced yet.
  1369. // (we serialized only the IsDirty() components, or all of
  1370. // them if initialState. clearing the dirty ones is enough.)
  1371. //
  1372. // NOTE: this is what we did before push->pull
  1373. // broadcasting. let's keep doing this for
  1374. // feature parity to not break anyone's project.
  1375. // TODO make this more simple / unnecessary later.
  1376. identity.ClearDirtyComponentsDirtyBits();
  1377. }
  1378. // spawned list should have no null entries because we
  1379. // always call Remove in OnObjectDestroy everywhere.
  1380. // if it does have null then someone used
  1381. // GameObject.Destroy instead of NetworkServer.Destroy.
  1382. else Debug.LogWarning("Found 'null' entry in observing list for connectionId=" + connection.connectionId + ". Please call NetworkServer.Destroy to destroy networked objects. Don't use GameObject.Destroy.");
  1383. }
  1384. }
  1385. // helper function to check a connection for inactivity
  1386. // and disconnect if necessary
  1387. // => returns true if disconnected
  1388. static bool DisconnectIfInactive(NetworkConnectionToClient connection)
  1389. {
  1390. // check for inactivity
  1391. #pragma warning disable 618
  1392. if (disconnectInactiveConnections &&
  1393. !connection.IsAlive(disconnectInactiveTimeout))
  1394. {
  1395. Debug.LogWarning($"Disconnecting {connection} for inactivity!");
  1396. connection.Disconnect();
  1397. return true;
  1398. }
  1399. #pragma warning restore 618
  1400. return false;
  1401. }
  1402. // NetworkLateUpdate called after any Update/FixedUpdate/LateUpdate
  1403. // (we add this to the UnityEngine in NetworkLoop)
  1404. static readonly List<NetworkConnectionToClient> connectionsCopy =
  1405. new List<NetworkConnectionToClient>();
  1406. static void Broadcast()
  1407. {
  1408. // copy all connections into a helper collection so that
  1409. // OnTransportDisconnected can be called while iterating.
  1410. // -> OnTransportDisconnected removes from the collection
  1411. // -> which would throw 'can't modify while iterating' errors
  1412. // => see also: https://github.com/vis2k/Mirror/issues/2739
  1413. // (copy nonalloc)
  1414. // TODO remove this when we move to 'lite' transports with only
  1415. // socket send/recv later.
  1416. connectionsCopy.Clear();
  1417. connections.Values.CopyTo(connectionsCopy);
  1418. // go through all connections
  1419. foreach (NetworkConnectionToClient connection in connectionsCopy)
  1420. {
  1421. // check for inactivity. disconnects if necessary.
  1422. if (DisconnectIfInactive(connection))
  1423. continue;
  1424. // has this connection joined the world yet?
  1425. // for each READY connection:
  1426. // pull in UpdateVarsMessage for each entity it observes
  1427. if (connection.isReady)
  1428. {
  1429. // broadcast world state to this connection
  1430. BroadcastToConnection(connection);
  1431. }
  1432. // update connection to flush out batched messages
  1433. connection.Update();
  1434. }
  1435. // TODO we already clear the serialized component's dirty bits above
  1436. // might as well clear everything???
  1437. //
  1438. // TODO this unfortunately means we still need to iterate ALL
  1439. // spawned and not just the ones with observers. figure
  1440. // out a way to get rid of this.
  1441. //
  1442. // TODO clear dirty bits when removing the last observer instead!
  1443. // no need to do it for ALL entities ALL the time.
  1444. //
  1445. ClearSpawnedDirtyBits();
  1446. }
  1447. // update //////////////////////////////////////////////////////////////
  1448. // NetworkEarlyUpdate called before any Update/FixedUpdate
  1449. // (we add this to the UnityEngine in NetworkLoop)
  1450. internal static void NetworkEarlyUpdate()
  1451. {
  1452. // process all incoming messages first before updating the world
  1453. if (Transport.activeTransport != null)
  1454. Transport.activeTransport.ServerEarlyUpdate();
  1455. }
  1456. internal static void NetworkLateUpdate()
  1457. {
  1458. // only broadcast world if active
  1459. if (active)
  1460. Broadcast();
  1461. // process all outgoing messages after updating the world
  1462. // (even if not active. still want to process disconnects etc.)
  1463. if (Transport.activeTransport != null)
  1464. Transport.activeTransport.ServerLateUpdate();
  1465. }
  1466. // obsolete to not break people's projects. Update was public.
  1467. // Deprecated 2021-03-02
  1468. [Obsolete("NetworkServer.Update is now called internally from our custom update loop. No need to call Update manually anymore.")]
  1469. public static void Update() => NetworkLateUpdate();
  1470. }
  1471. }