Exemplo n.º 1
0
        public static UnpackedMessage ReadDataMessage(byte[] buffer, IMessageSerializer serializer)
        {
            var header = ReadHeader(buffer);

            if (header.MessageFormatVersion != MessageHeader.DataMessageFormatVersion)
            {
                throw new InvalidOperationException("Unexpected message format");
            }

            var    attributes = ReadAttributes(buffer, header);
            string contract   = attributes
                                .GetAttributeString(MessageAttributeTypeContract.ContractName)
                                .ExposeException("Protocol violation: message should have contract name");
            var type = serializer
                       .GetTypeByContractName(contract)
                       .ExposeException("Unsupported contract name: '{0}'", contract);

            var index = MessageHeader.FixedSize + (int)header.AttributesLength;
            var count = (int)header.ContentLength;

            using (var stream = new MemoryStream(buffer, index, count))
            {
                var instance = serializer.Deserialize(stream, type);
                return(new UnpackedMessage(header, attributes, instance, type));
            }
        }