Exemple #1
0
        public void Start()
        {
            var zmqVersion = ZmqUtil.GetVersion();

            _logger.InfoFormat("Loaded ZMQ v{0}", zmqVersion.ToString(3));

            if (zmqVersion.Major != 4)
            {
                throw new InvalidOperationException($"Expected ZMQ v4.*, loaded ZMQ v{zmqVersion.ToString(3)}");
            }

            _isListening = true;

            _outboundSockets       = new ConcurrentDictionary <PeerId, ZmqOutboundSocket>();
            _outboundSocketActions = new BlockingCollection <OutboundSocketAction>();
            _pendingDisconnects    = new BlockingCollection <PendingDisconnect>();
            _context = new ZmqContext();

            var startSequenceState = new InboundProcStartSequenceState();

            _inboundThread    = BackgroundThread.Start(InboundProc, startSequenceState);
            _outboundThread   = BackgroundThread.Start(OutboundProc);
            _disconnectThread = BackgroundThread.Start(DisconnectProc);

            startSequenceState.Wait();
            _isRunning = true;
        }
Exemple #2
0
        public CodedInputStream?Receive(TimeSpan?timeout = null)
        {
            var receiveTimeout = timeout ?? _options.ReceiveTimeout;

            if (receiveTimeout != _lastReceiveTimeout)
            {
                _socket !.SetOption(ZmqSocketOption.RCVTIMEO, (int)receiveTimeout.TotalMilliseconds);
                _lastReceiveTimeout = receiveTimeout;
            }

            if (_socket !.TryReadMessage(ref _readBuffer, out var messageLength, out var error))
            {
                return(new CodedInputStream(_readBuffer, 0, messageLength));
            }

            // EAGAIN: Non-blocking mode was requested and no messages are available at the moment.
            if (error == ZmqErrorCode.EAGAIN || messageLength == 0)
            {
                return(null);
            }

            throw ZmqUtil.ThrowLastError("ZMQ Receive error");
        }
Exemple #3
0
        public void Disconnect()
        {
            var endpoint = _socket?.GetOptionString(ZmqSocketOption.LAST_ENDPOINT);

            if (endpoint == null)
            {
                return;
            }

            _logger.InfoFormat("Unbinding socket, Inbound Endpoint: {0}", endpoint);
            if (!_socket !.TryUnbind(endpoint))
            {
                _logger.WarnFormat("Socket error, Inbound Endpoint: {0}, Error: {1}", endpoint, ZmqUtil.GetLastErrorMessage());
            }
        }