Example #1
0
        internal virtual byte[] Receive(byte[] buffer, SocketFlags flags, out int size)
        {
            EnsureNotDisposed();

            if (buffer == null)
            {
                buffer = new byte[0];
            }

            buffer = _socketProxy.Receive(buffer, (int)flags, out size);

            if (size >= 0)
            {
                ReceiveStatus = ReceiveStatus.Received;
                return(buffer);
            }

            if (ErrorProxy.ShouldTryAgain)
            {
                ReceiveStatus = ReceiveStatus.TryAgain;
                return(buffer);
            }

            if (ErrorProxy.ContextWasTerminated)
            {
                ReceiveStatus = ReceiveStatus.Interrupted;
                return(buffer);
            }

            throw new ZmqSocketException(ErrorProxy.GetLastError());
        }
Example #2
0
 private void SetContextOption(ContextOption option, int value)
 {
     if (_contextProxy.SetContextOption((int)option, value) == -1)
     {
         throw new ZmqException(ErrorProxy.GetLastError());
     }
 }
Example #3
0
        internal virtual int Receive(byte[] buffer, SocketFlags flags)
        {
            EnsureNotDisposed();

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

            int receivedBytes = _socketProxy.Receive(buffer, (int)flags);

            if (receivedBytes >= 0)
            {
                ReceiveStatus = ReceiveStatus.Received;
                return(receivedBytes);
            }

            if (ErrorProxy.ShouldTryAgain)
            {
                ReceiveStatus = ReceiveStatus.TryAgain;
                return(-1);
            }

            if (ErrorProxy.ContextWasTerminated)
            {
                ReceiveStatus = ReceiveStatus.Interrupted;
                return(-1);
            }

            throw new ZmqSocketException(ErrorProxy.GetLastError());
        }
Example #4
0
 private static void HandleProxyResult(int result)
 {
     // Context termination (ETERM) is an allowable error state, occurring when the
     // ZmqContext was terminated during a socket method.
     if (result == -1 && !ErrorProxy.ContextWasTerminated)
     {
         throw new ZmqSocketException(ErrorProxy.GetLastError());
     }
 }
Example #5
0
 private static void ContinueIfInterrupted()
 {
     // An error value of EINTR indicates that the operation was interrupted
     // by delivery of a signal before any events were available. This is a recoverable
     // error, so try polling again for the remaining amount of time in the timeout.
     if (!ErrorProxy.ThreadWasInterrupted)
     {
         throw new ZmqSocketException(ErrorProxy.GetLastError());
     }
 }
Example #6
0
        /// <summary>
        /// Create a <see cref="ZmqContext"/> instance.
        /// </summary>
        /// <returns>A <see cref="ZmqContext"/> instance with the default thread pool size (1).</returns>
        public static ZmqContext Create()
        {
            var contextProxy = new ContextProxy();

            if (contextProxy.Initialize() == -1)
            {
                throw new ZmqException(ErrorProxy.GetLastError());
            }

            return(new ZmqContext(contextProxy));
        }
Example #7
0
        /// <summary>
        /// Forwards a single-part or all parts of a multi-part message to a destination socket.
        /// </summary>
        /// <remarks>
        /// This method is useful for implementing devices as data is not marshalled into managed code; it
        /// is forwarded directly in the unmanaged layer. As an example, this method could forward all traffic
        /// from a device's front-end socket to its backend socket.
        /// </remarks>
        /// <param name="destination">A <see cref="ZmqSocket"/> that will receive the incoming message(s).</param>
        public void Forward(ZmqSocket destination)
        {
            if (destination == null)
            {
                throw new ArgumentNullException("destination");
            }

            if (_socketProxy.Forward(destination.SocketHandle) == -1)
            {
                throw new ZmqSocketException(ErrorProxy.GetLastError());
            }
        }
Example #8
0
        private TSocket CreateSocket <TSocket>(Func <SocketProxy, TSocket> constructor, SocketType socketType) where TSocket : ZmqSocket
        {
            EnsureNotDisposed();

            IntPtr socketHandle = _contextProxy.CreateSocket((int)socketType);

            if (socketHandle == IntPtr.Zero)
            {
                throw new ZmqException(ErrorProxy.GetLastError());
            }

            return(constructor(new SocketProxy(socketHandle)));
        }
Example #9
0
        /// <summary>
        /// Create a <see cref="ZmqContext"/> instance.
        /// </summary>
        /// <param name="threadPoolSize">Number of threads to use in the ZMQ thread pool.</param>
        /// <returns>A <see cref="ZmqContext"/> instance with the specified thread pool size.</returns>
        public static ZmqContext Create(int threadPoolSize)
        {
            if (threadPoolSize < 0)
            {
                throw new ArgumentOutOfRangeException("threadPoolSize", threadPoolSize, "Thread pool size must be non-negative.");
            }

            var contextProxy = new ContextProxy(threadPoolSize);

            if (contextProxy.Initialize() == -1)
            {
                throw new ZmqException(ErrorProxy.GetLastError());
            }

            return(new ZmqContext(contextProxy));
        }
Example #10
0
        /// <summary>
        /// Queue a message buffer to be sent by the socket in blocking mode.
        /// </summary>
        /// <param name="buffer">A <see cref="byte"/> array that contains the message to be sent.</param>
        /// <param name="size">The size of the message to send.</param>
        /// <param name="flags">A combination of <see cref="SocketFlags"/> values to use when sending.</param>
        /// <returns>The number of bytes sent by the socket.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="size"/> is a negative value or is larger than the length of <paramref name="buffer"/>.</exception>
        /// <exception cref="ZmqSocketException">An error occurred sending data to a remote endpoint.</exception>
        /// <exception cref="ObjectDisposedException">The <see cref="ZmqSocket"/> has been closed.</exception>
        /// <exception cref="NotSupportedException">The current socket type does not support Send operations.</exception>
        public virtual int Send(byte[] buffer, int size, SocketFlags flags)
        {
            EnsureNotDisposed();

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

            if (size < 0 || size > buffer.Length)
            {
                throw new ArgumentOutOfRangeException("size", "Expected a non-negative value less than or equal to the buffer length.");
            }

            int sentBytes = _socketProxy.Send(buffer, size, (int)flags);

            if (sentBytes >= 0)
            {
                SendStatus = (sentBytes == size || LibZmq.MajorVersion < LatestVersion) ? SendStatus.Sent : SendStatus.Incomplete;
                return(sentBytes);
            }

            if (ErrorProxy.ShouldTryAgain)
            {
                SendStatus = SendStatus.TryAgain;
                return(-1);
            }

            if (ErrorProxy.ContextWasTerminated)
            {
                SendStatus = SendStatus.Interrupted;
                return(-1);
            }

            throw new ZmqSocketException(ErrorProxy.GetLastError());
        }