summaryrefslogtreecommitdiff
path: root/src/network/network_content.cpp
diff options
context:
space:
mode:
authorRubidium <rubidium@openttd.org>2021-04-18 10:23:41 +0200
committerrubidium42 <rubidium42@users.noreply.github.com>2021-04-24 20:42:01 +0200
commitd4f027c03bfc5f632b7d63c125dfa57a7ba89926 (patch)
treebce76afc0a253cf8128153fcb7b5ebae36c93434 /src/network/network_content.cpp
parent98aa561cf759f75971afd5dfc4d42e6921a8ab1a (diff)
downloadopenttd-d4f027c03bfc5f632b7d63c125dfa57a7ba89926.tar.xz
Codechange: encapsulate writing data from Packets into sockets/files/buffers to prevent packet state modifications outside of the Packet
Diffstat (limited to 'src/network/network_content.cpp')
-rw-r--r--src/network/network_content.cpp16
1 files changed, 14 insertions, 2 deletions
diff --git a/src/network/network_content.cpp b/src/network/network_content.cpp
index 0220f890b..529225235 100644
--- a/src/network/network_content.cpp
+++ b/src/network/network_content.cpp
@@ -459,6 +459,18 @@ static bool GunzipFile(const ContentInfo *ci)
#endif /* defined(WITH_ZLIB) */
}
+/**
+ * Simple wrapper around fwrite to be able to pass it to Packet's TransferOut.
+ * @param file The file to write data to.
+ * @param buffer The buffer to write to the file.
+ * @param amount The number of bytes to write.
+ * @return The number of bytes that were written.
+ */
+static inline ssize_t TransferOutFWrite(FILE *file, const char *buffer, size_t amount)
+{
+ return fwrite(buffer, 1, amount, file);
+}
+
bool ClientNetworkContentSocketHandler::Receive_SERVER_CONTENT(Packet *p)
{
if (this->curFile == nullptr) {
@@ -476,8 +488,8 @@ bool ClientNetworkContentSocketHandler::Receive_SERVER_CONTENT(Packet *p)
}
} else {
/* We have a file opened, thus are downloading internal content */
- size_t toRead = (size_t)(p->size - p->pos);
- if (fwrite(p->buffer + p->pos, 1, toRead, this->curFile) != toRead) {
+ size_t toRead = p->RemainingBytesToTransfer();
+ if (toRead != 0 && (size_t)p->TransferOut(TransferOutFWrite, this->curFile) != toRead) {
DeleteWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD);
ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE, WL_ERROR);
this->Close();