summaryrefslogtreecommitdiff
path: root/src/network/network_content.cpp
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2010-02-03 17:15:35 +0000
committerrubidium <rubidium@openttd.org>2010-02-03 17:15:35 +0000
commite437362c7babbdc4d4dff812e88b77bb521be6c5 (patch)
treeed76e575c516952e96951e2fedd17e61cd889987 /src/network/network_content.cpp
parent589aee0cee8fd72ff07fd2dc6043443e07097212 (diff)
downloadopenttd-e437362c7babbdc4d4dff812e88b77bb521be6c5.tar.xz
(svn r18992) -Codechange: move the file opening/closing out of the content download function
Diffstat (limited to 'src/network/network_content.cpp')
-rw-r--r--src/network/network_content.cpp83
1 files changed, 48 insertions, 35 deletions
diff --git a/src/network/network_content.cpp b/src/network/network_content.cpp
index 64acd86e0..576d5ad30 100644
--- a/src/network/network_content.cpp
+++ b/src/network/network_content.cpp
@@ -369,6 +369,7 @@ exit:
DEF_CONTENT_RECEIVE_COMMAND(Client, PACKET_CONTENT_SERVER_CONTENT)
{
if (this->curFile == NULL) {
+ delete this->curInfo;
/* When we haven't opened a file this must be our first packet with metadata. */
this->curInfo = new ContentInfo;
this->curInfo->type = (ContentType)p->Recv_uint8();
@@ -376,26 +377,10 @@ DEF_CONTENT_RECEIVE_COMMAND(Client, PACKET_CONTENT_SERVER_CONTENT)
this->curInfo->filesize = p->Recv_uint32();
p->Recv_string(this->curInfo->filename, lengthof(this->curInfo->filename));
- if (!this->curInfo->IsValid()) {
- delete this->curInfo;
- this->curInfo = NULL;
+ if (!this->BeforeDownload()) {
this->Close();
return false;
}
-
- if (this->curInfo->filesize != 0) {
- /* The filesize is > 0, so we are going to download it */
- const char *filename = GetFullFilename(this->curInfo, true);
- if (filename == NULL) {
- /* Unless that fails ofcourse... */
- DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
- ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, 0, 0);
- this->Close();
- return false;
- }
-
- this->curFile = fopen(filename, "wb");
- }
} else {
/* We have a file opened, thus are downloading internal content */
size_t toRead = (size_t)(p->size - p->pos);
@@ -411,34 +396,62 @@ DEF_CONTENT_RECEIVE_COMMAND(Client, PACKET_CONTENT_SERVER_CONTENT)
this->OnDownloadProgress(this->curInfo, (uint)toRead);
- if (toRead == 0) {
- /* We read nothing; that's our marker for end-of-stream.
- * Now gunzip the tar and make it known. */
- fclose(this->curFile);
- this->curFile = NULL;
-
- if (GunzipFile(this->curInfo)) {
- unlink(GetFullFilename(this->curInfo, true));
-
- TarListAddFile(GetFullFilename(this->curInfo, false));
-
- this->OnDownloadComplete(this->curInfo->id);
- } else {
- ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_EXTRACT, INVALID_STRING_ID, 0, 0);
- }
- }
+ if (toRead == 0) this->AfterDownload();
}
- /* We ended this file, so clean up the mess */
- if (this->curFile == NULL) {
+ return true;
+}
+
+/**
+ * Handle the opening of the file before downloading.
+ * @return false on any error.
+ */
+bool ClientNetworkContentSocketHandler::BeforeDownload()
+{
+ if (!this->curInfo->IsValid()) {
delete this->curInfo;
this->curInfo = NULL;
+ return false;
}
+ if (this->curInfo->filesize != 0) {
+ /* The filesize is > 0, so we are going to download it */
+ const char *filename = GetFullFilename(this->curInfo, true);
+ if (filename == NULL) {
+ /* Unless that fails ofcourse... */
+ DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
+ ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, 0, 0);
+ return false;
+ }
+
+ this->curFile = fopen(filename, "wb");
+ }
return true;
}
/**
+ * Handle the closing and extracting of a file after
+ * downloading it has been done.
+ */
+void ClientNetworkContentSocketHandler::AfterDownload()
+{
+ /* We read nothing; that's our marker for end-of-stream.
+ * Now gunzip the tar and make it known. */
+ fclose(this->curFile);
+ this->curFile = NULL;
+
+ if (GunzipFile(this->curInfo)) {
+ unlink(GetFullFilename(this->curInfo, true));
+
+ TarListAddFile(GetFullFilename(this->curInfo, false));
+
+ this->OnDownloadComplete(this->curInfo->id);
+ } else {
+ ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_EXTRACT, INVALID_STRING_ID, 0, 0);
+ }
+}
+
+/**
* Create a socket handler with the given socket and (server) address.
* @param s the socket to communicate over
* @param sin the IP/port of the server