Пример #1
0
 public static void Close(ZmqMessage *message)
 {
     if (ZmqNative.msg_close(message) == -1)
     {
         ZmqUtil.ThrowLastError("Could not close ZMQ message");
     }
 }
Пример #2
0
 public void Connect(string endpoint)
 {
     if (ZmqNative.connect(_handle, endpoint) == -1)
     {
         ZmqUtil.ThrowLastError($"Unable to connect ZMQ socket to {endpoint}");
     }
 }
Пример #3
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);
                }
            }
        }
Пример #4
0
 public void Bind(string endpoint)
 {
     if (ZmqNative.bind(_handle, endpoint) == -1)
     {
         ZmqUtil.ThrowLastError($"Unable to bind ZMQ socket to {endpoint}");
     }
 }
Пример #5
0
        public static Version GetVersion()
        {
            int major, minor, patch;

            ZmqNative.version(&major, &minor, &patch);
            return(new Version(major, minor, patch));
        }
Пример #6
0
 public static void Init(ZmqMessage *message)
 {
     if (ZmqNative.msg_init(message) == -1)
     {
         ZmqUtil.ThrowLastError("Could not initialize ZMQ message");
     }
 }
Пример #7
0
        public ZmqContext()
        {
            Handle = ZmqNative.ctx_new();

            if (Handle == IntPtr.Zero)
            {
                ZmqUtil.ThrowLastError("Could not create ZMQ context");
            }
        }
Пример #8
0
        public bool TryDisconnect(string endpoint)
        {
            if (ZmqNative.disconnect(_handle, endpoint) == -1)
            {
                return(false);
            }

            return(true);
        }
Пример #9
0
        public bool TryUnbind(string endpoint)
        {
            if (ZmqNative.unbind(_handle, endpoint) == -1)
            {
                return(false);
            }

            return(true);
        }
Пример #10
0
        public ZmqSocket(ZmqContext context, ZmqSocketType type)
        {
            _context = context;

            _handle = ZmqNative.socket(_context.Handle, (int)type);

            if (_handle == IntPtr.Zero)
            {
                ZmqUtil.ThrowLastError($"Could not create ZMQ {type} socket");
            }
        }
Пример #11
0
        public static void Init(ZmqMessage *message)
        {
            while (ZmqNative.msg_init(message) == -1)
            {
                if (ZmqUtil.WasInterrupted())
                {
                    continue;
                }

                ZmqUtil.ThrowLastError("Could not initialize ZMQ message");
            }
        }
Пример #12
0
        public void SetOption(ZmqSocketOption option, int value)
        {
            while (ZmqNative.setsockopt(_handle, (int)option, &value, (IntPtr)sizeof(int)) == -1)
            {
                if (ZmqUtil.WasInterrupted())
                {
                    continue;
                }

                ZmqUtil.ThrowLastError($"Unable to set ZMQ socket option {option} to {value}");
            }
        }
Пример #13
0
        public void Connect(string endpoint)
        {
            while (ZmqNative.connect(_handle, endpoint) == -1)
            {
                if (ZmqUtil.WasInterrupted())
                {
                    continue;
                }

                ZmqUtil.ThrowLastError($"Unable to connect ZMQ socket to {endpoint}");
            }
        }
Пример #14
0
        public bool TryDisconnect(string endpoint)
        {
            while (ZmqNative.disconnect(_handle, endpoint) == -1)
            {
                if (ZmqUtil.WasInterrupted())
                {
                    continue;
                }

                return(false);
            }

            return(true);
        }
Пример #15
0
        public void SetOption(ZmqSocketOption option, byte[] value)
        {
            fixed(byte *valuePtr = value)
            {
                while (ZmqNative.setsockopt(_handle, (int)option, valuePtr, (IntPtr)(value?.Length ?? 0)) == -1)
                {
                    if (ZmqUtil.WasInterrupted())
                    {
                        continue;
                    }

                    ZmqUtil.ThrowLastError($"Unable to set ZMQ socket option {option}");
                }
            }
        }
Пример #16
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})");
        }
Пример #17
0
        private void Close(bool canThrow)
        {
            if (_handle == IntPtr.Zero)
            {
                return;
            }

            if (ZmqNative.close(_handle) == -1)
            {
                if (canThrow)
                {
                    ZmqUtil.ThrowLastError("Could not close ZMQ socket");
                }
            }

            _handle = IntPtr.Zero;
        }
Пример #18
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);
            }
        }
Пример #19
0
        public string GetOptionString(ZmqSocketOption option)
        {
            const int bufSize = 256;
            var       buf     = stackalloc byte[bufSize];
            var       size    = (IntPtr)bufSize;

            while (ZmqNative.getsockopt(_handle, (int)option, buf, &size) == -1)
            {
                if (ZmqUtil.WasInterrupted())
                {
                    continue;
                }

                ZmqUtil.ThrowLastError($"Unable to get ZMQ socket option {option}");
            }

            if ((long)size <= 1)
            {
                return(string.Empty);
            }

            return(Marshal.PtrToStringAnsi((IntPtr)buf, (int)size - 1));
        }
Пример #20
0
        private void Terminate(bool canThrow)
        {
            if (Handle == IntPtr.Zero)
            {
                return;
            }

            while (ZmqNative.ctx_term(Handle) == -1)
            {
                if (ZmqUtil.WasInterrupted())
                {
                    continue;
                }

                if (canThrow)
                {
                    ZmqUtil.ThrowLastError("Could not terminate ZMQ context");
                }

                break;
            }

            Handle = IntPtr.Zero;
        }
Пример #21
0
 public static bool WasInterrupted()
 => ZmqNative.errno() == ZmqErrorCode.EINTR;
Пример #22
0
 public static string GetLastErrorMessage()
 => ZmqNative.errno().ToErrorMessage();