using Quasar.Common.Enums;
using Quasar.Common.Messages;
using Quasar.Common.Networking;
using Quasar.Server.Networking;
namespace Quasar.Server.Messages
{
///
/// Handles messages for the interaction with the remote client status.
///
public class ClientStatusHandler : MessageProcessorBase
{
///
/// Represents the method that will handle status updates.
///
/// The message handler which raised the event.
/// The client which updated the status.
/// The new status.
public delegate void StatusUpdatedEventHandler(object sender, Client client, string statusMessage);
///
/// Represents the method that will handle user status updates.
///
/// The message handler which raised the event.
/// The client which updated the user status.
/// The new user status.
public delegate void UserStatusUpdatedEventHandler(object sender, Client client, UserStatus userStatusMessage);
///
/// Raised when a client updated its status.
///
///
/// Handlers registered with this event will be invoked on the
/// chosen when the instance was constructed.
///
public event StatusUpdatedEventHandler StatusUpdated;
///
/// Raised when a client updated its user status.
///
///
/// Handlers registered with this event will be invoked on the
/// chosen when the instance was constructed.
///
public event UserStatusUpdatedEventHandler UserStatusUpdated;
///
/// Reports an updated status.
///
/// The client which updated the status.
/// The new status.
private void OnStatusUpdated(Client client, string statusMessage)
{
SynchronizationContext.Post(c =>
{
var handler = StatusUpdated;
handler?.Invoke(this, (Client) c, statusMessage);
}, client);
}
///
/// Reports an updated user status.
///
/// The client which updated the user status.
/// The new user status.
private void OnUserStatusUpdated(Client client, UserStatus userStatusMessage)
{
SynchronizationContext.Post(c =>
{
var handler = UserStatusUpdated;
handler?.Invoke(this, (Client) c, userStatusMessage);
}, client);
}
///
/// Initializes a new instance of the class.
///
public ClientStatusHandler() : base(true)
{
}
///
public override bool CanExecute(IMessage message) => message is SetStatus || message is SetUserStatus;
///
public override bool CanExecuteFrom(ISender sender) => true;
///
public override void Execute(ISender sender, IMessage message)
{
switch (message)
{
case SetStatus status:
Execute((Client) sender, status);
break;
case SetUserStatus userStatus:
Execute((Client) sender, userStatus);
break;
}
}
private void Execute(Client client, SetStatus message)
{
OnStatusUpdated(client, message.Message);
}
private void Execute(Client client, SetUserStatus message)
{
OnUserStatusUpdated(client, message.Message);
}
}
}