-
Notifications
You must be signed in to change notification settings - Fork 4
/
WebSocketHelper.cs
127 lines (113 loc) · 5.02 KB
/
WebSocketHelper.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
#region Related components
using System;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Microsoft.IO;
using net.vieapps.Components.Utility;
using net.vieapps.Components.WebSockets.Exceptions;
#endregion
namespace net.vieapps.Components.WebSockets
{
public static class WebSocketHelper
{
static RecyclableMemoryStreamManager RecyclableMemoryStreamManager { get; set; }
/// <summary>
/// Gets or sets the size (length) of the protocol buffer used to receive and parse frames
/// </summary>
public static int ReceiveBufferSize { get; internal set; } = 16 * 1024;
/// <summary>
/// Gets or sets the agent name of the protocol for working with related headers
/// </summary>
public static string AgentName { get; internal set; } = "VIEApps NGX WebSockets";
/// <summary>
/// Gets a factory to get recyclable memory stream with RecyclableMemoryStreamManager class to limit LOH fragmentation and improve performance
/// </summary>
/// <returns></returns>
public static Func<MemoryStream> GetRecyclableMemoryStreamFactory()
=> () =>
{
try
{
WebSocketHelper.RecyclableMemoryStreamManager = WebSocketHelper.RecyclableMemoryStreamManager ?? UtilityService.GetRecyclableMemoryStreamManager(16 * 1024, 4, 128 * 1024);
return WebSocketHelper.RecyclableMemoryStreamManager.GetStream();
}
catch
{
return new MemoryStream();
}
};
/// <summary>
/// Reads the header
/// </summary>
/// <param name="stream">The stream to read from</param>
/// <param name="cancellationToken">The cancellation token</param>
/// <returns>The HTTP header</returns>
public static async ValueTask<string> ReadHeaderAsync(this Stream stream, CancellationToken cancellationToken = default)
{
var buffer = new byte[WebSocketHelper.ReceiveBufferSize];
var offset = 0;
int read;
do
{
if (offset >= WebSocketHelper.ReceiveBufferSize)
throw new EntityTooLargeException("Header is too large to fit into the buffer");
#if NETSTANDARD2_0
read = await stream.ReadAsync(buffer, offset, WebSocketHelper.ReceiveBufferSize - offset, cancellationToken).ConfigureAwait(false);
#else
read = await stream.ReadAsync(buffer.AsMemory(offset, WebSocketHelper.ReceiveBufferSize - offset), cancellationToken).ConfigureAwait(false);
#endif
offset += read;
var header = buffer.GetString(offset);
// as per specs, all headers should end like this
if (header.Contains("\r\n\r\n"))
return header;
}
while (read > 0);
return string.Empty;
}
/// <summary>
/// Writes the header
/// </summary>
/// <param name="stream">The stream to write to</param>
/// <param name="header">The header (without the new line characters)</param>
/// <param name="cancellationToken">The cancellation token</param>
/// <returns></returns>
public static async ValueTask WriteHeaderAsync(this Stream stream, string header, CancellationToken cancellationToken = default)
=> await stream.WriteAsync((header.Trim() + "\r\n\r\n").ToArraySegment(), cancellationToken).ConfigureAwait(false);
internal static string ComputeAcceptKey(this string key)
=> (key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").GetHash("SHA1").ToBase64();
internal static string NegotiateSubProtocol(this IEnumerable<string> requestedSubProtocols, IEnumerable<string> supportedSubProtocols)
=> requestedSubProtocols == null || supportedSubProtocols == null || !requestedSubProtocols.Any() || !supportedSubProtocols.Any()
? null
: requestedSubProtocols.Intersect(supportedSubProtocols).FirstOrDefault() ?? throw new SubProtocolNegotiationFailedException("Unable to negotiate a sub-protocol");
internal static void SetOptions(this Socket socket, bool noDelay = true, bool dualMode = false, uint keepaliveInterval = 60000, uint retryInterval = 10000)
{
// general options
socket.NoDelay = noDelay;
if (dualMode)
{
socket.DualMode = true;
socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
}
// specifict options (only avalable when running on Windows)
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
socket.IOControl(IOControlCode.KeepAliveValues, ((uint)1).ToBytes().Concat(keepaliveInterval.ToBytes(), retryInterval.ToBytes()), null);
}
internal static Dictionary<string, string> ToDictionary(this string @string, Action<Dictionary<string, string>> onPreCompleted = null)
{
var dictionary = string.IsNullOrWhiteSpace(@string)
? new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
: @string.Replace("\r", "").ToList("\n")
.Where(header => header.IndexOf(":") > 0)
.ToDictionary(header => header.Left(header.IndexOf(":")).Trim(), header => header.Right(header.Length - header.IndexOf(":") - 1).Trim(), StringComparer.OrdinalIgnoreCase);
onPreCompleted?.Invoke(dictionary);
return dictionary;
}
}
}