Esempio n. 1
0
        /// <summary>
        /// 转发
        /// </summary>
        /// <returns></returns>
        private async Task Relay(ClientModel clientModel, SocketClient socketClient, Log log, string message)
        {
            RequestModel  request  = null;
            ResponseModel response = null;

            #region 解析消息
            try
            {
                request = JsonHelper.DeserializeObject <RequestModel>(message);
            }
            catch (Exception ex)
            {
                log.Stage = "解析请求参数异常";
                AsyncHelper.TaskRun(() =>
                {
                    ProcessCallback(clientModel, socketClient, log);
                });
                ex       = Log4netHelper.GetRealException(ex);
                response = new ResponseModel()
                {
                    Response = Encoding.UTF8.GetBytes(ex.Message),
                };
                log.Content += $"{Environment.NewLine}解析消息异常:{ex}";
                Log4netHelper.Fatal($"解析消息异常:{message}", ex);
            }
            log.Request = request;
            #endregion

            #region 转发
            if (request != null)
            {
                try
                {
                    log.StartHttpRelayTime = DateTime.Now;
                    var url = clientModel.HandleUrl;
                    url       = string.Format("{0}{1}", clientModel.HandleUrl, request.UrlPathAndQuery);
                    log.Stage = "开始转发请求";
                    AsyncHelper.TaskRun(() =>
                    {
                        ProcessCallback(clientModel, socketClient, log);
                    });
                    response = await HttpRelayHelper.HttpRelayAsync(url, request);

                    log.Stage = "收到请求响应";
                    AsyncHelper.TaskRun(() =>
                    {
                        ProcessCallback(clientModel, socketClient, log);
                    });
                    log.EndHttpRelayTime = DateTime.Now;
                    log.Relay            = true;
                }
                catch (Exception ex)
                {
                    log.Stage = "转发请求异常";
                    AsyncHelper.TaskRun(() =>
                    {
                        ProcessCallback(clientModel, socketClient, log);
                    });
                    response = new ResponseModel()
                    {
                        Response = Encoding.UTF8.GetBytes(ex.Message),
                        Id       = request.Id,
                    };
                    log.Relay    = false;
                    log.Content += $"{Environment.NewLine}转发消息异常:{ex}";
                }
            }
            #endregion

            log.Response = response;

            SocketClient reply_socket = null;
            Policy.Handle <Exception>().WaitAndRetryForever(retryAttempt => TimeSpan.FromSeconds(Math.Pow(1, retryAttempt)), (policy_ex, timer) =>
            {
                log.Stage = "回复异常";
                AsyncHelper.TaskRun(() =>
                {
                    ProcessCallback(clientModel, socketClient, log);
                });
            }).Execute(() =>
            {
                #region 确保当前客户端状态可用

                if (socketClient.IsDisposable || !socketClient.Socket.Connected)
                {
                    log.Content += $"{Environment.NewLine}客户端已被释放,开始创建新的客户端";
                    reply_socket = clientModel.GetOneFreeOnlineClient();
                    if (reply_socket != null)
                    {
                        if (reply_socket.IsDisposable || !reply_socket.Socket.Connected)
                        {
                            log.Content += $"{Environment.NewLine}客户端已被释放,开始创建新的客户端";
                            Policy.Handle <Exception>().WaitAndRetryForever(retryAttempt => TimeSpan.FromSeconds(Math.Pow(1, retryAttempt)), (policy_ex, timer) =>
                            {
                                log.Stage = "无可用客户端,创建客户端异常";
                                AsyncHelper.TaskRun(() =>
                                {
                                    ProcessCallback(clientModel, socketClient, log);
                                });
                                log.Content += $"{Environment.NewLine}创建新的客户端异常:{policy_ex}";
                            }).Execute(() =>
                            {
                                reply_socket = clientModel.CreateSession();
                            });
                        }
                    }
                    else
                    {
                        reply_socket = clientModel.CreateSession();
                    }
                }
                else
                {
                    reply_socket = socketClient;
                }

                #endregion

                #region 回复

                log.Stage = "开始回复";
                AsyncHelper.TaskRun(() =>
                {
                    ProcessCallback(clientModel, socketClient, log);
                });
                reply_socket.Send(response);
                #endregion
            });

            log.Stage = "回复完毕";
            AsyncHelper.TaskRun(() =>
            {
                ProcessCallback(clientModel, socketClient, log);
            });
            log.Reply            = true;
            log.ResponseDateTime = DateTime.Now;
            if (ProcessedCallback != null)
            {
                ProcessedCallback(clientModel, reply_socket, log);
            }
        }