示例#1
0
        public byte[] Recv(bool noWait = false)
        {
            EnsureNotDisposed();

            using (var frame = new MsgFrame())
            {
                var flags = noWait ? Native.Socket.DONTWAIT : 0;

again:
                var res = Native.MsgFrame.zmq_msg_recv(frame._msgPtr, this.SocketPtr, flags);

                if (res == Native.ErrorCode)
                {
                    var error = Native.LastError();

                    if (error == ZmqErrorCode.EAGAIN || error == ZmqErrorCode.EINTR)
                    {
                        goto again;
                    }

                    if (LogAdapter.LogEnabled)
                    {
                        LogAdapter.LogError("Socket", "Recv interrupted with " + error + " - " + Native.LastErrorString(error));
                    }

                    Native.ThrowZmqError(error, "Recv");
                }
                else
                {
                    return(frame.ToBytes());
                }
            }

            return(null);
        }
示例#2
0
        public void Send(byte[] buffer, bool hasMoreToSend = false, bool noWait = false)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            EnsureNotDisposed();

            var flags = hasMoreToSend ? Native.Socket.SNDMORE : 0;

            flags += noWait ? Native.Socket.DONTWAIT : 0;

            var len = buffer.Length;

again:
            var res = Native.Socket.zmq_send(this.SocketPtr, buffer, len, flags);

            // for now we're treating EAGAIN as error.
            // not sure that's OK
            if (res == Native.ErrorCode)
            {
                var error = Native.LastError();
                if (error == ZmqErrorCode.EINTR || error == ZmqErrorCode.EAGAIN)
                {
                    goto again;
                }

                if (LogAdapter.LogEnabled)
                {
                    LogAdapter.LogError("Socket", "Send interrupted with " + error + " - " + Native.LastErrorString(error));
                }

                Native.ThrowZmqError("Send");
            }
        }
示例#3
0
        public bool Poll(int timeout)
        {
            int res = 0;

            try
            {
                if (!Context.IsMono && Environment.Is64BitProcess)
                {
                    res = Native.Poll.zmq_poll_x64(_items_x64, _items_x64.Length, timeout);

                    if (res == 0)
                    {
                        return(false);                              // nothing happened
                    }
                    if (res > 0)
                    {
                        this.InternalFireEvents64(_items_x64);
                    }
                }
                else
                {
                    res = Native.Poll.zmq_poll_x86(_items_x86, _items_x86.Length, timeout);

                    if (res == 0)
                    {
                        return(false);                              // nothing happened
                    }
                    if (res > 0)
                    {
                        this.InternalFireEvents86(_items_x86);
                    }
                }

                if (res == Native.ErrorCode)
                {
                    var error = Native.LastError();

                    if (error != ZmqErrorCode.EINTR)                     // Unix system interruption
                    {
                        Native.ThrowZmqError();
                    }
                }
            }
            catch (SEHException zex)             // rare, but may happen if the endpoint disconnects uncleanly
            {
                var msg = "Error polling socket(s): " + Native.LastErrorString() + " Inner: " + zex.InnerException;
                System.Diagnostics.Trace.TraceError(msg);
                System.Diagnostics.Debug.WriteLine(msg);
                if (LogAdapter.LogEnabled)
                {
                    LogAdapter.LogError(this.GetType().FullName, msg);
                }
            }
            return(res > 0);
        }
示例#4
0
        public void Connect(string endpoint)
        {
            if (string.IsNullOrEmpty(endpoint))
            {
                throw new ArgumentNullException("endpoint");
            }
            EnsureNotDisposed();

            var res = Native.Socket.zmq_connect(this.SocketPtr, endpoint);

            if (res == Native.ErrorCode)
            {
                if (Native.LastError() == 128)
                {
                    System.Diagnostics.Debugger.Break();
                }
                Native.ThrowZmqError("Connecting " + endpoint);
            }
        }