// // BaseNostaleClient.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 NosCore.Packets.Interfaces; using NosSmooth.Core.Commands; using NosSmooth.Core.Packets; using Remora.Results; namespace NosSmooth.Core.Client; /// /// Represents base class of . /// /// /// This class serializes packets and processes commands. /// public abstract class BaseNostaleClient : INostaleClient { private readonly CommandProcessor _commandProcessor; private readonly IPacketSerializer _packetSerializer; /// /// Initializes a new instance of the class. /// /// The command processor. /// The packet serializer. protected BaseNostaleClient ( CommandProcessor commandProcessor, IPacketSerializer packetSerializer ) { _commandProcessor = commandProcessor; _packetSerializer = packetSerializer; } /// public abstract Task RunAsync(CancellationToken stopRequested = default); /// public virtual 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 abstract Task SendPacketAsync(string packetString, CancellationToken ct = default); /// public abstract Task ReceivePacketAsync(string packetString, CancellationToken ct = default); /// public virtual Task ReceivePacketAsync(IPacket packet, CancellationToken ct = default) { var serialized = _packetSerializer.Serialize(packet); return serialized.IsSuccess ? ReceivePacketAsync(serialized.Entity, ct) : Task.FromResult(Result.FromError(serialized)); } /// public Task SendCommandAsync(ICommand command, CancellationToken ct = default) => _commandProcessor.ProcessCommand(command, ct); }