예제 #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));
 }