示例#1
0
        private void DispatchResponse(EpoxyHeaders headers, ArraySegment <byte> payload, ArraySegment <byte> layerData)
        {
            IMessage response;

            if (headers.error_code != (int)ErrorCode.OK)
            {
                response = Message.FromError(Unmarshal <Error> .From(payload));
            }
            else
            {
                response = Message.FromPayload(Unmarshal.From(payload));
            }

            var receiveContext = new EpoxyReceiveContext(this);

            IBonded bondedLayerData = (layerData.Array == null) ? null : Unmarshal.From(layerData);

            Error layerError = LayerStackUtils.ProcessOnReceive(parentTransport.LayerStack, MessageType.Response, receiveContext, bondedLayerData);

            if (layerError != null)
            {
                Log.Error("{0}.{1}: Receiving response {2}/{3} failed due to layer error (Code: {4}, Message: {5}).",
                          this, nameof(DispatchResponse), headers.conversation_id, headers.method_name,
                          layerError.error_code, layerError.message);
                response = Message.FromError(layerError);
            }

            if (!responseMap.Complete(headers.conversation_id, response))
            {
                Log.Error("{0}.{1}: Response for unmatched request. Conversation ID: {2}",
                          this, nameof(DispatchResponse), headers.conversation_id);
            }
        }
示例#2
0
        private void FlushReceivingQueue()
        {
            if (this.mReceivingSemaphore.WaitOne(0) == true)
            {
                // handle required packets
                while (this.mPacketizer.PacketCount > 0 && this.mPacketReceiveCallback != null)
                {
                    try
                    {
                        // unmarshal the packet
                        PyDataType packet = Unmarshal.ReadFromByteArray(this.mPacketizer.PopItem());
#if DEBUG
                        this.mPacketLog.Trace(PrettyPrinter.FromDataType(packet));
#endif
                        // and invoke the callback for the packet handling if it is present
                        this.mPacketReceiveCallback.Invoke(packet);
                    }
                    catch (Exception e)
                    {
                        this.HandleException(e);
                    }
                }

                // finally free the receiving semaphore
                this.mReceivingSemaphore.Release();
            }

            // semaphore not acquired, there's something already sending data, so we're sure the data will get there eventually
        }
示例#3
0
文件: program.cs 项目: zsybupt/bond
        static void Main()
        {
            // Define root node for a tree of strings
            var root = new Node <string> {
                data = "root"
            };

            root.left = new Node <string> {
                data = "root/left"
            };
            root.right = new Node <string> {
                data = "root/right"
            };
            root.left.left = new Node <string> {
                data = "root/left/left"
            };
            root.left.left.right = new Node <string> {
                data = "root/left/left/right"
            };

            var output = new OutputBuffer();
            var writer = new CompactBinaryWriter <OutputBuffer>(output);

            Marshal.To(writer, root);

            var tree = Unmarshal <Node <string> > .From(output.Data);

            Debug.Assert(Comparer.Equal(root, tree));
        }
示例#4
0
 public PySubStream(byte[] data)
     : base(PyObjectType.SubStream)
 {
     RawData = data;
     DataUnmarshal = new Unmarshal();
     Data = DataUnmarshal.Process(data);
 }
 public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
 {
     if (op == MarshalOpcode.RealZero)
         Value = 0.0d;
     else
         Value = source.ReadDouble();
 }
示例#6
0
        public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
        {
            if (op == MarshalOpcode.ObjectEx2)
                IsType2 = true;

            Dictionary = new Dictionary<PyObject, PyObject>();
            List = new List<PyObject>();
            Header = context.ReadObject(source);

            while (source.BaseStream.Position < source.BaseStream.Length)
            {
                var b = source.ReadByte();
                if (b == PackedTerminator)
                    break;
                source.BaseStream.Seek(-1, SeekOrigin.Current);
                List.Add(context.ReadObject(source));
            }

            while (source.BaseStream.Position < source.BaseStream.Length)
            {
                var b = source.ReadByte();
                if (b == PackedTerminator)
                    break;
                source.BaseStream.Seek(-1, SeekOrigin.Current);
                var key = context.ReadObject(source);
                var value = context.ReadObject(source);
                Dictionary.Add(key, value);
            }
        }
示例#7
0
 public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
 {
     uint len = source.ReadSizeEx();
     RawData = source.ReadBytes((int) len);
     DataUnmarshal = new Unmarshal();
     Data = DataUnmarshal.Process(RawData);
 }
