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

TrySend() public method

Transmit the given Msg across the message-queueing system.
The socket has been stopped. is not initialised.
public TrySend ( Msg &msg, System.TimeSpan timeout, bool more ) : bool
msg Msg The to send.
timeout System.TimeSpan The timeout to wait before returning false. Pass to disable timeout.
more bool Whether this message will contain another frame after this one.
return bool
        public bool TrySend(ref Msg msg, TimeSpan timeout, bool more)
        {
            CheckContextTerminated();

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

            // Process pending commands, if any.
            ProcessCommands(0, true);

            // Clear any user-visible flags that are set on the message.
            msg.ResetFlags(MsgFlags.More);

            // At this point we impose the flags on the message.
            if (more)
                msg.SetFlags(MsgFlags.More);

            // Try to send the message.
            bool isMessageSent = XSend(ref msg);

            if (isMessageSent)
                return true;

            // In case of non-blocking send we'll simply return false
            if (timeout == TimeSpan.Zero)
                return false;

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

            // Oops, we couldn't send the message. Wait for the next
            // command, process it and try to send the message again.
            // If timeout is reached in the meantime, return EAGAIN.
            while (true)
            {
                ProcessCommands(timeoutMillis, false);

                isMessageSent = XSend(ref msg);

                if (isMessageSent)
                    break;

                if (timeoutMillis <= 0)
                    continue;

                timeoutMillis = (int)(end - Clock.NowMs());

                if (timeoutMillis <= 0)
                    return false;
            }

            return true;
        }

Usage Example

Example #1
0
        public void Write( SocketBase s)
        {
            int size = 4 + 1 + m_addr.Length + 1; // event + len(addr) + addr + flag

            if (m_flag == ValueInteger)
                size += 4;
            else if (m_flag == ValueChannel)
                size += s_sizeOfIntPtr;

            int pos = 0;

            ByteArraySegment buffer = new byte[size];
            buffer.PutInteger(Endianness.Little, (int)m_monitorEvent, pos);
            pos += 4;
            buffer[pos++] = (byte)m_addr.Length;

            // was not here originally

            buffer.PutString(m_addr, pos);
            pos += m_addr.Length;

            buffer[pos++] = ((byte)m_flag);
            if (m_flag == ValueInteger)
            {
                buffer.PutInteger(Endianness.Little, (int)m_arg, pos);
            }
            else if (m_flag == ValueChannel)
            {
                GCHandle handle = GCHandle.Alloc(m_arg, GCHandleType.Weak);

                if (s_sizeOfIntPtr == 4)
                    buffer.PutInteger(Endianness.Little, GCHandle.ToIntPtr(handle).ToInt32(), pos);
                else
                    buffer.PutLong(Endianness.Little, GCHandle.ToIntPtr(handle).ToInt64(), pos);
            }

            var msg = new Msg();
            msg.InitGC((byte[])buffer, buffer.Size);
            s.TrySend(ref msg, SendReceiveConstants.InfiniteTimeout, false);
        }