Exemplo n.º 1
0
        public void TestKnownSizeWithAppend()
        {
            var buffer = new MemoryStream(1000);
            var node1  = NextNodeWith(1);
            var join   = new Join(node1);

            MessageConverters.MessageToBytes(join, buffer);
            buffer.Flip();
            var messageSize = buffer.Length;
            var message     = new RawMessage(messageSize); // known size

            message.Header(RawMessageHeader.From(node1.Id.Value, 0, messageSize));
            message.Append(buffer.ToArray(), 0, messageSize);

            Assert.Equal(node1.Id.Value, message.Header().NodeId);
            Assert.Equal(messageSize, message.Header().Length);
            Assert.Equal(join, OperationalMessage.MessageFrom(message.AsTextMessage()));
        }
        private void PrepareRawMessage(int position, long bytesToAppend)
        {
            var inboundHeader  = new RawMessageHeader(_node1.Id.Value, (short)0, (short)_buffer.Length);
            var inboundMessage = new RawMessage(_buffer.Length);

            if (position == 0)
            {
                inboundMessage.Header(inboundHeader);
            }
            inboundMessage.Append(_buffer.ToArray(), position, bytesToAppend);
            inboundMessage.CopyBytesTo(_builder.WorkBuffer());
        }
Exemplo n.º 3
0
        public void TestFromBytesWithLengthAndRequiredMessageLength()
        {
            var buffer = new MemoryStream(1000);
            var node1  = NextNodeWith(1);
            var join   = new Join(node1);

            MessageConverters.MessageToBytes(join, buffer);
            buffer.Flip();
            var messageSize  = buffer.Length;
            var messageBytes = new byte[messageSize];

            Array.Copy(buffer.ToArray(), 0, messageBytes, 0, messageSize);
            var message = new RawMessage(messageBytes);

            message.Header(RawMessageHeader.From(node1.Id.Value, 0, message.Length));

            Assert.Equal(node1.Id.Value, message.Header().NodeId);
            Assert.Equal(message.Length, message.Header().Length);
            Assert.Equal(message.Length, message.RequiredMessageLength);
            Assert.Equal(join, OperationalMessage.MessageFrom(message.AsTextMessage()));
        }
Exemplo n.º 4
0
    public static RawMessage ToRawMessage(this short sendingNodeId, Stream buffer)
    {
        buffer.Flip();
        var message = new RawMessage(buffer.Length);

        message.Put(buffer, false);
        buffer.SetLength(0); // clear

        var header = new RawMessageHeader(sendingNodeId, (short)0, (int)message.Length);

        message.Header(header);
        return(message);
    }
Exemplo n.º 5
0
        public static MemoryStream BytesFrom(string text)
        {
            var message = new RawMessage(Converters.TextToBytes(text));
            var header  = RawMessageHeader.From(1, 0, message.Length);

            message.Header(header);

            var buffer = new MemoryStream(4000);

            message.CopyBytesTo(buffer);
            buffer.Flip();

            return(buffer);
        }
Exemplo n.º 6
0
        public void TestCopyBytesTo()
        {
            var buffer = new MemoryStream(1000);
            var node1  = NextNodeWith(1);
            var join   = new Join(node1);

            MessageConverters.MessageToBytes(join, buffer);
            buffer.Flip();
            var messageSize  = buffer.Length;
            var messageBytes = new byte[messageSize];

            Array.Copy(buffer.ToArray(), 0, messageBytes, 0, messageSize);
            var message = new RawMessage(messageBytes);

            message.Header(RawMessageHeader.From(node1.Id.Value, 0, message.Length));

            buffer.Clear();
            message.CopyBytesTo(buffer); // copyBytesTo
            var text = buffer.ToArray().BytesToText(RawMessageHeader.Bytes, (int)message.Length);

            Assert.True(OperationalMessage.MessageFrom(text).IsJoin);
        }
Exemplo n.º 7
0
        public void TestCopyBytesTo()
        {
            var buffer = new MemoryStream(1000);
            var text   = "JOIN\nid=1 nm=node1 op=localhost:35745 app=localhost:35746";
            var node1  = Node.With(Id.Of(1), Name.Of("node1"), Host.Of("localhost"), 35745, 35746);
            var bytes  = Converters.TextToBytes(text);

            buffer.Write(bytes, 0, bytes.Length);
            buffer.Flip();
            var messageSize  = buffer.Length;
            var messageBytes = new byte[messageSize];

            Array.Copy(buffer.ToArray(), 0, messageBytes, 0, messageSize);
            var message = new RawMessage(messageBytes);

            message.Header(RawMessageHeader.From(node1.Id.Value, 0, message.Length));
            Assert.Equal(text, message.AsTextMessage());

            buffer.Clear();
            message.CopyBytesTo(buffer); // copyBytesTo
            var convertedText = buffer.ToArray().BytesToText(RawMessageHeader.Bytes, (int)message.Length);

            Assert.Equal(text, convertedText);
        }