Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address WebSocket Byte Array handling #167

Merged
merged 2 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/FishyFlip/ATWebSocketProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ private void Dispose(bool disposing)

private void HandleMessage(byte[] byteArray)
{
if (byteArray.Length == 0)
{
this.logger?.LogDebug("WSS: ATError reading message. Empty byte array.");
return;
}

using var stream = new MemoryStream(byteArray);
CBORObject[]? objects = null;
try
Expand Down Expand Up @@ -286,25 +292,21 @@ private async Task ReceiveMessages(ClientWebSocket webSocket, CancellationToken
try
{
#if NETSTANDARD
var result =
await webSocket.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), token);
var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), token);
if (result is not { MessageType: WebSocketMessageType.Binary, EndOfMessage: true })
{
continue;
}

byte[] newArray = new byte[result.Count];
Array.Copy(receiveBuffer, 0, newArray, 0, result.Count);
var newArray = receiveBuffer.AsSpan(0, result.Count).ToArray();
#else
var result =
await webSocket.ReceiveAsync(new Memory<byte>(receiveBuffer), token);
var result = await webSocket.ReceiveAsync(receiveBuffer, token);
if (result is not { MessageType: WebSocketMessageType.Binary, EndOfMessage: true })
{
continue;
}

byte[] newArray = new byte[result.Count];
Array.Copy(receiveBuffer, 0, newArray, 0, result.Count);
var newArray = receiveBuffer.AsSpan(0, result.Count).ToArray();
#endif

Task.Run(() => this.HandleMessage(newArray)).FireAndForgetSafeAsync(this.logger);
Expand Down
5 changes: 5 additions & 0 deletions src/FishyFlip/Models/FrameHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public class FrameHeader
/// <param name="obj">The CBOR Object.</param>
public FrameHeader(CBORObject obj)
{
if (obj.Count <= 0)
{
return;
}

this.Operation = (FrameHeaderOperation)(obj["op"]?.AsInt32() ?? 0);
this.Type = obj["t"]?.AsString();
}
Expand Down
Loading