aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBastien Bouclet2017-09-20 19:12:04 +0200
committerBastien Bouclet2017-09-22 07:05:59 +0200
commit2832332e74ecd0d166631dd516a98ffcde5b65b3 (patch)
tree9333dedd00141ddc25d115ce69363eda4efae1ae
parentbc531e3ebf6e2c3a67a79be4439f32b4989ce808 (diff)
downloadscummvm-rg350-2832332e74ecd0d166631dd516a98ffcde5b65b3.tar.gz
scummvm-rg350-2832332e74ecd0d166631dd516a98ffcde5b65b3.tar.bz2
scummvm-rg350-2832332e74ecd0d166631dd516a98ffcde5b65b3.zip
NETWORKING: Changed NetworkReadStream not to subclass MemoryReadWriteStream
Also fix the MemoryReadWriteStream managed buffer being leaked. Fixes #9718.
-rw-r--r--backends/networking/curl/networkreadstream.cpp23
-rw-r--r--backends/networking/curl/networkreadstream.h46
2 files changed, 39 insertions, 30 deletions
diff --git a/backends/networking/curl/networkreadstream.cpp b/backends/networking/curl/networkreadstream.cpp
index e4fc5492b5..d3859cebc2 100644
--- a/backends/networking/curl/networkreadstream.cpp
+++ b/backends/networking/curl/networkreadstream.cpp
@@ -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)
diff --git a/backends/networking/curl/networkreadstream.h b/backends/networking/curl/networkreadstream.h
index 8e59429a0a..c0ed5ee1c6 100644
--- a/backends/networking/curl/networkreadstream.h
+++ b/backends/networking/curl/networkreadstream.h
@@ -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