Added shutdown functions and changed protection level of API

This commit is contained in:
Albin Corén 2018-01-08 08:09:58 +01:00
parent aa10528971
commit fa85e53f05
2 changed files with 56 additions and 12 deletions

View File

@ -47,7 +47,7 @@ namespace MLAPI
}
}
private NetworkedObject _networkedObject = null;
public uint objectNetworkId
public uint networkId
{
get
{
@ -66,14 +66,14 @@ namespace MLAPI
//Change data type
private Dictionary<string, int> registeredMessageHandlers = new Dictionary<string, int>();
public int RegisterMessageHandler(string name, Action<int, byte[]> action)
protected int RegisterMessageHandler(string name, Action<int, byte[]> action)
{
int counter = MessageManager.AddIncomingMessageHandler(name, action);
registeredMessageHandlers.Add(name, counter);
return counter;
}
public void DeregisterMessageHandler(string name, int counter)
protected void DeregisterMessageHandler(string name, int counter)
{
MessageManager.RemoveIncomingMessageHandler(name, counter);
}
@ -86,7 +86,7 @@ namespace MLAPI
}
}
public void SendToServer(string messageType, string channelName, byte[] data)
protected void SendToServer(string messageType, string channelName, byte[] data)
{
if (isServer)
{
@ -98,7 +98,7 @@ namespace MLAPI
}
}
public void SendToLocalClient(string messageType, string channelName, byte[] data)
protected void SendToLocalClient(string messageType, string channelName, byte[] data)
{
if (!isServer)
{
@ -108,7 +108,7 @@ namespace MLAPI
NetworkingManager.singleton.Send(ownerClientId, messageType, channelName, data);
}
public void SendToNonLocalClients(string messageType, string channelName, byte[] data)
protected void SendToNonLocalClients(string messageType, string channelName, byte[] data)
{
if (!isServer)
{
@ -118,7 +118,7 @@ namespace MLAPI
NetworkingManager.singleton.Send(messageType, channelName, data, ownerClientId);
}
public void SendToClient(int clientId, string messageType, string channelName, byte[] data)
protected void SendToClient(int clientId, string messageType, string channelName, byte[] data)
{
if (!isServer)
{
@ -128,7 +128,7 @@ namespace MLAPI
NetworkingManager.singleton.Send(clientId, messageType, channelName, data);
}
public void SendToClients(int[] clientIds, string messageType, string channelName, byte[] data)
protected void SendToClients(int[] clientIds, string messageType, string channelName, byte[] data)
{
if (!isServer)
{
@ -138,7 +138,7 @@ namespace MLAPI
NetworkingManager.singleton.Send(clientIds, messageType, channelName, data);
}
public void SendToClients(List<int> clientIds, string messageType, string channelName, byte[] data)
protected void SendToClients(List<int> clientIds, string messageType, string channelName, byte[] data)
{
if (!isServer)
{
@ -148,7 +148,7 @@ namespace MLAPI
NetworkingManager.singleton.Send(clientIds, messageType, channelName, data);
}
public void SendToClients(string messageType, string channelName, byte[] data)
protected void SendToClients(string messageType, string channelName, byte[] data)
{
if (!isServer)
{
@ -158,7 +158,7 @@ namespace MLAPI
NetworkingManager.singleton.Send(messageType, channelName, data);
}
public NetworkedObject GetNetworkedObject(uint networkId)
protected NetworkedObject GetNetworkedObject(uint networkId)
{
return SpawnManager.spawnedObjects[networkId];
}

View File

@ -147,6 +147,41 @@ namespace MLAPI
serverClientId = NetworkTransport.Connect(hostId, NetworkConfig.Address, NetworkConfig.Port, 0, out error);
}
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)
foreach (KeyValuePair<int, NetworkedClient> pair in connectedClients)
{
if(!sentIds.Contains(pair.Key))
{
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();
}
public void StopHost()
{
StopServer();
//We don't stop client since we dont actually have a transport connection to our own host. We just handle host messages directly in the MLAPI
}
public void StopClient()
{
NetworkTransport.Disconnect(hostId, serverClientId, out error);
Shutdown();
}
public void StartHost(NetworkingConfiguration netConfig)
{
ConnectionConfig cConfig = Init(netConfig);
@ -188,6 +223,15 @@ namespace MLAPI
private void OnDestroy()
{
singleton = null;
Shutdown();
}
private void Shutdown()
{
isListening = false;
isClient = false;
isServer = false;
NetworkTransport.Shutdown();
}
//Receive stuff
@ -602,7 +646,7 @@ namespace MLAPI
}
}
internal void DisconnectClient(int clientId)
private void DisconnectClient(int clientId)
{
if (!isServer)
return;