コード例 #1
0
        private bool SendOne(byte[] data, SendFlags flags)
        {
            Contract.Requires(!Disposed);
            Contract.Requires(data != null);

            if (C.zmq_msg_init_size(m_msg, data.Length) != 0)
            {
                throw ZeroMQException.CurrentError();
            }

            Marshal.Copy(data, 0, C.zmq_msg_data(m_msg), data.Length);
            int rc = C.zmq_send(m_sock, m_msg, (int)flags);

            C.zmq_msg_close(m_msg);

            if (rc == 0)
            {
                return(true);
            }
            else if (C.zmq_errno() == C.EAGAIN)
            {
                return(false);
            }
            else
            {
                throw ZeroMQException.CurrentError();
            }
        }
コード例 #2
0
        /// <summary>
        /// Disposes of the object.
        /// </summary>
        /// <param name="disposing">Whether to dispose of managed resources.</param>
        protected virtual void Dispose(bool disposing)
        {
            Contract.Ensures(Disposed);

            try
            {
                if (m_msg != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(m_msg);
                    m_msg = IntPtr.Zero;
                }

                if (m_sock != IntPtr.Zero)
                {
                    int rc = C.zmq_close(m_sock);
                    m_sock = IntPtr.Zero;

                    if (rc != 0)
                    {
                        throw ZeroMQException.CurrentError();
                    }
                }
            }
            finally
            {
                Disposed = true;
            }
        }
コード例 #3
0
        /// <summary>
        /// Sets socket options.
        /// </summary>
        /// <param name="option">Option to set.</param>
        /// <param name="value">Value to set the option to.</param>
        public void SetOption(SocketOption option, byte[] value)
        {
            Contract.Requires(!Disposed);
            Contract.Requires(option == SocketOption.SocketIdentity ||
                              option == SocketOption.Subscribe ||
                              option == SocketOption.Unsubscribe);
            Contract.Requires(value != null);
            Contract.Requires(option != SocketOption.SocketIdentity || (value.Length <= 255 && value.Length > 0));

            //Automatic marshalling of the byte array doesn't quite seem to work.

            IntPtr valuePtr = Marshal.AllocHGlobal(value.Length);

            try
            {
                Marshal.Copy(value, 0, valuePtr, value.Length);

                if (C.zmq_setsockopt(m_sock, (int)option, valuePtr, new C.size_t( value.Length )) != 0)
                {
                    throw ZeroMQException.CurrentError();
                }
            }
            finally
            {
                Marshal.FreeHGlobal(valuePtr);
            }
        }
コード例 #4
0
        /// <summary>
        /// Connects the socket to an address.
        /// </summary>
        /// <param name="addr"></param>
        public void Connect(string addr)
        {
            Contract.Requires(!Disposed);
            Contract.Requires(!string.IsNullOrEmpty(addr));

            if (C.zmq_connect(m_sock, addr) != 0)
            {
                throw ZeroMQException.CurrentError();
            }
        }
コード例 #5
0
        /// <summary>
        /// Sets socket options.
        /// </summary>
        /// <param name="option">Option to set.</param>
        /// <param name="value">Value to set the option to.</param>
        public void SetOption(SocketOption option, long value)
        {
            Contract.Requires(!Disposed);
            Contract.Requires(option == SocketOption.SwapSize);

            if (C.zmq_setsockopt(m_sock, (int)option, ref value, new C.size_t( Marshal.SizeOf(value))) != 0)
            {
                throw ZeroMQException.CurrentError();
            }
        }
コード例 #6
0
ファイル: Context.cs プロジェクト: jcranendonk/ZeroMQ.Net
        /// <summary>
        /// Creates a ØMQ context without specifying thread pool size, for use with the inproc transport.
        /// </summary>
        public Context()
        {
            Disposed = false;

            m_ptr = C.zmq_init(0);

            if (m_ptr == IntPtr.Zero)
            {
                throw ZeroMQException.CurrentError();
            }
        }
コード例 #7
0
        /// <summary>
        /// Sets socket options.
        /// </summary>
        /// <param name="option">Option to set.</param>
        /// <param name="value">Value to set the option to.</param>
        public void SetOption(SocketOption option, int value)
        {
            Contract.Requires(!Disposed);
            Contract.Requires(option == SocketOption.Linger ||
                              option == SocketOption.ReconnectInterval ||
                              option == SocketOption.ConnectionBacklog ||
                              option == SocketOption.ReconnectIntervalMax);

            if (C.zmq_setsockopt(m_sock, (int)option, ref value, new C.size_t( Marshal.SizeOf(value))) != 0)
            {
                throw ZeroMQException.CurrentError();
            }
        }
コード例 #8
0
        /// <summary>
        /// Sets socket options.
        /// </summary>
        /// <param name="option">Option to set.</param>
        /// <param name="value">Value to set the option to.</param>
        public void SetOption(SocketOption option, ulong value)
        {
            Contract.Requires(!Disposed);
            Contract.Requires(option == SocketOption.HighWaterMark ||
                              option == SocketOption.ThreadAffinity ||
                              option == SocketOption.SendBuffer ||
                              option == SocketOption.ReceiveBuffer);

            if (C.zmq_setsockopt(m_sock, (int)option, ref value, new C.size_t( Marshal.SizeOf(value))) != 0)
            {
                throw ZeroMQException.CurrentError();
            }
        }
