Skip to content

Commit fba648c

Browse files
committed
Added Multihost / Websocket support
1 parent 32d9437 commit fba648c

16 files changed

+377
-273
lines changed

MLAPI/Data/ClientIdKey.cs

Lines changed: 0 additions & 52 deletions
This file was deleted.

MLAPI/Data/NetId.cs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using MLAPI.MonoBehaviours.Core;
2+
using System;
3+
4+
namespace MLAPI.Data
5+
{
6+
public struct NetId
7+
{
8+
public byte HostId;
9+
public ushort ConnectionId;
10+
public byte Meta;
11+
12+
public bool IsHost()
13+
{
14+
return Meta == 1;
15+
}
16+
17+
public bool IsInvalid()
18+
{
19+
return Meta == 2;
20+
}
21+
22+
public static NetId ServerNetId
23+
{
24+
get
25+
{
26+
return new NetId((byte)NetworkingManager.singleton.serverHostId, (ushort)NetworkingManager.singleton.serverConnectionId, false, false);
27+
}
28+
}
29+
30+
public NetId(byte hostId, ushort connectionId, bool isHost, bool isInvalid)
31+
{
32+
HostId = hostId;
33+
ConnectionId = connectionId;
34+
if (isHost)
35+
Meta = 1;
36+
else if (isInvalid)
37+
Meta = 2;
38+
else
39+
Meta = 0;
40+
}
41+
42+
43+
public NetId(uint clientId)
44+
{
45+
byte[] bytes = BitConverter.GetBytes(clientId);
46+
HostId = bytes[0];
47+
ConnectionId = BitConverter.ToUInt16(bytes, 1);
48+
Meta = bytes[3];
49+
}
50+
51+
public uint GetClientId()
52+
{
53+
byte[] bytes = new byte[4];
54+
byte[] connIdBytes = BitConverter.GetBytes(ConnectionId);
55+
bytes[0] = HostId;
56+
bytes[1] = connIdBytes[0];
57+
bytes[2] = connIdBytes[1];
58+
bytes[3] = Meta;
59+
return BitConverter.ToUInt32(bytes, 0);
60+
}
61+
62+
public override bool Equals (object obj)
63+
{
64+
if (obj == null || GetType() != obj.GetType())
65+
return false;
66+
67+
NetId key = (NetId)obj;
68+
return (HostId == key.HostId) && (ConnectionId == key.ConnectionId);
69+
}
70+
71+
public override int GetHashCode()
72+
{
73+
return (int)GetClientId();
74+
}
75+
76+
public static bool operator ==(NetId client1, NetId client2)
77+
{
78+
return (client1.HostId == client2.HostId && client1.ConnectionId == client2.ConnectionId) || (client1.IsHost() == client2.IsHost());
79+
}
80+
81+
public static bool operator !=(NetId client1, NetId client2)
82+
{
83+
return !(client1 == client2);
84+
}
85+
}
86+
}

MLAPI/Data/NetworkConfig.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,14 @@ public class NetworkConfig
128128
/// Wheter or not to enable scene switching
129129
/// </summary>
130130
public bool EnableSceneSwitching = false;
131+
/// <summary>
132+
/// Wheter or not we should have an additional host that listens for WebSocket requests
133+
/// </summary>
134+
public bool UseWebsockets = false;
135+
/// <summary>
136+
/// The port the websocket host listens on
137+
/// </summary>
138+
public int WebsocketsPort = 7778;
131139

132140
private byte[] ConfigHash = null;
133141
/// <summary>

MLAPI/Data/NetworkedClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class NetworkedClient
1212
/// <summary>
1313
/// The Id of the NetworkedClient
1414
/// </summary>
15-
public int ClientId;
15+
public uint ClientId;
1616
/// <summary>
1717
/// The PlayerObject of the Client
1818
/// </summary>

MLAPI/GlobalSuppressions.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+

2+
// This file is used by Code Analysis to maintain SuppressMessage
3+
// attributes that are applied to this project.
4+
// Project-level suppressions either have no target or are given
5+
// a specific target and scoped to a namespace, type, member, etc.
6+
7+
[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")]
8+
9+
[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")]
10+
[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")]
11+
[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")]
12+
[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")]
13+
[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)")]
14+
[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})")]
15+
[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})")]
16+
[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)")]
17+
[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)")]
18+
[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})")]
19+
[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})")]
20+
[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})")]

