Skip to content

Commit

Permalink
Change receive process
Browse files Browse the repository at this point in the history
  • Loading branch information
Filipe GOMES PEIXOTO committed Feb 25, 2018
1 parent 853b805 commit 8a953ee
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static void Clear<T>(this BlockingCollection<T> blockingCollection)
{
if (blockingCollection == null)
{
throw new ArgumentNullException("blockingCollection");
throw new ArgumentNullException(nameof(blockingCollection));
}

while (blockingCollection.Count > 0)
Expand Down
2 changes: 1 addition & 1 deletion src/Ether.Network/Packets/INetPacketStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public interface INetPacketStream : IDisposable
/// <typeparam name="T">Value type.</typeparam>
/// <param name="amount">Amount to read.</param>
/// <returns>An array of type T.</returns>
T[] Read<T>(int amount);
T[] ReadArray<T>(int amount);

/// <summary>
/// Writes a T value in the packet.
Expand Down
12 changes: 6 additions & 6 deletions src/Ether.Network/Packets/IPacketProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ public interface IPacketProcessor
bool IncludeHeader { get; }

/// <summary>
/// Gets the packet length.
/// Gets the packet message length.
/// </summary>
/// <param name="buffer">Input buffer</param>
/// <returns>Packet data length</returns>
int GetLength(byte[] buffer);
/// <param name="buffer">Header buffer</param>
/// <returns>Packet message data length</returns>
int GetMessageLength(byte[] buffer);

/// <summary>
/// Creates a T packet instance.
/// Creates a new <see cref="INetPacketStream"/> packet instance.
/// </summary>
/// <param name="buffer">Input buffer</param>
/// <returns></returns>
/// <returns>New packet</returns>
INetPacketStream CreatePacket(byte[] buffer);
}
}
12 changes: 6 additions & 6 deletions src/Ether.Network/Packets/NetPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ namespace Ether.Network.Packets
/// </summary>
public sealed class NetPacket : NetPacketStream
{
/// <summary>
/// Gets the packet buffer.
/// </summary>
private readonly int HeaderSize = sizeof(int);

/// <inheritdoc />
public override byte[] Buffer => this.BuildBuffer();

/// <summary>
/// Creates a new NetPacket in write-only mode.
/// Creates a new <see cref="NetPacket"/> in write-only mode.
/// </summary>
public NetPacket()
{
this.Write(0); // Packet size
}

/// <summary>
/// Creates a new NetPacket in read-only mode.
/// Creates a new <see cref="NetPacket"/> in read-only mode.
/// </summary>
/// <param name="buffer"></param>
public NetPacket(byte[] buffer)
Expand All @@ -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;
Expand Down
34 changes: 7 additions & 27 deletions src/Ether.Network/Packets/NetPacketProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,20 @@
using System.IO;
using System;
using System.Linq;

namespace Ether.Network.Packets
{
internal sealed class NetPacketProcessor : IPacketProcessor
{
/// <summary>
/// Gets the <see cref="NetPacket"/> header size.
/// </summary>
/// <inheritdoc />
public int HeaderSize => sizeof(int);

/// <summary>
/// Gets a value indicating whether the <see cref="NetPacket"/> header should be put in front of the buffer.
/// </summary>
/// <inheritdoc />
public bool IncludeHeader => false;

/// <summary>
/// Gets the <see cref="NetPacket"/> length size.
/// </summary>
/// <param name="buffer">Incoming buffer</param>
/// <returns>Packet length</returns>
public int GetLength(byte[] buffer)
{
var packetLength = 0;
/// <inheritdoc />
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;
}

/// <summary>
/// Creates a <see cref="NetPacket"/> instance.
/// </summary>
/// <param name="buffer">Input buffer</param>
/// <returns></returns>
/// <inheritdoc />
public INetPacketStream CreatePacket(byte[] buffer) => new NetPacket(buffer);
}
}
4 changes: 2 additions & 2 deletions src/Ether.Network/Packets/NetPacketStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public virtual T Read<T>()
}

/// <inheritdoc />
public virtual T[] Read<T>(int amount)
public virtual T[] ReadArray<T>(int amount)
{
if (this._state != PacketStateType.Read)
throw new InvalidOperationException("Packet is in write-only mode.");
Expand All @@ -83,7 +83,7 @@ public virtual T[] Read<T>(int amount)
}

/// <inheritdoc />
public void Write<T>(T value)
public virtual void Write<T>(T value)
{
if (this._state != PacketStateType.Write)
throw new InvalidOperationException("Packet is in read-only mode.");
Expand Down
7 changes: 5 additions & 2 deletions src/Ether.Network/Server/NetServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,11 @@ private void CloseConnection(SocketAsyncEventArgs e)
/// <param name="messageData">Incoming message data</param>
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);
});
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Ether.Network/Utils/SocketAsyncUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");

Expand Down
2 changes: 1 addition & 1 deletion test/Ether.Network.Tests/NetPacketStreamTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void ReadString()
byte[] value = null;

using (INetPacketStream packetStream = new NetPacketStream(StringTestArray))
value = packetStream.Read<byte>(StringTestArray.Length);
value = packetStream.ReadArray<byte>(StringTestArray.Length);

string convertedValue = Encoding.ASCII.GetString(value);

Expand Down

0 comments on commit 8a953ee

Please sign in to comment.