From 8a953ee712c54762653b20674159cbeaeeace762 Mon Sep 17 00:00:00 2001 From: Filipe GOMES PEIXOTO Date: Sun, 25 Feb 2018 12:19:14 +0100 Subject: [PATCH] Change receive process --- .../BlockingCollectionExtensions.cs | 2 +- src/Ether.Network/Packets/INetPacketStream.cs | 2 +- src/Ether.Network/Packets/IPacketProcessor.cs | 12 +++---- src/Ether.Network/Packets/NetPacket.cs | 12 +++---- .../Packets/NetPacketProcessor.cs | 34 ++++--------------- src/Ether.Network/Packets/NetPacketStream.cs | 4 +-- src/Ether.Network/Server/NetServer.cs | 7 ++-- src/Ether.Network/Utils/SocketAsyncUtils.cs | 2 +- .../NetPacketStreamTest.cs | 2 +- 9 files changed, 30 insertions(+), 47 deletions(-) diff --git a/src/Ether.Network/Extensions/BlockingCollectionExtensions.cs b/src/Ether.Network/Extensions/BlockingCollectionExtensions.cs index b5961ef..aa055b7 100644 --- a/src/Ether.Network/Extensions/BlockingCollectionExtensions.cs +++ b/src/Ether.Network/Extensions/BlockingCollectionExtensions.cs @@ -17,7 +17,7 @@ public static void Clear(this BlockingCollection blockingCollection) { if (blockingCollection == null) { - throw new ArgumentNullException("blockingCollection"); + throw new ArgumentNullException(nameof(blockingCollection)); } while (blockingCollection.Count > 0) diff --git a/src/Ether.Network/Packets/INetPacketStream.cs b/src/Ether.Network/Packets/INetPacketStream.cs index bff22f0..965aa06 100644 --- a/src/Ether.Network/Packets/INetPacketStream.cs +++ b/src/Ether.Network/Packets/INetPacketStream.cs @@ -35,7 +35,7 @@ public interface INetPacketStream : IDisposable /// Value type. /// Amount to read. /// An array of type T. - T[] Read(int amount); + T[] ReadArray(int amount); /// /// Writes a T value in the packet. diff --git a/src/Ether.Network/Packets/IPacketProcessor.cs b/src/Ether.Network/Packets/IPacketProcessor.cs index a9061e9..cad7c8d 100644 --- a/src/Ether.Network/Packets/IPacketProcessor.cs +++ b/src/Ether.Network/Packets/IPacketProcessor.cs @@ -16,17 +16,17 @@ public interface IPacketProcessor bool IncludeHeader { get; } /// - /// Gets the packet length. + /// Gets the packet message length. /// - /// Input buffer - /// Packet data length - int GetLength(byte[] buffer); + /// Header buffer + /// Packet message data length + int GetMessageLength(byte[] buffer); /// - /// Creates a T packet instance. + /// Creates a new packet instance. /// /// Input buffer - /// + /// New packet INetPacketStream CreatePacket(byte[] buffer); } } diff --git a/src/Ether.Network/Packets/NetPacket.cs b/src/Ether.Network/Packets/NetPacket.cs index 9605283..eace933 100644 --- a/src/Ether.Network/Packets/NetPacket.cs +++ b/src/Ether.Network/Packets/NetPacket.cs @@ -7,13 +7,13 @@ namespace Ether.Network.Packets /// public sealed class NetPacket : NetPacketStream { - /// - /// Gets the packet buffer. - /// + private readonly int HeaderSize = sizeof(int); + + /// public override byte[] Buffer => this.BuildBuffer(); /// - /// Creates a new NetPacket in write-only mode. + /// Creates a new in write-only mode. /// public NetPacket() { @@ -21,7 +21,7 @@ public NetPacket() } /// - /// Creates a new NetPacket in read-only mode. + /// Creates a new in read-only mode. /// /// public NetPacket(byte[] buffer) @@ -38,7 +38,7 @@ private byte[] BuildBuffer() long oldPosition = this.Position; this.Seek(0, SeekOrigin.Begin); - this.Write(this.Size); + this.Write(this.Size - HeaderSize); this.Seek((int)oldPosition, SeekOrigin.Begin); return base.Buffer; diff --git a/src/Ether.Network/Packets/NetPacketProcessor.cs b/src/Ether.Network/Packets/NetPacketProcessor.cs index c5e51ba..15cc720 100644 --- a/src/Ether.Network/Packets/NetPacketProcessor.cs +++ b/src/Ether.Network/Packets/NetPacketProcessor.cs @@ -1,40 +1,20 @@ -using System.IO; +using System; +using System.Linq; namespace Ether.Network.Packets { internal sealed class NetPacketProcessor : IPacketProcessor { - /// - /// Gets the header size. - /// + /// public int HeaderSize => sizeof(int); - /// - /// Gets a value indicating whether the header should be put in front of the buffer. - /// + /// public bool IncludeHeader => false; - /// - /// Gets the length size. - /// - /// Incoming buffer - /// Packet length - public int GetLength(byte[] buffer) - { - var packetLength = 0; + /// + public int GetMessageLength(byte[] buffer) => BitConverter.ToInt32(buffer.Take(HeaderSize).ToArray(), 0); - using (var memoryStream = new MemoryStream(buffer)) - using (var binaryReader = new BinaryReader(memoryStream)) - packetLength = binaryReader.ReadInt32(); - - return packetLength; - } - - /// - /// Creates a instance. - /// - /// Input buffer - /// + /// public INetPacketStream CreatePacket(byte[] buffer) => new NetPacket(buffer); } } diff --git a/src/Ether.Network/Packets/NetPacketStream.cs b/src/Ether.Network/Packets/NetPacketStream.cs index 93e1d2c..48984c8 100644 --- a/src/Ether.Network/Packets/NetPacketStream.cs +++ b/src/Ether.Network/Packets/NetPacketStream.cs @@ -63,7 +63,7 @@ public virtual T Read() } /// - public virtual T[] Read(int amount) + public virtual T[] ReadArray(int amount) { if (this._state != PacketStateType.Read) throw new InvalidOperationException("Packet is in write-only mode."); @@ -83,7 +83,7 @@ public virtual T[] Read(int amount) } /// - public void Write(T value) + public virtual void Write(T value) { if (this._state != PacketStateType.Write) throw new InvalidOperationException("Packet is in read-only mode."); diff --git a/src/Ether.Network/Server/NetServer.cs b/src/Ether.Network/Server/NetServer.cs index 92259b2..7acc4a5 100644 --- a/src/Ether.Network/Server/NetServer.cs +++ b/src/Ether.Network/Server/NetServer.cs @@ -335,8 +335,11 @@ private void CloseConnection(SocketAsyncEventArgs e) /// Incoming message data private void HandleIncomingMessages(T user, byte[] messageData) { - using (INetPacketStream packet = this.PacketProcessor.CreatePacket(messageData)) - user.HandleMessage(packet); + Task.Run(() => + { + using (INetPacketStream packet = this.PacketProcessor.CreatePacket(messageData)) + user.HandleMessage(packet); + }); } /// diff --git a/src/Ether.Network/Utils/SocketAsyncUtils.cs b/src/Ether.Network/Utils/SocketAsyncUtils.cs index d92e0b7..5ba106b 100644 --- a/src/Ether.Network/Utils/SocketAsyncUtils.cs +++ b/src/Ether.Network/Utils/SocketAsyncUtils.cs @@ -34,7 +34,7 @@ internal static void ReceiveData(SocketAsyncEventArgs e, IAsyncUserToken token, if (token.ReceivedHeaderBytesCount == packetProcessor.HeaderSize && token.HeaderData != null) { if (!token.MessageSize.HasValue) - token.MessageSize = packetProcessor.GetLength(token.HeaderData) - packetProcessor.HeaderSize; + token.MessageSize = packetProcessor.GetMessageLength(token.HeaderData); if (token.MessageSize.Value < 0) throw new InvalidOperationException("Message size cannot be smaller than zero."); diff --git a/test/Ether.Network.Tests/NetPacketStreamTest.cs b/test/Ether.Network.Tests/NetPacketStreamTest.cs index a40a0ef..6b1ba07 100644 --- a/test/Ether.Network.Tests/NetPacketStreamTest.cs +++ b/test/Ether.Network.Tests/NetPacketStreamTest.cs @@ -53,7 +53,7 @@ public void ReadString() byte[] value = null; using (INetPacketStream packetStream = new NetPacketStream(StringTestArray)) - value = packetStream.Read(StringTestArray.Length); + value = packetStream.ReadArray(StringTestArray.Length); string convertedValue = Encoding.ASCII.GetString(value);