Example #1
0
        public void TestOutOfOrderSingleFragment()
        {
            Configuration.SocketConfig config = new Configuration.SocketConfig()
            {
                MinimumMTU = 1050
            };

            MemoryManager memoryManager = new MemoryManager(config);

            Connection clientsConnectionToServer = Connection.Stub(config, memoryManager);
            Connection serversConnectionToClient = Connection.Stub(config, memoryManager);

            ReliableSequencedFragmentedChannel clientChannel = new ReliableSequencedFragmentedChannel(0, clientsConnectionToServer, config, memoryManager);
            ReliableSequencedFragmentedChannel serverChannel = new ReliableSequencedFragmentedChannel(0, serversConnectionToClient, config, memoryManager);

            // Create 3 payloads
            byte[] message1 = BufferHelper.GetRandomBuffer(1024, 0);
            byte[] message2 = BufferHelper.GetRandomBuffer(1024, 1);
            byte[] message3 = BufferHelper.GetRandomBuffer(1024, 2);

            // Sequence all payloads as outgoing
            HeapMemory message1Memory = ((HeapMemory)clientChannel.CreateOutgoingMessage(new ArraySegment <byte>(message1, 0, 1024), out _, out bool dealloc).Pointers[0]);
            HeapMemory message2Memory = ((HeapMemory)clientChannel.CreateOutgoingMessage(new ArraySegment <byte>(message2, 0, 1024), out _, out dealloc).Pointers[0]);
            HeapMemory message3Memory = ((HeapMemory)clientChannel.CreateOutgoingMessage(new ArraySegment <byte>(message3, 0, 1024), out _, out dealloc).Pointers[0]);

            // Consume 1st payload
            ArraySegment <byte>?payload1 = serverChannel.HandleIncomingMessagePoll(new ArraySegment <byte>(message1Memory.Buffer, (int)message1Memory.VirtualOffset + 2, (int)message1Memory.VirtualCount - 2), out _, 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 _, 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 _, out bool hasMore2);

            Assert.Null(payload1);
            Assert.Null(payload2);
            Assert.Null(payload3);

            Assert.True(hasMore1);
            Assert.True(hasMore2);
            Assert.True(hasMore3);

            HeapMemory poll1 = serverChannel.HandlePoll();
            HeapMemory poll2 = serverChannel.HandlePoll();
            HeapMemory poll3 = serverChannel.HandlePoll();

            Assert.NotNull(poll1);
            Assert.NotNull(poll2);
            Assert.NotNull(poll3);

            {
                Assert.That(BufferHelper.ValidateBufferSizeAndIdentity(poll1, 0, 1024));
            }

            {
                Assert.That(BufferHelper.ValidateBufferSizeAndIdentity(poll2, 1, 1024));
            }

            {
                Assert.That(BufferHelper.ValidateBufferSizeAndIdentity(poll3, 2, 1024));
            }
        }
Example #2
0
        public void TestSimpleMessageSingleFragment()
        {
            Configuration.SocketConfig config = new Configuration.SocketConfig()
            {
                MinimumMTU = 1050
            };

            MemoryManager memoryManager = new MemoryManager(config);

            Connection clientsConnectionToServer = Connection.Stub(config, memoryManager);
            Connection serversConnectionToClient = Connection.Stub(config, memoryManager);

            ReliableSequencedFragmentedChannel clientChannel = new ReliableSequencedFragmentedChannel(0, clientsConnectionToServer, config, memoryManager);
            ReliableSequencedFragmentedChannel serverChannel = new ReliableSequencedFragmentedChannel(0, serversConnectionToClient, config, memoryManager);

            byte[] message = BufferHelper.GetRandomBuffer(1024, 0);

            HeapMemory          messageMemory = ((HeapMemory)clientChannel.CreateOutgoingMessage(new ArraySegment <byte>(message, 0, 1024), out _, out bool dealloc).Pointers[0]);
            ArraySegment <byte>?payload       = serverChannel.HandleIncomingMessagePoll(new ArraySegment <byte>(messageMemory.Buffer, (int)messageMemory.VirtualOffset + 2, (int)messageMemory.VirtualCount - 2), out _, out bool hasMore);

            Assert.Null(payload);
            Assert.True(hasMore);

            HeapMemory payloadMemory = serverChannel.HandlePoll();

            Assert.NotNull(payloadMemory);

            Assert.That(BufferHelper.ValidateBufferSizeAndIdentity(payloadMemory, 0, 1024));
        }
