From: JamJar00 Date: Mon, 19 Dec 2016 16:34:53 +0000 (+0000) Subject: Refactored UDP X-Git-Tag: 1.0.0~119 X-Git-Url: https://git.deb.at/?a=commitdiff_plain;h=106a04e57d15414f6ee6d74155aea9e1c3eac770;p=rhonda%2Fimpostor.hazel.git Refactored UDP --- diff --git a/Hazel/Udp/UdpClientConnection.cs b/Hazel/Udp/UdpClientConnection.cs index 4f2e33f..26eece7 100644 --- a/Hazel/Udp/UdpClientConnection.cs +++ b/Hazel/Udp/UdpClientConnection.cs @@ -199,11 +199,9 @@ namespace Hazel.Udp return; } - //Decode the data received - byte[] buffer = HandleReceive(dataBuffer, bytesReceived); - SendOption sendOption = (SendOption)dataBuffer[0]; - - //TODO may get better performance with Handle receive after and block copy call added + //Copy data to new array + byte[] bytes = new byte[bytesReceived]; + Buffer.BlockCopy(dataBuffer, 0, bytes, 0, bytesReceived); //Begin receiving again try @@ -219,9 +217,8 @@ namespace Hazel.Udp //If the socket's been disposed then we can just end there. return; } - - if (buffer != null) - InvokeDataReceived(buffer, sendOption); + + HandleReceive(bytes); } /// diff --git a/Hazel/Udp/UdpConnection.Reliable.cs b/Hazel/Udp/UdpConnection.Reliable.cs index f3217f4..0ced334 100644 --- a/Hazel/Udp/UdpConnection.Reliable.cs +++ b/Hazel/Udp/UdpConnection.Reliable.cs @@ -177,12 +177,18 @@ namespace Hazel.Udp } /// - /// Writes the bytes neccessary for a reliable send and stores the send. + /// Sends the bytes reliably and stores the send. /// /// The byte array to write to. /// The callback to make once the packet has been acknowledged. - void WriteReliableSendHeader(byte[] bytes, Action ackCallback) + void ReliableSend(byte sendOption, byte[] data, Action ackCallback) { + byte[] bytes = new byte[data.Length + 3]; + + //Add message type + bytes[0] = sendOption; + + //Find and reliable ID lock (reliableDataPacketsSent) { //Find an ID not used yet. @@ -240,6 +246,26 @@ namespace Hazel.Udp //Remember packet reliableDataPacketsSent.Add(id, packet); } + + //Copy data into new array + Buffer.BlockCopy(data, 0, bytes, bytes.Length - data.Length, data.Length); + + //Write to connection + WriteBytesToConnection(bytes); + + Statistics.LogSend(data.Length, bytes.Length); + } + + /// + /// Handles a reliable message being received and invokes the data event. + /// + /// The buffer received. + void ReliableReceive(byte[] buffer) + { + if (ProcessReliableReceive(buffer)) + InvokeDataReceived(SendOption.Reliable, buffer, 3); + + Statistics.LogReceive(buffer.Length - 3, buffer.Length); } /// @@ -247,7 +273,7 @@ namespace Hazel.Udp /// /// The buffer containing the data. /// Whether the packet was a new packet or not. - bool HandleReliableReceive(byte[] bytes) + bool ProcessReliableReceive(byte[] bytes) { //Get the ID form the packet ushort id = (ushort)((bytes[1] << 8) + bytes[2]); @@ -322,7 +348,7 @@ namespace Hazel.Udp /// Handles acknowledgement packets to us. /// /// The buffer containing the data. - void HandleAcknowledgement(byte[] bytes) + void AcknowledgementReceive(byte[] bytes) { //Get ID ushort id = (ushort)((bytes[1] << 8) + bytes[2]); @@ -349,6 +375,8 @@ namespace Hazel.Udp reliableDataPacketsSent.Remove(id); } } + + Statistics.LogReceive(0, bytes.Length); } /// diff --git a/Hazel/Udp/UdpConnection.cs b/Hazel/Udp/UdpConnection.cs index ed21ccd..e65b1f5 100644 --- a/Hazel/Udp/UdpConnection.cs +++ b/Hazel/Udp/UdpConnection.cs @@ -56,83 +56,92 @@ namespace Hazel.Udp /// The bytes that should actually be sent. protected void HandleSend(byte[] data, byte sendOption, Action ackCallback = null) { - byte[] bytes; + //Inform keepalive not to send for a while + ResetKeepAliveTimer(); + switch (sendOption) { //Handle reliable header and hellos case (byte)SendOption.Reliable: case (byte)SendOptionInternal.Hello: - bytes = new byte[data.Length + 3]; - WriteReliableSendHeader(bytes, ackCallback); + ReliableSend(sendOption, data, ackCallback); break; - + + //Treat all else as unreliable default: - bytes = new byte[data.Length + 1]; + UnreliableSend(sendOption, data); break; } - - //Add message type - bytes[0] = sendOption; - - //Copy data into new array - Buffer.BlockCopy(data, 0, bytes, bytes.Length - data.Length, data.Length); - - //Inform keepalive not to send for a while - ResetKeepAliveTimer(); - - //Write to connection - WriteBytesToConnection(bytes); - - Statistics.LogSend(data.Length, bytes.Length); } /// /// Handles the receiving of data. /// /// The buffer containing the bytes received. - /// The number of bytes that were received. - /// The bytes of data received. - protected byte[] HandleReceive(byte[] buffer, int bytesReceived) + protected void HandleReceive(byte[] buffer) { //Inform keepalive not to send for a while ResetKeepAliveTimer(); - - int headerSize = 1; + switch (buffer[0]) { - //Handle reliable receives + //Handle reliable receives case (byte)SendOption.Reliable: - headerSize = 3; - - if (HandleReliableReceive(buffer) == false) - return null; + ReliableReceive(buffer); break; - //Handle acknowledgments + //Handle acknowledgments case (byte)SendOptionInternal.Acknowledgement: - HandleAcknowledgement(buffer); - - return null; + AcknowledgementReceive(buffer); + break; - //We need to acknowledge hello messages so just use the same reliable receive - //method + //We need to acknowledge hello messages but dont want to invoke any events! case (byte)SendOptionInternal.Hello: - HandleReliableReceive(buffer); - - return null; + ProcessReliableReceive(buffer); + Statistics.LogReceive(buffer.Length - 3, buffer.Length); + break; case (byte)SendOptionInternal.Disconnect: HandleDisconnect(); + Statistics.LogReceive(0, buffer.Length); + break; - return null; + //Treat everything else as unreliable + default: + InvokeDataReceived(SendOption.None, buffer, 1); + Statistics.LogReceive(buffer.Length - 1, buffer.Length); + break; } + } + + void UnreliableSend(byte sendOption, byte[] data) + { + byte[] bytes = new byte[data.Length + 1]; + + //Add message type + bytes[0] = sendOption; + + //Copy data into new array + Buffer.BlockCopy(data, 0, bytes, bytes.Length - data.Length, data.Length); - byte[] dataBytes = new byte[bytesReceived - headerSize]; - Buffer.BlockCopy(buffer, headerSize, dataBytes, 0, dataBytes.Length); + //Write to connection + WriteBytesToConnection(bytes); - Statistics.LogReceive(dataBytes.Length, bytesReceived); + Statistics.LogSend(data.Length, bytes.Length); + } - return dataBytes; + /// + /// Helper method to invoke the data received event. + /// + /// The send option the message was received with. + /// The buffer received. + /// The offset of data in the buffer. + void InvokeDataReceived(SendOption sendOption, byte[] buffer, int dataOffset) + { + byte[] dataBytes = new byte[buffer.Length - dataOffset]; + Buffer.BlockCopy(buffer, dataOffset, dataBytes, 0, dataBytes.Length); + + InvokeDataReceived(dataBytes, sendOption); } /// diff --git a/Hazel/Udp/UdpServerConnection.cs b/Hazel/Udp/UdpServerConnection.cs index ff19df4..1eb2afe 100644 --- a/Hazel/Udp/UdpServerConnection.cs +++ b/Hazel/Udp/UdpServerConnection.cs @@ -71,10 +71,7 @@ namespace Hazel.Udp /// internal void InvokeDataReceived(byte[] buffer) { - byte[] data = HandleReceive(buffer, buffer.Length); - - if (data != null) - InvokeDataReceived(data, (SendOption)buffer[0]); + HandleReceive(buffer); } ///