1
0
mirror of https://github.com/oliverbooth/TcpDotNet synced 2024-11-09 23:25:42 +00:00

[ci skip] style: run code cleanup

This commit is contained in:
Oliver Booth 2024-02-12 17:40:15 +00:00
parent 32fecb5ace
commit 0c5c2087b9
Signed by: oliverbooth
GPG Key ID: E60B570D1B7557B5
8 changed files with 38 additions and 32 deletions

View File

@ -148,7 +148,7 @@ public abstract class ClientNode : Node
if (constructor is null)
return null;
var packet = (Packet) constructor.Invoke(null);
var packet = (Packet)constructor.Invoke(null);
packet.Deserialize(bufferReader);
await targetStream.DisposeAsync();
@ -198,7 +198,7 @@ public abstract class ClientNode : Node
buffer.Position = 0;
}
var length = (int) buffer.Length;
var length = (int)buffer.Length;
networkWriter.Write(length);
try
@ -239,7 +239,8 @@ public abstract class ClientNode : Node
requestPacket.CallbackId = _callbackIdGenerator.GetId(packetToSend, out _);
var completionSource = new TaskCompletionSource<Packet>();
if (!_packetCompletionSources.TryGetValue(attribute.Id, out List<TaskCompletionSource<Packet>>? completionSources))
if (!_packetCompletionSources.TryGetValue(attribute.Id,
out List<TaskCompletionSource<Packet>>? completionSources))
{
completionSources = new List<TaskCompletionSource<Packet>>();
_packetCompletionSources.TryAdd(attribute.Id, completionSources);
@ -292,7 +293,8 @@ public abstract class ClientNode : Node
if (attribute is null)
throw new ArgumentException($"The packet type {typeof(TPacket).Name} is not a valid packet.");
if (!_packetCompletionSources.TryGetValue(attribute.Id, out List<TaskCompletionSource<Packet>>? completionSources))
if (!_packetCompletionSources.TryGetValue(attribute.Id,
out List<TaskCompletionSource<Packet>>? completionSources))
{
completionSources = new List<TaskCompletionSource<Packet>>();
_packetCompletionSources.TryAdd(attribute.Id, completionSources);
@ -326,7 +328,7 @@ public abstract class ClientNode : Node
completionSource.SetCanceled();
}, cancellationToken);
var packet = (TPacket) await Task.Run(() => completionSource.Task, cancellationToken);
var packet = (TPacket)await Task.Run(() => completionSource.Task, cancellationToken);
if (_packetCompletionSources.TryGetValue(attribute.Id, out completionSources))
{
lock (completionSources)

View File

@ -38,7 +38,8 @@ public abstract class Node : IDisposable
/// <value>The registered packets.</value>
public IReadOnlyDictionary<Type, IReadOnlyCollection<PacketHandler>> RegisteredPacketHandlers =>
new ReadOnlyDictionary<Type, IReadOnlyCollection<PacketHandler>>(
_registeredPacketHandlers.ToDictionary(p => p.Key, p => (IReadOnlyCollection<PacketHandler>) p.Value.AsReadOnly()));
_registeredPacketHandlers.ToDictionary(p => p.Key,
p => (IReadOnlyCollection<PacketHandler>)p.Value.AsReadOnly()));
/// <summary>
/// Closes the base socket connection and releases all associated resources.
@ -124,7 +125,8 @@ public abstract class Node : IDisposable
var attribute = packetType.GetCustomAttribute<PacketAttribute>();
if (attribute is null) throw new ArgumentException($"{packetType.Name} is not a valid packet.");
if (_registeredPackets.TryGetValue(attribute.Id, out Type? registeredPacket))
throw new ArgumentException($"The packet type {attribute.Id:X8} is already registered to {registeredPacket.Name}.");
throw new ArgumentException(
$"The packet type {attribute.Id:X8} is already registered to {registeredPacket.Name}.");
_registeredPackets.TryAdd(attribute.Id, packetType);
}

View File

@ -22,7 +22,8 @@ internal sealed class HandshakeRequestPacketHandler : PacketHandler<HandshakeReq
if (packet.ProtocolVersion != Node.ProtocolVersion)
{
response = new HandshakeResponsePacket(packet.ProtocolVersion, HandshakeResponse.UnsupportedProtocolVersion);
response = new HandshakeResponsePacket(packet.ProtocolVersion,
HandshakeResponse.UnsupportedProtocolVersion);
await client.SendPacketAsync(response, cancellationToken);
client.Close();
return;

View File

@ -24,12 +24,12 @@ internal sealed class DisconnectPacket : Packet
/// <inheritdoc />
protected internal override void Deserialize(ProtocolReader reader)
{
Reason = (DisconnectReason) reader.ReadByte();
Reason = (DisconnectReason)reader.ReadByte();
}
/// <inheritdoc />
protected internal override void Serialize(ProtocolWriter writer)
{
writer.Write((byte) Reason);
writer.Write((byte)Reason);
}
}

View File

@ -38,14 +38,14 @@ internal sealed class HandshakeResponsePacket : Packet
/// <inheritdoc />
protected internal override void Deserialize(ProtocolReader reader)
{
HandshakeResponse = (HandshakeResponse) reader.ReadByte();
HandshakeResponse = (HandshakeResponse)reader.ReadByte();
ProtocolVersion = reader.ReadInt32();
}
/// <inheritdoc />
protected internal override void Serialize(ProtocolWriter writer)
{
writer.Write((byte) HandshakeResponse);
writer.Write((byte)HandshakeResponse);
writer.Write(ProtocolVersion);
}
}

