diff --git a/dotnet/src/webdriver/BiDi/Communication/Broker.cs b/dotnet/src/webdriver/BiDi/Communication/Broker.cs index 2e3982b4bdccb..433f6a604a262 100644 --- a/dotnet/src/webdriver/BiDi/Communication/Broker.cs +++ b/dotnet/src/webdriver/BiDi/Communication/Broker.cs @@ -111,15 +111,17 @@ public async Task ConnectAsync(CancellationToken cancellationToken) await _transport.ConnectAsync(cancellationToken).ConfigureAwait(false); _receiveMessagesCancellationTokenSource = new CancellationTokenSource(); - _receivingMessageTask = _myTaskFactory.StartNew(async () => await ReceiveMessagesAsync(_receiveMessagesCancellationTokenSource.Token), TaskCreationOptions.LongRunning).Unwrap(); - _eventEmitterTask = _myTaskFactory.StartNew(async () => await ProcessEventsAwaiterAsync(), TaskCreationOptions.LongRunning).Unwrap(); + _receivingMessageTask = _myTaskFactory.StartNew(async () => await ReceiveMessagesAsync(_receiveMessagesCancellationTokenSource.Token)).Unwrap(); + _eventEmitterTask = _myTaskFactory.StartNew(ProcessEventsAwaiterAsync).Unwrap(); } private async Task ReceiveMessagesAsync(CancellationToken cancellationToken) { while (!cancellationToken.IsCancellationRequested) { - var message = await _transport.ReceiveAsJsonAsync(_jsonSerializerContext, cancellationToken); + var data = await _transport.ReceiveAsync(cancellationToken).ConfigureAwait(false); + + var message = JsonSerializer.Deserialize(new ReadOnlySpan(data), _jsonSerializerContext.Message); switch (message) { @@ -179,7 +181,7 @@ private async Task ProcessEventsAwaiterAsync() } public async Task ExecuteCommandAsync(TCommand command, CommandOptions? options) - where TCommand: Command + where TCommand : Command { var jsonElement = await ExecuteCommandCoreAsync(command, options).ConfigureAwait(false); @@ -187,13 +189,13 @@ public async Task ExecuteCommandAsync(TCommand comma } public async Task ExecuteCommandAsync(TCommand command, CommandOptions? options) - where TCommand: Command + where TCommand : Command { await ExecuteCommandCoreAsync(command, options).ConfigureAwait(false); } private async Task ExecuteCommandCoreAsync(TCommand command, CommandOptions? options) - where TCommand: Command + where TCommand : Command { command.Id = Interlocked.Increment(ref _currentCommandId); @@ -207,7 +209,9 @@ private async Task ExecuteCommandCoreAsync(TCommand comma _pendingCommands[command.Id] = tcs; - await _transport.SendAsJsonAsync(command, _jsonSerializerContext, cts.Token).ConfigureAwait(false); + var data = JsonSerializer.SerializeToUtf8Bytes(command, typeof(TCommand), _jsonSerializerContext); + + await _transport.SendAsync(data, cts.Token).ConfigureAwait(false); return await tcs.Task.ConfigureAwait(false); } diff --git a/dotnet/src/webdriver/BiDi/Communication/Transport/ITransport.cs b/dotnet/src/webdriver/BiDi/Communication/Transport/ITransport.cs index 777930655eb33..9bd25774ba846 100644 --- a/dotnet/src/webdriver/BiDi/Communication/Transport/ITransport.cs +++ b/dotnet/src/webdriver/BiDi/Communication/Transport/ITransport.cs @@ -20,7 +20,6 @@ using System.Threading.Tasks; using System.Threading; using System; -using System.Text.Json.Serialization; #nullable enable @@ -30,8 +29,7 @@ interface ITransport : IDisposable { Task ConnectAsync(CancellationToken cancellationToken); - Task ReceiveAsJsonAsync(JsonSerializerContext jsonSerializerContext, CancellationToken cancellationToken); + Task ReceiveAsync(CancellationToken cancellationToken); - Task SendAsJsonAsync(TCommand command, JsonSerializerContext jsonSerializerContext, CancellationToken cancellationToken) - where TCommand : Command; + Task SendAsync(byte[] data, CancellationToken cancellationToken); } diff --git a/dotnet/src/webdriver/BiDi/Communication/Transport/WebSocketTransport.cs b/dotnet/src/webdriver/BiDi/Communication/Transport/WebSocketTransport.cs index 9b420c0aa8a37..803881bfbb505 100644 --- a/dotnet/src/webdriver/BiDi/Communication/Transport/WebSocketTransport.cs +++ b/dotnet/src/webdriver/BiDi/Communication/Transport/WebSocketTransport.cs @@ -22,10 +22,8 @@ using System.Net.WebSockets; using System.Threading.Tasks; using System.Threading; -using System.Text.Json; using System.Text; using OpenQA.Selenium.Internal.Logging; -using System.Text.Json.Serialization; #nullable enable @@ -45,7 +43,7 @@ public async Task ConnectAsync(CancellationToken cancellationToken) await _webSocket.ConnectAsync(_uri, cancellationToken).ConfigureAwait(false); } - public async Task ReceiveAsJsonAsync(JsonSerializerContext jsonSerializerContext, CancellationToken cancellationToken) + public async Task ReceiveAsync(CancellationToken cancellationToken) { using var ms = new MemoryStream(); @@ -61,31 +59,28 @@ public async Task ReceiveAsJsonAsync(JsonSerializerContext jsonSerializerC ms.Seek(0, SeekOrigin.Begin); + byte[] data = ms.ToArray(); + if (_logger.IsEnabled(LogEventLevel.Trace)) { - _logger.Trace($"BiDi RCV <-- {Encoding.UTF8.GetString(ms.ToArray())}"); + _logger.Trace($"BiDi RCV <-- {Encoding.UTF8.GetString(data)}"); } - var res = await JsonSerializer.DeserializeAsync(ms, typeof(T), jsonSerializerContext, cancellationToken).ConfigureAwait(false); - - return (T)res!; + return data; } - public async Task SendAsJsonAsync(TCommand command, JsonSerializerContext jsonSerializerContext, CancellationToken cancellationToken) - where TCommand : Command + public async Task SendAsync(byte[] data, CancellationToken cancellationToken) { - var buffer = JsonSerializer.SerializeToUtf8Bytes(command, typeof(TCommand), jsonSerializerContext); - await _socketSendSemaphoreSlim.WaitAsync(cancellationToken); try { if (_logger.IsEnabled(LogEventLevel.Trace)) { - _logger.Trace($"BiDi SND --> {Encoding.UTF8.GetString(buffer)}"); + _logger.Trace($"BiDi SND --> {Encoding.UTF8.GetString(data)}"); } - await _webSocket.SendAsync(new ArraySegment(buffer), WebSocketMessageType.Text, true, cancellationToken).ConfigureAwait(false); + await _webSocket.SendAsync(new ArraySegment(data), WebSocketMessageType.Text, true, cancellationToken).ConfigureAwait(false); } finally {