NetMQ.Core.SocketBase.TryRecv C# (CSharp) Method

TryRecv() public method

Receive a frame into the given msg and return true if successful, false if it timed out.
For timeout, there are three categories of value: TimeSpan.Zero - return false immediately if no message is available SendReceiveConstants.InfiniteTimeout (or a negative value) - wait indefinitely, always returning true Any positive value - return false after the corresponding duration if no message has become available
the Msg must already have been uninitialised The socket must not already be stopped.
public TryRecv ( Msg &msg, System.TimeSpan timeout ) : bool
msg Msg the Msg to read the received message into
timeout System.TimeSpan this controls whether the call blocks, and for how long.
return bool
        public bool TryRecv(ref Msg msg, TimeSpan timeout)
        {
            CheckContextTerminated();

            // Check whether message passed to the function is valid.
            if (!msg.IsInitialised)
                throw new FaultException("SocketBase.Recv passed an uninitialised Msg.");

            // Get the message.
            bool isMessageAvailable = XRecv(ref msg);

            // Once every Config.InboundPollRate messages check for signals and process
            // incoming commands. This happens only if we are not polling altogether
            // because there are messages available all the time. If poll occurs,
            // ticks is set to zero and thus we avoid this code.
            //
            // Note that 'recv' uses different command throttling algorithm (the one
            // described above) from the one used by 'send'. This is because counting
            // ticks is more efficient than doing RDTSC all the time.
            if (++m_ticks == Config.InboundPollRate)
            {
                ProcessCommands(0, false);
                m_ticks = 0;
            }

            // If we have the message, return immediately.
            if (isMessageAvailable)
            {
                ExtractFlags(ref msg);
                return true;
            }

            // If the message cannot be fetched immediately, there are two scenarios.
            // For non-blocking recv, commands are processed in case there's an
            // activate_reader command already waiting in a command pipe.
            // If it's not, return false.
            if (timeout == TimeSpan.Zero)
            {
                ProcessCommands(0, false);
                m_ticks = 0;

                isMessageAvailable = XRecv(ref msg);

                if (!isMessageAvailable)
                    return false;

                ExtractFlags(ref msg);
                return true;
            }

            // Compute the time when the timeout should occur.
            // If the timeout is infinite (negative), don't care.
            int timeoutMillis = (int)timeout.TotalMilliseconds;
            long end = timeoutMillis < 0 ? 0L : Clock.NowMs() + timeoutMillis;

            // In blocking scenario, commands are processed over and over again until
            // we are able to fetch a message.
            bool block = m_ticks != 0;
            while (true)
            {
                ProcessCommands(block ? timeoutMillis : 0, false);

                isMessageAvailable = XRecv(ref msg);
                if (isMessageAvailable)
                {
                    m_ticks = 0;
                    break;
                }

                block = true;
                if (timeoutMillis > 0)
                {
                    timeoutMillis = (int)(end - Clock.NowMs());

                    if (timeoutMillis <= 0)
                        return false;
                }
            }

            ExtractFlags(ref msg);
            return true;
        }

Usage Example

Example #1
0
        public static MonitorEvent Read( SocketBase s)
        {
            var msg = new Msg();
            msg.InitEmpty();

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

            int pos = msg.Offset;
            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);
        }