NETWORKING: Changed NetworkReadStream not to subclass MemoryReadWriteStream
Also fix the MemoryReadWriteStream managed buffer being leaked. Fixes #9718.
This commit is contained in:
parent
bc531e3ebf
commit
2832332e74
2 changed files with 39 additions and 30 deletions
|
@ -29,35 +29,35 @@
|
|||
|
||||
namespace Networking {
|
||||
|
||||
static size_t curlDataCallback(char *d, size_t n, size_t l, void *p) {
|
||||
size_t NetworkReadStream::curlDataCallback(char *d, size_t n, size_t l, void *p) {
|
||||
NetworkReadStream *stream = (NetworkReadStream *)p;
|
||||
if (stream)
|
||||
return stream->write(d, n * l);
|
||||
return stream->_backingStream.write(d, n * l);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static size_t curlReadDataCallback(char *d, size_t n, size_t l, void *p) {
|
||||
size_t NetworkReadStream::curlReadDataCallback(char *d, size_t n, size_t l, void *p) {
|
||||
NetworkReadStream *stream = (NetworkReadStream *)p;
|
||||
if (stream)
|
||||
return stream->fillWithSendingContents(d, n * l);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static size_t curlHeadersCallback(char *d, size_t n, size_t l, void *p) {
|
||||
size_t NetworkReadStream::curlHeadersCallback(char *d, size_t n, size_t l, void *p) {
|
||||
NetworkReadStream *stream = (NetworkReadStream *)p;
|
||||
if (stream)
|
||||
return stream->addResponseHeaders(d, n * l);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int curlProgressCallback(void *p, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) {
|
||||
int NetworkReadStream::curlProgressCallback(void *p, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) {
|
||||
NetworkReadStream *stream = (NetworkReadStream *)p;
|
||||
if (stream)
|
||||
stream->setProgress(dlnow, dltotal);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int curlProgressCallbackOlder(void *p, double dltotal, double dlnow, double ultotal, double ulnow) {
|
||||
int NetworkReadStream::curlProgressCallbackOlder(void *p, double dltotal, double dlnow, double ultotal, double ulnow) {
|
||||
// for libcurl older than 7.32.0 (CURLOPT_PROGRESSFUNCTION)
|
||||
return curlProgressCallback(p, (curl_off_t)dltotal, (curl_off_t)dlnow, (curl_off_t)ultotal, (curl_off_t)ulnow);
|
||||
}
|
||||
|
@ -178,15 +178,18 @@ void NetworkReadStream::init(const char *url, curl_slist *headersList, Common::H
|
|||
ConnMan.registerEasyHandle(_easy);
|
||||
}
|
||||
|
||||
NetworkReadStream::NetworkReadStream(const char *url, curl_slist *headersList, Common::String postFields, bool uploading, bool usingPatch) {
|
||||
NetworkReadStream::NetworkReadStream(const char *url, curl_slist *headersList, Common::String postFields, bool uploading, bool usingPatch) :
|
||||
_backingStream(DisposeAfterUse::YES) {
|
||||
init(url, headersList, (const byte *)postFields.c_str(), postFields.size(), uploading, usingPatch, false);
|
||||
}
|
||||
|
||||
NetworkReadStream::NetworkReadStream(const char *url, curl_slist *headersList, Common::HashMap<Common::String, Common::String> formFields, Common::HashMap<Common::String, Common::String> formFiles) {
|
||||
NetworkReadStream::NetworkReadStream(const char *url, curl_slist *headersList, Common::HashMap<Common::String, Common::String> formFields, Common::HashMap<Common::String, Common::String> formFiles) :
|
||||
_backingStream(DisposeAfterUse::YES) {
|
||||
init(url, headersList, formFields, formFiles);
|
||||
}
|
||||
|
||||
NetworkReadStream::NetworkReadStream(const char *url, curl_slist *headersList, const byte *buffer, uint32 bufferSize, bool uploading, bool usingPatch, bool post) {
|
||||
NetworkReadStream::NetworkReadStream(const char *url, curl_slist *headersList, const byte *buffer, uint32 bufferSize, bool uploading, bool usingPatch, bool post) :
|
||||
_backingStream(DisposeAfterUse::YES) {
|
||||
init(url, headersList, buffer, bufferSize, uploading, usingPatch, post);
|
||||
}
|
||||
|
||||
|
@ -201,7 +204,7 @@ bool NetworkReadStream::eos() const {
|
|||
}
|
||||
|
||||
uint32 NetworkReadStream::read(void *dataPtr, uint32 dataSize) {
|
||||
uint32 actuallyRead = MemoryReadWriteStream::read(dataPtr, dataSize);
|
||||
uint32 actuallyRead = _backingStream.read(dataPtr, dataSize);
|
||||
|
||||
if (actuallyRead == 0) {
|
||||
if (_requestComplete)
|
||||
|
|
|
@ -34,8 +34,9 @@ struct curl_slist;
|
|||
|
||||
namespace Networking {
|
||||
|
||||
class NetworkReadStream: public Common::MemoryReadWriteStream {
|
||||
class NetworkReadStream: public Common::ReadStream {
|
||||
CURL *_easy;
|
||||
Common::MemoryReadWriteStream _backingStream;
|
||||
bool _eos, _requestComplete;
|
||||
const byte *_sendingContentsBuffer;
|
||||
uint32 _sendingContentsSize;
|
||||
|
@ -46,6 +47,30 @@ class NetworkReadStream: public Common::MemoryReadWriteStream {
|
|||
void init(const char *url, curl_slist *headersList, const byte *buffer, uint32 bufferSize, bool uploading, bool usingPatch, bool post);
|
||||
void init(const char *url, curl_slist *headersList, Common::HashMap<Common::String, Common::String> formFields, Common::HashMap<Common::String, Common::String> formFiles);
|
||||
|
||||
/**
|
||||
* Fills the passed buffer with _sendingContentsBuffer contents.
|
||||
* It works similarly to read(), expect it's not for reading
|
||||
* Stream's contents, but for sending our own data to the server.
|
||||
*
|
||||
* @returns how many bytes were actually read (filled in)
|
||||
*/
|
||||
uint32 fillWithSendingContents(char *bufferToFill, uint32 maxSize);
|
||||
|
||||
/**
|
||||
* Remembers headers returned to CURL in server's response.
|
||||
*
|
||||
* @returns how many bytes were actually read
|
||||
*/
|
||||
uint32 addResponseHeaders(char *buffer, uint32 bufferSize);
|
||||
|
||||
/** Used in curl progress callback to pass current downloaded/total values. */
|
||||
void setProgress(uint64 downloaded, uint64 total);
|
||||
|
||||
static size_t curlDataCallback(char *d, size_t n, size_t l, void *p);
|
||||
static size_t curlReadDataCallback(char *d, size_t n, size_t l, void *p);
|
||||
static size_t curlHeadersCallback(char *d, size_t n, size_t l, void *p);
|
||||
static int curlProgressCallback(void *p, long dltotal, long dlnow, long ultotal, long ulnow);
|
||||
static int curlProgressCallbackOlder(void *p, double dltotal, double dlnow, double ultotal, double ulnow);
|
||||
public:
|
||||
/** Send <postFields>, using POST by default. */
|
||||
NetworkReadStream(const char *url, curl_slist *headersList, Common::String postFields, bool uploading = false, bool usingPatch = false);
|
||||
|
@ -115,27 +140,8 @@ public:
|
|||
*/
|
||||
Common::String responseHeaders() const;
|
||||
|
||||
/**
|
||||
* Fills the passed buffer with _sendingContentsBuffer contents.
|
||||
* It works similarly to read(), expect it's not for reading
|
||||
* Stream's contents, but for sending our own data to the server.
|
||||
*
|
||||
* @returns how many bytes were actually read (filled in)
|
||||
*/
|
||||
uint32 fillWithSendingContents(char *bufferToFill, uint32 maxSize);
|
||||
|
||||
/**
|
||||
* Remembers headers returned to CURL in server's response.
|
||||
*
|
||||
* @returns how many bytes were actually read
|
||||
*/
|
||||
uint32 addResponseHeaders(char *buffer, uint32 bufferSize);
|
||||
|
||||
/** Returns a number in range [0, 1], where 1 is "complete". */
|
||||
double getProgress() const;
|
||||
|
||||
/** Used in curl progress callback to pass current downloaded/total values. */
|
||||
void setProgress(uint64 downloaded, uint64 total);
|
||||
};
|
||||
|
||||
} // End of namespace Networking
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue