From 6c0122eda5288769c260b805f0c26665fe423341 Mon Sep 17 00:00:00 2001 From: Jay X Peet Date: Wed, 11 Aug 2021 23:51:35 +0100 Subject: [PATCH 1/6] Upgrade MLAPI implementation to support MLAPI 0.1.0 --- MlapiClient.cs | 43 +++++++++++++++---------------------- MlapiCommsNetwork.cs | 8 +++---- MlapiServer.cs | 50 ++++++++++++++++++-------------------------- 3 files changed, 41 insertions(+), 60 deletions(-) diff --git a/MlapiClient.cs b/MlapiClient.cs index a2c3ac7..881f533 100644 --- a/MlapiClient.cs +++ b/MlapiClient.cs @@ -1,11 +1,9 @@ using Dissonance.Networking; using MLAPI; -using MLAPI.Messaging; +using MLAPI.Messaging; using MLAPI.Serialization.Pooled; -using MLAPI.Transports.UNET; +using MLAPI.Transports; using System; -using System.IO; -using UnityEngine; public class MlapiClient : BaseClient { @@ -37,48 +35,41 @@ protected override void ReadMessages() protected override void SendReliable(ArraySegment packet) { - if (NetworkingManager.Singleton.IsHost) + if (NetworkManager.Singleton.IsHost) { - // As we are the host in this scenario we should send the packet directly to the server rather than over the network and avoid loopback issues + // As we are the host in this scenario we should send the packet directly to the server rather than over the network and avoid loop-back issues _network.server.NetworkReceivedPacket(new MlapiConn(), packet); } else { - - using (PooledBitStream stream = PooledBitStream.Get()) - { - using (PooledBitWriter writer = PooledBitWriter.Get(stream)) - { - for (var i = 0; i < packet.Count; i++) - { - writer.WriteByte(packet.Array[i + packet.Offset]); - } - CustomMessagingManager.SendNamedMessage("DissonanceToServer", NetworkingManager.Singleton.ServerClientId, stream, "MLAPI_ANIMATION_UPDATE"); + using (PooledNetworkBuffer writer = NetworkBufferPool.GetBuffer()) + { + for (var i = 0; i < packet.Count; i++) + { + writer.WriteByte(packet.Array[i + packet.Offset]); } + CustomMessagingManager.SendNamedMessage("DissonanceToServer", NetworkManager.Singleton.ServerClientId, writer, NetworkChannel.AnimationUpdate); } } } protected override void SendUnreliable(ArraySegment packet) { - if (NetworkingManager.Singleton.IsHost) + if (NetworkManager.Singleton.IsHost) { // As we are the host in this scenario we should send the packet directly to the server rather than over the network and avoid loopback issues _network.server.NetworkReceivedPacket(new MlapiConn(), packet); } else - { - - using (PooledBitStream stream = PooledBitStream.Get()) + { + + using (PooledNetworkBuffer writer = NetworkBufferPool.GetBuffer()) { - using (PooledBitWriter writer = PooledBitWriter.Get(stream)) + for (var i = 0; i < packet.Count; i++) { - for (var i = 0; i < packet.Count; i++) - { - writer.WriteByte(packet.Array[i + packet.Offset]); - } - CustomMessagingManager.SendNamedMessage("DissonanceToServer", NetworkingManager.Singleton.ServerClientId, stream, "MLAPI_TIME_SYNC"); + writer.WriteByte(packet.Array[i + packet.Offset]); } + CustomMessagingManager.SendNamedMessage("DissonanceToServer", NetworkManager.Singleton.ServerClientId, writer, NetworkChannel.TimeSync); } } diff --git a/MlapiCommsNetwork.cs b/MlapiCommsNetwork.cs index e298d4c..86286ec 100644 --- a/MlapiCommsNetwork.cs +++ b/MlapiCommsNetwork.cs @@ -39,13 +39,13 @@ protected override void Update() if (IsInitialized) { // Check if the MLAPI is ready - var networkActive = NetworkingManager.Singleton.isActiveAndEnabled && - (NetworkingManager.Singleton.IsClient || NetworkingManager.Singleton.IsHost); + var networkActive = NetworkManager.Singleton.isActiveAndEnabled && + (NetworkManager.Singleton.IsClient || NetworkManager.Singleton.IsHost); if (networkActive) { // Check what mode the MLAPI is in - var server = NetworkingManager.Singleton.IsHost; - var client = NetworkingManager.Singleton.IsClient; + var server = NetworkManager.Singleton.IsHost; + var client = NetworkManager.Singleton.IsClient; // Check what mode Dissonance is in and if // they're different then call the correct method diff --git a/MlapiServer.cs b/MlapiServer.cs index cdbc656..1fad261 100644 --- a/MlapiServer.cs +++ b/MlapiServer.cs @@ -1,13 +1,9 @@ using Dissonance.Networking; -using JetBrains.Annotations; using MLAPI; using MLAPI.Messaging; using MLAPI.Serialization.Pooled; -using MLAPI.Transports.UNET; using System; -using System.Collections.Generic; -using System.IO; -using UnityEngine; +using MLAPI.Transports; public class MlapiServer : BaseServer { @@ -45,52 +41,46 @@ protected override void ReadMessages() } protected override void SendReliable(MlapiConn destination, ArraySegment packet) - { - using (PooledBitStream stream = PooledBitStream.Get()) + { + using (PooledNetworkBuffer writer = NetworkBufferPool.GetBuffer()) { - using (PooledBitWriter writer = PooledBitWriter.Get(stream)) + var count = packet.Count; + var offset = packet.Offset; + for (var i = 0; i < count; i++) { - var count = packet.Count; - var offset = packet.Offset; - for (var i = 0; i < count; i++) - { - writer.WriteByte(packet.Array[i + offset]); - } - if (NetworkingManager.Singleton.LocalClientId == destination.clientId) - { - // As we are the host in this scenario we should send the packet directly to the client rather than over the network and avoid loopback issues - _network.client.NetworkReceivedPacket(packet); - } - else - { - CustomMessagingManager.SendNamedMessage("DissonanceToClient", destination.clientId, stream, "MLAPI_ANIMATION_UPDATE"); - } + writer.WriteByte(packet.Array[i + offset]); + } + if (NetworkManager.Singleton.LocalClientId == destination.clientId) + { + // As we are the host in this scenario we should send the packet directly to the client rather than over the network and avoid loopback issues + _network.client.NetworkReceivedPacket(packet); + } + else + { + CustomMessagingManager.SendNamedMessage("DissonanceToClient", destination.clientId, writer, NetworkChannel.AnimationUpdate); } } } protected override void SendUnreliable(MlapiConn destination, ArraySegment packet) { - using (PooledBitStream stream = PooledBitStream.Get()) + using (PooledNetworkBuffer writer = NetworkBufferPool.GetBuffer()) { - using (PooledBitWriter writer = PooledBitWriter.Get(stream)) - { var count = packet.Count; var offset = packet.Offset; for (var i = 0; i < count; i++) { writer.WriteByte(packet.Array[i + offset]); } - if (NetworkingManager.Singleton.LocalClientId == destination.clientId) + if (NetworkManager.Singleton.LocalClientId == destination.clientId) { - // As we are the host in this scenario we should send the packet directly to the client rather than over the network and avoid loopback issues + // As we are the host in this scenario we should send the packet directly to the client rather than over the network and avoid loop-back issues _network.client.NetworkReceivedPacket(packet); } else { - CustomMessagingManager.SendNamedMessage("DissonanceToClient", destination.clientId, stream, "MLAPI_TIME_SYNC"); + CustomMessagingManager.SendNamedMessage("DissonanceToClient", destination.clientId, writer, NetworkChannel.TimeSync); } - } } } From ba6401402f18e3ebac1a54078a97a6372a7727a4 Mon Sep 17 00:00:00 2001 From: Jay X Peet Date: Sat, 30 Oct 2021 19:09:42 +0100 Subject: [PATCH 2/6] Upgrading to NFGO 1.0 --- MlapiClient.cs | 52 +++++----- MlapiCommsNetwork.cs | 2 +- ...orkEditor.cs => MlapiCommsNetworkEditor.cs | 2 +- ...cs.meta => MlapiCommsNetworkEditor.cs.meta | 0 MlapiServer.cs | 98 +++++++++---------- 5 files changed, 74 insertions(+), 80 deletions(-) rename UNetCommsNetworkEditor.cs => MlapiCommsNetworkEditor.cs (89%) rename UNetCommsNetworkEditor.cs.meta => MlapiCommsNetworkEditor.cs.meta (100%) diff --git a/MlapiClient.cs b/MlapiClient.cs index 881f533..ce31394 100644 --- a/MlapiClient.cs +++ b/MlapiClient.cs @@ -1,9 +1,7 @@ using Dissonance.Networking; -using MLAPI; -using MLAPI.Messaging; -using MLAPI.Serialization.Pooled; -using MLAPI.Transports; +using Unity.Netcode; using System; +using Allocator = Unity.Collections.Allocator; public class MlapiClient : BaseClient { @@ -17,17 +15,19 @@ public MlapiClient(MlapiCommsNetwork network) : base(network) public override void Connect() { // Register receiving packets on the client from the server - CustomMessagingManager.RegisterNamedMessageHandler("DissonanceToClient", (senderClientId, stream) => - { - Int32 length = stream.Length > Int32.MaxValue ? Int32.MaxValue : Convert.ToInt32(stream.Length); - Byte[] buffer = new Byte[length]; - stream.Read(buffer, 0, length); - - base.NetworkReceivedPacket(new ArraySegment(buffer)); - }); + NetworkManager.Singleton.CustomMessagingManager.RegisterNamedMessageHandler("DissonanceToClient", OnDissonanceToClient); Connected(); } + protected void OnDissonanceToClient(ulong id, FastBufferReader reader) + { + int length = reader.Length; + Byte[] buffer = new Byte[length]; + reader.ReadBytes(ref buffer, length); + + base.NetworkReceivedPacket(new ArraySegment(buffer)); + } + protected override void ReadMessages() { @@ -35,41 +35,41 @@ protected override void ReadMessages() protected override void SendReliable(ArraySegment packet) { - if (NetworkManager.Singleton.IsHost) + if (NetworkManager.Singleton.IsServer) { // As we are the host in this scenario we should send the packet directly to the server rather than over the network and avoid loop-back issues _network.server.NetworkReceivedPacket(new MlapiConn(), packet); } else { - using (PooledNetworkBuffer writer = NetworkBufferPool.GetBuffer()) + using (FastBufferWriter writer = new FastBufferWriter(packet.Count, Allocator.TempJob)) { - for (var i = 0; i < packet.Count; i++) - { - writer.WriteByte(packet.Array[i + packet.Offset]); - } - CustomMessagingManager.SendNamedMessage("DissonanceToServer", NetworkManager.Singleton.ServerClientId, writer, NetworkChannel.AnimationUpdate); + writer.WriteBytes(packet.Array, packet.Count, packet.Offset); + + NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("DissonanceToServer", + NetworkManager.Singleton.ServerClientId, writer, NetworkDelivery.Reliable); } } } protected override void SendUnreliable(ArraySegment packet) { - if (NetworkManager.Singleton.IsHost) + if (NetworkManager.Singleton.IsServer) { // As we are the host in this scenario we should send the packet directly to the server rather than over the network and avoid loopback issues _network.server.NetworkReceivedPacket(new MlapiConn(), packet); } else - { - - using (PooledNetworkBuffer writer = NetworkBufferPool.GetBuffer()) - { + { + using (FastBufferWriter writer = new FastBufferWriter(packet.Count, Allocator.TempJob)) + { for (var i = 0; i < packet.Count; i++) { writer.WriteByte(packet.Array[i + packet.Offset]); - } - CustomMessagingManager.SendNamedMessage("DissonanceToServer", NetworkManager.Singleton.ServerClientId, writer, NetworkChannel.TimeSync); + } + + NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("DissonanceToServer", + NetworkManager.Singleton.ServerClientId, writer, NetworkDelivery.Unreliable); } } diff --git a/MlapiCommsNetwork.cs b/MlapiCommsNetwork.cs index 86286ec..ebad25d 100644 --- a/MlapiCommsNetwork.cs +++ b/MlapiCommsNetwork.cs @@ -1,7 +1,7 @@ using Dissonance; using Dissonance.Networking; using JetBrains.Annotations; -using MLAPI; +using Unity.Netcode; public class MlapiCommsNetwork : BaseCommsNetwork< diff --git a/UNetCommsNetworkEditor.cs b/MlapiCommsNetworkEditor.cs similarity index 89% rename from UNetCommsNetworkEditor.cs rename to MlapiCommsNetworkEditor.cs index 65b3785..862b056 100644 --- a/UNetCommsNetworkEditor.cs +++ b/MlapiCommsNetworkEditor.cs @@ -4,7 +4,7 @@ #if UNITY_EDITOR [CustomEditor(typeof(MlapiCommsNetwork))] - public class UNetCommsNetworkEditor + public class MlapiCommsNetworkEditor : Dissonance.Editor.BaseDissonnanceCommsNetworkEditor< MlapiCommsNetwork, MlapiServer, diff --git a/UNetCommsNetworkEditor.cs.meta b/MlapiCommsNetworkEditor.cs.meta similarity index 100% rename from UNetCommsNetworkEditor.cs.meta rename to MlapiCommsNetworkEditor.cs.meta diff --git a/MlapiServer.cs b/MlapiServer.cs index 1fad261..c118724 100644 --- a/MlapiServer.cs +++ b/MlapiServer.cs @@ -1,10 +1,7 @@ using Dissonance.Networking; -using MLAPI; -using MLAPI.Messaging; -using MLAPI.Serialization.Pooled; using System; -using MLAPI.Transports; - +using Unity.Netcode; +using Allocator = Unity.Collections.Allocator; public class MlapiServer : BaseServer { private MlapiCommsNetwork _network; @@ -16,18 +13,22 @@ public MlapiServer(MlapiCommsNetwork network) public override void Connect() { // Register receiving packets on the server from the client - CustomMessagingManager.RegisterNamedMessageHandler("DissonanceToServer", (senderClientId, stream) => - { - var client = new MlapiConn(); - client.clientId = senderClientId; - - Int32 length = stream.Length > Int32.MaxValue ? Int32.MaxValue : Convert.ToInt32(stream.Length); - Byte[] buffer = new Byte[length]; - stream.Read(buffer, 0, length); - - base.NetworkReceivedPacket(client, new ArraySegment(buffer)); - }); + NetworkManager.Singleton.CustomMessagingManager.RegisterNamedMessageHandler("DissonanceToServer", OnDissonanceToServer); base.Connect(); + } + + protected void OnDissonanceToServer(ulong client_id, FastBufferReader reader) + { + var client = new MlapiConn(); + client.clientId = client_id; + + //JP : I think there would be a way to access the raw buffer from the reader and just send that to the array segment. Maybe with unsafe c# + //Would skip the allocation below. + //Alternativly, we could just preallocate a big buffer and reuse it for each message? + Byte[] buffer = new Byte[reader.Length]; + reader.ReadBytes(ref buffer, reader.Length); + + base.NetworkReceivedPacket(client, new ArraySegment(buffer)); } public override void Disconnect() @@ -42,47 +43,40 @@ protected override void ReadMessages() protected override void SendReliable(MlapiConn destination, ArraySegment packet) { - using (PooledNetworkBuffer writer = NetworkBufferPool.GetBuffer()) - { - var count = packet.Count; - var offset = packet.Offset; - for (var i = 0; i < count; i++) - { - writer.WriteByte(packet.Array[i + offset]); - } - if (NetworkManager.Singleton.LocalClientId == destination.clientId) - { - // As we are the host in this scenario we should send the packet directly to the client rather than over the network and avoid loopback issues - _network.client.NetworkReceivedPacket(packet); - } - else - { - CustomMessagingManager.SendNamedMessage("DissonanceToClient", destination.clientId, writer, NetworkChannel.AnimationUpdate); - } + if (NetworkManager.Singleton.LocalClientId == destination.clientId) + { + // As we are the host in this scenario we should send the packet directly to the client rather than over the network and avoid loopback issues + _network.client.NetworkReceivedPacket(packet); + } + else + { + using (FastBufferWriter writer = new FastBufferWriter(packet.Count, Allocator.TempJob)) + { + writer.WriteBytes(packet.Array, packet.Count, packet.Offset); + + NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("DissonanceToClient", + destination.clientId, writer, NetworkDelivery.Reliable); + } + } } protected override void SendUnreliable(MlapiConn destination, ArraySegment packet) - { - using (PooledNetworkBuffer writer = NetworkBufferPool.GetBuffer()) - { - var count = packet.Count; - var offset = packet.Offset; - for (var i = 0; i < count; i++) - { - writer.WriteByte(packet.Array[i + offset]); - } - if (NetworkManager.Singleton.LocalClientId == destination.clientId) - { - // As we are the host in this scenario we should send the packet directly to the client rather than over the network and avoid loop-back issues - _network.client.NetworkReceivedPacket(packet); - } - else - { - CustomMessagingManager.SendNamedMessage("DissonanceToClient", destination.clientId, writer, NetworkChannel.TimeSync); - } + { + if (NetworkManager.Singleton.LocalClientId == destination.clientId) + { + // As we are the host in this scenario we should send the packet directly to the client rather than over the network and avoid loop-back issues + _network.client.NetworkReceivedPacket(packet); + } + else + { + using (FastBufferWriter writer = new FastBufferWriter(packet.Count, Allocator.TempJob)) + { + writer.WriteBytes(packet.Array, packet.Count, packet.Offset); + NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("DissonanceToClient", + destination.clientId, writer, NetworkDelivery.Unreliable); + } } - } public void ClientDisconnected() From 36826b796a8a38f5e41746c6cbcd612880aee0c7 Mon Sep 17 00:00:00 2001 From: Jay X Peet Date: Sat, 30 Oct 2021 21:22:37 +0100 Subject: [PATCH 3/6] Ported HlapiPlayer across to Mlapi. It used a sync var on a string, which is not supported with Mlapis NetworkVariable. I cant actually see why it was a sync var, since it also sends it across an RPC --- MlapiPlayer.cs | 174 ++++++++++++++++++++++++++++++++++++++++++++ MlapiPlayer.cs.meta | 11 +++ 2 files changed, 185 insertions(+) create mode 100644 MlapiPlayer.cs create mode 100644 MlapiPlayer.cs.meta diff --git a/MlapiPlayer.cs b/MlapiPlayer.cs new file mode 100644 index 0000000..4172ae1 --- /dev/null +++ b/MlapiPlayer.cs @@ -0,0 +1,174 @@ +using UnityEngine; +using Unity.Netcode; +using Dissonance; + + /// + /// When added to the player prefab, allows Dissonance to automatically track + /// the location of remote players for positional audio for games using the + /// MLAPI / NFGO + /// +[RequireComponent(typeof (NetworkObject))] +public class MlapiPlayer : NetworkBehaviour, IDissonancePlayer +{ + private static readonly Log Log = Logs.Create(LogCategory.Network, "HLAPI Player Component"); + + private DissonanceComms _comms; + + public bool IsTracking { get; private set; } + + /// + /// The name of the player + /// + /// + private string _playerId; + public string PlayerId { get { return _playerId; } } + + public Vector3 Position + { + get { return transform.position; } + } + + public Quaternion Rotation + { + get { return transform.rotation; } + } + + public NetworkPlayerType Type + { + get + { + if (_comms == null || _playerId == null) + return NetworkPlayerType.Unknown; + return _comms.LocalPlayerName.Equals(_playerId) ? NetworkPlayerType.Local : NetworkPlayerType.Remote; + } + } + + public override void OnDestroy() + { + base.OnDestroy(); + if (_comms != null) + _comms.LocalPlayerNameChanged -= SetPlayerName; + } + + public void OnEnable() + { + _comms = FindObjectOfType(); + } + + public void OnDisable() + { + if (IsTracking) + StopTracking(); + } + + public override void OnNetworkSpawn() + { + base.OnNetworkSpawn(); + + if(IsClient) + { + OnStartClient(); + + if (IsLocalPlayer) + OnStartLocalPlayer(); + } + } + + protected void OnStartLocalPlayer() + { + var comms = FindObjectOfType(); + if (comms == null) + { + throw Log.CreateUserErrorException( + "cannot find DissonanceComms component in scene", + "not placing a DissonanceComms component on a game object in the scene", + "", + "9A79FDCB-263E-4124-B54D-67EDA39C09A5" + ); + } + + Log.Debug("Tracking `OnStartLocalPlayer` Name={0}", comms.LocalPlayerName); + + // This method is called on the client which has control authority over this object. This will be the local client of whichever player we are tracking. + if (comms.LocalPlayerName != null) + SetPlayerName(comms.LocalPlayerName); + + //Subscribe to future name changes (this is critical because we may not have run the initial set name yet and this will trigger that initial call) + comms.LocalPlayerNameChanged += SetPlayerName; + } + + private void SetPlayerName(string playerName) + { + //We need the player name to be set on all the clients and then tracking to be started (on each client). + //To do this we send a command from this client, informing the server of our name. The server will pass this on to all the clients (with an RPC) + // Client -> Server -> Client + + //We need to stop and restart tracking to handle the name change + if (IsTracking) + StopTracking(); + + //Perform the actual work + _playerId = playerName; + StartTracking(); + + //Inform the server the name has changed + if (IsLocalPlayer) + SetPlayerNameServerRpc(playerName); + } + + protected void OnStartClient() + { + //A client is starting. Start tracking if the name has been properly initialised. + if (!string.IsNullOrEmpty(PlayerId)) + StartTracking(); + } + + /// + /// Invoking on client will cause it to run on the server + /// + /// + [ServerRpc] + private void SetPlayerNameServerRpc(string playerName) + { + _playerId = playerName; + + //Now call the RPC to inform clients they need to handle this changed value + SetPlayerNameClientRpc(playerName); + } + + /// + /// Invoking on the server will cause it to run on all the clients + /// + /// + [ClientRpc] + private void SetPlayerNameClientRpc(string playerName) + { + //received a message from server (on all clients). If this is not the local player then apply the change + if (!IsLocalPlayer) + SetPlayerName(playerName); + } + + private void StartTracking() + { + if (IsTracking) + throw Log.CreatePossibleBugException("Attempting to start player tracking, but tracking is already started", "B7D1F25E-72AF-4E93-8CFF-90CEBEAC68CF"); + + if (_comms != null) + { + _comms.TrackPlayerPosition(this); + IsTracking = true; + } + } + + private void StopTracking() + { + if (!IsTracking) + throw Log.CreatePossibleBugException("Attempting to stop player tracking, but tracking is not started", "EC5C395D-B544-49DC-B33C-7D7533349134"); + + if (_comms != null) + { + _comms.StopTracking(this); + IsTracking = false; + } + } +} \ No newline at end of file diff --git a/MlapiPlayer.cs.meta b/MlapiPlayer.cs.meta new file mode 100644 index 0000000..cf72fa7 --- /dev/null +++ b/MlapiPlayer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e68b96cd0875e5a4ea3c05a1185c0c7f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: From 49b1caa36ca0df3bc1d2cf07b9852ecab6ca0958 Mon Sep 17 00:00:00 2001 From: Jay X Peet Date: Sat, 30 Oct 2021 22:50:58 +0100 Subject: [PATCH 4/6] Added required calls to TryBeginWrite --- MlapiClient.cs | 7 +++---- MlapiServer.cs | 2 ++ 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/MlapiClient.cs b/MlapiClient.cs index ce31394..560b9bd 100644 --- a/MlapiClient.cs +++ b/MlapiClient.cs @@ -44,6 +44,7 @@ protected override void SendReliable(ArraySegment packet) { using (FastBufferWriter writer = new FastBufferWriter(packet.Count, Allocator.TempJob)) { + writer.TryBeginWrite(packet.Count); writer.WriteBytes(packet.Array, packet.Count, packet.Offset); NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("DissonanceToServer", @@ -63,10 +64,8 @@ protected override void SendUnreliable(ArraySegment packet) { using (FastBufferWriter writer = new FastBufferWriter(packet.Count, Allocator.TempJob)) { - for (var i = 0; i < packet.Count; i++) - { - writer.WriteByte(packet.Array[i + packet.Offset]); - } + writer.TryBeginWrite(packet.Count); + writer.WriteBytes(packet.Array, packet.Count, packet.Offset); NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("DissonanceToServer", NetworkManager.Singleton.ServerClientId, writer, NetworkDelivery.Unreliable); diff --git a/MlapiServer.cs b/MlapiServer.cs index c118724..225dc0b 100644 --- a/MlapiServer.cs +++ b/MlapiServer.cs @@ -52,6 +52,7 @@ protected override void SendReliable(MlapiConn destination, ArraySegment p { using (FastBufferWriter writer = new FastBufferWriter(packet.Count, Allocator.TempJob)) { + writer.TryBeginWrite(packet.Count); writer.WriteBytes(packet.Array, packet.Count, packet.Offset); NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("DissonanceToClient", @@ -72,6 +73,7 @@ protected override void SendUnreliable(MlapiConn destination, ArraySegment { using (FastBufferWriter writer = new FastBufferWriter(packet.Count, Allocator.TempJob)) { + writer.TryBeginWrite(packet.Count); writer.WriteBytes(packet.Array, packet.Count, packet.Offset); NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("DissonanceToClient", destination.clientId, writer, NetworkDelivery.Unreliable); From 508659e8fc7867e5e5ba81f7b2cce478be89fdec Mon Sep 17 00:00:00 2001 From: Jay X Peet Date: Sun, 31 Oct 2021 03:29:51 +0000 Subject: [PATCH 5/6] Fixed up IsHost check + Reader / Writer Errors --- MlapiClient.cs | 8 +++----- MlapiCommsNetwork.cs | 4 ++-- MlapiServer.cs | 8 +++----- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/MlapiClient.cs b/MlapiClient.cs index 560b9bd..e442dba 100644 --- a/MlapiClient.cs +++ b/MlapiClient.cs @@ -23,7 +23,7 @@ protected void OnDissonanceToClient(ulong id, FastBufferReader reader) { int length = reader.Length; Byte[] buffer = new Byte[length]; - reader.ReadBytes(ref buffer, length); + reader.ReadBytesSafe(ref buffer, length); base.NetworkReceivedPacket(new ArraySegment(buffer)); } @@ -44,8 +44,7 @@ protected override void SendReliable(ArraySegment packet) { using (FastBufferWriter writer = new FastBufferWriter(packet.Count, Allocator.TempJob)) { - writer.TryBeginWrite(packet.Count); - writer.WriteBytes(packet.Array, packet.Count, packet.Offset); + writer.WriteBytesSafe(packet.Array, packet.Count, packet.Offset); NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("DissonanceToServer", NetworkManager.Singleton.ServerClientId, writer, NetworkDelivery.Reliable); @@ -64,8 +63,7 @@ protected override void SendUnreliable(ArraySegment packet) { using (FastBufferWriter writer = new FastBufferWriter(packet.Count, Allocator.TempJob)) { - writer.TryBeginWrite(packet.Count); - writer.WriteBytes(packet.Array, packet.Count, packet.Offset); + writer.WriteBytesSafe(packet.Array, packet.Count, packet.Offset); NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("DissonanceToServer", NetworkManager.Singleton.ServerClientId, writer, NetworkDelivery.Unreliable); diff --git a/MlapiCommsNetwork.cs b/MlapiCommsNetwork.cs index ebad25d..708f3dd 100644 --- a/MlapiCommsNetwork.cs +++ b/MlapiCommsNetwork.cs @@ -40,11 +40,11 @@ protected override void Update() { // Check if the MLAPI is ready var networkActive = NetworkManager.Singleton.isActiveAndEnabled && - (NetworkManager.Singleton.IsClient || NetworkManager.Singleton.IsHost); + (NetworkManager.Singleton.IsClient || NetworkManager.Singleton.IsServer); if (networkActive) { // Check what mode the MLAPI is in - var server = NetworkManager.Singleton.IsHost; + var server = NetworkManager.Singleton.IsServer; var client = NetworkManager.Singleton.IsClient; // Check what mode Dissonance is in and if diff --git a/MlapiServer.cs b/MlapiServer.cs index 225dc0b..2238416 100644 --- a/MlapiServer.cs +++ b/MlapiServer.cs @@ -26,7 +26,7 @@ protected void OnDissonanceToServer(ulong client_id, FastBufferReader reader) //Would skip the allocation below. //Alternativly, we could just preallocate a big buffer and reuse it for each message? Byte[] buffer = new Byte[reader.Length]; - reader.ReadBytes(ref buffer, reader.Length); + reader.ReadBytesSafe(ref buffer, reader.Length); base.NetworkReceivedPacket(client, new ArraySegment(buffer)); } @@ -52,8 +52,7 @@ protected override void SendReliable(MlapiConn destination, ArraySegment p { using (FastBufferWriter writer = new FastBufferWriter(packet.Count, Allocator.TempJob)) { - writer.TryBeginWrite(packet.Count); - writer.WriteBytes(packet.Array, packet.Count, packet.Offset); + writer.WriteBytesSafe(packet.Array, packet.Count, packet.Offset); NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("DissonanceToClient", destination.clientId, writer, NetworkDelivery.Reliable); @@ -73,8 +72,7 @@ protected override void SendUnreliable(MlapiConn destination, ArraySegment { using (FastBufferWriter writer = new FastBufferWriter(packet.Count, Allocator.TempJob)) { - writer.TryBeginWrite(packet.Count); - writer.WriteBytes(packet.Array, packet.Count, packet.Offset); + writer.WriteBytesSafe(packet.Array, packet.Count, packet.Offset); NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("DissonanceToClient", destination.clientId, writer, NetworkDelivery.Unreliable); } From 05f6a26dd79d62e0a65720066afb530a3a91ece6 Mon Sep 17 00:00:00 2001 From: Jay X Peet Date: Sun, 31 Oct 2021 04:07:48 +0000 Subject: [PATCH 6/6] Increase buffers by the length field --- MlapiClient.cs | 8 +++++--- MlapiServer.cs | 11 +++++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/MlapiClient.cs b/MlapiClient.cs index e442dba..bb78776 100644 --- a/MlapiClient.cs +++ b/MlapiClient.cs @@ -21,7 +21,7 @@ public override void Connect() protected void OnDissonanceToClient(ulong id, FastBufferReader reader) { - int length = reader.Length; + reader.ReadValueSafe(out int length); Byte[] buffer = new Byte[length]; reader.ReadBytesSafe(ref buffer, length); @@ -42,8 +42,9 @@ protected override void SendReliable(ArraySegment packet) } else { - using (FastBufferWriter writer = new FastBufferWriter(packet.Count, Allocator.TempJob)) + using (FastBufferWriter writer = new FastBufferWriter(packet.Count + 4, Allocator.TempJob)) { + writer.WriteValueSafe(packet.Count); writer.WriteBytesSafe(packet.Array, packet.Count, packet.Offset); NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("DissonanceToServer", @@ -61,8 +62,9 @@ protected override void SendUnreliable(ArraySegment packet) } else { - using (FastBufferWriter writer = new FastBufferWriter(packet.Count, Allocator.TempJob)) + using (FastBufferWriter writer = new FastBufferWriter(packet.Count + 4, Allocator.TempJob)) { + writer.WriteValueSafe(packet.Count); writer.WriteBytesSafe(packet.Array, packet.Count, packet.Offset); NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("DissonanceToServer", diff --git a/MlapiServer.cs b/MlapiServer.cs index 2238416..7b241b7 100644 --- a/MlapiServer.cs +++ b/MlapiServer.cs @@ -25,8 +25,9 @@ protected void OnDissonanceToServer(ulong client_id, FastBufferReader reader) //JP : I think there would be a way to access the raw buffer from the reader and just send that to the array segment. Maybe with unsafe c# //Would skip the allocation below. //Alternativly, we could just preallocate a big buffer and reuse it for each message? - Byte[] buffer = new Byte[reader.Length]; - reader.ReadBytesSafe(ref buffer, reader.Length); + reader.ReadValueSafe(out int length); + Byte[] buffer = new Byte[length]; + reader.ReadBytesSafe(ref buffer, length); base.NetworkReceivedPacket(client, new ArraySegment(buffer)); } @@ -50,8 +51,9 @@ protected override void SendReliable(MlapiConn destination, ArraySegment p } else { - using (FastBufferWriter writer = new FastBufferWriter(packet.Count, Allocator.TempJob)) + using (FastBufferWriter writer = new FastBufferWriter(packet.Count + 4, Allocator.TempJob)) { + writer.WriteValueSafe(packet.Count); writer.WriteBytesSafe(packet.Array, packet.Count, packet.Offset); NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("DissonanceToClient", @@ -70,8 +72,9 @@ protected override void SendUnreliable(MlapiConn destination, ArraySegment } else { - using (FastBufferWriter writer = new FastBufferWriter(packet.Count, Allocator.TempJob)) + using (FastBufferWriter writer = new FastBufferWriter(packet.Count + 4, Allocator.TempJob)) { + writer.WriteValueSafe(packet.Count); writer.WriteBytesSafe(packet.Array, packet.Count, packet.Offset); NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("DissonanceToClient", destination.clientId, writer, NetworkDelivery.Unreliable);