View File

@ -66,7 +66,7 @@ public sealed class ProtocolReader : BinaryReader
result |= (byteReadJustNow & 0x7Fu) << shift;
if (byteReadJustNow <= 0x7Fu)
return (int) result; // early exit
return (int)result; // early exit
}
// Read the 5th byte. Since we already read 28 bits,
@ -77,8 +77,8 @@ public sealed class ProtocolReader : BinaryReader
if (byteReadJustNow > 0b_1111u)
throw new FormatException();
result |= (uint) byteReadJustNow << (maxBytesWithoutOverflow * 7);
return (int) result;
result |= (uint)byteReadJustNow << (maxBytesWithoutOverflow * 7);
return (int)result;
}
/// <summary>
@ -107,7 +107,7 @@ public sealed class ProtocolReader : BinaryReader
result |= (byteReadJustNow & 0x7Ful) << shift;
if (byteReadJustNow <= 0x7Fu)
return (long) result; // early exit
return (long)result; // early exit
}
// Read the 10th byte. Since we already read 63 bits,
@ -118,8 +118,8 @@ public sealed class ProtocolReader : BinaryReader
if (byteReadJustNow > 0b_1u)
throw new FormatException();
result |= (ulong) byteReadJustNow << (maxBytesWithoutOverflow * 7);
return (long) result;
result |= (ulong)byteReadJustNow << (maxBytesWithoutOverflow * 7);
return (long)result;
}
/// <inheritdoc />
@ -196,21 +196,21 @@ public sealed class ProtocolReader : BinaryReader
[CLSCompliant(false)]
public override ushort ReadUInt16()
{
return (ushort) IPAddress.NetworkToHostOrder((short) base.ReadUInt16());
return (ushort)IPAddress.NetworkToHostOrder((short)base.ReadUInt16());
}
/// <inheritdoc />
[CLSCompliant(false)]
public override uint ReadUInt32()
{
return (uint) IPAddress.NetworkToHostOrder((int) base.ReadUInt32());
return (uint)IPAddress.NetworkToHostOrder((int)base.ReadUInt32());
}
/// <inheritdoc />
[CLSCompliant(false)]
public override ulong ReadUInt64()
{
return (ulong) IPAddress.NetworkToHostOrder((long) base.ReadUInt64());
return (ulong)IPAddress.NetworkToHostOrder((long)base.ReadUInt64());
}
/// <summary>

View File

@ -1,4 +1,4 @@
using System.Net;
using System.Net;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
@ -107,21 +107,21 @@ public sealed class ProtocolWriter : BinaryWriter
[CLSCompliant(false)]
public override void Write(ushort value)
{
base.Write((ushort) IPAddress.HostToNetworkOrder((short) value));
base.Write((ushort)IPAddress.HostToNetworkOrder((short)value));
}
/// <inheritdoc />
[CLSCompliant(false)]
public override void Write(uint value)
{
base.Write((uint) IPAddress.HostToNetworkOrder((int) value));
base.Write((uint)IPAddress.HostToNetworkOrder((int)value));
}
/// <inheritdoc />
[CLSCompliant(false)]
public override void Write(ulong value)
{
base.Write((ulong) IPAddress.HostToNetworkOrder((long) value));
base.Write((ulong)IPAddress.HostToNetworkOrder((long)value));
}
/// <summary>
@ -163,7 +163,7 @@ public sealed class ProtocolWriter : BinaryWriter
/// <param name="value">The 32-bit integer to be written.</param>
public void Write7BitEncodedInt32(int value)
{
var uValue = (uint) value;
var uValue = (uint)value;
// Write out an int 7 bits at a time. The high bit of the byte,
// when on, tells reader to continue reading more bytes.
@ -173,11 +173,11 @@ public sealed class ProtocolWriter : BinaryWriter
while (uValue > 0x7Fu)
{
Write((byte) (uValue | ~0x7Fu));
Write((byte)(uValue | ~0x7Fu));
uValue >>= 7;
}
Write((byte) uValue);
Write((byte)uValue);
}
/// <summary>
@ -186,7 +186,7 @@ public sealed class ProtocolWriter : BinaryWriter
/// <param name="value">The 64-bit integer to be written.</param>
public void Write7BitEncodedInt64(long value)
{
var uValue = (ulong) value;
var uValue = (ulong)value;
// Write out an int 7 bits at a time. The high bit of the byte,
// when on, tells reader to continue reading more bytes.
@ -196,10 +196,10 @@ public sealed class ProtocolWriter : BinaryWriter
while (uValue > 0x7Fu)
{
Write((byte) ((uint) uValue | ~0x7Fu));
Write((byte)((uint)uValue | ~0x7Fu));
uValue >>= 7;
}
Write((byte) uValue);
Write((byte)uValue);
}
}

View File

@ -35,7 +35,8 @@ public sealed partial class ProtocolListener
foreach (Type packetType in ParentListener.RegisteredPackets.Values)
RegisterPacket(packetType);
foreach ((Type packetType, IReadOnlyCollection<PacketHandler>? handlers) in ParentListener.RegisteredPacketHandlers)
foreach ((Type packetType, IReadOnlyCollection<PacketHandler>? handlers) in ParentListener
.RegisteredPacketHandlers)
foreach (PacketHandler handler in handlers)
RegisterPacketHandler(packetType, handler);