示例#8
0
        private void DispatchEvent(EpoxyHeaders headers, ArraySegment <byte> payload, ArraySegment <byte> layerData)
        {
            if (headers.error_code != (int)ErrorCode.OK)
            {
                Log.Error("{0}.{1}: Received event with a non-zero error code. Conversation ID: {2}",
                          this, nameof(DispatchEvent), headers.conversation_id);
                return;
            }

            IMessage request = Message.FromPayload(Unmarshal.From(payload));

            var receiveContext = new EpoxyReceiveContext(this);

            IBonded bondedLayerData = (layerData.Array == null) ? null : Unmarshal.From(layerData);

            Error layerError = LayerStackUtils.ProcessOnReceive(parentTransport.LayerStack, MessageType.Event, receiveContext, bondedLayerData);

            if (layerError != null)
            {
                Log.Error("{0}.{1}: Receiving event {2}/{3} failed due to layer error (Code: {4}, Message: {5}).",
                          this, nameof(DispatchEvent), headers.conversation_id, headers.method_name,
                          layerError.error_code, layerError.message);
                return;
            }

            Task.Run(async() =>
            {
                await serviceHost.DispatchEvent(headers.method_name, receiveContext, request, connectionMetrics);
            });
        }
        static void Subscriber()
        {
            var ctx        = new ZContext();
            var subscriber = new ZSocket(ctx, ZSocketType.SUB);

            subscriber.Connect("tcp://127.0.0.1:12345");
            subscriber.SubscribeAll();

            for (;;)
            {
                var received = subscriber.ReceiveFrame();
                // INCORRECT
                // var str = received.ReadString();
                // var byteArr = Encoding.ASCII.GetBytes(str);
                var byteArr = received.Read();
                var arrSeg  = new ArraySegment <byte>(byteArr);   // There's an InputBuffer ctor that takes a byte[] directly
                var input   = new InputBuffer(arrSeg);
                var dst     = Unmarshal <Record> .From(input);

                foreach (var kvp in dst.payload)
                {
                    Console.WriteLine("{0} {1}", kvp.Key, kvp.Value);
                }
            }
        }
示例#10
0
        private State?DispatchRequest(EpoxyHeaders headers, EpoxyProtocol.MessageData messageData, ArraySegment <byte> layerData)
        {
            Task.Run(async() =>
            {
                var totalTime      = Stopwatch.StartNew();
                var requestMetrics = Metrics.StartRequestMetrics(ConnectionMetrics);
                var receiveContext = new EpoxyReceiveContext(this, ConnectionMetrics, requestMetrics);

                ILayerStack layerStack = null;

                IMessage result;

                if (messageData.IsError)
                {
                    logger.Site().Error("{0} Received request with an error message. Only payload messages are allowed. Conversation ID: {1}",
                                        this, headers.conversation_id);
                    result = Message.FromError(new Error
                    {
                        error_code = (int)ErrorCode.INVALID_INVOCATION,
                        message    = "Received request with an error message"
                    });
                }
                else
                {
                    IMessage request        = Message.FromPayload(Unmarshal.From(messageData.Data));
                    IBonded bondedLayerData = (layerData.Array == null) ? null : Unmarshal.From(layerData);

                    Error layerError = parentTransport.GetLayerStack(requestMetrics.request_id, out layerStack);

                    if (layerError == null)
                    {
                        layerError = LayerStackUtils.ProcessOnReceive(
                            layerStack, MessageType.REQUEST, receiveContext, bondedLayerData, logger);
                    }

                    if (layerError == null)
                    {
                        result = await serviceHost.DispatchRequest(headers.service_name, headers.method_name, receiveContext, request);
                    }
                    else
                    {
                        logger.Site().Error("{0} Receiving request {1}/{2}.{3} failed due to layer error (Code: {4}, Message: {5}).",
                                            this, headers.conversation_id, headers.service_name, headers.method_name,
                                            layerError.error_code, layerError.message);

                        // Set layer error as result of this Bond method call and do not dispatch to method.
                        // Since this error will be returned to client, cleanse out internal server error details, if any.
                        result = Message.FromError(Errors.CleanseInternalServerError(layerError));
                    }
                }

                await SendReplyAsync(headers.conversation_id, result, layerStack, requestMetrics);
                Metrics.FinishRequestMetrics(requestMetrics, totalTime);
                metrics.Emit(requestMetrics);
            });

            // no state change needed
            return(null);
        }
