Added multihost support

This commit is contained in:
Albin Corén 2018-03-13 11:00:06 +01:00
parent 8f3319cd14
commit 75418c42a8
3 changed files with 35 additions and 23 deletions

View File

@ -20,10 +20,13 @@ namespace MLAPI
public int SendTickrate = 64; public int SendTickrate = 64;
public int EventTickrate = 64; public int EventTickrate = 64;
public int MaxConnections = 100; public int MaxConnections = 100;
public int Port = 7777; public int UDPPort = 7777;
public int WebsocketPort = 7778;
public string Address = "127.0.0.1"; public string Address = "127.0.0.1";
public int ClientConnectionBufferTimeout = 10; public int ClientConnectionBufferTimeout = 10;
public bool ConnectionApproval = false; public bool ConnectionApproval = false;
public bool UseUDPListener = true;
public bool UseWebsocketListener = false;
public Action<byte[], int, Action<int, bool>> ConnectionApprovalCallback = null; public Action<byte[], int, Action<int, bool>> ConnectionApprovalCallback = null;
public byte[] ConnectionData = new byte[0]; public byte[] ConnectionData = new byte[0];
public float SecondsHistory = 5; public float SecondsHistory = 5;

View File

@ -192,43 +192,38 @@ namespace MLAPI
} }
} }
HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections); HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections);
hostId = NetworkTransport.AddHost(hostTopology, NetworkConfig.Port); if (netConfig.UseUDPListener)
hostId = NetworkTransport.AddHost(hostTopology, NetworkConfig.UDPPort);
if (netConfig.UseWebsocketListener)
hostId = NetworkTransport.AddWebsocketHost(hostTopology, NetworkConfig.WebsocketPort);
isServer = true; isServer = true;
isClient = false; isClient = false;
isListening = true; isListening = true;
} }
public void StartClient(NetworkingConfiguration netConfig) public void StartClient(NetworkingConfiguration netConfig, bool websocket = false)
{ {
ConnectionConfig cConfig = Init(netConfig); ConnectionConfig cConfig = Init(netConfig);
HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections); HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections);
hostId = NetworkTransport.AddHost(hostTopology, 0, null); if (!websocket)
hostId = NetworkTransport.AddHost(hostTopology, 0, null);
else
hostId = NetworkTransport.AddWebsocketHost(hostTopology, 0, null);
isServer = false; isServer = false;
isClient = true; isClient = true;
isListening = true; isListening = true;
serverClientId = NetworkTransport.Connect(hostId, NetworkConfig.Address, NetworkConfig.Port, 0, out error); int port = websocket ? NetworkConfig.WebsocketPort : NetworkConfig.UDPPort;
serverClientId = NetworkTransport.Connect(hostId, NetworkConfig.Address, port, 0, out error);
} }
public void StopServer() public void StopServer()
{ {
HashSet<int> sentIds = new HashSet<int>();
//Don't know if I have to disconnect the clients. I'm assuming the NetworkTransport does all the cleaning on shtudown. But this way the clients get a disconnect message from server (so long it does't get lost) //Don't know if I have to disconnect the clients. I'm assuming the NetworkTransport does all the cleaning on shtudown. But this way the clients get a disconnect message from server (so long it does't get lost)
foreach (KeyValuePair<int, NetworkedClient> pair in connectedClients) foreach (KeyValuePair<int, NetworkedClient> pair in connectedClients)
{ {
if(!sentIds.Contains(pair.Key)) NetworkTransport.Disconnect(ClientIdManager.GetClientIdKey(pair.Key).hostId,
{ ClientIdManager.GetClientIdKey(pair.Key).connectionId, out error);
sentIds.Add(pair.Key);
NetworkTransport.Disconnect(hostId, pair.Key, out error);
}
}
foreach (int clientId in pendingClients)
{
if (!sentIds.Contains(clientId))
{
sentIds.Add(clientId);
NetworkTransport.Disconnect(hostId, clientId, out error);
}
} }
Shutdown(); Shutdown();
} }
@ -256,7 +251,18 @@ namespace MLAPI
} }
} }
HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections); HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections);
hostId = NetworkTransport.AddHost(hostTopology, NetworkConfig.Port, null); if (netConfig.UseWebsocketListener)
{
hostId = NetworkTransport.AddWebsocketHost(hostTopology, NetworkConfig.WebsocketPort);
ClientIdManager.clientIdToKey.Add(-1, new ClientIdKey(hostId, -1));
ClientIdManager.keyToClientId.Add(new ClientIdKey(hostId, -1), -1);
}
if (netConfig.UseUDPListener)
{
hostId = NetworkTransport.AddHost(hostTopology, NetworkConfig.UDPPort);
ClientIdManager.clientIdToKey.Add(-1, new ClientIdKey(hostId, -1));
ClientIdManager.keyToClientId.Add(new ClientIdKey(hostId, -1), -1);
}
isServer = true; isServer = true;
isClient = true; isClient = true;
isListening = true; isListening = true;
@ -297,7 +303,7 @@ namespace MLAPI
} }
//Receive stuff //Receive stuff
internal int hostId; private int hostId;
private int connectionId; private int connectionId;
private int channelId; private int channelId;
private int receivedSize; private int receivedSize;
@ -313,7 +319,8 @@ namespace MLAPI
{ {
foreach (KeyValuePair<int, NetworkedClient> pair in connectedClients) foreach (KeyValuePair<int, NetworkedClient> pair in connectedClients)
{ {
NetworkTransport.SendQueuedMessages(hostId, pair.Key, out error); NetworkTransport.SendQueuedMessages(ClientIdManager.GetClientIdKey(pair.Key).hostId,
ClientIdManager.GetClientIdKey(pair.Key).connectionId, out error);
} }
lastSendTickTime = Time.time; lastSendTickTime = Time.time;
} }
@ -324,6 +331,7 @@ namespace MLAPI
do do
{ {
processedEvents++; processedEvents++;
int hostId;
eventType = NetworkTransport.Receive(out hostId, out connectionId, out channelId, messageBuffer, messageBuffer.Length, out receivedSize, out error); eventType = NetworkTransport.Receive(out hostId, out connectionId, out channelId, messageBuffer, messageBuffer.Length, out receivedSize, out error);
NetworkError networkError = (NetworkError)error; NetworkError networkError = (NetworkError)error;
if (networkError == NetworkError.Timeout) if (networkError == NetworkError.Timeout)

View File

@ -38,7 +38,8 @@ namespace MLAPI.NetworkingManagerComponents
Debug.LogWarning("MLAPI: Lag compensation simulations are only to be ran on the server."); Debug.LogWarning("MLAPI: Lag compensation simulations are only to be ran on the server.");
return; return;
} }
float milisecondsDelay = NetworkTransport.GetCurrentRTT(NetworkingManager.singleton.hostId, clientId, out error) / 2f; float milisecondsDelay = NetworkTransport.GetCurrentRTT(ClientIdManager.GetClientIdKey(clientId).hostId,
ClientIdManager.GetClientIdKey(clientId).connectionId, out error) / 2f;
Simulate(milisecondsDelay * 1000f, action); Simulate(milisecondsDelay * 1000f, action);
} }