예제 #1
0
        private void InternalDispose(bool isDispose)
        {
            if (_disposed)
            {
                return;
            }

            if (isDispose)
            {
                GC.SuppressFinalize(this);
            }

            _disposed = true;

            TryCancelLinger();

            var res = Native.Socket.zmq_close(this.SocketPtr);

            if (res == Native.ErrorCode)
            {
                // we cannot throw in dispose.
                var msg = "Error disposing socket: " + Native.LastErrorString();
                System.Diagnostics.Trace.TraceError(msg);
                System.Diagnostics.Debug.WriteLine(msg);
                if (LogAdapter.LogEnabled)
                {
                    LogAdapter.LogError("Socket", msg);
                }
            }

#if DEBUG
            _context.Untrack(this);
#endif
        }
예제 #2
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);
        }
예제 #3
0
        private void InternalDispose(bool isDispose)
        {
            if (this._disposed)
            {
                return;
            }

            if (isDispose)
            {
                GC.SuppressFinalize(this);
            }

            this._disposed = true;

            if (this._contextPtr != IntPtr.Zero)
            {
                var ev = this.Disposing;
                if (ev != null)
                {
                    // Notifying of pre-context-termination
                    ev();
                }

                Native.Context.zmq_ctx_shutdown(this._contextPtr);                 // discard any error

#if DEBUG
                var message = this.GetTrackedSockets()
                              .Aggregate("", (prev, tuple) => prev + "Socket " + tuple.Item1.SocketType + " at " + tuple.Item2 + Environment.NewLine);

                System.Diagnostics.Trace.TraceError("Socket tracking: " + message);
                System.Diagnostics.Debug.WriteLine("Socket tracking: " + message);
                if (LogAdapter.LogEnabled)
                {
                    LogAdapter.LogError("Context", "Socket tracking: " + message);
                }

                var t = new System.Threading.Thread(() =>
                {
                    Thread.Sleep(2000);

                    var message2 =
                        this.GetTrackedSockets()
                        .Aggregate("", (prev, tuple) => prev + "Socket " + tuple.Item1.SocketType + " at " + tuple.Item2 + Environment.NewLine);

                    System.Diagnostics.Trace.TraceError("Socket tracking: " + message2);
                    System.Diagnostics.Debug.WriteLine("Socket tracking: " + message2);
                    if (LogAdapter.LogEnabled)
                    {
                        LogAdapter.LogError("Context", "**** STILL Hanging **** - Socket tracking: " + message2);
                    }
                });
                t.Start();
#endif

                var error = Native.Context.zmq_ctx_term(this._contextPtr);
                if (error == Native.ErrorCode)
                {
                    // Not good, but we can't throw an exception in the Dispose
                    var msg = "Error disposing context: " + Native.LastErrorString();
                    System.Diagnostics.Trace.TraceError(msg);
                    System.Diagnostics.Debug.WriteLine(msg);

                    if (LogAdapter.LogEnabled)
                    {
                        LogAdapter.LogError("Context", msg);
                    }
                }

                ev = this.Disposed;
                if (ev != null)
                {
                    // Notifying of post-context-termination
                    ev();
                }
            }
        }
예제 #4
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");
            }
        }
예제 #5
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);
        }