~ruther/NosSmooth.Comms

ref: ae864d55d0a530321f0f9e9d9a2df6e6195e3bed NosSmooth.Comms/src/Local/NosSmooth.Comms.Inject/MessageResponders/RunClientResponder.cs -rw-r--r-- 2.3 KiB
ae864d55 — František Boháček feat: add RunClientRequest message with support of setting binding options 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
//
//  RunClientResponder.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.Core;
using NosSmooth.Comms.Data.Responders;
using NosSmooth.Comms.Inject.Messages;
using Remora.Results;

namespace NosSmooth.Comms.Inject.MessageResponders;

/// <summary>
/// A responder to <see cref="RunClientRequest"/>.
/// </summary>
public class RunClientResponder : IMessageResponder<RunClientRequest>
{
    private readonly ClientState _state;
    private readonly ConnectionHandler _connectionHandler;

    /// <summary>
    /// Initializes a new instance of the <see cref="RunClientResponder"/> class.
    /// </summary>
    /// <param name="state">The client state.</param>
    /// <param name="connectionHandler">The connection handler wrapper.</param>
    public RunClientResponder
    (
        ClientState state,
        ConnectionHandler connectionHandler
    )
    {
        _state = state;
        _connectionHandler = connectionHandler;
    }

    /// <inheritdoc />
    public async Task<Result> Respond(RunClientRequest request, CancellationToken ct = default)
    {
        _state.HookOptions = request.HookOptions;
        _state.NetworkManagerOptions = request.NetworkManagerOptions;
        _state.NtClientOptions = request.NtClientOptions;
        _state.PetManagerOptions = request.PetManagerOptions;
        _state.PlayerManagerOptions = request.PlayerManagerOptions;
        _state.SceneManagerOptions = request.SceneManagerOptions;
        _state.UnitManagerOptions = request.UnitManagerOptions;
        _state.UnitManagerOptions = request.UnitManagerOptions;

        if (!_state.Starting.IsCancellationRequested)
        { // start the client.
            _state.Starting.Cancel();
        }

        if (!_state.Started.IsCancellationRequested)
        { // wait until the client has started.
            try
            {
                await Task.Delay(-1, _state.Started.Token);
            }
            catch
            {
                // ignored
            }
        }

        var sendMessageResult = await _connectionHandler.SendMessageAsync
            (new RunClientResponse(_state.InitResult, _state.BindingResult), ct);

        return sendMessageResult.IsSuccess ? Result.FromSuccess() : Result.FromError(sendMessageResult);
    }
}
Do not follow this link