Ejemplo n.º 1
0
        public int Forward(IntPtr destinationHandle)
        {
            if (_msg.Init() == -1)
            {
                return(-1);
            }

            int receiveMore;
            int bytesSent;

            do
            {
                if (LibZmq.zmq_msg_recv(_msg, SocketHandle, 0) == -1)
                {
                    return(-1);
                }

                if (GetReceiveMore(out receiveMore) == -1)
                {
                    return(-1);
                }

                if ((bytesSent = LibZmq.zmq_msg_send(_msg, destinationHandle, receiveMore == 1 ? (int)SocketFlags.SendMore : 0)) == -1)
                {
                    return(-1);
                }
            }while (receiveMore == 1);

            if (_msg.Close() == -1)
            {
                return(-1);
            }

            return(bytesSent);
        }
Ejemplo n.º 2
0
        public int Receive(byte[] buffer, int flags)
        {
            if (LibZmq.zmq_msg_init_size(_message, buffer.Length) == -1)
            {
                return(-1);
            }

            int bytesReceived = RetryIfInterrupted(() => LibZmq.zmq_recvmsg(SocketHandle, _message, flags));

            if (bytesReceived == 0 && LibZmq.MajorVersion < 3)
            {
                // 0MQ 2.x does not return number of bytes received
                bytesReceived = LibZmq.zmq_msg_size(_message);
            }

            if (bytesReceived > 0)
            {
                Marshal.Copy(LibZmq.zmq_msg_data(_message), buffer, 0, bytesReceived);
            }

            if (LibZmq.zmq_msg_close(_message) == -1)
            {
                return(-1);
            }

            return(bytesReceived);
        }
Ejemplo n.º 3
0
        public int Forward(IntPtr destinationHandle)
        {
            if (LibZmq.zmq_msg_init(_message) == -1)
            {
                return(-1);
            }

            int receiveMore;
            int bytesSent;

            do
            {
                if (LibZmq.zmq_recvmsg(SocketHandle, _message, 0) == -1)
                {
                    return(-1);
                }

                if (GetReceiveMore(out receiveMore) == -1)
                {
                    return(-1);
                }

                if ((bytesSent = LibZmq.zmq_sendmsg(destinationHandle, _message, receiveMore == 1 ? (int)SocketFlags.SendMore : 0)) == -1)
                {
                    return(-1);
                }
            }while (receiveMore == 1);

            if (LibZmq.zmq_msg_close(_message) == -1)
            {
                return(-1);
            }

            return(bytesSent);
        }
Ejemplo n.º 4
0
        public int Send(byte[] buffer, int size, int flags)
        {
            if (LibZmq.zmq_msg_init_size(_message, size) == -1)
            {
                return(-1);
            }

            if (size > 0)
            {
                Marshal.Copy(buffer, 0, LibZmq.zmq_msg_data(_message), size);
            }

            int bytesSent = RetryIfInterrupted(() => LibZmq.zmq_sendmsg(SocketHandle, _message, flags));

            if (bytesSent == 0 && LibZmq.MajorVersion < 3)
            {
                // 0MQ 2.x does not report number of bytes sent, so this may be inaccurate/misleading
                bytesSent = size;
            }

            if (LibZmq.zmq_msg_close(_message) == -1)
            {
                return(-1);
            }

            return(bytesSent);
        }
Ejemplo n.º 5
0
        public int Init()
        {
            int rc = LibZmq.zmq_msg_init(_ptr);

            _initialized = true;

            return(rc);
        }
Ejemplo n.º 6
0
        public int Init(int size)
        {
            int rc = LibZmq.zmq_msg_init_size(_ptr, size);

            _initialized = true;

            return(rc);
        }
Ejemplo n.º 7
0
        public int Close()
        {
            int rc = LibZmq.zmq_msg_close(_ptr);

            _initialized = false;

            return(rc);
        }
Ejemplo n.º 8
0
        public int SetSocketOption(int option, int value)
        {
            using (var optionValue = new DisposableIntPtr(Marshal.SizeOf(typeof(int))))
            {
                Marshal.WriteInt32(optionValue, value);

                return(RetryIfInterrupted(() => LibZmq.zmq_setsockopt(SocketHandle, option, optionValue, sizeof(int))));
            }
        }
