-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMbsSdk.cs
executable file
·77 lines (68 loc) · 2.12 KB
/
MbsSdk.cs
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
using Sportradar.Mbs.Sdk.Internal.Protocol;
using Sportradar.Mbs.Sdk.Internal.Utils;
using Sportradar.Mbs.Sdk.Protocol;
namespace Sportradar.Mbs.Sdk;
/// <summary>
/// Represents the main class for interacting with the MBS service.
/// </summary>
public class MbsSdk : IDisposable
{
private readonly object _lock;
private readonly ProtocolProvider _protocolProvider;
private readonly Action<MbsSdk, Exception>? _unhandledExceptionHandler;
private bool _connected;
private bool _disposed;
/// <summary>
/// Initializes a new instance of the <see cref="MbsSdk"/> class.
/// </summary>
/// <param name="config">The configuration for the SDK.</param>
public MbsSdk(MbsSdkConfig config)
{
_unhandledExceptionHandler = config.UnhandledExceptionHandler;
_protocolProvider = new ProtocolProvider(config);
_protocolProvider.UnhandledException += ProtocolProviderOnUnhandledException;
_lock = new object();
}
/// <summary>
/// Gets the ticket protocol.
/// </summary>
public ITicketProtocol TicketProtocol => _protocolProvider.TicketProtocol;
/// <summary>
/// Disposes the SDK and releases all resources.
/// </summary>
public void Dispose()
{
lock (_lock)
{
if (_disposed) return;
_disposed = true;
ExcSuppress.Dispose(_protocolProvider);
}
}
/// <summary>
/// Connects to the MBS service.
/// </summary>
public void Connect()
{
lock (_lock)
{
if (_disposed) throw new ObjectDisposedException(nameof(MbsSdk));
if (_connected) return;
Task.Run(async () => await _protocolProvider.Connect()).Wait();
_connected = true;
}
}
/// <summary>
/// Handles unhandled exceptions from the protocol provider.
/// </summary>
private void ProtocolProviderOnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
_unhandledExceptionHandler?.Invoke(this, (e.ExceptionObject as Exception)!);
}
catch
{
}
}
}