Example #3
0
        public void TestOutOfPacketOrderMultiFragment()
        {
            Configuration.SocketConfig config = new Configuration.SocketConfig()
            {
                MinimumMTU = 1050
            };

            MemoryManager memoryManager = new MemoryManager(config);

            Connection clientsConnectionToServer = Connection.Stub(config, memoryManager);
            Connection serversConnectionToClient = Connection.Stub(config, memoryManager);

            ReliableSequencedFragmentedChannel clientChannel = new ReliableSequencedFragmentedChannel(0, clientsConnectionToServer, config, memoryManager);
            ReliableSequencedFragmentedChannel serverChannel = new ReliableSequencedFragmentedChannel(0, serversConnectionToClient, config, memoryManager);

            // Create 3 payloads
            byte[] message1 = BufferHelper.GetRandomBuffer(1024 * 5, 0);
            byte[] message2 = BufferHelper.GetRandomBuffer(1024 * 8, 1);
            byte[] message3 = BufferHelper.GetRandomBuffer(1024 * 6, 2);

            // Sequence all payloads as outgoing
            HeapPointers message1Memory = clientChannel.CreateOutgoingMessage(new ArraySegment <byte>(message1, 0, 1024 * 5), out _, out bool dealloc);
            HeapPointers message2Memory = clientChannel.CreateOutgoingMessage(new ArraySegment <byte>(message2, 0, 1024 * 8), out _, out dealloc);
            HeapPointers message3Memory = clientChannel.CreateOutgoingMessage(new ArraySegment <byte>(message3, 0, 1024 * 6), out _, out dealloc);

            int message1MemoryLength = (int)message1Memory.VirtualCount;
            int message2MemoryLength = (int)message2Memory.VirtualCount;
            int message3MemoryLength = (int)message3Memory.VirtualCount;

            // Consume 1st payload
            bool[] hasMore1s = new bool[message1MemoryLength];
            ArraySegment <byte>?[] payload1s = new ArraySegment <byte>?[message1MemoryLength];

            for (int i = 0; i < message1MemoryLength; i++)
            {
                payload1s[i] = serverChannel.HandleIncomingMessagePoll(new ArraySegment <byte>(((HeapMemory)message1Memory.Pointers[i]).Buffer, (int)((HeapMemory)message1Memory.Pointers[i]).VirtualOffset + 2, (int)((HeapMemory)message1Memory.Pointers[i]).VirtualCount - 2), out _, out bool hasMore1);
                hasMore1s[i] = hasMore1;
            }
            // Consume 3rd payload
            bool[] hasMore3s = new bool[message3MemoryLength];
            ArraySegment <byte>?[] payload3s = new ArraySegment <byte>?[message3MemoryLength];

            for (int i = 0; i < message3MemoryLength; i++)
            {
                payload3s[i] = serverChannel.HandleIncomingMessagePoll(new ArraySegment <byte>(((HeapMemory)message3Memory.Pointers[i]).Buffer, (int)((HeapMemory)message3Memory.Pointers[i]).VirtualOffset + 2, (int)((HeapMemory)message3Memory.Pointers[i]).VirtualCount - 2), out _, out bool hasMore3);
                hasMore3s[i] = hasMore3;
            }
            // Consume 2nd payload
            bool[] hasMore2s = new bool[message2MemoryLength];
            ArraySegment <byte>?[] payload2s = new ArraySegment <byte>?[message2MemoryLength];

            for (int i = 0; i < message2MemoryLength; i++)
            {
                payload2s[i] = serverChannel.HandleIncomingMessagePoll(new ArraySegment <byte>(((HeapMemory)message2Memory.Pointers[i]).Buffer, (int)((HeapMemory)message2Memory.Pointers[i]).VirtualOffset + 2, (int)((HeapMemory)message2Memory.Pointers[i]).VirtualCount - 2), out _, out bool hasMore2);
                hasMore2s[i] = hasMore2;
            }


            for (int i = 0; i < payload1s.Length; i++)
            {
                Assert.Null(payload1s[i]);
            }

            for (int i = 0; i < payload3s.Length; i++)
            {
                Assert.Null(payload3s[i]);
            }

            for (int i = 0; i < payload2s.Length; i++)
            {
                Assert.Null(payload2s[i]);
            }

            // TODO: Assert HasMore states

            HeapMemory poll1 = serverChannel.HandlePoll();
            HeapMemory poll2 = serverChannel.HandlePoll();
            HeapMemory poll3 = serverChannel.HandlePoll();

            Assert.NotNull(poll1);
            Assert.NotNull(poll2);
            Assert.NotNull(poll3);

            {
                Assert.That(BufferHelper.ValidateBufferSizeAndIdentity(poll1, 0, 1024 * 5));
            }

            {
                Assert.That(BufferHelper.ValidateBufferSizeAndIdentity(poll2, 1, 1024 * 8));
            }

            {
                Assert.That(BufferHelper.ValidateBufferSizeAndIdentity(poll3, 2, 1024 * 6));
            }
        }