Skip to content

Commit c60015a

Browse files
authored
Merge pull request #20 from exomia/development
Development
2 parents eadf551 + f8e1e61 commit c60015a

39 files changed

Lines changed: 753 additions & 561 deletions

Exomia.Network.UnitTest/Serialization.Tcp.UnitTest1.cs

Lines changed: 97 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -19,110 +19,120 @@ namespace Exomia.Network.UnitTest
1919
public class SerializationTcpUnitTest1
2020
{
2121
[TestMethod]
22-
public void SerializeTcpNoResponseTest()
22+
public unsafe void SerializeTcpNoResponseTest()
2323
{
2424
byte[] data = { 0b1000_0000, 0b1000_0000, 0b0000_0000, 0b1000_0000, 0b1000_0000, 0b0000_0000, 0b1000_0000 };
25-
26-
Serialization.Serialization.SerializeTcp(
27-
1337u, data, 0, data.Length, 0, EncryptionMode.None, out byte[] send, out int size);
28-
Assert.AreEqual(size, Constants.TCP_HEADER_SIZE + data.Length + 1 + 1);
29-
30-
CircularBuffer cb = new CircularBuffer();
31-
cb.Write(send, 0, size);
32-
33-
Assert.IsTrue(
34-
cb.PeekHeader(0, out byte packetHeader, out uint commandID, out int dataLength, out ushort checksum));
35-
36-
Assert.AreEqual((byte)0, packetHeader);
37-
Assert.AreEqual(1337u, commandID);
38-
Assert.AreEqual(data.Length + 1 + 1, dataLength);
39-
40-
Assert.IsTrue(cb.PeekByte((Constants.TCP_HEADER_SIZE + dataLength) - 1, out byte b));
41-
Assert.AreEqual((byte)0, b);
42-
43-
cb.Read(send, 0, dataLength, Constants.TCP_HEADER_SIZE);
44-
45-
byte[] deserializeBuffer = new byte[128];
46-
/*int checksum2 = Serialization.Serialization.Deserialize(
47-
send, 0, dataLength - 1, deserializeBuffer, out int bufferLength);
48-
49-
Assert.AreEqual(checksum, checksum2);
50-
51-
Assert.AreEqual(data.Length, bufferLength);
52-
Assert.IsTrue(data.SequenceEqual(deserializeBuffer.Take(bufferLength)));*/
25+
fixed (byte* src = data)
26+
{
27+
Serialization.Serialization.SerializeTcp(
28+
1337u, src, data.Length, 0, EncryptionMode.None, out byte[] send, out int size);
29+
Assert.AreEqual(size, Constants.TCP_HEADER_SIZE + data.Length + 1 + 1);
30+
31+
CircularBuffer cb = new CircularBuffer();
32+
cb.Write(send, 0, size);
33+
34+
Assert.IsTrue(
35+
cb.PeekHeader(
36+
0, out byte packetHeader, out uint commandID, out int dataLength, out ushort checksum));
37+
38+
Assert.AreEqual((byte)0, packetHeader);
39+
Assert.AreEqual(1337u, commandID);
40+
Assert.AreEqual(data.Length + 1 + 1, dataLength);
41+
42+
Assert.IsTrue(cb.PeekByte((Constants.TCP_HEADER_SIZE + dataLength) - 1, out byte b));
43+
Assert.AreEqual((byte)0, b);
44+
45+
cb.Read(send, 0, dataLength, Constants.TCP_HEADER_SIZE);
46+
47+
byte[] deserializeBuffer = new byte[128];
48+
/*int checksum2 = Serialization.Serialization.Deserialize(
49+
send, 0, dataLength - 1, deserializeBuffer, out int bufferLength);
50+
51+
Assert.AreEqual(checksum, checksum2);
52+
53+
Assert.AreEqual(data.Length, bufferLength);
54+
Assert.IsTrue(data.SequenceEqual(deserializeBuffer.Take(bufferLength)));*/
55+
}
5356
}
5457

5558
[TestMethod]
56-
public void SerializeTcpResponseTest()
59+
public unsafe void SerializeTcpResponseTest()
5760
{
5861
byte[] data = { 0b0000_0000, 0b0000_0000, 0b0000_0000, 0b0000_0000, 0b0000_0000, 0b0000_0000, 0b0000_0000 };
59-
60-
Serialization.Serialization.SerializeTcp(
61-
1337u, data, 0, data.Length, 654584478, EncryptionMode.None, out byte[] send,
62-
out int size);
63-
Assert.AreEqual(size, Constants.TCP_HEADER_SIZE + 4 + data.Length + 1 + 1);
64-
65-
CircularBuffer cb = new CircularBuffer();
66-
cb.Write(send, 0, size);
67-
68-
Assert.IsTrue(
69-
cb.PeekHeader(0, out byte packetHeader, out uint commandID, out int dataLength, out ushort checksum));
70-
71-
Assert.AreEqual((byte)64, packetHeader);
72-
Assert.AreEqual(1337u, commandID);
73-
Assert.AreEqual(4 + data.Length + 1 + 1, dataLength);
74-
75-
Assert.IsTrue(cb.PeekByte((Constants.TCP_HEADER_SIZE + dataLength) - 1, out byte b));
76-
Assert.AreEqual((byte)0, b);
77-
78-
cb.Read(send, 0, dataLength, Constants.TCP_HEADER_SIZE);
79-
80-
byte[] deserializeBuffer = new byte[128];
81-
/*int checksum2 = Serialization.Serialization.Deserialize(
82-
send, 4, dataLength - 5, deserializeBuffer, out int bufferLength);
83-
84-
Assert.AreEqual(checksum, checksum2);
85-
86-
Assert.AreEqual(data.Length, bufferLength);
87-
Assert.IsTrue(data.SequenceEqual(deserializeBuffer.Take(bufferLength)));*/
62+
fixed (byte* src = data)
63+
{
64+
Serialization.Serialization.SerializeTcp(
65+
1337u, src, data.Length, 654584478, EncryptionMode.None, out byte[] send,
66+
out int size);
67+
Assert.AreEqual(size, Constants.TCP_HEADER_SIZE + 4 + data.Length + 1 + 1);
68+
69+
CircularBuffer cb = new CircularBuffer();
70+
cb.Write(send, 0, size);
71+
72+
Assert.IsTrue(
73+
cb.PeekHeader(
74+
0, out byte packetHeader, out uint commandID, out int dataLength, out ushort checksum));
75+
76+
Assert.AreEqual((byte)64, packetHeader);
77+
Assert.AreEqual(1337u, commandID);
78+
Assert.AreEqual(4 + data.Length + 1 + 1, dataLength);
79+
80+
Assert.IsTrue(cb.PeekByte((Constants.TCP_HEADER_SIZE + dataLength) - 1, out byte b));
81+
Assert.AreEqual((byte)0, b);
82+
83+
cb.Read(send, 0, dataLength, Constants.TCP_HEADER_SIZE);
84+
85+
byte[] deserializeBuffer = new byte[128];
86+
/*int checksum2 = Serialization.Serialization.Deserialize(
87+
send, 4, dataLength - 5, deserializeBuffer, out int bufferLength);
88+
89+
Assert.AreEqual(checksum, checksum2);
90+
91+
Assert.AreEqual(data.Length, bufferLength);
92+
Assert.IsTrue(data.SequenceEqual(deserializeBuffer.Take(bufferLength)));*/
93+
}
8894
}
8995

9096
[TestMethod]
91-
public void SerializeTcpNoResponseBigPayloadTest()
97+
public unsafe void SerializeTcpNoResponseBigPayloadTest()
9298
{
9399
Random rnd = new Random(7576);
94100
byte[] data = new byte[(1 << 12) + 200];
95101

96102
//rnd.NextBytes(data);
97103
int expectedLength = data.Length + Math2.Ceiling(data.Length / 7.0f);
98104

99-
Serialization.Serialization.SerializeTcp(
100-
1337u, data, 0, data.Length, 0, EncryptionMode.None, out byte[] send, out int size);
101-
Assert.IsTrue(size < Constants.TCP_HEADER_SIZE + 4 + expectedLength + 1 + 1);
102-
103-
CircularBuffer cb = new CircularBuffer(data.Length);
104-
cb.Write(send, 0, size);
105-
106-
Assert.IsTrue(
107-
cb.PeekHeader(0, out byte packetHeader, out uint commandID, out int dataLength, out ushort checksum));
108-
109-
Assert.AreEqual((byte)32, packetHeader);
110-
Assert.AreEqual(1337u, commandID);
111-
Assert.IsTrue(4 + data.Length + 1 + 1 > dataLength);
112-
113-
Assert.IsTrue(cb.PeekByte((Constants.TCP_HEADER_SIZE + dataLength) - 1, out byte b));
114-
Assert.AreEqual((byte)0, b);
115-
116-
cb.Read(send, 0, dataLength, Constants.TCP_HEADER_SIZE);
117-
118-
byte[] deserializeBuffer = new byte[128];
119-
/*int checksum2 = Serialization.Serialization.Deserialize(
120-
send, 4, dataLength - 5, deserializeBuffer, out int bufferLength);
121-
122-
Assert.AreEqual(27, bufferLength);
123-
124-
Assert.AreEqual(checksum, checksum2);
125-
Assert.AreEqual(data.Length, BitConverter.ToInt32(send, 0));*/
105+
fixed (byte* src = data)
106+
{
107+
Serialization.Serialization.SerializeTcp(
108+
1337u, src, data.Length, 0, EncryptionMode.None, out byte[] send, out int size);
109+
Assert.IsTrue(size < Constants.TCP_HEADER_SIZE + 4 + expectedLength + 1 + 1);
110+
111+
CircularBuffer cb = new CircularBuffer(data.Length);
112+
cb.Write(send, 0, size);
113+
114+
Assert.IsTrue(
115+
cb.PeekHeader(
116+
0, out byte packetHeader, out uint commandID, out int dataLength, out ushort checksum));
117+
118+
Assert.AreEqual((byte)32, packetHeader);
119+
Assert.AreEqual(1337u, commandID);
120+
Assert.IsTrue(4 + data.Length + 1 + 1 > dataLength);
121+
122+
Assert.IsTrue(cb.PeekByte((Constants.TCP_HEADER_SIZE + dataLength) - 1, out byte b));
123+
Assert.AreEqual((byte)0, b);
124+
125+
cb.Read(send, 0, dataLength, Constants.TCP_HEADER_SIZE);
126+
127+
byte[] deserializeBuffer = new byte[128];
128+
/*int checksum2 = Serialization.Serialization.Deserialize(
129+
send, 4, dataLength - 5, deserializeBuffer, out int bufferLength);
130+
131+
Assert.AreEqual(27, bufferLength);
132+
133+
Assert.AreEqual(checksum, checksum2);
134+
Assert.AreEqual(data.Length, BitConverter.ToInt32(send, 0));*/
135+
}
126136
}
127137
}
128138
}

