Beispiel #1
0
        public async Task <Dictionary <string, PropDesc> > GetObjPropDescRange(HeapPtr ptr, int start, int end)
        {
            var reply = await DoRequest(DValueTag.REQ, Request.GetObjPropDescRange, ptr, start, end);

            var props = new Dictionary <string, PropDesc>();
            int count = (reply.Length - 1) / 2;
            int i     = 1;

            while (i < reply.Length)
            {
                PropFlags flags = (PropFlags)(int)reply[i++];
                string    name  = reply[i++].ToString();
                if (flags.HasFlag(PropFlags.Accessor))
                {
                    DValue   getter    = reply[i++];
                    DValue   setter    = reply[i++];
                    PropDesc propValue = new PropDesc(getter, setter, flags);
                    if (!flags.HasFlag(PropFlags.Internal))
                    {
                        props.Add(name, propValue);
                    }
                }
                else
                {
                    DValue   value     = reply[i++];
                    PropDesc propValue = new PropDesc(value, flags);
                    if (!flags.HasFlag(PropFlags.Internal) && value.Tag != DValueTag.Unused)
                    {
                        props.Add(name, propValue);
                    }
                }
            }
            return(props);
        }
Beispiel #2
0
        public static DValue Receive(Socket socket)
        {
            byte initialByte;

            byte[]   bytes;
            int      length = -1;
            Encoding utf8   = new UTF8Encoding(false);

            if (!socket.ReceiveAll(bytes = new byte[1]))
            {
                return(null);
            }
            initialByte = bytes[0];
            if (initialByte >= 0x60 && initialByte < 0x80)
            {
                length = initialByte - 0x60;
                if (!socket.ReceiveAll(bytes = new byte[length]))
                {
                    return(null);
                }
                return(new DValue(utf8.GetString(bytes)));
            }
            else if (initialByte >= 0x80 && initialByte < 0xC0)
            {
                return(new DValue(initialByte - 0x80));
            }
            else if (initialByte >= 0xC0)
            {
                Array.Resize(ref bytes, 2);
                if (socket.Receive(bytes, 1, 1, SocketFlags.None) == 0)
                {
                    return(null);
                }
                return(new DValue(((initialByte - 0xC0) << 8) + bytes[1]));
            }
            else
            {
                HeapPtr heapPtr = new HeapPtr();
                switch ((DValueTag)initialByte)
                {
                case DValueTag.EOM:
                case DValueTag.REQ:
                case DValueTag.REP:
                case DValueTag.ERR:
                case DValueTag.NFY:
                case DValueTag.Unused:
                case DValueTag.Undefined:
                case DValueTag.Null:
                case DValueTag.True:
                case DValueTag.False:
                    return(new DValue((DValueTag)initialByte));

                case DValueTag.Integer:     // 32-bit integer
                    if (!socket.ReceiveAll(bytes = new byte[4]))
                    {
                        return(null);
                    }
                    return(new DValue((bytes[0] << 24) + (bytes[1] << 16) + (bytes[2] << 8) + bytes[3]));

                case DValueTag.String:
                    if (!socket.ReceiveAll(bytes = new byte[4]))
                    {
                        return(null);
                    }
                    length = (bytes[0] << 24) + (bytes[1] << 16) + (bytes[2] << 8) + bytes[3];
                    if (!socket.ReceiveAll(bytes = new byte[length]))
                    {
                        return(null);
                    }
                    return(new DValue(utf8.GetString(bytes)));

                case DValueTag.SmallString:
                    if (!socket.ReceiveAll(bytes = new byte[2]))
                    {
                        return(null);
                    }
                    length = (bytes[0] << 8) + bytes[1];
                    if (!socket.ReceiveAll(bytes = new byte[length]))
                    {
                        return(null);
                    }
                    return(new DValue(utf8.GetString(bytes)));

                case DValueTag.Buffer:
                    if (!socket.ReceiveAll(bytes = new byte[4]))
                    {
                        return(null);
                    }
                    length = (bytes[0] << 24) + (bytes[1] << 16) + (bytes[2] << 8) + bytes[3];
                    if (!socket.ReceiveAll(bytes = new byte[length]))
                    {
                        return(null);
                    }
                    return(new DValue(bytes));

                case DValueTag.SmallBuffer:
                    if (!socket.ReceiveAll(bytes = new byte[2]))
                    {
                        return(null);
                    }
                    length = (bytes[0] << 8) + bytes[1];
                    if (!socket.ReceiveAll(bytes = new byte[length]))
                    {
                        return(null);
                    }
                    return(new DValue(bytes));

                case DValueTag.Float:
                    if (!socket.ReceiveAll(bytes = new byte[8]))
                    {
                        return(null);
                    }
                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(bytes);
                    }
                    return(new DValue(BitConverter.ToDouble(bytes, 0)));

                case DValueTag.Object:
                    socket.ReceiveAll(bytes        = new byte[1]);
                    heapPtr.Class                  = (HeapClass)bytes[0];
                    socket.ReceiveAll(bytes        = new byte[1]);
                    heapPtr.Size                   = bytes[0];
                    socket.ReceiveAll(heapPtr.Data = new byte[heapPtr.Size]);
                    return(new DValue(heapPtr));

                case DValueTag.Pointer:
                    socket.ReceiveAll(bytes = new byte[1]);
                    socket.ReceiveAll(new byte[bytes[0]]);
                    return(new DValue(DValueTag.Pointer));

                case DValueTag.LightFunc:
                    socket.ReceiveAll(bytes = new byte[3]);
                    socket.ReceiveAll(new byte[bytes[2]]);
                    return(new DValue(DValueTag.LightFunc));

                case DValueTag.HeapPtr:
                    heapPtr.Class                  = HeapClass.Unknown;
                    socket.ReceiveAll(bytes        = new byte[1]);
                    heapPtr.Size                   = bytes[0];
                    socket.ReceiveAll(heapPtr.Data = new byte[heapPtr.Size]);
                    return(new DValue(heapPtr));

                default:
                    return(null);
                }
            }
        }
Beispiel #3
0
 public DValue(HeapPtr ptr)
 {
     _tag   = DValueTag.HeapPtr;
     _value = ptr;
 }