feat: bump max receive size to 1 MiB

This commit is contained in:
Anna 2022-07-30 00:37:06 -04:00
parent 1854c165d4
commit 6649efc232
1 changed files with 7 additions and 6 deletions

View File

@ -16,19 +16,20 @@ public static class Ext {
} }
public static async Task<ResponseContainer> ReceiveMessage(this ClientWebSocket client) { public static async Task<ResponseContainer> ReceiveMessage(this ClientWebSocket client) {
var bytes = new ArraySegment<byte>(new byte[2048]); var bytes = new List<byte>(2048);
var buffer = new ArraySegment<byte>(new byte[2048]);
WebSocketReceiveResult result; WebSocketReceiveResult result;
var i = 0;
do { do {
result = await client.ReceiveAsync(bytes[i..], CancellationToken.None); result = await client.ReceiveAsync(buffer, CancellationToken.None);
i += result.Count; bytes.AddRange(buffer[..result.Count]);
if (i >= bytes.Count) { // 1 MiB
if (bytes.Count > 1_048_576) {
throw new Exception(); throw new Exception();
} }
} while (!result.EndOfMessage); } while (!result.EndOfMessage);
return MessagePackSerializer.Deserialize<ResponseContainer>(bytes[..i]); return MessagePackSerializer.Deserialize<ResponseContainer>(bytes.ToArray());
} }
} }