Пример #1
0
        public void FromToBytes()
        {
            // Arrange
            var version = new ProtocolVersion(1, 0);
            var name    = "Something";

            // Act
            var header  = new SerializationHeader(version, name);
            var bytes   = header.ToBytes();
            var header2 = SerializationHeader.FromBytes(bytes);

            // Assert
            Assert.That(header2.Protocol, Is.EqualTo(version));
            Assert.That(header2.InvariantName, Is.EqualTo(name));
        }
Пример #2
0
        /// <summary>
        /// Extracts a response from the <see cref="NetMQMessage"/>
        /// </summary>
        /// <param name="message"><see cref="NetMQMessage"/> wrapping a response object</param>
        /// <returns>Request identifier, response object contained within the <see cref="NetMQMessage"/></returns>
        public (int requestId, object response) ExtractResponse(NetMQMessage message)
        {
            var requestId = message[1].ConvertToInt32();
            var data      = message[3].ToByteArray();
            var header    = SerializationHeader.FromBytes(data);

            if (!serializerCache.SerializerFor(header.InvariantName, out var serializer))
            {
                throw new MissingSerializerException(header.InvariantName);
            }

            var package  = serializer.Deserialize <Package>(data, header.EncodedLength);
            var response = packageFactory.Unpack(package);

            return(requestId, response);
        }
Пример #3
0
        /// <summary>
        /// Extracts a request from the <see cref="NetMQMessage"/>
        /// </summary>
        /// <param name="message"><see cref="NetMQMessage"/> wrapping a request object</param>
        /// <returns>Request object contained within the <see cref="NetMQMessage"/>, address of remote sender, and request identifier</returns>
        public (object request, byte[] address, int requestId, string serializationName) ExtractRequest(NetMQMessage message)
        {
            var address   = message[0].ToByteArray();
            var requestId = message[2].ConvertToInt32();

            var data   = message[4].ToByteArray();
            var header = SerializationHeader.FromBytes(data);

            if (!serializerCache.SerializerFor(header.InvariantName, out var serializer))
            {
                throw new MissingSerializerException(header.InvariantName);
            }

            var package = serializer.Deserialize <Package>(data, header.EncodedLength);
            var request = packageFactory.Unpack(package);

            return(request, address, requestId, header.InvariantName);
        }