示例#11
0
        static void Main()
        {
            var obj = new Struct
            {
                n     = 0x1000,
                str   = "test",
                items = { 3.14, 0 }
            };

            // Protocols may have different versions with different features.
            // When serializing/deserializing the same version needs to be used.
            //
            // Marshaling can be used to embed the protocol and version in the
            // payload so the reading side can automatically determine which
            // protocol and version to use.
            {
                // Here, we use Compact Binary v1.
                var output = new OutputBuffer();
                var writer = new CompactBinaryWriter <OutputBuffer>(output, version: 1);
                Serialize.To(writer, obj);

                var input  = new InputBuffer(output.Data);
                var reader = new CompactBinaryReader <InputBuffer>(input, version: 1);

                var obj2 = Deserialize <Struct> .From(reader);

                ThrowIfFalse(Comparer.Equal(obj, obj2));
            }

            {
                // Here, we use Compact Binary v2.
                var output = new OutputBuffer();
                var writer = new CompactBinaryWriter <OutputBuffer>(output, version: 2);
                Serialize.To(writer, obj);

                var input  = new InputBuffer(output.Data);
                var reader = new CompactBinaryReader <InputBuffer>(input, version: 2);

                var obj2 = Deserialize <Struct> .From(reader);

                ThrowIfFalse(Comparer.Equal(obj, obj2));
            }

            {
                // Here, we Marshal to Compact Binary v2.
                var output = new OutputBuffer();
                var writer = new CompactBinaryWriter <OutputBuffer>(output, version: 2);
                Marshal.To(writer, obj);

                var input = new InputBuffer(output.Data);
                // The protocol and version are determined from the payload
                // itself.
                var obj2 = Unmarshal <Struct> .From(input);

                ThrowIfFalse(Comparer.Equal(obj, obj2));
            }
        }
        public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
        {
            var nameObject = context.ReadObject(source);
            if (nameObject.Type != PyObjectType.String)
                throw new DataException("Expected PyString");
            Name = (nameObject as PyString).Value;

            Arguments = context.ReadObject(source);
        }
