示例#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);
        }
示例#2
0
文件: NN.cs 项目: zvikara/NNanomsg
        /// <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);
        }
示例#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));
            }
        }
示例#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);
        }
示例#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);
        }
示例#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;
 }
示例#7
0
 public int Recv(byte[] buf, SendRecvFlags flags)
 {
     var rc = NN.Recv(_socket, buf, flags);
     if (rc < 0)
     {
         throw new NanomsgException();
     }
     return rc;
 }
示例#8
0
文件: NN.cs 项目: Wolfium/NNanomsg
 public static int Send(int s, byte[] buf, SendRecvFlags flags)
 {
     return Interop.nn_send(s, buf, buf.Length, (int)flags);
 }
示例#9
0
文件: NN.cs 项目: Wolfium/NNanomsg
        /// <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;
        }
示例#10
0
文件: NN.cs 项目: Wolfium/NNanomsg
 /// <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);
 }
示例#11
0
文件: NN.cs 项目: zvikara/NNanomsg
 /// <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));
 }
示例#12
0
文件: NN.cs 项目: zvikara/NNanomsg
 public static int Send(int s, byte[] buf, SendRecvFlags flags)
 {
     return(Interop.nn_send(s, buf, buf.Length, (int)flags));
 }
示例#13
0
 internal static int Send(int s, byte[] buf, SendRecvFlags flags)
 {
     return(USend(s, buf, buf.Length, (int)flags));
 }