From 0c5c2087b93c6dedc70dc4ba1879be57962d12f1 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Mon, 12 Feb 2024 17:40:15 +0000 Subject: [PATCH] [ci skip] style: run code cleanup --- TcpDotNet/ClientNode.cs | 12 ++++++----- TcpDotNet/Node.cs | 6 ++++-- .../HandshakeRequestPacketHandler.cs | 3 ++- .../Packets/ClientBound/DisconnectPacket.cs | 4 ++-- .../ClientBound/HandshakeResponsePacket.cs | 4 ++-- TcpDotNet/Protocol/ProtocolReader.cs | 18 ++++++++--------- TcpDotNet/Protocol/ProtocolWriter.cs | 20 +++++++++---------- TcpDotNet/ProtocolListener.Client.cs | 3 ++- 8 files changed, 38 insertions(+), 32 deletions(-) diff --git a/TcpDotNet/ClientNode.cs b/TcpDotNet/ClientNode.cs index 546dcb3..13b1738 100644 --- a/TcpDotNet/ClientNode.cs +++ b/TcpDotNet/ClientNode.cs @@ -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(); - if (!_packetCompletionSources.TryGetValue(attribute.Id, out List>? completionSources)) + if (!_packetCompletionSources.TryGetValue(attribute.Id, + out List>? completionSources)) { completionSources = new List>(); _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>? completionSources)) + if (!_packetCompletionSources.TryGetValue(attribute.Id, + out List>? completionSources)) { completionSources = new List>(); _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) diff --git a/TcpDotNet/Node.cs b/TcpDotNet/Node.cs index bdcf759..ed50de5 100644 --- a/TcpDotNet/Node.cs +++ b/TcpDotNet/Node.cs @@ -38,7 +38,8 @@ public abstract class Node : IDisposable /// The registered packets. public IReadOnlyDictionary> RegisteredPacketHandlers => new ReadOnlyDictionary>( - _registeredPacketHandlers.ToDictionary(p => p.Key, p => (IReadOnlyCollection) p.Value.AsReadOnly())); + _registeredPacketHandlers.ToDictionary(p => p.Key, + p => (IReadOnlyCollection)p.Value.AsReadOnly())); /// /// Closes the base socket connection and releases all associated resources. @@ -124,7 +125,8 @@ public abstract class Node : IDisposable var attribute = packetType.GetCustomAttribute(); 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); } diff --git a/TcpDotNet/Protocol/PacketHandlers/HandshakeRequestPacketHandler.cs b/TcpDotNet/Protocol/PacketHandlers/HandshakeRequestPacketHandler.cs index 9ec2a75..d37209d 100644 --- a/TcpDotNet/Protocol/PacketHandlers/HandshakeRequestPacketHandler.cs +++ b/TcpDotNet/Protocol/PacketHandlers/HandshakeRequestPacketHandler.cs @@ -22,7 +22,8 @@ internal sealed class HandshakeRequestPacketHandler : PacketHandler protected internal override void Deserialize(ProtocolReader reader) { - Reason = (DisconnectReason) reader.ReadByte(); + Reason = (DisconnectReason)reader.ReadByte(); } /// protected internal override void Serialize(ProtocolWriter writer) { - writer.Write((byte) Reason); + writer.Write((byte)Reason); } } diff --git a/TcpDotNet/Protocol/Packets/ClientBound/HandshakeResponsePacket.cs b/TcpDotNet/Protocol/Packets/ClientBound/HandshakeResponsePacket.cs index 0cc020e..8fcf1a8 100644 --- a/TcpDotNet/Protocol/Packets/ClientBound/HandshakeResponsePacket.cs +++ b/TcpDotNet/Protocol/Packets/ClientBound/HandshakeResponsePacket.cs @@ -38,14 +38,14 @@ internal sealed class HandshakeResponsePacket : Packet /// protected internal override void Deserialize(ProtocolReader reader) { - HandshakeResponse = (HandshakeResponse) reader.ReadByte(); + HandshakeResponse = (HandshakeResponse)reader.ReadByte(); ProtocolVersion = reader.ReadInt32(); } /// protected internal override void Serialize(ProtocolWriter writer) { - writer.Write((byte) HandshakeResponse); + writer.Write((byte)HandshakeResponse); writer.Write(ProtocolVersion); } } diff --git a/TcpDotNet/Protocol/ProtocolReader.cs b/TcpDotNet/Protocol/ProtocolReader.cs index 810613d..36df3bb 100644 --- a/TcpDotNet/Protocol/ProtocolReader.cs +++ b/TcpDotNet/Protocol/ProtocolReader.cs @@ -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; } /// @@ -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; } /// @@ -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()); } /// [CLSCompliant(false)] public override uint ReadUInt32() { - return (uint) IPAddress.NetworkToHostOrder((int) base.ReadUInt32()); + return (uint)IPAddress.NetworkToHostOrder((int)base.ReadUInt32()); } /// [CLSCompliant(false)] public override ulong ReadUInt64() { - return (ulong) IPAddress.NetworkToHostOrder((long) base.ReadUInt64()); + return (ulong)IPAddress.NetworkToHostOrder((long)base.ReadUInt64()); } /// diff --git a/TcpDotNet/Protocol/ProtocolWriter.cs b/TcpDotNet/Protocol/ProtocolWriter.cs index 138bee6..f667a30 100644 --- a/TcpDotNet/Protocol/ProtocolWriter.cs +++ b/TcpDotNet/Protocol/ProtocolWriter.cs @@ -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)); } /// [CLSCompliant(false)] public override void Write(uint value) { - base.Write((uint) IPAddress.HostToNetworkOrder((int) value)); + base.Write((uint)IPAddress.HostToNetworkOrder((int)value)); } /// [CLSCompliant(false)] public override void Write(ulong value) { - base.Write((ulong) IPAddress.HostToNetworkOrder((long) value)); + base.Write((ulong)IPAddress.HostToNetworkOrder((long)value)); } /// @@ -163,7 +163,7 @@ public sealed class ProtocolWriter : BinaryWriter /// The 32-bit integer to be written. 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); } /// @@ -186,7 +186,7 @@ public sealed class ProtocolWriter : BinaryWriter /// The 64-bit integer to be written. 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); } } diff --git a/TcpDotNet/ProtocolListener.Client.cs b/TcpDotNet/ProtocolListener.Client.cs index 3c834c4..2364d1b 100644 --- a/TcpDotNet/ProtocolListener.Client.cs +++ b/TcpDotNet/ProtocolListener.Client.cs @@ -35,7 +35,8 @@ public sealed partial class ProtocolListener foreach (Type packetType in ParentListener.RegisteredPackets.Values) RegisterPacket(packetType); - foreach ((Type packetType, IReadOnlyCollection? handlers) in ParentListener.RegisteredPacketHandlers) + foreach ((Type packetType, IReadOnlyCollection? handlers) in ParentListener + .RegisteredPacketHandlers) foreach (PacketHandler handler in handlers) RegisterPacketHandler(packetType, handler);