public void Return(IZmqSocket socket, string endpoint, bool inError) { if (socket == null) throw new ArgumentNullException("socket"); if (string.IsNullOrEmpty(endpoint)) throw new ArgumentNullException("endpoint"); var endpointPoll = _endpoint2Sockets.GetOrAdd(endpoint, _ => new EndpointPoll()); if (_disposed || inError) { // if (endpointPoll.monitor != null && endpointPoll.monitor.Socket == socket) // { // endpointPoll.monitor.Dispose(); // endpointPoll.monitored = false; // } socket.Dispose(); } this.Untrack(socket); if (_disposed || inError) { return; } var socketQueue = endpointPoll.SocketsQueue; // make available to next one socketQueue.Enqueue(socket); // this may defeat the purpose of a poll (if it puts the SO socket in a time_wait state) // socket.Disconnect(endpoint); }
public void Return(IZmqSocket socket, string endpoint, bool inError) { if (socket == null) { throw new ArgumentNullException("socket"); } if (string.IsNullOrEmpty(endpoint)) { throw new ArgumentNullException("endpoint"); } var endpointPoll = _endpoint2Sockets.GetOrAdd(endpoint, _ => new EndpointPoll()); if (_disposed || inError) { // if (endpointPoll.monitor != null && endpointPoll.monitor.Socket == socket) // { // endpointPoll.monitor.Dispose(); // endpointPoll.monitored = false; // } socket.Dispose(); } this.Untrack(socket); if (_disposed || inError) { return; } var socketQueue = endpointPoll.SocketsQueue; // make available to next one socketQueue.Enqueue(socket); // this may defeat the purpose of a poll (if it puts the SO socket in a time_wait state) // socket.Disconnect(endpoint); }
public virtual void Start() { EnsureNotDisposed(); if (!this._ownSockets) { if (!(this.Frontend is Socket)) { throw new InvalidOperationException("Frontend instance is not a Socket"); } if (!(this.Backend is Socket)) { throw new InvalidOperationException("Backend instance is not a Socket"); } } var thread = new Thread(() => { if (this._ownSockets) { this.Frontend = _ctx.CreateSocket(this._frontendType); this.Backend = _ctx.CreateSocket(this._backendType); } var front = (Socket)this.Frontend; var back = (Socket)this.Backend; StartFrontEnd(); StartBackEnd(); IZmqSocket capReceiver; IZmqSocket captureSink = capReceiver = null; if (this._enableCapture) { var rnd = new Random((int)DateTime.Now.Ticks); var captureendpoint = "inproc://capture" + rnd.Next(0, Int32.MaxValue); captureSink = _ctx.Pair(); captureSink.Bind(captureendpoint); capReceiver = _ctx.Pair(); capReceiver.Connect(captureendpoint); var captureThread = new Thread(() => { try { while (true) { var data = capReceiver.Recv(); if (data == null) { continue; } var ev = this.Captured; if (ev != null) { ev(data); } } } catch (Exception e) { if (LogAdapter.LogEnabled) { LogAdapter.LogError("DeviceCapture", e.ToString()); } } }) { IsBackground = true, Name = "Capture thread for " + captureendpoint }; captureThread.Start(); } var captureHandle = _enableCapture ? captureSink.Handle() : IntPtr.Zero; restart: // this will block forever, hence it's running in a separate thread var res = Native.Device.zmq_proxy(front.Handle(), back.Handle(), captureHandle); if (res == Native.ErrorCode) { if (Native.LastError() == ZmqErrorCode.EINTR) // unix interruption { goto restart; } // force disposal since these sockets were eterm'ed or worse this.Dispose(); if (captureSink != null) { captureSink.Dispose(); } if (capReceiver != null) { capReceiver.Dispose(); } // this is expected if (Native.LastError() == ZmqErrorCode.ETERM) { return; } // not expected var msg = "Error on zmq_proxy: " + Native.LastErrorString(); System.Diagnostics.Trace.TraceError(msg); System.Diagnostics.Debug.WriteLine(msg); if (LogAdapter.LogEnabled) { LogAdapter.LogError(this.GetType().FullName, msg); } } }) { IsBackground = true }; thread.Start(); }
public void Dispose() { _factory.RemoveSocket(this); _socket.Dispose(); }
public void Stop() { _reply.Dispose(); }