@@ -111,15 +111,17 @@ public async Task ConnectAsync(CancellationToken cancellationToken)
111
111
await _transport . ConnectAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
112
112
113
113
_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 ( ) ;
116
116
}
117
117
118
118
private async Task ReceiveMessagesAsync ( CancellationToken cancellationToken )
119
119
{
120
120
while ( ! cancellationToken . IsCancellationRequested )
121
121
{
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 ) ;
123
125
124
126
switch ( message )
125
127
{
@@ -179,21 +181,21 @@ private async Task ProcessEventsAwaiterAsync()
179
181
}
180
182
181
183
public async Task < TResult > ExecuteCommandAsync < TCommand , TResult > ( TCommand command , CommandOptions ? options )
182
- where TCommand : Command
184
+ where TCommand : Command
183
185
{
184
186
var jsonElement = await ExecuteCommandCoreAsync ( command , options ) . ConfigureAwait ( false ) ;
185
187
186
188
return ( TResult ) jsonElement . Deserialize ( typeof ( TResult ) , _jsonSerializerContext ) ! ;
187
189
}
188
190
189
191
public async Task ExecuteCommandAsync < TCommand > ( TCommand command , CommandOptions ? options )
190
- where TCommand : Command
192
+ where TCommand : Command
191
193
{
192
194
await ExecuteCommandCoreAsync ( command , options ) . ConfigureAwait ( false ) ;
193
195
}
194
196
195
197
private async Task < JsonElement > ExecuteCommandCoreAsync < TCommand > ( TCommand command , CommandOptions ? options )
196
- where TCommand : Command
198
+ where TCommand : Command
197
199
{
198
200
command . Id = Interlocked . Increment ( ref _currentCommandId ) ;
199
201
@@ -207,7 +209,9 @@ private async Task<JsonElement> ExecuteCommandCoreAsync<TCommand>(TCommand comma
207
209
208
210
_pendingCommands [ command . Id ] = tcs ;
209
211
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 ) ;
211
215
212
216
return await tcs . Task . ConfigureAwait ( false ) ;
213
217
}
0 commit comments