Skip to content

Commit

Permalink
Merge pull request #6 from tinohager/master
Browse files Browse the repository at this point in the history
Fix buffer receive logic
  • Loading branch information
akacdev authored Aug 20, 2024
2 parents 84690c6 + 1b4c566 commit ec1ce5d
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Certstream/Certstream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,23 @@ public async void Connect()
while (WebSocket.State == WebSocketState.Open)
{
byte[] receiveBuffer = new byte[Constants.BufferSize];

int offset = 0;

while (true)
{
try
{
ArraySegment<byte> bytesReceived = new(receiveBuffer, offset, receiveBuffer.Length);
int remainingBufferSpace = receiveBuffer.Length - offset;

if (remainingBufferSpace <= 0)
{
throw new InvalidOperationException("Buffer overflow: The receive buffer is full.");
}

ArraySegment<byte> bytesReceived = new(receiveBuffer, offset, remainingBufferSpace);

WebSocketReceiveResult result = await WebSocket.ReceiveAsync(bytesReceived, CancellationSource.Token);

offset += result.Count;

if (result.EndOfMessage) break;
Expand Down

0 comments on commit ec1ce5d

Please sign in to comment.