Ejemplo n.º 1
0
        private byte[] Receive(SendRecvFlags flags)
        {
            IntPtr buffer = IntPtr.Zero;
            int    rc     = Interop.nn_recv(_socket, ref buffer, Constants.NN_MSG, (int)flags);

            if (rc < 0 || buffer == null)
            {
                return(null);
            }

            byte[] output = new byte[rc];
            try
            {
                Marshal.Copy(buffer, output, 0, rc);
            }
            finally
            {
                rc = Interop.nn_freemsg(buffer);
                if (rc != 0)
                {
                    throw new NanomsgException("freemsg");
                }
            }

            return(output);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Receives a message of unknown length into a new buffer.
        /// </summary>
        /// <returns>
        ///     The error code from nn_freemsg. Should always be 0. Probably safe to ignore.
        /// </returns>
        /// <remarks>
        ///     This is a bit inefficient at the moment.
        /// </remarks>
        public static int Recv(int s, out byte[] buf, SendRecvFlags flags)
        {
            IntPtr buffer = IntPtr.Zero;
            int    rc     = Interop.nn_recv(s, ref buffer, Constants.NN_MSG, (int)flags);

            if (rc < 0)
            {
                buf = null;
                return(rc);
            }

            int numberOfBytes = rc;

            // this inefficient, I'm sure there must be a better way.
            buf = new byte[numberOfBytes];
            for (int i = 0; i < numberOfBytes; ++i)
            {
                buf[i] = Marshal.ReadByte(buffer, i);
            }

            int rc_free = Interop.nn_freemsg(buffer);

            Debug.Assert(rc_free == 0);

            return(rc);
        }
Ejemplo n.º 3
0
        protected int SendStreamImpl(NanomsgWriteStream stream, SendRecvFlags flags)
        {
            unsafe
            {
                int        bufferCount = stream.PageCount;
                nn_iovec * iovec       = stackalloc nn_iovec[bufferCount];
                nn_msghdr *hdr         = stackalloc nn_msghdr[1];

                var buffer = stream.FirstPage();
                int i      = 0;
                do
                {
                    iovec[i].iov_len  = buffer.Length;
                    iovec[i].iov_base = (void *)buffer.Buffer;
                    buffer            = stream.NextPage(buffer);
                } while (buffer.Buffer != IntPtr.Zero && i++ < bufferCount);

                (*hdr).msg_control    = null;
                (*hdr).msg_controllen = 0;
                (*hdr).msg_iov        = iovec;
                (*hdr).msg_iovlen     = bufferCount;

                return(Interop.nn_sendmsg(SocketID, hdr, (int)flags));
            }
        }
Ejemplo n.º 4
0
        internal static byte[] Receive(int s, SendRecvFlags flags)
        {
            IntPtr buffer = IntPtr.Zero;
            int    rc     = UReceive(s, ref buffer, Constants.NN_MSG, (int)flags);

            //Console.WriteLine($"Receive: {rc}");

            if (rc < 0 || buffer == null)
            {
                return(null);
            }

            byte[] buf = new byte[rc];

            try
            {
                Marshal.Copy(buffer, buf, 0, rc);
            }
            finally
            {
                int rc_free = UFreeMessage(buffer);
                if (rc_free != 0)
                {
                    throw new InvalidOperationException("Cannot free message!");
                }
            }

            return(buf);
        }
Ejemplo n.º 5
0
        private NanomsgReadStream ReceiveStream(SendRecvFlags flags)
        {
            IntPtr buffer = IntPtr.Zero;
            int    rc     = Interop.nn_recv(_socket, ref buffer, Constants.NN_MSG, (int)flags);

            if (rc < 0 || buffer == null)
            {
                return(null);
            }

            /*
             * In order to prevent managed allocations per receive, we attempt to recycle stream objects.  This
             * will work optimally if the stream is disposed before the next receive call, as in this case each
             * socket class will always reuse the same stream.
             *
             * Disposing the stream will both release its nanomsg-allocated native buffer and return it to its
             * socket class for reuse.
             */

            var stream = Interlocked.Exchange(ref _recycledReadStream, null);

            if (stream != null)
            {
                stream.Reinitialize(buffer, rc);
            }
            else
            {
                stream = new NanomsgReadStream(buffer, rc,
                                               _freeReadDisposer ?? (_freeReadDisposer = new NanomsgNativeDisposer()
                {
                    Socket = (NanomsgSocketBase)this
                }));
            }

            return(stream);
        }
Ejemplo n.º 6
0
 public byte[] Recv(SendRecvFlags flags)
 {
     byte[] buf;
     var rc = NN.Recv(_socket, out buf, flags);
     if (rc < 0)
     {
         throw new NanomsgException();
     }
     return buf;
 }
Ejemplo n.º 7
0
 public int Recv(byte[] buf, SendRecvFlags flags)
 {
     var rc = NN.Recv(_socket, buf, flags);
     if (rc < 0)
     {
         throw new NanomsgException();
     }
     return rc;
 }
Ejemplo n.º 8
0
 public static int Send(int s, byte[] buf, SendRecvFlags flags)
 {
     return Interop.nn_send(s, buf, buf.Length, (int)flags);
 }
Ejemplo n.º 9
0
        /// <summary>
        ///     Receives a message of unknown length into a new buffer.
        /// </summary>
        /// <returns>
        ///     The error code from nn_freemsg. Should always be 0. Probably safe to ignore.
        /// </returns>
        /// <remarks>
        ///     This is a bit inefficient at the moment.
        /// </remarks>
        public static int Recv(int s, out byte[] buf, SendRecvFlags flags)
        {
            IntPtr buffer = IntPtr.Zero;
            int rc = Interop.nn_recv(s, ref buffer, Constants.NN_MSG, (int)flags);

            if (rc < 0)
            {
                buf = null;
                return rc;
            }

            int numberOfBytes = rc;

            // this inefficient, I'm sure there must be a better way.
            buf = new byte[numberOfBytes];
            for (int i = 0; i < numberOfBytes; ++i)
            {
                buf[i] = Marshal.ReadByte(buffer, i);
            }

            int rc_free = Interop.nn_freemsg(buffer);
            Debug.Assert(rc_free == 0);

            return rc;
        }
Ejemplo n.º 10
0
 /// <summary>
 ///     Receives a message into an already allocated buffer
 ///     If the message is longer than the allocated buffer, it is truncated.
 /// </summary>
 /// <returns>
 ///     The length, in bytes, of the message.
 /// </returns>
 public static int Recv(int s, byte[] buf, SendRecvFlags flags)
 {
     return Interop.nn_recv_array(s, buf, buf.Length, (int)flags);
 }
Ejemplo n.º 11
0
 /// <summary>
 ///     Receives a message into an already allocated buffer
 ///     If the message is longer than the allocated buffer, it is truncated.
 /// </summary>
 /// <returns>
 ///     The length, in bytes, of the message.
 /// </returns>
 public static int Recv(int s, byte[] buf, SendRecvFlags flags)
 {
     return(Interop.nn_recv_array(s, buf, buf.Length, (int)flags));
 }
Ejemplo n.º 12
0
 public static int Send(int s, byte[] buf, SendRecvFlags flags)
 {
     return(Interop.nn_send(s, buf, buf.Length, (int)flags));
 }
Ejemplo n.º 13
0
 internal static int Send(int s, byte[] buf, SendRecvFlags flags)
 {
     return(USend(s, buf, buf.Length, (int)flags));
 }