Transport.activeTransport instead of NetworkManager.singleton.transport (#479)

* Created Transport.activeTransport to decrease NetworkManager god status.

* Fixed NetworkManagerHUD. (I forgot I had deleted it temporarily for testing)
This commit is contained in:
rodolphito 2019-02-28 02:58:37 -08:00 committed by vis2k
parent d4d764fb53
commit 7ecd66d6ec
7 changed files with 45 additions and 39 deletions

View File

@ -35,7 +35,7 @@ internal static void Shutdown()
ready = false;
s_IsSpawnFinished = false;
NetworkManager.singleton.transport.ClientDisconnect();
Transport.activeTransport.ClientDisconnect();
}
// this is called from message handler for Owner message

View File

@ -59,7 +59,7 @@ public void Connect(string ip)
serverIp = ip;
connectState = ConnectState.Connecting;
NetworkManager.singleton.transport.ClientConnect(ip);
Transport.activeTransport.ClientConnect(ip);
// setup all the handlers
connection = new NetworkConnection(serverIp, 0);
@ -69,10 +69,10 @@ public void Connect(string ip)
private void InitializeTransportHandlers()
{
// TODO do this in inspector?
NetworkManager.singleton.transport.OnClientConnected.AddListener(OnConnected);
NetworkManager.singleton.transport.OnClientDataReceived.AddListener(OnDataReceived);
NetworkManager.singleton.transport.OnClientDisconnected.AddListener(OnDisconnected);
NetworkManager.singleton.transport.OnClientError.AddListener(OnError);
Transport.activeTransport.OnClientConnected.AddListener(OnConnected);
Transport.activeTransport.OnClientDataReceived.AddListener(OnDataReceived);
Transport.activeTransport.OnClientDisconnected.AddListener(OnDisconnected);
Transport.activeTransport.OnClientError.AddListener(OnError);
}
void OnError(Exception exception)
@ -118,7 +118,7 @@ void PrepareForConnect()
{
active = true;
RegisterSystemHandlers(false);
NetworkManager.singleton.transport.enabled = true;
Transport.activeTransport.enabled = true;
InitializeTransportHandlers();
}
@ -141,10 +141,10 @@ public virtual void Disconnect()
void RemoveTransportHandlers()
{
// so that we don't register them more than once
NetworkManager.singleton.transport.OnClientConnected.RemoveListener(OnConnected);
NetworkManager.singleton.transport.OnClientDataReceived.RemoveListener(OnDataReceived);
NetworkManager.singleton.transport.OnClientDisconnected.RemoveListener(OnDisconnected);
NetworkManager.singleton.transport.OnClientError.RemoveListener(OnError);
Transport.activeTransport.OnClientConnected.RemoveListener(OnConnected);
Transport.activeTransport.OnClientDataReceived.RemoveListener(OnDataReceived);
Transport.activeTransport.OnClientDisconnected.RemoveListener(OnDisconnected);
Transport.activeTransport.OnClientError.RemoveListener(OnError);
}
public bool Send(short msgType, MessageBase msg)

View File

