~ruther/NosSmooth

e736a777161b6c911dbc77f38b3d68abfe1c5122 — František Boháček 2 years ago 0a2c083
feat(packets): merge send and receive packet handling to one method
2 files changed, 28 insertions(+), 54 deletions(-)

M Core/NosSmooth.Core/Packets/PacketHandler.cs
M Samples/FileClient/Client.cs
M Core/NosSmooth.Core/Packets/PacketHandler.cs => Core/NosSmooth.Core/Packets/PacketHandler.cs +2 -41
@@ 38,51 38,12 @@ public class PacketHandler
    /// Calls a responder for the given packet.
    /// </summary>
    /// <param name="client">The current NosTale client.</param>
    /// <param name="packetType">The source of the packet.</param>
    /// <param name="packet">The packet.</param>
    /// <param name="packetString">The string of the packet.</param>
    /// <param name="ct">The cancellation token for cancelling the operation.</param>
    /// <returns>A result that may or may not have succeeded.</returns>
    public Task<Result> HandleReceivedPacketAsync
    (
        INostaleClient client,
        IPacket packet,
        string packetString,
        CancellationToken ct = default
    )
        => HandlePacketAsync
        (
            client,
            PacketSource.Server,
            packet,
            packetString,
            ct
        );

    /// <summary>
    /// Calls a responder for the given packet.
    /// </summary>
    /// <param name="client">The current NosTale client.</param>
    /// <param name="packet">The packet.</param>
    /// <param name="packetString">The string of the packet.</param>
    /// <param name="ct">The cancellation token for cancelling the operation.</param>
    /// <returns>A result that may or may not have succeeded.</returns>
    public Task<Result> HandleSentPacketAsync
    (
        INostaleClient client,
        IPacket packet,
        string packetString,
        CancellationToken ct = default
    )
        => HandlePacketAsync
        (
            client,
            PacketSource.Client,
            packet,
            packetString,
            ct
        );

    private Task<Result> HandlePacketAsync
    public Task<Result> HandlePacketAsync
    (
        INostaleClient client,
        PacketSource packetType,

M Samples/FileClient/Client.cs => Samples/FileClient/Client.cs +26 -13
@@ 36,7 36,8 @@ public class Client : BaseNostaleClient
    /// <param name="commandProcessor">The command processor.</param>
    /// <param name="packetSerializer">The packet serializer.</param>
    /// <param name="logger">The logger.</param>
    public Client(
    public Client
    (
        Stream stream,
        PacketHandler packetHandler,
        CommandProcessor commandProcessor,


@@ 77,16 78,14 @@ public class Client : BaseNostaleClient

            var source = type == "Recv" ? PacketSource.Server : PacketSource.Client;
            var packet = CreatePacket(packetStr, source);
            Result result;
            if (source == PacketSource.Client)
            {
                result = await _packetHandler.HandleSentPacketAsync(this, packet, packetStr, stopRequested);
            }
            else
            {
                result = await _packetHandler.HandleReceivedPacketAsync(this, packet, packetStr, stopRequested);
            }

            Result result = await _packetHandler.HandlePacketAsync
            (
                this,
                source,
                packet,
                packetStr,
                stopRequested
            );
            if (!result.IsSuccess)
            {
                _logger.LogResultError(result);


@@ 99,13 98,27 @@ public class Client : BaseNostaleClient
    /// <inheritdoc/>
    public override async Task<Result> SendPacketAsync(string packetString, CancellationToken ct = default)
    {
        return await _packetHandler.HandleReceivedPacketAsync(this, CreatePacket(packetString, PacketSource.Client), packetString, ct);
        return await _packetHandler.HandlePacketAsync
        (
            this,
            PacketSource.Client,
            CreatePacket(packetString, PacketSource.Client),
            packetString,
            ct
        );
    }

    /// <inheritdoc/>
    public override async Task<Result> ReceivePacketAsync(string packetString, CancellationToken ct = default)
    {
        return await _packetHandler.HandleReceivedPacketAsync(this, CreatePacket(packetString, PacketSource.Server), packetString, ct);
        return await _packetHandler.HandlePacketAsync
        (
            this,
            PacketSource.Server,
            CreatePacket(packetString, PacketSource.Server),
            packetString,
            ct
        );
    }

    private IPacket CreatePacket(string packetStr, PacketSource source)

Do not follow this link