Skip to content

Commit

Permalink
changed: Have Curl Fillbuffer indicate whether error was fatal or not
Browse files Browse the repository at this point in the history
  • Loading branch information
arnova committed Jan 10, 2016
1 parent 4e1e7fd commit 78c03b8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
38 changes: 23 additions & 15 deletions xbmc/filesystem/CurlFile.cpp
Expand Up @@ -286,7 +286,7 @@ bool CCurlFile::CReadState::Seek(int64_t pos)
int len = m_buffer.getMaxReadSize();
m_filePos += len;
m_buffer.SkipBytes(len);
if(!FillBuffer(m_bufferSize))
if (FillBuffer(m_bufferSize) <= 0)
{
if(!m_buffer.SkipBytes(-len))
CLog::Log(LOGERROR, "%s - Failed to restore position after failed fill", __FUNCTION__);
Expand Down Expand Up @@ -346,7 +346,7 @@ long CCurlFile::CReadState::Connect(unsigned int size)
m_stillRunning = 1;

// (Try to) fill buffer
if (!FillBuffer(1))
if (FillBuffer(1) <= 0)
{
// Check response code
long response;
Expand Down Expand Up @@ -1116,8 +1116,12 @@ bool CCurlFile::CReadState::ReadString(char *szLine, int iLineLength)
{
unsigned int want = (unsigned int)iLineLength;

if((m_fileSize == 0 || m_filePos < m_fileSize) && !FillBuffer(want))
return false;
if (m_fileSize == 0 || m_filePos < m_fileSize)
{
int iRes = FillBuffer(want);
if (iRes <= 0)
return false;
}

// ensure only available data is considered
want = XMIN((unsigned int)m_buffer.getMaxReadSize(), want);
Expand Down Expand Up @@ -1440,8 +1444,12 @@ int CCurlFile::Stat(const CURL& url, struct __stat64* buffer)
unsigned int CCurlFile::CReadState::Read(void* lpBuf, size_t uiBufSize)
{
/* only request 1 byte, for truncated reads (only if not eof) */
if((m_fileSize == 0 || m_filePos < m_fileSize) && !FillBuffer(1))
return 0;
if (m_fileSize == 0 || m_filePos < m_fileSize)
{
int iRes = FillBuffer(1);
if (iRes <= 0)
return iRes;
}

/* ensure only available data is considered */
unsigned int want = (unsigned int)XMIN(m_buffer.getMaxReadSize(), uiBufSize);
Expand All @@ -1464,7 +1472,7 @@ unsigned int CCurlFile::CReadState::Read(void* lpBuf, size_t uiBufSize)
}

/* use to attempt to fill the read buffer up to requested number of bytes */
bool CCurlFile::CReadState::FillBuffer(unsigned int want)
int CCurlFile::CReadState::FillBuffer(unsigned int want)
{
int retry = 0;
fd_set fdread;
Expand All @@ -1476,7 +1484,7 @@ bool CCurlFile::CReadState::FillBuffer(unsigned int want)
while ((unsigned int)m_buffer.getMaxReadSize() < want && m_buffer.getMaxWriteSize() > 0 )
{
if (m_cancelled)
return false;
return -1;

/* if there is data in overflow buffer, try to use that first */
if (m_overflowSize)
Expand All @@ -1502,7 +1510,7 @@ bool CCurlFile::CReadState::FillBuffer(unsigned int want)
{
/* if we still have stuff in buffer, we are fine */
if (m_buffer.getMaxReadSize())
return true;
return 1;

// check for errors
int msgs;
Expand All @@ -1512,7 +1520,7 @@ bool CCurlFile::CReadState::FillBuffer(unsigned int want)
if (msg->msg == CURLMSG_DONE)
{
if (msg->data.result == CURLE_OK)
return true;
return 1;

long httpCode = 0;
if (msg->data.result == CURLE_HTTP_RETURNED_ERROR)
Expand Down Expand Up @@ -1551,7 +1559,7 @@ bool CCurlFile::CReadState::FillBuffer(unsigned int want)
else
{
// For all other errors, abort the operation
return false;
return -1;
}
}
}
Expand All @@ -1577,7 +1585,7 @@ bool CCurlFile::CReadState::FillBuffer(unsigned int want)
continue;
}
}
return false;
return 0;
}

// We've finished out first loop
Expand Down Expand Up @@ -1649,7 +1657,7 @@ bool CCurlFile::CReadState::FillBuffer(unsigned int want)
CLog::Log(LOGERROR, "CCurlFile::FillBuffer - Failed with socket error:%s", str);
#endif

return false;
return -1;
}
}
break;
Expand All @@ -1664,12 +1672,12 @@ bool CCurlFile::CReadState::FillBuffer(unsigned int want)
default:
{
CLog::Log(LOGERROR, "CCurlFile::FillBuffer - Multi perform failed with code %d, aborting", result);
return false;
return -1;
}
break;
}
}
return true;
return 1;
}

void CCurlFile::CReadState::SetReadBuffer(const void* lpBuf, int64_t uiBufSize)
Expand Down
2 changes: 1 addition & 1 deletion xbmc/filesystem/CurlFile.h
Expand Up @@ -141,7 +141,7 @@ namespace XFILE
bool Seek(int64_t pos);
unsigned int Read(void* lpBuf, size_t uiBufSize);
bool ReadString(char *szLine, int iLineLength);
bool FillBuffer(unsigned int want);
int FillBuffer(unsigned int want);
void SetReadBuffer(const void* lpBuf, int64_t uiBufSize);

void SetResume(void);
Expand Down

0 comments on commit 78c03b8

Please sign in to comment.