@ -84,12 +84,12 @@ public void Disconnect()
// paul: we may be connecting or connected, either way, we need to disconnect
// transport should not do anything if it is not connecting/connected
NetworkManager.singleton.transport.ClientDisconnect();
Transport.activeTransport.ClientDisconnect();
// server? then disconnect that client
if (NetworkManager.singleton.transport.ServerActive())
if (Transport.activeTransport.ServerActive())
{
NetworkManager.singleton.transport.ServerDisconnect(connectionId);
Transport.activeTransport.ServerDisconnect(connectionId);
}
// remove observers
@ -138,9 +138,9 @@ internal virtual bool SendBytes( byte[] bytes, int channelId = Channels.DefaultR
{
if (logNetworkMessages) { Debug.Log("ConnectionSend con:" + connectionId + " bytes:" + BitConverter.ToString(bytes)); }
if (bytes.Length > NetworkManager.singleton.transport.GetMaxPacketSize(channelId))
if (bytes.Length > Transport.activeTransport.GetMaxPacketSize(channelId))
{
Debug.LogError("NetworkConnection:SendBytes cannot send packet larger than " + NetworkManager.singleton.transport.GetMaxPacketSize(channelId) + " bytes");
Debug.LogError("NetworkConnection:SendBytes cannot send packet larger than " + Transport.activeTransport.GetMaxPacketSize(channelId) + " bytes");
return false;
}
@ -249,13 +249,13 @@ public virtual void TransportReceive(byte[] buffer)
public virtual bool TransportSend(int channelId, byte[] bytes, out byte error)
{
error = 0;
if (NetworkManager.singleton.transport.ClientConnected())
if (Transport.activeTransport.ClientConnected())
{
return NetworkManager.singleton.transport.ClientSend(channelId, bytes);
return Transport.activeTransport.ClientSend(channelId, bytes);
}
else if (NetworkManager.singleton.transport.ServerActive())
else if (Transport.activeTransport.ServerActive())
{
return NetworkManager.singleton.transport.ServerSend(connectionId, channelId, bytes);
return Transport.activeTransport.ServerSend(connectionId, channelId, bytes);
}
return false;
}

View File

@ -35,7 +35,7 @@ public class NetworkManager : MonoBehaviour
[Header("Network Info")]
// transport layer
public Transport transport;
[SerializeField] Transport transport;
[FormerlySerializedAs("m_NetworkAddress")] public string networkAddress = "localhost";
[FormerlySerializedAs("m_MaxConnections")] public int maxConnections = 4;
@ -104,6 +104,8 @@ void InitializeSingleton()
return;
}
Transport.activeTransport = transport;
// do this early
LogFilter.Debug = showDebugMessages;
@ -160,7 +162,7 @@ public virtual void LateUpdate()
// virtual so that inheriting classes' OnApplicationQuit() can call base.OnApplicationQuit() too
public virtual void OnApplicationQuit()
{
transport.Shutdown();
Transport.activeTransport.Shutdown();
}
// virtual so that inheriting classes' OnValidate() can call base.OnValidate() too
@ -424,7 +426,7 @@ internal void ClientChangeScene(string newSceneName, bool forceReload)
if (client != null)
{
if (LogFilter.Debug) { Debug.Log("ClientChangeScene: pausing handlers while scene is loading to avoid data loss after scene was loaded."); }
NetworkManager.singleton.transport.enabled = false;
Transport.activeTransport.enabled = false;
}
// Let client prepare for scene change
@ -442,7 +444,7 @@ void FinishLoadScene()
{
// process queued messages that we received while loading the scene
if (LogFilter.Debug) { Debug.Log("FinishLoadScene: resuming handlers after scene was loading."); }
NetworkManager.singleton.transport.enabled = true;
Transport.activeTransport.enabled = true;
if (s_ClientReadyConnection != null)
{

View File

@ -78,7 +78,7 @@ void OnGUI()
// server / client status message
if (NetworkServer.active)
{
GUILayout.Label("Server: active. Transport: " + manager.transport);
GUILayout.Label("Server: active. Transport: " + Transport.activeTransport);
}
if (manager.IsClientConnected())
{

View File

@ -41,13 +41,13 @@ public static void Shutdown()
}
else
{
NetworkManager.singleton.transport.ServerStop();
Transport.activeTransport.ServerStop();
}
NetworkManager.singleton.transport.OnServerDisconnected.RemoveListener(OnDisconnected);
NetworkManager.singleton.transport.OnServerConnected.RemoveListener(OnConnected);
NetworkManager.singleton.transport.OnServerDataReceived.RemoveListener(OnDataReceived);
NetworkManager.singleton.transport.OnServerError.RemoveListener(OnError);
Transport.activeTransport.OnServerDisconnected.RemoveListener(OnDisconnected);
Transport.activeTransport.OnServerConnected.RemoveListener(OnConnected);
Transport.activeTransport.OnServerDataReceived.RemoveListener(OnDataReceived);
Transport.activeTransport.OnServerError.RemoveListener(OnError);
s_Initialized = false;
}
@ -65,10 +65,10 @@ static void Initialize()
//Make sure connections are cleared in case any old connections references exist from previous sessions
connections.Clear();
NetworkManager.singleton.transport.OnServerDisconnected.AddListener(OnDisconnected);
NetworkManager.singleton.transport.OnServerConnected.AddListener(OnConnected);
NetworkManager.singleton.transport.OnServerDataReceived.AddListener(OnDataReceived);
NetworkManager.singleton.transport.OnServerError.AddListener(OnError);
Transport.activeTransport.OnServerDisconnected.AddListener(OnDisconnected);
Transport.activeTransport.OnServerConnected.AddListener(OnConnected);
Transport.activeTransport.OnServerDataReceived.AddListener(OnDataReceived);
Transport.activeTransport.OnServerError.AddListener(OnError);
}
@ -88,7 +88,7 @@ public static bool Listen(int maxConnections)
// only start server if we want to listen
if (!dontListen)
{
NetworkManager.singleton.transport.ServerStart();
Transport.activeTransport.ServerStart();
if (LogFilter.Debug) { Debug.Log("Server started listening"); }
}
@ -286,14 +286,14 @@ static void OnConnected(int connectionId)
if (connectionId <= 0)
{
Debug.LogError("Server.HandleConnect: invalid connectionId: " + connectionId + " . Needs to be >0, because 0 is reserved for local player.");
NetworkManager.singleton.transport.ServerDisconnect(connectionId);
Transport.activeTransport.ServerDisconnect(connectionId);
return;
}
// connectionId not in use yet?
if (connections.ContainsKey(connectionId))
{
NetworkManager.singleton.transport.ServerDisconnect(connectionId);
Transport.activeTransport.ServerDisconnect(connectionId);
if (LogFilter.Debug) { Debug.Log("Server connectionId " + connectionId + " already in use. kicked client:" + connectionId); }
return;
}
@ -306,7 +306,7 @@ static void OnConnected(int connectionId)
if (connections.Count < s_MaxConnections)
{
// get ip address from connection
string address = NetworkManager.singleton.transport.ServerGetClientAddress(connectionId);
string address = Transport.activeTransport.ServerGetClientAddress(connectionId);
// add player info
NetworkConnection conn = new NetworkConnection(address, connectionId);
@ -315,7 +315,7 @@ static void OnConnected(int connectionId)
else
{
// kick
NetworkManager.singleton.transport.ServerDisconnect(connectionId);
Transport.activeTransport.ServerDisconnect(connectionId);
if (LogFilter.Debug) { Debug.Log("Server full, kicked client:" + connectionId); }
}
}

View File

@ -15,6 +15,10 @@ namespace Mirror
public abstract class Transport : MonoBehaviour
{
// static Transport which receives all network events
// this is usually set by NetworkManager, but doesn't have to be.
public static Transport activeTransport;
// determines if the transport is available for this platform
// by default a transport is available in all platforms except webgl
public virtual bool Available()