Skip to content

Commit

Permalink
Merge pull request #30 from exomia/development
Browse files Browse the repository at this point in the history
v1.6.2.0
  • Loading branch information
baetz-daniel authored Aug 5, 2019
2 parents 618f26b + 5b54a19 commit 427e02c
Show file tree
Hide file tree
Showing 24 changed files with 295 additions and 152 deletions.
22 changes: 11 additions & 11 deletions Example.Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@

#endregion

#define TCP
#define TCP9

#if TCP
using Exomia.Network.TCP;
#else
using Exomia.Network.UDP;
#endif
using System;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Exomia.Network;
#if TCP
using Exomia.Network.TCP;

#else
using Exomia.Network.UDP;
#endif

namespace Example.Client
{
Expand All @@ -30,9 +30,9 @@ class Program
private static async Task Main(string[] args)
{
#if TCP
TcpClientEap client = new TcpClientEap(8096);
TcpClientEap client = new TcpClientEap(512);
#else
var client = new UdpClientEap(8096);
UdpClientEap client = new UdpClientEap(512);
#endif
client.Disconnected += (c, r) => { Console.WriteLine(r + " | Disconnected"); };
client.AddCommand(
Expand All @@ -47,10 +47,10 @@ private static async Task Main(string[] args)
Console.WriteLine(data + " -- OK");
return true;
});

Thread.Sleep(100);
Console.WriteLine(client.Connect("127.0.0.1", 3000) ? "CONNECTED" : "CONNECT FAILED");

byte[] request = Encoding.UTF8.GetBytes(string.Join(" ", Enumerable.Range(1, 10_000)));
byte[] request = Encoding.UTF8.GetBytes(string.Join(" ", Enumerable.Range(1, 1_000)));
Console.WriteLine(request.Length);
for (int i = 0; i < 10; i++)
{
Expand Down
18 changes: 8 additions & 10 deletions Example.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@

#endregion

#define TCP
#define TCP9

using System;
using System.Text;
using Exomia.Network;
#if TCP
using Exomia.Network.TCP;

#else
using Exomia.Network.UDP;
#endif
using System;
using System.Text;
using Exomia.Network;

namespace Example.Server
{
Expand All @@ -45,8 +44,7 @@ private static void Main(string[] args)
45, (server1, client, data, responseid) =>
{
string request = (string)data;
//Console.WriteLine($"Request: {request}");
Console.WriteLine($"Request: {request}");
byte[] buffer = Encoding.UTF8.GetBytes(DateTime.Now.ToLongDateString());
server1.SendTo(client, 45, buffer, 0, buffer.Length, responseid);
return true;
Expand All @@ -66,14 +64,14 @@ class Server : TcpServerEapBase<ServerClient>
class Server : UdpServerEapBase<ServerClient>
#endif
{
public Server(ushort expectedMaxClient = 32, ushort expectedMaxPayloadSize = 512)
: base(expectedMaxClient, expectedMaxPayloadSize) { }

protected override bool CreateServerClient(out ServerClient serverClient)
{
serverClient = new ServerClient();
return true;
}

public Server(ushort expectedMaxClient = 32, ushort expectedMaxPayloadSize = 8096)
: base(expectedMaxClient, expectedMaxPayloadSize) { }
}

#if TCP
Expand Down
70 changes: 48 additions & 22 deletions Exomia.Network/ClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ public abstract class ClientBase : IClient
/// </summary>
public event DisconnectedHandler Disconnected;

/// <summary>
/// Occurs when data from a client is received.
/// </summary>
public event CommandDataReceivedHandler DataReceived
{
add { _dataReceived.Add(value); }
remove { _dataReceived.Remove(value); }
}

/// <summary>
/// called than a ping is received.
/// </summary>
Expand Down Expand Up @@ -116,6 +125,16 @@ public abstract class ClientBase : IClient
/// </summary>
private readonly Dictionary<uint, TaskCompletionSource<Packet>> _taskCompletionSources;

/// <summary>
/// The listener count.
/// </summary>
private readonly byte _listenerCount;

/// <summary>
/// The client data received event handler.
/// </summary>
private readonly Event<CommandDataReceivedHandler> _dataReceived;

/// <summary>
/// The data received callbacks lock.
/// </summary>
Expand Down Expand Up @@ -176,8 +195,9 @@ public string ServerAddress
/// <summary>
/// Initializes a new instance of the <see cref="ClientBase" /> class.
/// </summary>
private protected ClientBase()
private protected ClientBase(byte listenerCount = 1)
{
_listenerCount = listenerCount;
_clientSocket = null;
_dataReceivedCallbacks = new Dictionary<uint, ClientEventEntry>(INITIAL_QUEUE_SIZE);
_taskCompletionSources =
Expand All @@ -186,6 +206,8 @@ private protected ClientBase()
_lockTaskCompletionSources = new SpinLock(Debugger.IsAttached);
_dataReceivedCallbacksLock = new SpinLock(Debugger.IsAttached);

_dataReceived = new Event<CommandDataReceivedHandler>();

_responseID = 1;

_bigDataHandler = new BigDataHandler();
Expand Down Expand Up @@ -224,7 +246,10 @@ public bool Connect(IPAddress[] ipAddresses, int port, int timeout = 10)
if (result)
{
_state = RECEIVE_FLAG | SEND_FLAG;
ReceiveAsync();
for (int i = 0; i < _listenerCount; i++)
{
ReceiveAsync();
}
if (SendConnect() == SendError.None)
{
_port = port;
Expand Down Expand Up @@ -320,17 +345,11 @@ private protected void Disconnect(DisconnectReason reason)
/// <summary>
/// Deserialize data.
/// </summary>
/// <param name="commandID"> command id. </param>
/// <param name="data"> The data. </param>
/// <param name="offset"> The offset. </param>
/// <param name="length"> The length. </param>
/// <param name="responseID"> The responseID. </param>
private protected unsafe void DeserializeData(uint commandID,
byte[] data,
int offset,
int length,
uint responseID)
/// <param name="deserializePacketInfo"> Information describing the deserialize packet. </param>
private protected unsafe void DeserializeData(in DeserializePacketInfo deserializePacketInfo)
{
uint commandID = deserializePacketInfo.CommandID;
uint responseID = deserializePacketInfo.ResponseID;
if (responseID != 0)
{
TaskCompletionSource<Packet> cs;
Expand All @@ -348,9 +367,9 @@ private protected unsafe void DeserializeData(uint commandID,
if (lockTaken) { _lockTaskCompletionSources.Exit(false); }
}
if (cs != null && !cs.TrySetResult(
new Packet(data, offset, length)))
new Packet(deserializePacketInfo.Data, 0, deserializePacketInfo.Length)))
{
ByteArrayPool.Return(data);
ByteArrayPool.Return(deserializePacketInfo.Data);
}
return;
}
Expand All @@ -359,16 +378,16 @@ private protected unsafe void DeserializeData(uint commandID,
case CommandID.PING:
{
PingPacket pingStruct;
fixed (byte* ptr = data)
fixed (byte* ptr = deserializePacketInfo.Data)
{
pingStruct = *(PingPacket*)(ptr + offset);
pingStruct = *(PingPacket*)ptr;
}
Ping?.Invoke(pingStruct);
break;
}
case CommandID.CONNECT:
{
data.FromBytesUnsafe(offset, out ConnectPacket connectPacket);
deserializePacketInfo.Data.FromBytesUnsafe(out ConnectPacket connectPacket);
fixed (byte* ptr = _connectChecksum)
{
if (SequenceEqual(connectPacket.Checksum, ptr, 16))
Expand All @@ -383,21 +402,28 @@ private protected unsafe void DeserializeData(uint commandID,
if (commandID <= Constants.USER_COMMAND_LIMIT &&
_dataReceivedCallbacks.TryGetValue(commandID, out ClientEventEntry cee))
{
Packet packet = new Packet(data, offset, length);
Packet packet = new Packet(deserializePacketInfo.Data, 0, deserializePacketInfo.Length);
ThreadPool.QueueUserWorkItem(
x =>
{
object res = cee._deserialize(in packet);
ByteArrayPool.Return(data);
if (res != null) { cee.Raise(this, res); }
ByteArrayPool.Return(packet.Buffer);
if (res != null)
{
for (int i = _dataReceived.Count - 1; i >= 0; --i)
{
_dataReceived[i].Invoke(this, commandID, res);
}
cee.Raise(this, res);
}
});
return;
}
break;
}
}
ByteArrayPool.Return(data);
ByteArrayPool.Return(deserializePacketInfo.Data);
}

#region Add & Remove
Expand Down
4 changes: 2 additions & 2 deletions Exomia.Network/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static class Constants
/// <summary>
/// The TCP maximum payload size.
/// </summary>
internal const ushort TCP_PAYLOAD_SIZE_MAX = ushort.MaxValue - TCP_HEADER_OFFSET - 1 - 8189;
internal const ushort TCP_PAYLOAD_SIZE_MAX = 65535 - TCP_HEADER_OFFSET - 1 - 8189;

/// <summary>
/// Size of the UDP header.
Expand All @@ -48,7 +48,7 @@ static class Constants
/// <summary>
/// The UDP maximum payload size.
/// </summary>
internal const ushort UDP_PAYLOAD_SIZE_MAX = ushort.MaxValue - UDP_HEADER_OFFSET;
internal const ushort UDP_PAYLOAD_SIZE_MAX = 65507 - UDP_HEADER_OFFSET;

/// <summary>
/// The user command limit.
Expand Down
11 changes: 11 additions & 0 deletions Exomia.Network/DataReceivedHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@

namespace Exomia.Network
{
/// <summary>
/// Called than a client received data from the server.
/// </summary>
/// <param name="client"> The client. </param>
/// <param name="commandID"> Identifier for the command. </param>
/// <param name="data"> The data. </param>
/// <returns>
/// A bool.
/// </returns>
public delegate bool CommandDataReceivedHandler(IClient client,uint commandID, object data);

/// <summary>
/// Called than a client received data from the server.
/// </summary>
Expand Down
38 changes: 38 additions & 0 deletions Exomia.Network/DeserializePacketInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#region License

// Copyright (c) 2018-2019, exomia
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.

#endregion

namespace Exomia.Network
{
/// <summary>
/// Information about the deserialize packet.
/// </summary>
struct DeserializePacketInfo
{
/// <summary>
/// Identifier for the command.
/// </summary>
public uint CommandID;

/// <summary>
/// Identifier for the response.
/// </summary>
public uint ResponseID;

/// <summary>
/// The data.
/// </summary>
public byte[] Data;

/// <summary>
/// The length.
/// </summary>
public int Length;
}
}
2 changes: 1 addition & 1 deletion Exomia.Network/Exomia.Network.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Authors>exomia</Authors>
<Description>tcp / udp client and server</Description>
<Copyright>Copyright © $([System.DateTime]::Now.Year) exomia</Copyright>
<Version>1.6.0.0</Version>
<Version>1.6.2.0</Version>
<PackageLicenseUrl>https://raw.githubusercontent.com/exomia/network/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/exomia/network</PackageProjectUrl>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
Expand Down
Loading

0 comments on commit 427e02c

Please sign in to comment.