Skip to content

Commit

Permalink
Use byte array in message received to avoid some unnecessary encoding (
Browse files Browse the repository at this point in the history
  • Loading branch information
campersau authored Sep 3, 2024
1 parent 68d60c7 commit 40cdf62
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
6 changes: 5 additions & 1 deletion lib/PuppeteerSharp/Cdp/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,11 @@ private async Task ProcessMessage(MessageReceivedEventArgs e)
return;
}

_logger.LogTrace("◀ Receive {Message}", response);
if (_logger.IsEnabled(LogLevel.Trace))
{
_logger.LogTrace("◀ Receive {Message}", Encoding.UTF8.GetString(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(string message) => Message = message;
public MessageReceivedEventArgs(byte[] message) => Message = message;

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

while (!IsClosed)
{
var endOfMessage = false;
var response = new StringBuilder();

while (!endOfMessage)
MemoryStream memoryStream = null;
WebSocketReceiveResult result;
do
{
WebSocketReceiveResult result;
try
{
result = await _client.ReceiveAsync(
Expand All @@ -156,31 +155,35 @@ private async Task<object> GetResponseAsync(CancellationToken cancellationToken)
}
catch (OperationCanceledException)
{
return null;
return;
}
catch (Exception ex)
{
OnClose(ex.Message);
return null;
return;
}

endOfMessage = result.EndOfMessage;

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

if (memoryStream is null && !result.EndOfMessage)
{
memoryStream = new MemoryStream(buffer.Length);
}

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

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

return null;
}

private void OnClose(string closeReason)
Expand Down

0 comments on commit 40cdf62

Please sign in to comment.