Example #1
0
        public void SendAndReceiveBuffer_ShouldBeEqual()
        {
            BMSByte msg = new BMSByte();

            msg.Append(new byte[] { 3, 6, 9 });
            BMSByte buffer = new BMSByte();

            buffer.SetArraySize(512);

            ForgeTCPSocket server = new ForgeTCPSocket();

            server.Listen(TEST_PORT, 100);

            Task backgroundThread = Task.Run(() =>
            {
                var connectedClient = server.AwaitAccept();
                connectedClient.Send(A.Fake <EndPoint>(), msg);
            });

            EndPoint       ep     = new IPEndPoint(IPAddress.Parse(CommonSocketBase.LOCAL_IPV4), 15937);
            ForgeTCPSocket client = new ForgeTCPSocket();

            client.Connect(LOCAL_IP, TEST_PORT);
            int readLength = client.Receive(buffer, ref ep);

            client.Close();
            server.Close();

            Assert.AreEqual(msg.Size, readLength);
            Assert.AreEqual(msg.Size, buffer.Size);
            for (int i = 0; i < msg.Size; ++i)
            {
                Assert.AreEqual(msg[i], buffer[i]);
            }
        }
Example #2
0
        public IPagenatedMessage BreakdownMessage(BMSByte messageBuffer)
        {
            if (messageBuffer.Size == 0)
            {
                throw new CantBreakdownEmptyMessageException();
            }
            int offset = 0;
            var pm     = AbstractFactory.Get <INetworkTypeFactory>().GetNew <IPagenatedMessage>();

            pm.Buffer = messageBuffer;

            int messageId = NextId();
            int pageCount = ((messageBuffer.Size - 1) / MaxPageLength) + 1;
            int totalSize = messageBuffer.Size + (pageCount * HeaderLength);

            messageBuffer.SetArraySize(totalSize);
            do
            {
                BMSByte header = GetHeader(messageId, pm.Pages.Count, totalSize);
                var     page   = AbstractFactory.Get <INetworkTypeFactory>().GetNew <IMessagePage>();
                page.StartOffset = offset;
                page.Length      = System.Math.Min(messageBuffer.Size - offset, MaxPageLength) + header.Size;
                messageBuffer.InsertRange(offset, header);
                pm.Pages.Add(page);
                offset += page.Length;
                BufferPool.Release(header);
            } while (offset < messageBuffer.Size);
            return(pm);
        }
        private static BMSByte GetPageSection(BMSByte buffer, IPagenatedMessage pm, int pageNumber)
        {
            var page       = pm.Pages[pageNumber];
            var pageBuffer = new BMSByte();

            pageBuffer.SetArraySize(page.Length);
            pageBuffer.BlockCopy(buffer.byteArr, page.StartOffset, page.Length);
            return(pageBuffer);
        }
Example #4
0
        protected void ReadNetwork()
        {
            EndPoint readEp = new IPEndPoint(IPAddress.Parse(CommonSocketBase.LOCAL_IPV4), 0);
            var      buffer = new BMSByte();

            buffer.SetArraySize(2048);
            try
            {
                while (true)
                {
                    CancellationSource.Token.ThrowIfCancellationRequested();
                    buffer.Clear();
                    ManagedSocket.Receive(buffer, ref readEp);
                    ProcessMessageRead(buffer, readEp);
                }
            }
            catch (OperationCanceledException) { }
        }