Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for sending "Scene" message to a chosen peer #3079

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,25 @@ public void Dispose()
cancellationTokenSource.SafeCancelAndDispose();
}

public object SendBinary(IReadOnlyList<PoolableByteArray> data)
public void SendBinary(IReadOnlyList<PoolableByteArray> broadcastData, string? recipient = null)
{
foreach (PoolableByteArray poolable in data)
foreach (PoolableByteArray poolable in broadcastData)
if (poolable.Length > 0)
{
ISceneCommunicationPipe.ConnectivityAssertiveness assertiveness = poolable.Span[0] == REQ_CRDT_STATE
? ISceneCommunicationPipe.ConnectivityAssertiveness.DELIVERY_ASSERTED
: ISceneCommunicationPipe.ConnectivityAssertiveness.DROP_IF_NOT_CONNECTED;

EncodeAndSendMessage(ISceneCommunicationPipe.MsgType.Uint8Array, poolable.Memory.Span, assertiveness);
EncodeAndSendMessage(ISceneCommunicationPipe.MsgType.Uint8Array, poolable.Memory.Span, assertiveness, recipient);
}
}


public object GetResult()
{
lock (eventsToProcess)
{
object result = jsOperations.ConvertToScriptTypedArrays(eventsToProcess);
CleanUpReceivedMessages();

return result;
}
}
Expand All @@ -83,13 +84,12 @@ private void CleanUpReceivedMessages()
eventsToProcess.Clear();
}

protected void EncodeAndSendMessage(ISceneCommunicationPipe.MsgType msgType, ReadOnlySpan<byte> message, ISceneCommunicationPipe.ConnectivityAssertiveness assertiveness)
protected void EncodeAndSendMessage(ISceneCommunicationPipe.MsgType msgType, ReadOnlySpan<byte> message, ISceneCommunicationPipe.ConnectivityAssertiveness assertivenes, string? specialRecipient)
{
Span<byte> encodedMessage = stackalloc byte[message.Length + 1];
encodedMessage[0] = (byte)msgType;
message.CopyTo(encodedMessage[1..]);

sceneCommunicationPipe.SendMessage(encodedMessage, sceneId, assertiveness, cancellationTokenSource.Token);
sceneCommunicationPipe.SendMessage(encodedMessage, sceneId, assertivenes, cancellationTokenSource.Token, specialRecipient);
}

