~ruther/NosSmooth.Comms

ref: e79a2839b9c316659d9a7b607bd33d8e0beccee8 NosSmooth.Comms/src/Local/NosSmooth.Comms.Inject/MessageResponders/HandshakeResponder.cs -rw-r--r-- 2.7 KiB
e79a2839 — Rutherther feat: update to new split raw and managed client 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
//
//  HandshakeResponder.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 Microsoft.Extensions.Logging;
using NosSmooth.Comms.Core;
using NosSmooth.Comms.Data.Messages;
using NosSmooth.Comms.Data.Responders;
using NosSmooth.LocalBinding;
using Remora.Results;

namespace NosSmooth.Comms.Inject.MessageResponders;

/// <summary>
/// A responder to <see cref="HandshakeRequest"/>.
/// </summary>
public class HandshakeResponder : IMessageResponder<HandshakeRequest>
{
    private readonly ClientState _state;
    private readonly NosBrowserManager _browserManager;
    private readonly ConnectionHandler _connectionHandler;
    private readonly CallbackConfigRepository _config;
    private readonly ILogger<HandshakeResponder> _logger;

    /// <summary>
    /// Initializes a new instance of the <see cref="HandshakeResponder"/> class.
    /// </summary>
    /// <param name="state">The state of the application.</param>
    /// <param name="browserManager">The browser manager.</param>
    /// <param name="connectionHandler">The connection handler.</param>
    /// <param name="config">The config.</param>
    /// <param name="logger">The logger.</param>
    public HandshakeResponder
    (
        ClientState state,
        NosBrowserManager browserManager,
        ConnectionHandler connectionHandler,
        CallbackConfigRepository config,
        ILogger<HandshakeResponder> logger
    )
    {
        _state = state;
        _browserManager = browserManager;
        _connectionHandler = connectionHandler;
        _config = config;
        _logger = logger;
    }

    /// <inheritdoc />
    public async Task<Result> Respond(HandshakeRequest message, CancellationToken ct = default)
    {
        var config = new CallbackConfig(message.SendRawPackets, message.SendDeserializedPackets);
        _config.SetConfig(_connectionHandler, config);

        string? playerName = null;
        long? playerId = null;

        if (_browserManager.IsInGame)
        {
            playerName = _browserManager.PlayerManager.Player.Name;
            playerId = _browserManager.PlayerManager.PlayerId;
        }

        var result = await _connectionHandler.SendMessageAsync
        (
            new HandshakeResponse(_state.IsRunning, _state.InitResult, playerId, playerName),
            ct
        );

        _logger.LogInformation
        (
            "Handshaked with {Identification}! (connection {ConnectionID})",
            message.Identification,
            _connectionHandler.Id
        );

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