Esempio n. 1
0
        public void Start()
        {
            _reply.Bind(_endpoint);

            var t = new Thread((_) =>
            {
                try
                {
                    while (true)
                    {
                        var data = _reply.Recv();

                        if (data != null)
                        {
                            OnRequestReceived(data, _reply);
                        }
                    }
                }
                catch (Exception e)
                {
                    LogAdapter.LogError("Listener", e.ToString());
                }
            })
            {
                IsBackground = true
            };

            t.Start();
        }
Esempio n. 2
0
        public static void Bind(this IZmqSocket source, Transport transport, string address, uint port, int timeout = Socket.InfiniteTimeout)
        {
            var endpoint = BuildEndpoint(transport, address, port);

            if (timeout != Socket.InfiniteTimeout)
            {
                source.SetOption(SocketOpt.RCVTIMEO, timeout);
            }

            source.Bind(endpoint);
        }
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();
        }
Esempio n. 4
0
 public void Bind(string address)
 {
     _socket.Bind(address);
 }