Esempio n. 1
0
        public Monitor(IZmqSocket socket, IZmqContext context,
                       string monitorName   = null,
                       MonitorEvents events = MonitorEvents.All)
        {
            if (socket == null)
            {
                throw new ArgumentNullException("socket");
            }
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            if (monitorName != null)
            {
                if (!monitorName.StartsWith("inproc://"))
                {
                    monitorName = "inproc://" + monitorName;
                }
            }
            else
            {
                monitorName = "inproc://temp" + Rnd.Next(0, Int32.MaxValue);
            }

            this._monitorName = monitorName;

            // Creates a inproc socket pair
            var res = Native.Monitor.zmq_socket_monitor(socket.Handle(), monitorName, (int)events);

            if (res == Native.ErrorCode)
            {
                Native.ThrowZmqError("Monitor");
            }

            // Connects to the newly created socket pair
            this._pairSocket = context.Pair();
            this._pairSocket.Connect(this._monitorName);

            this._thread = new Thread(EventsWorker)
            {
                IsBackground = true
            };
            this._thread.Start();
        }
Esempio n. 2
0
		public Monitor(IZmqSocket socket, IZmqContext context, 
					   string monitorName = null, 
					   MonitorEvents events = MonitorEvents.All)
		{
			if (socket == null) throw new ArgumentNullException("socket");
			if (context == null) throw new ArgumentNullException("context");

			if (monitorName != null)
			{
				if (!monitorName.StartsWith("inproc://"))
					monitorName = "inproc://" + monitorName;
			}
			else
			{
				monitorName = "inproc://temp" + Rnd.Next(0, Int32.MaxValue);
			}

			this._monitorName = monitorName;

			// Creates a inproc socket pair
			var res = Native.Monitor.zmq_socket_monitor(socket.Handle(), monitorName, (int)events);
			if (res == Native.ErrorCode)
			{
				Native.ThrowZmqError("Monitor");
			}

			// Connects to the newly created socket pair
			this._pairSocket = context.Pair();
			this._pairSocket.Connect(this._monitorName);

			this._thread = new Thread(EventsWorker)
			{
				IsBackground = true
			};
			this._thread.Start();
		}
Esempio n. 3
0
        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();
        }