~ruther/NosSmooth.Comms

ref: fe65520941bab5c5e7053f94f58a692e1f65a6b2 NosSmooth.Comms/src/Local/NosSmooth.Comms.Inject/CallbackConfigRepository.cs -rw-r--r-- 1.5 KiB
fe655209 — Rutherther chore: put Tcp, NamedPipes and LocalData assemblies into other assemblies to reduce number of unnecessary projects 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
//
//  CallbackConfigRepository.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 NosSmooth.Comms.Core;

namespace NosSmooth.Comms.Inject;

/// <summary>
/// A repository containing configurations for given connection handlers.
/// </summary>
public class CallbackConfigRepository
{
    private readonly ConcurrentDictionary<ConnectionHandler, CallbackConfig> _configs;

    /// <summary>
    /// Initializes a new instance of the <see cref="CallbackConfigRepository"/> class.
    /// </summary>
    public CallbackConfigRepository()
    {
        _configs = new ConcurrentDictionary<ConnectionHandler, CallbackConfig>();
    }

    /// <summary>
    /// Get config of the given connection, or default.
    /// </summary>
    /// <param name="connection">The connection to get config of.</param>
    /// <returns>A config for the connection.</returns>
    public CallbackConfig GetConfig(ConnectionHandler connection)
    {
        return _configs.GetValueOrDefault(connection, new CallbackConfig(false, false));
    }

    /// <summary>
    /// Set config of the given connection.
    /// </summary>
    /// <param name="connection">The connection to set config.</param>
    /// <param name="config">The config to set.</param>
    public void SetConfig(ConnectionHandler connection, CallbackConfig config)
    {
        _configs.AddOrUpdate(connection, _ => config, (a, b) => config);
    }
}
Do not follow this link