~ruther/NosSmooth.Comms

ref: 3b9b88c2fee4419a3325fcc71d7b949d9d63f90e NosSmooth.Comms/src/Core/NosSmooth.Comms.Core/ClientNostaleClient.cs -rw-r--r-- 3.3 KiB
3b9b88c2 — Rutherther chore: add versions for packing 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
//  ClientNostaleClient.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 NosSmooth.Comms.Data;
using NosSmooth.Comms.Data.Messages;
using NosSmooth.Core.Client;
using NosSmooth.Core.Commands;
using NosSmooth.Core.Contracts;
using NosSmooth.Packets;
using NosSmooth.PacketSerializer.Abstractions.Attributes;
using Remora.Results;

namespace NosSmooth.Comms.Core;

/// <summary>
/// A NosTale client using <see cref="IClient"/>.
/// </summary>
public class ClientNostaleClient : INostaleClient
{
    private readonly ConnectionHandler _connection;

    /// <summary>
    /// Initializes a new instance of the <see cref="ClientNostaleClient"/> class.
    /// </summary>
    /// <param name="connection">The connection handler.</param>
    public ClientNostaleClient
        (ConnectionHandler connection)
    {
        _connection = connection;
    }

    /// <inheritdoc />
    public Task<Result> RunAsync(CancellationToken stopRequested = default)
    {
        _connection.StartHandler(stopRequested);
        return Task.FromResult(Result.FromSuccess());
    }

    /// <inheritdoc />
    public async Task<Result> SendPacketAsync(IPacket packet, CancellationToken ct = default)
    {
        var messageResponse = await _connection.ContractSendMessage
                (new PacketMessage(PacketSource.Client, packet))
            .WaitForAsync(DefaultStates.ResponseObtained, ct: ct);
        return messageResponse.IsSuccess ? messageResponse.Entity : Result.FromError(messageResponse);
    }

    /// <inheritdoc />
    public async Task<Result> SendPacketAsync(string packetString, CancellationToken ct = default)
    {
        var messageResponse = await _connection.ContractSendMessage
                (new RawPacketMessage(PacketSource.Client, packetString))
            .WaitForAsync(DefaultStates.ResponseObtained, ct: ct);
        return messageResponse.IsSuccess ? messageResponse.Entity : Result.FromError(messageResponse);
    }

    /// <inheritdoc />
    public async Task<Result> ReceivePacketAsync(string packetString, CancellationToken ct = default)
    {
        var messageResponse = await _connection.ContractSendMessage
                (new RawPacketMessage(PacketSource.Server, packetString))
            .WaitForAsync(DefaultStates.ResponseObtained, ct: ct);
        return messageResponse.IsSuccess ? messageResponse.Entity : Result.FromError(messageResponse);
    }

    /// <inheritdoc />
    public async Task<Result> ReceivePacketAsync(IPacket packet, CancellationToken ct = default)
    {
        var messageResponse = await _connection.ContractSendMessage
                (new PacketMessage(PacketSource.Server, packet))
            .WaitForAsync(DefaultStates.ResponseObtained, ct: ct);
        return messageResponse.IsSuccess ? messageResponse.Entity : Result.FromError(messageResponse);
    }

    /// <inheritdoc />
    public async Task<Result> SendCommandAsync(ICommand command, CancellationToken ct = default)
    {
        var messageResponse = await _connection.ContractSendMessage
                (new CommandMessage(command))
            .WaitForAsync(DefaultStates.ResponseObtained, ct: ct);
        return messageResponse.IsSuccess ? messageResponse.Entity : Result.FromError(messageResponse);
    }
}
Do not follow this link