// // ManagedNostaleClient.cs // // Copyright (c) František Boháček. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.Threading; using System.Threading.Tasks; using NosSmooth.Core.Commands; using NosSmooth.Packets; using NosSmooth.PacketSerializer; using Remora.Results; namespace NosSmooth.Core.Client; /// /// A NosTale client that supports sending and receiving packets using . /// public class ManagedNostaleClient : INostaleClient { private readonly INostaleClient _rawClient; private readonly IPacketSerializer _packetSerializer; /// /// Initializes a new instance of the class. /// /// The raw nostale client. /// The packet serializer. protected ManagedNostaleClient ( INostaleClient rawClient, IPacketSerializer packetSerializer ) { _rawClient = rawClient; _packetSerializer = packetSerializer; } /// /// Receives the given packet. /// /// The packet to receive. /// The cancellation token for cancelling the operation. /// A result that may or may not have succeeded. public Task ReceivePacketAsync(IPacket packet, CancellationToken ct = default) { var serialized = _packetSerializer.Serialize(packet); return serialized.IsSuccess ? ReceivePacketAsync(serialized.Entity, ct) : Task.FromResult(Result.FromError(serialized)); } /// /// Sends the given packet to the server. /// /// The packet to send. /// The cancellation token for cancelling the operation. /// A result that may or may not have succeeded. public Task SendPacketAsync(IPacket packet, CancellationToken ct = default) { var serialized = _packetSerializer.Serialize(packet); return serialized.IsSuccess ? SendPacketAsync(serialized.Entity, ct) : Task.FromResult(Result.FromError(serialized)); } /// public Task RunAsync(CancellationToken stopRequested = default) => _rawClient.RunAsync(stopRequested); /// public Task SendPacketAsync(string packetString, CancellationToken ct = default) => _rawClient.SendPacketAsync(packetString, ct); /// public Task ReceivePacketAsync(string packetString, CancellationToken ct = default) => _rawClient.ReceivePacketAsync(packetString, ct); /// public Task SendCommandAsync(ICommand command, CancellationToken ct = default) => _rawClient.SendCommandAsync(command, ct); }