Exemplo n.º 1
0
        public static MonitorEvent Read(SocketBase s)
        {
            var msg = new Msg();

            msg.InitEmpty();

            s.TryRecv(ref msg, SendReceiveConstants.InfiniteTimeout);

            int pos = msg.UnsafeOffset;

            Assumes.NotNull(msg.UnsafeData);

            ByteArraySegment data = msg.UnsafeData;

            var @event = (SocketEvents)data.GetInteger(Endianness.Little, pos);

            pos += 4;
            var    len  = (int)data[pos++];
            string addr = data.GetString(len, pos);

            pos += len;
            var    flag = (int)data[pos++];
            object?arg  = null;

            if (flag == ValueInteger)
            {
                arg = data.GetInteger(Endianness.Little, pos);
            }
            else if (flag == ValueChannel)
            {
                IntPtr value = s_sizeOfIntPtr == 4
                    ? new IntPtr(data.GetInteger(Endianness.Little, pos))
                    : new IntPtr(data.GetLong(Endianness.Little, pos));

                GCHandle    handle = GCHandle.FromIntPtr(value);
                AsyncSocket?socket = null;

                if (handle.IsAllocated)
                {
                    socket = handle.Target as AsyncSocket;
                }

                handle.Free();

                arg = socket;
            }

            return(new MonitorEvent(@event, addr, arg));
        }
Exemplo n.º 2
0
        public static MonitorEvent Read([NotNull] SocketBase s)
        {
            var msg = new Msg();

            msg.InitEmpty();

            s.Recv(ref msg);

            int pos = 0;
            ByteArraySegment data = msg.Data;

            var @event = (SocketEvents)data.GetInteger(Endianness.Little, pos);

            pos += 4;
            var    len  = (int)data[pos++];
            string addr = data.GetString(len, pos);

            pos += len;
            var    flag = (int)data[pos++];
            Object arg  = null;

            if (flag == ValueInteger)
            {
                arg = data.GetInteger(Endianness.Little, pos);
            }
            else if (flag == ValueChannel)
            {
                IntPtr value = s_sizeOfIntPtr == 4
                    ? new IntPtr(data.GetInteger(Endianness.Little, pos))
                    : new IntPtr(data.GetLong(Endianness.Little, pos));

                GCHandle    handle = GCHandle.FromIntPtr(value);
                AsyncSocket socket = null;

                if (handle.IsAllocated)
                {
                    socket = handle.Target as AsyncSocket;
                }

                handle.Free();

                arg = socket;
            }

            return(new MonitorEvent(@event, addr, arg));
        }