コード例 #9
0
        /// <summary>
        /// Sets multicast socket options.
        /// </summary>
        /// <param name="option">Option to set.</param>
        /// <param name="value">Value to set the option to.</param>
        public void SetOption(MulticastOption option, long value)
        {
            Contract.Requires(!Disposed);
            Contract.Requires(option == MulticastOption.DataRate ||
                              option == MulticastOption.RecoveryInterval ||
                              option == MulticastOption.RecoveryIntervalMS ||
                              option == MulticastOption.Loopback);

            if (C.zmq_setsockopt(m_sock, (int)option, ref value, new C.size_t( Marshal.SizeOf(value))) != 0)
            {
                throw ZeroMQException.CurrentError();
            }
        }
コード例 #10
0
ファイル: Context.cs プロジェクト: jcranendonk/ZeroMQ.Net
        /// <summary>
        /// Creates a ØMQ context with a specified thread pool size.
        /// </summary>
        /// <param name="io_threads">Size of the ØMQ thread pool to handle I/O operations. Must be at least zero.</param>
        public Context(int io_threads)
        {
            Contract.Requires(io_threads >= 0, "Thread pool size must be at least 0.");

            Disposed = false;

            m_ptr = C.zmq_init(io_threads);

            if (m_ptr == IntPtr.Zero)
            {
                throw ZeroMQException.CurrentError();
            }
        }
コード例 #11
0
        private ulong GetOptionUInt64(int option)
        {
            Contract.Requires(!Disposed);

            ulong value;

            C.size_t len = new C.size_t( Marshal.SizeOf(typeof(ulong)));

            if (C.zmq_getsockopt(m_sock, option, out value, ref len) != 0)
            {
                throw ZeroMQException.CurrentError();
            }

            return(value);
        }
コード例 #12
0
        private string GetOptionString(int option)
        {
            Contract.Requires(!Disposed);

            C.size_t baLen = new C.size_t( 255 );
            IntPtr   baPtr = Marshal.AllocHGlobal(baLen.ToInt32());

            //Automatic marshalling of the byte array doesn't quite seem to work.

            if (C.zmq_getsockopt(m_sock, option, baPtr, ref baLen) != 0)
            {
                throw ZeroMQException.CurrentError();
            }

            byte[] baValue = new byte[baLen.ToInt32()];
            Marshal.Copy(baPtr, baValue, 0, baValue.Length);

            return(Encoding.ASCII.GetString(baValue));
        }
コード例 #13
0
ファイル: Context.cs プロジェクト: jcranendonk/ZeroMQ.Net
        /// <summary>
        /// Creates a socket within the current context.
        /// </summary>
        /// <param name="type">The socket type.</param>
        /// <param name="lingerTime">Linger period for socket shutdown, in ms, or <see cref="System.Threading.Timeout.Infinite"/> for no linger.</param>
        /// <returns>An unbound socket.</returns>
        public Socket CreateSocket(SocketType type, int lingerTime = DEFAULT_LINGER)
        {
            Contract.Requires(!Disposed);
            Contract.Ensures(Contract.Result <Socket>() != null);

            IntPtr socket = C.zmq_socket(m_ptr, (int)type);

            if (socket == IntPtr.Zero)
            {
                throw ZeroMQException.CurrentError();
            }

            Socket sock = new Socket(socket);

            if (lingerTime >= 0)
            {
                sock.SetOption(SocketOption.Linger, lingerTime);
            }

            return(sock);
        }
コード例 #14
0
ファイル: Context.cs プロジェクト: jcranendonk/ZeroMQ.Net
        /// <summary>
        /// Disposes of the object.
        /// </summary>
        /// <param name="disposing">Whether to dispose of managed resources.</param>
        protected virtual void Dispose(bool disposing)
        {
            Contract.Ensures(Disposed);

            try
            {
                if (m_ptr != IntPtr.Zero)
                {
                    int rc = C.zmq_term(m_ptr);
                    m_ptr = IntPtr.Zero;

                    if (rc != 0)
                    {
                        throw ZeroMQException.CurrentError();
                    }
                }
            }
            finally
            {
                Disposed = true;
            }
        }
コード例 #15
0
        /// <summary>
        /// Attempts to receive data from the socket with flags set.
        /// </summary>
        /// <param name="data"></param>
        /// <param name="flags"></param>
        /// <returns>True if data was received; otherwise, false, and <paramref name="data"/> will be an empty array.</returns>
        public bool TryReceive(out byte[] data, ReceiveFlags flags)
        {
            Contract.Requires(!Disposed);
            Contract.Ensures(Contract.ValueAtReturn(out data) != null);

            if (C.zmq_msg_init(m_msg) != 0)
            {
                throw ZeroMQException.CurrentError();
            }

            int rc = C.zmq_recv(m_sock, m_msg, (int)flags);

            if (rc == 0)
            {
                data = new byte[C.zmq_msg_size(m_msg)];
                Marshal.Copy(C.zmq_msg_data(m_msg), data, 0, data.Length);
            }
            else
            {
                data = new byte[0];
            }

            C.zmq_msg_close(m_msg);

            if (rc == 0)
            {
                return(true);
            }
            else if (C.zmq_errno() == C.EAGAIN)
            {
                return(false);
            }
            else
            {
                throw ZeroMQException.CurrentError();
            }
        }