Esempio n. 1
0
        private static HcpElement MarshalDecodedMessage(HcpDecodedMessage Message)
        {
            var output = new Dictionary <string, dynamic>();
            var size   = Marshal.SizeOf(typeof(HcpParameter));
            var count  = Message.parameterCount.ToInt32();

            for (var i = 0; i < count; i++)
            {
                var address = IntPtr.Zero;

                if (IntPtr.Size == 4)
                {
                    address = new IntPtr(Message.parameters.ToInt32() + (int)i * size);
                }
                else
                {
                    address = new IntPtr(Message.parameters.ToInt64() + (long)i * size);
                }

                var parameter = (HcpParameter)Marshal.PtrToStructure(address, typeof(HcpParameter));

                output.Add(parameter.Name, parameter.Value);
            }

            return(new HcpElement(Message.command.ToString(), Message.family.ToString(), output));
        }
Esempio n. 2
0
        public HcpElement Decode(byte[] Source, int Length, out int BytesRead)
        {
            if (_decode == null)
            {
                throw new MissingMethodException("The HCP-runtime does not support decoding.");
            }

            lock (this) {
                var handle = GCHandle.Alloc(Source, GCHandleType.Pinned);
                HcpDecodedMessage message = new HcpDecodedMessage();
                var codec = GetValidCodec();
                try {
                    BytesRead = _decode(_state, codec, handle.AddrOfPinnedObject(), new IntPtr(Length), ref message);
                }
                catch (Exception x) {
                    throw x;
                }
                finally {
                    handle.Free();
                }

                return(MarshalDecodedMessage(message));
            }
        }