-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added fragmentation, unconnectedMessages and a hashcash issue (#3)
* feat: Added fragmentation * Added fragmented channel * fix: Fixed various fragmentation issues * Finalized ReliableSequencedFragmented channel * Pooled pointer arrays * Fixed fragment resending * Added unit tests * Adjusted defaults and added validation to SocketConfig * Updated readme * Removed old comment * Added print when an invalid channel got a message * fix: Fixes hashcash difficulty * Updated socketConfig to use more reasonable defaults * Reduced example difficulty * feat: Added unconnected messages * Fixed compilation
- Loading branch information
Showing
31 changed files
with
1,622 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
Ruffles.Tests/Channels/ReliableSequencedChannelTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
using NUnit.Framework; | ||
using Ruffles.Channeling.Channels; | ||
using Ruffles.Memory; | ||
using Ruffles.Tests.Helpers; | ||
using Ruffles.Tests.Stubs; | ||
using System; | ||
|
||
namespace Ruffles.Tests.Channels | ||
{ | ||
[TestFixture()] | ||
public class ReliableSequencedChannelTests | ||
{ | ||
[Test()] | ||
public void TestSimpleMessage() | ||
{ | ||
Configuration.SocketConfig config = new Configuration.SocketConfig(); | ||
|
||
MemoryManager memoryManager = new MemoryManager(config); | ||
|
||
ConnectionStub clientsConnectionToServer = new ConnectionStub(); | ||
ConnectionStub serversConnectionToClient = new ConnectionStub(); | ||
|
||
ReliableSequencedChannel clientChannel = new ReliableSequencedChannel(0, clientsConnectionToServer, config, memoryManager); | ||
ReliableSequencedChannel serverChannel = new ReliableSequencedChannel(0, serversConnectionToClient, config, memoryManager); | ||
|
||
byte[] message = BufferHelper.GetRandomBuffer(1024); | ||
|
||
HeapMemory messageMemory = clientChannel.CreateOutgoingMessage(new ArraySegment<byte>(message, 0, 1024), out bool dealloc)[0]; | ||
ArraySegment<byte>? payload = serverChannel.HandleIncomingMessagePoll(new ArraySegment<byte>(messageMemory.Buffer, (int)messageMemory.VirtualOffset + 2, (int)messageMemory.VirtualCount - 2), out bool hasMore); | ||
|
||
Assert.NotNull(payload); | ||
Assert.False(hasMore); | ||
|
||
byte[] bytePayload = new byte[payload.Value.Count]; | ||
Array.Copy(payload.Value.Array, payload.Value.Offset, bytePayload, 0, payload.Value.Count); | ||
|
||
Assert.AreEqual(message, bytePayload); | ||
} | ||
|
||
[Test()] | ||
public void TestOutOfOrder() | ||
{ | ||
Configuration.SocketConfig config = new Configuration.SocketConfig(); | ||
|
||
MemoryManager memoryManager = new MemoryManager(config); | ||
|
||
ConnectionStub clientsConnectionToServer = new ConnectionStub(); | ||
ConnectionStub serversConnectionToClient = new ConnectionStub(); | ||
|
||
ReliableSequencedChannel clientChannel = new ReliableSequencedChannel(0, clientsConnectionToServer, config, memoryManager); | ||
ReliableSequencedChannel serverChannel = new ReliableSequencedChannel(0, serversConnectionToClient, config, memoryManager); | ||
|
||
// Create 3 payloads | ||
byte[] message1 = BufferHelper.GetRandomBuffer(1024); | ||
byte[] message2 = BufferHelper.GetRandomBuffer(1024); | ||
byte[] message3 = BufferHelper.GetRandomBuffer(1024); | ||
|
||
// Sequence all payloads as outgoing | ||
HeapMemory message1Memory = clientChannel.CreateOutgoingMessage(new ArraySegment<byte>(message1, 0, 1024), out bool dealloc)[0]; | ||
HeapMemory message2Memory = clientChannel.CreateOutgoingMessage(new ArraySegment<byte>(message2, 0, 1024), out dealloc)[0]; | ||
HeapMemory message3Memory = clientChannel.CreateOutgoingMessage(new ArraySegment<byte>(message3, 0, 1024), out dealloc)[0]; | ||
|
||
// Consume 1st payload | ||
ArraySegment<byte>? payload1 = serverChannel.HandleIncomingMessagePoll(new ArraySegment<byte>(message1Memory.Buffer, (int)message1Memory.VirtualOffset + 2, (int)message1Memory.VirtualCount - 2), out bool hasMore1); | ||
// Consume 3rd payload | ||
ArraySegment<byte>? payload3 = serverChannel.HandleIncomingMessagePoll(new ArraySegment<byte>(message3Memory.Buffer, (int)message3Memory.VirtualOffset + 2, (int)message3Memory.VirtualCount - 2), out bool hasMore3); | ||
// Consume 2nd payload | ||
ArraySegment<byte>? payload2 = serverChannel.HandleIncomingMessagePoll(new ArraySegment<byte>(message2Memory.Buffer, (int)message2Memory.VirtualOffset + 2, (int)message2Memory.VirtualCount - 2), out bool hasMore2); | ||
|
||
HeapMemory pollMemory = serverChannel.HandlePoll(); | ||
|
||
{ | ||
Assert.NotNull(payload1); | ||
Assert.False(hasMore1); | ||
|
||
byte[] bytePayload = new byte[payload1.Value.Count]; | ||
Array.Copy(payload1.Value.Array, payload1.Value.Offset, bytePayload, 0, payload1.Value.Count); | ||
|
||
Assert.AreEqual(message1, bytePayload); | ||
} | ||
|
||
{ | ||
Assert.Null(payload3); | ||
Assert.False(hasMore3); | ||
} | ||
|
||
{ | ||
Assert.NotNull(payload2); | ||
Assert.True(hasMore2); | ||
|
||
byte[] bytePayload = new byte[payload2.Value.Count]; | ||
Array.Copy(payload2.Value.Array, payload2.Value.Offset, bytePayload, 0, payload2.Value.Count); | ||
|
||
Assert.AreEqual(message2, bytePayload); | ||
} | ||
|
||
{ | ||
// Check for the third packet | ||
Assert.NotNull(pollMemory); | ||
|
||
byte[] bytePayload = new byte[pollMemory.VirtualCount]; | ||
Array.Copy(pollMemory.Buffer, pollMemory.VirtualOffset, bytePayload, 0, pollMemory.VirtualCount); | ||
|
||
Assert.AreEqual(message3, bytePayload); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.