From 72d45670afe6b691a1c5234bc2117f811258e756 Mon Sep 17 00:00:00 2001 From: Forest Date: Wed, 2 Jan 2019 11:27:16 -0800 Subject: [PATCH] Clean up some stuff --- Hazel/Connection.cs | 8 ++++---- Hazel/NetworkConnectionListener.cs | 2 +- Hazel/Udp/UdpClientConnection.cs | 19 +++++-------------- Hazel/Udp/UdpConnection.Reliable.cs | 15 +++++++-------- Hazel/Udp/UdpConnection.cs | 14 +++----------- Hazel/Udp/UdpConnectionListener.cs | 23 +++++++---------------- Hazel/Udp/UdpServerConnection.cs | 16 ++++------------ 7 files changed, 31 insertions(+), 66 deletions(-) diff --git a/Hazel/Connection.cs b/Hazel/Connection.cs index f136ffc..5e14cc6 100644 --- a/Hazel/Connection.cs +++ b/Hazel/Connection.cs @@ -113,20 +113,20 @@ namespace Hazel { get { - return this.state; + return this._state; } protected set { - this.state = value; - if (this.state == ConnectionState.Connected) + this._state = value; + if (this._state == ConnectionState.Connected) connectWaitLock.Set(); else connectWaitLock.Reset(); } } - protected ConnectionState state; + protected ConnectionState _state; /// /// Reset event that is triggered when the connection is marked Connected. diff --git a/Hazel/NetworkConnectionListener.cs b/Hazel/NetworkConnectionListener.cs index 1e5e237..af26c4c 100644 --- a/Hazel/NetworkConnectionListener.cs +++ b/Hazel/NetworkConnectionListener.cs @@ -16,7 +16,7 @@ namespace Hazel /// /// The local end point the listener is listening for new clients on. /// - public EndPoint EndPoint { get; protected set; } + public IPEndPoint EndPoint { get; protected set; } /// /// The IPMode the listener is listening for new clients on. diff --git a/Hazel/Udp/UdpClientConnection.cs b/Hazel/Udp/UdpClientConnection.cs index 048ff60..4dd101e 100644 --- a/Hazel/Udp/UdpClientConnection.cs +++ b/Hazel/Udp/UdpClientConnection.cs @@ -78,9 +78,6 @@ namespace Hazel.Udp { DataSentRaw?.Invoke(bytes, length); - if (State != ConnectionState.Connected && State != ConnectionState.Connecting) - throw new InvalidOperationException("Could not send data as this Connection is not connected and is not connecting. Did you disconnect?"); - try { socket.BeginSendTo( @@ -123,9 +120,6 @@ namespace Hazel.Udp { DataSentRaw?.Invoke(bytes, length); - if (State != ConnectionState.Connected && State != ConnectionState.Connecting) - throw new InvalidOperationException("Could not send data as this Connection is not connected and is not connecting. Did you disconnect?"); - try { socket.SendTo( @@ -166,10 +160,7 @@ namespace Hazel.Udp /// public override void ConnectAsync(byte[] bytes = null, int timeout = 5000) { - if (State != ConnectionState.NotConnected) - throw new InvalidOperationException("Cannot connect as the Connection is already connected."); - - State = ConnectionState.Connecting; + this.State = ConnectionState.Connecting; //Begin listening try @@ -292,11 +283,11 @@ namespace Hazel.Udp { if (disposing) { - if (this.state == ConnectionState.Connected - || this.state == ConnectionState.Disconnecting) + if (this._state == ConnectionState.Connected + || this._state == ConnectionState.Disconnecting) { - // SendDisconnect(); - this.state = ConnectionState.NotConnected; + SendDisconnect(); + this._state = ConnectionState.NotConnected; } } diff --git a/Hazel/Udp/UdpConnection.Reliable.cs b/Hazel/Udp/UdpConnection.Reliable.cs index 4788cdb..0e1182e 100644 --- a/Hazel/Udp/UdpConnection.Reliable.cs +++ b/Hazel/Udp/UdpConnection.Reliable.cs @@ -200,12 +200,12 @@ namespace Hazel.Udp foreach (var kvp in this.reliableDataPacketsSent) { Packet pkt = kvp.Value; - - try - { - output += pkt.Resend(); - } - catch { } + + try + { + output += pkt.Resend(); + } + catch { } minTimeout = Math.Min(pkt.NextTimeout, minTimeout); } @@ -239,8 +239,7 @@ namespace Hazel.Udp buffer, sendLength, resendTimeout > 0 ? resendTimeout : (int)Math.Max(300, Math.Min(AveragePingMs * this.ResendPingMultiplier, 2000)), - ackCallback - ); + ackCallback); if (!reliableDataPacketsSent.TryAdd(id, packet)) { diff --git a/Hazel/Udp/UdpConnection.cs b/Hazel/Udp/UdpConnection.cs index a488cde..7c1b592 100644 --- a/Hazel/Udp/UdpConnection.cs +++ b/Hazel/Udp/UdpConnection.cs @@ -41,7 +41,7 @@ namespace Hazel.Udp public override void Send(MessageWriter msg) { //Early check - if (State != ConnectionState.Connected) + if (this._state != ConnectionState.Connected) throw new InvalidOperationException("Could not send data as this Connection is not connected. Did you disconnect?"); byte[] buffer = new byte[msg.Length]; @@ -75,10 +75,6 @@ namespace Hazel.Udp /// public override void SendBytes(byte[] bytes, SendOption sendOption = SendOption.None) { - //Early check - if (State != ConnectionState.Connected) - throw new InvalidOperationException("Could not send data as this Connection is not connected. Did you disconnect?"); - //Add header information and send HandleSend(bytes, (byte)sendOption); } @@ -100,10 +96,6 @@ namespace Hazel.Udp /// public override void SendBytes(byte[] bytes, int offset, int length, SendOption sendOption = SendOption.None) { - //Early check - if (State != ConnectionState.Connected) - throw new InvalidOperationException("Could not send data as this Connection is not connected. Did you disconnect?"); - switch (sendOption) { //Handle reliable header and hellos @@ -269,9 +261,9 @@ namespace Hazel.Udp bool invoke = false; lock (this) { - if (this.state == ConnectionState.Connected) + if (this._state == ConnectionState.Connected) { - this.state = skipSendDisconnect ? ConnectionState.NotConnected : ConnectionState.Disconnecting; + this._state = skipSendDisconnect ? ConnectionState.NotConnected : ConnectionState.Disconnecting; invoke = true; } } diff --git a/Hazel/Udp/UdpConnectionListener.cs b/Hazel/Udp/UdpConnectionListener.cs index e8f4def..b61a91a 100644 --- a/Hazel/Udp/UdpConnectionListener.cs +++ b/Hazel/Udp/UdpConnectionListener.cs @@ -57,6 +57,9 @@ namespace Hazel.Udp this.socket.SetSocketOption(SocketOptionLevel.IPv6, (SocketOptionName)27, false); } + socket.ReceiveBufferSize = 4194304; + socket.SendBufferSize = 1048576; + reliablePacketTimer = new Timer(ManageReliablePackets, null, 100, Timeout.Infinite); } @@ -100,7 +103,7 @@ namespace Hazel.Udp /// /// Instructs the listener to begin listening. /// - public void StartListeningForData() + private void StartListeningForData() { EndPoint remoteEP = EndPoint; @@ -214,21 +217,9 @@ namespace Hazel.Udp } } - var stopwatch = System.Diagnostics.Stopwatch.StartNew(); - try - { - //Inform the connection of the buffer (new connections need to send an ack back to client) - connection.HandleReceive(message, bytesReceived); - } - finally - { - var el = stopwatch.ElapsedMilliseconds; - if (el > 5) - { - this.Logger?.Invoke($"Long Packet {el}ms = {string.Join(" ", message.Buffer.Take(bytesReceived))}"); - } - } - + //Inform the connection of the buffer (new connections need to send an ack back to client) + connection.HandleReceive(message, bytesReceived); + //If it's a new connection invoke the NewConnection event. if (!aware) { diff --git a/Hazel/Udp/UdpServerConnection.cs b/Hazel/Udp/UdpServerConnection.cs index b0f5058..a9f63c4 100644 --- a/Hazel/Udp/UdpServerConnection.cs +++ b/Hazel/Udp/UdpServerConnection.cs @@ -21,12 +21,7 @@ namespace Hazel.Udp /// created this connection and is hence the listener this conenction sends and receives via. /// public UdpConnectionListener Listener { get; private set; } - - /// - /// Lock object for the writing to the state of the connection. - /// - private ReaderWriterLockSlim stateLock = new ReaderWriterLockSlim(); - + /// /// Creates a UdpConnection for the virtual connection to the endpoint. /// @@ -47,9 +42,6 @@ namespace Hazel.Udp /// protected override void WriteBytesToConnection(byte[] bytes, int length) { - if (State != ConnectionState.Connected) - throw new InvalidOperationException("Could not send data: Not connected."); - Listener.SendData(bytes, length, RemoteEndPoint); } @@ -98,11 +90,11 @@ namespace Hazel.Udp if (disposing) { - if (this.state == ConnectionState.Connected - || this.state == ConnectionState.Disconnecting) + if (this._state == ConnectionState.Connected + || this._state == ConnectionState.Disconnecting) { SendDisconnect(); - this.state = ConnectionState.NotConnected; + this._state = ConnectionState.NotConnected; } } -- 2.39.5