Exemple #1
0
        public bool TrySend(byte[] buffer, int offset, int count, out ZmqErrorCode error)
        {
            if ((uint)offset > (uint)buffer.Length || (uint)count > (uint)(buffer.Length - offset))
            {
                ZmqUtil.ThrowArgOutOfRange();
            }

            if (count == 0)
            {
                error = ZmqErrorCode.None;
                return(false);
            }

            fixed(byte *pBuf = &buffer[0])
            {
                while (true)
                {
                    var length = ZmqNative.send(_handle, pBuf + offset, (IntPtr)count, 0);
                    if (length >= 0)
                    {
                        error = ZmqErrorCode.None;
                        return(true);
                    }

                    error = ZmqNative.errno();
                    if (error == ZmqErrorCode.EINTR)
                    {
                        continue;
                    }

                    return(false);
                }
            }
        }
Exemple #2
0
        public static string ToErrorMessage(this ZmqErrorCode errorCode)
        {
            if (errorCode == ZmqErrorCode.None)
            {
                return(string.Empty);
            }

            var errorStrBuf = ZmqNative.strerror((int)errorCode);

            if (errorStrBuf == null)
            {
                return(string.Empty);
            }

            return($"{Marshal.PtrToStringAnsi((IntPtr)errorStrBuf)} (code {(int)errorCode})");
        }
Exemple #3
0
        public bool TryReadMessage(ref byte[] buffer, out int messageLength, out ZmqErrorCode error)
        {
            ZmqMessage message;

            ZmqMessage.Init(&message);

            try
            {
                while ((messageLength = ZmqNative.msg_recv(&message, _handle, 0)) == -1)
                {
                    error = ZmqNative.errno();
                    if (error == ZmqErrorCode.EINTR)
                    {
                        continue;
                    }

                    messageLength = 0;
                    return(false);
                }

                if (messageLength <= 0)
                {
                    error = ZmqErrorCode.None;
                    return(false);
                }

                if (buffer == null || buffer.Length < messageLength)
                    buffer = new byte[messageLength];

                fixed(byte *pBuf = &buffer[0])
                {
                    Buffer.MemoryCopy(ZmqNative.msg_data(&message), pBuf, buffer.Length, messageLength);
                }

                error = ZmqErrorCode.None;
                return(true);
            }
            finally
            {
                ZmqMessage.Close(&message);
            }
        }