Exemplo n.º 1
0
        private void InternalDispose(bool isDispose)
        {
            if (_disposed)
            {
                return;
            }

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

            _disposed = true;

            if (isDispose)
            {
                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
        }
Exemplo n.º 2
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();
                }
            }
        }
Exemplo n.º 3
0
        private void EventsWorker()
        {
            try
            {
                while (!_disposed)
                {
                    var binary = _pairSocket.Recv();

                    if (binary == null || binary.Length == 0)
                    {
                        continue;
                    }

                    var ev  = (MonitorEvents)BitConverter.ToUInt16(binary, 0);
                    int val = 0;

                    if (binary.Length > sizeof(UInt16))
                    {
                        val = BitConverter.ToInt32(binary, sizeof(UInt16));
                    }

                    ZmqError error   = null;
                    var      address = "";

                    switch (ev)
                    {
                    case MonitorEvents.BindFailed:
                    case MonitorEvents.AcceptFailed:
                    case MonitorEvents.CloseFailed:
                        address = _pairSocket.RecvString();
                        error   = new ZmqError(val);
                        break;

                    case MonitorEvents.MonitorStopped:
                        break;

                    default:
                        address = _pairSocket.RecvString();
                        break;
                    }

                    FireEvent(ev, address, error);
                }
            }
            catch (ZmqException ex)
            {
                if (ex.ZmqErrorCode != ZmqErrorCode.ETERM)
                {
                    if (LogAdapter.LogEnabled)
                    {
                        LogAdapter.LogError("Monitor", ex.ToString());
                    }
                }
            }
            catch (Exception e)
            {
                if (LogAdapter.LogEnabled)
                {
                    LogAdapter.LogError("Monitor", e.ToString());
                }
            }
            finally
            {
                this.Dispose();
            }
        }