//парсим email, ищем заявку там string _getResultByEmail(long requestId, long startTime, string btcAddr, double rubAmount, string phoneFrom, string forwardtobtc, int dempingpercent, int forwardint, string forwardfraction) { //в течении следующих time_check_email_sec проверяем email long maxTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds() + App.settings.data.time_check_email_sec; while (DateTimeOffset.UtcNow.ToUnixTimeSeconds() < maxTime) { var emails = App.emailParser.GetLastByClient(App.LAST_PARSED_RATE.client); TicketClientCreateResponse ticketByEmail = null; CreateResponse createResponseByEmail = EmailParser.Find(emails, btcAddr, phoneFrom, out ticketByEmail); if (createResponseByEmail == null) //еще не пришло, ждем { Thread.Sleep(500); continue; } try { if (forwardtobtc != null) { (new BtcAddressForwardModel(App.db)) .Create(btcAddr, forwardtobtc, dempingpercent, forwardint, forwardfraction); } } catch (Exception e) { } try { (new BtcAddressModel(App.db)).Create(startTime, btcAddr, ticketByEmail.account, //phone to ticketByEmail.comment, ticketByEmail.btc_amount, App.LAST_PARSED_RATE.GetClientNameOnPath(), phoneFrom, ticketByEmail.ip, ticketByEmail.email); } catch (Exception e) { } createResponseByEmail.other = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("type_get", "complicated") }.ToArray(); try { (new LogRequestModel(App.db)).SetResponse(requestId, createResponseByEmail.toJson()); } catch (Exception e) { } return(createResponseByEmail.toJson()); } return(null); //после парсинга не даем переключаться на следующий клиент, всегда выходим }
public System.IO.Stream Create(double rubAmount, string phoneFrom, string forwardtobtc = null, int dempingpercent = 0, int forwardint = 0, string forwardfraction = "0") { long startTime = Now(); try { if (!App.credentials.IsAllow("create")) { return(null); } string requestStr = @"{ rubAmount : '@rubAmount', phoneFrom : '@phoneFrom', forwardtobtc : '@forwardtobtc', dempingpercent : '@dempingpercent', forwardint : '@forwardint', forwardfraction : '@forwardfraction' }" .Replace("@rubAmount", rubAmount.ToString()) .Replace("@phoneFrom", phoneFrom) .Replace("@forwardtobtc", forwardtobtc) .Replace("@dempingpercent", dempingpercent.ToString()) .Replace("@forwardint", forwardint.ToString()) .Replace("@forwardfraction", forwardfraction); long requestId = (new LogRequestModel(App.db)) .SetRequest("create: " + rubAmount.ToString(), requestStr); //ждем курсов int waitRateCnt = 10; while (App.LAST_PARSED_RATE == null) { Thread.Sleep(1000); if (waitRateCnt-- < 0) { return(null); } } //все доступные клиенты на случай неудачи for (int clientId = 0; clientId < App.LAST_PARSED_RATE.clients.Count; clientId++) { //max_repeat_on_fault кол-во попыток int cntTryOnClient = App.LAST_PARSED_RATE.client.max_repeat_on_fault == -1 ? 1 : App.LAST_PARSED_RATE.client.max_repeat_on_fault; for (int i = 0; i < cntTryOnClient; i++) { string addresstype = App.LAST_PARSED_RATE.client.GetTypeBtcAddress(); string btcAddr = App.btcAddrStorage.GetOne(addresstype); (new BtcAddressForwardModel(App.db)) .Create(btcAddr, forwardtobtc, dempingpercent, forwardint, forwardfraction); CreateResponse reseponse = null; try { bool isExpire; reseponse = _create(out isExpire, startTime, btcAddr, rubAmount, PhoneHelper.PhoneReplacer(phoneFrom), forwardtobtc, dempingpercent, forwardint, forwardfraction); if (isExpire) { return(null); } } catch (Exception) { reseponse = null; } try { string jsonResponse = ""; //не пытаемся получить результат, парсим email if (App.LAST_PARSED_RATE.client.max_repeat_on_fault == -1) { jsonResponse = _getResultByEmail(requestId, startTime, btcAddr, rubAmount, phoneFrom, forwardtobtc, dempingpercent, forwardint, forwardfraction); } else { //если ответ неудачный, повторяем попытку if (reseponse == null || !reseponse.IsValide()) { continue; } reseponse.client = App.LAST_PARSED_RATE.ToShort(); reseponse.other = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("type_get", "simple") }.ToArray(); jsonResponse = reseponse.toJson(); (new LogRequestModel(App.db)) .SetResponse(requestId, jsonResponse); } WebOperationContext.Current.OutgoingResponse.ContentType = "application/json"; return(new MemoryStream(Encoding.UTF8.GetBytes(jsonResponse))); } catch (Exception ex) { } if (App.LAST_PARSED_RATE.client.max_repeat_on_fault == -1) { return(null); } } if (!App.LAST_PARSED_RATE.client.allow_switch_to_next_client_on_fault) { return(null); } //все попытки завершились неудачей, переключим на следующий клиент App.LAST_PARSED_RATE.Switch(); } } catch (Exception) { } return(null); }