MLAPI/MLAPI.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
<Compile Include="Data\NetworkConfig.cs" />
7474
<Compile Include="Data\NetworkPool.cs" />
7575
<Compile Include="Data\TrackedPointData.cs" />
76+
<Compile Include="GlobalSuppressions.cs" />
7677
<Compile Include="MonoBehaviours\Prototyping\NetworkedAnimator.cs" />
7778
<Compile Include="MonoBehaviours\Prototyping\NetworkedNavMeshAgent.cs" />
7879
<Compile Include="NetworkingManagerComponents\Binary\BinarySerializer.cs" />
@@ -91,8 +92,7 @@
9192
<Compile Include="NetworkingManagerComponents\Core\NetworkSceneManager.cs" />
9293
<Compile Include="NetworkingManagerComponents\Core\SpawnManager.cs" />
9394
<Compile Include="Properties\AssemblyInfo.cs" />
94-
<Compile Include="NetworkingManagerComponents\Core\ClientIdManager.cs" />
95-
<Compile Include="Data\ClientIdKey.cs" />
95+
<Compile Include="Data\NetId.cs" />
9696
<Compile Include="NetworkingManagerComponents\Binary\MessageChunker.cs" />
9797
</ItemGroup>
9898
<ItemGroup>

MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public uint networkId
9898
/// <summary>
9999
/// Gets the clientId that owns the NetworkedObject
100100
/// </summary>
101-
public int ownerClientId
101+
public uint ownerClientId
102102
{
103103
get
104104
{
@@ -146,7 +146,7 @@ public virtual void OnLostOwnership()
146146
/// <param name="name">The MessageType to register</param>
147147
/// <param name="action">The callback to get invoked whenever a message is received</param>
148148
/// <returns>HandlerId for the messageHandler that can be used to deregister the messageHandler</returns>
149-
protected int RegisterMessageHandler(string name, Action<int, byte[]> action)
149+
protected int RegisterMessageHandler(string name, Action<uint, byte[]> action)
150150
{
151151
if (!MessageManager.messageTypes.ContainsKey(name))
152152
{
@@ -157,7 +157,7 @@ protected int RegisterMessageHandler(string name, Action<int, byte[]> action)
157157
ushort behaviourOrder = networkedObject.GetOrderIndex(this);
158158

159159
if (!networkedObject.targetMessageActions.ContainsKey(behaviourOrder))
160-
networkedObject.targetMessageActions.Add(behaviourOrder, new Dictionary<ushort, Action<int, byte[]>>());
160+
networkedObject.targetMessageActions.Add(behaviourOrder, new Dictionary<ushort, Action<uint, byte[]>>());
161161
if (networkedObject.targetMessageActions[behaviourOrder].ContainsKey(messageType))
162162
{
163163
Debug.LogWarning("MLAPI: Each NetworkedBehaviour can only register one callback per instance per message type");
@@ -368,7 +368,7 @@ internal void OnSyncVarUpdate(object value, byte fieldIndex)
368368
syncedVarHooks[fieldIndex].Invoke(this, null);
369369
}
370370

371-
internal void FlushToClient(int clientId)
371+
internal void FlushToClient(uint clientId)
372372
{
373373
//This NetworkedBehaviour has no SyncVars
374374
if (dirtyFields.Length == 0)
@@ -689,7 +689,7 @@ protected void SendToServer(string messageType, string channelName, byte[] data)
689689
Debug.LogWarning("MLAPI: Server can not send messages to server.");
690690
return;
691691
}
692-
NetworkingManager.singleton.Send(NetworkingManager.singleton.serverClientId, messageType, channelName, data);
692+
NetworkingManager.singleton.Send(NetId.ServerNetId.GetClientId(), messageType, channelName, data);
693693
}
694694

695695
/// <summary>
@@ -722,7 +722,7 @@ protected void SendToServerTarget(string messageType, string channelName, byte[]
722722
Debug.LogWarning("MLAPI: Server can not send messages to server.");
723723
return;
724724
}
725-
NetworkingManager.singleton.Send(NetworkingManager.singleton.serverClientId, messageType, channelName, data, networkId, networkedObject.GetOrderIndex(this));
725+
NetworkingManager.singleton.Send(NetId.ServerNetId.GetClientId(), messageType, channelName, data, networkId, networkedObject.GetOrderIndex(this));
726726
}
727727

728728
/// <summary>
@@ -876,7 +876,7 @@ protected void SendToNonLocalClientsTarget<T>(string messageType, string channel
876876
/// <param name="messageType">User defined messageType</param>
877877
/// <param name="channelName">User defined channelName</param>
878878
/// <param name="data">The binary data to send</param>
879-
protected void SendToClient(int clientId, string messageType, string channelName, byte[] data)
879+
protected void SendToClient(uint clientId, string messageType, string channelName, byte[] data)
880880
{
881881
if (MessageManager.messageTypes[messageType] < 32)
882882
{
@@ -911,7 +911,7 @@ protected void SendToClient<T>(int clientId, string messageType, string channelN
911911
/// <param name="messageType">User defined messageType</param>
912912
/// <param name="channelName">User defined channelName</param>
913913
/// <param name="data">The binary data to send</param>
914-
protected void SendToClientTarget(int clientId, string messageType, string channelName, byte[] data)
914+
protected void SendToClientTarget(uint clientId, string messageType, string channelName, byte[] data)
915915
{
916916
if (MessageManager.messageTypes[messageType] < 32)
917917
{
@@ -946,7 +946,7 @@ protected void SendToClientTarget<T>(int clientId, string messageType, string ch
946946
/// <param name="messageType">User defined messageType</param>
947947
/// <param name="channelName">User defined channelName</param>
948948
/// <param name="data">The binary data to send</param>
949-
protected void SendToClients(int[] clientIds, string messageType, string channelName, byte[] data)
949+
protected void SendToClients(uint[] clientIds, string messageType, string channelName, byte[] data)
950950
{
951951
if (MessageManager.messageTypes[messageType] < 32)
952952
{
@@ -981,7 +981,7 @@ protected void SendToClients<T>(int[] clientIds, string messageType, string chan
981981
/// <param name="messageType">User defined messageType</param>
982982
/// <param name="channelName">User defined channelName</param>
983983
/// <param name="data">The binary data to send</param>
984-
protected void SendToClientsTarget(int[] clientIds, string messageType, string channelName, byte[] data)
984+
protected void SendToClientsTarget(uint[] clientIds, string messageType, string channelName, byte[] data)
985985
{
986986
if (MessageManager.messageTypes[messageType] < 32)
987987
{
@@ -1016,7 +1016,7 @@ protected void SendToClientsTarget<T>(int[] clientIds, string messageType, strin
10161016
/// <param name="messageType">User defined messageType</param>
10171017
/// <param name="channelName">User defined channelName</param>
10181018
/// <param name="data">The binary data to send</param>
1019-
protected void SendToClients(List<int> clientIds, string messageType, string channelName, byte[] data)
1019+
protected void SendToClients(List<uint> clientIds, string messageType, string channelName, byte[] data)
10201020
{
10211021
if (MessageManager.messageTypes[messageType] < 32)
10221022
{
@@ -1051,7 +1051,7 @@ protected void SendToClients<T>(List<int> clientIds, string messageType, string
10511051
/// <param name="messageType">User defined messageType</param>
10521052
/// <param name="channelName">User defined channelName</param>
10531053
/// <param name="data">The binary data to send</param>
1054-
protected void SendToClientsTarget(List<int> clientIds, string messageType, string channelName, byte[] data)
1054+
protected void SendToClientsTarget(List<uint> clientIds, string messageType, string channelName, byte[] data)
10551055
{
10561056
if (MessageManager.messageTypes[messageType] < 32)
10571057
{
@@ -1074,7 +1074,7 @@ protected void SendToClientsTarget(List<int> clientIds, string messageType, stri
10741074
/// <param name="messageType">User defined messageType</param>
10751075
/// <param name="channelName">User defined channelName</param>
10761076
/// <param name="instance">The instance to send</param>
1077-
protected void SendToClientsTarget<T>(List<int> clientIds, string messageType, string channelName, T instance)
1077+
protected void SendToClientsTarget<T>(List<uint> clientIds, string messageType, string channelName, T instance)
10781078
{
10791079
SendToClientsTarget(clientIds, messageType, channelName, BinarySerializer.Serialize<T>(instance));
10801080
}

0 commit comments

Comments
 (0)