mirror of
https://github.com/oliverbooth/TcpDotNet
synced 2024-11-10 03:15:42 +00:00
[ci skip] style: run code cleanup
This commit is contained in:
parent
32fecb5ace
commit
0c5c2087b9
@ -148,7 +148,7 @@ public abstract class ClientNode : Node
|
|||||||
if (constructor is null)
|
if (constructor is null)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
var packet = (Packet) constructor.Invoke(null);
|
var packet = (Packet)constructor.Invoke(null);
|
||||||
packet.Deserialize(bufferReader);
|
packet.Deserialize(bufferReader);
|
||||||
await targetStream.DisposeAsync();
|
await targetStream.DisposeAsync();
|
||||||
|
|
||||||
@ -198,7 +198,7 @@ public abstract class ClientNode : Node
|
|||||||
buffer.Position = 0;
|
buffer.Position = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
var length = (int) buffer.Length;
|
var length = (int)buffer.Length;
|
||||||
networkWriter.Write(length);
|
networkWriter.Write(length);
|
||||||
|
|
||||||
try
|
try
|
||||||
@ -239,7 +239,8 @@ public abstract class ClientNode : Node
|
|||||||
requestPacket.CallbackId = _callbackIdGenerator.GetId(packetToSend, out _);
|
requestPacket.CallbackId = _callbackIdGenerator.GetId(packetToSend, out _);
|
||||||
|
|
||||||
var completionSource = new TaskCompletionSource<Packet>();
|
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>>();
|
completionSources = new List<TaskCompletionSource<Packet>>();
|
||||||
_packetCompletionSources.TryAdd(attribute.Id, completionSources);
|
_packetCompletionSources.TryAdd(attribute.Id, completionSources);
|
||||||
@ -292,7 +293,8 @@ public abstract class ClientNode : Node
|
|||||||
if (attribute is null)
|
if (attribute is null)
|
||||||
throw new ArgumentException($"The packet type {typeof(TPacket).Name} is not a valid packet.");
|
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>>();
|
completionSources = new List<TaskCompletionSource<Packet>>();
|
||||||
_packetCompletionSources.TryAdd(attribute.Id, completionSources);
|
_packetCompletionSources.TryAdd(attribute.Id, completionSources);
|
||||||
@ -326,7 +328,7 @@ public abstract class ClientNode : Node
|
|||||||
completionSource.SetCanceled();
|
completionSource.SetCanceled();
|
||||||
}, cancellationToken);
|
}, 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))
|
if (_packetCompletionSources.TryGetValue(attribute.Id, out completionSources))
|
||||||
{
|
{
|
||||||
lock (completionSources)
|
lock (completionSources)
|
||||||
|
@ -38,7 +38,8 @@ public abstract class Node : IDisposable
|
|||||||
/// <value>The registered packets.</value>
|
/// <value>The registered packets.</value>
|
||||||
public IReadOnlyDictionary<Type, IReadOnlyCollection<PacketHandler>> RegisteredPacketHandlers =>
|
public IReadOnlyDictionary<Type, IReadOnlyCollection<PacketHandler>> RegisteredPacketHandlers =>
|
||||||
new ReadOnlyDictionary<Type, IReadOnlyCollection<PacketHandler>>(
|
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>
|
/// <summary>
|
||||||
/// Closes the base socket connection and releases all associated resources.
|
/// Closes the base socket connection and releases all associated resources.
|
||||||
@ -124,7 +125,8 @@ public abstract class Node : IDisposable
|
|||||||
var attribute = packetType.GetCustomAttribute<PacketAttribute>();
|
var attribute = packetType.GetCustomAttribute<PacketAttribute>();
|
||||||
if (attribute is null) throw new ArgumentException($"{packetType.Name} is not a valid packet.");
|
if (attribute is null) throw new ArgumentException($"{packetType.Name} is not a valid packet.");
|
||||||
if (_registeredPackets.TryGetValue(attribute.Id, out Type? registeredPacket))
|
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);
|
_registeredPackets.TryAdd(attribute.Id, packetType);
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,8 @@ internal sealed class HandshakeRequestPacketHandler : PacketHandler<HandshakeReq
|
|||||||
|
|
||||||
if (packet.ProtocolVersion != Node.ProtocolVersion)
|
if (packet.ProtocolVersion != Node.ProtocolVersion)
|
||||||
{
|
{
|
||||||
response = new HandshakeResponsePacket(packet.ProtocolVersion, HandshakeResponse.UnsupportedProtocolVersion);
|
response = new HandshakeResponsePacket(packet.ProtocolVersion,
|
||||||
|
HandshakeResponse.UnsupportedProtocolVersion);
|
||||||
await client.SendPacketAsync(response, cancellationToken);
|
await client.SendPacketAsync(response, cancellationToken);
|
||||||
client.Close();
|
client.Close();
|
||||||
return;
|
return;
|
||||||
|
@ -24,12 +24,12 @@ internal sealed class DisconnectPacket : Packet
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected internal override void Deserialize(ProtocolReader reader)
|
protected internal override void Deserialize(ProtocolReader reader)
|
||||||
{
|
{
|
||||||
Reason = (DisconnectReason) reader.ReadByte();
|
Reason = (DisconnectReason)reader.ReadByte();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected internal override void Serialize(ProtocolWriter writer)
|
protected internal override void Serialize(ProtocolWriter writer)
|
||||||
{
|
{
|
||||||
writer.Write((byte) Reason);
|
writer.Write((byte)Reason);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,14 +38,14 @@ internal sealed class HandshakeResponsePacket : Packet
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected internal override void Deserialize(ProtocolReader reader)
|
protected internal override void Deserialize(ProtocolReader reader)
|
||||||
{
|
{
|
||||||
HandshakeResponse = (HandshakeResponse) reader.ReadByte();
|
HandshakeResponse = (HandshakeResponse)reader.ReadByte();
|
||||||
ProtocolVersion = reader.ReadInt32();
|
ProtocolVersion = reader.ReadInt32();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected internal override void Serialize(ProtocolWriter writer)
|
protected internal override void Serialize(ProtocolWriter writer)
|
||||||
{
|
{
|
||||||
writer.Write((byte) HandshakeResponse);
|
writer.Write((byte)HandshakeResponse);
|
||||||
writer.Write(ProtocolVersion);
|
writer.Write(ProtocolVersion);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -66,7 +66,7 @@ public sealed class ProtocolReader : BinaryReader
|
|||||||
result |= (byteReadJustNow & 0x7Fu) << shift;
|
result |= (byteReadJustNow & 0x7Fu) << shift;
|
||||||
|
|
||||||
if (byteReadJustNow <= 0x7Fu)
|
if (byteReadJustNow <= 0x7Fu)
|
||||||
return (int) result; // early exit
|
return (int)result; // early exit
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the 5th byte. Since we already read 28 bits,
|
// Read the 5th byte. Since we already read 28 bits,
|
||||||
@ -77,8 +77,8 @@ public sealed class ProtocolReader : BinaryReader
|
|||||||
if (byteReadJustNow > 0b_1111u)
|
if (byteReadJustNow > 0b_1111u)
|
||||||
throw new FormatException();
|
throw new FormatException();
|
||||||
|
|
||||||
result |= (uint) byteReadJustNow << (maxBytesWithoutOverflow * 7);
|
result |= (uint)byteReadJustNow << (maxBytesWithoutOverflow * 7);
|
||||||
return (int) result;
|
return (int)result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -107,7 +107,7 @@ public sealed class ProtocolReader : BinaryReader
|
|||||||
result |= (byteReadJustNow & 0x7Ful) << shift;
|
result |= (byteReadJustNow & 0x7Ful) << shift;
|
||||||
|
|
||||||
if (byteReadJustNow <= 0x7Fu)
|
if (byteReadJustNow <= 0x7Fu)
|
||||||
return (long) result; // early exit
|
return (long)result; // early exit
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the 10th byte. Since we already read 63 bits,
|
// Read the 10th byte. Since we already read 63 bits,
|
||||||
@ -118,8 +118,8 @@ public sealed class ProtocolReader : BinaryReader
|
|||||||
if (byteReadJustNow > 0b_1u)
|
if (byteReadJustNow > 0b_1u)
|
||||||
throw new FormatException();
|
throw new FormatException();
|
||||||
|
|
||||||
result |= (ulong) byteReadJustNow << (maxBytesWithoutOverflow * 7);
|
result |= (ulong)byteReadJustNow << (maxBytesWithoutOverflow * 7);
|
||||||
return (long) result;
|
return (long)result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
@ -196,21 +196,21 @@ public sealed class ProtocolReader : BinaryReader
|
|||||||
[CLSCompliant(false)]
|
[CLSCompliant(false)]
|
||||||
public override ushort ReadUInt16()
|
public override ushort ReadUInt16()
|
||||||
{
|
{
|
||||||
return (ushort) IPAddress.NetworkToHostOrder((short) base.ReadUInt16());
|
return (ushort)IPAddress.NetworkToHostOrder((short)base.ReadUInt16());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
[CLSCompliant(false)]
|
[CLSCompliant(false)]
|
||||||
public override uint ReadUInt32()
|
public override uint ReadUInt32()
|
||||||
{
|
{
|
||||||
return (uint) IPAddress.NetworkToHostOrder((int) base.ReadUInt32());
|
return (uint)IPAddress.NetworkToHostOrder((int)base.ReadUInt32());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
[CLSCompliant(false)]
|
[CLSCompliant(false)]
|
||||||
public override ulong ReadUInt64()
|
public override ulong ReadUInt64()
|
||||||
{
|
{
|
||||||
return (ulong) IPAddress.NetworkToHostOrder((long) base.ReadUInt64());
|
return (ulong)IPAddress.NetworkToHostOrder((long)base.ReadUInt64());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@ -107,21 +107,21 @@ public sealed class ProtocolWriter : BinaryWriter
|
|||||||
[CLSCompliant(false)]
|
[CLSCompliant(false)]
|
||||||
public override void Write(ushort value)
|
public override void Write(ushort value)
|
||||||
{
|
{
|
||||||
base.Write((ushort) IPAddress.HostToNetworkOrder((short) value));
|
base.Write((ushort)IPAddress.HostToNetworkOrder((short)value));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
[CLSCompliant(false)]
|
[CLSCompliant(false)]
|
||||||
public override void Write(uint value)
|
public override void Write(uint value)
|
||||||
{
|
{
|
||||||
base.Write((uint) IPAddress.HostToNetworkOrder((int) value));
|
base.Write((uint)IPAddress.HostToNetworkOrder((int)value));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
[CLSCompliant(false)]
|
[CLSCompliant(false)]
|
||||||
public override void Write(ulong value)
|
public override void Write(ulong value)
|
||||||
{
|
{
|
||||||
base.Write((ulong) IPAddress.HostToNetworkOrder((long) value));
|
base.Write((ulong)IPAddress.HostToNetworkOrder((long)value));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -163,7 +163,7 @@ public sealed class ProtocolWriter : BinaryWriter
|
|||||||
/// <param name="value">The 32-bit integer to be written.</param>
|
/// <param name="value">The 32-bit integer to be written.</param>
|
||||||
public void Write7BitEncodedInt32(int value)
|
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,
|
// Write out an int 7 bits at a time. The high bit of the byte,
|
||||||
// when on, tells reader to continue reading more bytes.
|
// when on, tells reader to continue reading more bytes.
|
||||||
@ -173,11 +173,11 @@ public sealed class ProtocolWriter : BinaryWriter
|
|||||||
|
|
||||||
while (uValue > 0x7Fu)
|
while (uValue > 0x7Fu)
|
||||||
{
|
{
|
||||||
Write((byte) (uValue | ~0x7Fu));
|
Write((byte)(uValue | ~0x7Fu));
|
||||||
uValue >>= 7;
|
uValue >>= 7;
|
||||||
}
|
}
|
||||||
|
|
||||||
Write((byte) uValue);
|
Write((byte)uValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -186,7 +186,7 @@ public sealed class ProtocolWriter : BinaryWriter
|
|||||||
/// <param name="value">The 64-bit integer to be written.</param>
|
/// <param name="value">The 64-bit integer to be written.</param>
|
||||||
public void Write7BitEncodedInt64(long value)
|
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,
|
// Write out an int 7 bits at a time. The high bit of the byte,
|
||||||
// when on, tells reader to continue reading more bytes.
|
// when on, tells reader to continue reading more bytes.
|
||||||
@ -196,10 +196,10 @@ public sealed class ProtocolWriter : BinaryWriter
|
|||||||
|
|
||||||
while (uValue > 0x7Fu)
|
while (uValue > 0x7Fu)
|
||||||
{
|
{
|
||||||
Write((byte) ((uint) uValue | ~0x7Fu));
|
Write((byte)((uint)uValue | ~0x7Fu));
|
||||||
uValue >>= 7;
|
uValue >>= 7;
|
||||||
}
|
}
|
||||||
|
|
||||||
Write((byte) uValue);
|
Write((byte)uValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,8 @@ public sealed partial class ProtocolListener
|
|||||||
foreach (Type packetType in ParentListener.RegisteredPackets.Values)
|
foreach (Type packetType in ParentListener.RegisteredPackets.Values)
|
||||||
RegisterPacket(packetType);
|
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)
|
foreach (PacketHandler handler in handlers)
|
||||||
RegisterPacketHandler(packetType, handler);
|
RegisterPacketHandler(packetType, handler);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user