Skip to content

Commit 4ecab12

Browse files
committed
Merge remote-tracking branch 'upstream/trunk' into dotnet-bidi-events
2 parents 9fd1bd3 + 57f541a commit 4ecab12

File tree

3 files changed

+18
-21
lines changed

3 files changed

+18
-21
lines changed

Diff for: dotnet/src/webdriver/BiDi/Communication/Broker.cs

+8-4
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,17 @@ public async Task ConnectAsync(CancellationToken cancellationToken)
112112
await _transport.ConnectAsync(cancellationToken).ConfigureAwait(false);
113113

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

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

125127
switch (message)
126128
{
@@ -208,7 +210,9 @@ private async Task<JsonElement> ExecuteCommandCoreAsync<TCommand>(TCommand comma
208210

209211
_pendingCommands[command.Id] = tcs;
210212

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

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

Diff for: dotnet/src/webdriver/BiDi/Communication/Transport/ITransport.cs

+2-4
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
}

Diff for: dotnet/src/webdriver/BiDi/Communication/Transport/WebSocketTransport.cs

+8-13
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)