public void Deserialize(SuperServiceMessage msg)
        {
            if (this.DataBuffer == null || this.DataBuffer.Length == 0)
            {
                throw new InvalidOperationException("DataBuffer is not set or empty");
            }

            if (this.DataBuffer.Length < this.BufferOffset + this.DataLength)
            {
                throw new InvalidOperationException("DataBuffer is too short for specified offset and data length");
            }

            try
            {
                var superMessageTypeInt = BitConverter.ToInt32(this.DataBuffer, this.BufferOffset);
                msg.SuperMessageType = (SuperServiceMessage.SuperServiceMessageType)superMessageTypeInt;
                var dataLen = BitConverter.ToInt32(this.DataBuffer, this.BufferOffset + 4);
                if (dataLen > 0)
                {
                    msg.DataBuffer = new byte[dataLen];
                    Buffer.BlockCopy(this.DataBuffer, this.BufferOffset + 8, msg.DataBuffer, 0, dataLen);
                }
                else
                {
                    msg.DataBuffer = null;
                }
            }
            catch (Exception ex)
            {
                throw new SerializationException("Exception during deserializing SuperServiceMessage", ex);
            }
        }
예제 #2
0
        public void SuperMessageConsistencyTest()
        {
            var original = new SuperServiceMessage()
                               {
                                   SuperMessageType =
                                       SuperServiceMessage.SuperServiceMessageType.Response,
                                   DataBuffer = new byte[] { 0xAA, 0xBB, 0xCC }
                               };

            // Serializing
            var serializer = new MessageSerializingVisitor();
            original.Accept(serializer);
            var serializedData = serializer.Result;
            Assert.NotNull(serializedData);
            Assert.True(serializedData.Length > 0);

            // Deserializing
            var deserializer = new MessageDeserializingVisitor()
                                   {
                                       DataBuffer = serializedData,
                                       DataLength = serializedData.Length,
                                       BufferOffset = 0
                                   };
            var reconstructedMessage = new SuperServiceMessage();
            reconstructedMessage.Accept(deserializer);

            Assert.AreEqual(original.SuperMessageType, reconstructedMessage.SuperMessageType);
            Assert.AreEqual(original.DataBuffer, reconstructedMessage.DataBuffer);
        }