Added Multihost / Websocket support
This commit is contained in:
parent
32d9437bb4
commit
fba648c31a
@ -1,52 +0,0 @@
|
|||||||
namespace MLAPI.Data
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// A struct representing a client. Contains a hostId and a connectionId.
|
|
||||||
/// </summary>
|
|
||||||
internal struct ClientIdKey
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// The NetworkTransport hostId
|
|
||||||
/// </summary>
|
|
||||||
internal readonly int hostId;
|
|
||||||
/// <summary>
|
|
||||||
/// The NetworkTransport connectionId
|
|
||||||
/// </summary>
|
|
||||||
internal readonly int connectionId;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates a new ClientIdKey
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="hostId">The NetworkTransport hostId</param>
|
|
||||||
/// <param name="connectionId">The NetworkTransport connectionId</param>
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
86
MLAPI/Data/NetId.cs
Normal file
86
MLAPI/Data/NetId.cs
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -128,6 +128,14 @@ namespace MLAPI.Data
|
|||||||
/// Wheter or not to enable scene switching
|
/// Wheter or not to enable scene switching
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool EnableSceneSwitching = false;
|
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;
|
private byte[] ConfigHash = null;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -12,7 +12,7 @@ namespace MLAPI.Data
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The Id of the NetworkedClient
|
/// The Id of the NetworkedClient
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int ClientId;
|
public uint ClientId;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The PlayerObject of the Client
|
/// The PlayerObject of the Client
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
20
MLAPI/GlobalSuppressions.cs
Normal file
20
MLAPI/GlobalSuppressions.cs
Normal file
@ -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})")]
|
@ -73,6 +73,7 @@
|
|||||||
<Compile Include="Data\NetworkConfig.cs" />
|
<Compile Include="Data\NetworkConfig.cs" />
|
||||||
<Compile Include="Data\NetworkPool.cs" />
|
<Compile Include="Data\NetworkPool.cs" />
|
||||||
<Compile Include="Data\TrackedPointData.cs" />
|
<Compile Include="Data\TrackedPointData.cs" />
|
||||||
|
<Compile Include="GlobalSuppressions.cs" />
|
||||||
<Compile Include="MonoBehaviours\Prototyping\NetworkedAnimator.cs" />
|
<Compile Include="MonoBehaviours\Prototyping\NetworkedAnimator.cs" />
|
||||||
<Compile Include="MonoBehaviours\Prototyping\NetworkedNavMeshAgent.cs" />
|
<Compile Include="MonoBehaviours\Prototyping\NetworkedNavMeshAgent.cs" />
|
||||||
<Compile Include="NetworkingManagerComponents\Binary\BinarySerializer.cs" />
|
<Compile Include="NetworkingManagerComponents\Binary\BinarySerializer.cs" />
|
||||||
@ -91,8 +92,7 @@
|
|||||||
<Compile Include="NetworkingManagerComponents\Core\NetworkSceneManager.cs" />
|
<Compile Include="NetworkingManagerComponents\Core\NetworkSceneManager.cs" />
|
||||||
<Compile Include="NetworkingManagerComponents\Core\SpawnManager.cs" />
|
<Compile Include="NetworkingManagerComponents\Core\SpawnManager.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="NetworkingManagerComponents\Core\ClientIdManager.cs" />
|
<Compile Include="Data\NetId.cs" />
|
||||||
<Compile Include="Data\ClientIdKey.cs" />
|
|
||||||
<Compile Include="NetworkingManagerComponents\Binary\MessageChunker.cs" />
|
<Compile Include="NetworkingManagerComponents\Binary\MessageChunker.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -98,7 +98,7 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the clientId that owns the NetworkedObject
|
/// Gets the clientId that owns the NetworkedObject
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int ownerClientId
|
public uint ownerClientId
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
@ -146,7 +146,7 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
/// <param name="name">The MessageType to register</param>
|
/// <param name="name">The MessageType to register</param>
|
||||||
/// <param name="action">The callback to get invoked whenever a message is received</param>
|
/// <param name="action">The callback to get invoked whenever a message is received</param>
|
||||||
/// <returns>HandlerId for the messageHandler that can be used to deregister the messageHandler</returns>
|
/// <returns>HandlerId for the messageHandler that can be used to deregister the messageHandler</returns>
|
||||||
protected int RegisterMessageHandler(string name, Action<int, byte[]> action)
|
protected int RegisterMessageHandler(string name, Action<uint, byte[]> action)
|
||||||
{
|
{
|
||||||
if (!MessageManager.messageTypes.ContainsKey(name))
|
if (!MessageManager.messageTypes.ContainsKey(name))
|
||||||
{
|
{
|
||||||
@ -157,7 +157,7 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
ushort behaviourOrder = networkedObject.GetOrderIndex(this);
|
ushort behaviourOrder = networkedObject.GetOrderIndex(this);
|
||||||
|
|
||||||
if (!networkedObject.targetMessageActions.ContainsKey(behaviourOrder))
|
if (!networkedObject.targetMessageActions.ContainsKey(behaviourOrder))
|
||||||
networkedObject.targetMessageActions.Add(behaviourOrder, new Dictionary<ushort, Action<int, byte[]>>());
|
networkedObject.targetMessageActions.Add(behaviourOrder, new Dictionary<ushort, Action<uint, byte[]>>());
|
||||||
if (networkedObject.targetMessageActions[behaviourOrder].ContainsKey(messageType))
|
if (networkedObject.targetMessageActions[behaviourOrder].ContainsKey(messageType))
|
||||||
{
|
{
|
||||||
Debug.LogWarning("MLAPI: Each NetworkedBehaviour can only register one callback per instance per message type");
|
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);
|
syncedVarHooks[fieldIndex].Invoke(this, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void FlushToClient(int clientId)
|
internal void FlushToClient(uint clientId)
|
||||||
{
|
{
|
||||||
//This NetworkedBehaviour has no SyncVars
|
//This NetworkedBehaviour has no SyncVars
|
||||||
if (dirtyFields.Length == 0)
|
if (dirtyFields.Length == 0)
|
||||||
@ -689,7 +689,7 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
Debug.LogWarning("MLAPI: Server can not send messages to server.");
|
Debug.LogWarning("MLAPI: Server can not send messages to server.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
NetworkingManager.singleton.Send(NetworkingManager.singleton.serverClientId, messageType, channelName, data);
|
NetworkingManager.singleton.Send(NetId.ServerNetId.GetClientId(), messageType, channelName, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -722,7 +722,7 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
Debug.LogWarning("MLAPI: Server can not send messages to server.");
|
Debug.LogWarning("MLAPI: Server can not send messages to server.");
|
||||||
return;
|
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -876,7 +876,7 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
/// <param name="messageType">User defined messageType</param>
|
/// <param name="messageType">User defined messageType</param>
|
||||||
/// <param name="channelName">User defined channelName</param>
|
/// <param name="channelName">User defined channelName</param>
|
||||||
/// <param name="data">The binary data to send</param>
|
/// <param name="data">The binary data to send</param>
|
||||||
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)
|
if (MessageManager.messageTypes[messageType] < 32)
|
||||||
{
|
{
|
||||||
@ -911,7 +911,7 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
/// <param name="messageType">User defined messageType</param>
|
/// <param name="messageType">User defined messageType</param>
|
||||||
/// <param name="channelName">User defined channelName</param>
|
/// <param name="channelName">User defined channelName</param>
|
||||||
/// <param name="data">The binary data to send</param>
|
/// <param name="data">The binary data to send</param>
|
||||||
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)
|
if (MessageManager.messageTypes[messageType] < 32)
|
||||||
{
|
{
|
||||||
@ -946,7 +946,7 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
/// <param name="messageType">User defined messageType</param>
|
/// <param name="messageType">User defined messageType</param>
|
||||||
/// <param name="channelName">User defined channelName</param>
|
/// <param name="channelName">User defined channelName</param>
|
||||||
/// <param name="data">The binary data to send</param>
|
/// <param name="data">The binary data to send</param>
|
||||||
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)
|
if (MessageManager.messageTypes[messageType] < 32)
|
||||||
{
|
{
|
||||||
@ -981,7 +981,7 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
/// <param name="messageType">User defined messageType</param>
|
/// <param name="messageType">User defined messageType</param>
|
||||||
/// <param name="channelName">User defined channelName</param>
|
/// <param name="channelName">User defined channelName</param>
|
||||||
/// <param name="data">The binary data to send</param>
|
/// <param name="data">The binary data to send</param>
|
||||||
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)
|
if (MessageManager.messageTypes[messageType] < 32)
|
||||||
{
|
{
|
||||||
@ -1016,7 +1016,7 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
/// <param name="messageType">User defined messageType</param>
|
/// <param name="messageType">User defined messageType</param>
|
||||||
/// <param name="channelName">User defined channelName</param>
|
/// <param name="channelName">User defined channelName</param>
|
||||||
/// <param name="data">The binary data to send</param>
|
/// <param name="data">The binary data to send</param>
|
||||||
protected void SendToClients(List<int> clientIds, string messageType, string channelName, byte[] data)
|
protected void SendToClients(List<uint> clientIds, string messageType, string channelName, byte[] data)
|
||||||
{
|
{
|
||||||
if (MessageManager.messageTypes[messageType] < 32)
|
if (MessageManager.messageTypes[messageType] < 32)
|
||||||
{
|
{
|
||||||
@ -1051,7 +1051,7 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
/// <param name="messageType">User defined messageType</param>
|
/// <param name="messageType">User defined messageType</param>
|
||||||
/// <param name="channelName">User defined channelName</param>
|
/// <param name="channelName">User defined channelName</param>
|
||||||
/// <param name="data">The binary data to send</param>
|
/// <param name="data">The binary data to send</param>
|
||||||
protected void SendToClientsTarget(List<int> clientIds, string messageType, string channelName, byte[] data)
|
protected void SendToClientsTarget(List<uint> clientIds, string messageType, string channelName, byte[] data)
|
||||||
{
|
{
|
||||||
if (MessageManager.messageTypes[messageType] < 32)
|
if (MessageManager.messageTypes[messageType] < 32)
|
||||||
{
|
{
|
||||||
@ -1074,7 +1074,7 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
/// <param name="messageType">User defined messageType</param>
|
/// <param name="messageType">User defined messageType</param>
|
||||||
/// <param name="channelName">User defined channelName</param>
|
/// <param name="channelName">User defined channelName</param>
|
||||||
/// <param name="instance">The instance to send</param>
|
/// <param name="instance">The instance to send</param>
|
||||||
protected void SendToClientsTarget<T>(List<int> clientIds, string messageType, string channelName, T instance)
|
protected void SendToClientsTarget<T>(List<uint> clientIds, string messageType, string channelName, T instance)
|
||||||
{
|
{
|
||||||
SendToClientsTarget(clientIds, messageType, channelName, BinarySerializer.Serialize<T>(instance));
|
SendToClientsTarget(clientIds, messageType, channelName, BinarySerializer.Serialize<T>(instance));
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using MLAPI.NetworkingManagerComponents.Core;
|
using MLAPI.Data;
|
||||||
|
using MLAPI.NetworkingManagerComponents.Core;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
@ -25,14 +26,14 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the clientId of the owner of this NetworkedObject
|
/// Gets the clientId of the owner of this NetworkedObject
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int OwnerClientId
|
public uint OwnerClientId
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return ownerClientId;
|
return ownerClientId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
internal int ownerClientId = -2;
|
internal uint ownerClientId = new NetId(0, 0, false, true).GetClientId();
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The index of the prefab used to spawn this in the spawnablePrefabs list
|
/// The index of the prefab used to spawn this in the spawnablePrefabs list
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -89,7 +90,7 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
{
|
{
|
||||||
get
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -99,7 +100,7 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
{
|
{
|
||||||
get
|
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
|
/// Spawns an object across the network with a given owner. Can only be called from server
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="clientId">The clientId to own the object</param>
|
/// <param name="clientId">The clientId to own the object</param>
|
||||||
public void SpawnWithOwnership(int clientId)
|
public void SpawnWithOwnership(uint clientId)
|
||||||
{
|
{
|
||||||
if (NetworkingManager.singleton != null)
|
if (NetworkingManager.singleton != null)
|
||||||
SpawnManager.OnSpawnObject(this, clientId);
|
SpawnManager.OnSpawnObject(this, clientId);
|
||||||
@ -150,7 +151,7 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
/// Changes the owner of the object. Can only be called from server
|
/// Changes the owner of the object. Can only be called from server
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="newOwnerClientId">The new owner clientId</param>
|
/// <param name="newOwnerClientId">The new owner clientId</param>
|
||||||
public void ChangeOwnership(int newOwnerClientId)
|
public void ChangeOwnership(uint newOwnerClientId)
|
||||||
{
|
{
|
||||||
SpawnManager.ChangeOwnership(NetworkId, newOwnerClientId);
|
SpawnManager.ChangeOwnership(NetworkId, newOwnerClientId);
|
||||||
}
|
}
|
||||||
@ -214,7 +215,7 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Flushes all syncVars to client
|
//Flushes all syncVars to client
|
||||||
internal void FlushToClient(int clientId)
|
internal void FlushToClient(uint clientId)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < childNetworkedBehaviours.Count; i++)
|
for (int i = 0; i < childNetworkedBehaviours.Count; i++)
|
||||||
{
|
{
|
||||||
@ -239,6 +240,6 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Key: behaviourOrderId, value key: messageType, value value callback
|
//Key: behaviourOrderId, value key: messageType, value value callback
|
||||||
internal Dictionary<ushort, Dictionary<ushort, Action<int, byte[]>>> targetMessageActions = new Dictionary<ushort, Dictionary<ushort, Action<int, byte[]>>>();
|
internal Dictionary<ushort, Dictionary<ushort, Action<uint, byte[]>>> targetMessageActions = new Dictionary<ushort, Dictionary<ushort, Action<uint, byte[]>>>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -51,26 +51,26 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The clientId the server calls the local client by, only valid for clients
|
/// The clientId the server calls the local client by, only valid for clients
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int MyClientId
|
public uint MyClientId
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return myClientId;
|
return myClientId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
internal int myClientId;
|
internal uint myClientId;
|
||||||
internal Dictionary<int, NetworkedClient> connectedClients;
|
internal Dictionary<uint, NetworkedClient> connectedClients;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a dictionary of connected clients
|
/// Gets a dictionary of connected clients
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Dictionary<int, NetworkedClient> ConnectedClients
|
public Dictionary<uint, NetworkedClient> ConnectedClients
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return connectedClients;
|
return connectedClients;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
internal HashSet<int> pendingClients;
|
internal HashSet<uint> pendingClients;
|
||||||
internal bool _isServer;
|
internal bool _isServer;
|
||||||
internal bool _isClient;
|
internal bool _isClient;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -107,7 +107,8 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
|
|
||||||
private bool isListening;
|
private bool isListening;
|
||||||
private byte[] messageBuffer;
|
private byte[] messageBuffer;
|
||||||
internal int serverClientId;
|
internal int serverConnectionId;
|
||||||
|
internal int serverHostId;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets if we are connected as a client
|
/// Gets if we are connected as a client
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -122,11 +123,11 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The callback to invoke once a client connects
|
/// The callback to invoke once a client connects
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Action<int> OnClientConnectedCallback = null;
|
public Action<uint> OnClientConnectedCallback = null;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The callback to invoke when a client disconnects
|
/// The callback to invoke when a client disconnects
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Action<int> OnClientDisconnectCallback = null;
|
public Action<uint> OnClientDisconnectCallback = null;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The callback to invoke once the server is ready
|
/// The callback to invoke once the server is ready
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -134,14 +135,14 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The callback to invoke during connection approval
|
/// The callback to invoke during connection approval
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Action<byte[], int, Action<int, bool>> ConnectionApprovalCallback = null;
|
public Action<byte[], uint, Action<uint, bool>> ConnectionApprovalCallback = null;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The current NetworkingConfiguration
|
/// The current NetworkingConfiguration
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public NetworkConfig NetworkConfig;
|
public NetworkConfig NetworkConfig;
|
||||||
|
|
||||||
private EllipticDiffieHellman clientDiffieHellman;
|
private EllipticDiffieHellman clientDiffieHellman;
|
||||||
private Dictionary<int, byte[]> diffieHellmanPublicKeys;
|
private Dictionary<uint, byte[]> diffieHellmanPublicKeys;
|
||||||
private byte[] clientAesKey;
|
private byte[] clientAesKey;
|
||||||
|
|
||||||
public bool RegenerateRSAKeys = false;
|
public bool RegenerateRSAKeys = false;
|
||||||
@ -195,13 +196,13 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
lastSendTickTime = 0;
|
lastSendTickTime = 0;
|
||||||
lastEventTickTime = 0;
|
lastEventTickTime = 0;
|
||||||
lastReceiveTickTime = 0;
|
lastReceiveTickTime = 0;
|
||||||
pendingClients = new HashSet<int>();
|
pendingClients = new HashSet<uint>();
|
||||||
connectedClients = new Dictionary<int, NetworkedClient>();
|
connectedClients = new Dictionary<uint, NetworkedClient>();
|
||||||
messageBuffer = new byte[NetworkConfig.MessageBufferSize];
|
messageBuffer = new byte[NetworkConfig.MessageBufferSize];
|
||||||
diffieHellmanPublicKeys = new Dictionary<int, byte[]>();
|
diffieHellmanPublicKeys = new Dictionary<uint, byte[]>();
|
||||||
MessageManager.channels = new Dictionary<string, int>();
|
MessageManager.channels = new Dictionary<string, int>();
|
||||||
MessageManager.messageTypes = new Dictionary<string, ushort>();
|
MessageManager.messageTypes = new Dictionary<string, ushort>();
|
||||||
MessageManager.messageCallbacks = new Dictionary<ushort, Dictionary<int, Action<int, byte[]>>>();
|
MessageManager.messageCallbacks = new Dictionary<ushort, Dictionary<int, Action<uint, byte[]>>>();
|
||||||
MessageManager.messageHandlerCounter = new Dictionary<ushort, int>();
|
MessageManager.messageHandlerCounter = new Dictionary<ushort, int>();
|
||||||
MessageManager.releasedMessageHandlerCounters = new Dictionary<ushort, Stack<int>>();
|
MessageManager.releasedMessageHandlerCounters = new Dictionary<ushort, Stack<int>>();
|
||||||
MessageManager.reverseChannels = new Dictionary<int, string>();
|
MessageManager.reverseChannels = new Dictionary<int, string>();
|
||||||
@ -213,10 +214,6 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
NetworkSceneManager.registeredSceneNames = new HashSet<string>();
|
NetworkSceneManager.registeredSceneNames = new HashSet<string>();
|
||||||
NetworkSceneManager.sceneIndexToString = new Dictionary<uint, string>();
|
NetworkSceneManager.sceneIndexToString = new Dictionary<uint, string>();
|
||||||
NetworkSceneManager.sceneNameToIndex = new Dictionary<string, uint>();
|
NetworkSceneManager.sceneNameToIndex = new Dictionary<string, uint>();
|
||||||
ClientIdManager.clientIdCounter = 0;
|
|
||||||
ClientIdManager.clientIdToKey = new Dictionary<int, ClientIdKey>();
|
|
||||||
ClientIdManager.keyToClientId = new Dictionary<ClientIdKey, int>();
|
|
||||||
ClientIdManager.releasedClientIds = new Queue<int>();
|
|
||||||
|
|
||||||
if (NetworkConfig.HandleObjectSpawning)
|
if (NetworkConfig.HandleObjectSpawning)
|
||||||
{
|
{
|
||||||
@ -390,7 +387,11 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections);
|
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;
|
_isServer = true;
|
||||||
_isClient = false;
|
_isClient = false;
|
||||||
isListening = true;
|
isListening = true;
|
||||||
@ -413,12 +414,38 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
|
|
||||||
ConnectionConfig cConfig = Init();
|
ConnectionConfig cConfig = Init();
|
||||||
HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections);
|
HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections);
|
||||||
hostId = NetworkTransport.AddHost(hostTopology, 0, null);
|
serverHostId = NetworkTransport.AddHost(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);
|
byte error;
|
||||||
|
serverConnectionId = NetworkTransport.Connect(serverHostId, NetworkConfig.Address, NetworkConfig.Port, 0, out error);
|
||||||
|
Debug.LogWarning("MLAPI: Connection failed: " + ((NetworkError)error).ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Starts a client with a given NetworkingConfiguration
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="netConfig">The NetworkingConfiguration to use</param>
|
||||||
|
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());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -426,22 +453,32 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void StopServer()
|
public void StopServer()
|
||||||
{
|
{
|
||||||
HashSet<int> sentIds = new HashSet<int>();
|
HashSet<uint> disconnectedIds = new HashSet<uint>();
|
||||||
//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<uint, NetworkedClient> pair in connectedClients)
|
||||||
{
|
{
|
||||||
if(!sentIds.Contains(pair.Key))
|
if(!disconnectedIds.Contains(pair.Key))
|
||||||
{
|
{
|
||||||
sentIds.Add(pair.Key);
|
disconnectedIds.Add(pair.Key);
|
||||||
NetworkTransport.Disconnect(hostId, pair.Key, out error);
|
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);
|
disconnectedIds.Add(clientId);
|
||||||
NetworkTransport.Disconnect(hostId, clientId, out error);
|
NetId netId = new NetId(clientId);
|
||||||
|
if (netId.IsHost())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
byte error;
|
||||||
|
NetworkTransport.Disconnect(netId.HostId, netId.ConnectionId, out error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_isServer = false;
|
_isServer = false;
|
||||||
@ -465,7 +502,8 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
public void StopClient()
|
public void StopClient()
|
||||||
{
|
{
|
||||||
_isClient = false;
|
_isClient = false;
|
||||||
NetworkTransport.Disconnect(hostId, serverClientId, out error);
|
byte error;
|
||||||
|
NetworkTransport.Disconnect(serverHostId, serverConnectionId, out error);
|
||||||
Shutdown();
|
Shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -489,14 +527,23 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
HostTopology hostTopology = new HostTopology(cConfig, NetworkConfig.MaxConnections);
|
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;
|
_isServer = true;
|
||||||
_isClient = true;
|
_isClient = true;
|
||||||
isListening = 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)
|
if(NetworkConfig.HandleObjectSpawning)
|
||||||
{
|
{
|
||||||
SpawnManager.SpawnPlayerObject(-1, 0);
|
SpawnManager.SpawnPlayerObject(netId.GetClientId(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (OnServerStarted != null)
|
if (OnServerStarted != null)
|
||||||
@ -534,11 +581,11 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Receive stuff
|
//Receive stuff
|
||||||
internal int hostId;
|
//internal int hostId;
|
||||||
private int clientId;
|
//private int clientId;
|
||||||
private int channelId;
|
//private int channelId;
|
||||||
private int receivedSize;
|
//private int receivedSize;
|
||||||
private byte error;
|
//private byte error;
|
||||||
private float lastReceiveTickTime;
|
private float lastReceiveTickTime;
|
||||||
private float lastSendTickTime;
|
private float lastSendTickTime;
|
||||||
private float lastEventTickTime;
|
private float lastEventTickTime;
|
||||||
@ -548,9 +595,14 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
{
|
{
|
||||||
if((Time.time - lastSendTickTime >= (1f / NetworkConfig.SendTickrate)) || NetworkConfig.SendTickrate <= 0)
|
if((Time.time - lastSendTickTime >= (1f / NetworkConfig.SendTickrate)) || NetworkConfig.SendTickrate <= 0)
|
||||||
{
|
{
|
||||||
foreach (KeyValuePair<int, NetworkedClient> pair in connectedClients)
|
foreach (KeyValuePair<uint, NetworkedClient> 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;
|
lastSendTickTime = Time.time;
|
||||||
}
|
}
|
||||||
@ -561,21 +613,27 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
do
|
do
|
||||||
{
|
{
|
||||||
processedEvents++;
|
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;
|
NetworkError networkError = (NetworkError)error;
|
||||||
if (networkError == NetworkError.Timeout)
|
if (networkError == NetworkError.Timeout)
|
||||||
{
|
{
|
||||||
//Client timed out.
|
//Client timed out.
|
||||||
if (isServer)
|
if (isServer)
|
||||||
{
|
{
|
||||||
OnClientDisconnect(clientId);
|
OnClientDisconnect(netId.GetClientId());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
_isClientConnected = false;
|
_isClientConnected = false;
|
||||||
|
|
||||||
if (OnClientDisconnectCallback != null)
|
if (OnClientDisconnectCallback != null)
|
||||||
OnClientDisconnectCallback.Invoke(clientId);
|
OnClientDisconnectCallback.Invoke(netId.GetClientId());
|
||||||
}
|
}
|
||||||
else if (networkError != NetworkError.Ok)
|
else if (networkError != NetworkError.Ok)
|
||||||
{
|
{
|
||||||
@ -588,8 +646,8 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
case NetworkEventType.ConnectEvent:
|
case NetworkEventType.ConnectEvent:
|
||||||
if (isServer)
|
if (isServer)
|
||||||
{
|
{
|
||||||
pendingClients.Add(clientId);
|
pendingClients.Add(netId.GetClientId());
|
||||||
StartCoroutine(ApprovalTimeout(clientId));
|
StartCoroutine(ApprovalTimeout(netId.GetClientId()));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -622,21 +680,21 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
writer.Write(NetworkConfig.ConnectionData);
|
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;
|
break;
|
||||||
case NetworkEventType.DataEvent:
|
case NetworkEventType.DataEvent:
|
||||||
HandleIncomingData(clientId, messageBuffer, channelId);
|
HandleIncomingData(netId.GetClientId(), messageBuffer, channelId);
|
||||||
break;
|
break;
|
||||||
case NetworkEventType.DisconnectEvent:
|
case NetworkEventType.DisconnectEvent:
|
||||||
if (isServer)
|
if (isServer)
|
||||||
OnClientDisconnect(clientId);
|
OnClientDisconnect(netId.GetClientId());
|
||||||
else
|
else
|
||||||
_isClientConnected = false;
|
_isClientConnected = false;
|
||||||
|
|
||||||
if (OnClientDisconnectCallback != null)
|
if (OnClientDisconnectCallback != null)
|
||||||
OnClientDisconnectCallback.Invoke(clientId);
|
OnClientDisconnectCallback.Invoke(netId.GetClientId());
|
||||||
break;
|
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)
|
// 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;
|
float timeStarted = Time.time;
|
||||||
//We yield every frame incase a pending client disconnects and someone else gets its connection id
|
//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))
|
using(MemoryStream readStream = new MemoryStream(data))
|
||||||
{
|
{
|
||||||
@ -685,13 +743,13 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
}
|
}
|
||||||
bool isPassthrough = reader.ReadBoolean();
|
bool isPassthrough = reader.ReadBoolean();
|
||||||
|
|
||||||
int passthroughOrigin = 0;
|
uint passthroughOrigin = 0;
|
||||||
int passthroughTarget = 0;
|
uint passthroughTarget = 0;
|
||||||
|
|
||||||
if (isPassthrough && isServer)
|
if (isPassthrough && isServer)
|
||||||
passthroughTarget = reader.ReadInt32();
|
passthroughTarget = reader.ReadUInt32();
|
||||||
else if (isPassthrough && !isServer)
|
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.
|
//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
|
else
|
||||||
{
|
{
|
||||||
foreach (KeyValuePair<int, Action<int, byte[]>> pair in MessageManager.messageCallbacks[messageType])
|
foreach (KeyValuePair<int, Action<uint, byte[]>> pair in MessageManager.messageCallbacks[messageType])
|
||||||
{
|
{
|
||||||
if (isPassthrough)
|
if (isPassthrough)
|
||||||
pair.Value(passthroughOrigin, incommingData);
|
pair.Value(passthroughOrigin, incommingData);
|
||||||
@ -825,7 +883,7 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
{
|
{
|
||||||
using (BinaryReader messageReader = new BinaryReader(messageReadStream))
|
using (BinaryReader messageReader = new BinaryReader(messageReadStream))
|
||||||
{
|
{
|
||||||
myClientId = messageReader.ReadInt32();
|
myClientId = messageReader.ReadUInt32();
|
||||||
uint sceneIndex = 0;
|
uint sceneIndex = 0;
|
||||||
if(NetworkConfig.EnableSceneSwitching)
|
if(NetworkConfig.EnableSceneSwitching)
|
||||||
{
|
{
|
||||||
@ -858,17 +916,19 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
|
|
||||||
float netTime = messageReader.ReadSingle();
|
float netTime = messageReader.ReadSingle();
|
||||||
int remoteStamp = messageReader.ReadInt32();
|
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)
|
if ((NetworkError)error != NetworkError.Ok)
|
||||||
msDelay = 0;
|
msDelay = 0;
|
||||||
networkTime = netTime + (msDelay / 1000f);
|
networkTime = netTime + (msDelay / 1000f);
|
||||||
|
|
||||||
connectedClients.Add(MyClientId, new NetworkedClient() { ClientId = MyClientId });
|
connectedClients.Add(clientId, new NetworkedClient() { ClientId = clientId });
|
||||||
int clientCount = messageReader.ReadInt32();
|
int clientCount = messageReader.ReadInt32();
|
||||||
for (int i = 0; i < clientCount; i++)
|
for (int i = 0; i < clientCount; i++)
|
||||||
{
|
{
|
||||||
int conId = messageReader.ReadInt32();
|
uint _clientId = messageReader.ReadUInt32();
|
||||||
connectedClients.Add(conId, new NetworkedClient() { ClientId = conId });
|
connectedClients.Add(_clientId, new NetworkedClient() { ClientId = _clientId });
|
||||||
}
|
}
|
||||||
if(NetworkConfig.HandleObjectSpawning)
|
if(NetworkConfig.HandleObjectSpawning)
|
||||||
{
|
{
|
||||||
@ -878,7 +938,7 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
{
|
{
|
||||||
bool isPlayerObject = messageReader.ReadBoolean();
|
bool isPlayerObject = messageReader.ReadBoolean();
|
||||||
uint networkId = messageReader.ReadUInt32();
|
uint networkId = messageReader.ReadUInt32();
|
||||||
int ownerId = messageReader.ReadInt32();
|
uint ownerId = messageReader.ReadUInt32();
|
||||||
int prefabId = messageReader.ReadInt32();
|
int prefabId = messageReader.ReadInt32();
|
||||||
bool isActive = messageReader.ReadBoolean();
|
bool isActive = messageReader.ReadBoolean();
|
||||||
|
|
||||||
@ -927,7 +987,7 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
{
|
{
|
||||||
bool isPlayerObject = messageReader.ReadBoolean();
|
bool isPlayerObject = messageReader.ReadBoolean();
|
||||||
uint networkId = messageReader.ReadUInt32();
|
uint networkId = messageReader.ReadUInt32();
|
||||||
int ownerId = messageReader.ReadInt32();
|
uint ownerId = messageReader.ReadUInt32();
|
||||||
int prefabId = messageReader.ReadInt32();
|
int prefabId = messageReader.ReadInt32();
|
||||||
|
|
||||||
float xPos = messageReader.ReadSingle();
|
float xPos = messageReader.ReadSingle();
|
||||||
@ -951,7 +1011,7 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
int ownerId = messageReader.ReadInt32();
|
uint ownerId = messageReader.ReadUInt32();
|
||||||
connectedClients.Add(ownerId, new NetworkedClient() { ClientId = ownerId });
|
connectedClients.Add(ownerId, new NetworkedClient() { ClientId = ownerId });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -967,7 +1027,7 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
{
|
{
|
||||||
using (BinaryReader messageReader = new BinaryReader(messageReadStream))
|
using (BinaryReader messageReader = new BinaryReader(messageReadStream))
|
||||||
{
|
{
|
||||||
int disconnectedClientId = messageReader.ReadInt32();
|
uint disconnectedClientId = messageReader.ReadUInt32();
|
||||||
OnClientDisconnect(disconnectedClientId);
|
OnClientDisconnect(disconnectedClientId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1042,7 +1102,7 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
using (BinaryReader messageReader = new BinaryReader(messageReadStream))
|
using (BinaryReader messageReader = new BinaryReader(messageReadStream))
|
||||||
{
|
{
|
||||||
uint netId = messageReader.ReadUInt32();
|
uint netId = messageReader.ReadUInt32();
|
||||||
int ownerClientId = messageReader.ReadInt32();
|
uint ownerClientId = messageReader.ReadUInt32();
|
||||||
if (SpawnManager.spawnedObjects[netId].OwnerClientId == MyClientId)
|
if (SpawnManager.spawnedObjects[netId].OwnerClientId == MyClientId)
|
||||||
{
|
{
|
||||||
//We are current owner.
|
//We are current owner.
|
||||||
@ -1168,9 +1228,10 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
}
|
}
|
||||||
|
|
||||||
#region SEND METHODS
|
#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
|
//Host trying to send data to it's own client
|
||||||
Debug.LogWarning("MLAPI: Send method got message aimed at server from the server?");
|
Debug.LogWarning("MLAPI: Send method got message aimed at server from the server?");
|
||||||
@ -1209,25 +1270,28 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
writer.Write(data);
|
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.
|
//Don't invoke the message on our own machine. Instant stack overflow.
|
||||||
Debug.LogWarning("MLAPI: Cannot send message to own client");
|
Debug.LogWarning("MLAPI: Cannot send message to own client");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else if(clientId == -1)
|
else if(netId.IsHost())
|
||||||
{
|
{
|
||||||
//Client trying to send data to host
|
//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]))
|
if (isPassthrough && !NetworkConfig.PassthroughMessageHashSet.Contains(MessageManager.messageTypes[messageType]))
|
||||||
{
|
{
|
||||||
Debug.LogWarning("MLAPI: The The MessageType " + messageType + " is not registered as an allowed passthrough message type.");
|
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);
|
writer.Write(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
byte error;
|
||||||
if (isPassthrough)
|
if (isPassthrough)
|
||||||
clientId = serverClientId;
|
netId = new NetId((byte)serverHostId, (ushort)serverConnectionId, false, false);
|
||||||
if (skipQueue)
|
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
|
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))
|
if (NetworkConfig.EncryptedChannelsHashSet.Contains(channelName))
|
||||||
{
|
{
|
||||||
Debug.LogWarning("MLAPI: Cannot send messages over encrypted channel to multiple clients.");
|
Debug.LogWarning("MLAPI: Cannot send messages over encrypted channel to multiple clients.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int sizeOfStream = 6;
|
|
||||||
|
int sizeOfStream = 6;
|
||||||
if (networkId != null)
|
if (networkId != null)
|
||||||
sizeOfStream += 4;
|
sizeOfStream += 4;
|
||||||
if (orderId != null)
|
if (orderId != null)
|
||||||
@ -1316,23 +1382,24 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
int channel = MessageManager.channels[channelName];
|
int channel = MessageManager.channels[channelName];
|
||||||
for (int i = 0; i < clientIds.Length; i++)
|
for (int i = 0; i < clientIds.Length; i++)
|
||||||
{
|
{
|
||||||
int clientId = clientIds[i];
|
NetId netId = new NetId(clientIds[i]);
|
||||||
if (isHost && clientId == -1)
|
if (isHost && netId.IsHost())
|
||||||
{
|
{
|
||||||
//Don't invoke the message on our own machine. Instant stack overflow.
|
//Don't invoke the message on our own machine. Instant stack overflow.
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
else if (clientId == -1)
|
else if (netId.IsHost())
|
||||||
{
|
{
|
||||||
//Client trying to send data to host
|
//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<int> clientIds, string messageType, string channelName, byte[] data, uint? networkId = null, ushort? orderId = null)
|
internal void Send(List<uint> clientIds, string messageType, string channelName, byte[] data, uint? networkId = null, ushort? orderId = null)
|
||||||
{
|
{
|
||||||
if (NetworkConfig.EncryptedChannelsHashSet.Contains(channelName))
|
if (NetworkConfig.EncryptedChannelsHashSet.Contains(channelName))
|
||||||
{
|
{
|
||||||
@ -1365,18 +1432,19 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
int channel = MessageManager.channels[channelName];
|
int channel = MessageManager.channels[channelName];
|
||||||
for (int i = 0; i < clientIds.Count; i++)
|
for (int i = 0; i < clientIds.Count; i++)
|
||||||
{
|
{
|
||||||
int clientId = clientIds[i];
|
NetId netId = new NetId(clientIds[i]);
|
||||||
if (clientId == -1 && isHost)
|
if (isHost && netId.IsHost())
|
||||||
{
|
{
|
||||||
//Don't invoke the message on our own machine. Instant stack overflow.
|
//Don't invoke the message on our own machine. Instant stack overflow.
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
else if (clientId == -1)
|
else if (netId.IsHost())
|
||||||
{
|
{
|
||||||
//Client trying to send data to host
|
//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);
|
writer.Write(data);
|
||||||
}
|
}
|
||||||
int channel = MessageManager.channels[channelName];
|
int channel = MessageManager.channels[channelName];
|
||||||
foreach (KeyValuePair<int, NetworkedClient> pair in connectedClients)
|
foreach (KeyValuePair<uint, NetworkedClient> pair in connectedClients)
|
||||||
{
|
{
|
||||||
int clientId = pair.Key;
|
NetId netId = new NetId(pair.Key);
|
||||||
if(isHost && pair.Key == -1)
|
if(isHost && netId.IsHost())
|
||||||
{
|
{
|
||||||
//Don't invoke the message on our own machine. Instant stack overflow.
|
//Don't invoke the message on our own machine. Instant stack overflow.
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
else if (clientId == -1)
|
else if (netId.IsHost())
|
||||||
{
|
{
|
||||||
//Client trying to send data to host
|
//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))
|
if (NetworkConfig.EncryptedChannels.Contains(channelName))
|
||||||
{
|
{
|
||||||
@ -1462,28 +1530,30 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
writer.Write(data);
|
writer.Write(data);
|
||||||
}
|
}
|
||||||
int channel = MessageManager.channels[channelName];
|
int channel = MessageManager.channels[channelName];
|
||||||
foreach (KeyValuePair<int, NetworkedClient> pair in connectedClients)
|
foreach (KeyValuePair<uint, NetworkedClient> pair in connectedClients)
|
||||||
{
|
{
|
||||||
if (pair.Key == clientIdToIgnore)
|
if (pair.Key == clientIdToIgnore)
|
||||||
continue;
|
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.
|
//Don't invoke the message on our own machine. Instant stack overflow.
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
else if (clientId == -1)
|
else if (netId.IsHost())
|
||||||
{
|
{
|
||||||
//Client trying to send data to host
|
//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
|
#endregion
|
||||||
|
|
||||||
private void DisconnectClient(int clientId)
|
private void DisconnectClient(uint clientId)
|
||||||
{
|
{
|
||||||
if (!isServer)
|
if (!isServer)
|
||||||
return;
|
return;
|
||||||
@ -1497,10 +1567,15 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
if (diffieHellmanPublicKeys.ContainsKey(clientId))
|
if (diffieHellmanPublicKeys.ContainsKey(clientId))
|
||||||
diffieHellmanPublicKeys.Remove(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))
|
if (pendingClients.Contains(clientId))
|
||||||
pendingClients.Remove(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)
|
if(approved)
|
||||||
{
|
{
|
||||||
@ -1625,12 +1700,12 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
writer.Write(NetworkTransport.GetNetworkTimestamp());
|
writer.Write(NetworkTransport.GetNetworkTimestamp());
|
||||||
|
|
||||||
writer.Write(connectedClients.Count - 1);
|
writer.Write(connectedClients.Count - 1);
|
||||||
foreach (KeyValuePair<int, NetworkedClient> item in connectedClients)
|
foreach (KeyValuePair<uint, NetworkedClient> item in connectedClients)
|
||||||
{
|
{
|
||||||
//Our own ID. Already added as the first one above
|
//Our own ID. Already added as the first one above
|
||||||
if (item.Key == clientId)
|
if (item.Key == clientId)
|
||||||
continue;
|
continue;
|
||||||
writer.Write(item.Key); //Connection id
|
writer.Write(item.Key); //ClientId
|
||||||
}
|
}
|
||||||
if (NetworkConfig.HandleObjectSpawning)
|
if (NetworkConfig.HandleObjectSpawning)
|
||||||
{
|
{
|
||||||
@ -1701,7 +1776,10 @@ namespace MLAPI.MonoBehaviours.Core
|
|||||||
if (diffieHellmanPublicKeys.ContainsKey(clientId))
|
if (diffieHellmanPublicKeys.ContainsKey(clientId))
|
||||||
diffieHellmanPublicKeys.Remove(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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -138,8 +138,8 @@ namespace MLAPI.MonoBehaviours.Prototyping
|
|||||||
{
|
{
|
||||||
if(EnableProximity)
|
if(EnableProximity)
|
||||||
{
|
{
|
||||||
List<int> clientsInProximity = new List<int>();
|
List<uint> clientsInProximity = new List<uint>();
|
||||||
foreach (KeyValuePair<int, NetworkedClient> client in NetworkingManager.singleton.connectedClients)
|
foreach (KeyValuePair<uint, NetworkedClient> client in NetworkingManager.singleton.connectedClients)
|
||||||
{
|
{
|
||||||
if (Vector3.Distance(transform.position, client.Value.PlayerObject.transform.position) <= ProximityRange)
|
if (Vector3.Distance(transform.position, client.Value.PlayerObject.transform.position) <= ProximityRange)
|
||||||
clientsInProximity.Add(client.Key);
|
clientsInProximity.Add(client.Key);
|
||||||
@ -207,8 +207,8 @@ namespace MLAPI.MonoBehaviours.Prototyping
|
|||||||
{
|
{
|
||||||
if (EnableProximity)
|
if (EnableProximity)
|
||||||
{
|
{
|
||||||
List<int> clientsInProximity = new List<int>();
|
List<uint> clientsInProximity = new List<uint>();
|
||||||
foreach (KeyValuePair<int, NetworkedClient> client in NetworkingManager.singleton.connectedClients)
|
foreach (KeyValuePair<uint, NetworkedClient> client in NetworkingManager.singleton.connectedClients)
|
||||||
{
|
{
|
||||||
if (Vector3.Distance(transform.position, client.Value.PlayerObject.transform.position) <= ProximityRange)
|
if (Vector3.Distance(transform.position, client.Value.PlayerObject.transform.position) <= ProximityRange)
|
||||||
clientsInProximity.Add(client.Key);
|
clientsInProximity.Add(client.Key);
|
||||||
@ -248,7 +248,7 @@ namespace MLAPI.MonoBehaviours.Prototyping
|
|||||||
if (i == 5) param5 = p;
|
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.
|
// usually transitions will be triggered by parameters, if not, play anims directly.
|
||||||
// NOTE: this plays "animations", not transitions, so any transitions will be skipped.
|
// NOTE: this plays "animations", not transitions, so any transitions will be skipped.
|
||||||
@ -258,8 +258,8 @@ namespace MLAPI.MonoBehaviours.Prototyping
|
|||||||
{
|
{
|
||||||
if (EnableProximity)
|
if (EnableProximity)
|
||||||
{
|
{
|
||||||
List<int> clientsInProximity = new List<int>();
|
List<uint> clientsInProximity = new List<uint>();
|
||||||
foreach (KeyValuePair<int, NetworkedClient> client in NetworkingManager.singleton.connectedClients)
|
foreach (KeyValuePair<uint, NetworkedClient> client in NetworkingManager.singleton.connectedClients)
|
||||||
{
|
{
|
||||||
if (Vector3.Distance(transform.position, client.Value.PlayerObject.transform.position) <= ProximityRange)
|
if (Vector3.Distance(transform.position, client.Value.PlayerObject.transform.position) <= ProximityRange)
|
||||||
clientsInProximity.Add(client.Key);
|
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 (isServer)
|
||||||
{
|
{
|
||||||
if (EnableProximity)
|
if (EnableProximity)
|
||||||
{
|
{
|
||||||
List<int> clientsInProximity = new List<int>();
|
List<uint> clientsInProximity = new List<uint>();
|
||||||
foreach (KeyValuePair<int, NetworkedClient> client in NetworkingManager.singleton.connectedClients)
|
foreach (KeyValuePair<uint, NetworkedClient> client in NetworkingManager.singleton.connectedClients)
|
||||||
{
|
{
|
||||||
if (Vector3.Distance(transform.position, client.Value.PlayerObject.transform.position) <= ProximityRange)
|
if (Vector3.Distance(transform.position, client.Value.PlayerObject.transform.position) <= ProximityRange)
|
||||||
clientsInProximity.Add(client.Key);
|
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 (isServer)
|
||||||
{
|
{
|
||||||
if (EnableProximity)
|
if (EnableProximity)
|
||||||
{
|
{
|
||||||
List<int> clientsInProximity = new List<int>();
|
List<uint> clientsInProximity = new List<uint>();
|
||||||
foreach (KeyValuePair<int, NetworkedClient> client in NetworkingManager.singleton.connectedClients)
|
foreach (KeyValuePair<uint, NetworkedClient> client in NetworkingManager.singleton.connectedClients)
|
||||||
{
|
{
|
||||||
if (Vector3.Distance(transform.position, client.Value.PlayerObject.transform.position) <= ProximityRange)
|
if (Vector3.Distance(transform.position, client.Value.PlayerObject.transform.position) <= ProximityRange)
|
||||||
clientsInProximity.Add(client.Key);
|
clientsInProximity.Add(client.Key);
|
||||||
@ -434,8 +434,8 @@ namespace MLAPI.MonoBehaviours.Prototyping
|
|||||||
{
|
{
|
||||||
if (EnableProximity)
|
if (EnableProximity)
|
||||||
{
|
{
|
||||||
List<int> clientsInProximity = new List<int>();
|
List<uint> clientsInProximity = new List<uint>();
|
||||||
foreach (KeyValuePair<int, NetworkedClient> client in NetworkingManager.singleton.connectedClients)
|
foreach (KeyValuePair<uint, NetworkedClient> client in NetworkingManager.singleton.connectedClients)
|
||||||
{
|
{
|
||||||
if (Vector3.Distance(transform.position, client.Value.PlayerObject.transform.position) <= ProximityRange)
|
if (Vector3.Distance(transform.position, client.Value.PlayerObject.transform.position) <= ProximityRange)
|
||||||
clientsInProximity.Add(client.Key);
|
clientsInProximity.Add(client.Key);
|
||||||
|
@ -89,8 +89,8 @@ namespace MLAPI.MonoBehaviours.Prototyping
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
List<int> proximityClients = new List<int>();
|
List<uint> proximityClients = new List<uint>();
|
||||||
foreach (KeyValuePair<int, NetworkedClient> client in NetworkingManager.singleton.connectedClients)
|
foreach (KeyValuePair<uint, NetworkedClient> client in NetworkingManager.singleton.connectedClients)
|
||||||
{
|
{
|
||||||
if (Vector3.Distance(client.Value.PlayerObject.transform.position, transform.position) <= ProximityRange)
|
if (Vector3.Distance(client.Value.PlayerObject.transform.position, transform.position) <= ProximityRange)
|
||||||
proximityClients.Add(client.Key);
|
proximityClients.Add(client.Key);
|
||||||
@ -121,8 +121,8 @@ namespace MLAPI.MonoBehaviours.Prototyping
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
List<int> proximityClients = new List<int>();
|
List<uint> proximityClients = new List<uint>();
|
||||||
foreach (KeyValuePair<int, NetworkedClient> client in NetworkingManager.singleton.connectedClients)
|
foreach (KeyValuePair<uint, NetworkedClient> client in NetworkingManager.singleton.connectedClients)
|
||||||
{
|
{
|
||||||
if (Vector3.Distance(client.Value.PlayerObject.transform.position, transform.position) <= ProximityRange)
|
if (Vector3.Distance(client.Value.PlayerObject.transform.position, transform.position) <= ProximityRange)
|
||||||
proximityClients.Add(client.Key);
|
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))
|
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))
|
using (MemoryStream stream = new MemoryStream(data))
|
||||||
{
|
{
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using MLAPI.MonoBehaviours.Core;
|
using MLAPI.Data;
|
||||||
|
using MLAPI.MonoBehaviours.Core;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
@ -97,7 +98,7 @@ namespace MLAPI.MonoBehaviours.Prototyping
|
|||||||
|
|
||||||
private void Update()
|
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.
|
//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))
|
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))
|
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))
|
using(BinaryReader reader = new BinaryReader(readStream))
|
||||||
{
|
{
|
||||||
@ -200,7 +201,8 @@ namespace MLAPI.MonoBehaviours.Prototyping
|
|||||||
}
|
}
|
||||||
if(EnableProximity)
|
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)
|
if (Vector3.Distance(NetworkingManager.singleton.connectedClients[i].PlayerObject.transform.position, transform.position) <= ProximityRange)
|
||||||
{
|
{
|
||||||
|
@ -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<int> releasedClientIds;
|
|
||||||
internal static Dictionary<int, ClientIdKey> clientIdToKey;
|
|
||||||
internal static Dictionary<ClientIdKey, int> 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,4 +1,5 @@
|
|||||||
using MLAPI.MonoBehaviours.Core;
|
using MLAPI.Data;
|
||||||
|
using MLAPI.MonoBehaviours.Core;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
@ -54,14 +55,15 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="clientId">The clientId's RTT to use</param>
|
/// <param name="clientId">The clientId's RTT to use</param>
|
||||||
/// <param name="action">The action to invoke when time is turned back</param>
|
/// <param name="action">The action to invoke when time is turned back</param>
|
||||||
public static void Simulate(int clientId, Action action)
|
public static void Simulate(uint clientId, Action action)
|
||||||
{
|
{
|
||||||
if (!NetworkingManager.singleton.isServer)
|
if (!NetworkingManager.singleton.isServer)
|
||||||
{
|
{
|
||||||
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;
|
NetId netId = new NetId(clientId);
|
||||||
|
float milisecondsDelay = NetworkTransport.GetCurrentRTT(netId.HostId, netId.ConnectionId, out error) / 2f;
|
||||||
Simulate(milisecondsDelay * 1000f, action);
|
Simulate(milisecondsDelay * 1000f, action);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
|||||||
internal static Dictionary<string, ushort> messageTypes;
|
internal static Dictionary<string, ushort> messageTypes;
|
||||||
internal static Dictionary<ushort, string> reverseMessageTypes;
|
internal static Dictionary<ushort, string> reverseMessageTypes;
|
||||||
|
|
||||||
internal static Dictionary<ushort, Dictionary<int, Action<int, byte[]>>> messageCallbacks;
|
internal static Dictionary<ushort, Dictionary<int, Action<uint, byte[]>>> messageCallbacks;
|
||||||
internal static Dictionary<ushort, int> messageHandlerCounter;
|
internal static Dictionary<ushort, int> messageHandlerCounter;
|
||||||
internal static Dictionary<ushort, Stack<int>> releasedMessageHandlerCounters;
|
internal static Dictionary<ushort, Stack<int>> releasedMessageHandlerCounters;
|
||||||
|
|
||||||
@ -25,7 +25,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
internal static int AddIncomingMessageHandler(string name, Action<int, byte[]> action, uint networkId)
|
internal static int AddIncomingMessageHandler(string name, Action<uint, byte[]> action, uint networkId)
|
||||||
{
|
{
|
||||||
if (messageTypes.ContainsKey(name))
|
if (messageTypes.ContainsKey(name))
|
||||||
{
|
{
|
||||||
@ -56,7 +56,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
messageCallbacks.Add(messageTypes[name], new Dictionary<int, Action<int, byte[]>>());
|
messageCallbacks.Add(messageTypes[name], new Dictionary<int, Action<uint, byte[]>>());
|
||||||
messageHandlerCounter.Add(messageTypes[name], 1);
|
messageHandlerCounter.Add(messageTypes[name], 1);
|
||||||
messageCallbacks[messageTypes[name]].Add(0, action);
|
messageCallbacks[messageTypes[name]].Add(0, action);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using MLAPI.MonoBehaviours.Core;
|
using MLAPI.Data;
|
||||||
|
using MLAPI.MonoBehaviours.Core;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
@ -36,19 +37,19 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
|||||||
{
|
{
|
||||||
NetworkedObject netObject = SpawnManager.spawnedObjects[netId];
|
NetworkedObject netObject = SpawnManager.spawnedObjects[netId];
|
||||||
NetworkingManager.singleton.connectedClients[netObject.OwnerClientId].OwnedObjects.RemoveAll(x => x.NetworkId == 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 (MemoryStream stream = new MemoryStream(8))
|
||||||
{
|
{
|
||||||
using (BinaryWriter writer = new BinaryWriter(stream))
|
using (BinaryWriter writer = new BinaryWriter(stream))
|
||||||
{
|
{
|
||||||
writer.Write(netId);
|
writer.Write(netId);
|
||||||
writer.Write(-2);
|
writer.Write(netObject.ownerClientId);
|
||||||
}
|
}
|
||||||
netManager.Send("MLAPI_CHANGE_OWNER", "MLAPI_INTERNAL", stream.GetBuffer());
|
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];
|
NetworkedObject netObject = SpawnManager.spawnedObjects[netId];
|
||||||
NetworkingManager.singleton.connectedClients[netObject.OwnerClientId].OwnedObjects.RemoveAll(x => x.NetworkId == 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)
|
if (spawnablePrefabIndex >= netManager.NetworkConfig.SpawnablePrefabs.Count)
|
||||||
return null;
|
return null;
|
||||||
@ -117,7 +118,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
|||||||
return go;
|
return go;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static GameObject SpawnPlayerObject(int clientId, uint networkId)
|
internal static GameObject SpawnPlayerObject(uint clientId, uint networkId)
|
||||||
{
|
{
|
||||||
if (netManager.NetworkConfig.PlayerPrefab == null)
|
if (netManager.NetworkConfig.PlayerPrefab == null)
|
||||||
return null;
|
return null;
|
||||||
@ -149,7 +150,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
|||||||
{
|
{
|
||||||
if (!spawnedObjects.ContainsKey(networkId) || (netManager != null && !netManager.NetworkConfig.HandleObjectSpawning))
|
if (!spawnedObjects.ContainsKey(networkId) || (netManager != null && !netManager.NetworkConfig.HandleObjectSpawning))
|
||||||
return;
|
return;
|
||||||
if (spawnedObjects[networkId].OwnerClientId > -2 && !spawnedObjects[networkId].isPlayerObject)
|
if (!new NetId(spawnedObjects[networkId].OwnerClientId).IsInvalid() && !spawnedObjects[networkId].isPlayerObject)
|
||||||
{
|
{
|
||||||
//Someone owns it.
|
//Someone owns it.
|
||||||
NetworkingManager.singleton.connectedClients[spawnedObjects[networkId].OwnerClientId].OwnedObjects.RemoveAll(x => x.NetworkId == networkId);
|
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 we are host, send to everyone except ourselves. Otherwise, send to all
|
||||||
if (netManager != null && netManager.isHost)
|
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
|
else
|
||||||
netManager.Send("MLAPI_DESTROY_OBJECT", "MLAPI_INTERNAL", stream.GetBuffer());
|
netManager.Send("MLAPI_DESTROY_OBJECT", "MLAPI_INTERNAL", stream.GetBuffer());
|
||||||
}
|
}
|
||||||
@ -179,7 +180,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
|||||||
spawnedObjects.Remove(networkId);
|
spawnedObjects.Remove(networkId);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static void OnSpawnObject(NetworkedObject netObject, int? clientOwnerId = null)
|
internal static void OnSpawnObject(NetworkedObject netObject, uint? clientOwnerId = null)
|
||||||
{
|
{
|
||||||
if (netObject.isSpawned)
|
if (netObject.isSpawned)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user