Skip to content

Commit 57f541a

Browse files
authored
[dotnet][bidi] Remove json serialization from transport layer (#15250)
1 parent e19d069 commit 57f541a

File tree

3 files changed

+21
-24
lines changed

3 files changed

+21
-24
lines changed

dotnet/src/webdriver/BiDi/Communication/Broker.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,17 @@ public async Task ConnectAsync(CancellationToken cancellationToken)
111111
await _transport.ConnectAsync(cancellationToken).ConfigureAwait(false);
112112

113113
_receiveMessagesCancellationTokenSource = new CancellationTokenSource();
114-
_receivingMessageTask = _myTaskFactory.StartNew(async () => await ReceiveMessagesAsync(_receiveMessagesCancellationTokenSource.Token), TaskCreationOptions.LongRunning).Unwrap();
115-
_eventEmitterTask = _myTaskFactory.StartNew(async () => await ProcessEventsAwaiterAsync(), TaskCreationOptions.LongRunning).Unwrap();
114+
_receivingMessageTask = _myTaskFactory.StartNew(async () => await ReceiveMessagesAsync(_receiveMessagesCancellationTokenSource.Token)).Unwrap();
115+
_eventEmitterTask = _myTaskFactory.StartNew(ProcessEventsAwaiterAsync).Unwrap();
116116
}
117117

118118
private async Task ReceiveMessagesAsync(CancellationToken cancellationToken)
119119
{
120120
while (!cancellationToken.IsCancellationRequested)
121121
{
122-
var message = await _transport.ReceiveAsJsonAsync<Message>(_jsonSerializerContext, cancellationToken);
122+
var data = await _transport.ReceiveAsync(cancellationToken).ConfigureAwait(false);
123+
124+
var message = JsonSerializer.Deserialize(new ReadOnlySpan<byte>(data), _jsonSerializerContext.Message);
123125

124126
switch (message)
125127
{
@@ -179,21 +181,21 @@ private async Task ProcessEventsAwaiterAsync()
179181
}
180182

181183
public async Task<TResult> ExecuteCommandAsync<TCommand, TResult>(TCommand command, CommandOptions? options)
182-
where TCommand: Command
184+
where TCommand : Command
183185
{
184186
var jsonElement = await ExecuteCommandCoreAsync(command, options).ConfigureAwait(false);
185187

186188
return (TResult)jsonElement.Deserialize(typeof(TResult), _jsonSerializerContext)!;
187189
}
188190

189191
public async Task ExecuteCommandAsync<TCommand>(TCommand command, CommandOptions? options)
190-
where TCommand: Command
192+
where TCommand : Command
191193
{
192194
await ExecuteCommandCoreAsync(command, options).ConfigureAwait(false);
193195
}
194196

195197
private async Task<JsonElement> ExecuteCommandCoreAsync<TCommand>(TCommand command, CommandOptions? options)
196-
where TCommand: Command
198+
where TCommand : Command
197199
{
198200
command.Id = Interlocked.Increment(ref _currentCommandId);
199201

@@ -207,7 +209,9 @@ private async Task<JsonElement> ExecuteCommandCoreAsync<TCommand>(TCommand comma
207209

208210
_pendingCommands[command.Id] = tcs;
209211

210-
await _transport.SendAsJsonAsync(command, _jsonSerializerContext, cts.Token).ConfigureAwait(false);
212+
var data = JsonSerializer.SerializeToUtf8Bytes(command, typeof(TCommand), _jsonSerializerContext);
213+
214+
await _transport.SendAsync(data, cts.Token).ConfigureAwait(false);
211215

212216
return await tcs.Task.ConfigureAwait(false);
213217
}

dotnet/src/webdriver/BiDi/Communication/Transport/ITransport.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
using System.Threading.Tasks;
2121
using System.Threading;
2222
using System;
23-
using System.Text.Json.Serialization;
2423

2524
#nullable enable
2625

@@ -30,8 +29,7 @@ interface ITransport : IDisposable
3029
{
3130
Task ConnectAsync(CancellationToken cancellationToken);
3231

33-
Task<T> ReceiveAsJsonAsync<T>(JsonSerializerContext jsonSerializerContext, CancellationToken cancellationToken);
32+
Task<byte[]> ReceiveAsync(CancellationToken cancellationToken);
3433

35-
Task SendAsJsonAsync<TCommand>(TCommand command, JsonSerializerContext jsonSerializerContext, CancellationToken cancellationToken)
36-
where TCommand : Command;
34+
Task SendAsync(byte[] data, CancellationToken cancellationToken);
3735
}

dotnet/src/webdriver/BiDi/Communication/Transport/WebSocketTransport.cs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@
2222
using System.Net.WebSockets;
2323
using System.Threading.Tasks;
2424
using System.Threading;
25-
using System.Text.Json;
2625
using System.Text;
2726
using OpenQA.Selenium.Internal.Logging;
28-
using System.Text.Json.Serialization;
2927

3028
#nullable enable
3129

@@ -45,7 +43,7 @@ public async Task ConnectAsync(CancellationToken cancellationToken)
4543
await _webSocket.ConnectAsync(_uri, cancellationToken).ConfigureAwait(false);
4644
}
4745

48-
public async Task<T> ReceiveAsJsonAsync<T>(JsonSerializerContext jsonSerializerContext, CancellationToken cancellationToken)
46+
public async Task<byte[]> ReceiveAsync(CancellationToken cancellationToken)
4947
{
5048
using var ms = new MemoryStream();
5149

@@ -61,31 +59,28 @@ public async Task<T> ReceiveAsJsonAsync<T>(JsonSerializerContext jsonSerializerC
6159

6260
ms.Seek(0, SeekOrigin.Begin);
6361

62+
byte[] data = ms.ToArray();
63+
6464
if (_logger.IsEnabled(LogEventLevel.Trace))
6565
{
66-
_logger.Trace($"BiDi RCV <-- {Encoding.UTF8.GetString(ms.ToArray())}");
66+
_logger.Trace($"BiDi RCV <-- {Encoding.UTF8.GetString(data)}");
6767
}
6868

69-
var res = await JsonSerializer.DeserializeAsync(ms, typeof(T), jsonSerializerContext, cancellationToken).ConfigureAwait(false);
70-
71-
return (T)res!;
69+
return data;
7270
}
7371

74-
public async Task SendAsJsonAsync<TCommand>(TCommand command, JsonSerializerContext jsonSerializerContext, CancellationToken cancellationToken)
75-
where TCommand : Command
72+
public async Task SendAsync(byte[] data, CancellationToken cancellationToken)
7673
{
77-
var buffer = JsonSerializer.SerializeToUtf8Bytes(command, typeof(TCommand), jsonSerializerContext);
78-
7974
await _socketSendSemaphoreSlim.WaitAsync(cancellationToken);
8075

8176
try
8277
{
8378
if (_logger.IsEnabled(LogEventLevel.Trace))
8479
{
85-
_logger.Trace($"BiDi SND --> {Encoding.UTF8.GetString(buffer)}");
80+
_logger.Trace($"BiDi SND --> {Encoding.UTF8.GetString(data)}");
8681
}
8782

88-
await _webSocket.SendAsync(new ArraySegment<byte>(buffer), WebSocketMessageType.Text, true, cancellationToken).ConfigureAwait(false);
83+
await _webSocket.SendAsync(new ArraySegment<byte>(data), WebSocketMessageType.Text, true, cancellationToken).ConfigureAwait(false);
8984
}
9085
finally
9186
{

0 commit comments

Comments
 (0)