Ejemplo n.º 9
0
        public int SetSocketOption(int option, ulong value)
        {
            using (var optionValue = new DisposableIntPtr(Marshal.SizeOf(typeof(ulong))))
            {
                Marshal.WriteInt64(optionValue, unchecked (Convert.ToInt64(value)));

                return(RetryIfInterrupted(() => LibZmq.zmq_setsockopt(SocketHandle, option, optionValue, sizeof(ulong))));
            }
        }
Ejemplo n.º 10
0
        public int SetSocketOption(int option, byte[] value)
        {
            using (var optionValue = new DisposableIntPtr(value.Length))
            {
                Marshal.Copy(value, 0, optionValue, value.Length);

                return(RetryIfInterrupted(() => LibZmq.zmq_setsockopt(SocketHandle, option, optionValue, value.Length)));
            }
        }
Ejemplo n.º 11
0
        public static int IfInterrupted <T1, T2, T3>(Func <T1, T2, T3, int> operation, T1 arg1, T2 arg2, T3 arg3)
        {
            int rc;

            do
            {
                rc = operation(arg1, arg2, arg3);
            }while (rc == -1 && LibZmq.zmq_errno() == ErrorCode.EINTR);

            return(rc);
        }
Ejemplo n.º 12
0
        private static int RetryIfInterrupted(Func <int> func)
        {
            int rc;

            do
            {
                rc = func();
            }while (rc == -1 && LibZmq.zmq_errno() == ErrorCode.EINTR);

            return(rc);
        }
Ejemplo n.º 13
0
        public int Initialize()
        {
            try
            {
                ContextHandle = LibZmq.zmq_ctx_new();
            }
            catch (Exception ex)
            {
            }

            return(ContextHandle == IntPtr.Zero ? -1 : 0);
        }
Ejemplo n.º 14
0
        public int Copy(ZmqMsgT dest)
        {
            if (!_initialized)
            {
                dest.Close();
                return(0);
            }

            int rc = LibZmq.zmq_msg_copy(dest._ptr, _ptr);

            dest._initialized = rc == 0;
            return(rc);
        }
Ejemplo n.º 15
0
        public int GetSocketOption(int option, out ulong value)
        {
            using (var optionLength = new DisposableIntPtr(IntPtr.Size))
                using (var optionValue = new DisposableIntPtr(Marshal.SizeOf(typeof(ulong))))
                {
                    Marshal.WriteInt32(optionLength, sizeof(ulong));

                    int rc = RetryIfInterrupted(() => LibZmq.zmq_getsockopt(SocketHandle, option, optionValue, optionLength));
                    value = unchecked (Convert.ToUInt64(Marshal.ReadInt64(optionValue)));

                    return(rc);
                }
        }
Ejemplo n.º 16
0
        public int Close()
        {
            // Allow Close to be called repeatedly without failure
            if (SocketHandle == IntPtr.Zero)
            {
                return 0;
            }

            int rc = LibZmq.zmq_close(SocketHandle);

            SocketHandle = IntPtr.Zero;

            return rc;
        }
Ejemplo n.º 17
0
        public int GetSocketOption(int option, out byte[] value)
        {
            using (var optionLength = new DisposableIntPtr(IntPtr.Size))
                using (var optionValue = new DisposableIntPtr(MaxBinaryOptionSize))
                {
                    Marshal.WriteInt32(optionLength, MaxBinaryOptionSize);

                    int rc = RetryIfInterrupted(() => LibZmq.zmq_getsockopt(SocketHandle, option, optionValue, optionLength));

                    value = new byte[Marshal.ReadInt32(optionLength)];
                    Marshal.Copy(optionValue, value, 0, value.Length);

                    return(rc);
                }
        }
Ejemplo n.º 18
0
        public static ErrorDetails GetLastError()
        {
            int errorCode = GetErrorCode();

            if (KnownErrors.ContainsKey(errorCode))
            {
                return(KnownErrors[errorCode]);
            }

            string message = Marshal.PtrToStringAnsi(LibZmq.zmq_strerror(errorCode));

            var errorDetails = new ErrorDetails(errorCode, message);

            KnownErrors[errorCode] = errorDetails;

            return(errorDetails);
        }
