public void StructResponsesAreWrittenCorrectly(string key, string value)
        {
            var output = new MemoryStream();
            var writer = new HmBinaryMessageWriter(output, true);

            writer.BeginMessage(MessageType.Response);
            writer.BeginContent();
            writer.BeginStruct(1);
            writer.BeginItem();
            writer.WritePropertyName(key);
            writer.WriteStringValue(value);
            writer.EndItem();
            writer.EndStruct();
            writer.EndContent();
            writer.EndMessage();

            var result = BinaryUtils.FormatMemoryStream(output);

            var keyBinary = BinaryUtils.FormatByteArray(Encoding.ASCII.GetBytes(key));
            var valueBinary = BinaryUtils.FormatByteArray(Encoding.ASCII.GetBytes(value));
            var expectedContent =
                string.Format("0000010100000001{0:X8}{1}00000003{2:X8}{3}", key.Length, keyBinary, value.Length, valueBinary);

            //expectedContent.Length / 2 because this is already in byte encoding (2 chars/byte);
            var expected = "42696E01" + (expectedContent.Length / 2).ToString("X8") + expectedContent;
            result.ShouldBe(expected);
        }
        public void StringResponsesAreWrittenCorrectly()
        {
            var output = new MemoryStream();
            var writer = new HmBinaryMessageWriter(output, true);

            writer.BeginMessage(MessageType.Response);
            writer.BeginContent();
            writer.WriteStringValue("Bla");
            writer.EndContent();
            writer.EndMessage();

            var result = BinaryUtils.FormatMemoryStream(output);

            result.ShouldBe("42696E010000000B0000000300000003426C61");
        }
        public void StringResponsesAreWrittenCorrectly()
        {
            var input = new MemoryStream();
            var writer = new HmBinaryMessageWriter(input);

            writer.BeginMessage(MessageType.Request);
            writer.BeginContent();
            writer.SetMethod("newDevices");
            writer.BeginArray(1);
            writer.BeginItem();

            //parameter 1
            writer.BeginArray(2);

            writer.BeginItem();
            writer.BeginStruct(2);
            writer.BeginItem();
            writer.WritePropertyName("CHILDREN");
            var items = 3;
            writer.BeginArray(items);
            for (; items > 0; items--)
            {
                writer.BeginItem();
                writer.WriteStringValue("value " + items);
                writer.EndItem();
            }
            writer.EndArray();
            writer.EndItem();
            writer.BeginItem();
            writer.WritePropertyName("FIRMWARE");
            writer.WriteStringValue("1.505");
            writer.EndItem();
            writer.EndStruct();
            writer.EndItem();

            writer.BeginItem();
            writer.BeginStruct(3);
            writer.BeginItem();
            writer.WritePropertyName("ADDRESS");
            writer.WriteStringValue("BidCoS-RF");
            writer.EndItem();
            writer.BeginItem();
            writer.WritePropertyName("CHILDREN");
            var items2 = 5;
            writer.BeginArray(items2);
            for (; items2 > 0; items2--)
            {
                writer.BeginItem();
                writer.WriteStringValue("value " + items2);
                writer.EndItem();
            }
            writer.EndArray();
            writer.EndItem();
            writer.BeginItem();
            writer.WritePropertyName("FIRMWARE");
            writer.WriteStringValue("1.505");
            writer.EndItem();
            writer.EndStruct();
            writer.EndItem();

            //end parameter 1
            writer.EndArray();
            writer.EndItem();

            //end params
            writer.EndArray();
            writer.EndContent();
            writer.EndMessage();
            var result = BinaryUtils.FormatMemoryStream(input);

            input.Seek(0, SeekOrigin.Begin);
            var converter = new MessageConverter();
            var outputReader = new JsonMessageBuilder();
            converter.Convert(new HmBinaryMessageReader(input), outputReader);

            result.ShouldBe("42696E00000001180000000A6E6577446576696365730000000100000100000000020000010100000002000000084348494C4452454E0000010000000003000000030000000776616C75652033000000030000000776616C75652032000000030000000776616C75652031000000084649524D574152450000000300000005312E353035000001010000000300000007414444524553530000000300000009426964436F532D5246000000084348494C4452454E0000010000000005000000030000000776616C75652035000000030000000776616C75652034000000030000000776616C75652033000000030000000776616C75652032000000030000000776616C75652031000000084649524D574152450000000300000005312E353035");
        }
