Exemple #1
0
        public async Task <Exception> ReceiveAllAsync(int timeOutMsec = 1000 * 60 * 5, CancellationToken cancellationToken = default)
        {
            await ConnectAsync(cancellationToken);

            var processingTask = Task.Run(async() => {
                await ProcessAsync(cancellationToken);
            });

            Exception lastException = null;
            var       eot           = false;

            OnEot += () => eot = true;

            while (!cancellationToken.IsCancellationRequested && !eot)
            {
                using var cts = new CancellationTokenSource();
                cts.CancelAfter(timeOutMsec);
                using var combinedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, cts.Token);

                var(message, exception) = await ReceiveAsync(combinedCancellationTokenSource.Token);

                if (exception != null)
                {
                    lastException = exception;
                }

                if (message == null && exception != null)
                {
                    break;
                }
                else
                {
                    await _jsonChannel.Writer.WriteAsync(message, cancellationToken);

                    Interlocked.Increment(ref _jsonChannelSize);
                }
            }
            _jsonChannel.Writer.Complete();

            await processingTask;

            if (lastException != null && !eot)
            {
                _logger?.LogError("Exception on receive", lastException);
            }

            return(lastException);
        }
Exemple #2
0
        public void ProcessMessage(string message)
        {
            if (message is null || message.Length < 3 || message[0] != '!')
            {
                message ??= "<null>";
                if (message.StartsWith("Error", StringComparison.InvariantCultureIgnoreCase))
                {
                    _logger?.LogError(message);
                }
                else if (message.StartsWith("Warn", StringComparison.InvariantCultureIgnoreCase))
                {
                    _logger?.LogWarn(message);
                }
                else
                {
                    _logger?.LogInfo(message);
                }
                return;
            }
            message = message.TrimStart('!');
            try {
                var responseBase = Deserialize <CrawlerResponseBase>(message);
                switch (responseBase.Type)
                {
                case "eot": OnEot?.Invoke(); break;

                case "status": OnStatus?.Invoke(Deserialize <CrawlerResponseStatus>(message)); break;

                case "edges": OnEdges?.Invoke(Deserialize <CrawlerResponseEdges>(message)); break;

                case "node": OnNode?.Invoke(Deserialize <CrawlerResponseNode>(message)); break;

                default: _logger?.LogWarn($"Unknown Message Type `{responseBase.Type ?? "<null>"}`"); break;
                }
            }
            catch (JsonException ex) { _logger?.LogWarn($"invalid json: {message}", ex); }
        }