protected abstract void OnMessageReceived(ISceneCommunicationPipe.DecodedMessage decodedMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public enum ConnectivityAssertiveness

void RemoveSceneMessageHandler(string sceneId, MsgType msgType, SceneMessageHandler onSceneMessage);

void SendMessage(ReadOnlySpan<byte> message, string sceneId, ConnectivityAssertiveness assertiveness, CancellationToken ct);
void SendMessage(ReadOnlySpan<byte> message, string sceneId, ConnectivityAssertiveness assertiveness, CancellationToken ct, string? specialRecipient = null);

readonly ref struct DecodedMessage
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void ClearMessages()
public void Send(string data)
{
byte[] dataBytes = Encoding.UTF8.GetBytes(data);
EncodeAndSendMessage(ISceneCommunicationPipe.MsgType.String, dataBytes, ISceneCommunicationPipe.ConnectivityAssertiveness.DROP_IF_NOT_CONNECTED);
EncodeAndSendMessage(ISceneCommunicationPipe.MsgType.String, dataBytes, ISceneCommunicationPipe.ConnectivityAssertiveness.DROP_IF_NOT_CONNECTED, null);
}

protected override void OnMessageReceived(ISceneCommunicationPipe.DecodedMessage message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,15 @@ public void RemoveSceneMessageHandler(string sceneId, ISceneCommunicationPipe.Ms
sceneMessageHandlers.Remove(key);
}

public void SendMessage(ReadOnlySpan<byte> message, string sceneId, ISceneCommunicationPipe.ConnectivityAssertiveness assertiveness, CancellationToken ct)
public void SendMessage(ReadOnlySpan<byte> message, string sceneId, ISceneCommunicationPipe.ConnectivityAssertiveness assertiveness, CancellationToken ct, string? specialRecipient = null)
{
if (!sceneRoom.IsSceneConnected(sceneId))
{
if (assertiveness == ISceneCommunicationPipe.ConnectivityAssertiveness.DELIVERY_ASSERTED)
ReportHub.LogError(ReportCategory.COMMS_SCENE_HANDLER, $"Scene \"{sceneId}\" expected to deliver the message but {nameof(GateKeeperSceneRoom)} is connected to \"{sceneRoom.ConnectedScene?.SceneEntityDefinition.id}\"");
if (!sceneRoom.IsSceneConnected(sceneId)) return;

return;
}
MessageWrap<Scene> sceneMessage = messagePipe.NewMessage<Scene>();

if (assertiveness == ISceneCommunicationPipe.ConnectivityAssertiveness.DELIVERY_ASSERTED)
ReportHub.Log(ReportCategory.COMMS_SCENE_HANDLER, $"Sending scene message to {sceneRoom.Room().Participants.RemoteParticipantIdentities().Count} peers");
if (!string.IsNullOrEmpty(specialRecipient))
sceneMessage.AddSpecialRecipient(specialRecipient);

MessageWrap<Scene> sceneMessage = messagePipe.NewMessage<Scene>();
sceneMessage.Payload.Data = ByteString.CopyFrom(message);
sceneMessage.Payload.SceneId = sceneId;
sceneMessage.SendAndDisposeAsync(ct, DataPacketKind.KindReliable).Forget();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void AddSceneMessageHandler(string sceneId, ISceneCommunicationPipe.MsgTy

public void RemoveSceneMessageHandler(string sceneId, ISceneCommunicationPipe.MsgType msgType, ISceneCommunicationPipe.SceneMessageHandler onSceneMessage) { }

public void SendMessage(ReadOnlySpan<byte> message, string sceneId, ISceneCommunicationPipe.ConnectivityAssertiveness assertiveness, CancellationToken ct)
public void SendMessage(ReadOnlySpan<byte> message, string sceneId, ISceneCommunicationPipe.ConnectivityAssertiveness assertiveness, CancellationToken ct, string specialRecipient = null)
{
sendMessageCalls.Add(message.ToArray());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CrdtEcsBridge.PoolsProviders;
using DCL.Diagnostics;
using JetBrains.Annotations;
using Microsoft.ClearScript;
using Microsoft.ClearScript.JavaScript;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -30,8 +31,7 @@ protected override void DisposeInternal()
lastInput.Clear();
}

[UsedImplicitly]
public object SendBinary(IList<object> dataList)
private void SendBinary(IList<object> dataList, string? recipient)
{
try
{
Expand Down Expand Up @@ -62,13 +62,44 @@ public object SendBinary(IList<object> dataList)
lastInput.RemoveAt(lastIndex);
}

return api.SendBinary(lastInput);
api.SendBinary(lastInput, recipient);
}
catch (Exception e)
{
ReportHub.LogException(e, ReportCategory.ENGINE);
throw;
}
}

[UsedImplicitly]
public object SendBinary(IList<object> broadcastData)
{
SendBinary(broadcastData, null);
return api.GetResult();
}

[UsedImplicitly]
public object SendBinary(IList<object> broadcastData, object? perRecipientData)
{
SendBinary(broadcastData, null);

if (perRecipientData is IList<object> perRecipientDataList)
{
for (var i = 0; i < perRecipientDataList.Count; i++)
{
object? obj = perRecipientDataList[i];

if (obj is IScriptObject perRecipientStruct)
{
var recipient = (string)perRecipientStruct.GetProperty("address");
var data = (IList<object>)perRecipientStruct.GetProperty("data");

SendBinary(data, recipient);
}
}
}

return api.GetResult();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace SceneRuntime.Apis.Modules.CommunicationsControllerApi
{
public interface ICommunicationsControllerAPI : IDisposable
{
object SendBinary(IReadOnlyList<PoolableByteArray> data);
void SendBinary(IReadOnlyList<PoolableByteArray> broadcastData, string? recipient = null);

object GetResult();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports.sendBinary = async function(message) {
const resultData = UnityCommunicationsControllerApi.SendBinary(message.data)
const resultData = UnityCommunicationsControllerApi.SendBinary(message.data, message.messagesToAddress)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const resultData = UnityCommunicationsControllerApi.SendBinary(message.data, message.messagesToAddress)
const resultData = UnityCommunicationsControllerApi.SendBinary(message.data, message.peerData)

return {
data: resultData
};
Expand Down
Loading