Exemple #1
0
        protected async Task <bool> TryFetchAsync(IRequestTask requestTask, CancellationToken cancellationToken)
        {
            try
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    _logger.LogInformation($"cancel fetch resource!");
                    return(true);
                }

                await _fetcherProvider.GetFetcher(requestTask.GetType()).FetchAsync(requestTask, OnReceivedContent, cancellationToken);
            }
            catch (TaskCanceledException ex)
            {
                _logger.LogInformation($"uri[{requestTask.Uri}] canceled!", ex);
            }
            catch (ObjectDisposedException ex)
            {
                _logger.LogInformation($"uri[{requestTask.Uri}] canceled!", ex);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"uri[{requestTask.Uri}] connect error!");

                return(false);
            }

            return(true);
        }
Exemple #2
0
        public virtual async Task FetchAsync(IRequestTask requestTask, Action <IRequestTask, string> onReceivedContent, CancellationToken cancellationToken)
        {
            if (!TypeUtils.IsClassAssignableFrom(requestTask.GetType(), typeof(HttpRequestTask)))
            {
                throw new ArgumentException($"{nameof(requestTask)} is not http task");
            }

            var httpClient      = _httpClientFactory.CreateClient("default");
            var httpRequestTask = requestTask as HttpRequestTask;
            HttpRequestMessage  httpRequestMessage;
            HttpResponseMessage httpResponse;

            try
            {
                while (true && !cancellationToken.IsCancellationRequested)
                {
                    httpRequestMessage = GenerateHttpRequestMessage(httpRequestTask);
                    httpResponse       = await httpClient.SendAsync(httpRequestMessage, cancellationToken);

                    httpResponse.EnsureSuccessStatusCode();

                    var content = ReadContent(httpResponse, httpRequestTask);

                    onReceivedContent(requestTask, content);

                    if (!httpRequestTask.IsCycleRequest)
                    {
                        httpRequestTask.Status = RequestTaskStatus.Success;
                        break;
                    }

                    await Task.Delay(httpRequestTask.CycleRequestTimeSpan, cancellationToken);
                }
            }
            catch (HttpRequestException hre)
            {
                _logger.LogError(hre, $"HttpMethod[{httpRequestTask.HttpMethod}] Uri[{requestTask.Uri}]  response status error!");
                throw hre;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"receive HttpMethod[{httpRequestTask.HttpMethod}] Uri[{requestTask.Uri}] data error!");
                throw ex;
            }
        }
Exemple #3
0
        protected virtual void OnReceivedContent(IRequestTask requestTask, string content)
        {
            requestTask.TryCount = 0;

            var fetchResult     = new FetchResult(content, requestTask);
            var requestTaskType = requestTask.GetType();

            var processors = _fetchResultProcessorProvider.GetFetchResultProcessors(requestTaskType);

            foreach (var processor in processors)
            {
                processor.Process(fetchResult);
            }

            var pipelines = _pipelineProvider.GetPipelines(requestTaskType);

            foreach (var pipeline in pipelines)
            {
                Task.Factory.StartNew(async() =>
                {
                    await pipeline.HandleAsync(fetchResult);
                });
            }
        }
Exemple #4
0
        public virtual Task FetchAsync(IRequestTask requestTask, Action <IRequestTask, string> onReceivedContent, CancellationToken cancellationToken)
        {
            if (!TypeUtils.IsClassAssignableFrom(requestTask.GetType(), typeof(SocketIORequestTask)))
            {
                throw new ArgumentException($"{nameof(requestTask)} is not socket-io task");
            }

            var socketIORequestTask = requestTask as SocketIORequestTask;

            var client = IO.Socket(socketIORequestTask.Uri, null);

            client.Open();
            try
            {
                bool isDisconnect = false;
                client.On(Socket.EVENT_DISCONNECT, message =>
                {
                    client.Disconnect();
                    isDisconnect = true;
                });

                client.On(Socket.EVENT_ERROR, message =>
                {
                    client.Disconnect();
                    isDisconnect = true;
                });

                foreach (var @event in socketIORequestTask.Events)
                {
                    client.On(@event, message =>
                    {
                        onReceivedContent(requestTask, message.ToString());
                    });
                }

                foreach (var command in socketIORequestTask.Commands)
                {
                    var commandJson = JObject.FromObject(command.Value);
                    client.Emit(command.Key, commandJson);
                }

                while (!isDisconnect)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        client.Disconnect();
                        client.Close();
                        isDisconnect = true;
                        return(Task.CompletedTask);
                    }

                    Thread.Sleep(1000);
                }

                throw new Exception("socket-io disconnected caused by unknown reason.");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"receive socket-io[{requestTask.Uri}] data error!");

                client.Disconnect();
                client.Close();
                throw ex;
            }
        }
