Skip to content

Commit

Permalink
refactor: Remove networkAddress from NetworkManager (#67)
Browse files Browse the repository at this point in the history
* refactor: Remove networkAddress from NetworkManager

* Remove networkIp from client too

BREAKING CHANGE: StartClient now receives the server ip
BREAKING CHANGE: NetworkManager no longer has NetworkAddress
  • Loading branch information
paulpach committed Mar 5, 2020
1 parent e88b87c commit e89c32d
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 43 deletions.
5 changes: 0 additions & 5 deletions Assets/Mirror/Examples/Chat/Scripts/ChatNetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ public class ChatNetworkManager : NetworkManager
{
public string playerName { get; set; }

public void SetHostname(string hostname)
{
networkAddress = hostname;
}

public ChatWindow chatWindow;

public class CreatePlayerMessage : MessageBase
Expand Down
3 changes: 1 addition & 2 deletions Assets/Mirror/Examples/ListServer/ListServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,7 @@ void OnUI()
slot.joinButton.onClick.RemoveAllListeners();
slot.joinButton.onClick.AddListener(() =>
{
manager.networkAddress = server.ip;
manager.StartClient();
manager.StartClient(server.ip);
});
}

Expand Down
2 changes: 1 addition & 1 deletion Assets/Mirror/Examples/Tanks/Scenes/Scene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ MonoBehaviour:
showGUI: 1
offsetX: 0
offsetY: 0
serverIp: localhost
--- !u!114 &1282001520
MonoBehaviour:
m_ObjectHideFlags: 0
Expand All @@ -465,7 +466,6 @@ MonoBehaviour:
server: {fileID: 1282001523}
client: {fileID: 1282001522}
transport: {fileID: 1282001524}
networkAddress: localhost
maxConnections: 4
authenticator: {fileID: 0}
playerPrefab: {fileID: 1916082411674582, guid: 6f43bf5488a7443d19ab2a83c6b91f35,
Expand Down
12 changes: 3 additions & 9 deletions Assets/Mirror/Runtime/NetworkClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ public class NetworkClient : MonoBehaviour

internal ConnectState connectState = ConnectState.None;

/// <summary>
/// The IP address of the server that this client is connected to.
/// <para>This will be empty if the client has not connected yet.</para>
/// </summary>
public string serverIp => connection.address;

/// <summary>
/// active is true while a client is connecting/connected
/// (= while the network is active)
Expand All @@ -66,16 +60,16 @@ public class NetworkClient : MonoBehaviour
/// Connect client to a NetworkServer instance.
/// </summary>
/// <param name="address"></param>
public async Task ConnectAsync(string address)
public async Task ConnectAsync(string serverIp)
{
if (LogFilter.Debug) Debug.Log("Client Connect: " + address);
if (LogFilter.Debug) Debug.Log("Client Connect: " + serverIp);

RegisterSystemHandlers(false);
Transport.activeTransport.enabled = true;
InitializeTransportHandlers();

connectState = ConnectState.Connecting;
await Transport.activeTransport.ClientConnectAsync(address);
await Transport.activeTransport.ClientConnectAsync(serverIp);

// setup all the handlers
connection = new NetworkConnectionToServer();
Expand Down
22 changes: 5 additions & 17 deletions Assets/Mirror/Runtime/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,12 @@ public class NetworkManager : MonoBehaviour
public NetworkServer server;
public NetworkClient client;

[Header("Network Info")]

// transport layer
[Header("Network Info")]
[Tooltip("Transport component attached to this object that server and client will use to connect")]
[SerializeField]
protected Transport transport;

/// <summary>
/// The network address currently in use.
/// <para>For clients, this is the address of the server that is connected to. For servers, this is the local address.</para>
/// </summary>
[FormerlySerializedAs("m_NetworkAddress")]
[Tooltip("Network Address where the client should connect to the server. Server does not use this for anything.")]
public string networkAddress = "localhost";

/// <summary>
/// The maximum number of concurrent network connections to support.
/// <para>This effects the memory usage of the network layer.</para>
Expand Down Expand Up @@ -368,7 +358,7 @@ public void StartServer()
/// This starts a network client. It uses the networkAddress and networkPort properties as the address to connect to.
/// <para>This makes the newly created client connect to the server immediately.</para>
/// </summary>
public void StartClient()
public void StartClient(string serverIp)
{
mode = NetworkManagerMode.ClientOnly;

Expand All @@ -387,14 +377,14 @@ public void StartClient()

RegisterClientMessages();

if (string.IsNullOrEmpty(networkAddress))
if (string.IsNullOrEmpty(serverIp))
{
Debug.LogError("Must set the Network Address field in the manager");
Debug.LogError("serverIp shouldn't be empty");
return;
}
if (LogFilter.Debug) Debug.Log("NetworkManager StartClient address:" + networkAddress);
if (LogFilter.Debug) Debug.Log("NetworkManager StartClient address:" + serverIp);

_ = client.ConnectAsync(networkAddress);
_ = client.ConnectAsync(serverIp);

OnStartClient();
}
Expand Down Expand Up @@ -426,7 +416,6 @@ public void StartClient(Uri uri)
RegisterClientMessages();

if (LogFilter.Debug) Debug.Log("NetworkManager StartClient address:" + uri);
networkAddress = uri.Host;

_ = client.ConnectAsync(uri);

Expand Down Expand Up @@ -547,7 +536,6 @@ void StartHostClient()
authenticator.OnClientAuthenticated += OnClientAuthenticated;
}

networkAddress = "localhost";
server.ActivateHostScene();
RegisterClientMessages();

Expand Down
13 changes: 9 additions & 4 deletions Assets/Mirror/Runtime/NetworkManagerHUD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public class NetworkManagerHUD : MonoBehaviour
/// </summary>
public int offsetY;

/// <summary>
/// The IP address we're connecting to.
/// </summary>
public string serverIp = "localhost";

void Awake()
{
manager = GetComponent<NetworkManager>();
Expand Down Expand Up @@ -60,9 +65,9 @@ void OnGUI()
GUILayout.BeginHorizontal();
if (GUILayout.Button("LAN Client"))
{
manager.StartClient();
manager.StartClient(serverIp);
}
manager.networkAddress = GUILayout.TextField(manager.networkAddress);
serverIp = GUILayout.TextField(serverIp);
GUILayout.EndHorizontal();

// LAN Server Only
Expand All @@ -79,7 +84,7 @@ void OnGUI()
else
{
// Connecting
GUILayout.Label("Connecting to " + manager.networkAddress + "..");
GUILayout.Label("Connecting to " + serverIp + "..");
if (GUILayout.Button("Cancel Connection Attempt"))
{
manager.StopClient();
Expand All @@ -95,7 +100,7 @@ void OnGUI()
}
if (manager.client.isConnected)
{
GUILayout.Label("Client: address=" + manager.networkAddress);
GUILayout.Label("Client: address=" + serverIp);
}
}

Expand Down
9 changes: 4 additions & 5 deletions Assets/Mirror/Tests/Play/NetworkManagerTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using NUnit.Framework;
using NUnit.Framework;
using UnityEngine;

namespace Mirror.Tests
Expand Down Expand Up @@ -34,7 +34,6 @@ public void VariableTest()
Assert.That(manager.showDebugMessages, Is.False);
Assert.That(manager.serverTickRate, Is.EqualTo(30));
Assert.That(manager.offlineScene, Is.Empty);
Assert.That(manager.networkAddress, Is.EqualTo("localhost"));
Assert.That(manager.maxConnections, Is.EqualTo(4));
Assert.That(manager.autoCreatePlayer, Is.True);
Assert.That(manager.spawnPrefabs, Is.Empty);
Expand Down Expand Up @@ -73,7 +72,7 @@ public void StopServerTest()
[Test]
public void StartClientTest()
{
manager.StartClient();
manager.StartClient("localhost");

Assert.That(manager.isNetworkActive , Is.True);
Assert.That(manager.mode, Is.EqualTo(NetworkManagerMode.ClientOnly));
Expand All @@ -84,7 +83,7 @@ public void StartClientTest()
[Test]
public void StopClientTest()
{
manager.StartClient();
manager.StartClient("localhost");
manager.StopClient();

Assert.That(manager.isNetworkActive , Is.False);
Expand All @@ -94,7 +93,7 @@ public void StopClientTest()
[Test]
public void ShutdownTest()
{
manager.StartClient();
manager.StartClient("localhost");
manager.StopClient();

Assert.That(manager.startPositions , Is.Empty);
Expand Down

0 comments on commit e89c32d

Please sign in to comment.