forked from NethermindEth/nethermind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensions.cs
More file actions
84 lines (76 loc) · 3.14 KB
/
Extensions.cs
File metadata and controls
84 lines (76 loc) · 3.14 KB
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
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System;
using System.Linq;
using System.Net.WebSockets;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Primitives;
using Nethermind.Logging;
namespace Nethermind.Sockets;
public static class Extensions
{
public static void UseWebSocketsModules(this IApplicationBuilder app)
{
IWebSocketsManager? webSocketsManager;
ILogger logger;
using (var scope = app.ApplicationServices.CreateScope())
{
webSocketsManager = scope.ServiceProvider.GetService<IWebSocketsManager>();
logger = scope.ServiceProvider.GetService<ILogManager>()?.GetClassLogger() ?? default;
}
app.Run(async (context) =>
{
string id = string.Empty;
string clientName = string.Empty;
IWebSocketsModule? module = null;
try
{
string moduleName = string.Empty;
if (context.Request.Path.HasValue)
{
var path = context.Request.Path.Value;
moduleName = path.Split("/", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).FirstOrDefault() ?? string.Empty;
}
module = webSocketsManager?.GetModule(moduleName);
if (module is null)
{
context.Response.StatusCode = 400;
return;
}
clientName = context.Request.Query.TryGetValue("client", out StringValues clientValues)
? clientValues.FirstOrDefault() ?? string.Empty
: string.Empty;
if (logger.IsDebug) logger.Info($"Initializing WebSockets for client: '{clientName}'.");
using WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync();
using ISocketsClient socketsClient = await module.CreateClient(webSocket, clientName, context);
id = socketsClient.Id;
await socketsClient.ReceiveLoopAsync();
}
catch (WebSocketException ex)
{
if (ex.WebSocketErrorCode == WebSocketError.InvalidState)
{
// Websocket closed
if (logger.IsDebug) logger.Debug($"WebSockets error {ex.WebSocketErrorCode}: {ex.WebSocketErrorCode} {ex}");
}
else
{
if (logger.IsError) logger.Error($"WebSockets error {ex.WebSocketErrorCode}: {ex.WebSocketErrorCode} {ex.Message}", ex);
}
}
catch (Exception ex)
{
logger.Error($"WebSockets error {ex.Message}", ex);
}
finally
{
if (module is not null && !string.IsNullOrWhiteSpace(id))
{
module.RemoveClient(id);
if (logger.IsDebug) logger.Info($"Closing WebSockets for client: '{clientName}'.");
}
}
});
}
}