示例#13
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (txtInput.Text.Length == 0)
            {
                txtOutput.Text = "";
                return;
            }
            string hex = txtInput.Text.Replace(" ", "").Replace(System.Environment.NewLine, "");

            try
            {
                //txtOutput.Text = hex;
                byte[] raw = new Byte[hex.Length / 2];
                for (int i = 0; i < raw.Length; i++)
                {
                    raw[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
                }
                Console.WriteLine(raw.Length + ", " + hex.Length);
                Unmarshal un = new Unmarshal();
                //un.DebugMode = true;
                //PyObject obj = un.Process(BinaryReader.);
                PyRep         obj = un.Process(raw);
                PrettyPrinter pp  = new PrettyPrinter();
                txtOutput.Text = pp.Print(obj);
            }
            catch
            {
                hex = txtInput.Text.Substring(8, txtInput.Text.Length - 8).Replace(" ", "").Replace(System.Environment.NewLine, "");
                byte[] raw = new Byte[hex.Length / 2];
                for (int i = 0; i < raw.Length; i++)
                {
                    raw[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
                }
                Console.WriteLine(raw.Length + ", " + hex.Length);
                Unmarshal un = new Unmarshal();
                un.DebugMode = true;
                // un.DebugMode = true;
                //PyObject obj = un.Process(BinaryReader.);
                PyRep         obj = un.Process(raw);
                PrettyPrinter pp  = new PrettyPrinter();
                txtOutput.Text = pp.Print(obj);
            }

            /*
             * //txtOutput.Text = hex;
             * byte[] raw = new Byte[hex.Length / 2];
             * for (int i = 0; i < raw.Length; i++) {
             *  raw[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
             * }
             * Console.WriteLine(raw.Length+", "+hex.Length);
             * Unmarshal un = new Unmarshal();
             * // un.DebugMode = true;
             * //PyObject obj = un.Process(BinaryReader.);
             * PyObject obj = un.Process(raw);
             * txtOutput.Text = PrettyPrinter.Print(obj);
             */
        }
示例#14
0
        public void DecimalUnmarshal_Value()
        {
            PyDataType value = Unmarshal.ReadFromByteArray(sDecimal_ValueBuffer, false);

            Assert.IsInstanceOf <PyDecimal>(value);

            PyDecimal @decimal = value as PyDecimal;

            Assert.AreEqual(sDecimal_Value, @decimal.Value);
        }
示例#15
0
        public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
        {
            context.NeedObjectEx = true;
            Header = context.ReadObject(source);
            context.NeedObjectEx = false;
            RawData = LoadZeroCompressed(source);

            if (!ParseRowData(context, source))
                 throw new InvalidDataException("Could not fully unpack PackedRow, stream integrity is broken");
        }
示例#16
0
        public void TupleUnmarshal_Empty()
        {
            PyDataType value = Unmarshal.ReadFromByteArray(sTupleMarshaling_Empty, false);

            Assert.IsInstanceOf <PyTuple>(value);

            PyTuple tuple = value as PyTuple;

            Assert.AreEqual(0, tuple.Count);
        }
示例#17
0
        public void BooleanUnmarshal_False()
        {
            PyDataType result = Unmarshal.ReadFromByteArray(sBooleanMarshal_FalseValueBuffer, false);

            Assert.IsInstanceOf <PyBool>(result);

            PyBool pyBool = result as PyBool;

            Assert.AreEqual(sBooleanMarshal_FalseValue, pyBool.Value);
        }
示例#18
0
        public void ListUnmarshal_Empty()
        {
            PyDataType value = Unmarshal.ReadFromByteArray(sListMarshaling_Empty, false);

            Assert.IsInstanceOf <PyList>(value);

            PyList list = value as PyList;

            Assert.AreEqual(0, list.Count);
        }
示例#19
0
        public void TokenUmarshal_Normal()
        {
            PyDataType result = Unmarshal.ReadFromByteArray(sTokenMarshal_NormalBuffer, false);

            Assert.IsInstanceOf <PyToken>(result);

            PyToken pyToken = result as PyToken;

            Assert.AreEqual(sTokenMarshal_Normal.Length, pyToken.Length);
            Assert.AreEqual(sTokenMarshal_Normal, pyToken.Token);
        }
示例#20
0
 public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
 {
     var entries = source.ReadSizeEx();
     Dictionary = new Dictionary<PyObject, PyObject>((int)entries);
     for (uint i = 0; i < entries; i++)
     {
         var value = context.ReadObject(source);
         var key = context.ReadObject(source);
         Dictionary.Add(key, value);
     }
 }
示例#21
0
        public void IntegerUnmarshal_Long2()
        {
            PyDataType result = Unmarshal.ReadFromByteArray(sIntegerMarshal_LongValue2Buffer, false);

            Assert.IsInstanceOf <PyInteger>(result);

            PyInteger pyInteger = result as PyInteger;

            Assert.AreEqual(sIntegerMarshal_LongValue2, pyInteger.Value);
            Assert.AreEqual(true, sIntegerMarshal_LongValue2 == pyInteger);
            Assert.AreEqual(PyInteger.IntegerTypeEnum.Long, pyInteger.IntegerType);
        }
        IBonded CreateBondedTestData(string value)
        {
            var realLayerData = new Dummy {
                string_value = value
            };

            var outputBuffer  = new OutputBuffer(20);
            var compactWriter = new CompactBinaryWriter <OutputBuffer>(outputBuffer);

            Marshal.To <CompactBinaryWriter <OutputBuffer>, Dummy>(compactWriter, realLayerData);
            return(Unmarshal.From(outputBuffer.Data));
        }
示例#23
0
        public void TupleUnmarshal_One()
        {
            PyDataType value = Unmarshal.ReadFromByteArray(sTupleMarshaling_One, false);

            Assert.IsInstanceOf <PyTuple>(value);

            PyTuple tuple = value as PyTuple;

            Assert.AreEqual(1, tuple.Count);
            Assert.IsInstanceOf <PyInteger>(tuple[0]);
            Assert.AreEqual(sTupleMarshaling_FirstValue, (tuple[0] as PyInteger).Value);
        }
示例#24
0
        public void ListUnmarshal_One()
        {
            PyDataType value = Unmarshal.ReadFromByteArray(sListMarshaling_One, false);

            Assert.IsInstanceOf <PyList>(value);

            PyList list = value as PyList;

            Assert.AreEqual(1, list.Count);
            Assert.IsInstanceOf <PyInteger>(list[0]);
            Assert.AreEqual(sListMarshaling_FirstValue, (list[0] as PyInteger).Value);
        }
示例#25
0
        private State?DispatchRequest(EpoxyHeaders headers, ArraySegment <byte> payload, ArraySegment <byte> layerData)
        {
            if (headers.error_code != (int)ErrorCode.OK)
            {
                logger.Site().Error("{0} Received request with a non-zero error code. Conversation ID: {1}",
                                    this, headers.conversation_id);
                protocolError = ProtocolErrorCode.PROTOCOL_VIOLATED;
                return(State.SendProtocolError);
            }

            Task.Run(async() =>
            {
                var totalTime      = Stopwatch.StartNew();
                var requestMetrics = Metrics.StartRequestMetrics(ConnectionMetrics);
                var receiveContext = new EpoxyReceiveContext(this, ConnectionMetrics, requestMetrics);

                IMessage request        = Message.FromPayload(Unmarshal.From(payload));
                IBonded bondedLayerData = (layerData.Array == null) ? null : Unmarshal.From(layerData);

                ILayerStack layerStack;
                Error layerError = parentTransport.GetLayerStack(requestMetrics.request_id, out layerStack);

                if (layerError == null)
                {
                    layerError = LayerStackUtils.ProcessOnReceive(
                        layerStack, MessageType.Request, receiveContext, bondedLayerData, logger);
                }

                IMessage result;

                if (layerError == null)
                {
                    result = await serviceHost.DispatchRequest(headers.method_name, receiveContext, request);
                }
                else
                {
                    logger.Site().Error("{0} Receiving request {1}/{2} failed due to layer error (Code: {3}, Message: {4}).",
                                        this, headers.conversation_id, headers.method_name,
                                        layerError.error_code, layerError.message);

                    // Set layer error as result of this Bond method call and do not dispatch to method.
                    // Since this error will be returned to client, cleanse out internal server error details, if any.
                    result = Message.FromError(Errors.CleanseInternalServerError(layerError));
                }

                await SendReplyAsync(headers.conversation_id, result, layerStack, requestMetrics);
                Metrics.FinishRequestMetrics(requestMetrics, totalTime);
                metrics.Emit(requestMetrics);
            });

            // no state change needed
            return(null);
        }
        public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
        {
            switch (op)
            {
                case MarshalOpcode.BoolTrue:
                    Value = true;
                    break;

                case MarshalOpcode.BoolFalse:
                    Value = false;
                    break;
            }
        }
示例#27
0
        public void StringUnmarshal_LongMultiByteString()
        {
            PyDataType result = Unmarshal.ReadFromByteArray(sStringMarshal_LongMultiByteStringBuffer, false);

            Assert.IsInstanceOf <PyString>(result);

            PyString pyString = result as PyString;

            Assert.AreEqual(sStringMarshal_LongString.Length, pyString.Length);
            Assert.AreEqual(sStringMarshal_LongString, pyString.Value);
            Assert.AreEqual(true, pyString.IsUTF8);
            Assert.AreEqual(false, pyString.IsStringTableEntry);
        }
示例#28
0
        void DispatchEvent(EpoxyHeaders headers, EpoxyProtocol.MessageData messageData, ArraySegment <byte> layerData)
        {
            if (messageData.IsError)
            {
                logger.Site().Error(
                    "{0} Received event with an error message. Only payload messages are allowed. Conversation ID: {1}",
                    this,
                    headers.conversation_id);
                return;
            }

            Task.Run(
                async() =>
            {
                IMessage request   = Message.FromPayload(Unmarshal.From(messageData.Data));
                var totalTime      = Stopwatch.StartNew();
                var requestMetrics = Metrics.StartRequestMetrics(ConnectionMetrics);
                var receiveContext = new RelayEpoxyReceiveContext(this, ConnectionMetrics, requestMetrics);

                IBonded bondedLayerData = (layerData.Array == null) ? null : Unmarshal.From(layerData);
                ILayerStack layerStack;
                Error layerError = parentTransport.GetLayerStack(requestMetrics.request_id, out layerStack);

                if (layerError == null)
                {
                    layerError = LayerStackUtils.ProcessOnReceive(
                        layerStack,
                        MessageType.EVENT,
                        receiveContext,
                        bondedLayerData,
                        logger);
                }

                if (layerError != null)
                {
                    logger.Site().Error(
                        "{0}: Receiving event {1}/{2}.{3} failed due to layer error (Code: {4}, Message: {5}).",
                        this,
                        headers.conversation_id,
                        headers.service_name,
                        headers.method_name,
                        layerError.error_code,
                        layerError.message);
                    return;
                }

                await serviceHost.DispatchEvent(headers.service_name, headers.method_name, receiveContext, request);
                Metrics.FinishRequestMetrics(requestMetrics, totalTime);
                metrics.Emit(requestMetrics);
            });
        }
示例#29
0
        public void StringUnmarshal_StringTableString()
        {
            PyDataType result = Unmarshal.ReadFromByteArray(sStringMarshal_StringTableBuffer, false);

            Assert.IsInstanceOf <PyString>(result);

            PyString pyString = result as PyString;

            Assert.AreEqual(sStringMarshal_StringTable.Length, pyString.Length);
            Assert.AreEqual(sStringMarshal_StringTable, pyString.Value);
            Assert.AreEqual(StringTableUtils.EntryList.contraband, pyString.StringTableEntryIndex);
            Assert.AreEqual(false, pyString.IsUTF8);
            Assert.AreEqual(true, pyString.IsStringTableEntry);
        }
示例#30
0
 public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
 {
     if (op == MarshalOpcode.IntegerOne)
         Value = 1;
     else if (op == MarshalOpcode.IntegerZero)
         Value = 0;
     else if (op == MarshalOpcode.IntegerMinusOne)
         Value = -1;
     else if (op == MarshalOpcode.IntegerByte)
         Value = source.ReadByte();
     else if (op == MarshalOpcode.IntegerSignedShort)
         Value = source.ReadInt16();
     else if (op == MarshalOpcode.IntegerLong)
         Value = source.ReadInt32();
 }
示例#31
0
        public void TupleUnmarshal_Big()
        {
            PyDataType value = Unmarshal.ReadFromByteArray(sTupleMarshaling_Big, false);

            Assert.IsInstanceOf <PyTuple>(value);

            PyTuple tuple = value as PyTuple;

            Assert.AreEqual(256, tuple.Count);

            foreach (PyDataType entry in tuple)
            {
                Assert.AreEqual(null, entry);
            }
        }
示例#32
0
        public void ListUnmarshal_Big()
        {
            PyDataType value = Unmarshal.ReadFromByteArray(sListMarshaling_Big, false);

            Assert.IsInstanceOf <PyList>(value);

            PyList list = value as PyList;

            Assert.AreEqual(256, list.Count);

            foreach (PyDataType entry in list)
            {
                Assert.AreEqual(null, entry);
            }
        }
示例#33
0
        void DispatchResponse(EpoxyHeaders headers, EpoxyProtocol.MessageData messageData, ArraySegment <byte> layerData)
        {
            IMessage response = messageData.IsError
                ? Message.FromError(Unmarshal <Error> .From(messageData.Data))
                : Message.FromPayload(Unmarshal.From(messageData.Data));

            TaskCompletionSource <IMessage> tcs = responseMap.TakeTaskCompletionSource(headers.conversation_id);

            if (tcs == null)
            {
                logger.Site().Error(
                    "{0} Response for unmatched request. Conversation ID: {1}",
                    this,
                    headers.conversation_id);
                return;
            }

            Task.Run(
                () =>
            {
                var totalTime      = Stopwatch.StartNew();
                var requestMetrics = Metrics.StartRequestMetrics(ConnectionMetrics);
                var receiveContext = new RelayEpoxyReceiveContext(this, ConnectionMetrics, requestMetrics);

                IBonded bondedLayerData = (layerData.Array == null) ? null : Unmarshal.From(layerData);

                ILayerStack layerStack = tcs.Task.AsyncState as ILayerStack;

                Error layerError = LayerStackUtils.ProcessOnReceive(layerStack, MessageType.RESPONSE, receiveContext, bondedLayerData, logger);

                if (layerError != null)
                {
                    logger.Site().Error(
                        "{0} Receiving response {1}/{2}.{3} failed due to layer error (Code: {4}, Message: {5}).",
                        this,
                        headers.conversation_id,
                        headers.service_name,
                        headers.method_name,
                        layerError.error_code,
                        layerError.message);
                    response = Message.FromError(layerError);
                }

                tcs.SetResult(response);
                Metrics.FinishRequestMetrics(requestMetrics, totalTime);
                metrics.Emit(requestMetrics);
            });
        }
示例#34
0
        public void TupleUnmarshal_Three()
        {
            PyDataType value = Unmarshal.ReadFromByteArray(sTupleMarshaling_Three, false);

            Assert.IsInstanceOf <PyTuple>(value);

            PyTuple tuple = value as PyTuple;

            Assert.AreEqual(3, tuple.Count);
            Assert.IsInstanceOf <PyInteger>(tuple[0]);
            Assert.AreEqual(sTupleMarshaling_FirstValue, (tuple[0] as PyInteger).Value);
            Assert.IsInstanceOf <PyString>(tuple[1]);
            Assert.AreEqual(sTupleMarshaling_SecondValue, (tuple[1] as PyString).Value);
            Assert.IsInstanceOf <PyDecimal>(tuple[2]);
            Assert.AreEqual(sTupleMarshaling_ThirdValue, (tuple[2] as PyDecimal).Value);
        }
示例#35
0
文件: program.cs 项目: zsybupt/bond
        static void Main()
        {
            var v1 = new Struct_v1();

            // Struct_v1 has a required field foo which by default is set to
            // 'nothing'. If we try to serialize object v1 w/o initializing
            // the field to some value Bond will throw an exception.
            try
            {
                Console.WriteLine("Serializing v1... ");
                Marshal(v1);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            // Initialize field by assigning a value to it...
            v1.foo = 10;

            // ... or for complex fields create a new instance of the
            // appropriate C# type.
            v1.baz = new LinkedList <string>();
            v1.baz.AddLast("test1");
            v1.baz.AddLast("test2");

            // We can also set a field to 'nothing' by assigning null to the
            // field itself. Optional fields that are set to 'nothing' are
            // omitted when object is serialized.
            v1.baz = null;

            ArraySegment <byte> buffer = Marshal(v1);

            // Deserialize the payload into object of type Struct_v2
            Struct_v2 v2 = Unmarshal <Struct_v2> .From(buffer);

            // Struct_v2 has an optional field bar, which didn't exist in
            // Struct_v1. It is initialized to 'nothing' by default. By
            // checking if the field is 'nothing' (== null) after
            // de-serialization we can detect if it was present in the
            // payload or not.
            Debug.Assert(v2.bar == null);
            Debug.Assert(v2.baz == null);
        }
示例#36
0
        private void DispatchResponse(EpoxyHeaders headers, ArraySegment <byte> payload, ArraySegment <byte> layerData)
        {
            IMessage response;

            if (headers.error_code != (int)ErrorCode.OK)
            {
                response = Message.FromError(Unmarshal <Error> .From(payload));
            }
            else
            {
                response = Message.FromPayload(Unmarshal.From(payload));
            }

            TaskCompletionSource <IMessage> tcs = responseMap.TakeTaskCompletionSource(headers.conversation_id);

            if (tcs == null)
            {
                logger.Site().Error("{0} Response for unmatched request. Conversation ID: {1}",
                                    this, headers.conversation_id);
                return;
            }

            Task.Run(() =>
            {
                var receiveContext = new EpoxyReceiveContext(this);

                IBonded bondedLayerData = (layerData.Array == null) ? null : Unmarshal.From(layerData);

                ILayerStack layerStack = tcs.Task.AsyncState as ILayerStack;

                Error layerError = LayerStackUtils.ProcessOnReceive(layerStack, MessageType.Response, receiveContext, bondedLayerData, logger);

                if (layerError != null)
                {
                    logger.Site().Error("{0} Receiving response {1}/{2} failed due to layer error (Code: {3}, Message: {4}).",
                                        this, headers.conversation_id, headers.method_name,
                                        layerError.error_code, layerError.message);
                    response = Message.FromError(layerError);
                }

                tcs.SetResult(response);
            });
        }
示例#37
0
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem == null)
            {
                richTextBox2.Text = "";
                return;
            }

            if (Cache.UpdateCache(listBox1.SelectedItem.ToString()) == false)
            {
                WindowLog.Error("Main::LoadCache", "Error loading cache");
                return;
            }

            PyObject cache = Cache.GetCache(listBox1.SelectedItem.ToString());

            PyCachedObject obj = new PyCachedObject();

            if (obj.Decode(cache) == false)
            {
                WindowLog.Error("Main::LoadCache", "Cannot decode the cache data");
                return;
            }

            if (Program.cacheDataDisplayMode == "Pretty")
            {
                try
                {
                    richTextBox2.Text = PrettyPrinter.Print(Unmarshal.Process <PyObject>(obj.cache.Data));
                }
                catch (Exception)
                {
                    WindowLog.Error("Main::LoadCache", "Cannot Unmarshal the cache data");
                    richTextBox2.Text = "Error";
                }
            }
            else
            {
                richTextBox2.Text = ByteToString(obj.cache.Data);
            }

            WindowLog.Debug("Main::LoadCache", "Cache loaded");
        }
