示例#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);
        }
示例#2
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);
                }
            }
        }