Skip to content

Commit

Permalink
Revert "Use byte array in message received to avoid some unnecessary …
Browse files Browse the repository at this point in the history
…encoding (#2748)" (#2750)

This reverts commit a62b86c.
  • Loading branch information
kblok authored Aug 21, 2024
1 parent a62b86c commit 3315b38
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 29 deletions.
7 changes: 1 addition & 6 deletions lib/PuppeteerSharp/Cdp/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -281,11 +280,7 @@ private async Task ProcessMessage(MessageReceivedEventArgs e)
return;
}

if (_logger.IsEnabled(LogLevel.Trace))
{
_logger.LogTrace("◀ Receive {Message}", Encoding.UTF8.GetString(response));
}

_logger.LogTrace("◀ Receive {Message}", response);
ProcessIncomingMessage(obj);
}
catch (Exception ex)
Expand Down
4 changes: 2 additions & 2 deletions lib/PuppeteerSharp/Transport/MessageReceivedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public class MessageReceivedEventArgs : EventArgs
/// Initializes a new instance of the <see cref="PuppeteerSharp.Transport.MessageReceivedEventArgs"/> class.
/// </summary>
/// <param name="message">Message.</param>
public MessageReceivedEventArgs(byte[] message) => Message = message;
public MessageReceivedEventArgs(string message) => Message = message;

/// <summary>
/// Transport message.
/// </summary>
public byte[] Message { get; }
public string Message { get; }
}
}
39 changes: 18 additions & 21 deletions lib/PuppeteerSharp/Transport/WebSocketTransport.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -139,16 +138,18 @@ private static void ScheduleTransportTask(Func<CancellationToken, Task> taskFact
/// Starts listening the socket.
/// </summary>
/// <returns>The start.</returns>
private async Task GetResponseAsync(CancellationToken cancellationToken)
private async Task<object> GetResponseAsync(CancellationToken cancellationToken)
{
var buffer = new byte[2048];

while (!IsClosed)
{
MemoryStream memoryStream = null;
WebSocketReceiveResult result;
do
var endOfMessage = false;
var response = new StringBuilder();

while (!endOfMessage)
{
WebSocketReceiveResult result;
try
{
result = await _client.ReceiveAsync(
Expand All @@ -157,35 +158,31 @@ private async Task GetResponseAsync(CancellationToken cancellationToken)
}
catch (OperationCanceledException)
{
return;
return null;
}
catch (Exception ex)
{
OnClose(ex.Message);
return;
return null;
}

if (result.MessageType == WebSocketMessageType.Close)
endOfMessage = result.EndOfMessage;

if (result.MessageType == WebSocketMessageType.Text)
{
OnClose("WebSocket closed");
return;
response.Append(Encoding.UTF8.GetString(buffer, 0, result.Count));
}
else if (result.MessageType == WebSocketMessageType.Binary)
else if (result.MessageType == WebSocketMessageType.Close)
{
continue;
}

if (memoryStream is null && !result.EndOfMessage)
{
memoryStream = new MemoryStream(buffer.Length);
OnClose("WebSocket closed");
return null;
}

memoryStream?.Write(buffer, 0, result.Count);
}
while (!result.EndOfMessage);

MessageReceived?.Invoke(this, new MessageReceivedEventArgs(memoryStream is null ? buffer.AsSpan(0, result.Count).ToArray() : memoryStream.ToArray()));
MessageReceived?.Invoke(this, new MessageReceivedEventArgs(response.ToString()));
}

return null;
}

private void OnClose(string closeReason)
Expand Down

0 comments on commit 3315b38

Please sign in to comment.