Exomia.Network/Buffers/ByteArrayPool.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,10 @@ internal static byte[] Rent(int size)
102102
/// Returns the given array.
103103
/// </summary>
104104
/// <param name="array"> The array to return. </param>
105-
/// <exception cref="ArgumentException"> Thrown when one or more arguments have unsupported or
106-
/// illegal values. </exception>
105+
/// <exception cref="ArgumentException">
106+
/// Thrown when one or more arguments have unsupported or
107+
/// illegal values.
108+
/// </exception>
107109
internal static void Return(byte[] array)
108110
{
109111
int bucketIndex = SelectBucketIndex(array.Length);

Exomia.Network/ClientActionHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
namespace Exomia.Network
1212
{
1313
/// <summary>
14-
/// ClientActionHandler callback.
14+
/// Called than a client action occurs.
1515
/// </summary>
1616
/// <typeparam name="T"> Socket|EndPoint. </typeparam>
17-
/// <param name="arg0"> . </param>
17+
/// <param name="arg0"> The socket or endpoint </param>
1818
public delegate void ClientActionHandler<in T>(T arg0) where T : class;
1919
}

Exomia.Network/ClientBase.cs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ private protected ClientBase()
168168
}
169169

170170
/// <summary>
171-
/// ClientBase destructor.
171+
/// Finalizes an instance of the <see cref="ClientBase" /> class.
172172
/// </summary>
173173
~ClientBase()
174174
{
@@ -297,8 +297,11 @@ private protected void Disconnect(DisconnectReason reason)
297297
/// <param name="offset"> The offset. </param>
298298
/// <param name="length"> The length. </param>
299299
/// <param name="responseid"> The responseid. </param>
300-
private protected unsafe void DeserializeData(uint commandid, byte[] data, int offset, int length,
301-
uint responseid)
300+
private protected unsafe void DeserializeData(uint commandid,
301+
byte[] data,
302+
int offset,
303+
int length,
304+
uint responseid)
302305
{
303306
if (responseid != 0)
304307
{
@@ -516,12 +519,15 @@ public void RemoveDataReceivedCallback(uint commandid, DataReceivedHandler callb
516519
/// <param name="data"> The data. </param>
517520
/// <param name="offset"> The offset. </param>
518521
/// <param name="length"> The length. </param>
519-
/// <param name="responseID"> Identifier for the response. </param>
522+
/// <param name="responseid"> Identifier for the response. </param>
520523
/// <returns>
521524
/// A SendError.
522525
/// </returns>
523-
private protected abstract SendError BeginSendData(uint commandid, byte[] data, int offset, int length,
524-
uint responseID);
526+
private protected abstract SendError BeginSendData(uint commandid,
527+
byte[] data,
528+
int offset,
529+
int length,
530+
uint responseid);
525531

526532
/// <inheritdoc />
527533
public SendError Send(uint commandid, byte[] data, int offset, int length)
@@ -537,24 +543,31 @@ public Task<Response<TResult>> SendR<TResult>(uint commandid, byte[] data, int o
537543
}
538544

539545
/// <inheritdoc />
540-
public Task<Response<TResult>> SendR<TResult>(uint commandid, byte[] data,
541-
int offset, int length,
546+
public Task<Response<TResult>> SendR<TResult>(uint commandid,
547+
byte[] data,
548+
int offset,
549+
int length,
542550
DeserializePacketHandler<TResult> deserialize)
543551
{
544552
return SendR(commandid, data, offset, length, deserialize, s_defaultTimeout);
545553
}
546554

547555
/// <inheritdoc />
548-
public Task<Response<TResult>> SendR<TResult>(uint commandid, byte[] data, int offset, int length,
556+
public Task<Response<TResult>> SendR<TResult>(uint commandid,
557+
byte[] data,
558+
int offset,
559+
int length,
549560
TimeSpan timeout)
550561
where TResult : unmanaged
551562
{
552563
return SendR(commandid, data, offset, length, DeserializeResponse<TResult>, timeout);
553564
}
554565

555566
/// <inheritdoc />
556-
public async Task<Response<TResult>> SendR<TResult>(uint commandid, byte[] data,
557-
int offset, int length,
567+
public async Task<Response<TResult>> SendR<TResult>(uint commandid,
568+
byte[] data,
569+
int offset,
570+
int length,
558571
DeserializePacketHandler<TResult> deserialize,
559572
TimeSpan timeout)
560573
{
@@ -677,7 +690,8 @@ public Task<Response<TResult>> SendR<T, TResult>(uint commandid, in T data)
677690
}
678691

679692
/// <inheritdoc />
680-
public Task<Response<TResult>> SendR<T, TResult>(uint commandid, in T data,
693+
public Task<Response<TResult>> SendR<T, TResult>(uint commandid,
694+
in T data,
681695
DeserializePacketHandler<TResult> deserialize)
682696
where T : unmanaged
683697
{
@@ -695,7 +709,8 @@ public Task<Response<TResult>> SendR<T, TResult>(uint commandid, in T data, Time
695709
}
696710

697711
/// <inheritdoc />
698-
public Task<Response<TResult>> SendR<T, TResult>(uint commandid, in T data,
712+
public Task<Response<TResult>> SendR<T, TResult>(uint commandid,
713+
in T data,
699714
DeserializePacketHandler<TResult> deserialize,
700715
TimeSpan timeout) where T : unmanaged
701716
{

Exomia.Network/ClientDataReceivedHandler.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,21 @@
1111
namespace Exomia.Network
1212
{
1313
/// <summary>
14-
/// ClientDataReceivedHandler callback.
14+
/// Handler, called when the server received client data.
1515
/// </summary>
1616
/// <typeparam name="T"> Socket|EndPoint. </typeparam>
17-
/// <typeparam name="TServerClient"> TServerClient. </typeparam>
18-
/// <param name="server"> IServer. </param>
19-
/// <param name="arg0"> Socket|EndPoint. </param>
20-
/// <param name="data"> object. </param>
21-
/// <param name="responseid"> responseid. </param>
22-
/// <param name="client"> TServerClient. </param>
17+
/// <typeparam name="TServerClient"> Type of the server client. </typeparam>
18+
/// <param name="server"> The server. </param>
19+
/// <param name="client"> The client. </param>
20+
/// <param name="data"> The data. </param>
21+
/// <param name="responseid"> The responseid. </param>
2322
/// <returns>
2423
/// <b>true</b> if you want to handle more data; <b>false</b> otherwise.
2524
/// </returns>
26-
public delegate bool ClientDataReceivedHandler<T, TServerClient>(ServerBase<T, TServerClient> server, T arg0,
25+
public delegate bool ClientDataReceivedHandler<T, TServerClient>(ServerBase<T, TServerClient> server,
26+
TServerClient client,
2727
object data,
28-
uint responseid,
29-
TServerClient client)
28+
uint responseid)
3029
where T : class
3130
where TServerClient : ServerClientBase<T>;
3231
}

Exomia.Network/ClientDisconnectHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
namespace Exomia.Network
1212
{
1313
/// <summary>
14-
/// ClientDisconnectHandler callback.
14+
/// Called than a client disconnects from the server.
1515
/// </summary>
1616
/// <typeparam name="T"> Socket|EndPoint. </typeparam>
1717
/// <param name="arg0"> The args. </param>

Exomia.Network/DataReceivedHandler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
namespace Exomia.Network
1212
{
1313
/// <summary>
14-
/// DataReceivedHandler callback.
14+
/// Called than a client received data from the server.
1515
/// </summary>
16-
/// <param name="client"> IClient. </param>
17-
/// <param name="data"> data. </param>
16+
/// <param name="client"> The client. </param>
17+
/// <param name="data"> The data. </param>
1818
/// <returns>
1919
/// A bool.
2020
/// </returns>

0 commit comments

Comments
 (0)