-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
Session.cs
191 lines (162 loc) · 5.75 KB
/
Session.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using StrawberryShake.Properties;
using StrawberryShake.Transport.WebSockets.Messages;
namespace StrawberryShake.Transport.WebSockets;
/// <inheritdoc />
public sealed class Session : ISession
{
private readonly ISocketClient _socketClient;
private ISocketProtocol? _socketProtocol;
private readonly ConcurrentDictionary<string, SocketOperation> _operations = new();
private bool _disposed;
/// <summary>
/// Creates a new instance <see cref="Session"/>
/// </summary>
public Session(ISocketClient socketClient)
{
_socketClient = socketClient ??
throw new ArgumentNullException(nameof(socketClient));
_socketClient.ReceiveFinished += ReceiveFinishHandler;
}
/// <inheritdoc />
public string Name => _socketClient.Name;
/// <inheritdoc />
public Task<ISocketOperation> StartOperationAsync(
OperationRequest request,
CancellationToken cancellationToken = default)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
return StartOperationAsyncInternal(request, cancellationToken);
}
private async Task<ISocketOperation> StartOperationAsyncInternal(
OperationRequest request,
CancellationToken cancellationToken)
{
EnsureSession(out var socketProtocol);
var operation = new SocketOperation(this);
if (_operations.TryAdd(operation.Id, operation))
{
try
{
socketProtocol.Disposed += (_, _) => BeginStopOperation(operation.Id);
await socketProtocol
.StartOperationAsync(operation.Id, request, cancellationToken)
.ConfigureAwait(false);
}
catch
{
_operations.TryRemove(operation.Id, out _);
throw;
}
}
else
{
throw ThrowHelper.SessionManager_OperationWasAlreadyRegistered(operation.Id);
}
return operation;
}
private void BeginStopOperation(string operationId) =>
Task.Run(async () => await StopOperationAsync(operationId));
/// <inheritdoc />
public async Task StopOperationAsync(
string operationId,
CancellationToken cancellationToken = default)
{
EnsureSession(out ISocketProtocol socketProtocol);
if (_operations.TryRemove(operationId, out SocketOperation? operation))
{
await socketProtocol
.StopOperationAsync(operationId, cancellationToken)
.ConfigureAwait(false);
await operation.DisposeAsync().ConfigureAwait(false);
}
}
/// <inheritdoc />
private async ValueTask CompleteOperation(CancellationToken cancellationToken)
{
foreach (var operation in _operations)
{
await operation.Value.CompleteAsync(cancellationToken);
}
}
/// <inheritdoc />
private void ReceiveFinishHandler(object? sender, EventArgs args)
{
_ = CompleteOperation(default);
}
/// <summary>
/// Opens a session over the socket
/// </summary>
/// <param name="cancellationToken">The cancellation token to cancel the operation</param>
public async Task OpenSessionAsync(CancellationToken cancellationToken = default)
{
ISocketProtocol socketProtocol =
await _socketClient.OpenAsync(cancellationToken)
.ConfigureAwait(false);
_socketProtocol = socketProtocol ??
throw ThrowHelper.SessionManager_SocketWasNotInitialized(_socketClient.Name);
_socketProtocol.Subscribe(ReceiveMessage);
}
/// <summary>
/// Closes a session over the socket
/// </summary>
/// <param name="cancellationToken">The cancellation token to cancel the operation</param>
public async Task CloseSessionAsync(CancellationToken cancellationToken = default)
{
await _socketClient.CloseAsync(
Resources.SocketClient_AllOperationsFinished,
SocketCloseStatus.NormalClosure,
cancellationToken)
.ConfigureAwait(false);
}
/// <summary>
/// Receive a message from the socket
/// </summary>
/// <param name="operationId">Id of the operation</param>
/// <param name="message">The payload of the message</param>
/// <param name="cancellationToken">
/// The cancellation token to cancel
/// </param>
private async ValueTask ReceiveMessage(
string operationId,
OperationMessage message,
CancellationToken cancellationToken = default)
{
if (_operations.TryGetValue(operationId, out SocketOperation? operation))
{
await operation.ReceiveMessageAsync(message, cancellationToken);
}
}
private void EnsureSession(out ISocketProtocol socketProtocol)
{
socketProtocol = _socketProtocol ??
throw ThrowHelper.SessionManager_SessionIsNotOpen();
}
/// <inheritdoc />
public async ValueTask DisposeAsync()
{
if (!_disposed)
{
_disposed = true;
if (_operations.Count > 0)
{
SocketOperation[] operations = _operations.Values.ToArray();
for (var i = 0; i < operations.Length; i++)
{
await operations[i].DisposeAsync();
}
_operations.Clear();
}
_socketClient.ReceiveFinished -= ReceiveFinishHandler;
_socketProtocol?.Unsubscribe(ReceiveMessage);
await _socketClient.DisposeAsync();
}
}
}