示例#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);
        }
示例#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);
        }
示例#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);
        }
示例#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);
        }
示例#5
0
文件: ZmqMsgT.cs 项目: saleyn/clrzmq
        public int Init()
        {
            int rc = LibZmq.zmq_msg_init(_ptr);

            _initialized = true;

            return(rc);
        }
示例#6
0
文件: ZmqMsgT.cs 项目: saleyn/clrzmq
        public int Init(int size)
        {
            int rc = LibZmq.zmq_msg_init_size(_ptr, size);

            _initialized = true;

            return(rc);
        }
示例#7
0
文件: ZmqMsgT.cs 项目: saleyn/clrzmq
        public int Close()
        {
            int rc = LibZmq.zmq_msg_close(_ptr);

            _initialized = false;

            return(rc);
        }
示例#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))));
            }
        }
示例#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))));
            }
        }
示例#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)));
            }
        }
示例#11
0
文件: Retry.cs 项目: xuzhe35/clrzmq
        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);
        }
示例#12
0
        private static int RetryIfInterrupted(Func <int> func)
        {
            int rc;

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

            return(rc);
        }
示例#13
0
        public int Initialize()
        {
            try
            {
                ContextHandle = LibZmq.zmq_ctx_new();
            }
            catch (Exception ex)
            {
            }

            return(ContextHandle == IntPtr.Zero ? -1 : 0);
        }
示例#14
0
文件: ZmqMsgT.cs 项目: saleyn/clrzmq
        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);
        }
示例#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);
                }
        }
示例#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;
        }
示例#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);
                }
        }
示例#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);
        }
示例#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);
        }
示例#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;
        }
示例#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);
        }
示例#22
0
 public IntPtr CreateSocket(int socketType)
 {
     return(LibZmq.zmq_socket(ContextHandle, socketType));
 }
示例#23
0
文件: ZmqMsgT.cs 项目: saleyn/clrzmq
 public IntPtr Data()
 {
     return(LibZmq.zmq_msg_data(_ptr));
 }
示例#24
0
        public int RegisterMonitor(LibZmq.MonitorFuncCallback callback)
        {
            MonitorRegistered = true;

            return LibZmq.zmq_ctx_set_monitor(ContextHandle, callback);
        }
示例#25
0
        public int Initialize()
        {
            ContextHandle = LibZmq.zmq_init(ThreadPoolSize);

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

            return LibZmq.zmq_ctx_set_monitor(ContextHandle, _callback);
        }
示例#28
0
 public int Disconnect(string endpoint)
 {
     return LibZmq.zmq_disconnect(SocketHandle, Encoding.ASCII.GetBytes(endpoint));
 }
示例#29
0
 public int Unbind(string endpoint)
 {
     return LibZmq.zmq_unbind(SocketHandle, Encoding.ASCII.GetBytes(endpoint));
 }
示例#30
0
 public int SetContextOption(int option, int value)
 {
     return(LibZmq.zmq_ctx_set(ContextHandle, option, value));
 }
示例#31
0
 public static int GetErrorCode()
 {
     return(LibZmq.zmq_errno());
 }
示例#32
0
文件: ZmqMsgT.cs 项目: saleyn/clrzmq
 public int Size()
 {
     return(LibZmq.zmq_msg_size(_ptr));
 }