示例#38
0
文件: program.cs 项目: zsybupt/bond
        static void Main()
        {
            var example = new Example
            {
                num   = 42,
                str   = "test",
                items = { 3.14, 0 }
            };

            var output = new OutputBuffer();
            var writer = new CompactBinaryWriter <OutputBuffer>(output);

            Marshal.To(writer, example);

            var         input = new InputBuffer(output.Data);
            ExampleView view  = Unmarshal <ExampleView> .From(input);

            Debug.Assert(example.num == view.num);
            Debug.Assert(example.str.Equals(view.str, StringComparison.Ordinal));
        }
示例#39
0
        private State?DispatchRequest(EpoxyHeaders headers, ArraySegment <byte> payload, ArraySegment <byte> layerData)
        {
            if (headers.error_code != (int)ErrorCode.OK)
            {
                Log.Error("{0}.{1}: Received request with a non-zero error code. Conversation ID: {2}",
                          this, nameof(DispatchRequest), headers.conversation_id);
                protocolError = ProtocolErrorCode.PROTOCOL_VIOLATED;
                return(State.SendProtocolError);
            }

            IMessage request = Message.FromPayload(Unmarshal.From(payload));

            var receiveContext = new EpoxyReceiveContext(this);

            IBonded bondedLayerData = (layerData.Array == null) ? null : Unmarshal.From(layerData);

            Error layerError = LayerStackUtils.ProcessOnReceive(parentTransport.LayerStack, MessageType.Request, receiveContext, bondedLayerData);

            Task.Run(async() =>
            {
                IMessage result;

                if (layerError == null)
                {
                    result = await serviceHost.DispatchRequest(headers.method_name, receiveContext, request,
                                                               connectionMetrics);
                }
                else
                {
                    Log.Error("{0}.{1}: Receiving request {2}/{3} failed due to layer error (Code: {4}, Message: {5}).",
                              this, nameof(DispatchRequest), headers.conversation_id, headers.method_name,
                              layerError.error_code, layerError.message);
                    result = Message.FromError(layerError);
                }

                await SendReplyAsync(headers.conversation_id, result);
            });

            // no state change needed
            return(null);
        }
 public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
 {
     if (op == MarshalOpcode.StringEmpty)
         Update(new byte[0]);
     else if (op == MarshalOpcode.StringChar)
         Update(new[]{source.ReadByte()});
     else if (op == MarshalOpcode.WStringUTF8)
         UpdateUTF8(source.ReadBytes((int)source.ReadSizeEx()));
     else if (op == MarshalOpcode.WStringUCS2Char)
         Update(new[]{source.ReadByte(), source.ReadByte()}, true);
     else if (op == MarshalOpcode.WStringEmpty)
         Update(new byte[0]);
     else if (op == MarshalOpcode.WStringUCS2)
         Update(source.ReadBytes((int)source.ReadSizeEx()), true);
     else if (op == MarshalOpcode.StringShort)
         Update(source.ReadBytes(source.ReadByte()));
     else if (op == MarshalOpcode.StringLong)
         Update(source.ReadBytes((int)source.ReadSizeEx()));
     else if (op == MarshalOpcode.StringTable)
     {
         byte index = source.ReadByte();
         Update(StringTable.Entries[index-1]);
     }
 }
 public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
 {
     var size = source.ReadSizeEx();
     Data = source.ReadBytes((int)size);
 }
