Example #1
0
        public void Write(SocketBase s)
        {
            int size = 4 + 1 + (m_addr?.Length ?? 0) + 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;

            if (m_addr != null)
            {
                buffer[pos++] = (byte)m_addr.Length;

                // was not here originally

                buffer.PutString(m_addr, pos);
                pos += m_addr.Length;
            }
            else
            {
                buffer[pos++] = 0;
            }

            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);
            // An infinite timeout here can cause the IO thread to hang
            // see https://github.com/zeromq/netmq/issues/539
            s.TrySend(ref msg, TimeSpan.Zero, false);
        }
Example #2
0
        public void Write([NotNull] SocketBase s)
        {
            int size = 4 + 1 + (m_addr?.Length ?? 0) + 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;

            if (m_addr != null)
            {
                buffer[pos++] = (byte)m_addr.Length;

                // was not here originally

                buffer.PutString(m_addr, pos);
                pos += m_addr.Length;
            }
            else
            {
                buffer[pos++] = 0;
            }

            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);
        }
Example #3
0
        public virtual void Send(ref Msg msg, SendReceiveOptions options)
        {
            bool more = (options & SendReceiveOptions.SendMore) != 0;

            // 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.TrySend(ref msg, TimeSpan.Zero, more))
                {
                    throw new AgainException();
                }
            }
            else
            {
                // User is expecting to wait, however we must still consider the socket's (obsolete) SendTimeout.
                if (!m_socketHandle.TrySend(ref msg, Options.SendTimeout, more))
                {
                    throw new AgainException();
                }
            }
        }
Example #4
0
 /// <summary>
 /// Send a message if one is available within <paramref name="timeout"/>.
 /// </summary>
 /// <param name="msg">An object with message's data to send.</param>
 /// <param name="timeout">The maximum length of time to try and send a message. If <see cref="TimeSpan.Zero"/>, no
 /// wait occurs.</param>
 /// <returns><c>true</c> if a message was sent, otherwise <c>false</c>.</returns>
 public virtual bool TrySend(ref Msg msg, TimeSpan timeout)
 {
     return(m_socketHandle.TrySend(ref msg, timeout, false));
 }