Skip to content

Commit f6427ee

Browse files
authored
Merge pull request #57 from exomia/development
v1.7.2
2 parents 967873a + 22f359f commit f6427ee

32 files changed

+329
-314
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ class UdpServer : UdpServerEapBase<UdpServerClient>
6161
}
6262

6363
/// <inheritdoc />
64-
public UdpServer(ushort maxClients, ushort maxPacketSize = 65522)
65-
: base(maxClients, maxPacketSize) { }
64+
public UdpServer(ushort maxPacketSize = 65522)
65+
: base(maxPacketSize) { }
6666
}
6767

6868
class UdpServerClient : UdpServerClientBase
@@ -74,7 +74,7 @@ class UdpServerClient : UdpServerClientBase
7474
```csharp
7575
static void Main(string[] args)
7676
{
77-
using(UdpServer server = new UdpServer(32))
77+
using(UdpServer server = new UdpServer())
7878
{
7979
server.ClientConnected += (server1, client) =>
8080
{
@@ -154,8 +154,8 @@ class TcpServer : TcpServerEapBase<TcpServerClient>
154154
return true;
155155
}
156156

157-
public TcpServer(ushort expectedMaxClient = 32, ushort maxPacketSize = 65520)
158-
: base(expectedMaxClient, maxPacketSize) { }
157+
public TcpServer(ushort maxPacketSize = 65520)
158+
: base(maxPacketSize) { }
159159
}
160160

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

examples/Example.Client/Program.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private static void Main(string[] args)
3535
#endif
3636
client.Disconnected += (c, r) => { Console.WriteLine(r + " | Disconnected"); };
3737

38-
client.AddCommand(DeserializePacketToString, 1);
38+
client.AddCommand(1, DeserializePacketToString);
3939

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

65-
private static async void SendRequestAndWaitForResponse(IClient client, string data, uint responseID)
65+
private static async void SendRequestAndWaitForResponse(IClient client, string data, ushort responseID)
6666
{
6767
byte[] response =
6868
Encoding.UTF8.GetBytes(data + "World " + string.Join(", ", Enumerable.Range(1, 1_000_000)));
6969
Response<string> result = await client.SendR(
7070
responseID, response, 0, response.Length, DeserializePacketToString, true);
71-
Console.WriteLine("GOT: {0}", result.Result);
71+
72+
Console.WriteLine("GOT({1}): {0}", result.Result, result.SendError);
7273
}
7374
}
7475
}

examples/Example.Server/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private static async void SendRequestAndWaitForResponse(IServer<ServerClient> se
5252
byte[] request = Encoding.UTF8.GetBytes("Hello ");
5353
Response<string> response = await server.SendToR(
5454
client, 1, request, 0, request.Length, DeserializePacketToString);
55-
Console.WriteLine("GOT: {0}", response.Result);
55+
Console.WriteLine("GOT({1}): {0}", response.Result, response.SendError);
5656

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

7676
protected override bool CreateServerClient(out ServerClient serverClient)
7777
{

src/Exomia.Network/BigDataHandler.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@
1818

1919
namespace Exomia.Network
2020
{
21-
/// <summary>
22-
/// A big data handler.
23-
/// </summary>
24-
/// <typeparam name="TKey"> Type of the key. </typeparam>
2521
abstract class BigDataHandler<TKey> : IDisposable where TKey : struct
2622
{
2723
private readonly Dictionary<TKey, Buffer> _bigDataBuffers;
@@ -179,7 +175,7 @@ internal override int AddBytes(int count)
179175
{
180176
int bytes = base.AddBytes(count);
181177
if (bytes == 0) { _timer.Dispose(); }
182-
if (bytes != 0) { _timer.Change(TIMER_INTERVAL, Timeout.Infinite); }
178+
else { _timer.Change(TIMER_INTERVAL, Timeout.Infinite); }
183179
return bytes;
184180
}
185181
}

src/Exomia.Network/Buffers/ByteArrayPool.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ namespace Exomia.Network.Buffers
1616
{
1717
static class ByteArrayPool
1818
{
19-
private static SpinLock s_lock;
20-
private static readonly byte[]?[][] s_buffers;
21-
private static readonly uint[] s_index;
22-
private static readonly int[] s_bufferLength;
23-
private static readonly int[] s_bufferCount;
19+
private static SpinLock s_lock;
20+
private static readonly byte[]?[]?[] s_buffers;
21+
private static readonly uint[] s_index;
22+
private static readonly int[] s_bufferLength;
23+
private static readonly int[] s_bufferCount;
2424

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

55-
if (s_buffers[bucketIndex] == null)
56-
{
57-
s_buffers[bucketIndex] = new byte[s_bufferCount[bucketIndex]][];
58-
}
55+
s_buffers[bucketIndex] ??= new byte[s_bufferCount[bucketIndex]][];
5956

60-
if (s_index[bucketIndex] < s_buffers[bucketIndex].Length)
57+
if (s_index[bucketIndex] < s_buffers[bucketIndex]!.Length)
6158
{
6259
uint index = s_index[bucketIndex]++;
63-
buffer = s_buffers[bucketIndex][index];
64-
s_buffers[bucketIndex][index] = null;
60+
buffer = s_buffers[bucketIndex]![index];
61+
s_buffers[bucketIndex]![index] = null;
6562
}
6663
return buffer ?? new byte[s_bufferLength[bucketIndex]];
6764
}
@@ -86,7 +83,7 @@ internal static void Return(byte[] array)
8683

8784
if (s_index[bucketIndex] != 0)
8885
{
89-
s_buffers[bucketIndex][--s_index[bucketIndex]] = array;
86+
s_buffers[bucketIndex]![--s_index[bucketIndex]] = array;
9087
}
9188
}
9289
finally

src/Exomia.Network/ClientBase.Send.cs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,44 +24,44 @@ namespace Exomia.Network
2424
public abstract partial class ClientBase
2525
{
2626
/// <inheritdoc />
27-
public SendError Send(uint commandOrResponseID, byte[] data, int offset, int length, bool isResponse = false)
27+
public SendError Send(ushort commandOrResponseID, byte[] data, int offset, int length, bool isResponse = false)
2828
{
2929
return BeginSend(commandOrResponseID, data, offset, length, 0, isResponse);
3030
}
3131

3232
/// <inheritdoc />
33-
public SendError Send(uint commandOrResponseID, byte[] data, bool isResponse = false)
33+
public SendError Send(ushort commandOrResponseID, byte[] data, bool isResponse = false)
3434
{
3535
return BeginSend(commandOrResponseID, data, 0, data.Length, 0, isResponse);
3636
}
3737

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

4545
/// <inheritdoc />
46-
public SendError Send(uint commandOrResponseID, ISerializable serializable, bool isResponse = false)
46+
public SendError Send(ushort commandOrResponseID, ISerializable serializable, bool isResponse = false)
4747
{
4848
byte[] dataB = serializable.Serialize(out int length);
4949
return BeginSend(commandOrResponseID, dataB, 0, length, 0, isResponse);
5050
}
5151

5252
/// <inheritdoc />
53-
public async Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
53+
public async Task<Response<TResult>> SendR<TResult>(ushort commandOrResponseID,
5454
byte[] data,
5555
int offset,
5656
int length,
5757
DeserializePacketHandler<TResult> deserialize,
5858
TimeSpan timeout,
5959
bool isResponse = false)
6060
{
61-
TaskCompletionSource<(uint requestID, Packet packet)> tcs =
62-
new TaskCompletionSource<(uint, Packet)>(TaskCreationOptions.None);
61+
TaskCompletionSource<(ushort requestID, Packet packet)> tcs =
62+
new TaskCompletionSource<(ushort, Packet)>(TaskCreationOptions.None);
6363
using CancellationTokenSource cts = new CancellationTokenSource(timeout);
64-
uint requestID;
64+
ushort requestID;
6565
bool lockTaken = false;
6666
try
6767
{
@@ -92,7 +92,7 @@ public async Task<Response<TResult>> SendR<TResult>(uint
9292
SendError sendError = BeginSend(commandOrResponseID, data, offset, length, requestID, isResponse);
9393
if (sendError == SendError.None)
9494
{
95-
(uint rID, Packet packet) = await tcs.Task;
95+
(ushort rID, Packet packet) = await tcs.Task;
9696

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

123123
/// <inheritdoc />
124-
public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID, byte[] data, bool isResponse = false)
124+
public Task<Response<TResult>> SendR<TResult>(ushort commandOrResponseID, byte[] data, bool isResponse = false)
125125
where TResult : unmanaged
126126
{
127127
return SendR(
128128
commandOrResponseID, data, 0, data.Length, DeserializeResponse<TResult>, s_defaultTimeout, isResponse);
129129
}
130130

131131
/// <inheritdoc />
132-
public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
132+
public Task<Response<TResult>> SendR<TResult>(ushort commandOrResponseID,
133133
byte[] data,
134134
int offset,
135135
int length,
@@ -141,7 +141,7 @@ public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
141141
}
142142

143143
/// <inheritdoc />
144-
public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
144+
public Task<Response<TResult>> SendR<TResult>(ushort commandOrResponseID,
145145
byte[] data,
146146
int offset,
147147
int length,
@@ -152,7 +152,7 @@ public Task<Response<TResult>> SendR<TResult>(uint
152152
}
153153

154154
/// <inheritdoc />
155-
public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
155+
public Task<Response<TResult>> SendR<TResult>(ushort commandOrResponseID,
156156
byte[] data,
157157
DeserializePacketHandler<TResult> deserialize,
158158
bool isResponse = false)
@@ -161,7 +161,7 @@ public Task<Response<TResult>> SendR<TResult>(uint
161161
}
162162

163163
/// <inheritdoc />
164-
public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
164+
public Task<Response<TResult>> SendR<TResult>(ushort commandOrResponseID,
165165
byte[] data,
166166
int offset,
167167
int length,
@@ -173,7 +173,7 @@ public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
173173
}
174174

175175
/// <inheritdoc />
176-
public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
176+
public Task<Response<TResult>> SendR<TResult>(ushort commandOrResponseID,
177177
byte[] data,
178178
TimeSpan timeout,
179179
bool isResponse = false)
@@ -183,7 +183,7 @@ public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
183183
}
184184

185185
/// <inheritdoc />
186-
public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
186+
public Task<Response<TResult>> SendR<TResult>(ushort commandOrResponseID,
187187
byte[] data,
188188
DeserializePacketHandler<TResult> deserialize,
189189
TimeSpan timeout,
@@ -193,7 +193,7 @@ public Task<Response<TResult>> SendR<TResult>(uint
193193
}
194194

195195
/// <inheritdoc />
196-
public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
196+
public Task<Response<TResult>> SendR<TResult>(ushort commandOrResponseID,
197197
ISerializable serializable,
198198
bool isResponse = false)
199199
where TResult : unmanaged
@@ -204,7 +204,7 @@ public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
204204
}
205205

206206
/// <inheritdoc />
207-
public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
207+
public Task<Response<TResult>> SendR<TResult>(ushort commandOrResponseID,
208208
ISerializable serializable,
209209
DeserializePacketHandler<TResult> deserialize,
210210
bool isResponse = false)
@@ -214,7 +214,7 @@ public Task<Response<TResult>> SendR<TResult>(uint
214214
}
215215

216216
/// <inheritdoc />
217-
public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
217+
public Task<Response<TResult>> SendR<TResult>(ushort commandOrResponseID,
218218
ISerializable serializable,
219219
TimeSpan timeout,
220220
bool isResponse = false)
@@ -225,7 +225,7 @@ public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
225225
}
226226

227227
/// <inheritdoc />
228-
public Task<Response<TResult>> SendR<TResult>(uint commandOrResponseID,
228+
public Task<Response<TResult>> SendR<TResult>(ushort commandOrResponseID,
229229
ISerializable serializable,
230230
DeserializePacketHandler<TResult> deserialize,
231231
TimeSpan timeout,
@@ -236,7 +236,7 @@ public Task<Response<TResult>> SendR<TResult>(uint
236236
}
237237

238238
/// <inheritdoc />
239-
public Task<Response<TResult>> SendR<T, TResult>(uint commandOrResponseID, in T data, bool isResponse = false)
239+
public Task<Response<TResult>> SendR<T, TResult>(ushort commandOrResponseID, in T data, bool isResponse = false)
240240
where T : unmanaged
241241
where TResult : unmanaged
242242
{
@@ -246,7 +246,7 @@ public Task<Response<TResult>> SendR<T, TResult>(uint commandOrResponseID, in T
246246
}
247247

248248
/// <inheritdoc />
249-
public Task<Response<TResult>> SendR<T, TResult>(uint commandOrResponseID,
249+
public Task<Response<TResult>> SendR<T, TResult>(ushort commandOrResponseID,
250250
in T data,
251251
DeserializePacketHandler<TResult> deserialize,
252252
bool isResponse = false)
@@ -257,7 +257,7 @@ public Task<Response<TResult>> SendR<T, TResult>(uint
257257
}
258258

259259
/// <inheritdoc />
260-
public Task<Response<TResult>> SendR<T, TResult>(uint commandOrResponseID,
260+
public Task<Response<TResult>> SendR<T, TResult>(ushort commandOrResponseID,
261261
in T data,
262262
TimeSpan timeout,
263263
bool isResponse = false)
@@ -269,7 +269,7 @@ public Task<Response<TResult>> SendR<T, TResult>(uint commandOrResponseID,
269269
}
270270

271271
/// <inheritdoc />
272-
public Task<Response<TResult>> SendR<T, TResult>(uint commandOrResponseID,
272+
public Task<Response<TResult>> SendR<T, TResult>(ushort commandOrResponseID,
273273
in T data,
274274
DeserializePacketHandler<TResult> deserialize,
275275
TimeSpan timeout,
@@ -320,11 +320,11 @@ private static TResult DeserializeResponse<TResult>(in Packet packet)
320320
/// A SendError.
321321
/// </returns>
322322
/// <exception cref="ArgumentOutOfRangeException"> Thrown when one or more arguments are outside the required range. </exception>
323-
private unsafe SendError BeginSend(uint commandOrResponseID,
323+
private unsafe SendError BeginSend(ushort commandOrResponseID,
324324
byte[] data,
325325
int offset,
326326
int length,
327-
uint requestID,
327+
ushort requestID,
328328
bool isResponse)
329329
{
330330
if (_clientSocket == null || (_state & SEND_FLAG) != SEND_FLAG) { return SendError.Invalid; }

0 commit comments

Comments
 (0)