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; ready = false;
s_IsSpawnFinished = false; s_IsSpawnFinished = false;
NetworkManager.singleton.transport.ClientDisconnect(); Transport.activeTransport.ClientDisconnect();
} }
// this is called from message handler for Owner message // this is called from message handler for Owner message

View File

@ -59,7 +59,7 @@ public void Connect(string ip)
serverIp = ip; serverIp = ip;
connectState = ConnectState.Connecting; connectState = ConnectState.Connecting;
NetworkManager.singleton.transport.ClientConnect(ip); Transport.activeTransport.ClientConnect(ip);
// setup all the handlers // setup all the handlers
connection = new NetworkConnection(serverIp, 0); connection = new NetworkConnection(serverIp, 0);
@ -69,10 +69,10 @@ public void Connect(string ip)
private void InitializeTransportHandlers() private void InitializeTransportHandlers()
{ {
// TODO do this in inspector? // TODO do this in inspector?
NetworkManager.singleton.transport.OnClientConnected.AddListener(OnConnected); Transport.activeTransport.OnClientConnected.AddListener(OnConnected);
NetworkManager.singleton.transport.OnClientDataReceived.AddListener(OnDataReceived); Transport.activeTransport.OnClientDataReceived.AddListener(OnDataReceived);
NetworkManager.singleton.transport.OnClientDisconnected.AddListener(OnDisconnected); Transport.activeTransport.OnClientDisconnected.AddListener(OnDisconnected);
NetworkManager.singleton.transport.OnClientError.AddListener(OnError); Transport.activeTransport.OnClientError.AddListener(OnError);
} }
void OnError(Exception exception) void OnError(Exception exception)
@ -118,7 +118,7 @@ void PrepareForConnect()
{ {
active = true; active = true;
RegisterSystemHandlers(false); RegisterSystemHandlers(false);
NetworkManager.singleton.transport.enabled = true; Transport.activeTransport.enabled = true;
InitializeTransportHandlers(); InitializeTransportHandlers();
} }
@ -141,10 +141,10 @@ public virtual void Disconnect()
void RemoveTransportHandlers() void RemoveTransportHandlers()
{ {
// so that we don't register them more than once // so that we don't register them more than once
NetworkManager.singleton.transport.OnClientConnected.RemoveListener(OnConnected); Transport.activeTransport.OnClientConnected.RemoveListener(OnConnected);
NetworkManager.singleton.transport.OnClientDataReceived.RemoveListener(OnDataReceived); Transport.activeTransport.OnClientDataReceived.RemoveListener(OnDataReceived);
NetworkManager.singleton.transport.OnClientDisconnected.RemoveListener(OnDisconnected); Transport.activeTransport.OnClientDisconnected.RemoveListener(OnDisconnected);
NetworkManager.singleton.transport.OnClientError.RemoveListener(OnError); Transport.activeTransport.OnClientError.RemoveListener(OnError);
} }
public bool Send(short msgType, MessageBase msg) 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 // paul: we may be connecting or connected, either way, we need to disconnect
// transport should not do anything if it is not connecting/connected // transport should not do anything if it is not connecting/connected
NetworkManager.singleton.transport.ClientDisconnect(); Transport.activeTransport.ClientDisconnect();
// server? then disconnect that client // 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 // 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 (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; return false;
} }
@ -249,13 +249,13 @@ public virtual void TransportReceive(byte[] buffer)
public virtual bool TransportSend(int channelId, byte[] bytes, out byte error) public virtual bool TransportSend(int channelId, byte[] bytes, out byte error)
{ {
error = 0; 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; return false;
} }

View File

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

View File

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

View File

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

View File

@ -15,6 +15,10 @@ namespace Mirror
public abstract class Transport : MonoBehaviour 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 // determines if the transport is available for this platform
// by default a transport is available in all platforms except webgl // by default a transport is available in all platforms except webgl
public virtual bool Available() public virtual bool Available()