Exemple #5
0
        public virtual async Task FetchAsync(IRequestTask requestTask, Action <IRequestTask, string> onReceivedContent, CancellationToken cancellationToken)
        {
            if (!TypeUtils.IsClassAssignableFrom(requestTask.GetType(), typeof(WebSocketRequestTask)))
            {
                throw new ArgumentException($"{nameof(requestTask)} is not websocket task");
            }

            var webSocketRequestTask = requestTask as WebSocketRequestTask;

            var client = new ClientWebSocket();
            await client.ConnectAsync(requestTask.Uri, cancellationToken);

            //send command
            await Task.Factory.StartNew(async() =>
            {
                try
                {
                    await Task.Delay(1000, cancellationToken);
                    foreach (var command in webSocketRequestTask.Commands)
                    {
                        var commandBytes = Encoding.UTF8.GetBytes(command.Value);
                        await client.SendAsync(new ArraySegment <byte>(commandBytes), WebSocketMessageType.Text, true, cancellationToken);
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, $"websocket[{requestTask.Uri}] send command error!");
                }
            });

            //heartbeat
            if (webSocketRequestTask.HeartBeatCommands != null && webSocketRequestTask.HeartBeatCommands.Count > 0)
            {
                await Task.Factory.StartNew(async() =>
                {
                    while (client.State == WebSocketState.Open && !cancellationToken.IsCancellationRequested)
                    {
                        try
                        {
                            foreach (var command in webSocketRequestTask.HeartBeatCommands)
                            {
                                var commandBytes = Encoding.UTF8.GetBytes(command.Value);
                                await client.SendAsync(new ArraySegment <byte>(commandBytes), WebSocketMessageType.Text, true, cancellationToken);
                            }

                            await Task.Delay(TimeSpan.FromSeconds(webSocketRequestTask.HeartBeatInterval));
                        }
                        catch (Exception ex)
                        {
                            _logger.LogError(ex, $"websocket[{requestTask.Uri}] send heartbeat error!");
                        }
                    }
                });
            }

            //receive message
            try
            {
                while (client.State == WebSocketState.Open && !cancellationToken.IsCancellationRequested)
                {
                    var    buffer                 = new ArraySegment <byte>(new Byte[1024 * 16]);
                    string serializedMessage      = null;
                    WebSocketReceiveResult result = null;
                    using (var ms = new MemoryStream())
                    {
                        do
                        {
                            result = await client.ReceiveAsync(buffer, cancellationToken).ConfigureAwait(false);

                            ms.Write(buffer.Array, buffer.Offset, result.Count);
                        }while (!result.EndOfMessage);

                        ms.Seek(0, SeekOrigin.Begin);

                        using (var reader = new StreamReader(ms, Encoding.UTF8))
                        {
                            serializedMessage = await reader.ReadToEndAsync().ConfigureAwait(false);
                        }
                    }

                    if (result.MessageType == WebSocketMessageType.Text)
                    {
                        onReceivedContent(requestTask, serializedMessage);
                    }
                }

                throw new Exception($"websocket disconnected caused by unknown reason, close description[{client.CloseStatusDescription}].");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"receive websocket[{requestTask.Uri}] data error, close description[{client.CloseStatusDescription}].");

                client.Dispose();
                throw;
            }
        }