Added better Transport config to allow for multiple hosts

This commit is contained in:
Albin Corén 2018-04-03 18:43:01 +02:00
parent 3d06ad177d
commit 1d4cb9a347
4 changed files with 44 additions and 20 deletions

View File

@ -21,6 +21,18 @@ namespace MLAPI.Data
/// </summary>
public ushort ProtocolVersion = 0;
/// <summary>
/// The transport hosts the sever uses
/// </summary>
public List<TransportHost> ServerTransports = new List<TransportHost>()
{
new TransportHost()
{
Name = "UDP Socket",
Port = 7777,
Websockets = false
}
};
/// <summary>
/// Channels used by the NetworkedTransport
/// </summary>
public List<Channel> Channels = new List<Channel>();
@ -75,13 +87,13 @@ namespace MLAPI.Data
/// </summary>
public int MaxConnections = 100;
/// <summary>
/// The port for the NetworkTransport to use
/// The port for the NetworkTransport to use when connecting
/// </summary>
public int Port = 7777;
public int ConnectPort = 7777;
/// <summary>
/// The address to connect to
/// </summary>
public string Address = "127.0.0.1";
public string ConnectAddress = "127.0.0.1";
/// <summary>
/// The amount of seconds to wait for handshake to complete before timing out a client
/// </summary>
@ -128,14 +140,6 @@ namespace MLAPI.Data
/// Wheter or not to enable scene switching
/// </summary>
public bool EnableSceneSwitching = false;
/// <summary>
/// Wheter or not we should have an additional host that listens for WebSocket requests
/// </summary>
public bool UseWebsockets = false;
/// <summary>
/// The port the websocket host listens on
/// </summary>
public int WebsocketsPort = 7778;
private byte[] ConfigHash = null;
/// <summary>

View File

@ -0,0 +1,12 @@
using System;
namespace MLAPI.Data
{
[Serializable]
public class TransportHost
{
public string Name = Guid.NewGuid().ToString().Replace("-", "");
public int Port = 7777;
public bool Websockets = false;
}
}

View File

@ -73,6 +73,7 @@
<Compile Include="Data\NetworkConfig.cs" />
<Compile Include="Data\NetworkPool.cs" />
<Compile Include="Data\TrackedPointData.cs" />
<Compile Include="Data\TransportHost.cs" />
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="MonoBehaviours\Prototyping\NetworkedAnimator.cs" />
<Compile Include="MonoBehaviours\Prototyping\NetworkedNavMeshAgent.cs" />

View File

@ -387,10 +387,13 @@ namespace MLAPI.MonoBehaviours.Core
}
}
HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections);
NetworkTransport.AddHost(hostTopology, NetworkConfig.Port);
if(NetworkConfig.UseWebsockets)
NetworkTransport.AddWebsocketHost(hostTopology, NetworkConfig.WebsocketsPort);
for (int i = 0; i < NetworkConfig.ServerTransports.Count; i++)
{
if (NetworkConfig.ServerTransports[i].Websockets)
NetworkTransport.AddWebsocketHost(hostTopology, NetworkConfig.ServerTransports[i].Port);
else
NetworkTransport.AddHost(hostTopology, NetworkConfig.ServerTransports[i].Port);
}
_isServer = true;
_isClient = false;
@ -420,7 +423,7 @@ namespace MLAPI.MonoBehaviours.Core
_isClient = true;
isListening = true;
byte error;
serverConnectionId = NetworkTransport.Connect(serverHostId, NetworkConfig.Address, NetworkConfig.Port, 0, out error);
serverConnectionId = NetworkTransport.Connect(serverHostId, NetworkConfig.ConnectAddress, NetworkConfig.ConnectPort, 0, out error);
Debug.LogWarning("MLAPI: Connection failed: " + ((NetworkError)error).ToString());
}
@ -444,7 +447,7 @@ namespace MLAPI.MonoBehaviours.Core
_isClient = true;
isListening = true;
byte error;
serverConnectionId = NetworkTransport.Connect(serverHostId, NetworkConfig.Address, NetworkConfig.WebsocketsPort, 0, out error);
serverConnectionId = NetworkTransport.Connect(serverHostId, NetworkConfig.ConnectAddress, NetworkConfig.ConnectPort, 0, out error);
Debug.LogWarning("MLAPI: Connection failed: " + ((NetworkError)error).ToString());
}
@ -527,9 +530,13 @@ namespace MLAPI.MonoBehaviours.Core
}
}
HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections);
NetworkTransport.AddHost(hostTopology, NetworkConfig.Port, null);
if (NetworkConfig.UseWebsockets)
NetworkTransport.AddWebsocketHost(hostTopology, NetworkConfig.WebsocketsPort);
for (int i = 0; i < NetworkConfig.ServerTransports.Count; i++)
{
if (NetworkConfig.ServerTransports[i].Websockets)
NetworkTransport.AddWebsocketHost(hostTopology, NetworkConfig.ServerTransports[i].Port);
else
NetworkTransport.AddHost(hostTopology, NetworkConfig.ServerTransports[i].Port);
}
_isServer = true;
_isClient = true;