~ruther/NosSmooth.Comms

ref: 3b9b88c2fee4419a3325fcc71d7b949d9d63f90e NosSmooth.Comms/src/Core/NosSmooth.Comms.Core/NostaleClientResolver.cs -rw-r--r-- 2.1 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
//
//  NostaleClientResolver.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.Collections.Concurrent;
using Microsoft.Extensions.DependencyInjection;
using NosSmooth.Comms.Data;
using NosSmooth.Core.Client;

namespace NosSmooth.Comms.Core;

/// <summary>
/// Resolves <see cref="IConnection"/>s into <see cref="INostaleClient"/>s.
/// </summary>
/// <remarks>
/// Clients will be connected in case the client is not registered yet.
/// If you wish to register the client yourself, use <see cref="RegisterClient"/>.
/// </remarks>
public class NostaleClientResolver
{
    private readonly IServiceProvider _services;
    private readonly ConcurrentDictionary<IConnection, INostaleClient> _clients;

    /// <summary>
    /// Initializes a new instance of the <see cref="NostaleClientResolver"/> class.
    /// </summary>
    /// <param name="services">The service provider.</param>
    public NostaleClientResolver(IServiceProvider services)
    {
        _services = services;
        _clients = new ConcurrentDictionary<IConnection, INostaleClient>();
    }

    /// <summary>
    /// Resolve the connection handler into nostale client.
    /// </summary>
    /// <param name="connection">The connection.</param>
    /// <returns>The resolved client.</returns>
    public INostaleClient Resolve(ConnectionHandler connection)
    {
        if (!_clients.ContainsKey(connection.Connection))
        {
            RegisterClient(connection, ActivatorUtilities.CreateInstance<ClientNostaleClient>(_services, connection));
        }

        return _clients[connection.Connection];
    }

    /// <summary>
    /// Register the given client for the given connection.
    /// </summary>
    /// <param name="connection">The connection handler.</param>
    /// <param name="client">The client to register for the given handler.</param>
    public void RegisterClient(ConnectionHandler connection, INostaleClient client)
    {
        _clients[connection.Connection] = client;
    }
}
Do not follow this link