예제 #1
0
        public static DefaultMessage FromBuffer(IBuffer buffer,
                                                Func <IBuffer, int, int, ushort, object> messageBodyDecoder)
        {
            if (buffer.Remaining < DefaultMessageHeader.HeaderLength)
            {
                return(null);
            }

            int index = buffer.IndexOf(BitConverter.GetBytes(HeaderTag));

            if (index != -1)
            {
                buffer.Position = index;

                if (buffer.Remaining >= DefaultMessageHeader.HeaderLength)
                {
                    int messageLength = BitConverter.ToInt32(
                        buffer.ByteArray, buffer.Position + 11);
                    if (buffer.Remaining >= DefaultMessageHeader.HeaderLength + messageLength)
                    {
                        DefaultMessageHeader header = new DefaultMessageHeader();
                        header.Tag     = HeaderTag;
                        header.Version = buffer.ByteArray[buffer.Position + 4];
                        header.Type    = BitConverter.ToUInt16(
                            buffer.ByteArray, buffer.Position + 5);
                        header.SubType = BitConverter.ToUInt16(
                            buffer.ByteArray, buffer.Position + 7);
                        header.Zip       = BitConverter.ToBoolean(buffer.ByteArray, buffer.Position + 9);
                        header.Encrypted = BitConverter.ToBoolean(buffer.ByteArray, buffer.Position + 10);
                        header.Length    = messageLength;

                        object obj = messageBodyDecoder(buffer,
                                                        buffer.Position + DefaultMessageHeader.HeaderLength, messageLength, header.Type);

                        buffer.Position += (DefaultMessageHeader.HeaderLength + messageLength);
                        return(new DefaultMessage
                        {
                            Header = header,
                            Obj = obj
                        });
                    }
                }
            }
            else
            {
                buffer.Position = buffer.Limit;
            }

            return(null);
        }
예제 #2
0
        public DefaultMessage(object data, ushort type, ushort subType = 0, bool zip = false,
                              bool encrypted = false, uint tag = HeaderTag, byte version = Version)
        {
            Header = new DefaultMessageHeader()
            {
                Tag       = HeaderTag,
                Version   = version,
                Type      = type,
                SubType   = subType,
                Zip       = zip,
                Encrypted = encrypted
            };

            Obj = data;
        }