Esempio 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));
        }
Esempio 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));
        }
Esempio n. 3
0
        public void IntBigEndian()
        {
            ByteArraySegment byteArraySegment = new ByteArraySegment(new byte[4]);

            byteArraySegment.PutInteger(Endianness.Big, 1, 0);

            Assert.Equal(1, byteArraySegment[3]);
            Assert.Equal(0, byteArraySegment[0]);

            long num = byteArraySegment.GetInteger(Endianness.Big, 0);

            Assert.Equal(1, num);

            byteArraySegment.PutInteger(Endianness.Big, 16777216, 0);

            Assert.Equal(1, byteArraySegment[0]);
            Assert.Equal(0, byteArraySegment[3]);

            num = byteArraySegment.GetInteger(Endianness.Big, 0);

            Assert.Equal(16777216, num);
        }