Wrap reading in Task.Factory.StartNew

This commit is contained in:
Oliver Booth 2023-01-23 10:27:50 +00:00
parent 534ba147c5
commit f377cd7de1
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
1 changed files with 12 additions and 3 deletions

View File

@ -1,4 +1,4 @@
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Reflection; using System.Reflection;
@ -96,7 +96,7 @@ public abstract class BaseClientNode : Node
int length; int length;
try try
{ {
length = await Task.Run(networkReader.ReadInt32, cancellationToken); length = await Task.Factory.StartNew(networkReader.ReadInt32, cancellationToken);
} }
catch (EndOfStreamException) catch (EndOfStreamException)
{ {
@ -120,7 +120,16 @@ public abstract class BaseClientNode : Node
} }
using var bufferReader = new ProtocolReader(targetStream); using var bufferReader = new ProtocolReader(targetStream);
int packetHeader = await Task.Run(() => bufferReader.ReadInt32(), cancellationToken); int packetHeader;
try
{
packetHeader = await Task.Factory.StartNew(bufferReader.ReadInt32, cancellationToken);
}
catch (EndOfStreamException)
{
State = ClientState.Disconnected;
throw;
}
if (!RegisteredPackets.TryGetValue(packetHeader, out Type? packetType)) if (!RegisteredPackets.TryGetValue(packetHeader, out Type? packetType))
{ {