コード例 #1
0
ファイル: NetMQSocket.cs プロジェクト: yeowgit/netmq
        public virtual void Receive(ref Msg msg, SendReceiveOptions options)
        {
            // This legacy method adapts the newer nothrow API to the older AgainException one.

            if ((options & SendReceiveOptions.DontWait) != 0)
            {
                // User specified DontWait, so use a zero timeout.
                if (!m_socketHandle.TryRecv(ref msg, TimeSpan.Zero))
                {
                    throw new AgainException();
                }
            }
            else
            {
                // User is expecting to wait, however we must still consider the socket's (obsolete) ReceiveTimeout.
                if (!m_socketHandle.TryRecv(ref msg, Options.ReceiveTimeout))
                {
                    throw new AgainException();
                }
            }
        }
コード例 #2
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));
        }
コード例 #3
0
 /// <summary>Attempt to receive a message for the specified amount of time.</summary>
 /// <param name="msg">A reference to a <see cref="Msg"/> instance into which the received message
 /// data should be placed.</param>
 /// <param name="timeout">The maximum amount of time the call should wait for a message before returning.</param>
 /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
 /// <returns><c>true</c> if a message was received before <paramref name="timeout"/> elapsed,
 /// otherwise <c>false</c>.</returns>
 public virtual bool TryReceive(ref Msg msg, TimeSpan timeout, CancellationToken cancellationToken = default)
 {
     return(m_socketHandle.TryRecv(ref msg, timeout, cancellationToken));
 }
コード例 #4
0
 public virtual bool TryReceive(ref Msg msg, TimeSpan timeout)
 {
     return(m_socketHandle.TryRecv(ref msg, timeout));
 }