Skip to content

Commit

Permalink
По возможности везде используем опции сетевого тайм-аута для коннекта…
Browse files Browse the repository at this point in the history
… и чтения.
  • Loading branch information
Aleksoid1978 committed May 30, 2024
1 parent d486dee commit 229a8fc
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 32 deletions.
4 changes: 3 additions & 1 deletion src/DSUtil/HTTPAsync.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* (C) 2016-2023 see Authors.txt
* (C) 2016-2024 see Authors.txt
*
* This file is part of MPC-BE.
*
Expand Down Expand Up @@ -27,6 +27,8 @@

namespace http {
inline CStringW userAgent = L"Mozilla/5.0";
inline DWORD connectTimeout = 10000;
inline DWORD readTimeout = 10000;
}

class CHTTPAsync
Expand Down
6 changes: 3 additions & 3 deletions src/Subtitles/TextFile.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* (C) 2003-2006 Gabest
* (C) 2006-2023 see Authors.txt
* (C) 2006-2024 see Authors.txt
*
* This file is part of MPC-BE.
*
Expand Down Expand Up @@ -747,7 +747,7 @@ bool CWebTextFile::Open(LPCWSTR lpszFileName)
}

CHTTPAsync HTTPAsync;
if (SUCCEEDED(HTTPAsync.Connect(lpszFileName, 5000))) {
if (SUCCEEDED(HTTPAsync.Connect(lpszFileName, http::connectTimeout))) {
if (GetTemporaryFilePath(L".tmp", fn)) {
CFile temp;
if (!temp.Open(fn, modeCreate | modeWrite | typeBinary | shareDenyWrite)) {
Expand All @@ -768,7 +768,7 @@ bool CWebTextFile::Open(LPCWSTR lpszFileName)
DWORD dwSizeRead = 0;
DWORD totalSize = 0;
do {
if (HTTPAsync.Read(buffer, 1024, dwSizeRead) != S_OK) {
if (HTTPAsync.Read(buffer, 1024, dwSizeRead, http::readTimeout) != S_OK) {
break;
}
temp.Write(buffer, dwSizeRead);
Expand Down
4 changes: 4 additions & 0 deletions src/apps/mplayerc/AppSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,8 @@ void CAppSettings::LoadSettings(bool bForce/* = false*/)
profile.ReadInt(IDS_R_SETTINGS, IDS_RS_BUFFERDURATION, iBufferDuration, APP_BUFDURATION_MIN, APP_BUFDURATION_MAX);
profile.ReadInt(IDS_R_SETTINGS, IDS_RS_NETWORKTIMEOUT, iNetworkTimeout, APP_NETTIMEOUT_MIN, APP_NETTIMEOUT_MAX);
profile.ReadInt(IDS_R_SETTINGS, IDS_RS_NETRECEIVETIMEOUT, iNetworkReceiveTimeout, APP_NETRECEIVETIMEOUT_MIN, APP_NETRECEIVETIMEOUT_MAX);
http::connectTimeout = iNetworkTimeout * 1000;
http::readTimeout = iNetworkReceiveTimeout * 1000;

// Audio
profile.ReadInt(IDS_R_AUDIO, IDS_RS_VOLUME, nVolume, 0, 100);
Expand Down Expand Up @@ -1728,6 +1730,8 @@ void CAppSettings::SaveSettings()
profile.WriteInt(IDS_R_SETTINGS, IDS_RS_BUFFERDURATION, iBufferDuration);
profile.WriteInt(IDS_R_SETTINGS, IDS_RS_NETWORKTIMEOUT, iNetworkTimeout);
profile.WriteInt(IDS_R_SETTINGS, IDS_RS_NETRECEIVETIMEOUT, iNetworkReceiveTimeout);
http::connectTimeout = iNetworkTimeout * 1000;
http::readTimeout = iNetworkReceiveTimeout * 1000;

// Prevent Minimize when in Fullscreen mode on non default monitor
profile.WriteBool(IDS_R_SETTINGS, IDS_RS_PREVENT_MINIMIZE, fPreventMinimize);
Expand Down
4 changes: 2 additions & 2 deletions src/apps/mplayerc/Content.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ namespace Content {

content.raw.resize(nMinSize);
DWORD dwSizeRead = 0;
if (content.HTTPAsync->Read(content.raw.data(), nMinSize, dwSizeRead) == S_OK) {
if (content.HTTPAsync->Read(content.raw.data(), nMinSize, dwSizeRead, AfxGetAppSettings().iNetworkReceiveTimeout * 1000) == S_OK) {
content.raw.resize(dwSizeRead);
if (dwSizeRead) {
Encoding(content);
Expand Down Expand Up @@ -176,7 +176,7 @@ namespace Content {
nMaxSize -= old_size;
content.raw.resize(old_size + nMaxSize);
DWORD dwSizeRead = 0;
if (content.HTTPAsync->Read(content.raw.data() + old_size, nMaxSize, dwSizeRead) == S_OK) {
if (content.HTTPAsync->Read(content.raw.data() + old_size, nMaxSize, dwSizeRead, AfxGetAppSettings().iNetworkReceiveTimeout * 1000) == S_OK) {
content.raw.resize(old_size + dwSizeRead);
if (dwSizeRead) {
Encoding(content);
Expand Down
6 changes: 3 additions & 3 deletions src/apps/mplayerc/MainFrm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19623,19 +19623,19 @@ HRESULT CMainFrame::SetAudioPicture(BOOL show)

if (!bLoadRes && !m_youtubeFields.thumbnailUrl.IsEmpty()) {
CHTTPAsync HTTPAsync;
if (SUCCEEDED(HTTPAsync.Connect(m_youtubeFields.thumbnailUrl.GetString(), 10000))) {
if (SUCCEEDED(HTTPAsync.Connect(m_youtubeFields.thumbnailUrl.GetString(), http::connectTimeout))) {
const auto contentLength = HTTPAsync.GetLenght();
if (contentLength) {
m_youtubeThumbnailData.resize(contentLength);
DWORD dwSizeRead = 0;
if (S_OK != HTTPAsync.Read((PBYTE)m_youtubeThumbnailData.data(), contentLength, dwSizeRead) || dwSizeRead != contentLength) {
if (S_OK != HTTPAsync.Read((PBYTE)m_youtubeThumbnailData.data(), contentLength, dwSizeRead, http::readTimeout) || dwSizeRead != contentLength) {
m_youtubeThumbnailData.clear();
}
} else {
std::vector<char> tmp(16 * KILOBYTE);
for (;;) {
DWORD dwSizeRead = 0;
if (S_OK != HTTPAsync.Read((PBYTE)tmp.data(), tmp.size(), dwSizeRead)) {
if (S_OK != HTTPAsync.Read((PBYTE)tmp.data(), tmp.size(), dwSizeRead, http::readTimeout)) {
break;
}

Expand Down
10 changes: 5 additions & 5 deletions src/apps/mplayerc/PlayerYouTube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,21 +200,21 @@ namespace Youtube
pData.clear();

CHTTPAsync HTTPAsync;
if (FAILED(HTTPAsync.Connect(url, 10000, header))) {
if (FAILED(HTTPAsync.Connect(url, http::connectTimeout, header))) {
return false;
}
const auto contentLength = HTTPAsync.GetLenght();
if (contentLength) {
pData.resize(contentLength);
DWORD dwSizeRead = 0;
if (S_OK != HTTPAsync.Read((PBYTE)pData.data(), contentLength, dwSizeRead) || dwSizeRead != contentLength) {
if (S_OK != HTTPAsync.Read((PBYTE)pData.data(), contentLength, dwSizeRead, http::readTimeout) || dwSizeRead != contentLength) {
pData.clear();
}
} else {
static std::vector<char> tmp(16 * KILOBYTE);
for (;;) {
DWORD dwSizeRead = 0;
if (S_OK != HTTPAsync.Read((PBYTE)tmp.data(), tmp.size(), dwSizeRead)) {
if (S_OK != HTTPAsync.Read((PBYTE)tmp.data(), tmp.size(), dwSizeRead, http::readTimeout)) {
break;
}

Expand Down Expand Up @@ -546,7 +546,7 @@ namespace Youtube

const auto& url = final_item->url;
CHTTPAsync HTTPAsync;
if (FAILED(HTTPAsync.Connect(url, 10000))) {
if (FAILED(HTTPAsync.Connect(url, http::connectTimeout))) {
DLog(L"Youtube::Parse_URL() : failed to connect \"%s\", skip", url.GetString());
auto it = std::find_if(youtubeUrllist.cbegin(), youtubeUrllist.cend(),
[&url](const YoutubeUrllistItem& item) { return item.url == url; });
Expand Down Expand Up @@ -598,7 +598,7 @@ namespace Youtube

const auto& url = final_item->url;
CHTTPAsync HTTPAsync;
if (FAILED(HTTPAsync.Connect(url, 10000))) {
if (FAILED(HTTPAsync.Connect(url, http::connectTimeout))) {
DLog(L"Youtube::Parse_URL() : failed to connect \"%s\", skip", url.GetString());
auto it = std::find_if(youtubeAudioUrllist.cbegin(), youtubeAudioUrllist.cend(),
[&url](const YoutubeUrllistItem& item) { return item.url == url; });
Expand Down
2 changes: 1 addition & 1 deletion src/apps/mplayerc/SaveTaskDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ HRESULT CSaveTaskDlg::DownloadHTTP(CStringW url, const CStringW filepath)

while (!m_bAbort) {
DWORD dwSizeRead = 0;
hr = httpAsync.Read(pBuffer.data(), bufLen, dwSizeRead);
hr = httpAsync.Read(pBuffer.data(), bufLen, dwSizeRead, http::readTimeout);
if (hr != S_OK) {
break;
}
Expand Down
4 changes: 2 additions & 2 deletions src/apps/mplayerc/UpdateChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ Update_Status UpdateChecker::CheckNewVersion()

Update_Status updatestatus = UPDATER_ERROR_CONNECT;
CHTTPAsync HTTPAsync;
if (SUCCEEDED(HTTPAsync.Connect(L"https://api.github.com/repos/Aleksoid1978/MPC-BE/releases/latest", 3000))) {
if (SUCCEEDED(HTTPAsync.Connect(L"https://api.github.com/repos/Aleksoid1978/MPC-BE/releases/latest", http::connectTimeout))) {
constexpr auto sizeRead = 16 * KILOBYTE;
CStringA data;
DWORD dwSizeRead = 0;
int dataSize = 0;
while (S_OK == HTTPAsync.Read(reinterpret_cast<PBYTE>(data.GetBuffer(dataSize + sizeRead) + dataSize), sizeRead, dwSizeRead)) {
while (S_OK == HTTPAsync.Read(reinterpret_cast<PBYTE>(data.GetBuffer(dataSize + sizeRead) + dataSize), sizeRead, dwSizeRead, http::readTimeout)) {
data.ReleaseBuffer(dataSize + dwSizeRead);
dataSize = data.GetLength();
dwSizeRead = 0;
Expand Down
8 changes: 4 additions & 4 deletions src/filters/parser/BaseSplitter/AsyncReader.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* (C) 2003-2006 Gabest
* (C) 2006-2023 see Authors.txt
* (C) 2006-2024 see Authors.txt
*
* This file is part of MPC-BE.
*
Expand Down Expand Up @@ -61,7 +61,7 @@ BOOL CAsyncFileReader::Open(LPCWSTR lpszFileName)
CUrlParser urlParser;
if (m_bSupportURL
&& urlParser.Parse(lpszFileName)
&& m_HTTPAsync.Connect(lpszFileName, 10000) == S_OK) {
&& m_HTTPAsync.Connect(lpszFileName, http::connectTimeout) == S_OK) {
const UINT64 ContentLength = m_HTTPAsync.GetLenght();
if (ContentLength == 0) {
return FALSE;
Expand Down Expand Up @@ -112,7 +112,7 @@ STDMETHODIMP CAsyncFileReader::SyncRead(LONGLONG llPosition, LONG lLength, BYTE*
const DWORD lenght = llPosition - m_pos;

DWORD dwSizeRead = 0;
HRESULT hr = m_HTTPAsync.Read(pBufferTmp.data(), lenght, dwSizeRead);
HRESULT hr = m_HTTPAsync.Read(pBufferTmp.data(), lenght, dwSizeRead, http::readTimeout);
if (hr != S_OK || dwSizeRead != lenght) {
if (RetryOnError()) {
continue;
Expand All @@ -133,7 +133,7 @@ STDMETHODIMP CAsyncFileReader::SyncRead(LONGLONG llPosition, LONG lLength, BYTE*
}

DWORD dwSizeRead = 0;
HRESULT hr = m_HTTPAsync.Read(pBuffer, lLength, dwSizeRead);
HRESULT hr = m_HTTPAsync.Read(pBuffer, lLength, dwSizeRead, http::readTimeout);
if (hr != S_OK || dwSizeRead != lLength) {
if (RetryOnError()) {
continue;
Expand Down
22 changes: 11 additions & 11 deletions src/filters/reader/StreamReader/LiveStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ HRESULT CLiveStream::HTTPRead(PBYTE pBuffer, DWORD dwSizeToRead, DWORD& dwSizeRe
ZeroMemory(buff, sizeof(buff));

DWORD _dwSizeRead = 0;
if (S_OK == m_HTTPAsync.Read(&b, 1, _dwSizeRead) && _dwSizeRead == 1 && b) {
if (S_OK == m_HTTPAsync.Read(buff, b * 16, _dwSizeRead) && _dwSizeRead == b * 16) {
if (S_OK == m_HTTPAsync.Read(&b, 1, _dwSizeRead, dwTimeOut) && _dwSizeRead == 1 && b) {
if (S_OK == m_HTTPAsync.Read(buff, b * 16, _dwSizeRead, dwTimeOut) && _dwSizeRead == b * 16) {
int len = decode_html_entities_utf8((char*)buff, nullptr);

CString str = UTF8orLocalToWStr((LPCSTR)buff);
Expand Down Expand Up @@ -334,10 +334,10 @@ bool CLiveStream::ParseM3U8(const CString& url, CString& realUrl)
uri.Trim(L'"');
uri = CombinePath(base, uri);
CHTTPAsync http;
if (SUCCEEDED(http.Connect(uri))) {
if (SUCCEEDED(http.Connect(uri, http::connectTimeout))) {
BYTE key[CAESDecryptor::AESBLOCKSIZE] = {};
DWORD dwSizeRead = 0;
if (SUCCEEDED(http.Read(key, CAESDecryptor::AESBLOCKSIZE, dwSizeRead)) && dwSizeRead == CAESDecryptor::AESBLOCKSIZE) {
if (SUCCEEDED(http.Read(key, CAESDecryptor::AESBLOCKSIZE, dwSizeRead, http::readTimeout)) && dwSizeRead == CAESDecryptor::AESBLOCKSIZE) {
std::vector<BYTE> pIV;
iv.Delete(0, 2);
if (iv.GetLength() != (CAESDecryptor::AESBLOCKSIZE * 2)) {
Expand Down Expand Up @@ -475,7 +475,7 @@ bool CLiveStream::ParseM3U8(const CString& url, CString& realUrl)
bool CLiveStream::OpenHLSSegment()
{
if (!m_hlsData.Segments.empty()) {
auto hr = m_HTTPAsync.Connect(m_hlsData.Segments.front());
auto hr = m_HTTPAsync.Connect(m_hlsData.Segments.front(), http::connectTimeout);
m_hlsData.Segments.pop_front();
if (SUCCEEDED(hr)) {
m_hlsData.SegmentSize = m_HTTPAsync.GetLenght();
Expand Down Expand Up @@ -585,7 +585,7 @@ bool CLiveStream::Load(const WCHAR* fnw)
}
} else if (str_protocol == L"http" || str_protocol == L"https") {
m_protocol = protocol::PR_HTTP;
HRESULT hr = m_HTTPAsync.Connect(m_url_str, 10000, L"Icy-MetaData:1\r\n");
HRESULT hr = m_HTTPAsync.Connect(m_url_str, http::connectTimeout, L"Icy-MetaData:1\r\n");
if (FAILED(hr)) {
return false;
}
Expand Down Expand Up @@ -635,7 +635,7 @@ bool CLiveStream::Load(const WCHAR* fnw)
|| contentType.IsEmpty()) {
BYTE buf[1024] = {};
DWORD dwSizeRead = 0;
if (HTTPRead(buf, sizeof(buf), dwSizeRead) == S_OK && dwSizeRead) {
if (HTTPRead(buf, sizeof(buf), dwSizeRead, http::readTimeout) == S_OK && dwSizeRead) {
GetType(buf, dwSizeRead, m_subtype);
Append(buf, dwSizeRead);
}
Expand Down Expand Up @@ -672,7 +672,7 @@ bool CLiveStream::Load(const WCHAR* fnw)
} else {
BYTE body[8] = {};
DWORD dwSizeRead = 0;
if (m_HTTPAsync.Read(body, 7, dwSizeRead) == S_OK) {
if (m_HTTPAsync.Read(body, 7, dwSizeRead, http::readTimeout) == S_OK) {
if (memcmp(body, "#EXTM3U", 7) == 0) {
ret = ParseM3U8(m_url_str, m_hlsData.PlaylistUrl);
}
Expand Down Expand Up @@ -716,7 +716,7 @@ bool CLiveStream::Load(const WCHAR* fnw)
if (m_protocol == protocol::PR_HTTP && !m_len) {
BYTE buf[1024] = {};
DWORD dwSizeRead = 0;
if (HTTPRead(buf, sizeof(buf), dwSizeRead) == S_OK && dwSizeRead) {
if (HTTPRead(buf, sizeof(buf), dwSizeRead, http::readTimeout) == S_OK && dwSizeRead) {
Append(buf, dwSizeRead);
}
}
Expand Down Expand Up @@ -975,7 +975,7 @@ DWORD CLiveStream::ThreadProc()
}
} else if (m_protocol == protocol::PR_HTTP) {
DWORD dwSizeRead = 0;
const HRESULT hr = HTTPRead(&buff[buffsize], MAXBUFSIZE, dwSizeRead);
const HRESULT hr = HTTPRead(&buff[buffsize], MAXBUFSIZE, dwSizeRead, http::readTimeout);
if (FAILED(hr)) {
attempts += 50;
continue;
Expand Down Expand Up @@ -1034,7 +1034,7 @@ DWORD CLiveStream::ThreadProc()

DWORD dwSizeRead = 0;
auto ptr = m_hlsData.bAes128 ? encryptedBuff.get() : &buff[buffsize];
if (FAILED(m_HTTPAsync.Read(ptr, MAXBUFSIZE, dwSizeRead))) {
if (FAILED(m_HTTPAsync.Read(ptr, MAXBUFSIZE, dwSizeRead, http::readTimeout))) {
bEndOfStream = TRUE;
break;
} else if (dwSizeRead == 0) {
Expand Down

0 comments on commit 229a8fc

Please sign in to comment.