Rearranged StartHost methods (#1506)

* Rearranged StartHost methods

* Added braces

* Moved and added comments.

* moved finishStartHostPending per Vis2k request
This commit is contained in:
MrGadget 2020-02-20 06:58:58 -05:00 committed by GitHub
parent b8bcd9ad25
commit b33515ba8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -412,7 +412,9 @@ public void StartClient(Uri uri)
}
if (runInBackground)
{
Application.runInBackground = true;
}
isNetworkActive = true;
@ -426,75 +428,6 @@ public void StartClient(Uri uri)
OnStartClient();
}
void StartHostClient()
{
if (LogFilter.Debug) Debug.Log("NetworkManager ConnectLocalClient");
if (authenticator != null)
{
authenticator.OnStartClient();
authenticator.OnClientAuthenticated.AddListener(OnClientAuthenticated);
}
networkAddress = "localhost";
NetworkServer.ActivateLocalClientScene();
RegisterClientMessages();
// ConnectLocalServer needs to be called AFTER RegisterClientMessages
// (https://github.com/vis2k/Mirror/pull/1249/)
NetworkClient.ConnectLocalServer();
OnStartClient();
}
// FinishStartHost is guaranteed to be called after the host server was
// fully started and all the asynchronous StartHost magic is finished
// (= scene loading), or immediately if there was no asynchronous magic.
//
// note: we don't really need FinishStartClient/FinishStartServer. the
// host version is enough.
bool finishStartHostPending;
void FinishStartHost()
{
// ConnectHost needs to be called BEFORE SpawnObjects:
// https://github.com/vis2k/Mirror/pull/1249/
// -> this sets NetworkServer.localConnection.
// -> localConnection needs to be set before SpawnObjects because:
// -> SpawnObjects calls OnStartServer in all NetworkBehaviours
// -> OnStartServer might spawn an object and set [SyncVar(hook="OnColorChanged")] object.color = green;
// -> this calls SyncVar.set (generated by Weaver), which has
// a custom case for host mode (because host mode doesn't
// get OnDeserialize calls, where SyncVar hooks are usually
// called):
//
// if (!SyncVarEqual(value, ref color))
// {
// if (NetworkServer.localClientActive && !getSyncVarHookGuard(1uL))
// {
// setSyncVarHookGuard(1uL, value: true);
// OnColorChangedHook(value);
// setSyncVarHookGuard(1uL, value: false);
// }
// SetSyncVar(value, ref color, 1uL);
// }
//
// -> localClientActive needs to be true, otherwise the hook
// isn't called in host mode!
//
// TODO call this after spawnobjects and worry about the syncvar hook fix later?
NetworkClient.ConnectHost();
// server scene was loaded. now spawn all the objects
NetworkServer.SpawnObjects();
// connect client and call OnStartClient AFTER server scene was
// loaded and all objects were spawned.
// DO NOT do this earlier. it would cause race conditions where a
// client will do things before the server is even fully started.
Debug.Log("StartHostClient called");
StartHostClient();
}
/// <summary>
/// This starts a network "host" - a server and client in the same application.
/// <para>The client returned from StartHost() is a special "local" client that communicates to the in-process server using a message queue instead of the real network. But in almost all other cases, it can be treated as a normal client.</para>
@ -549,6 +482,77 @@ public virtual void StartHost()
}
}
// This may be set true in StartHost and is evaluated in FinishStartHost
bool finishStartHostPending;
// FinishStartHost is guaranteed to be called after the host server was
// fully started and all the asynchronous StartHost magic is finished
// (= scene loading), or immediately if there was no asynchronous magic.
//
// note: we don't really need FinishStartClient/FinishStartServer. the
// host version is enough.
void FinishStartHost()
{
// ConnectHost needs to be called BEFORE SpawnObjects:
// https://github.com/vis2k/Mirror/pull/1249/
// -> this sets NetworkServer.localConnection.
// -> localConnection needs to be set before SpawnObjects because:
// -> SpawnObjects calls OnStartServer in all NetworkBehaviours
// -> OnStartServer might spawn an object and set [SyncVar(hook="OnColorChanged")] object.color = green;
// -> this calls SyncVar.set (generated by Weaver), which has
// a custom case for host mode (because host mode doesn't
// get OnDeserialize calls, where SyncVar hooks are usually
// called):
//
// if (!SyncVarEqual(value, ref color))
// {
// if (NetworkServer.localClientActive && !getSyncVarHookGuard(1uL))
// {
// setSyncVarHookGuard(1uL, value: true);
// OnColorChangedHook(value);
// setSyncVarHookGuard(1uL, value: false);
// }
// SetSyncVar(value, ref color, 1uL);
// }
//
// -> localClientActive needs to be true, otherwise the hook
// isn't called in host mode!
//
// TODO call this after spawnobjects and worry about the syncvar hook fix later?
NetworkClient.ConnectHost();
// server scene was loaded. now spawn all the objects
NetworkServer.SpawnObjects();
// connect client and call OnStartClient AFTER server scene was
// loaded and all objects were spawned.
// DO NOT do this earlier. it would cause race conditions where a
// client will do things before the server is even fully started.
Debug.Log("StartHostClient called");
StartHostClient();
}
void StartHostClient()
{
if (LogFilter.Debug) Debug.Log("NetworkManager ConnectLocalClient");
if (authenticator != null)
{
authenticator.OnStartClient();
authenticator.OnClientAuthenticated.AddListener(OnClientAuthenticated);
}
networkAddress = "localhost";
NetworkServer.ActivateLocalClientScene();
RegisterClientMessages();
// ConnectLocalServer needs to be called AFTER RegisterClientMessages
// (https://github.com/vis2k/Mirror/pull/1249/)
NetworkClient.ConnectLocalServer();
OnStartClient();
}
/// <summary>
/// This stops both the client and the server that the manager is using.
/// </summary>