示例#42
0
 public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
 {
 }
示例#43
0
        private bool ParseRowData(Unmarshal context, BinaryReader source)
        {
            var objex = Header as PyObjectEx;
            if (objex == null)
                return false;

            var header = objex.Header as PyTuple;
            if (header == null || header.Items.Count < 2)
                return false;

            var columns = header.Items[1] as PyTuple;
            if (columns == null)
                return false;

            /*
            columns = columns.Items[0] as PyTuple;
            if (columns == null)
                return false;
            */
            Columns = new List<Column>(columns.Items.Count);

            foreach (var obj in columns)
            {
                var fieldData = obj as PyTuple;
                if (fieldData == null || fieldData.Items.Count < 2)
                    continue;

                var name = fieldData.Items[0] as PyString;
                if (name == null)
                    continue;

                Columns.Add(new Column(name.Value, (FieldType) fieldData.Items[1].IntValue));
            }

            var sizeList = Columns.OrderByDescending(c => FieldTypeHelper.GetTypeBits(c.Type));
            var sizeSum = sizeList.Sum(c => FieldTypeHelper.GetTypeBits(c.Type));
            // align
            sizeSum = (sizeSum + 7) >> 3;
            var rawStream = new MemoryStream();
            // fill up
            rawStream.Write(RawData, 0, RawData.Length);
            for (int i = 0; i < (sizeSum - RawData.Length); i++)
                rawStream.WriteByte(0);
            rawStream.Seek(0, SeekOrigin.Begin);
            var reader = new BinaryReader(rawStream);

            int bitOffset = 0;
            foreach (var column in sizeList)
            {
                switch (column.Type)
                {
                    case FieldType.I8:
                    case FieldType.UI8:
                    case FieldType.CY:
                    case FieldType.FileTime:
                        column.Value = new PyLongLong(reader.ReadInt64());
                        break;

                    case FieldType.I4:
                    case FieldType.UI4:
                        column.Value = new PyInt(reader.ReadInt32());
                        break;

                    case FieldType.I2:
                    case FieldType.UI2:
                        column.Value = new PyInt(reader.ReadInt16());
                        break;

                    case FieldType.I1:
                    case FieldType.UI1:
                        column.Value = new PyInt(reader.ReadByte());
                        break;

                    case FieldType.R8:
                        column.Value = new PyFloat(reader.ReadDouble());
                        break;

                    case FieldType.R4:
                        column.Value = new PyFloat(reader.ReadSingle());
                        break;

                    case FieldType.Bytes:
                    case FieldType.Str:
                    case FieldType.WStr:
                        column.Value = context.ReadObject(source);
                        break;

                    case FieldType.Bool:
                        {
                            if (7 < bitOffset)
                            {
                                bitOffset = 0;
                                reader.ReadByte();
                            }

                            var b = reader.ReadByte();
                            reader.BaseStream.Seek(-1, SeekOrigin.Current);
                            column.Value = new PyInt((b >> bitOffset++) & 0x01);
                            break;
                        }

                    default:
                        throw new Exception("No support for " + column.Type);
                }
            }

            return true;
        }
 public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
 {
     throw new NotImplementedException();
 }
 public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
 {
     var len = source.ReadSizeEx();
     Raw = source.ReadBytes((int) len);
 }
 public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
 {
     Checksum = source.ReadUInt32();
     Data = context.ReadObject(source);
 }
示例#47
0
 public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
 {
     byte len = source.ReadByte();
     RawToken = source.ReadBytes(len);
     Token = Encoding.ASCII.GetString(RawToken);
 }
示例#48
0
 public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
 {
     Definition = context.ReadObject(source);
 }
示例#49
0
 public override void Decode(Unmarshal context, MarshalOpcode op, BinaryReader source)
 {
     Value = source.ReadInt64();
 }