2017-11-22 14 views
0

HTTP 요청을하고 실패하면 요청을 반복합니다. 내 컴퓨터에 인터넷 케이블을 뽑고 다시 연결하여 시뮬레이션합니다. 대부분 내 코드가 작동하지만 항상co_await HttpClient :: PutAsync가 완료되지 않음 (C++, coroutine, UWP 사용)

co_await HttpClient :: PutAsync()는 결코 "반환"하거나 완료하지 않습니다. 다시()를 호출, tryExecute 실패하면

task<Aws::Result> Aws::S3Request::tryExecute() 
{ 
    // content 
    IHttpContent^ httpContent; 
    httpContent = ref new HttpBufferContent(mBufferContent); 
    httpContent->Headers->ContentType = ref new Headers::HttpMediaTypeHeaderValue(ref new String(mContentType.c_str())); 

    std::wstring signature = getSignature(); 

    // client 
    auto filter = ref new ::Filters::HttpBaseProtocolFilter; 
    filter->AllowUI = false; 
    filter->CacheControl->ReadBehavior = Filters::HttpCacheReadBehavior::NoCache; 
    filter->CacheControl->WriteBehavior = Filters::HttpCacheWriteBehavior::NoCache; 
    HttpClient^ httpClient = ref new HttpClient(filter); 
    httpClient->DefaultRequestHeaders->Authorization = ref new Headers::HttpCredentialsHeaderValue(L"AWS", ref new String((mUser.mAccessKey + L":" + signature).c_str())); 
    httpClient->DefaultRequestHeaders->Append(L"Date", ref new String(mDate.c_str())); 
    httpClient->DefaultRequestHeaders->Append(L"x-amz-acl", L"public-read"); 
    httpClient->DefaultRequestHeaders->Append(L"x-amz-security-token", ref new String(mUser.mSessionToken.c_str())); 

    // http req 
    Uri^ uri = ref new Uri(ref new String(mUri.c_str())); 
    HttpResponseMessage^ response; 
    IAsyncOperationWithProgress<HttpResponseMessage^, HttpProgress>^ progress; 
    try 
    { 
     progress = httpClient->PutAsync(uri, httpContent); 
     progress->Progress = ref new AsyncOperationProgressHandler<HttpResponseMessage^, HttpProgress>(
      [this](IAsyncOperationWithProgress<HttpResponseMessage^, HttpProgress>^ pretask, HttpProgress progressInfo) 
     { 
      /*double got = progressInfo.BytesReceived + progressInfo.BytesSent; 
      double total = 0; 
      if (progressInfo.TotalBytesToReceive) 
      total += progressInfo.TotalBytesToReceive->Value; 
      if (progressInfo.TotalBytesToSend) 
      total += progressInfo.TotalBytesToSend->Value; 
      float progress = (total >= 1) ? float(got/total) : 0.0f; 
      debugOut("--- part progress = ", progress); 
      if (mProgress) 
      *mProgress = progress;*/ 
     } 
     ); 
     response = co_await progress; 
     progress = nullptr; 
     if (!response->IsSuccessStatusCode) 
      debugOut("--- http fail error"); 
    } 
    catch (Platform::COMException^) 
    { 
     debugOut("--- http fail nointernet"); 
     progress = nullptr; 
     mResult = NoInternet; 
     mAnswer.clear(); 
     co_return mResult; 
    } 

    // answer 
    mAnswer = response->Content->ToString()->Data(); 
    debugOut(L"--- Http answer=", mAnswer); 
    mResult = response->IsSuccessStatusCode ? Success : Error; 

    co_return mResult; 
} 

:

내 코드는 다음과 같이 보입니다.

그런데 IAsyncOperationWithProgress는 나에게 0 % 또는 100 % 만 제공하고 inbetweens는 제공하지 않습니다. 각 요청마다 5MB의 크기를 업로드합니다.

+0

, 그래서 이것은 100 % 재현 문제가되지 않는다 "내 코드가 작동하지만, 때로는 대부분의 시간"? 실패한 시대의 모든 스페셜? 서버 쪽에서 put 요청에 제약이 있습니까? –

+0

아마도 케이블을 뽑고 다시 연결하는 시간의 10 %가 지나면 co_wait PutAsync는 절대로 "반환"하거나 완료하지 않습니다. 타임 아웃이 누락 된 것과 같습니다. – Morten

답변

0

이 작동하지만 좋은 해결책이 아니다 :

//response = co_await progress; 
for (int timeout = 5*60*100; true; timeout--) 
{ 
    if (progress->Status != AsyncStatus::Started) 
     break; 
    co_await winrt::resume_after(10ms); 
    if (timeout == 0) 
    { 
     debugOut("--- http fail timeout"); 
     mResult = NoInternet; 
     mAnswer.clear(); 
     co_return mResult; 
    } 
} 
response = progress->GetResults();