Ejemplo n.º 19
0
        public static ErrorDetails GetLastError()
        {
            int errorCode = GetErrorCode();

            if (KnownErrors.ContainsKey(errorCode))
            {
                return(KnownErrors[errorCode]);
            }

            string message = Marshal.PtrToStringUni(LibZmq.zmq_strerror(errorCode));

            byte[] temp = Encoding.Unicode.GetBytes(message);

            message = Encoding.ASCII.GetString(temp, 0, temp.Length).Replace('\0', ' ').Trim();;
            var errorDetails = new ErrorDetails(errorCode, message);

            KnownErrors[errorCode] = errorDetails;

            return(errorDetails);
        }
Ejemplo n.º 20
0
        public void Terminate()
        {
            if (ContextHandle == IntPtr.Zero)
            {
                return;
            }

            while (LibZmq.zmq_term(ContextHandle) != 0)
            {
                int errorCode = ErrorProxy.GetErrorCode();

                // If zmq_term fails, valid return codes are EFAULT or EINTR. If EINTR is set, termination
                // was interrupted by a signal and may be safely retried.
                if (errorCode == ErrorCode.EFAULT)
                {
                    // This indicates an invalid context was passed in. There's nothing we can do about it here.
                    // It's arguably not a fatal error, so throwing an exception would be bad seeing as this may
                    // run inside a finalizer.
                    break;
                }
            }

            ContextHandle = IntPtr.Zero;
        }
Ejemplo n.º 21
0
        public byte[] Receive(byte[] buffer, int flags, out int size)
        {
            size = -1;

            if (LibZmq.zmq_msg_init(_message) == -1)
            {
                return(buffer);
            }

            int bytesReceived = RetryIfInterrupted(() => LibZmq.zmq_recvmsg(SocketHandle, _message, flags));

            if (bytesReceived >= 0)
            {
                if (bytesReceived == 0 && LibZmq.MajorVersion < 3)
                {
                    // 0MQ 2.x does not return number of bytes received
                    bytesReceived = LibZmq.zmq_msg_size(_message);
                }

                size = bytesReceived;

                if (buffer == null || size > buffer.Length)
                {
                    buffer = new byte[size];
                }

                Marshal.Copy(LibZmq.zmq_msg_data(_message), buffer, 0, size);
            }

            if (LibZmq.zmq_msg_close(_message) == -1)
            {
                size = -1;
            }

            return(buffer);
        }
Ejemplo n.º 22
0
 public IntPtr CreateSocket(int socketType)
 {
     return(LibZmq.zmq_socket(ContextHandle, socketType));
 }
Ejemplo n.º 23
0
 public IntPtr Data()
 {
     return(LibZmq.zmq_msg_data(_ptr));
 }
Ejemplo n.º 24
0
        public int RegisterMonitor(LibZmq.MonitorFuncCallback callback)
        {
            MonitorRegistered = true;

            return LibZmq.zmq_ctx_set_monitor(ContextHandle, callback);
        }
Ejemplo n.º 25
0
        public int Initialize()
        {
            ContextHandle = LibZmq.zmq_init(ThreadPoolSize);

            return(ContextHandle == IntPtr.Zero ? -1 : 0);
        }
Ejemplo n.º 26
0
 public int Monitor(string endpoint, int events)
 {
     return LibZmq.zmq_socket_monitor(SocketHandle, Encoding.ASCII.GetBytes(endpoint), events);
 }
Ejemplo n.º 27
0
        public int RegisterMonitor(LibZmq.MonitorFuncCallback callback)
        {
            _callback = callback;

            return LibZmq.zmq_ctx_set_monitor(ContextHandle, _callback);
        }
Ejemplo n.º 28
0
 public int Disconnect(string endpoint)
 {
     return LibZmq.zmq_disconnect(SocketHandle, Encoding.ASCII.GetBytes(endpoint));
 }
Ejemplo n.º 29
0
 public int Unbind(string endpoint)
 {
     return LibZmq.zmq_unbind(SocketHandle, Encoding.ASCII.GetBytes(endpoint));
 }
Ejemplo n.º 30
0
 public int SetContextOption(int option, int value)
 {
     return(LibZmq.zmq_ctx_set(ContextHandle, option, value));
 }
Ejemplo n.º 31
0
 public static int GetErrorCode()
 {
     return(LibZmq.zmq_errno());
 }
Ejemplo n.º 32
0
 public int Size()
 {
     return(LibZmq.zmq_msg_size(_ptr));
 }