This commit is contained in:
Stef Heyenrath
2026-02-11 22:39:41 +01:00
parent 547b5673c0
commit 1bac18aeb1
5 changed files with 40 additions and 3 deletions

4
.github/copilot-instructions.md vendored Normal file
View File

@@ -0,0 +1,4 @@
# Copilot Instructions
## Project Guidelines
- All new byte[xx] calls should use using var data = ArrayPool<byte>.Shared.Lease(xx); instead of directly allocating byte arrays

View File

@@ -58,6 +58,12 @@ internal class WebSocketMessageBuilder : IWebSocketMessageBuilder
return this; return this;
} }
public IWebSocketMessageBuilder Close()
{
ShouldClose = true;
return this;
}
internal enum MessageType internal enum MessageType
{ {
Text, Text,

View File

@@ -0,0 +1,20 @@
// Copyright © WireMock.Net
namespace System.Buffers;
internal sealed class Lease<T>(ArrayPool<T> pool, int length) : IDisposable
{
public T[] Rented { get; } = pool.Rent(length);
public static implicit operator T[](Lease<T> lease) => lease.Rented;
public void Dispose()
{
pool.Return(Rented, true);
}
}
internal static class ArrayPoolExtensions
{
public static Lease<T> Lease<T>(this ArrayPool<T> source, int length) => new(source, length);
}

View File

@@ -49,4 +49,10 @@ public interface IWebSocketMessageBuilder
/// </summary> /// </summary>
[PublicAPI] [PublicAPI]
IWebSocketMessageBuilder AndClose(); IWebSocketMessageBuilder AndClose();
/// <summary>
/// Close the WebSocket connection.
/// </summary>
[PublicAPI]
IWebSocketMessageBuilder Close();
} }

View File

@@ -1,5 +1,6 @@
// Copyright © WireMock.Net // Copyright © WireMock.Net
using System.Buffers;
using System.Net.WebSockets; using System.Net.WebSockets;
using System.Text; using System.Text;
@@ -12,9 +13,9 @@ internal static class ClientWebSocketExtensions
return client.SendAsync(new ArraySegment<byte>(Encoding.UTF8.GetBytes(text)), WebSocketMessageType.Text, endOfMessage, cancellationToken); return client.SendAsync(new ArraySegment<byte>(Encoding.UTF8.GetBytes(text)), WebSocketMessageType.Text, endOfMessage, cancellationToken);
} }
internal static async Task<string> ReceiveAsTextAsync(this ClientWebSocket client, CancellationToken cancellationToken = default) internal static async Task<string> ReceiveAsTextAsync(this ClientWebSocket client, int bufferSize = 1024, CancellationToken cancellationToken = default)
{ {
var receiveBuffer = new byte[1024]; using var receiveBuffer = ArrayPool<byte>.Shared.Lease(1024);
var result = await client.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), cancellationToken); var result = await client.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), cancellationToken);
if (result.MessageType != WebSocketMessageType.Text) if (result.MessageType != WebSocketMessageType.Text)
@@ -32,7 +33,7 @@ internal static class ClientWebSocketExtensions
internal static async Task<byte[]> ReceiveAsBytesAsync(this ClientWebSocket client, int bufferSize = 1024, CancellationToken cancellationToken = default) internal static async Task<byte[]> ReceiveAsBytesAsync(this ClientWebSocket client, int bufferSize = 1024, CancellationToken cancellationToken = default)
{ {
var receiveBuffer = new byte[bufferSize]; using var receiveBuffer = ArrayPool<byte>.Shared.Lease(1024);
var result = await client.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), cancellationToken); var result = await client.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), cancellationToken);
if (result.MessageType != WebSocketMessageType.Binary) if (result.MessageType != WebSocketMessageType.Binary)