/// <summary> /// Выполнить запрос к серверу /// </summary> /// <param name="bioRequest">Конфигурация запроса</param> public void Request(BioRequest bioRequest) { this._request(bioRequest); }
private void _request(BioRequest bioRequest) { //Debug.WriteLine("Request:0 - bioRequest.requestType[" + bioRequest.requestType + "]; prms:" + bioRequest.prms); if ((this.RequestState == RequestState.Requesting) && (this._currentRequest != bioRequest)) { this._queue.Enqueue(bioRequest); //Debug.WriteLine("Request:1 - _queue.Enqueue(); return;"); return; } this.RequestState = RequestState.Requesting; this._currentRequest = bioRequest; if (bioRequest.URL == null) { bioRequest.URL = this.Env.ServerUrl; //Utl.BuildURL(this.ServerUrl, null); } var v_clbck = bioRequest.Callback; bioRequest.Callback = (sender, args) => { //Debug.WriteLine("Request:2 - bioRequest.callback - start"); var response = args.Response as BioResponse; if ((response != null) && !response.Success) { //Debug.WriteLine("Request:3 - response.success:false"); // Сервер вернул ошибку var bioLoginExcp = CAjaxLogin.decBioLoginExcp(response.Ex); if (bioLoginExcp != null) { //Debug.WriteLine("Request:4 - vBioLoginExcp:" + vBioLoginExcp.GetType().Name); // Попадаем сюда если сервер вернул сообщение связанное с логином (Например EBioStart-это значит, что соединение отсутствует и необходимо начать новую сессию) // Сбрасываем параметры сессии ajaxUTL.sessionID = null; if (bioLoginExcp is EBioStart) { this.ConnectionState = ConnectionState.Connecting; // - соединение отсутствует и сервер запросил параметры для новой сессии // - запускаем процедуру логина. //Debug.WriteLine("Request:5 - запускаем процедуру логина: this._loginPrc.processLogin"); this._loginPrc.processLogin(bioLoginExcp, args.Request as BioRequest, (e, r) => { response.Ex = e; if (response.Ex is EBioOk) { //Debug.WriteLine("Request:6 - this._loginPrc.processLogin: response.ex == null"); // -- Новая сессия создана без ошибок // устанавливаем состояние сессии if (this.ConnectionState == ConnectionState.Connecting) this.ConnectionState = ConnectionState.Connected; else if (this.ConnectionState == ConnectionState.Breaking) this.ConnectionState = ConnectionState.Breaked; // тут необходимо запустить запрос, который был отправлен на сервер перед тем как сервер вернул запрос параметров новой сессии //Debug.WriteLine("Request:7 - перезапускаем первоначальный запрос: this.Request(args.request as BioRequest);"); this.Request(args.Request as BioRequest); } else { //Debug.WriteLine("Request:8 - this._loginPrc.processLogin: response.ex: " + response.ex.GetType().Name); // -- При создании сессии произошла ошибка if (response.Ex is EBioCancel) { // Пользователь нажал кнопку "Отмена" в окне ввода логина this.ConnectionState = ConnectionState.Canceled; this.RequestState = RequestState.Requested; this._processCallback(v_clbck, this, args); } else { // Непредвиденная ошибка при проверке логина this.ConnectionState = ConnectionState.Error; this.RequestState = RequestState.Requested; if (!args.Request.Silent) { if (!(response.Ex is EBioAutenticationError)) msgBx.ShowError(response.Ex, "Ошибка обращения к серверу", () => this._processCallback(v_clbck, this, args)); } } } }); } else { //Debug.WriteLine("Request:11 - Непредвиденная ошибка."); // Непредвиденная ошибка this.ConnectionState = ConnectionState.Error; this.RequestState = RequestState.Requested; if (!args.Request.Silent) { msgBx.ShowError(response.Ex, "Ошибка обращения к серверу", () => { this._processCallback(v_clbck, this, args); }); } else this._processCallback(v_clbck, this, args); } } else { //Debug.WriteLine("Request:12 - Непредвиденная ошибка."); //this.ConnectionState = ConnectionState.Unconnected; this.RequestState = RequestState.Error; if (!args.Request.Silent) { msgBx.ShowError(response.Ex, "Ошибка обращения к серверу", () => this._processCallback(v_clbck, this, args)); } else this._processCallback(v_clbck, this, args); } } else { if (args.Response.Ex != null) { if (args.Response.Ex is EBioLoggedOut) { //Debug.WriteLine("Request:9 - сессия завершена."); // - сервер завершил сессию this.ConnectionState = ConnectionState.Breaked; } else if (args.Response.Ex is EBioOk) { // Это ответ на запрос doPing когда сессия уже существует //Debug.WriteLine("Request:10 - сессия создана."); // ответ - заершающий создание сесии this._loginPrc.assignCurUser((args.Response.Ex as EBioOk).Usr); this.ConnectionState = ConnectionState.Connected; //Debug.WriteLine("Request:10-1 - this._queue.Dequeue();"); } else { //Debug.WriteLine("Request:10-2 - Непредвиденная ошибка."); this.RequestState = RequestState.Error; if (!args.Request.Silent) msgBx.ShowError(args.Response.Ex, "Ошибка обращения к серверу", () => this._processCallback(v_clbck, this, args)); } } //Debug.WriteLine("Request:13 - Запрос выполнен."); this.RequestState = RequestState.Requested; this._processCallback(v_clbck, this, args); //Debug.WriteLine("Request:14 - this._queue.Dequeue();"); } }; this._ajax.Request(bioRequest, this.Env.ConfigRoot.RequestTimeout); }
public void processLogin(EBioLogin bioLoginExcp, BioRequest request, Action<EBioException, BioRequest> callback) { if (bioLoginExcp != null) { this._processLogin(bioLoginExcp, (EBioException e) => { if (callback != null) callback(e, request); }); } }