Esempio n. 1
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;
        }
Esempio n. 2
0
        public SocketProxy(IntPtr socketHandle)
        {
            if (socketHandle == IntPtr.Zero)
            {
                throw new ArgumentException("Socket handle must be a valid pointer.", "socketHandle");
            }

            SocketHandle = socketHandle;

            _msg = new ZmqMsgT();
            _buffer = Marshal.AllocHGlobal(MaxBufferSize);
        }
Esempio n. 3
0
        public SocketProxy(IntPtr socketHandle)
        {
            if (socketHandle == IntPtr.Zero)
            {
                throw new ArgumentException("Socket handle must be a valid pointer.", "socketHandle");
            }

            SocketHandle = socketHandle;

            _msg = new ZmqMsgT();
            _buffer = Marshal.AllocHGlobal(MaxBufferSize);
        }
Esempio n. 4
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);
        }
Esempio n. 5
0
        /// <summary>
        /// Receive a message in the space provided by msg.
        /// </summary>
        /// <param name="msg">Message is received in this parameter.  The caller is responsible for
        /// closing the message by calling msg.Close()</param>
        /// <param name="flags">Flags to pass to implementation</param>
        /// <returns>Number of bytes received or a negative value in case of an error</returns>
        public int Receive(ZmqMsgT msg, int flags)
        {
            if (msg.Init() == -1)
            {
                return(-1);
            }

            int bytesReceived = Retry.IfInterrupted(LibZmq.zmq_msg_recv.Invoke, msg.Ptr, SocketHandle, flags);

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

            return(bytesReceived);
        }
Esempio n. 6
0
        public SocketProxy(IntPtr socketHandle, Action<IntPtr> socketClosed)
        {
            if (socketHandle == IntPtr.Zero)
            {
                throw new ArgumentException("Socket handle must be a valid pointer.", "socketHandle");
            }

            if (socketClosed == null)
            {
                throw new ArgumentNullException("socketClosed");
            }

            SocketHandle = socketHandle;
            _socketClosed = socketClosed;

            _msg = new ZmqMsgT();
            _buffer = Marshal.AllocHGlobal(MaxBufferSize);
        }
Esempio n. 7
0
        public int Forward(IntPtr destinationHandle)
        {
            using (var message = new ZmqMsgT())
            {
                if (message.Init() == -1)
                {
                    return -1;
                }

                int receiveMore;
                int bytesSent;

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

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

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

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

                return bytesSent;
            }
        }
Esempio n. 8
0
        public int Send(byte[] buffer, int size, int flags)
        {
            using (var message = new ZmqMsgT())
            {
                if (message.Init(size) == -1)
                {
                    return -1;
                }

                if (size > 0)
                {
                    Marshal.Copy(buffer, 0, message.Data(), size);
                }

                int bytesSent = RetryIfInterrupted(() => LibZmq.zmq_msg_send(message, SocketHandle, 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 (message.Close() == -1)
                {
                    return -1;
                }

                return bytesSent;
            }
        }
Esempio n. 9
0
        public byte[] Receive(byte[] buffer, int flags, out int size)
        {
            size = -1;

            using (var message = new ZmqMsgT())
            {
                if (message.Init() == -1)
                {
                    return buffer;
                }

                int bytesReceived = RetryIfInterrupted(() => LibZmq.zmq_msg_recv(message, SocketHandle, flags));

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

                    size = bytesReceived;

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

                    Marshal.Copy(message.Data(), buffer, 0, size);
                }

                if (message.Close() == -1)
                {
                    size = -1;
                }

                return buffer;
            }
        }
Esempio n. 10
0
        public int Receive(byte[] buffer, int flags)
        {
            using (var message = new ZmqMsgT())
            {
                if (message.Init(buffer.Length) == -1)
                {
                    return -1;
                }

                int bytesReceived = RetryIfInterrupted(() => LibZmq.zmq_msg_recv(message, SocketHandle, flags));

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

                if (bytesReceived > 0)
                {
                    Marshal.Copy(message.Data(), buffer, 0, bytesReceived);
                }

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

                return bytesReceived;
            }
        }
Esempio n. 11
0
        /// <summary>
        /// Receive a message in the space provided by msg.
        /// </summary>
        /// <param name="msg">Message is received in this parameter.  The caller is responsible for
        /// closing the message by calling msg.Close()</param>
        /// <param name="flags">Flags to pass to implementation</param>
        /// <returns>Number of bytes received or a negative value in case of an error</returns>
        public int Receive(ZmqMsgT msg, int flags)
        {
            if (msg.Init() == -1)
                return -1;

            int bytesReceived = Retry.IfInterrupted(LibZmq.zmq_msg_recv.Invoke, msg.Ptr, SocketHandle, flags);

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

            return bytesReceived;
        }