// Copyright (c) Arlo Godfrey. All Rights Reserved.
// Licensed under the GNU Lesser General Public License, Version 3.0 with additional terms.
// See the LICENSE, LICENSE.LESSER and LICENSE.ADDITIONAL files in the project root for more information.
using System;
using System.Threading;
using System.Threading.Tasks;
using OwlCore.Remoting;
using OwlCore.Remoting.Transfer;
using OwlCore.Remoting.Transfer.MessageConverters;
namespace StrixMusic.Sdk.Plugins.CoreRemote
{
///
/// The remote message handler use for external cores.
///
public sealed class RemoteCoreMessageHandler : IRemoteMessageHandler
{
///
/// The message handler used when we have the actual core implementation.
///
public static RemoteCoreMessageHandler SingletonHost { get; } = new RemoteCoreMessageHandler(RemotingMode.Host);
///
/// The message handler used when the core implementation is elsewhere and we want to remote control it.
///
public static RemoteCoreMessageHandler SingletonClient { get; } = new RemoteCoreMessageHandler(RemotingMode.Client);
///
/// Creates a new instance of .
///
/// The mode to use for this handler.
/// If , it's assumed we have the actual core implementation, else we're a and are controlling a remote core.
///
public RemoteCoreMessageHandler(RemotingMode remotingMode)
{
Mode = remotingMode;
}
///
public RemotingMode Mode { get; set; }
///
public MemberSignatureScope MemberSignatureScope { get; set; } = MemberSignatureScope.AssemblyQualifiedName;
///
public IRemoteMessageConverter MessageConverter { get; } = new NewtonsoftRemoteMessageConverter();
///
public bool IsInitialized { get; private set; }
///
public event EventHandler? MessageReceived;
///
public Task InitAsync(CancellationToken cancellationToken = default)
{
IsInitialized = true;
return Task.CompletedTask;
}
///
/// Digests an incoming remote message.
///
/// The message to digest.
public void DigestMessageAsync(IRemoteMessage message) => MessageReceived?.Invoke(this, message);
///
/// Raised when an outbound message is created.
///
public event EventHandler? MessageOutbound;
///
public Task SendMessageAsync(IRemoteMessage memberMessage, CancellationToken? cancellationToken = null)
{
MessageOutbound?.Invoke(this, memberMessage);
return Task.CompletedTask;
}
}
}