Exemplo n.º 1
0
    public static RawMessage From(int nodeId, int type, string textMessage)
    {
        var textBytes = Converters.TextToBytes(textMessage);
        var header    = RawMessageHeader.From(nodeId, type, textBytes.Length);
        var message   = new RawMessage(header, textBytes.Length);

        message.Append(textBytes, 0, textBytes.Length);
        return(message);
    }
        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 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()));
        }
Exemplo n.º 4
0
    public void Sync()
    {
        if (!Underflow())
        {
            var content = _workBuffer.ToArray();

            if (_mode == ScanMode.ReadHeader)
            {
                _rawMessage.HeaderFrom(_workBuffer);
            }

            var messageTotalLength      = _rawMessage.RequiredMessageLength;
            var missingRawMessageLength = messageTotalLength - _rawMessage.Length;
            var contentPosition         = _workBuffer.Position;
            var availableContentLength  = _workBuffer.Length - contentPosition;

            var appendLength = Math.Min(missingRawMessageLength, availableContentLength);

            _rawMessage.Append(content, contentPosition, appendLength);

            _workBuffer.Position = contentPosition + appendLength;

            if (availableContentLength == missingRawMessageLength)
            {
                _workBuffer.Clear();
                SetMode(ScanMode.ReadHeader);
            }
            else if (availableContentLength > missingRawMessageLength)
            {
                SetMode(ScanMode.ReadHeader);
            }
            else if (availableContentLength < missingRawMessageLength)
            {
                _workBuffer.Clear();
                SetMode(ScanMode.ReuseHeader);
            }
        }
    }