public void ExactDoublePageSize_ShouldBeSame()
        {
            var ep                = A.Fake <EndPoint>();
            var buffer            = new BMSByte();
            var destructor        = AbstractFactory.Get <INetworkTypeFactory>().GetNew <IMessageDestructor>();
            var bufferInterpreter = AbstractFactory.Get <INetworkTypeFactory>().GetNew <IMessageBufferInterpreter>();

            buffer.AugmentSize(destructor.MaxPageLength * 2);
            buffer.PointToEnd();
            for (int i = 0; i < buffer.Size; i++)
            {
                buffer[i] = (byte)(i % byte.MaxValue);
            }
            var beforeBuffer = new BMSByte();

            beforeBuffer.Clone(buffer);
            IPagenatedMessage   pm          = destructor.BreakdownMessage(buffer);
            IMessageConstructor constructor = null;

            for (int i = 0; i < pm.Pages.Count; i++)
            {
                BMSByte pageBuffer = GetPageSection(buffer, pm, i);
                constructor = bufferInterpreter.ReconstructPacketPage(pageBuffer, ep);
            }
            Assert.IsNotNull(constructor);
            Assert.IsTrue(constructor.MessageReconstructed);
            Assert.AreEqual(beforeBuffer.Size, constructor.MessageBuffer.Size);
            for (int i = 0; i < beforeBuffer.Size; i++)
            {
                Assert.AreEqual(beforeBuffer[i], constructor.MessageBuffer[i]);
            }
        }
        public void PartialSinglePage_ShouldBeSame()
        {
            var buffer            = new BMSByte();
            var destructor        = AbstractFactory.Get <INetworkTypeFactory>().GetNew <IMessageDestructor>();
            var bufferInterpreter = AbstractFactory.Get <INetworkTypeFactory>().GetNew <IMessageBufferInterpreter>();

            buffer.AugmentSize(destructor.MaxPageLength / 2);
            buffer.PointToEnd();
            for (int i = 0; i < buffer.Size; i++)
            {
                buffer[i] = (byte)(i % byte.MaxValue);
            }
            var beforeBuffer = new BMSByte();

            beforeBuffer.Clone(buffer);
            IPagenatedMessage   pm          = destructor.BreakdownMessage(buffer);
            IMessageConstructor constructor = bufferInterpreter.ReconstructPacketPage(pm.Buffer, A.Fake <EndPoint>());

            Assert.IsTrue(constructor.MessageReconstructed);
            Assert.AreEqual(beforeBuffer.Size, constructor.MessageBuffer.Size);
            for (int i = 0; i < beforeBuffer.Size; i++)
            {
                Assert.AreEqual(beforeBuffer[i], constructor.MessageBuffer[i]);
            }
        }
        public int Receive(BMSByte buffer, ref EndPoint endpoint)
        {
            buffer.Clear();
            int length = _socket.ReceiveFrom(buffer.byteArr, 0, buffer.byteArr.Length, SocketFlags.None, ref endpoint);

            buffer.AugmentSize(length);
            return(length);
        }
        public void BelowMaxSize_ShouldBeSinglePage()
        {
            var buffer     = new BMSByte();
            var destructor = AbstractFactory.Get <INetworkTypeFactory>().GetNew <IMessageDestructor>();

            buffer.AugmentSize(destructor.MaxPageLength / 2);
            buffer.PointToEnd();
            for (int i = 0; i < buffer.Size; i++)
            {
                buffer[i] = (byte)(i % byte.MaxValue);
            }
            IPagenatedMessage pm = destructor.BreakdownMessage(buffer);

            Assert.AreEqual(1, pm.Pages.Count);
            Assert.AreEqual(buffer.Size, pm.TotalSize);
            Assert.AreEqual(buffer, pm.Buffer);
            Assert.AreEqual(0, pm.Pages[0].StartOffset);
            Assert.AreEqual(buffer.Size, pm.Pages[0].Length);
        }
        public void ExactTripplePageSizeInRandomOrder_ShouldBeSame()
        {
            var ep                = A.Fake <EndPoint>();
            var buffer            = new BMSByte();
            var destructor        = AbstractFactory.Get <INetworkTypeFactory>().GetNew <IMessageDestructor>();
            var bufferInterpreter = AbstractFactory.Get <INetworkTypeFactory>().GetNew <IMessageBufferInterpreter>();

            buffer.AugmentSize(destructor.MaxPageLength * 3);
            buffer.PointToEnd();
            for (int i = 0; i < buffer.Size; i++)
            {
                buffer[i] = (byte)(i % byte.MaxValue);
            }
            var beforeBuffer = new BMSByte();

            beforeBuffer.Clone(buffer);
            IPagenatedMessage   pm          = destructor.BreakdownMessage(buffer);
            IMessageConstructor constructor = null;

            int[] indexes = new int[pm.Pages.Count];
            for (int i = 0; i < indexes.Length; i++)
            {
                indexes[i] = i;
            }
            Random rnd = new Random();

            indexes = indexes.OrderBy(x => rnd.Next()).ToArray();
            for (int i = 0; i < indexes.Length; i++)
            {
                BMSByte pageBuffer = GetPageSection(buffer, pm, indexes[i]);
                constructor = bufferInterpreter.ReconstructPacketPage(pageBuffer, ep);
            }
            Assert.IsNotNull(constructor);
            Assert.IsTrue(constructor.MessageReconstructed);
            Assert.AreEqual(beforeBuffer.Size, constructor.MessageBuffer.Size);
            for (int i = 0; i < beforeBuffer.Size; i++)
            {
                Assert.AreEqual(beforeBuffer[i], constructor.MessageBuffer[i]);
            }
        }
        public void AboveMaxSize_ShouldBeTwoPagesButSecondIsntMax()
        {
            var buffer       = new BMSByte();
            var destructor   = AbstractFactory.Get <INetworkTypeFactory>().GetNew <IMessageDestructor>();
            int headerLength = destructor.HeaderLength;

            buffer.AugmentSize(destructor.MaxPageLength + destructor.MaxPageLength / 2);
            buffer.PointToEnd();
            for (int i = 0; i < buffer.Size; i++)
            {
                buffer[i] = (byte)(i % byte.MaxValue);
            }
            IPagenatedMessage pm = destructor.BreakdownMessage(buffer);

            Assert.AreEqual(2, pm.Pages.Count);
            Assert.AreEqual(buffer.Size, pm.TotalSize);
            Assert.AreEqual(buffer, pm.Buffer);
            Assert.AreEqual(0, pm.Pages[0].StartOffset);
            Assert.AreEqual(destructor.MaxPageLength + headerLength, pm.Pages[1].StartOffset);
            Assert.AreEqual(destructor.MaxPageLength + headerLength, pm.Pages[0].Length);
            Assert.AreEqual(destructor.MaxPageLength / 2 + headerLength, pm.Pages[1].Length);
        }
        public void RemovingAPlayer_ShouldRemoveBufferedMessages()
        {
            var ep     = A.Fake <EndPoint>();
            var player = A.Fake <INetPlayer>();

            A.CallTo(() => player.EndPoint).Returns(ep);
            var buffer            = new BMSByte();
            var destructor        = AbstractFactory.Get <INetworkTypeFactory>().GetNew <IMessageDestructor>();
            var bufferInterpreter = AbstractFactory.Get <INetworkTypeFactory>().GetNew <IMessageBufferInterpreter>();

            buffer.AugmentSize(destructor.MaxPageLength + destructor.MaxPageLength / 2);
            buffer.PointToEnd();
            for (int i = 0; i < buffer.Size; i++)
            {
                buffer[i] = (byte)(i % byte.MaxValue);
            }
            var beforeBuffer = new BMSByte();

            beforeBuffer.Clone(buffer);
            IPagenatedMessage   pm          = destructor.BreakdownMessage(buffer);
            IMessageConstructor constructor = null;

            for (int i = 0; i < pm.Pages.Count - 1; i++)
            {
                BMSByte pageBuffer = GetPageSection(buffer, pm, i);
                constructor = bufferInterpreter.ReconstructPacketPage(pageBuffer, ep);
            }
            bufferInterpreter.ClearBufferFor(player);
            Assert.IsNotNull(constructor);
            Assert.IsFalse(constructor.MessageReconstructed);

            BMSByte pbuf = GetPageSection(buffer, pm, pm.Pages.Count - 1);

            constructor = bufferInterpreter.ReconstructPacketPage(pbuf, ep);
            Assert.IsFalse(constructor.MessageReconstructed);
        }