Skip to content

Commit

Permalink
Merge pull request #57 from exomia/development
Browse files Browse the repository at this point in the history
v1.7.2
  • Loading branch information
baetz-daniel authored Sep 30, 2020
2 parents 967873a + 22f359f commit f6427ee
Show file tree
Hide file tree
Showing 32 changed files with 329 additions and 314 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ class UdpServer : UdpServerEapBase<UdpServerClient>
}

/// <inheritdoc />
public UdpServer(ushort maxClients, ushort maxPacketSize = 65522)
: base(maxClients, maxPacketSize) { }
public UdpServer(ushort maxPacketSize = 65522)
: base(maxPacketSize) { }
}

class UdpServerClient : UdpServerClientBase
Expand All @@ -74,7 +74,7 @@ class UdpServerClient : UdpServerClientBase
```csharp
static void Main(string[] args)
{
using(UdpServer server = new UdpServer(32))
using(UdpServer server = new UdpServer())
{
server.ClientConnected += (server1, client) =>
{
Expand Down Expand Up @@ -154,8 +154,8 @@ class TcpServer : TcpServerEapBase<TcpServerClient>
return true;
}

public TcpServer(ushort expectedMaxClient = 32, ushort maxPacketSize = 65520)
: base(expectedMaxClient, maxPacketSize) { }
public TcpServer(ushort maxPacketSize = 65520)
: base(maxPacketSize) { }
}

class TcpServerClient : TcpServerClientBase
Expand Down Expand Up @@ -188,7 +188,7 @@ static void Main(string[] args)
string request = (string)data;
Console.WriteLine($"Request: {request}");
byte[] buffer = Encoding.UTF8.GetBytes(DateTime.Now.ToLongDateString());
server1.SendTo(client, 45, buffer, 0, buffer.Length, responseid);
server1.SendTo(client, responseid, buffer, 0, buffer.Length, true);
return true;
});

Expand Down
7 changes: 4 additions & 3 deletions examples/Example.Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private static void Main(string[] args)
#endif
client.Disconnected += (c, r) => { Console.WriteLine(r + " | Disconnected"); };

client.AddCommand(DeserializePacketToString, 1);
client.AddCommand(1, DeserializePacketToString);

client.AddDataReceivedCallback(
1, (client1, data, responseID) =>
Expand All @@ -62,13 +62,14 @@ private static string DeserializePacketToString(in Packet packet)
return Encoding.UTF8.GetString(packet.Buffer, packet.Offset, packet.Length);
}

private static async void SendRequestAndWaitForResponse(IClient client, string data, uint responseID)
private static async void SendRequestAndWaitForResponse(IClient client, string data, ushort responseID)
{
byte[] response =
Encoding.UTF8.GetBytes(data + "World " + string.Join(", ", Enumerable.Range(1, 1_000_000)));
Response<string> result = await client.SendR(
responseID, response, 0, response.Length, DeserializePacketToString, true);
Console.WriteLine("GOT: {0}", result.Result);

Console.WriteLine("GOT({1}): {0}", result.Result, result.SendError);
}
}
}
6 changes: 3 additions & 3 deletions examples/Example.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private static async void SendRequestAndWaitForResponse(IServer<ServerClient> se
byte[] request = Encoding.UTF8.GetBytes("Hello ");
Response<string> response = await server.SendToR(
client, 1, request, 0, request.Length, DeserializePacketToString);
Console.WriteLine("GOT: {0}", response.Result);
Console.WriteLine("GOT({1}): {0}", response.Result, response.SendError);

byte[] requestResponse = Encoding.UTF8.GetBytes($"Current server time is: {DateTime.UtcNow}");
server.SendTo(client, response.ID, requestResponse, 0, requestResponse.Length, true);
Expand All @@ -70,8 +70,8 @@ class Server : TcpServerEapBase<ServerClient>
class Server : UdpServerEapBase<ServerClient>
#endif
{
public Server(ushort expectedMaxClient = 32, ushort expectedMaxPayloadSize = 512)
: base(expectedMaxClient, expectedMaxPayloadSize) { }
public Server(ushort expectedMaxPayloadSize = 512)
: base(expectedMaxPayloadSize) { }

protected override bool CreateServerClient(out ServerClient serverClient)
{
Expand Down
6 changes: 1 addition & 5 deletions src/Exomia.Network/BigDataHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@

namespace Exomia.Network
{
/// <summary>
/// A big data handler.
/// </summary>
/// <typeparam name="TKey"> Type of the key. </typeparam>
abstract class BigDataHandler<TKey> : IDisposable where TKey : struct
{
private readonly Dictionary<TKey, Buffer> _bigDataBuffers;
Expand Down Expand Up @@ -179,7 +175,7 @@ internal override int AddBytes(int count)
{
int bytes = base.AddBytes(count);
if (bytes == 0) { _timer.Dispose(); }
if (bytes != 0) { _timer.Change(TIMER_INTERVAL, Timeout.Infinite); }
else { _timer.Change(TIMER_INTERVAL, Timeout.Infinite); }
return bytes;
}
}
Expand Down
23 changes: 10 additions & 13 deletions src/Exomia.Network/Buffers/ByteArrayPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ namespace Exomia.Network.Buffers
{
static class ByteArrayPool
{
private static SpinLock s_lock;
private static readonly byte[]?[][] s_buffers;
private static readonly uint[] s_index;
private static readonly int[] s_bufferLength;
private static readonly int[] s_bufferCount;
private static SpinLock s_lock;
private static readonly byte[]?[]?[] s_buffers;
private static readonly uint[] s_index;
private static readonly int[] s_bufferLength;
private static readonly int[] s_bufferCount;

/// <summary>
/// Initializes static members of the <see cref="ByteArrayPool" /> class.
Expand Down Expand Up @@ -52,16 +52,13 @@ internal static byte[] Rent(int size)
{
s_lock.Enter(ref lockTaken);

if (s_buffers[bucketIndex] == null)
{
s_buffers[bucketIndex] = new byte[s_bufferCount[bucketIndex]][];
}
s_buffers[bucketIndex] ??= new byte[s_bufferCount[bucketIndex]][];

if (s_index[bucketIndex] < s_buffers[bucketIndex].Length)
if (s_index[bucketIndex] < s_buffers[bucketIndex]!.Length)
{
uint index = s_index[bucketIndex]++;
buffer = s_buffers[bucketIndex][index];
s_buffers[bucketIndex][index] = null;
buffer = s_buffers[bucketIndex]![index];
s_buffers[bucketIndex]![index] = null;
}
return buffer ?? new byte[s_bufferLength[bucketIndex]];
}
Expand All @@ -86,7 +83,7 @@ internal static void Return(byte[] array)

if (s_index[bucketIndex] != 0)
{
s_buffers[bucketIndex][--s_index[bucketIndex]] = array;
s_buffers[bucketIndex]![--s_index[bucketIndex]] = array;
}
}
finally
Expand Down
52 changes: 26 additions & 26 deletions src/Exomia.Network/ClientBase.Send.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,44 +24,44 @@ namespace Exomia.Network
public abstract partial class ClientBase
{
/// <inheritdoc />
public SendError Send(uint commandOrResponseID, byte[] data, int offset, int length, bool isResponse = false)
public SendError Send(ushort commandOrResponseID, byte[] data, int offset, int length, bool isResponse = false)
{
return BeginSend(commandOrResponseID, data, offset, length, 0, isResponse);
}

/// <inheritdoc />
public SendError Send(uint commandOrResponseID, byte[] data, bool isResponse = false)
public SendError Send(ushort commandOrResponseID, byte[] data, bool isResponse = false)
{
return BeginSend(commandOrResponseID, data, 0, data.Length, 0, isResponse);
}

/// <inheritdoc />
public SendError Send<T>(uint commandOrResponseID, in T data, bool isResponse = false) where T : unmanaged
public SendError Send<T>(ushort commandOrResponseID, in T data, bool isResponse = false) where T : unmanaged
{
data.ToBytesUnsafe2(out byte[] dataB, out int length);
return BeginSend(commandOrResponseID, dataB, 0, length, 0, isResponse);
}

/// <inheritdoc />
public SendError Send(uint commandOrResponseID, ISerializable serializable, bool isResponse = false)
public SendError Send(ushort commandOrResponseID, ISerializable serializable, bool isResponse = false)
{
byte[] dataB = serializable.Serialize(out int length);
return BeginSend(commandOrResponseID, dataB, 0, length, 0, isResponse);
}

/// <inheritdoc />
public async Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
public async Task<Response<TResult>> SendR<TResult>(ushort commandOrResponseID,
byte[] data,
int offset,
int length,
DeserializePacketHandler<TResult> deserialize,
TimeSpan timeout,
bool isResponse = false)
{
TaskCompletionSource<(uint requestID, Packet packet)> tcs =
new TaskCompletionSource<(uint, Packet)>(TaskCreationOptions.None);
TaskCompletionSource<(ushort requestID, Packet packet)> tcs =
new TaskCompletionSource<(ushort, Packet)>(TaskCreationOptions.None);
using CancellationTokenSource cts = new CancellationTokenSource(timeout);
uint requestID;
ushort requestID;
bool lockTaken = false;
try
{
Expand Down Expand Up @@ -92,7 +92,7 @@ public async Task<Response<TResult>> SendR<TResult>(uint
SendError sendError = BeginSend(commandOrResponseID, data, offset, length, requestID, isResponse);
if (sendError == SendError.None)
{
(uint rID, Packet packet) = await tcs.Task;
(ushort rID, Packet packet) = await tcs.Task;

// ReSharper disable once ConditionIsAlwaysTrueOrFalse
if (packet.Buffer != null)
Expand Down Expand Up @@ -121,15 +121,15 @@ public async Task<Response<TResult>> SendR<TResult>(uint
}

/// <inheritdoc />
public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID, byte[] data, bool isResponse = false)
public Task<Response<TResult>> SendR<TResult>(ushort commandOrResponseID, byte[] data, bool isResponse = false)
where TResult : unmanaged
{
return SendR(
commandOrResponseID, data, 0, data.Length, DeserializeResponse<TResult>, s_defaultTimeout, isResponse);
}

/// <inheritdoc />
public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
public Task<Response<TResult>> SendR<TResult>(ushort commandOrResponseID,
byte[] data,
int offset,
int length,
Expand All @@ -141,7 +141,7 @@ public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
}

/// <inheritdoc />
public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
public Task<Response<TResult>> SendR<TResult>(ushort commandOrResponseID,
byte[] data,
int offset,
int length,
Expand All @@ -152,7 +152,7 @@ public Task<Response<TResult>> SendR<TResult>(uint
}

/// <inheritdoc />
public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
public Task<Response<TResult>> SendR<TResult>(ushort commandOrResponseID,
byte[] data,
DeserializePacketHandler<TResult> deserialize,
bool isResponse = false)
Expand All @@ -161,7 +161,7 @@ public Task<Response<TResult>> SendR<TResult>(uint
}

/// <inheritdoc />
public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
public Task<Response<TResult>> SendR<TResult>(ushort commandOrResponseID,
byte[] data,
int offset,
int length,
Expand All @@ -173,7 +173,7 @@ public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
}

/// <inheritdoc />
public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
public Task<Response<TResult>> SendR<TResult>(ushort commandOrResponseID,
byte[] data,
TimeSpan timeout,
bool isResponse = false)
Expand All @@ -183,7 +183,7 @@ public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
}

/// <inheritdoc />
public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
public Task<Response<TResult>> SendR<TResult>(ushort commandOrResponseID,
byte[] data,
DeserializePacketHandler<TResult> deserialize,
TimeSpan timeout,
Expand All @@ -193,7 +193,7 @@ public Task<Response<TResult>> SendR<TResult>(uint
}

/// <inheritdoc />
public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
public Task<Response<TResult>> SendR<TResult>(ushort commandOrResponseID,
ISerializable serializable,
bool isResponse = false)
where TResult : unmanaged
Expand All @@ -204,7 +204,7 @@ public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
}

/// <inheritdoc />
public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
public Task<Response<TResult>> SendR<TResult>(ushort commandOrResponseID,
ISerializable serializable,
DeserializePacketHandler<TResult> deserialize,
bool isResponse = false)
Expand All @@ -214,7 +214,7 @@ public Task<Response<TResult>> SendR<TResult>(uint
}

/// <inheritdoc />
public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
public Task<Response<TResult>> SendR<TResult>(ushort commandOrResponseID,
ISerializable serializable,
TimeSpan timeout,
bool isResponse = false)
Expand All @@ -225,7 +225,7 @@ public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
}

/// <inheritdoc />
public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
public Task<Response<TResult>> SendR<TResult>(ushort commandOrResponseID,
ISerializable serializable,
DeserializePacketHandler<TResult> deserialize,
TimeSpan timeout,
Expand All @@ -236,7 +236,7 @@ public Task<Response<TResult>> SendR<TResult>(uint
}

/// <inheritdoc />
public Task<Response<TResult>> SendR<T, TResult>(uint commandOrResponseID, in T data, bool isResponse = false)
public Task<Response<TResult>> SendR<T, TResult>(ushort commandOrResponseID, in T data, bool isResponse = false)
where T : unmanaged
where TResult : unmanaged
{
Expand All @@ -246,7 +246,7 @@ public Task<Response<TResult>> SendR<T, TResult>(uint commandOrResponseID, in T
}

/// <inheritdoc />
public Task<Response<TResult>> SendR<T, TResult>(uint commandOrResponseID,
public Task<Response<TResult>> SendR<T, TResult>(ushort commandOrResponseID,
in T data,
DeserializePacketHandler<TResult> deserialize,
bool isResponse = false)
Expand All @@ -257,7 +257,7 @@ public Task<Response<TResult>> SendR<T, TResult>(uint
}

/// <inheritdoc />
public Task<Response<TResult>> SendR<T, TResult>(uint commandOrResponseID,
public Task<Response<TResult>> SendR<T, TResult>(ushort commandOrResponseID,
in T data,
TimeSpan timeout,
bool isResponse = false)
Expand All @@ -269,7 +269,7 @@ public Task<Response<TResult>> SendR<T, TResult>(uint commandOrResponseID,
}

/// <inheritdoc />
public Task<Response<TResult>> SendR<T, TResult>(uint commandOrResponseID,
public Task<Response<TResult>> SendR<T, TResult>(ushort commandOrResponseID,
in T data,
DeserializePacketHandler<TResult> deserialize,
TimeSpan timeout,
Expand Down Expand Up @@ -320,11 +320,11 @@ private static TResult DeserializeResponse<TResult>(in Packet packet)
/// A SendError.
/// </returns>
/// <exception cref="ArgumentOutOfRangeException"> Thrown when one or more arguments are outside the required range. </exception>
private unsafe SendError BeginSend(uint commandOrResponseID,
private unsafe SendError BeginSend(ushort commandOrResponseID,
byte[] data,
int offset,
int length,
uint requestID,
ushort requestID,
bool isResponse)
{
if (_clientSocket == null || (_state & SEND_FLAG) != SEND_FLAG) { return SendError.Invalid; }
Expand Down
Loading

0 comments on commit f6427ee

Please sign in to comment.