From f4dc0cd403f2ee3baa9b1b82db875e7af1112976 Mon Sep 17 00:00:00 2001 From: js6pak Date: Wed, 24 Mar 2021 19:53:01 +0100 Subject: [PATCH] Revert hazel changes for easier merging with its upstream --- src/Impostor.Hazel/Connection.cs | 56 +++++++-------- src/Impostor.Hazel/ConnectionListener.cs | 28 ++++---- src/Impostor.Hazel/ConnectionState.cs | 4 +- src/Impostor.Hazel/ConnectionStatistics.cs | 67 +++++++++--------- src/Impostor.Hazel/DataReceivedEventArgs.cs | 4 +- src/Impostor.Hazel/DisconnectedEventArgs.cs | 6 +- src/Impostor.Hazel/HazelException.cs | 6 +- src/Impostor.Hazel/IPMode.cs | 8 +-- src/Impostor.Hazel/IRecyclable.cs | 2 +- src/Impostor.Hazel/Impostor.Hazel.csproj | 24 +++---- src/Impostor.Hazel/MessageReader.cs | 20 +++--- src/Impostor.Hazel/MessageWriter.cs | 46 +++++++------ src/Impostor.Hazel/NetworkConnection.cs | 16 ++--- .../NetworkConnectionListener.cs | 4 +- src/Impostor.Hazel/NewConnectionEventArgs.cs | 6 +- src/Impostor.Hazel/ObjectPoolCustom.cs | 8 +-- src/Impostor.Hazel/Udp/SendOptionInternal.cs | 2 +- .../Udp/UdpBroadcastListener.cs | 26 +++---- src/Impostor.Hazel/Udp/UdpBroadcaster.cs | 16 ++--- src/Impostor.Hazel/Udp/UdpClientConnection.cs | 15 ++-- .../Udp/UdpConnection.KeepAlive.cs | 8 +-- .../Udp/UdpConnection.Reliable.cs | 68 +++++++++---------- src/Impostor.Hazel/Udp/UdpConnection.cs | 30 ++++---- .../Udp/UdpConnectionListener.cs | 12 ++-- .../Udp/UdpConnectionRateLimit.cs | 2 +- src/Impostor.Hazel/Udp/UdpServerConnection.cs | 6 +- 26 files changed, 239 insertions(+), 251 deletions(-) diff --git a/src/Impostor.Hazel/Connection.cs b/src/Impostor.Hazel/Connection.cs index 23d7f4b..dec8cfe 100644 --- a/src/Impostor.Hazel/Connection.cs +++ b/src/Impostor.Hazel/Connection.cs @@ -11,7 +11,7 @@ namespace Impostor.Hazel /// /// /// - /// Connection is the base class for all connections that Hazel can make. It provides common functionality and a + /// Connection is the base class for all connections that Hazel can make. It provides common functionality and a /// standard interface to allow connections to be swapped easily. /// /// @@ -29,7 +29,7 @@ namespace Impostor.Hazel /// /// /// - /// + /// public abstract class Connection : IDisposable { private static readonly ILogger Logger = Log.ForContext(); @@ -39,34 +39,34 @@ namespace Impostor.Hazel /// /// /// - /// DataReceived is invoked everytime a message is received from the end point of this connection, the message - /// that was received can be found in the alongside other information from the + /// DataReceived is invoked everytime a message is received from the end point of this connection, the message + /// that was received can be found in the alongside other information from the /// event. /// /// /// /// - /// + /// /// public Func DataReceived; public int TestLagMs = -1; public int TestDropRate = 0; protected int testDropCount = 0; - + /// /// Called when the end point disconnects or an error occurs. /// /// /// - /// Disconnected is invoked when the connection is closed due to an exception occuring or because the remote - /// end point disconnected. If it was invoked due to an exception occuring then the exception is available - /// in the passed with the event. + /// Disconnected is invoked when the connection is closed due to an exception occuring or because the remote + /// end point disconnected. If it was invoked due to an exception occuring then the exception is available + /// in the passed with the event. /// /// /// /// - /// + /// /// public Func Disconnected; @@ -74,8 +74,8 @@ namespace Impostor.Hazel /// The remote end point of this Connection. /// /// - /// This is the end point that this connection is connected to (i.e. the other device). This returns an abstract - /// which can then be cast to an appropriate end point depending on the + /// This is the end point that this connection is connected to (i.e. the other device). This returns an abstract + /// which can then be cast to an appropriate end point depending on the /// connection type. /// public IPEndPoint EndPoint { get; protected set; } @@ -95,7 +95,7 @@ namespace Impostor.Hazel /// /// /// All implementers should be aware that when this is set to ConnectionState.Connected it will - /// release all threads that are blocked on . + /// release all threads that are blocked on . /// public ConnectionState State { @@ -103,7 +103,7 @@ namespace Impostor.Hazel { return this._state; } - + protected set { this._state = value; @@ -113,13 +113,13 @@ namespace Impostor.Hazel protected ConnectionState _state; protected virtual void SetState(ConnectionState state) { } - + /// /// Constructor that initializes the ConnecitonStatistics object. /// /// - /// This constructor initialises with empty statistics and sets to - /// . + /// This constructor initialises with empty statistics and sets to + /// . /// protected Connection() { @@ -128,21 +128,21 @@ namespace Impostor.Hazel } /// - /// Sends a number of bytes to the end point of the connection using the specified . + /// Sends a number of bytes to the end point of the connection using the specified . /// /// The message to send. /// /// /// /// The messageType parameter is only a request to use those options and the actual method used to send the - /// data is up to the implementation. There are circumstances where this parameter may be ignored but in + /// data is up to the implementation. There are circumstances where this parameter may be ignored but in /// general any implementer should aim to always follow the user's request. /// /// public abstract ValueTask SendAsync(IMessageWriter msg); /// - /// Sends a number of bytes to the end point of the connection using the specified . + /// Sends a number of bytes to the end point of the connection using the specified . /// /// The bytes of the message to send. /// The option specifying how the message should be sent. @@ -150,7 +150,7 @@ namespace Impostor.Hazel /// /// /// The messageType parameter is only a request to use those options and the actual method used to send the - /// data is up to the implementation. There are circumstances where this parameter may be ignored but in + /// data is up to the implementation. There are circumstances where this parameter may be ignored but in /// general any implementer should aim to always follow the user's request. /// /// @@ -167,9 +167,9 @@ namespace Impostor.Hazel /// Invokes the DataReceived event. /// /// The bytes received. - /// The the message was received with. + /// The the message was received with. /// - /// Invokes the event on this connection to alert subscribers a new message has been + /// Invokes the event on this connection to alert subscribers a new message has been /// received. The bytes and the send option that the message was sent with should be passed in to give to the /// subscribers. /// @@ -197,8 +197,8 @@ namespace Impostor.Hazel /// The exception, if any, that occurred to cause this. /// Extra disconnect data /// - /// Invokes the event to alert subscribres this connection has been disconnected either - /// by the end point or because an error occurred. If an error occurred the error should be passed in in order to + /// Invokes the event to alert subscribres this connection has been disconnected either + /// by the end point or because an error occurred. If an error occurred the error should be passed in in order to /// pass to the subscribers, otherwise null can be passed in. /// protected async ValueTask InvokeDisconnected(string e, IMessageReader reader) @@ -219,11 +219,11 @@ namespace Impostor.Hazel } /// - /// For times when you want to force the disconnect handler to fire as well as close it. - /// If you only want to close it, just use Dispose. + /// For times when you want to force the disconnect handler to fire as well as close it. + /// If you only want to close it, just use Dispose. /// public abstract ValueTask Disconnect(string reason, MessageWriter writer = null); - + /// /// Disposes of this NetworkConnection. /// diff --git a/src/Impostor.Hazel/ConnectionListener.cs b/src/Impostor.Hazel/ConnectionListener.cs index 6c2f942..116f657 100644 --- a/src/Impostor.Hazel/ConnectionListener.cs +++ b/src/Impostor.Hazel/ConnectionListener.cs @@ -10,16 +10,16 @@ namespace Impostor.Hazel /// /// /// - /// ConnectionListeners are server side objects that listen for clients and create matching server side connections + /// ConnectionListeners are server side objects that listen for clients and create matching server side connections /// for each client in a similar way to TCP does. These connections should be ready for communication immediately. /// /// - /// Each time a client connects the event will be invoked to alert all subscribers to - /// the new connection. A disconnected event is then present on the that is passed to the + /// Each time a client connects the event will be invoked to alert all subscribers to + /// the new connection. A disconnected event is then present on the that is passed to the /// subscribers. /// /// - /// + /// public abstract class ConnectionListener : IAsyncDisposable { private static readonly ILogger Logger = Log.ForContext(); @@ -29,18 +29,18 @@ namespace Impostor.Hazel /// /// /// - /// NewConnection is invoked each time a client connects to the listener. The - /// contains the new for communication with this + /// NewConnection is invoked each time a client connects to the listener. The + /// contains the new for communication with this /// client. /// /// - /// Hazel may or may not store connections so it is your responsibility to keep track and properly Dispose of - /// connections to your server. + /// Hazel may or may not store connections so it is your responsibility to keep track and properly Dispose of + /// connections to your server. /// /// /// /// - /// + /// /// public Func NewConnection; @@ -49,15 +49,15 @@ namespace Impostor.Hazel /// /// /// - /// This instructs the listener to begin listening for new clients connecting to the server. When a new client - /// connects the event will be invoked containing the connection to the new client. + /// This instructs the listener to begin listening for new clients connecting to the server. When a new client + /// connects the event will be invoked containing the connection to the new client. /// /// - /// To stop listening you should call . + /// To stop listening you should call . /// /// /// - /// + /// /// public abstract Task StartAsync(); @@ -67,7 +67,7 @@ namespace Impostor.Hazel /// The user sent bytes that were received as part of the handshake. /// The connection to pass in the arguments. /// - /// Implementers should call this to invoke the event before data is received so that + /// Implementers should call this to invoke the event before data is received so that /// subscribers do not miss any data that may have been sent immediately after connecting. /// internal async Task InvokeNewConnection(IMessageReader msg, Connection connection) diff --git a/src/Impostor.Hazel/ConnectionState.cs b/src/Impostor.Hazel/ConnectionState.cs index c9fd4de..5dd7c6a 100644 --- a/src/Impostor.Hazel/ConnectionState.cs +++ b/src/Impostor.Hazel/ConnectionState.cs @@ -1,7 +1,7 @@ namespace Impostor.Hazel { /// - /// Represents the state a is currently in. + /// Represents the state a is currently in. /// public enum ConnectionState { @@ -9,7 +9,7 @@ /// The Connection has either not been established yet or has been disconnected. /// NotConnected, - + /// /// The Connection is currently connecting to an endpoint. /// diff --git a/src/Impostor.Hazel/ConnectionStatistics.cs b/src/Impostor.Hazel/ConnectionStatistics.cs index cd7567c..4802620 100644 --- a/src/Impostor.Hazel/ConnectionStatistics.cs +++ b/src/Impostor.Hazel/ConnectionStatistics.cs @@ -2,13 +2,12 @@ using System.Threading; [assembly: InternalsVisibleTo("Hazel.Tests")] - namespace Impostor.Hazel { /// - /// Holds statistics about the traffic through a . + /// Holds statistics about the traffic through a . /// - /// + /// public class ConnectionStatistics { private const int ExpectedMTU = 1200; @@ -28,8 +27,8 @@ namespace Impostor.Hazel /// The number of messages sent larger than 576 bytes. This is smaller than most default MTUs. /// /// - /// This is the number of unreliable messages that were sent from the , incremented - /// each time that LogUnreliableSend is called by the Connection. Messages that caused an error are not + /// This is the number of unreliable messages that were sent from the , incremented + /// each time that LogUnreliableSend is called by the Connection. Messages that caused an error are not /// counted and messages are only counted once all other operations in the send are complete. /// public int FragmentableMessagesSent @@ -49,8 +48,8 @@ namespace Impostor.Hazel /// The number of unreliable messages sent. /// /// - /// This is the number of unreliable messages that were sent from the , incremented - /// each time that LogUnreliableSend is called by the Connection. Messages that caused an error are not + /// This is the number of unreliable messages that were sent from the , incremented + /// each time that LogUnreliableSend is called by the Connection. Messages that caused an error are not /// counted and messages are only counted once all other operations in the send are complete. /// public int UnreliableMessagesSent @@ -70,8 +69,8 @@ namespace Impostor.Hazel /// The number of reliable messages sent. /// /// - /// This is the number of reliable messages that were sent from the , incremented - /// each time that LogReliableSend is called by the Connection. Messages that caused an error are not + /// This is the number of reliable messages that were sent from the , incremented + /// each time that LogReliableSend is called by the Connection. Messages that caused an error are not /// counted and messages are only counted once all other operations in the send are complete. /// public int ReliableMessagesSent @@ -91,8 +90,8 @@ namespace Impostor.Hazel /// The number of fragmented messages sent. /// /// - /// This is the number of fragmented messages that were sent from the , incremented - /// each time that LogFragmentedSend is called by the Connection. Messages that caused an error are not + /// This is the number of fragmented messages that were sent from the , incremented + /// each time that LogFragmentedSend is called by the Connection. Messages that caused an error are not /// counted and messages are only counted once all other operations in the send are complete. /// public int FragmentedMessagesSent @@ -112,8 +111,8 @@ namespace Impostor.Hazel /// The number of acknowledgement messages sent. /// /// - /// This is the number of acknowledgements that were sent from the , incremented - /// each time that LogAcknowledgementSend is called by the Connection. Messages that caused an error are not + /// This is the number of acknowledgements that were sent from the , incremented + /// each time that LogAcknowledgementSend is called by the Connection. Messages that caused an error are not /// counted and messages are only counted once all other operations in the send are complete. /// public int AcknowledgementMessagesSent @@ -133,8 +132,8 @@ namespace Impostor.Hazel /// The number of hello messages sent. /// /// - /// This is the number of hello messages that were sent from the , incremented - /// each time that LogHelloSend is called by the Connection. Messages that caused an error are not + /// This is the number of hello messages that were sent from the , incremented + /// each time that LogHelloSend is called by the Connection. Messages that caused an error are not /// counted and messages are only counted once all other operations in the send are complete. /// public int HelloMessagesSent @@ -155,12 +154,12 @@ namespace Impostor.Hazel /// /// /// - /// This is the number of bytes of data (i.e. user bytes) that were sent from the , - /// accumulated each time that LogSend is called by the Connection. Messages that caused an error are not + /// This is the number of bytes of data (i.e. user bytes) that were sent from the , + /// accumulated each time that LogSend is called by the Connection. Messages that caused an error are not /// counted and messages are only counted once all other operations in the send are complete. /// /// - /// For the number of bytes including protocol bytes see . + /// For the number of bytes including protocol bytes see . /// /// public long DataBytesSent @@ -181,13 +180,13 @@ namespace Impostor.Hazel /// /// /// - /// This is the total number of bytes (the data bytes plus protocol bytes) that were sent from the - /// , accumulated each time that LogSend is called by the Connection. Messages that - /// caused an error are not counted and messages are only counted once all other operations in the send are + /// This is the total number of bytes (the data bytes plus protocol bytes) that were sent from the + /// , accumulated each time that LogSend is called by the Connection. Messages that + /// caused an error are not counted and messages are only counted once all other operations in the send are /// complete. /// /// - /// For the number of data bytes excluding protocol bytes see . + /// For the number of data bytes excluding protocol bytes see . /// /// public long TotalBytesSent @@ -213,12 +212,12 @@ namespace Impostor.Hazel return UnreliableMessagesReceived + ReliableMessagesReceived + FragmentedMessagesReceived + AcknowledgementMessagesReceived + helloMessagesReceived; } } - + /// /// The number of unreliable messages received. /// /// - /// This is the number of unreliable messages that were received by the , incremented + /// This is the number of unreliable messages that were received by the , incremented /// each time that LogUnreliableReceive is called by the Connection. Messages are counted before the receive event is invoked. /// public int UnreliableMessagesReceived @@ -238,7 +237,7 @@ namespace Impostor.Hazel /// The number of reliable messages received. /// /// - /// This is the number of reliable messages that were received by the , incremented + /// This is the number of reliable messages that were received by the , incremented /// each time that LogReliableReceive is called by the Connection. Messages are counted before the receive event is invoked. /// public int ReliableMessagesReceived @@ -258,7 +257,7 @@ namespace Impostor.Hazel /// The number of fragmented messages received. /// /// - /// This is the number of fragmented messages that were received by the , incremented + /// This is the number of fragmented messages that were received by the , incremented /// each time that LogFragmentedReceive is called by the Connection. Messages are counted before the receive event is invoked. /// public int FragmentedMessagesReceived @@ -278,7 +277,7 @@ namespace Impostor.Hazel /// The number of acknowledgement messages received. /// /// - /// This is the number of acknowledgement messages that were received by the , incremented + /// This is the number of acknowledgement messages that were received by the , incremented /// each time that LogAcknowledgemntReceive is called by the Connection. Messages are counted before the receive event is invoked. /// public int AcknowledgementMessagesReceived @@ -298,7 +297,7 @@ namespace Impostor.Hazel /// The number of ping messages received. /// /// - /// This is the number of hello messages that were received by the , incremented + /// This is the number of hello messages that were received by the , incremented /// each time that LogHelloReceive is called by the Connection. Messages are counted before the receive event is invoked. /// public int PingMessagesReceived @@ -318,7 +317,7 @@ namespace Impostor.Hazel /// The number of hello messages received. /// /// - /// This is the number of hello messages that were received by the , incremented + /// This is the number of hello messages that were received by the , incremented /// each time that LogHelloReceive is called by the Connection. Messages are counted before the receive event is invoked. /// public int HelloMessagesReceived @@ -339,12 +338,12 @@ namespace Impostor.Hazel /// /// /// - /// This is the number of bytes of data (i.e. user bytes) that were received by the , + /// This is the number of bytes of data (i.e. user bytes) that were received by the , /// accumulated each time that LogReceive is called by the Connection. Messages are counted before the receive /// event is invoked. /// /// - /// For the number of bytes including protocol bytes see . + /// For the number of bytes including protocol bytes see . /// /// public long DataBytesReceived @@ -365,12 +364,12 @@ namespace Impostor.Hazel /// /// /// - /// This is the total number of bytes (the data bytes plus protocol bytes) that were received by the - /// , accumulated each time that LogReceive is called by the Connection. Messages are + /// This is the total number of bytes (the data bytes plus protocol bytes) that were received by the + /// , accumulated each time that LogReceive is called by the Connection. Messages are /// counted before the receive event is invoked. /// /// - /// For the number of data bytes excluding protocol bytes see . + /// For the number of data bytes excluding protocol bytes see . /// /// public long TotalBytesReceived diff --git a/src/Impostor.Hazel/DataReceivedEventArgs.cs b/src/Impostor.Hazel/DataReceivedEventArgs.cs index d2259e5..9176d8d 100644 --- a/src/Impostor.Hazel/DataReceivedEventArgs.cs +++ b/src/Impostor.Hazel/DataReceivedEventArgs.cs @@ -12,10 +12,10 @@ namespace Impostor.Hazel public readonly IMessageReader Message; /// - /// The the data was sent with. + /// The the data was sent with. /// public readonly MessageType Type; - + public DataReceivedEventArgs(Connection sender, IMessageReader msg, MessageType type) { this.Sender = sender; diff --git a/src/Impostor.Hazel/DisconnectedEventArgs.cs b/src/Impostor.Hazel/DisconnectedEventArgs.cs index 4872aea..d46df4b 100644 --- a/src/Impostor.Hazel/DisconnectedEventArgs.cs +++ b/src/Impostor.Hazel/DisconnectedEventArgs.cs @@ -6,13 +6,13 @@ namespace Impostor.Hazel public class DisconnectedEventArgs : EventArgs { /// - /// Optional disconnect reason. May be null. + /// Optional disconnect reason. May be null. /// public readonly string Reason; /// - /// Optional data sent with a disconnect message. May be null. - /// You must not recycle this. If you need the message outside of a callback, you should copy it. + /// Optional data sent with a disconnect message. May be null. + /// You must not recycle this. If you need the message outside of a callback, you should copy it. /// public readonly IMessageReader Message; diff --git a/src/Impostor.Hazel/HazelException.cs b/src/Impostor.Hazel/HazelException.cs index 81c8017..8c6fc3c 100644 --- a/src/Impostor.Hazel/HazelException.cs +++ b/src/Impostor.Hazel/HazelException.cs @@ -8,12 +8,14 @@ namespace Impostor.Hazel [Serializable] public class HazelException : Exception { - internal HazelException(string msg) : base(msg) + internal HazelException(string msg) : base (msg) { + } - internal HazelException(string msg, Exception e) : base(msg, e) + internal HazelException(string msg, Exception e) : base (msg, e) { + } } } diff --git a/src/Impostor.Hazel/IPMode.cs b/src/Impostor.Hazel/IPMode.cs index 3f43958..5eb6679 100644 --- a/src/Impostor.Hazel/IPMode.cs +++ b/src/Impostor.Hazel/IPMode.cs @@ -4,8 +4,8 @@ /// Represents the IP version that a connection or listener will use. /// /// - /// If you wand a client to connect or be able to connect using IPv6 then you should use , - /// this sets the underlying sockets to use IPv6 but still allow IPv4 sockets to connect for backwards compatability + /// If you wand a client to connect or be able to connect using IPv6 then you should use , + /// this sets the underlying sockets to use IPv6 but still allow IPv4 sockets to connect for backwards compatability /// and hence it is the default IPMode in most cases. /// public enum IPMode @@ -16,9 +16,9 @@ IPv4, /// - /// Instruction to use IPv6 only, IPv4 connections will not be able to connect. IPv4 addresses can be connected + /// Instruction to use IPv6 only, IPv4 connections will not be able to connect. IPv4 addresses can be connected /// by converting to IPv6 addresses. /// - IPv6, + IPv6 } } diff --git a/src/Impostor.Hazel/IRecyclable.cs b/src/Impostor.Hazel/IRecyclable.cs index 0ddc04c..69be122 100644 --- a/src/Impostor.Hazel/IRecyclable.cs +++ b/src/Impostor.Hazel/IRecyclable.cs @@ -3,7 +3,7 @@ /// /// Interface for all items that can be returned to an object pool. /// - /// + /// public interface IRecyclable { /// diff --git a/src/Impostor.Hazel/Impostor.Hazel.csproj b/src/Impostor.Hazel/Impostor.Hazel.csproj index 7ab52af..0099769 100644 --- a/src/Impostor.Hazel/Impostor.Hazel.csproj +++ b/src/Impostor.Hazel/Impostor.Hazel.csproj @@ -1,18 +1,18 @@ - - true - net5.0 - HAZEL_BAG - + + true + net5.0 + HAZEL_BAG + - - - - + + + + - - - + + + diff --git a/src/Impostor.Hazel/MessageReader.cs b/src/Impostor.Hazel/MessageReader.cs index 969e41b..317e45b 100644 --- a/src/Impostor.Hazel/MessageReader.cs +++ b/src/Impostor.Hazel/MessageReader.cs @@ -78,7 +78,7 @@ namespace Impostor.Hazel public bool ReadBoolean() { - var val = FastByte(); + byte val = FastByte(); return val != 0; } @@ -159,13 +159,13 @@ namespace Impostor.Hazel public uint ReadPackedUInt32() { - var readMore = true; - var shift = 0; + bool readMore = true; + int shift = 0; uint output = 0; while (readMore) { - var b = FastByte(); + byte b = FastByte(); if (b >= 0x80) { readMore = true; @@ -185,8 +185,8 @@ namespace Impostor.Hazel public void CopyTo(IMessageWriter writer) { - writer.Write((ushort)Length); - writer.Write((byte)Tag); + writer.Write((ushort) Length); + writer.Write((byte) Tag); writer.Write(Buffer.AsMemory(Offset, Length)); } @@ -213,7 +213,7 @@ namespace Impostor.Hazel System.Buffer.BlockCopy(Buffer, offsetEnd, Buffer, offsetStart, lengthToCopy); - ((MessageReader)message).Parent.AdjustLength(message.Offset, message.Length + 3); + ((MessageReader) message).Parent.AdjustLength(message.Offset, message.Length + 3); } private void AdjustLength(int offset, int amount) @@ -255,9 +255,9 @@ namespace Impostor.Hazel public Vector2 ReadVector2() { const float range = 50f; - - var x = ReadUInt16() / (float)ushort.MaxValue; - var y = ReadUInt16() / (float)ushort.MaxValue; + + var x = ReadUInt16() / (float) ushort.MaxValue; + var y = ReadUInt16() / (float) ushort.MaxValue; return new Vector2(Mathf.Lerp(-range, range, x), Mathf.Lerp(-range, range, y)); } diff --git a/src/Impostor.Hazel/MessageWriter.cs b/src/Impostor.Hazel/MessageWriter.cs index bb71a71..a7b57e7 100644 --- a/src/Impostor.Hazel/MessageWriter.cs +++ b/src/Impostor.Hazel/MessageWriter.cs @@ -1,11 +1,12 @@ -using System; +using Impostor.Api.Games; +using Impostor.Api.Net.Messages; + +using System; using System.Collections.Generic; using System.Net; using System.Numerics; using System.Text; -using Impostor.Api.Games; using Impostor.Api.Net.Inner; -using Impostor.Api.Net.Messages; using Impostor.Api.Unity; namespace Impostor.Hazel @@ -38,7 +39,7 @@ namespace Impostor.Hazel { if (includeHeader) { - var output = new byte[this.Length]; + byte[] output = new byte[this.Length]; System.Buffer.BlockCopy(this.Buffer, 0, output, 0, this.Length); return output; } @@ -47,17 +48,17 @@ namespace Impostor.Hazel switch (this.SendOption) { case MessageType.Reliable: - { - var output = new byte[this.Length - 3]; - System.Buffer.BlockCopy(this.Buffer, 3, output, 0, this.Length - 3); - return output; - } + { + byte[] output = new byte[this.Length - 3]; + System.Buffer.BlockCopy(this.Buffer, 3, output, 0, this.Length - 3); + return output; + } case MessageType.Unreliable: - { - var output = new byte[this.Length - 1]; - System.Buffer.BlockCopy(this.Buffer, 1, output, 0, this.Length - 1); - return output; - } + { + byte[] output = new byte[this.Length - 1]; + System.Buffer.BlockCopy(this.Buffer, 1, output, 0, this.Length - 1); + return output; + } default: throw new ArgumentOutOfRangeException(); } @@ -66,6 +67,7 @@ namespace Impostor.Hazel throw new NotImplementedException(); } + /// /// The option specifying how the message should be sent. public static MessageWriter Get(MessageType sendOption = MessageType.Unreliable) { @@ -104,8 +106,8 @@ namespace Impostor.Hazel public void Write(Vector2 vector) { - Write((ushort)(Mathf.ReverseLerp(vector.X) * (double)ushort.MaxValue)); - Write((ushort)(Mathf.ReverseLerp(vector.Y) * (double)ushort.MaxValue)); + Write((ushort)(Mathf.ReverseLerp(vector.X) * (double) ushort.MaxValue)); + Write((ushort)(Mathf.ReverseLerp(vector.Y) * (double) ushort.MaxValue)); } /// @@ -120,7 +122,7 @@ namespace Impostor.Hazel public void EndMessage() { var lastMessageStart = messageStarts.Pop(); - var length = (ushort)(this.Position - lastMessageStart - 3); // Minus length and type byte + ushort length = (ushort)(this.Position - lastMessageStart - 3); // Minus length and type byte this.Buffer[lastMessageStart] = (byte)length; this.Buffer[lastMessageStart + 1] = (byte)(length >> 8); } @@ -213,7 +215,7 @@ namespace Impostor.Hazel { fixed (byte* ptr = &this.Buffer[this.Position]) { - var valuePtr = (byte*)&value; + byte* valuePtr = (byte*)&value; *ptr = *valuePtr; *(ptr + 1) = *(valuePtr + 1); @@ -300,7 +302,7 @@ namespace Impostor.Hazel { do { - var b = (byte)(value & 0xFF); + byte b = (byte)(value & 0xFF); if (value >= 0x80) { b |= 0x80; @@ -315,7 +317,7 @@ namespace Impostor.Hazel public void Write(MessageWriter msg, bool includeHeader) { - var offset = 0; + int offset = 0; if (!includeHeader) { switch (msg.SendOption) @@ -338,8 +340,8 @@ namespace Impostor.Hazel byte b; unsafe { - var i = 1; - var bp = (byte*)&i; + int i = 1; + byte* bp = (byte*)&i; b = *bp; } diff --git a/src/Impostor.Hazel/NetworkConnection.cs b/src/Impostor.Hazel/NetworkConnection.cs index b530f01..9e81c5c 100644 --- a/src/Impostor.Hazel/NetworkConnection.cs +++ b/src/Impostor.Hazel/NetworkConnection.cs @@ -12,17 +12,17 @@ namespace Impostor.Hazel ReceivedZeroBytes, PingsWithoutResponse, ReliablePacketWithoutResponse, - ConnectionDisconnected, + ConnectionDisconnected } /// - /// Abstract base class for a to a remote end point via a network protocol like TCP or UDP. + /// Abstract base class for a to a remote end point via a network protocol like TCP or UDP. /// - /// + /// public abstract class NetworkConnection : Connection { /// - /// An event that gives us a chance to send well-formed disconnect messages to clients when an internal disconnect happens. + /// An event that gives us a chance to send well-formed disconnect messages to clients when an internal disconnect happens. /// public Func OnInternalDisconnect; @@ -30,8 +30,8 @@ namespace Impostor.Hazel /// The remote end point of this connection. /// /// - /// This is the end point of the other device given as an rather than a generic - /// as the base does. + /// This is the end point of the other device given as an rather than a generic + /// as the base does. /// public IPEndPoint RemoteEndPoint { get; protected set; } @@ -67,14 +67,14 @@ namespace Impostor.Hazel } /// - /// Called when socket is disconnected internally + /// Called when socket is disconnected internally /// internal async ValueTask DisconnectInternal(HazelInternalErrors error, string reason) { var handler = this.OnInternalDisconnect; if (handler != null) { - var messageToRemote = handler(error); + MessageWriter messageToRemote = handler(error); if (messageToRemote != null) { try diff --git a/src/Impostor.Hazel/NetworkConnectionListener.cs b/src/Impostor.Hazel/NetworkConnectionListener.cs index e2824b1..e1d7ffa 100644 --- a/src/Impostor.Hazel/NetworkConnectionListener.cs +++ b/src/Impostor.Hazel/NetworkConnectionListener.cs @@ -3,9 +3,9 @@ namespace Impostor.Hazel { /// - /// Abstract base class for a for network based connections. + /// Abstract base class for a for network based connections. /// - /// + /// public abstract class NetworkConnectionListener : ConnectionListener { /// diff --git a/src/Impostor.Hazel/NewConnectionEventArgs.cs b/src/Impostor.Hazel/NewConnectionEventArgs.cs index 77ec05d..be9e7a2 100644 --- a/src/Impostor.Hazel/NewConnectionEventArgs.cs +++ b/src/Impostor.Hazel/NewConnectionEventArgs.cs @@ -5,13 +5,13 @@ namespace Impostor.Hazel public struct NewConnectionEventArgs { /// - /// The data received from the client in the handshake. - /// This data is yours. Remember to recycle it. + /// The data received from the client in the handshake. + /// This data is yours. Remember to recycle it. /// public readonly IMessageReader HandshakeData; /// - /// The to the new client. + /// The to the new client. /// public readonly Connection Connection; diff --git a/src/Impostor.Hazel/ObjectPoolCustom.cs b/src/Impostor.Hazel/ObjectPoolCustom.cs index f1256eb..5c9ef9b 100644 --- a/src/Impostor.Hazel/ObjectPoolCustom.cs +++ b/src/Impostor.Hazel/ObjectPoolCustom.cs @@ -8,7 +8,7 @@ namespace Impostor.Hazel /// A fairly simple object pool for items that will be created a lot. /// /// The type that is pooled. - /// + /// public sealed class ObjectPoolCustom where T : IRecyclable { private int numberCreated; @@ -32,7 +32,7 @@ namespace Impostor.Hazel /// /// private readonly Func objectFactory; - + /// /// Internal constructor for our ObjectPool. /// @@ -48,7 +48,7 @@ namespace Impostor.Hazel internal T GetObject() { #if HAZEL_BAG - if (!pool.TryTake(out var item)) + if (!pool.TryTake(out T item)) { Interlocked.Increment(ref numberCreated); item = objectFactory.Invoke(); @@ -85,7 +85,7 @@ namespace Impostor.Hazel /// The item to return. internal void PutObject(T item) { - if (inuse.TryRemove(item, out var b)) + if (inuse.TryRemove(item, out bool b)) { #if HAZEL_BAG pool.Add(item); diff --git a/src/Impostor.Hazel/Udp/SendOptionInternal.cs b/src/Impostor.Hazel/Udp/SendOptionInternal.cs index 11baa85..c0c4e21 100644 --- a/src/Impostor.Hazel/Udp/SendOptionInternal.cs +++ b/src/Impostor.Hazel/Udp/SendOptionInternal.cs @@ -11,7 +11,7 @@ Hello = 8, /// - /// A single byte of continued existence + /// A single byte of continued existence /// Ping = 12, diff --git a/src/Impostor.Hazel/Udp/UdpBroadcastListener.cs b/src/Impostor.Hazel/Udp/UdpBroadcastListener.cs index 3c768ef..ed7b68d 100644 --- a/src/Impostor.Hazel/Udp/UdpBroadcastListener.cs +++ b/src/Impostor.Hazel/Udp/UdpBroadcastListener.cs @@ -89,21 +89,21 @@ namespace Impostor.Hazel.Udp return; } - if (numBytes < 3 + if (numBytes < 3 || buffer[0] != 4 || buffer[1] != 2) { this.StartListen(); return; } - var ipEnd = (IPEndPoint)endpt; - var data = UTF8Encoding.UTF8.GetString(buffer, 2, numBytes - 2); - var dataHash = data.GetHashCode(); + IPEndPoint ipEnd = (IPEndPoint)endpt; + string data = UTF8Encoding.UTF8.GetString(buffer, 2, numBytes - 2); + int dataHash = data.GetHashCode(); lock (packets) { - var found = false; - for (var i = 0; i < this.packets.Count; ++i) + bool found = false; + for (int i = 0; i < this.packets.Count; ++i) { var pkt = this.packets[i]; if (pkt == null || pkt.Data == null) @@ -146,17 +146,11 @@ namespace Impostor.Hazel.Udp { if (this.socket != null) { - try { this.socket.Shutdown(SocketShutdown.Both); } - catch { } - - try { this.socket.Close(); } - catch { } - - try { this.socket.Dispose(); } - catch { } - + try { this.socket.Shutdown(SocketShutdown.Both); } catch { } + try { this.socket.Close(); } catch { } + try { this.socket.Dispose(); } catch { } this.socket = null; } } } -} +} \ No newline at end of file diff --git a/src/Impostor.Hazel/Udp/UdpBroadcaster.cs b/src/Impostor.Hazel/Udp/UdpBroadcaster.cs index 32f0e18..5fa1cca 100644 --- a/src/Impostor.Hazel/Udp/UdpBroadcaster.cs +++ b/src/Impostor.Hazel/Udp/UdpBroadcaster.cs @@ -26,7 +26,7 @@ namespace Impostor.Hazel.Udp /// public void SetData(string data) { - var len = UTF8Encoding.UTF8.GetByteCount(data); + int len = UTF8Encoding.UTF8.GetByteCount(data); this.data = new byte[len + 2]; this.data[0] = 4; this.data[1] = 2; @@ -69,17 +69,11 @@ namespace Impostor.Hazel.Udp { if (this.socket != null) { - try { this.socket.Shutdown(SocketShutdown.Both); } - catch { } - - try { this.socket.Close(); } - catch { } - - try { this.socket.Dispose(); } - catch { } - + try { this.socket.Shutdown(SocketShutdown.Both); } catch { } + try { this.socket.Close(); } catch { } + try { this.socket.Dispose(); } catch { } this.socket = null; } } } -} +} \ No newline at end of file diff --git a/src/Impostor.Hazel/Udp/UdpClientConnection.cs b/src/Impostor.Hazel/Udp/UdpClientConnection.cs index cb27e24..5125ebe 100644 --- a/src/Impostor.Hazel/Udp/UdpClientConnection.cs +++ b/src/Impostor.Hazel/Udp/UdpClientConnection.cs @@ -1,7 +1,9 @@ using System; +using System.Buffers; using System.Net; using System.Net.Sockets; using System.Threading; +using System.Threading.Channels; using System.Threading.Tasks; using Impostor.Api.Net.Messages; using Microsoft.Extensions.ObjectPool; @@ -12,7 +14,7 @@ namespace Impostor.Hazel.Udp /// /// Represents a client's connection to a server that uses the UDP protocol. /// - /// + /// public sealed class UdpClientConnection : UdpConnection { private static readonly ILogger Logger = Log.ForContext(); @@ -29,7 +31,7 @@ namespace Impostor.Hazel.Udp /// /// Creates a new UdpClientConnection. /// - /// A to connect to. + /// A to connect to. public UdpClientConnection(IPEndPoint remoteEndPoint, ObjectPool readerPool, IPMode ipMode = IPMode.IPv4) : base(null, readerPool) { EndPoint = remoteEndPoint; @@ -38,7 +40,7 @@ namespace Impostor.Hazel.Udp _socket = new UdpClient { - DontFragment = false, + DontFragment = false }; _reliablePacketTimer = new Timer(ManageReliablePacketsInternal, null, 100, Timeout.Infinite); @@ -211,11 +213,8 @@ namespace Impostor.Hazel.Udp { State = ConnectionState.NotConnected; - try { _socket.Close(); } - catch { } - - try { _socket.Dispose(); } - catch { } + try { _socket.Close(); } catch { } + try { _socket.Dispose(); } catch { } _reliablePacketTimer.Dispose(); _connectWaitLock.Dispose(); diff --git a/src/Impostor.Hazel/Udp/UdpConnection.KeepAlive.cs b/src/Impostor.Hazel/Udp/UdpConnection.KeepAlive.cs index e8303a4..a73291b 100644 --- a/src/Impostor.Hazel/Udp/UdpConnection.KeepAlive.cs +++ b/src/Impostor.Hazel/Udp/UdpConnection.KeepAlive.cs @@ -8,6 +8,7 @@ namespace Impostor.Hazel.Udp { partial class UdpConnection { + /// /// Class to hold packet data /// @@ -57,7 +58,6 @@ namespace Impostor.Hazel.Udp ResetKeepAliveTimer(); } } - private int keepAliveInterval = 1500; public int MissingPingsUntilDisconnect { get; set; } = 6; @@ -109,9 +109,9 @@ namespace Impostor.Hazel.Udp // pings should cause a disconnect. private async ValueTask SendPing() { - var id = (ushort)Interlocked.Increment(ref lastIDAllocated); + ushort id = (ushort)Interlocked.Increment(ref lastIDAllocated); - var bytes = new byte[3]; + byte[] bytes = new byte[3]; bytes[0] = (byte)UdpSendOption.Ping; bytes[1] = (byte)(id >> 8); bytes[2] = (byte)id; @@ -164,4 +164,4 @@ namespace Impostor.Hazel.Udp } } } -} +} \ No newline at end of file diff --git a/src/Impostor.Hazel/Udp/UdpConnection.Reliable.cs b/src/Impostor.Hazel/Udp/UdpConnection.Reliable.cs index 9c13f22..7f43926 100644 --- a/src/Impostor.Hazel/Udp/UdpConnection.Reliable.cs +++ b/src/Impostor.Hazel/Udp/UdpConnection.Reliable.cs @@ -15,25 +15,25 @@ namespace Impostor.Hazel.Udp /// /// /// - /// For reliable delivery data is resent at specified intervals unless an acknowledgement is received from the + /// For reliable delivery data is resent at specified intervals unless an acknowledgement is received from the /// receiving device. The ResendTimeout specifies the interval between the packets being resent, each time a packet - /// is resent the interval is increased for that packet until the duration exceeds the value. + /// is resent the interval is increased for that packet until the duration exceeds the value. /// /// - /// Setting this to its default of 0 will mean the timeout is 2 times the value of the average ping, usually + /// Setting this to its default of 0 will mean the timeout is 2 times the value of the average ping, usually /// resulting in a more dynamic resend that responds to endpoints on slower or faster connections. /// /// public volatile int ResendTimeout = 0; /// - /// Max number of times to resend. 0 == no limit + /// Max number of times to resend. 0 == no limit /// public volatile int ResendLimit = 0; /// - /// A compounding multiplier to back off resend timeout. - /// Applied to ping before first timeout when ResendTimeout == 0. + /// A compounding multiplier to back off resend timeout. + /// Applied to ping before first timeout when ResendTimeout == 0. /// public volatile float ResendPingMultiplier = 2; @@ -48,7 +48,7 @@ namespace Impostor.Hazel.Udp internal ConcurrentDictionary reliableDataPacketsSent = new ConcurrentDictionary(); /// - /// Packet ids that have not been received, but are expected. + /// Packet ids that have not been received, but are expected. /// private HashSet reliableDataPacketsMissing = new HashSet(); @@ -63,7 +63,7 @@ namespace Impostor.Hazel.Udp /// Returns the average ping to this endpoint. /// /// - /// This returns the average ping for a one-way trip as calculated from the reliable packets that have been sent + /// This returns the average ping for a one-way trip as calculated from the reliable packets that have been sent /// and acknowledged by the endpoint. /// public float AveragePingMs = 500; @@ -72,7 +72,7 @@ namespace Impostor.Hazel.Udp /// The maximum times a message should be resent before marking the endpoint as disconnected. /// /// - /// Reliable packets will be resent at an interval defined in for the number of times + /// Reliable packets will be resent at an interval defined in for the number of times /// specified here. Once a packet has been retransmitted this number of times and has not been acknowledged the /// connection will be marked as disconnected and the Disconnected event /// will be invoked. @@ -136,10 +136,10 @@ namespace Impostor.Hazel.Udp var connection = this.Connection; if (!this.Acknowledged && connection != null) { - var lifetime = this.Stopwatch.ElapsedMilliseconds; + long lifetime = this.Stopwatch.ElapsedMilliseconds; if (lifetime >= connection.DisconnectTimeout) { - if (connection.reliableDataPacketsSent.TryRemove(this.Id, out var self)) + if (connection.reliableDataPacketsSent.TryRemove(this.Id, out Packet self)) { await connection.DisconnectInternal(HazelInternalErrors.ReliablePacketWithoutResponse, $"Reliable packet {self.Id} (size={this.Length}) was not ack'd after {lifetime}ms ({self.Retransmissions} resends)"); @@ -155,7 +155,7 @@ namespace Impostor.Hazel.Udp if (connection.ResendLimit != 0 && this.Retransmissions > connection.ResendLimit) { - if (connection.reliableDataPacketsSent.TryRemove(this.Id, out var self)) + if (connection.reliableDataPacketsSent.TryRemove(this.Id, out Packet self)) { await connection.DisconnectInternal(HazelInternalErrors.ReliablePacketWithoutResponse, $"Reliable packet {self.Id} (size={this.Length}) was not ack'd after {self.Retransmissions} resends ({lifetime}ms)"); @@ -196,12 +196,12 @@ namespace Impostor.Hazel.Udp internal async ValueTask ManageReliablePackets() { - var output = 0; + int output = 0; if (this.reliableDataPacketsSent.Count > 0) { foreach (var kvp in this.reliableDataPacketsSent) { - var pkt = kvp.Value; + Packet pkt = kvp.Value; try { @@ -222,12 +222,12 @@ namespace Impostor.Hazel.Udp /// The callback to make once the packet has been acknowledged. protected void AttachReliableID(byte[] buffer, int offset, int sendLength, Action ackCallback = null) { - var id = (ushort)Interlocked.Increment(ref lastIDAllocated); + ushort id = (ushort)Interlocked.Increment(ref lastIDAllocated); buffer[offset] = (byte)(id >> 8); buffer[offset + 1] = (byte)id; - var packet = Packet.GetObject(); + Packet packet = Packet.GetObject(); packet.Set( id, this, @@ -260,7 +260,7 @@ namespace Impostor.Hazel.Udp //Inform keepalive not to send for a while ResetKeepAliveTimer(); - var bytes = new byte[data.Length + 3]; + byte[] bytes = new byte[data.Length + 3]; //Add message type bytes[0] = sendOption; @@ -333,35 +333,35 @@ namespace Impostor.Hazel.Udp * * So... */ - + var result = true; lock (reliableDataPacketsMissing) { //Calculate overwritePointer - var overwritePointer = (ushort)(reliableReceiveLast - 32768); + ushort overwritePointer = (ushort)(reliableReceiveLast - 32768); //Calculate if it is a new packet by examining if it is within the range bool isNew; if (overwritePointer < reliableReceiveLast) - isNew = id > reliableReceiveLast || id <= overwritePointer; //Figure (2) + isNew = id > reliableReceiveLast || id <= overwritePointer; //Figure (2) else - isNew = id > reliableReceiveLast && id <= overwritePointer; //Figure (3) - + isNew = id > reliableReceiveLast && id <= overwritePointer; //Figure (3) + //If it's new or we've not received anything yet if (isNew) { // Mark items between the most recent receive and the id received as missing if (id > reliableReceiveLast) { - for (var i = (ushort)(reliableReceiveLast + 1); i < id; i++) + for (ushort i = (ushort)(reliableReceiveLast + 1); i < id; i++) { reliableDataPacketsMissing.Add(i); } } else { - var cnt = (ushort.MaxValue - reliableReceiveLast) + id; + int cnt = (ushort.MaxValue - reliableReceiveLast) + id; for (ushort i = 1; i < cnt; ++i) { reliableDataPacketsMissing.Add((ushort)(i + reliableReceiveLast)); @@ -371,7 +371,7 @@ namespace Impostor.Hazel.Udp //Update the most recently received reliableReceiveLast = id; } - + //Else it could be a missing packet else { @@ -382,7 +382,7 @@ namespace Impostor.Hazel.Udp } } } - + //Send an acknowledgement await SendAck(id); @@ -397,13 +397,13 @@ namespace Impostor.Hazel.Udp { this.pingsSinceAck = 0; - var id = (ushort)((bytes[1] << 8) + bytes[2]); + ushort id = (ushort)((bytes[1] << 8) + bytes[2]); AcknowledgeMessageId(id); if (bytes.Length == 4) { - var recentPackets = bytes[3]; - for (var i = 1; i <= 8; ++i) + byte recentPackets = bytes[3]; + for (int i = 1; i <= 8; ++i) { if ((recentPackets & 1) != 0) { @@ -420,7 +420,7 @@ namespace Impostor.Hazel.Udp private void AcknowledgeMessageId(ushort id) { // Dispose of timer and remove from dictionary - if (reliableDataPacketsSent.TryRemove(id, out var packet)) + if (reliableDataPacketsSent.TryRemove(id, out Packet packet)) { float rt = packet.Stopwatch.ElapsedMilliseconds; @@ -432,7 +432,7 @@ namespace Impostor.Hazel.Udp this.AveragePingMs = Math.Max(50, this.AveragePingMs * .7f + rt * .3f); } } - else if (this.activePingPackets.TryRemove(id, out var pingPkt)) + else if (this.activePingPackets.TryRemove(id, out PingPacket pingPkt)) { float rt = pingPkt.Stopwatch.ElapsedMilliseconds; @@ -455,7 +455,7 @@ namespace Impostor.Hazel.Udp byte recentPackets = 0; lock (this.reliableDataPacketsMissing) { - for (var i = 1; i <= 8; ++i) + for (int i = 1; i <= 8; ++i) { if (!this.reliableDataPacketsMissing.Contains((ushort)(id - i))) { @@ -464,12 +464,12 @@ namespace Impostor.Hazel.Udp } } - var bytes = new byte[] + byte[] bytes = new byte[] { (byte)UdpSendOption.Acknowledgement, (byte)(id >> 8), (byte)(id >> 0), - recentPackets, + recentPackets }; try diff --git a/src/Impostor.Hazel/Udp/UdpConnection.cs b/src/Impostor.Hazel/Udp/UdpConnection.cs index 4106bfe..5288d3c 100644 --- a/src/Impostor.Hazel/Udp/UdpConnection.cs +++ b/src/Impostor.Hazel/Udp/UdpConnection.cs @@ -34,7 +34,7 @@ namespace Impostor.Hazel.Udp Pipeline = Channel.CreateUnbounded(new UnboundedChannelOptions { SingleReader = true, - SingleWriter = true, + SingleWriter = true }); } @@ -113,13 +113,13 @@ namespace Impostor.Hazel.Udp /// protected abstract ValueTask WriteBytesToConnection(byte[] bytes, int length); - /// + /// public override async ValueTask SendAsync(IMessageWriter msg) { if (this._state != ConnectionState.Connected) throw new InvalidOperationException("Could not send data as this Connection is not connected. Did you disconnect?"); - var buffer = new byte[msg.Length]; + byte[] buffer = new byte[msg.Length]; Buffer.BlockCopy(msg.Buffer, 0, buffer, 0, msg.Length); switch (msg.SendOption) @@ -139,13 +139,13 @@ namespace Impostor.Hazel.Udp } } - /// + /// /// /// /// - /// Udp connections can currently send messages using and - /// . Fragmented messages are not currently supported and will default to - /// until implemented. + /// Udp connections can currently send messages using and + /// . Fragmented messages are not currently supported and will default to + /// until implemented. /// /// public override async ValueTask SendBytes(byte[] bytes, MessageType sendOption = MessageType.Unreliable) @@ -153,12 +153,12 @@ namespace Impostor.Hazel.Udp //Add header information and send await HandleSend(bytes, (byte)sendOption); } - + /// /// Handles the reliable/fragmented sending from this connection. /// /// The data being sent. - /// The specified as its byte value. + /// The specified as its byte value. /// The callback to invoke when this packet is acknowledged. /// The bytes that should actually be sent. protected async ValueTask HandleSend(byte[] data, byte sendOption, Action ackCallback = null) @@ -170,7 +170,7 @@ namespace Impostor.Hazel.Udp case (byte)UdpSendOption.Hello: await ReliableSend(sendOption, data, ackCallback); break; - + //Treat all else as unreliable default: await UnreliableSend(sendOption, data); @@ -226,16 +226,14 @@ namespace Impostor.Hazel.Udp { await DisconnectRemote("The remote sent a disconnect request", reader); } - break; - + //Treat everything else as unreliable default: using (var reader = message.Copy(1)) { await InvokeDataReceived(reader, MessageType.Unreliable); } - Statistics.LogUnreliableReceive(message.Length - 1, message.Length); break; } @@ -260,7 +258,7 @@ namespace Impostor.Hazel.Udp /// async ValueTask UnreliableSend(byte sendOption, byte[] data, int offset, int length) { - var bytes = new byte[length + 1]; + byte[] bytes = new byte[length + 1]; //Add message type bytes[0] = sendOption; @@ -295,8 +293,8 @@ namespace Impostor.Hazel.Udp return HandleSend(actualBytes, (byte)UdpSendOption.Hello, acknowledgeCallback); } - - /// + + /// protected override void Dispose(bool disposing) { if (disposing) diff --git a/src/Impostor.Hazel/Udp/UdpConnectionListener.cs b/src/Impostor.Hazel/Udp/UdpConnectionListener.cs index 782bf61..573a00c 100644 --- a/src/Impostor.Hazel/Udp/UdpConnectionListener.cs +++ b/src/Impostor.Hazel/Udp/UdpConnectionListener.cs @@ -4,6 +4,7 @@ using System.Collections.Concurrent; using System.Net; using System.Net.Sockets; using System.Threading; +using System.Threading.Channels; using System.Threading.Tasks; using Microsoft.Extensions.ObjectPool; using Serilog; @@ -19,12 +20,11 @@ namespace Impostor.Hazel.Udp private static readonly ILogger Logger = Log.ForContext(); /// - /// A callback for early connection rejection. - /// * Return false to reject connection. - /// * A null response is ok, we just won't send anything. + /// A callback for early connection rejection. + /// * Return false to reject connection. + /// * A null response is ok, we just won't send anything. /// public AcceptConnectionCheck AcceptConnection; - public delegate bool AcceptConnectionCheck(IPEndPoint endPoint, byte[] input, out byte[] response); private readonly UdpClient _socket; @@ -37,7 +37,7 @@ namespace Impostor.Hazel.Udp private Task _executingTask; /// - /// Creates a new UdpConnectionListener for the given , port and . + /// Creates a new UdpConnectionListener for the given , port and . /// /// The endpoint to listen on. /// @@ -72,7 +72,7 @@ namespace Impostor.Hazel.Udp } public int ConnectionCount => this._allConnections.Count; - + private async void ManageReliablePackets(object state) { foreach (var kvp in _allConnections) diff --git a/src/Impostor.Hazel/Udp/UdpConnectionRateLimit.cs b/src/Impostor.Hazel/Udp/UdpConnectionRateLimit.cs index b90d7b1..64881d3 100644 --- a/src/Impostor.Hazel/Udp/UdpConnectionRateLimit.cs +++ b/src/Impostor.Hazel/Udp/UdpConnectionRateLimit.cs @@ -72,4 +72,4 @@ namespace Impostor.Hazel.Udp _timer.Dispose(); } } -} +} \ No newline at end of file diff --git a/src/Impostor.Hazel/Udp/UdpServerConnection.cs b/src/Impostor.Hazel/Udp/UdpServerConnection.cs index 8c6cc06..8d3426e 100644 --- a/src/Impostor.Hazel/Udp/UdpServerConnection.cs +++ b/src/Impostor.Hazel/Udp/UdpServerConnection.cs @@ -9,14 +9,14 @@ namespace Impostor.Hazel.Udp /// /// Represents a servers's connection to a client that uses the UDP protocol. /// - /// + /// internal sealed class UdpServerConnection : UdpConnection { /// /// The connection listener that we use the socket of. /// /// - /// Udp server connections utilize the same socket in the listener for sends/receives, this is the listener that + /// Udp server connections utilize the same socket in the listener for sends/receives, this is the listener that /// created this connection and is hence the listener this conenction sends and receives via. /// public UdpConnectionListener Listener { get; private set; } @@ -63,7 +63,7 @@ namespace Impostor.Hazel.Udp if (this._state != ConnectionState.Connected) return false; this._state = ConnectionState.NotConnected; } - + var bytes = EmptyDisconnectBytes; if (data != null && data.Length > 0) { -- 2.39.5