Esempio n. 1
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);
        }
Esempio n. 2
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);
        }
Esempio n. 3
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);
        }