Exemple #4
0
        public async Task HandleRequest(IRequestContext context)
        {
            if (context.Protocol != "binary")
            {
                await _next.HandleRequest(context);
                return;
            }

            var binaryContext = (BinaryRequestContext)context;
            var incoming = binaryContext.GetRequestStream();

            //buffer for robustness.
            using (var outgoingBuffer = new MemoryStream())
            {
                var newContext = new BinaryRequestContext(incoming, outgoingBuffer);
                try
                {
                    await _next.HandleRequest(newContext);

                    var bufferArray = outgoingBuffer.ToArray();

                    if (Debugger.IsAttached)
                    {
                        Debug.WriteLine("Write response (Length={0} bytes): {1}", bufferArray.Length, Binary.Utils.Tokenize(bufferArray));
                    }

                    var responseStream = binaryContext.GetResponseStream();

                    await responseStream.WriteAsync(bufferArray, 0, bufferArray.Length);

                }
                catch (ProtocolException protocolException)
                {
                    var responseStream = binaryContext.GetResponseStream();
                    using (var errorBuffer = new MemoryStream())
                    {
                        var writer = new HmBinaryMessageWriter(errorBuffer);
                        writer.BeginMessage(MessageType.Error);
                        writer.BeginContent();
                        writer.BeginStruct(2);
                        writer.BeginItem();
                        writer.WritePropertyName("faultCode");
                        writer.WriteInt32Value(int.MinValue);
                        writer.EndItem();
                        writer.BeginItem();
                        writer.WritePropertyName("faultMessage");
                        writer.WriteStringValue(protocolException.Message);
                        writer.EndItem();
                        writer.EndStruct();
                        writer.EndContent();
                        writer.EndMessage();

#if DEBUG
                        errorBuffer.Seek(0, SeekOrigin.Begin);
                        var intBuf = new byte[4];
                        errorBuffer.Read(intBuf, 4, 4);
                        var length = HmBitConverter.ToInt32(intBuf);
                        if (errorBuffer.Length != length + 4)
                        {
                            throw new ProtocolException("Error message not correct.");
                        }
#endif

                        var errorBufferArray = errorBuffer.ToArray();
                        await responseStream.WriteAsync(errorBufferArray, 0, errorBufferArray.Length);
                    }
                }
            }

        }
Exemple #5
0
        public async Task HandleRequest(IRequestContext context)
        {
            if (context.Protocol != "binary")
            {
                await _next.HandleRequest(context);

                return;
            }

            var binaryContext = (BinaryRequestContext)context;
            var incoming      = binaryContext.GetRequestStream();

            //buffer for robustness.
            using (var outgoingBuffer = new MemoryStream())
            {
                var newContext = new BinaryRequestContext(incoming, outgoingBuffer);
                try
                {
                    await _next.HandleRequest(newContext);

                    var bufferArray = outgoingBuffer.ToArray();

                    if (Debugger.IsAttached)
                    {
                        Debug.WriteLine("Write response (Length={0} bytes): {1}", bufferArray.Length, Binary.Utils.Tokenize(bufferArray));
                    }

                    var responseStream = binaryContext.GetResponseStream();

                    await responseStream.WriteAsync(bufferArray, 0, bufferArray.Length);
                }
                catch (ProtocolException protocolException)
                {
                    var responseStream = binaryContext.GetResponseStream();
                    using (var errorBuffer = new MemoryStream())
                    {
                        var writer = new HmBinaryMessageWriter(errorBuffer);
                        writer.BeginMessage(MessageType.Error);
                        writer.BeginContent();
                        writer.BeginStruct(2);
                        writer.BeginItem();
                        writer.WritePropertyName("faultCode");
                        writer.WriteInt32Value(int.MinValue);
                        writer.EndItem();
                        writer.BeginItem();
                        writer.WritePropertyName("faultMessage");
                        writer.WriteStringValue(protocolException.Message);
                        writer.EndItem();
                        writer.EndStruct();
                        writer.EndContent();
                        writer.EndMessage();

#if DEBUG
                        errorBuffer.Seek(0, SeekOrigin.Begin);
                        var intBuf = new byte[4];
                        errorBuffer.Read(intBuf, 4, 4);
                        var length = HmBitConverter.ToInt32(intBuf);
                        if (errorBuffer.Length != length + 4)
                        {
                            throw new ProtocolException("Error message not correct.");
                        }
#endif

                        var errorBufferArray = errorBuffer.ToArray();
                        await responseStream.WriteAsync(errorBufferArray, 0, errorBufferArray.Length);
                    }
                }
            }
        }