diff --git a/MLAPI/Data/ClientIdKey.cs b/MLAPI/Data/ClientIdKey.cs
deleted file mode 100644
index cf8d941..0000000
--- a/MLAPI/Data/ClientIdKey.cs
+++ /dev/null
@@ -1,52 +0,0 @@
-namespace MLAPI.Data
-{
- ///
- /// A struct representing a client. Contains a hostId and a connectionId.
- ///
- internal struct ClientIdKey
- {
- ///
- /// The NetworkTransport hostId
- ///
- internal readonly int hostId;
- ///
- /// The NetworkTransport connectionId
- ///
- internal readonly int connectionId;
-
- ///
- /// Creates a new ClientIdKey
- ///
- /// The NetworkTransport hostId
- /// The NetworkTransport connectionId
- internal ClientIdKey (int hostId, int connectionId)
- {
- this.hostId = hostId;
- this.connectionId = connectionId;
- }
-
- public override bool Equals (object obj)
- {
- if (obj == null || GetType() != obj.GetType())
- return false;
-
- ClientIdKey key = (ClientIdKey)obj;
- return (hostId == key.hostId) && (connectionId == key.hostId);
- }
-
- public override int GetHashCode()
- {
- return hostId ^ connectionId;
- }
-
- public static bool operator ==(ClientIdKey x, ClientIdKey y)
- {
- return x.hostId == y.hostId && x.connectionId == y.connectionId;
- }
-
- public static bool operator !=(ClientIdKey x, ClientIdKey y)
- {
- return !(x == y);
- }
- }
-}
diff --git a/MLAPI/Data/NetId.cs b/MLAPI/Data/NetId.cs
new file mode 100644
index 0000000..0b97fed
--- /dev/null
+++ b/MLAPI/Data/NetId.cs
@@ -0,0 +1,86 @@
+using MLAPI.MonoBehaviours.Core;
+using System;
+
+namespace MLAPI.Data
+{
+ public struct NetId
+ {
+ public byte HostId;
+ public ushort ConnectionId;
+ public byte Meta;
+
+ public bool IsHost()
+ {
+ return Meta == 1;
+ }
+
+ public bool IsInvalid()
+ {
+ return Meta == 2;
+ }
+
+ public static NetId ServerNetId
+ {
+ get
+ {
+ return new NetId((byte)NetworkingManager.singleton.serverHostId, (ushort)NetworkingManager.singleton.serverConnectionId, false, false);
+ }
+ }
+
+ public NetId(byte hostId, ushort connectionId, bool isHost, bool isInvalid)
+ {
+ HostId = hostId;
+ ConnectionId = connectionId;
+ if (isHost)
+ Meta = 1;
+ else if (isInvalid)
+ Meta = 2;
+ else
+ Meta = 0;
+ }
+
+
+ public NetId(uint clientId)
+ {
+ byte[] bytes = BitConverter.GetBytes(clientId);
+ HostId = bytes[0];
+ ConnectionId = BitConverter.ToUInt16(bytes, 1);
+ Meta = bytes[3];
+ }
+
+ public uint GetClientId()
+ {
+ byte[] bytes = new byte[4];
+ byte[] connIdBytes = BitConverter.GetBytes(ConnectionId);
+ bytes[0] = HostId;
+ bytes[1] = connIdBytes[0];
+ bytes[2] = connIdBytes[1];
+ bytes[3] = Meta;
+ return BitConverter.ToUInt32(bytes, 0);
+ }
+
+ public override bool Equals (object obj)
+ {
+ if (obj == null || GetType() != obj.GetType())
+ return false;
+
+ NetId key = (NetId)obj;
+ return (HostId == key.HostId) && (ConnectionId == key.ConnectionId);
+ }
+
+ public override int GetHashCode()
+ {
+ return (int)GetClientId();
+ }
+
+ public static bool operator ==(NetId client1, NetId client2)
+ {
+ return (client1.HostId == client2.HostId && client1.ConnectionId == client2.ConnectionId) || (client1.IsHost() == client2.IsHost());
+ }
+
+ public static bool operator !=(NetId client1, NetId client2)
+ {
+ return !(client1 == client2);
+ }
+ }
+}
diff --git a/MLAPI/Data/NetworkConfig.cs b/MLAPI/Data/NetworkConfig.cs
index a8f3fc2..b493fd5 100644
--- a/MLAPI/Data/NetworkConfig.cs
+++ b/MLAPI/Data/NetworkConfig.cs
@@ -128,6 +128,14 @@ namespace MLAPI.Data
/// Wheter or not to enable scene switching
///
public bool EnableSceneSwitching = false;
+ ///
+ /// Wheter or not we should have an additional host that listens for WebSocket requests
+ ///
+ public bool UseWebsockets = false;
+ ///
+ /// The port the websocket host listens on
+ ///
+ public int WebsocketsPort = 7778;
private byte[] ConfigHash = null;
///
diff --git a/MLAPI/Data/NetworkedClient.cs b/MLAPI/Data/NetworkedClient.cs
index de7ae46..855adfc 100644
--- a/MLAPI/Data/NetworkedClient.cs
+++ b/MLAPI/Data/NetworkedClient.cs
@@ -12,7 +12,7 @@ namespace MLAPI.Data
///
/// The Id of the NetworkedClient
///
- public int ClientId;
+ public uint ClientId;
///
/// The PlayerObject of the Client
///
diff --git a/MLAPI/GlobalSuppressions.cs b/MLAPI/GlobalSuppressions.cs
new file mode 100644
index 0000000..2b128de
--- /dev/null
+++ b/MLAPI/GlobalSuppressions.cs
@@ -0,0 +1,20 @@
+
+// This file is used by Code Analysis to maintain SuppressMessage
+// attributes that are applied to this project.
+// Project-level suppressions either have no target or are given
+// a specific target and scoped to a namespace, type, member, etc.
+
+[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0018:Inline variable declaration", Justification = "Not supported in Unity Mono version", Scope = "member", Target = "~M:MLAPI.MonoBehaviours.Core.NetworkingManager.StartClient")]
+
+[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0018:Inline variable declaration", Justification = "Not supported in Unity Mono version", Scope = "member", Target = "~M:MLAPI.MonoBehaviours.Core.NetworkingManager.StartClientWebsocket")]
+[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0018:Inline variable declaration", Justification = "Not supported in Unity Mono version", Scope = "member", Target = "~M:MLAPI.MonoBehaviours.Core.NetworkingManager.StopServer")]
+[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0018:Inline variable declaration", Justification = "Not supported in Unity Mono version", Scope = "member", Target = "~M:MLAPI.MonoBehaviours.Core.NetworkingManager.StopClient")]
+[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0018:Inline variable declaration", Justification = "Not supported in Unity Mono version", Scope = "member", Target = "~M:MLAPI.MonoBehaviours.Core.NetworkingManager.Update")]
+[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0018:Inline variable declaration", Justification = "Not supported in Unity Mono version", Scope = "member", Target = "~M:MLAPI.MonoBehaviours.Core.NetworkingManager.HandleIncomingData(System.UInt32,System.Byte[],System.Int32)")]
+[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0018:Inline variable declaration", Justification = "Not supported in Unity Mono version", Scope = "member", Target = "~M:MLAPI.MonoBehaviours.Core.NetworkingManager.PassthroughSend(System.UInt32,System.UInt32,System.UInt16,System.Int32,System.Byte[],System.Nullable{System.UInt32},System.Nullable{System.UInt16})")]
+[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0018:Inline variable declaration", Justification = "Not supported in Unity Mono version", Scope = "member", Target = "~M:MLAPI.MonoBehaviours.Core.NetworkingManager.Send(System.Collections.Generic.List{System.UInt32},System.String,System.String,System.Byte[],System.Nullable{System.UInt32},System.Nullable{System.UInt16})")]
+[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0018:Inline variable declaration", Justification = "Not supported in Unity Mono version", Scope = "member", Target = "~M:MLAPI.MonoBehaviours.Core.NetworkingManager.HandleApproval(System.UInt32,System.Boolean)")]
+[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0018:Inline variable declaration", Justification = "Not supported in Unity Mono version", Scope = "member", Target = "~M:MLAPI.MonoBehaviours.Core.NetworkingManager.DisconnectClient(System.UInt32)")]
+[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0018:Inline variable declaration", Justification = "Not supported in Unity Mono version", Scope = "member", Target = "~M:MLAPI.MonoBehaviours.Core.NetworkingManager.Send(System.String,System.String,System.Byte[],System.UInt32,System.Nullable{System.UInt32},System.Nullable{System.UInt16})")]
+[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0018:Inline variable declaration", Justification = "Not supported in Unity Mono version", Scope = "member", Target = "~M:MLAPI.MonoBehaviours.Core.NetworkingManager.Send(System.String,System.String,System.Byte[],System.Nullable{System.UInt32},System.Nullable{System.UInt16})")]
+[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0018:Inline variable declaration", Justification = "Not supported in Unity Mono version", Scope = "member", Target = "~M:MLAPI.MonoBehaviours.Core.NetworkingManager.Send(System.UInt32[],System.String,System.String,System.Byte[],System.Nullable{System.UInt32},System.Nullable{System.UInt16})")]
diff --git a/MLAPI/MLAPI.csproj b/MLAPI/MLAPI.csproj
index 7b0bc3b..d684e2d 100644
--- a/MLAPI/MLAPI.csproj
+++ b/MLAPI/MLAPI.csproj
@@ -73,6 +73,7 @@
+
@@ -91,8 +92,7 @@
-
-
+
diff --git a/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs b/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs
index 3c97d4d..18db5c8 100644
--- a/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs
+++ b/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs
@@ -98,7 +98,7 @@ namespace MLAPI.MonoBehaviours.Core
///
/// Gets the clientId that owns the NetworkedObject
///
- public int ownerClientId
+ public uint ownerClientId
{
get
{
@@ -146,7 +146,7 @@ namespace MLAPI.MonoBehaviours.Core
/// The MessageType to register
/// The callback to get invoked whenever a message is received
/// HandlerId for the messageHandler that can be used to deregister the messageHandler
- protected int RegisterMessageHandler(string name, Action action)
+ protected int RegisterMessageHandler(string name, Action action)
{
if (!MessageManager.messageTypes.ContainsKey(name))
{
@@ -157,7 +157,7 @@ namespace MLAPI.MonoBehaviours.Core
ushort behaviourOrder = networkedObject.GetOrderIndex(this);
if (!networkedObject.targetMessageActions.ContainsKey(behaviourOrder))
- networkedObject.targetMessageActions.Add(behaviourOrder, new Dictionary>());
+ networkedObject.targetMessageActions.Add(behaviourOrder, new Dictionary>());
if (networkedObject.targetMessageActions[behaviourOrder].ContainsKey(messageType))
{
Debug.LogWarning("MLAPI: Each NetworkedBehaviour can only register one callback per instance per message type");
@@ -368,7 +368,7 @@ namespace MLAPI.MonoBehaviours.Core
syncedVarHooks[fieldIndex].Invoke(this, null);
}
- internal void FlushToClient(int clientId)
+ internal void FlushToClient(uint clientId)
{
//This NetworkedBehaviour has no SyncVars
if (dirtyFields.Length == 0)
@@ -689,7 +689,7 @@ namespace MLAPI.MonoBehaviours.Core
Debug.LogWarning("MLAPI: Server can not send messages to server.");
return;
}
- NetworkingManager.singleton.Send(NetworkingManager.singleton.serverClientId, messageType, channelName, data);
+ NetworkingManager.singleton.Send(NetId.ServerNetId.GetClientId(), messageType, channelName, data);
}
///
@@ -722,7 +722,7 @@ namespace MLAPI.MonoBehaviours.Core
Debug.LogWarning("MLAPI: Server can not send messages to server.");
return;
}
- NetworkingManager.singleton.Send(NetworkingManager.singleton.serverClientId, messageType, channelName, data, networkId, networkedObject.GetOrderIndex(this));
+ NetworkingManager.singleton.Send(NetId.ServerNetId.GetClientId(), messageType, channelName, data, networkId, networkedObject.GetOrderIndex(this));
}
///
@@ -876,7 +876,7 @@ namespace MLAPI.MonoBehaviours.Core
/// User defined messageType
/// User defined channelName
/// The binary data to send
- protected void SendToClient(int clientId, string messageType, string channelName, byte[] data)
+ protected void SendToClient(uint clientId, string messageType, string channelName, byte[] data)
{
if (MessageManager.messageTypes[messageType] < 32)
{
@@ -911,7 +911,7 @@ namespace MLAPI.MonoBehaviours.Core
/// User defined messageType
/// User defined channelName
/// The binary data to send
- protected void SendToClientTarget(int clientId, string messageType, string channelName, byte[] data)
+ protected void SendToClientTarget(uint clientId, string messageType, string channelName, byte[] data)
{
if (MessageManager.messageTypes[messageType] < 32)
{
@@ -946,7 +946,7 @@ namespace MLAPI.MonoBehaviours.Core
/// User defined messageType
/// User defined channelName
/// The binary data to send
- protected void SendToClients(int[] clientIds, string messageType, string channelName, byte[] data)
+ protected void SendToClients(uint[] clientIds, string messageType, string channelName, byte[] data)
{
if (MessageManager.messageTypes[messageType] < 32)
{
@@ -981,7 +981,7 @@ namespace MLAPI.MonoBehaviours.Core
/// User defined messageType
/// User defined channelName
/// The binary data to send
- protected void SendToClientsTarget(int[] clientIds, string messageType, string channelName, byte[] data)
+ protected void SendToClientsTarget(uint[] clientIds, string messageType, string channelName, byte[] data)
{
if (MessageManager.messageTypes[messageType] < 32)
{
@@ -1016,7 +1016,7 @@ namespace MLAPI.MonoBehaviours.Core
/// User defined messageType
/// User defined channelName
/// The binary data to send
- protected void SendToClients(List clientIds, string messageType, string channelName, byte[] data)
+ protected void SendToClients(List clientIds, string messageType, string channelName, byte[] data)
{
if (MessageManager.messageTypes[messageType] < 32)
{
@@ -1051,7 +1051,7 @@ namespace MLAPI.MonoBehaviours.Core
/// User defined messageType
/// User defined channelName
/// The binary data to send
- protected void SendToClientsTarget(List clientIds, string messageType, string channelName, byte[] data)
+ protected void SendToClientsTarget(List clientIds, string messageType, string channelName, byte[] data)
{
if (MessageManager.messageTypes[messageType] < 32)
{
@@ -1074,7 +1074,7 @@ namespace MLAPI.MonoBehaviours.Core
/// User defined messageType
/// User defined channelName
/// The instance to send
- protected void SendToClientsTarget(List clientIds, string messageType, string channelName, T instance)
+ protected void SendToClientsTarget(List clientIds, string messageType, string channelName, T instance)
{
SendToClientsTarget(clientIds, messageType, channelName, BinarySerializer.Serialize(instance));
}
diff --git a/MLAPI/MonoBehaviours/Core/NetworkedObject.cs b/MLAPI/MonoBehaviours/Core/NetworkedObject.cs
index 305934a..402ee97 100644
--- a/MLAPI/MonoBehaviours/Core/NetworkedObject.cs
+++ b/MLAPI/MonoBehaviours/Core/NetworkedObject.cs
@@ -1,4 +1,5 @@
-using MLAPI.NetworkingManagerComponents.Core;
+using MLAPI.Data;
+using MLAPI.NetworkingManagerComponents.Core;
using System;
using System.Collections.Generic;
using UnityEngine;
@@ -25,14 +26,14 @@ namespace MLAPI.MonoBehaviours.Core
///
/// Gets the clientId of the owner of this NetworkedObject
///
- public int OwnerClientId
+ public uint OwnerClientId
{
get
{
return ownerClientId;
}
}
- internal int ownerClientId = -2;
+ internal uint ownerClientId = new NetId(0, 0, false, true).GetClientId();
///
/// The index of the prefab used to spawn this in the spawnablePrefabs list
///
@@ -89,7 +90,7 @@ namespace MLAPI.MonoBehaviours.Core
{
get
{
- return isPlayerObject && (OwnerClientId == NetworkingManager.singleton.MyClientId || (OwnerClientId == -1 && NetworkingManager.singleton.isHost));
+ return isPlayerObject && (OwnerClientId == NetworkingManager.singleton.MyClientId || (new NetId(ownerClientId).IsHost() && NetworkingManager.singleton.isHost));
}
}
///
@@ -99,7 +100,7 @@ namespace MLAPI.MonoBehaviours.Core
{
get
{
- return !isPlayerObject && (OwnerClientId == NetworkingManager.singleton.MyClientId || (OwnerClientId == -1 && NetworkingManager.singleton.isHost));
+ return !isPlayerObject && (OwnerClientId == NetworkingManager.singleton.MyClientId || (new NetId(ownerClientId).IsHost() && NetworkingManager.singleton.isHost));
}
}
@@ -134,7 +135,7 @@ namespace MLAPI.MonoBehaviours.Core
/// Spawns an object across the network with a given owner. Can only be called from server
///
/// The clientId to own the object
- public void SpawnWithOwnership(int clientId)
+ public void SpawnWithOwnership(uint clientId)
{
if (NetworkingManager.singleton != null)
SpawnManager.OnSpawnObject(this, clientId);
@@ -150,7 +151,7 @@ namespace MLAPI.MonoBehaviours.Core
/// Changes the owner of the object. Can only be called from server
///
/// The new owner clientId
- public void ChangeOwnership(int newOwnerClientId)
+ public void ChangeOwnership(uint newOwnerClientId)
{
SpawnManager.ChangeOwnership(NetworkId, newOwnerClientId);
}
@@ -214,7 +215,7 @@ namespace MLAPI.MonoBehaviours.Core
}
//Flushes all syncVars to client
- internal void FlushToClient(int clientId)
+ internal void FlushToClient(uint clientId)
{
for (int i = 0; i < childNetworkedBehaviours.Count; i++)
{
@@ -239,6 +240,6 @@ namespace MLAPI.MonoBehaviours.Core
}
//Key: behaviourOrderId, value key: messageType, value value callback
- internal Dictionary>> targetMessageActions = new Dictionary>>();
+ internal Dictionary>> targetMessageActions = new Dictionary>>();
}
}
diff --git a/MLAPI/MonoBehaviours/Core/NetworkingManager.cs b/MLAPI/MonoBehaviours/Core/NetworkingManager.cs
index b57fdb2..c6836f4 100644
--- a/MLAPI/MonoBehaviours/Core/NetworkingManager.cs
+++ b/MLAPI/MonoBehaviours/Core/NetworkingManager.cs
@@ -51,26 +51,26 @@ namespace MLAPI.MonoBehaviours.Core
///
/// The clientId the server calls the local client by, only valid for clients
///
- public int MyClientId
+ public uint MyClientId
{
get
{
return myClientId;
}
}
- internal int myClientId;
- internal Dictionary connectedClients;
+ internal uint myClientId;
+ internal Dictionary connectedClients;
///
/// Gets a dictionary of connected clients
///
- public Dictionary ConnectedClients
+ public Dictionary ConnectedClients
{
get
{
return connectedClients;
}
}
- internal HashSet pendingClients;
+ internal HashSet pendingClients;
internal bool _isServer;
internal bool _isClient;
///
@@ -107,7 +107,8 @@ namespace MLAPI.MonoBehaviours.Core
private bool isListening;
private byte[] messageBuffer;
- internal int serverClientId;
+ internal int serverConnectionId;
+ internal int serverHostId;
///
/// Gets if we are connected as a client
///
@@ -122,11 +123,11 @@ namespace MLAPI.MonoBehaviours.Core
///
/// The callback to invoke once a client connects
///
- public Action OnClientConnectedCallback = null;
+ public Action OnClientConnectedCallback = null;
///
/// The callback to invoke when a client disconnects
///
- public Action OnClientDisconnectCallback = null;
+ public Action OnClientDisconnectCallback = null;
///
/// The callback to invoke once the server is ready
///
@@ -134,14 +135,14 @@ namespace MLAPI.MonoBehaviours.Core
///
/// The callback to invoke during connection approval
///
- public Action> ConnectionApprovalCallback = null;
+ public Action> ConnectionApprovalCallback = null;
///
/// The current NetworkingConfiguration
///
public NetworkConfig NetworkConfig;
private EllipticDiffieHellman clientDiffieHellman;
- private Dictionary diffieHellmanPublicKeys;
+ private Dictionary diffieHellmanPublicKeys;
private byte[] clientAesKey;
public bool RegenerateRSAKeys = false;
@@ -195,13 +196,13 @@ namespace MLAPI.MonoBehaviours.Core
lastSendTickTime = 0;
lastEventTickTime = 0;
lastReceiveTickTime = 0;
- pendingClients = new HashSet();
- connectedClients = new Dictionary();
+ pendingClients = new HashSet();
+ connectedClients = new Dictionary();
messageBuffer = new byte[NetworkConfig.MessageBufferSize];
- diffieHellmanPublicKeys = new Dictionary();
+ diffieHellmanPublicKeys = new Dictionary();
MessageManager.channels = new Dictionary();
MessageManager.messageTypes = new Dictionary();
- MessageManager.messageCallbacks = new Dictionary>>();
+ MessageManager.messageCallbacks = new Dictionary>>();
MessageManager.messageHandlerCounter = new Dictionary();
MessageManager.releasedMessageHandlerCounters = new Dictionary>();
MessageManager.reverseChannels = new Dictionary();
@@ -213,10 +214,6 @@ namespace MLAPI.MonoBehaviours.Core
NetworkSceneManager.registeredSceneNames = new HashSet();
NetworkSceneManager.sceneIndexToString = new Dictionary();
NetworkSceneManager.sceneNameToIndex = new Dictionary();
- ClientIdManager.clientIdCounter = 0;
- ClientIdManager.clientIdToKey = new Dictionary();
- ClientIdManager.keyToClientId = new Dictionary();
- ClientIdManager.releasedClientIds = new Queue();
if (NetworkConfig.HandleObjectSpawning)
{
@@ -390,7 +387,11 @@ namespace MLAPI.MonoBehaviours.Core
}
}
HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections);
- hostId = NetworkTransport.AddHost(hostTopology, NetworkConfig.Port);
+ NetworkTransport.AddHost(hostTopology, NetworkConfig.Port);
+
+ if(NetworkConfig.UseWebsockets)
+ NetworkTransport.AddWebsocketHost(hostTopology, NetworkConfig.WebsocketsPort);
+
_isServer = true;
_isClient = false;
isListening = true;
@@ -413,12 +414,38 @@ namespace MLAPI.MonoBehaviours.Core
ConnectionConfig cConfig = Init();
HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections);
- hostId = NetworkTransport.AddHost(hostTopology, 0, null);
+ serverHostId = NetworkTransport.AddHost(hostTopology, 0, null);
_isServer = false;
_isClient = true;
isListening = true;
- serverClientId = NetworkTransport.Connect(hostId, NetworkConfig.Address, NetworkConfig.Port, 0, out error);
+ byte error;
+ serverConnectionId = NetworkTransport.Connect(serverHostId, NetworkConfig.Address, NetworkConfig.Port, 0, out error);
+ Debug.LogWarning("MLAPI: Connection failed: " + ((NetworkError)error).ToString());
+ }
+
+ ///
+ /// Starts a client with a given NetworkingConfiguration
+ ///
+ /// The NetworkingConfiguration to use
+ public void StartClientWebsocket()
+ {
+ if (isServer || isClient)
+ {
+ Debug.LogWarning("MLAPI: Cannot start client while an instance is already running");
+ return;
+ }
+
+ ConnectionConfig cConfig = Init();
+ HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections);
+ serverHostId = NetworkTransport.AddWebsocketHost(hostTopology, 0, null);
+
+ _isServer = false;
+ _isClient = true;
+ isListening = true;
+ byte error;
+ serverConnectionId = NetworkTransport.Connect(serverHostId, NetworkConfig.Address, NetworkConfig.WebsocketsPort, 0, out error);
+ Debug.LogWarning("MLAPI: Connection failed: " + ((NetworkError)error).ToString());
}
///
@@ -426,22 +453,32 @@ namespace MLAPI.MonoBehaviours.Core
///
public void StopServer()
{
- HashSet sentIds = new HashSet();
+ HashSet disconnectedIds = new HashSet();
//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 pair in connectedClients)
+ foreach (KeyValuePair pair in connectedClients)
{
- if(!sentIds.Contains(pair.Key))
+ if(!disconnectedIds.Contains(pair.Key))
{
- sentIds.Add(pair.Key);
- NetworkTransport.Disconnect(hostId, pair.Key, out error);
+ disconnectedIds.Add(pair.Key);
+ NetId netId = new NetId(pair.Key);
+ if (netId.IsHost())
+ continue;
+
+ byte error;
+ NetworkTransport.Disconnect(netId.HostId, netId.ConnectionId, out error);
}
}
- foreach (int clientId in pendingClients)
+ foreach (uint clientId in pendingClients)
{
- if (!sentIds.Contains(clientId))
+ if (!disconnectedIds.Contains(clientId))
{
- sentIds.Add(clientId);
- NetworkTransport.Disconnect(hostId, clientId, out error);
+ disconnectedIds.Add(clientId);
+ NetId netId = new NetId(clientId);
+ if (netId.IsHost())
+ continue;
+
+ byte error;
+ NetworkTransport.Disconnect(netId.HostId, netId.ConnectionId, out error);
}
}
_isServer = false;
@@ -465,7 +502,8 @@ namespace MLAPI.MonoBehaviours.Core
public void StopClient()
{
_isClient = false;
- NetworkTransport.Disconnect(hostId, serverClientId, out error);
+ byte error;
+ NetworkTransport.Disconnect(serverHostId, serverConnectionId, out error);
Shutdown();
}
@@ -489,14 +527,23 @@ namespace MLAPI.MonoBehaviours.Core
}
}
HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections);
- hostId = NetworkTransport.AddHost(hostTopology, NetworkConfig.Port, null);
+ NetworkTransport.AddHost(hostTopology, NetworkConfig.Port, null);
+ if (NetworkConfig.UseWebsockets)
+ NetworkTransport.AddWebsocketHost(hostTopology, NetworkConfig.WebsocketsPort);
+
_isServer = true;
_isClient = true;
isListening = true;
- connectedClients.Add(-1, new NetworkedClient() { ClientId = -1 });
+
+ NetId netId = new NetId(0, 0, true, false);
+ connectedClients.Add(netId.GetClientId(), new NetworkedClient()
+ {
+ ClientId = netId.GetClientId()
+ });
+
if(NetworkConfig.HandleObjectSpawning)
{
- SpawnManager.SpawnPlayerObject(-1, 0);
+ SpawnManager.SpawnPlayerObject(netId.GetClientId(), 0);
}
if (OnServerStarted != null)
@@ -534,11 +581,11 @@ namespace MLAPI.MonoBehaviours.Core
}
//Receive stuff
- internal int hostId;
- private int clientId;
- private int channelId;
- private int receivedSize;
- private byte error;
+ //internal int hostId;
+ //private int clientId;
+ //private int channelId;
+ //private int receivedSize;
+ //private byte error;
private float lastReceiveTickTime;
private float lastSendTickTime;
private float lastEventTickTime;
@@ -548,9 +595,14 @@ namespace MLAPI.MonoBehaviours.Core
{
if((Time.time - lastSendTickTime >= (1f / NetworkConfig.SendTickrate)) || NetworkConfig.SendTickrate <= 0)
{
- foreach (KeyValuePair pair in connectedClients)
+ foreach (KeyValuePair pair in connectedClients)
{
- NetworkTransport.SendQueuedMessages(hostId, pair.Key, out error);
+ NetId netId = new NetId(pair.Key);
+ if (netId.IsHost() || netId.IsInvalid())
+ continue;
+
+ byte error;
+ NetworkTransport.SendQueuedMessages(netId.HostId, netId.ConnectionId, out error);
}
lastSendTickTime = Time.time;
}
@@ -561,21 +613,27 @@ namespace MLAPI.MonoBehaviours.Core
do
{
processedEvents++;
- eventType = NetworkTransport.Receive(out hostId, out clientId, out channelId, messageBuffer, messageBuffer.Length, out receivedSize, out error);
+ int hostId;
+ int connectionId;
+ int channelId;
+ int receivedSize;
+ byte error;
+ eventType = NetworkTransport.Receive(out hostId, out connectionId, out channelId, messageBuffer, messageBuffer.Length, out receivedSize, out error);
+ NetId netId = new NetId((byte)hostId, (ushort)connectionId, false, false);
NetworkError networkError = (NetworkError)error;
if (networkError == NetworkError.Timeout)
{
//Client timed out.
if (isServer)
{
- OnClientDisconnect(clientId);
+ OnClientDisconnect(netId.GetClientId());
return;
}
else
_isClientConnected = false;
if (OnClientDisconnectCallback != null)
- OnClientDisconnectCallback.Invoke(clientId);
+ OnClientDisconnectCallback.Invoke(netId.GetClientId());
}
else if (networkError != NetworkError.Ok)
{
@@ -588,8 +646,8 @@ namespace MLAPI.MonoBehaviours.Core
case NetworkEventType.ConnectEvent:
if (isServer)
{
- pendingClients.Add(clientId);
- StartCoroutine(ApprovalTimeout(clientId));
+ pendingClients.Add(netId.GetClientId());
+ StartCoroutine(ApprovalTimeout(netId.GetClientId()));
}
else
{
@@ -622,21 +680,21 @@ namespace MLAPI.MonoBehaviours.Core
writer.Write(NetworkConfig.ConnectionData);
}
}
- Send(clientId, "MLAPI_CONNECTION_REQUEST", "MLAPI_INTERNAL", writeStream.GetBuffer(), null, null, true);
+ Send(netId.GetClientId(), "MLAPI_CONNECTION_REQUEST", "MLAPI_INTERNAL", writeStream.GetBuffer(), null, null, true);
}
}
break;
case NetworkEventType.DataEvent:
- HandleIncomingData(clientId, messageBuffer, channelId);
+ HandleIncomingData(netId.GetClientId(), messageBuffer, channelId);
break;
case NetworkEventType.DisconnectEvent:
if (isServer)
- OnClientDisconnect(clientId);
+ OnClientDisconnect(netId.GetClientId());
else
_isClientConnected = false;
if (OnClientDisconnectCallback != null)
- OnClientDisconnectCallback.Invoke(clientId);
+ OnClientDisconnectCallback.Invoke(netId.GetClientId());
break;
}
// Only do another iteration if: there are no more messages AND (there is no limit to max events or we have processed less than the maximum)
@@ -653,7 +711,7 @@ namespace MLAPI.MonoBehaviours.Core
}
}
- private IEnumerator ApprovalTimeout(int clientId)
+ private IEnumerator ApprovalTimeout(uint clientId)
{
float timeStarted = Time.time;
//We yield every frame incase a pending client disconnects and someone else gets its connection id
@@ -668,7 +726,7 @@ namespace MLAPI.MonoBehaviours.Core
}
}
- private void HandleIncomingData(int clientId, byte[] data, int channelId)
+ private void HandleIncomingData(uint clientId, byte[] data, int channelId)
{
using(MemoryStream readStream = new MemoryStream(data))
{
@@ -685,13 +743,13 @@ namespace MLAPI.MonoBehaviours.Core
}
bool isPassthrough = reader.ReadBoolean();
- int passthroughOrigin = 0;
- int passthroughTarget = 0;
+ uint passthroughOrigin = 0;
+ uint passthroughTarget = 0;
if (isPassthrough && isServer)
- passthroughTarget = reader.ReadInt32();
+ passthroughTarget = reader.ReadUInt32();
else if (isPassthrough && !isServer)
- passthroughOrigin = reader.ReadInt32();
+ passthroughOrigin = reader.ReadUInt32();
//Client tried to send a network message that was not the connection request before he was accepted.
@@ -766,7 +824,7 @@ namespace MLAPI.MonoBehaviours.Core
}
else
{
- foreach (KeyValuePair> pair in MessageManager.messageCallbacks[messageType])
+ foreach (KeyValuePair> pair in MessageManager.messageCallbacks[messageType])
{
if (isPassthrough)
pair.Value(passthroughOrigin, incommingData);
@@ -825,7 +883,7 @@ namespace MLAPI.MonoBehaviours.Core
{
using (BinaryReader messageReader = new BinaryReader(messageReadStream))
{
- myClientId = messageReader.ReadInt32();
+ myClientId = messageReader.ReadUInt32();
uint sceneIndex = 0;
if(NetworkConfig.EnableSceneSwitching)
{
@@ -858,17 +916,19 @@ namespace MLAPI.MonoBehaviours.Core
float netTime = messageReader.ReadSingle();
int remoteStamp = messageReader.ReadInt32();
- int msDelay = NetworkTransport.GetRemoteDelayTimeMS(hostId, clientId, remoteStamp, out error);
+ byte error;
+ NetId netId = new NetId(clientId);
+ int msDelay = NetworkTransport.GetRemoteDelayTimeMS(netId.HostId, netId.ConnectionId, remoteStamp, out error);
if ((NetworkError)error != NetworkError.Ok)
msDelay = 0;
networkTime = netTime + (msDelay / 1000f);
- connectedClients.Add(MyClientId, new NetworkedClient() { ClientId = MyClientId });
+ connectedClients.Add(clientId, new NetworkedClient() { ClientId = clientId });
int clientCount = messageReader.ReadInt32();
for (int i = 0; i < clientCount; i++)
{
- int conId = messageReader.ReadInt32();
- connectedClients.Add(conId, new NetworkedClient() { ClientId = conId });
+ uint _clientId = messageReader.ReadUInt32();
+ connectedClients.Add(_clientId, new NetworkedClient() { ClientId = _clientId });
}
if(NetworkConfig.HandleObjectSpawning)
{
@@ -878,7 +938,7 @@ namespace MLAPI.MonoBehaviours.Core
{
bool isPlayerObject = messageReader.ReadBoolean();
uint networkId = messageReader.ReadUInt32();
- int ownerId = messageReader.ReadInt32();
+ uint ownerId = messageReader.ReadUInt32();
int prefabId = messageReader.ReadInt32();
bool isActive = messageReader.ReadBoolean();
@@ -927,7 +987,7 @@ namespace MLAPI.MonoBehaviours.Core
{
bool isPlayerObject = messageReader.ReadBoolean();
uint networkId = messageReader.ReadUInt32();
- int ownerId = messageReader.ReadInt32();
+ uint ownerId = messageReader.ReadUInt32();
int prefabId = messageReader.ReadInt32();
float xPos = messageReader.ReadSingle();
@@ -951,7 +1011,7 @@ namespace MLAPI.MonoBehaviours.Core
}
else
{
- int ownerId = messageReader.ReadInt32();
+ uint ownerId = messageReader.ReadUInt32();
connectedClients.Add(ownerId, new NetworkedClient() { ClientId = ownerId });
}
}
@@ -967,7 +1027,7 @@ namespace MLAPI.MonoBehaviours.Core
{
using (BinaryReader messageReader = new BinaryReader(messageReadStream))
{
- int disconnectedClientId = messageReader.ReadInt32();
+ uint disconnectedClientId = messageReader.ReadUInt32();
OnClientDisconnect(disconnectedClientId);
}
}
@@ -1042,7 +1102,7 @@ namespace MLAPI.MonoBehaviours.Core
using (BinaryReader messageReader = new BinaryReader(messageReadStream))
{
uint netId = messageReader.ReadUInt32();
- int ownerClientId = messageReader.ReadInt32();
+ uint ownerClientId = messageReader.ReadUInt32();
if (SpawnManager.spawnedObjects[netId].OwnerClientId == MyClientId)
{
//We are current owner.
@@ -1168,9 +1228,10 @@ namespace MLAPI.MonoBehaviours.Core
}
#region SEND METHODS
- internal void PassthroughSend(int targetId, int sourceId, ushort messageType, int channelId, byte[] data, uint? networkId = null, ushort? orderId = null)
+ internal void PassthroughSend(uint targetId, uint sourceId, ushort messageType, int channelId, byte[] data, uint? networkId = null, ushort? orderId = null)
{
- if (isHost && targetId == -1)
+ NetId targetNetId = new NetId(targetId);
+ if (isHost && targetNetId.IsHost())
{
//Host trying to send data to it's own client
Debug.LogWarning("MLAPI: Send method got message aimed at server from the server?");
@@ -1209,25 +1270,28 @@ namespace MLAPI.MonoBehaviours.Core
writer.Write(data);
}
}
- NetworkTransport.QueueMessageForSending(hostId, targetId, channelId, stream.GetBuffer(), sizeOfStream, out error);
+
+ byte error;
+ NetworkTransport.QueueMessageForSending(targetNetId.HostId, targetNetId.ConnectionId, channelId, stream.GetBuffer(), sizeOfStream, out error);
}
}
- internal void Send(int clientId, string messageType, string channelName, byte[] data, uint? networkId = null, ushort? orderId = null, bool skipQueue = false)
+ internal void Send(uint clientId, string messageType, string channelName, byte[] data, uint? networkId = null, ushort? orderId = null, bool skipQueue = false)
{
- if(clientId == -1 && isHost)
+ NetId netId = new NetId(clientId);
+ if(isHost && netId.IsHost())
{
//Don't invoke the message on our own machine. Instant stack overflow.
Debug.LogWarning("MLAPI: Cannot send message to own client");
return;
}
- else if(clientId == -1)
+ else if(netId.IsHost())
{
//Client trying to send data to host
- clientId = serverClientId;
+ netId = new NetId((byte)serverHostId, (ushort)serverConnectionId, false, false);
}
- bool isPassthrough = (!isServer && clientId != serverClientId && NetworkConfig.AllowPassthroughMessages);
+ bool isPassthrough = (!isServer && clientId != NetId.ServerNetId.GetClientId() && NetworkConfig.AllowPassthroughMessages);
if (isPassthrough && !NetworkConfig.PassthroughMessageHashSet.Contains(MessageManager.messageTypes[messageType]))
{
Debug.LogWarning("MLAPI: The The MessageType " + messageType + " is not registered as an allowed passthrough message type.");
@@ -1276,23 +1340,25 @@ namespace MLAPI.MonoBehaviours.Core
writer.Write(data);
}
}
+ byte error;
if (isPassthrough)
- clientId = serverClientId;
+ netId = new NetId((byte)serverHostId, (ushort)serverConnectionId, false, false);
if (skipQueue)
- NetworkTransport.Send(hostId, clientId, MessageManager.channels[channelName], stream.GetBuffer(), sizeOfStream, out error);
+ NetworkTransport.Send(netId.HostId, netId.ConnectionId, MessageManager.channels[channelName], stream.GetBuffer(), sizeOfStream, out error);
else
- NetworkTransport.QueueMessageForSending(hostId, clientId, MessageManager.channels[channelName], stream.GetBuffer(), sizeOfStream, out error);
+ NetworkTransport.QueueMessageForSending(netId.HostId, netId.ConnectionId, MessageManager.channels[channelName], stream.GetBuffer(), sizeOfStream, out error);
}
}
- internal void Send(int[] clientIds, string messageType, string channelName, byte[] data, uint? networkId = null, ushort? orderId = null)
+ internal void Send(uint[] clientIds, string messageType, string channelName, byte[] data, uint? networkId = null, ushort? orderId = null)
{
if (NetworkConfig.EncryptedChannelsHashSet.Contains(channelName))
{
Debug.LogWarning("MLAPI: Cannot send messages over encrypted channel to multiple clients.");
return;
}
- int sizeOfStream = 6;
+
+ int sizeOfStream = 6;
if (networkId != null)
sizeOfStream += 4;
if (orderId != null)
@@ -1316,23 +1382,24 @@ namespace MLAPI.MonoBehaviours.Core
int channel = MessageManager.channels[channelName];
for (int i = 0; i < clientIds.Length; i++)
{
- int clientId = clientIds[i];
- if (isHost && clientId == -1)
+ NetId netId = new NetId(clientIds[i]);
+ if (isHost && netId.IsHost())
{
//Don't invoke the message on our own machine. Instant stack overflow.
continue;
}
- else if (clientId == -1)
+ else if (netId.IsHost())
{
//Client trying to send data to host
- clientId = serverClientId;
+ netId = new NetId((byte)serverHostId, (ushort)serverConnectionId, false, false);
}
- NetworkTransport.QueueMessageForSending(hostId, clientId, channel, stream.GetBuffer(), sizeOfStream, out error);
+ byte error;
+ NetworkTransport.QueueMessageForSending(netId.HostId, netId.ConnectionId, channel, stream.GetBuffer(), sizeOfStream, out error);
}
}
}
- internal void Send(List clientIds, string messageType, string channelName, byte[] data, uint? networkId = null, ushort? orderId = null)
+ internal void Send(List clientIds, string messageType, string channelName, byte[] data, uint? networkId = null, ushort? orderId = null)
{
if (NetworkConfig.EncryptedChannelsHashSet.Contains(channelName))
{
@@ -1365,18 +1432,19 @@ namespace MLAPI.MonoBehaviours.Core
int channel = MessageManager.channels[channelName];
for (int i = 0; i < clientIds.Count; i++)
{
- int clientId = clientIds[i];
- if (clientId == -1 && isHost)
+ NetId netId = new NetId(clientIds[i]);
+ if (isHost && netId.IsHost())
{
//Don't invoke the message on our own machine. Instant stack overflow.
continue;
}
- else if (clientId == -1)
+ else if (netId.IsHost())
{
//Client trying to send data to host
- clientId = serverClientId;
+ netId = new NetId((byte)serverHostId, (ushort)serverConnectionId, false, false);
}
- NetworkTransport.QueueMessageForSending(hostId, clientId, channel, stream.GetBuffer(), sizeOfStream, out error);
+ byte error;
+ NetworkTransport.QueueMessageForSending(netId.HostId, netId.ConnectionId, channel, stream.GetBuffer(), sizeOfStream, out error);
}
}
}
@@ -1412,26 +1480,26 @@ namespace MLAPI.MonoBehaviours.Core
writer.Write(data);
}
int channel = MessageManager.channels[channelName];
- foreach (KeyValuePair pair in connectedClients)
+ foreach (KeyValuePair pair in connectedClients)
{
- int clientId = pair.Key;
- if(isHost && pair.Key == -1)
+ NetId netId = new NetId(pair.Key);
+ if(isHost && netId.IsHost())
{
//Don't invoke the message on our own machine. Instant stack overflow.
continue;
}
- else if (clientId == -1)
+ else if (netId.IsHost())
{
//Client trying to send data to host
- clientId = serverClientId;
+ netId = new NetId((byte)serverHostId, (ushort)serverConnectionId, false, false);
}
- NetworkTransport.QueueMessageForSending(hostId, clientId, channel, stream.GetBuffer(), sizeOfStream, out error);
-
+ byte error;
+ NetworkTransport.QueueMessageForSending(netId.HostId, netId.ConnectionId, channel, stream.GetBuffer(), sizeOfStream, out error);
}
}
}
- internal void Send(string messageType, string channelName, byte[] data, int clientIdToIgnore, uint? networkId = null, ushort? orderId = null)
+ internal void Send(string messageType, string channelName, byte[] data, uint clientIdToIgnore, uint? networkId = null, ushort? orderId = null)
{
if (NetworkConfig.EncryptedChannels.Contains(channelName))
{
@@ -1462,28 +1530,30 @@ namespace MLAPI.MonoBehaviours.Core
writer.Write(data);
}
int channel = MessageManager.channels[channelName];
- foreach (KeyValuePair pair in connectedClients)
+ foreach (KeyValuePair pair in connectedClients)
{
if (pair.Key == clientIdToIgnore)
continue;
- int clientId = pair.Key;
- if (isHost && pair.Key == -1)
+
+ NetId netId = new NetId(pair.Key);
+ if (isHost && netId.IsHost())
{
//Don't invoke the message on our own machine. Instant stack overflow.
continue;
}
- else if (clientId == -1)
+ else if (netId.IsHost())
{
//Client trying to send data to host
- clientId = serverClientId;
+ netId = new NetId((byte)serverHostId, (ushort)serverConnectionId, false, false);
}
- NetworkTransport.QueueMessageForSending(hostId, clientId, channel, stream.GetBuffer(), sizeOfStream, out error);
+ byte error;
+ NetworkTransport.QueueMessageForSending(netId.HostId, netId.ConnectionId, channel, stream.GetBuffer(), sizeOfStream, out error);
}
}
}
#endregion
- private void DisconnectClient(int clientId)
+ private void DisconnectClient(uint clientId)
{
if (!isServer)
return;
@@ -1497,10 +1567,15 @@ namespace MLAPI.MonoBehaviours.Core
if (diffieHellmanPublicKeys.ContainsKey(clientId))
diffieHellmanPublicKeys.Remove(clientId);
- NetworkTransport.Disconnect(hostId, clientId, out error);
+ NetId netId = new NetId(clientId);
+ if (netId.IsHost() || netId.IsInvalid())
+ return;
+
+ byte error;
+ NetworkTransport.Disconnect(netId.HostId, netId.ConnectionId, out error);
}
- private void OnClientDisconnect(int clientId)
+ private void OnClientDisconnect(uint clientId)
{
if (pendingClients.Contains(clientId))
pendingClients.Remove(clientId);
@@ -1532,7 +1607,7 @@ namespace MLAPI.MonoBehaviours.Core
}
}
- private void HandleApproval(int clientId, bool approved)
+ private void HandleApproval(uint clientId, bool approved)
{
if(approved)
{
@@ -1625,12 +1700,12 @@ namespace MLAPI.MonoBehaviours.Core
writer.Write(NetworkTransport.GetNetworkTimestamp());
writer.Write(connectedClients.Count - 1);
- foreach (KeyValuePair item in connectedClients)
+ foreach (KeyValuePair item in connectedClients)
{
//Our own ID. Already added as the first one above
if (item.Key == clientId)
continue;
- writer.Write(item.Key); //Connection id
+ writer.Write(item.Key); //ClientId
}
if (NetworkConfig.HandleObjectSpawning)
{
@@ -1701,7 +1776,10 @@ namespace MLAPI.MonoBehaviours.Core
if (diffieHellmanPublicKeys.ContainsKey(clientId))
diffieHellmanPublicKeys.Remove(clientId);
- NetworkTransport.Disconnect(hostId, clientId, out error);
+ NetId netId = new NetId(clientId);
+
+ byte error;
+ NetworkTransport.Disconnect(netId.HostId, netId.ConnectionId, out error);
}
}
}
diff --git a/MLAPI/MonoBehaviours/Prototyping/NetworkedAnimator.cs b/MLAPI/MonoBehaviours/Prototyping/NetworkedAnimator.cs
index 73fa744..c968874 100644
--- a/MLAPI/MonoBehaviours/Prototyping/NetworkedAnimator.cs
+++ b/MLAPI/MonoBehaviours/Prototyping/NetworkedAnimator.cs
@@ -138,8 +138,8 @@ namespace MLAPI.MonoBehaviours.Prototyping
{
if(EnableProximity)
{
- List clientsInProximity = new List();
- foreach (KeyValuePair client in NetworkingManager.singleton.connectedClients)
+ List clientsInProximity = new List();
+ foreach (KeyValuePair client in NetworkingManager.singleton.connectedClients)
{
if (Vector3.Distance(transform.position, client.Value.PlayerObject.transform.position) <= ProximityRange)
clientsInProximity.Add(client.Key);
@@ -207,8 +207,8 @@ namespace MLAPI.MonoBehaviours.Prototyping
{
if (EnableProximity)
{
- List clientsInProximity = new List();
- foreach (KeyValuePair client in NetworkingManager.singleton.connectedClients)
+ List clientsInProximity = new List();
+ foreach (KeyValuePair client in NetworkingManager.singleton.connectedClients)
{
if (Vector3.Distance(transform.position, client.Value.PlayerObject.transform.position) <= ProximityRange)
clientsInProximity.Add(client.Key);
@@ -248,7 +248,7 @@ namespace MLAPI.MonoBehaviours.Prototyping
if (i == 5) param5 = p;
}
- private void HandleAnimMsg(int clientId, byte[] data)
+ private void HandleAnimMsg(uint clientId, byte[] data)
{
// usually transitions will be triggered by parameters, if not, play anims directly.
// NOTE: this plays "animations", not transitions, so any transitions will be skipped.
@@ -258,8 +258,8 @@ namespace MLAPI.MonoBehaviours.Prototyping
{
if (EnableProximity)
{
- List clientsInProximity = new List();
- foreach (KeyValuePair client in NetworkingManager.singleton.connectedClients)
+ List clientsInProximity = new List();
+ foreach (KeyValuePair client in NetworkingManager.singleton.connectedClients)
{
if (Vector3.Distance(transform.position, client.Value.PlayerObject.transform.position) <= ProximityRange)
clientsInProximity.Add(client.Key);
@@ -284,14 +284,14 @@ namespace MLAPI.MonoBehaviours.Prototyping
}
}
- private void HandleAnimParamsMsg(int clientId, byte[] data)
+ private void HandleAnimParamsMsg(uint clientId, byte[] data)
{
if (isServer)
{
if (EnableProximity)
{
- List clientsInProximity = new List();
- foreach (KeyValuePair client in NetworkingManager.singleton.connectedClients)
+ List clientsInProximity = new List();
+ foreach (KeyValuePair client in NetworkingManager.singleton.connectedClients)
{
if (Vector3.Distance(transform.position, client.Value.PlayerObject.transform.position) <= ProximityRange)
clientsInProximity.Add(client.Key);
@@ -310,14 +310,14 @@ namespace MLAPI.MonoBehaviours.Prototyping
}
}
- private void HandleAnimTriggerMsg(int clientId, byte[] data)
+ private void HandleAnimTriggerMsg(uint clientId, byte[] data)
{
if (isServer)
{
if (EnableProximity)
{
- List clientsInProximity = new List();
- foreach (KeyValuePair client in NetworkingManager.singleton.connectedClients)
+ List clientsInProximity = new List();
+ foreach (KeyValuePair client in NetworkingManager.singleton.connectedClients)
{
if (Vector3.Distance(transform.position, client.Value.PlayerObject.transform.position) <= ProximityRange)
clientsInProximity.Add(client.Key);
@@ -434,8 +434,8 @@ namespace MLAPI.MonoBehaviours.Prototyping
{
if (EnableProximity)
{
- List clientsInProximity = new List();
- foreach (KeyValuePair client in NetworkingManager.singleton.connectedClients)
+ List clientsInProximity = new List();
+ foreach (KeyValuePair client in NetworkingManager.singleton.connectedClients)
{
if (Vector3.Distance(transform.position, client.Value.PlayerObject.transform.position) <= ProximityRange)
clientsInProximity.Add(client.Key);
diff --git a/MLAPI/MonoBehaviours/Prototyping/NetworkedNavMeshAgent.cs b/MLAPI/MonoBehaviours/Prototyping/NetworkedNavMeshAgent.cs
index 3fee1c2..2958bc9 100644
--- a/MLAPI/MonoBehaviours/Prototyping/NetworkedNavMeshAgent.cs
+++ b/MLAPI/MonoBehaviours/Prototyping/NetworkedNavMeshAgent.cs
@@ -89,8 +89,8 @@ namespace MLAPI.MonoBehaviours.Prototyping
}
else
{
- List proximityClients = new List();
- foreach (KeyValuePair client in NetworkingManager.singleton.connectedClients)
+ List proximityClients = new List();
+ foreach (KeyValuePair client in NetworkingManager.singleton.connectedClients)
{
if (Vector3.Distance(client.Value.PlayerObject.transform.position, transform.position) <= ProximityRange)
proximityClients.Add(client.Key);
@@ -121,8 +121,8 @@ namespace MLAPI.MonoBehaviours.Prototyping
}
else
{
- List proximityClients = new List();
- foreach (KeyValuePair client in NetworkingManager.singleton.connectedClients)
+ List proximityClients = new List();
+ foreach (KeyValuePair client in NetworkingManager.singleton.connectedClients)
{
if (Vector3.Distance(client.Value.PlayerObject.transform.position, transform.position) <= ProximityRange)
proximityClients.Add(client.Key);
@@ -134,7 +134,7 @@ namespace MLAPI.MonoBehaviours.Prototyping
}
}
- private void OnNavMeshStateUpdate(int clientId, byte[] data)
+ private void OnNavMeshStateUpdate(uint clientId, byte[] data)
{
using (MemoryStream stream = new MemoryStream(data))
{
@@ -166,7 +166,7 @@ namespace MLAPI.MonoBehaviours.Prototyping
}
}
- private void OnNavMeshCorrectionUpdate(int clinetId, byte[] data)
+ private void OnNavMeshCorrectionUpdate(uint clientId, byte[] data)
{
using (MemoryStream stream = new MemoryStream(data))
{
diff --git a/MLAPI/MonoBehaviours/Prototyping/NetworkedTransform.cs b/MLAPI/MonoBehaviours/Prototyping/NetworkedTransform.cs
index a55fe0b..c453572 100644
--- a/MLAPI/MonoBehaviours/Prototyping/NetworkedTransform.cs
+++ b/MLAPI/MonoBehaviours/Prototyping/NetworkedTransform.cs
@@ -1,4 +1,5 @@
-using MLAPI.MonoBehaviours.Core;
+using MLAPI.Data;
+using MLAPI.MonoBehaviours.Core;
using System.IO;
using UnityEngine;
@@ -97,7 +98,7 @@ namespace MLAPI.MonoBehaviours.Prototyping
private void Update()
{
- if(isOwner || isLocalPlayer || (ownerClientId == -2 && isServer))
+ if(isOwner || isLocalPlayer || (new NetId(ownerClientId).IsInvalid() && isServer))
{
//We own the object OR we are server and the object is not owned by anyone OR we are the object.
if(Time.time - lastSendTime >= timeForLerp && (Vector3.Distance(transform.position, lastSentPos) > MinMeters || Quaternion.Angle(transform.rotation, lastSentRot) > MinDegrees))
@@ -141,7 +142,7 @@ namespace MLAPI.MonoBehaviours.Prototyping
}
}
- private void OnRecieveTransformFromServer(int clientId, byte[] data)
+ private void OnRecieveTransformFromServer(uint clientId, byte[] data)
{
using (MemoryStream stream = new MemoryStream(data))
{
@@ -162,9 +163,9 @@ namespace MLAPI.MonoBehaviours.Prototyping
}
}
- private void OnRecieveTransformFromClient(int clientId, byte[] data)
+ private void OnRecieveTransformFromClient(uint clientId, byte[] data)
{
- using(MemoryStream readStream = new MemoryStream(data))
+ using (MemoryStream readStream = new MemoryStream(data))
{
using(BinaryReader reader = new BinaryReader(readStream))
{
@@ -200,7 +201,8 @@ namespace MLAPI.MonoBehaviours.Prototyping
}
if(EnableProximity)
{
- for (int i = 0; i < NetworkingManager.singleton.connectedClients.Count; i++)
+ // For instead of Foreach?! TODO!!!
+ for (uint i = 0; i < NetworkingManager.singleton.connectedClients.Count; i++)
{
if (Vector3.Distance(NetworkingManager.singleton.connectedClients[i].PlayerObject.transform.position, transform.position) <= ProximityRange)
{
diff --git a/MLAPI/NetworkingManagerComponents/Core/ClientIdManager.cs b/MLAPI/NetworkingManagerComponents/Core/ClientIdManager.cs
deleted file mode 100644
index 57dab7c..0000000
--- a/MLAPI/NetworkingManagerComponents/Core/ClientIdManager.cs
+++ /dev/null
@@ -1,42 +0,0 @@
-using System.Collections.Generic;
-using MLAPI.Data;
-
-namespace MLAPI.NetworkingManagerComponents.Core
-{
- internal static class ClientIdManager
- {
- internal static int clientIdCounter;
- // Use a queue instead of stack to (hopefully) reduce the chance of a clientId being re taken to quickly.
- internal static Queue releasedClientIds;
- internal static Dictionary clientIdToKey;
- internal static Dictionary keyToClientId;
-
- internal static int GetClientId(int connectionId, int hostId)
- {
- int clientId;
- if (releasedClientIds.Count > 0)
- {
- clientId = releasedClientIds.Dequeue();
- }
- else
- {
- clientId = clientIdCounter;
- clientIdCounter++;
- }
- clientIdToKey.Add(clientId, new ClientIdKey(hostId, connectionId));
- keyToClientId.Add(new ClientIdKey(hostId, connectionId), clientId);
- return clientId;
- }
-
- internal static void ReleaseClientId(int clientId)
- {
- ClientIdKey key = clientIdToKey[clientId];
- if (clientIdToKey.ContainsKey(clientId))
- clientIdToKey.Remove(clientId);
- if (keyToClientId.ContainsKey(key))
- keyToClientId.Remove(key);
-
- releasedClientIds.Enqueue(clientId);
- }
- }
-}
diff --git a/MLAPI/NetworkingManagerComponents/Core/LagCompensationManager.cs b/MLAPI/NetworkingManagerComponents/Core/LagCompensationManager.cs
index a5b7643..2c878d7 100644
--- a/MLAPI/NetworkingManagerComponents/Core/LagCompensationManager.cs
+++ b/MLAPI/NetworkingManagerComponents/Core/LagCompensationManager.cs
@@ -1,4 +1,5 @@
-using MLAPI.MonoBehaviours.Core;
+using MLAPI.Data;
+using MLAPI.MonoBehaviours.Core;
using System;
using System.Collections.Generic;
using UnityEngine;
@@ -54,14 +55,15 @@ namespace MLAPI.NetworkingManagerComponents.Core
///
/// The clientId's RTT to use
/// The action to invoke when time is turned back
- public static void Simulate(int clientId, Action action)
+ public static void Simulate(uint clientId, Action action)
{
if (!NetworkingManager.singleton.isServer)
{
Debug.LogWarning("MLAPI: Lag compensation simulations are only to be ran on the server.");
return;
}
- float milisecondsDelay = NetworkTransport.GetCurrentRTT(NetworkingManager.singleton.hostId, clientId, out error) / 2f;
+ NetId netId = new NetId(clientId);
+ float milisecondsDelay = NetworkTransport.GetCurrentRTT(netId.HostId, netId.ConnectionId, out error) / 2f;
Simulate(milisecondsDelay * 1000f, action);
}
diff --git a/MLAPI/NetworkingManagerComponents/Core/MessageManager.cs b/MLAPI/NetworkingManagerComponents/Core/MessageManager.cs
index e78ae37..8ccf506 100644
--- a/MLAPI/NetworkingManagerComponents/Core/MessageManager.cs
+++ b/MLAPI/NetworkingManagerComponents/Core/MessageManager.cs
@@ -12,7 +12,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
internal static Dictionary messageTypes;
internal static Dictionary reverseMessageTypes;
- internal static Dictionary>> messageCallbacks;
+ internal static Dictionary>> messageCallbacks;
internal static Dictionary messageHandlerCounter;
internal static Dictionary> releasedMessageHandlerCounters;
@@ -25,7 +25,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
}
- internal static int AddIncomingMessageHandler(string name, Action action, uint networkId)
+ internal static int AddIncomingMessageHandler(string name, Action action, uint networkId)
{
if (messageTypes.ContainsKey(name))
{
@@ -56,7 +56,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
}
else
{
- messageCallbacks.Add(messageTypes[name], new Dictionary>());
+ messageCallbacks.Add(messageTypes[name], new Dictionary>());
messageHandlerCounter.Add(messageTypes[name], 1);
messageCallbacks[messageTypes[name]].Add(0, action);
return 0;
diff --git a/MLAPI/NetworkingManagerComponents/Core/SpawnManager.cs b/MLAPI/NetworkingManagerComponents/Core/SpawnManager.cs
index 3de21a8..beb1ec4 100644
--- a/MLAPI/NetworkingManagerComponents/Core/SpawnManager.cs
+++ b/MLAPI/NetworkingManagerComponents/Core/SpawnManager.cs
@@ -1,4 +1,5 @@
-using MLAPI.MonoBehaviours.Core;
+using MLAPI.Data;
+using MLAPI.MonoBehaviours.Core;
using System;
using System.Collections.Generic;
using System.IO;
@@ -36,19 +37,19 @@ namespace MLAPI.NetworkingManagerComponents.Core
{
NetworkedObject netObject = SpawnManager.spawnedObjects[netId];
NetworkingManager.singleton.connectedClients[netObject.OwnerClientId].OwnedObjects.RemoveAll(x => x.NetworkId == netId);
- netObject.ownerClientId = -2;
+ netObject.ownerClientId = new NetId(0, 0, false, true).GetClientId();
using (MemoryStream stream = new MemoryStream(8))
{
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write(netId);
- writer.Write(-2);
+ writer.Write(netObject.ownerClientId);
}
netManager.Send("MLAPI_CHANGE_OWNER", "MLAPI_INTERNAL", stream.GetBuffer());
}
}
- internal static void ChangeOwnership(uint netId, int clientId)
+ internal static void ChangeOwnership(uint netId, uint clientId)
{
NetworkedObject netObject = SpawnManager.spawnedObjects[netId];
NetworkingManager.singleton.connectedClients[netObject.OwnerClientId].OwnedObjects.RemoveAll(x => x.NetworkId == netId);
@@ -87,7 +88,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
}
}
- internal static GameObject SpawnObject(int spawnablePrefabIndex, uint networkId, int ownerId, Vector3 position, Quaternion rotation)
+ internal static GameObject SpawnObject(int spawnablePrefabIndex, uint networkId, uint ownerId, Vector3 position, Quaternion rotation)
{
if (spawnablePrefabIndex >= netManager.NetworkConfig.SpawnablePrefabs.Count)
return null;
@@ -117,7 +118,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
return go;
}
- internal static GameObject SpawnPlayerObject(int clientId, uint networkId)
+ internal static GameObject SpawnPlayerObject(uint clientId, uint networkId)
{
if (netManager.NetworkConfig.PlayerPrefab == null)
return null;
@@ -149,7 +150,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
{
if (!spawnedObjects.ContainsKey(networkId) || (netManager != null && !netManager.NetworkConfig.HandleObjectSpawning))
return;
- if (spawnedObjects[networkId].OwnerClientId > -2 && !spawnedObjects[networkId].isPlayerObject)
+ if (!new NetId(spawnedObjects[networkId].OwnerClientId).IsInvalid() && !spawnedObjects[networkId].isPlayerObject)
{
//Someone owns it.
NetworkingManager.singleton.connectedClients[spawnedObjects[networkId].OwnerClientId].OwnedObjects.RemoveAll(x => x.NetworkId == networkId);
@@ -168,7 +169,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
}
//If we are host, send to everyone except ourselves. Otherwise, send to all
if (netManager != null && netManager.isHost)
- netManager.Send("MLAPI_DESTROY_OBJECT", "MLAPI_INTERNAL", stream.GetBuffer(), -1);
+ netManager.Send("MLAPI_DESTROY_OBJECT", "MLAPI_INTERNAL", stream.GetBuffer(), new NetId(0, 0, true, false).GetClientId());
else
netManager.Send("MLAPI_DESTROY_OBJECT", "MLAPI_INTERNAL", stream.GetBuffer());
}
@@ -179,7 +180,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
spawnedObjects.Remove(networkId);
}
- internal static void OnSpawnObject(NetworkedObject netObject, int? clientOwnerId = null)
+ internal static void OnSpawnObject(NetworkedObject netObject, uint? clientOwnerId = null)
{
if (netObject.isSpawned)
{