Skip to content

Commit

Permalink
Retry when some network error occurs in loading the stream
Browse files Browse the repository at this point in the history
  • Loading branch information
windows-server-2003 committed Nov 27, 2021
1 parent 36b9879 commit 32f7e4e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 2 deletions.
2 changes: 2 additions & 0 deletions include/network/network_downloader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
struct NetworkStream {
static constexpr u64 BLOCK_SIZE = 0x40000; // 256 KiB
static constexpr u64 MAX_CACHE_BLOCKS = 12 * 1000 * 1000 / BLOCK_SIZE;
static constexpr int RETRY_CNT_MAX = 1;

u64 block_num = 0;
std::string url;
Expand All @@ -23,6 +24,7 @@ struct NetworkStream {
volatile bool suspend_request = false;
volatile bool quit_request = false;
volatile bool error = false;
volatile int retry_cnt_left = RETRY_CNT_MAX;
volatile u64 read_head = 0;
const char * volatile network_waiting_status = NULL;
bool disable_interrupt = false;
Expand Down
1 change: 1 addition & 0 deletions include/network/network_io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ private :
void deinit(); // will be called for each instance when the app exits
public :
CURL* curl = NULL;
char *curl_errbuf;
// should not be used from outside network_io.cpp
std::map<std::string, NetworkSession> sessions;
std::vector<u8> *buffer;
Expand Down
5 changes: 4 additions & 1 deletion source/network/network_downloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,13 @@ void NetworkStreamDownloader::downloader_thread() {
}
if (cur_stream->ready && result.data.size() != expected_len) {
Util_log_save(LOG_THREAD_STR, "size discrepancy : " + std::to_string(expected_len) + " -> " + std::to_string(result.data.size()));
cur_stream->error = true;
if (cur_stream->retry_cnt_left) {
cur_stream->retry_cnt_left--;
} else cur_stream->error = true;
result.finalize();
continue;
}
cur_stream->retry_cnt_left = NetworkStream::RETRY_CNT_MAX;
cur_stream->set_data(block_reading, result.data);
cur_stream->ready = true;
} else {
Expand Down
4 changes: 3 additions & 1 deletion source/network/network_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,8 @@ static NetworkResult access_http_internal(NetworkSessionList &session_list, cons
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_receive_data_callback_func);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, curl_receive_headers_callback_func);
curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, curl_set_socket_options);
session_list.curl_errbuf = (char *) malloc(CURL_ERROR_SIZE);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, session_list.curl_errbuf);
// curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
}
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &res.data);
Expand All @@ -587,7 +589,7 @@ static NetworkResult access_http_internal(NetworkSessionList &session_list, cons
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &redirected_url);
res.redirected_url = redirected_url;
if (res.redirected_url != url) Util_log_save("curl", "redir : " + res.redirected_url);
} else Util_log_save("curl", "deep fail");
} else Util_log_save("curl", std::string("deep fail : ") + curl_easy_strerror(curl_code) + " / " + session_list.curl_errbuf);

curl_slist_free_all(request_headers_list);
}
Expand Down

0 comments on commit 32f7e4e

Please sign in to comment.