network_content.cpp

Go to the documentation of this file.
00001 /* $Id: network_content.cpp 25599 2013-07-13 10:13:55Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #if defined(ENABLE_NETWORK)
00013 
00014 #include "../stdafx.h"
00015 #include "../rev.h"
00016 #include "../ai/ai.hpp"
00017 #include "../game/game.hpp"
00018 #include "../window_func.h"
00019 #include "../error.h"
00020 #include "../base_media_base.h"
00021 #include "../settings_type.h"
00022 #include "network_content.h"
00023 
00024 #include "table/strings.h"
00025 
00026 #if defined(WITH_ZLIB)
00027 #include <zlib.h>
00028 #endif
00029 
00030 extern bool HasScenario(const ContentInfo *ci, bool md5sum);
00031 
00033 ClientNetworkContentSocketHandler _network_content_client;
00034 
00036 static bool HasGRFConfig(const ContentInfo *ci, bool md5sum)
00037 {
00038   return FindGRFConfig(BSWAP32(ci->unique_id), md5sum ? FGCM_EXACT : FGCM_ANY, md5sum ? ci->md5sum : NULL) != NULL;
00039 }
00040 
00048 typedef bool (*HasProc)(const ContentInfo *ci, bool md5sum);
00049 
00050 bool ClientNetworkContentSocketHandler::Receive_SERVER_INFO(Packet *p)
00051 {
00052   ContentInfo *ci = new ContentInfo();
00053   ci->type     = (ContentType)p->Recv_uint8();
00054   ci->id       = (ContentID)p->Recv_uint32();
00055   ci->filesize = p->Recv_uint32();
00056 
00057   p->Recv_string(ci->name, lengthof(ci->name));
00058   p->Recv_string(ci->version, lengthof(ci->name));
00059   p->Recv_string(ci->url, lengthof(ci->url));
00060   p->Recv_string(ci->description, lengthof(ci->description), SVS_REPLACE_WITH_QUESTION_MARK | SVS_ALLOW_NEWLINE);
00061 
00062   ci->unique_id = p->Recv_uint32();
00063   for (uint j = 0; j < sizeof(ci->md5sum); j++) {
00064     ci->md5sum[j] = p->Recv_uint8();
00065   }
00066 
00067   ci->dependency_count = p->Recv_uint8();
00068   ci->dependencies = MallocT<ContentID>(ci->dependency_count);
00069   for (uint i = 0; i < ci->dependency_count; i++) ci->dependencies[i] = (ContentID)p->Recv_uint32();
00070 
00071   ci->tag_count = p->Recv_uint8();
00072   ci->tags = MallocT<char[32]>(ci->tag_count);
00073   for (uint i = 0; i < ci->tag_count; i++) p->Recv_string(ci->tags[i], lengthof(*ci->tags));
00074 
00075   if (!ci->IsValid()) {
00076     delete ci;
00077     this->Close();
00078     return false;
00079   }
00080 
00081   /* Find the appropriate check function */
00082   HasProc proc = NULL;
00083   switch (ci->type) {
00084     case CONTENT_TYPE_NEWGRF:
00085       proc = HasGRFConfig;
00086       break;
00087 
00088     case CONTENT_TYPE_BASE_GRAPHICS:
00089       proc = BaseGraphics::HasSet;
00090       break;
00091 
00092     case CONTENT_TYPE_BASE_MUSIC:
00093       proc = BaseMusic::HasSet;
00094       break;
00095 
00096     case CONTENT_TYPE_BASE_SOUNDS:
00097       proc = BaseSounds::HasSet;
00098       break;
00099 
00100     case CONTENT_TYPE_AI:
00101       proc = AI::HasAI; break;
00102       break;
00103 
00104     case CONTENT_TYPE_AI_LIBRARY:
00105       proc = AI::HasAILibrary; break;
00106       break;
00107 
00108     case CONTENT_TYPE_GAME:
00109       proc = Game::HasGame; break;
00110       break;
00111 
00112     case CONTENT_TYPE_GAME_LIBRARY:
00113       proc = Game::HasGameLibrary; break;
00114       break;
00115 
00116     case CONTENT_TYPE_SCENARIO:
00117     case CONTENT_TYPE_HEIGHTMAP:
00118       proc = HasScenario;
00119       break;
00120 
00121     default:
00122       break;
00123   }
00124 
00125   if (proc != NULL) {
00126     if (proc(ci, true)) {
00127       ci->state = ContentInfo::ALREADY_HERE;
00128     } else {
00129       ci->state = ContentInfo::UNSELECTED;
00130       if (proc(ci, false)) ci->upgrade = true;
00131     }
00132   } else {
00133     ci->state = ContentInfo::UNSELECTED;
00134   }
00135 
00136   /* Something we don't have and has filesize 0 does not exist in te system */
00137   if (ci->state == ContentInfo::UNSELECTED && ci->filesize == 0) ci->state = ContentInfo::DOES_NOT_EXIST;
00138 
00139   /* Do we already have a stub for this? */
00140   for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
00141     ContentInfo *ici = *iter;
00142     if (ici->type == ci->type && ici->unique_id == ci->unique_id &&
00143         memcmp(ci->md5sum, ici->md5sum, sizeof(ci->md5sum)) == 0) {
00144       /* Preserve the name if possible */
00145       if (StrEmpty(ci->name)) strecpy(ci->name, ici->name, lastof(ci->name));
00146       if (ici->IsSelected()) ci->state = ici->state;
00147 
00148       /*
00149        * As ici might be selected by the content window we cannot delete that.
00150        * However, we want to keep most of the values of ci, except the values
00151        * we (just) already preserved.
00152        * So transfer data and ownership of allocated memory from ci to ici.
00153        */
00154       ici->TransferFrom(ci);
00155       delete ci;
00156 
00157       this->OnReceiveContentInfo(ici);
00158       return true;
00159     }
00160   }
00161 
00162   /* Missing content info? Don't list it */
00163   if (ci->filesize == 0) {
00164     delete ci;
00165     return true;
00166   }
00167 
00168   *this->infos.Append() = ci;
00169 
00170   /* Incoming data means that we might need to reconsider dependencies */
00171   for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
00172     this->CheckDependencyState(*iter);
00173   }
00174 
00175   this->OnReceiveContentInfo(ci);
00176 
00177   return true;
00178 }
00179 
00184 void ClientNetworkContentSocketHandler::RequestContentList(ContentType type)
00185 {
00186   if (type == CONTENT_TYPE_END) {
00187     this->RequestContentList(CONTENT_TYPE_BASE_GRAPHICS);
00188     this->RequestContentList(CONTENT_TYPE_BASE_MUSIC);
00189     this->RequestContentList(CONTENT_TYPE_BASE_SOUNDS);
00190     this->RequestContentList(CONTENT_TYPE_SCENARIO);
00191     this->RequestContentList(CONTENT_TYPE_HEIGHTMAP);
00192     this->RequestContentList(CONTENT_TYPE_AI);
00193     this->RequestContentList(CONTENT_TYPE_AI_LIBRARY);
00194     this->RequestContentList(CONTENT_TYPE_GAME);
00195     this->RequestContentList(CONTENT_TYPE_GAME_LIBRARY);
00196     this->RequestContentList(CONTENT_TYPE_NEWGRF);
00197     return;
00198   }
00199 
00200   this->Connect();
00201 
00202   Packet *p = new Packet(PACKET_CONTENT_CLIENT_INFO_LIST);
00203   p->Send_uint8 ((byte)type);
00204   p->Send_uint32(_openttd_newgrf_version);
00205 
00206   this->SendPacket(p);
00207 }
00208 
00214 void ClientNetworkContentSocketHandler::RequestContentList(uint count, const ContentID *content_ids)
00215 {
00216   this->Connect();
00217 
00218   while (count > 0) {
00219     /* We can "only" send a limited number of IDs in a single packet.
00220      * A packet begins with the packet size and a byte for the type.
00221      * Then this packet adds a byte for the content type and a uint16
00222      * for the count in this packet. The rest of the packet can be
00223      * used for the IDs. */
00224     uint p_count = min(count, (SEND_MTU - sizeof(PacketSize) - sizeof(byte) - sizeof(byte) - sizeof(uint16)) / sizeof(uint32));
00225 
00226     Packet *p = new Packet(PACKET_CONTENT_CLIENT_INFO_ID);
00227     p->Send_uint16(p_count);
00228 
00229     for (uint i = 0; i < p_count; i++) {
00230       p->Send_uint32(content_ids[i]);
00231     }
00232 
00233     this->SendPacket(p);
00234     count -= p_count;
00235     content_ids += p_count;
00236   }
00237 }
00238 
00244 void ClientNetworkContentSocketHandler::RequestContentList(ContentVector *cv, bool send_md5sum)
00245 {
00246   if (cv == NULL) return;
00247 
00248   this->Connect();
00249 
00250   /* 20 is sizeof(uint32) + sizeof(md5sum (byte[16])) */
00251   assert(cv->Length() < 255);
00252   assert(cv->Length() < (SEND_MTU - sizeof(PacketSize) - sizeof(byte) - sizeof(uint8)) / (send_md5sum ? 20 : sizeof(uint32)));
00253 
00254   Packet *p = new Packet(send_md5sum ? PACKET_CONTENT_CLIENT_INFO_EXTID_MD5 : PACKET_CONTENT_CLIENT_INFO_EXTID);
00255   p->Send_uint8(cv->Length());
00256 
00257   for (ContentIterator iter = cv->Begin(); iter != cv->End(); iter++) {
00258     const ContentInfo *ci = *iter;
00259     p->Send_uint8((byte)ci->type);
00260     p->Send_uint32(ci->unique_id);
00261     if (!send_md5sum) continue;
00262 
00263     for (uint j = 0; j < sizeof(ci->md5sum); j++) {
00264       p->Send_uint8(ci->md5sum[j]);
00265     }
00266   }
00267 
00268   this->SendPacket(p);
00269 
00270   for (ContentIterator iter = cv->Begin(); iter != cv->End(); iter++) {
00271     ContentInfo *ci = *iter;
00272     bool found = false;
00273     for (ContentIterator iter2 = this->infos.Begin(); iter2 != this->infos.End(); iter2++) {
00274       ContentInfo *ci2 = *iter2;
00275       if (ci->type == ci2->type && ci->unique_id == ci2->unique_id &&
00276           (!send_md5sum || memcmp(ci->md5sum, ci2->md5sum, sizeof(ci->md5sum)) == 0)) {
00277         found = true;
00278         break;
00279       }
00280     }
00281     if (!found) {
00282       *this->infos.Append() = ci;
00283     } else {
00284       delete ci;
00285     }
00286   }
00287 }
00288 
00295 void ClientNetworkContentSocketHandler::DownloadSelectedContent(uint &files, uint &bytes, bool fallback)
00296 {
00297   bytes = 0;
00298 
00299   ContentIDList content;
00300   for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
00301     const ContentInfo *ci = *iter;
00302     if (!ci->IsSelected() || ci->state == ContentInfo::ALREADY_HERE) continue;
00303 
00304     *content.Append() = ci->id;
00305     bytes += ci->filesize;
00306   }
00307 
00308   files = content.Length();
00309 
00310   /* If there's nothing to download, do nothing. */
00311   if (files == 0) return;
00312 
00313   if (_settings_client.network.no_http_content_downloads || fallback) {
00314     this->DownloadSelectedContentFallback(content);
00315   } else {
00316     this->DownloadSelectedContentHTTP(content);
00317   }
00318 }
00319 
00324 void ClientNetworkContentSocketHandler::DownloadSelectedContentHTTP(const ContentIDList &content)
00325 {
00326   uint count = content.Length();
00327 
00328   /* Allocate memory for the whole request.
00329    * Requests are "id\nid\n..." (as strings), so assume the maximum ID,
00330    * which is uint32 so 10 characters long. Then the newlines and
00331    * multiply that all with the count and then add the '\0'. */
00332   uint bytes = (10 + 1) * count + 1;
00333   char *content_request = MallocT<char>(bytes);
00334   const char *lastof = content_request + bytes - 1;
00335 
00336   char *p = content_request;
00337   for (const ContentID *id = content.Begin(); id != content.End(); id++) {
00338     p += seprintf(p, lastof, "%d\n", *id);
00339   }
00340 
00341   this->http_response_index = -1;
00342 
00343   NetworkAddress address(NETWORK_CONTENT_MIRROR_HOST, NETWORK_CONTENT_MIRROR_PORT);
00344   new NetworkHTTPContentConnecter(address, this, NETWORK_CONTENT_MIRROR_URL, content_request);
00345   /* NetworkHTTPContentConnecter takes over freeing of content_request! */
00346 }
00347 
00352 void ClientNetworkContentSocketHandler::DownloadSelectedContentFallback(const ContentIDList &content)
00353 {
00354   uint count = content.Length();
00355   const ContentID *content_ids = content.Begin();
00356   this->Connect();
00357 
00358   while (count > 0) {
00359     /* We can "only" send a limited number of IDs in a single packet.
00360      * A packet begins with the packet size and a byte for the type.
00361      * Then this packet adds a uint16 for the count in this packet.
00362      * The rest of the packet can be used for the IDs. */
00363     uint p_count = min(count, (SEND_MTU - sizeof(PacketSize) - sizeof(byte) - sizeof(uint16)) / sizeof(uint32));
00364 
00365     Packet *p = new Packet(PACKET_CONTENT_CLIENT_CONTENT);
00366     p->Send_uint16(p_count);
00367 
00368     for (uint i = 0; i < p_count; i++) {
00369       p->Send_uint32(content_ids[i]);
00370     }
00371 
00372     this->SendPacket(p);
00373     count -= p_count;
00374     content_ids += p_count;
00375   }
00376 }
00377 
00385 static char *GetFullFilename(const ContentInfo *ci, bool compressed)
00386 {
00387   Subdirectory dir = GetContentInfoSubDir(ci->type);
00388   if (dir == NO_DIRECTORY) return NULL;
00389 
00390   static char buf[MAX_PATH];
00391   FioGetFullPath(buf, lengthof(buf), SP_AUTODOWNLOAD_DIR, dir, ci->filename);
00392   strecat(buf, compressed ? ".tar.gz" : ".tar", lastof(buf));
00393 
00394   return buf;
00395 }
00396 
00402 static bool GunzipFile(const ContentInfo *ci)
00403 {
00404 #if defined(WITH_ZLIB)
00405   bool ret = true;
00406   FILE *ftmp = fopen(GetFullFilename(ci, true), "rb");
00407   gzFile fin = gzdopen(fileno(ftmp), "rb");
00408   FILE *fout = fopen(GetFullFilename(ci, false), "wb");
00409 
00410   if (fin == NULL || fout == NULL) {
00411     ret = false;
00412   } else {
00413     byte buff[8192];
00414     for (;;) {
00415       int read = gzread(fin, buff, sizeof(buff));
00416       if (read == 0) {
00417         /* If gzread() returns 0, either the end-of-file has been
00418          * reached or an underlying read error has occurred.
00419          *
00420          * gzeof() can't be used, because:
00421          * 1.2.5 - it is safe, 1 means 'everything was OK'
00422          * 1.2.3.5, 1.2.4 - 0 or 1 is returned 'randomly'
00423          * 1.2.3.3 - 1 is returned for truncated archive
00424          *
00425          * So we use gzerror(). When proper end of archive
00426          * has been reached, then:
00427          * errnum == Z_STREAM_END in 1.2.3.3,
00428          * errnum == 0 in 1.2.4 and 1.2.5 */
00429         int errnum;
00430         gzerror(fin, &errnum);
00431         if (errnum != 0 && errnum != Z_STREAM_END) ret = false;
00432         break;
00433       }
00434       if (read < 0 || (size_t)read != fwrite(buff, 1, read, fout)) {
00435         /* If gzread() returns -1, there was an error in archive */
00436         ret = false;
00437         break;
00438       }
00439       /* DO NOT DO THIS! It will fail to detect broken archive with 1.2.3.3!
00440        * if (read < sizeof(buff)) break; */
00441     }
00442   }
00443 
00444   if (fin != NULL) {
00445     /* Closes ftmp too! */
00446     gzclose(fin);
00447   } else if (ftmp != NULL) {
00448     /* In case the gz stream was opened correctly this will
00449      * be closed by gzclose. */
00450     fclose(ftmp);
00451   }
00452   if (fout != NULL) fclose(fout);
00453 
00454   return ret;
00455 #else
00456   NOT_REACHED();
00457 #endif /* defined(WITH_ZLIB) */
00458 }
00459 
00460 bool ClientNetworkContentSocketHandler::Receive_SERVER_CONTENT(Packet *p)
00461 {
00462   if (this->curFile == NULL) {
00463     delete this->curInfo;
00464     /* When we haven't opened a file this must be our first packet with metadata. */
00465     this->curInfo = new ContentInfo;
00466     this->curInfo->type     = (ContentType)p->Recv_uint8();
00467     this->curInfo->id       = (ContentID)p->Recv_uint32();
00468     this->curInfo->filesize = p->Recv_uint32();
00469     p->Recv_string(this->curInfo->filename, lengthof(this->curInfo->filename));
00470 
00471     if (!this->BeforeDownload()) {
00472       this->Close();
00473       return false;
00474     }
00475   } else {
00476     /* We have a file opened, thus are downloading internal content */
00477     size_t toRead = (size_t)(p->size - p->pos);
00478     if (fwrite(p->buffer + p->pos, 1, toRead, this->curFile) != toRead) {
00479       DeleteWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD);
00480       ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE, WL_ERROR);
00481       this->Close();
00482       fclose(this->curFile);
00483       this->curFile = NULL;
00484 
00485       return false;
00486     }
00487 
00488     this->OnDownloadProgress(this->curInfo, (int)toRead);
00489 
00490     if (toRead == 0) this->AfterDownload();
00491   }
00492 
00493   return true;
00494 }
00495 
00500 bool ClientNetworkContentSocketHandler::BeforeDownload()
00501 {
00502   if (!this->curInfo->IsValid()) {
00503     delete this->curInfo;
00504     this->curInfo = NULL;
00505     return false;
00506   }
00507 
00508   if (this->curInfo->filesize != 0) {
00509     /* The filesize is > 0, so we are going to download it */
00510     const char *filename = GetFullFilename(this->curInfo, true);
00511     if (filename == NULL || (this->curFile = fopen(filename, "wb")) == NULL) {
00512       /* Unless that fails of course... */
00513       DeleteWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD);
00514       ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE, WL_ERROR);
00515       return false;
00516     }
00517   }
00518   return true;
00519 }
00520 
00525 void ClientNetworkContentSocketHandler::AfterDownload()
00526 {
00527   /* We read nothing; that's our marker for end-of-stream.
00528    * Now gunzip the tar and make it known. */
00529   fclose(this->curFile);
00530   this->curFile = NULL;
00531 
00532   if (GunzipFile(this->curInfo)) {
00533     unlink(GetFullFilename(this->curInfo, true));
00534 
00535     Subdirectory sd = GetContentInfoSubDir(this->curInfo->type);
00536     if (sd == NO_DIRECTORY) NOT_REACHED();
00537 
00538     TarScanner ts;
00539     ts.AddFile(sd, GetFullFilename(this->curInfo, false));
00540 
00541     if (this->curInfo->type == CONTENT_TYPE_BASE_MUSIC) {
00542       /* Music can't be in a tar. So extract the tar! */
00543       ExtractTar(GetFullFilename(this->curInfo, false), BASESET_DIR);
00544       unlink(GetFullFilename(this->curInfo, false));
00545     }
00546 
00547     this->OnDownloadComplete(this->curInfo->id);
00548   } else {
00549     ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_EXTRACT, INVALID_STRING_ID, WL_ERROR);
00550   }
00551 }
00552 
00553 /* Also called to just clean up the mess. */
00554 void ClientNetworkContentSocketHandler::OnFailure()
00555 {
00556   /* If we fail, download the rest via the 'old' system. */
00557   uint files, bytes;
00558   this->DownloadSelectedContent(files, bytes, true);
00559 
00560   this->http_response.Reset();
00561   this->http_response_index = -2;
00562 
00563   if (this->curFile != NULL) {
00564     /* Revert the download progress when we are going for the old system. */
00565     long size = ftell(this->curFile);
00566     if (size > 0) this->OnDownloadProgress(this->curInfo, (int)-size);
00567 
00568     fclose(this->curFile);
00569     this->curFile = NULL;
00570   }
00571 }
00572 
00573 void ClientNetworkContentSocketHandler::OnReceiveData(const char *data, size_t length)
00574 {
00575   assert(data == NULL || length != 0);
00576 
00577   /* Ignore any latent data coming from a connection we closed. */
00578   if (this->http_response_index == -2) return;
00579 
00580   if (this->http_response_index == -1) {
00581     if (data != NULL) {
00582       /* Append the rest of the response. */
00583       memcpy(this->http_response.Append((uint)length), data, length);
00584       return;
00585     } else {
00586       /* Make sure the response is properly terminated. */
00587       *this->http_response.Append() = '\0';
00588 
00589       /* And prepare for receiving the rest of the data. */
00590       this->http_response_index = 0;
00591     }
00592   }
00593 
00594   if (data != NULL) {
00595     /* We have data, so write it to the file. */
00596     if (fwrite(data, 1, length, this->curFile) != length) {
00597       /* Writing failed somehow, let try via the old method. */
00598       this->OnFailure();
00599     } else {
00600       /* Just received the data. */
00601       this->OnDownloadProgress(this->curInfo, (int)length);
00602     }
00603     /* Nothing more to do now. */
00604     return;
00605   }
00606 
00607   if (this->curFile != NULL) {
00608     /* We've finished downloading a file. */
00609     this->AfterDownload();
00610   }
00611 
00612   if ((uint)this->http_response_index >= this->http_response.Length()) {
00613     /* It's not a real failure, but if there's
00614      * nothing more to download it helps with
00615      * cleaning up the stuff we allocated. */
00616     this->OnFailure();
00617     return;
00618   }
00619 
00620   delete this->curInfo;
00621   /* When we haven't opened a file this must be our first packet with metadata. */
00622   this->curInfo = new ContentInfo;
00623 
00625 #define check_not_null(p) { if ((p) == NULL) { this->OnFailure(); return; } }
00626 
00627 #define check_and_terminate(p) { check_not_null(p); *(p) = '\0'; }
00628 
00629   for (;;) {
00630     char *str = this->http_response.Begin() + this->http_response_index;
00631     char *p = strchr(str, '\n');
00632     check_and_terminate(p);
00633 
00634     /* Update the index for the next one */
00635     this->http_response_index += (int)strlen(str) + 1;
00636 
00637     /* Read the ID */
00638     p = strchr(str, ',');
00639     check_and_terminate(p);
00640     this->curInfo->id = (ContentID)atoi(str);
00641 
00642     /* Read the type */
00643     str = p + 1;
00644     p = strchr(str, ',');
00645     check_and_terminate(p);
00646     this->curInfo->type = (ContentType)atoi(str);
00647 
00648     /* Read the file size */
00649     str = p + 1;
00650     p = strchr(str, ',');
00651     check_and_terminate(p);
00652     this->curInfo->filesize = atoi(str);
00653 
00654     /* Read the URL */
00655     str = p + 1;
00656     /* Is it a fallback URL? If so, just continue with the next one. */
00657     if (strncmp(str, "ottd", 4) == 0) {
00658       if ((uint)this->http_response_index >= this->http_response.Length()) {
00659         /* Have we gone through all lines? */
00660         this->OnFailure();
00661         return;
00662       }
00663       continue;
00664     }
00665 
00666     p = strrchr(str, '/');
00667     check_not_null(p);
00668     p++; // Start after the '/'
00669 
00670     char tmp[MAX_PATH];
00671     if (strecpy(tmp, p, lastof(tmp)) == lastof(tmp)) {
00672       this->OnFailure();
00673       return;
00674     }
00675     /* Remove the extension from the string. */
00676     for (uint i = 0; i < 2; i++) {
00677       p = strrchr(tmp, '.');
00678       check_and_terminate(p);
00679     }
00680 
00681     /* Copy the string, without extension, to the filename. */
00682     strecpy(this->curInfo->filename, tmp, lastof(this->curInfo->filename));
00683 
00684     /* Request the next file. */
00685     if (!this->BeforeDownload()) {
00686       this->OnFailure();
00687       return;
00688     }
00689 
00690     NetworkHTTPSocketHandler::Connect(str, this);
00691     return;
00692   }
00693 
00694 #undef check
00695 #undef check_and_terminate
00696 }
00697 
00701 ClientNetworkContentSocketHandler::ClientNetworkContentSocketHandler() :
00702   NetworkContentSocketHandler(),
00703   http_response_index(-2),
00704   curFile(NULL),
00705   curInfo(NULL),
00706   isConnecting(false)
00707 {
00708 }
00709 
00711 ClientNetworkContentSocketHandler::~ClientNetworkContentSocketHandler()
00712 {
00713   delete this->curInfo;
00714   if (this->curFile != NULL) fclose(this->curFile);
00715 
00716   for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) delete *iter;
00717 }
00718 
00720 class NetworkContentConnecter : TCPConnecter {
00721 public:
00726   NetworkContentConnecter(const NetworkAddress &address) : TCPConnecter(address) {}
00727 
00728   virtual void OnFailure()
00729   {
00730     _network_content_client.isConnecting = false;
00731     _network_content_client.OnConnect(false);
00732   }
00733 
00734   virtual void OnConnect(SOCKET s)
00735   {
00736     assert(_network_content_client.sock == INVALID_SOCKET);
00737     _network_content_client.isConnecting = false;
00738     _network_content_client.sock = s;
00739     _network_content_client.Reopen();
00740     _network_content_client.OnConnect(true);
00741   }
00742 };
00743 
00747 void ClientNetworkContentSocketHandler::Connect()
00748 {
00749   this->lastActivity = _realtime_tick;
00750 
00751   if (this->sock != INVALID_SOCKET || this->isConnecting) return;
00752   this->isConnecting = true;
00753   new NetworkContentConnecter(NetworkAddress(NETWORK_CONTENT_SERVER_HOST, NETWORK_CONTENT_SERVER_PORT, AF_UNSPEC));
00754 }
00755 
00759 void ClientNetworkContentSocketHandler::Close()
00760 {
00761   if (this->sock == INVALID_SOCKET) return;
00762   NetworkContentSocketHandler::Close();
00763 
00764   this->OnDisconnect();
00765 }
00766 
00771 void ClientNetworkContentSocketHandler::SendReceive()
00772 {
00773   if (this->sock == INVALID_SOCKET || this->isConnecting) return;
00774 
00775   if (this->lastActivity + IDLE_TIMEOUT < _realtime_tick) {
00776     this->Close();
00777     return;
00778   }
00779 
00780   if (this->CanSendReceive()) {
00781     if (this->ReceivePackets()) {
00782       /* Only update activity once a packet is received, instead of everytime we try it. */
00783       this->lastActivity = _realtime_tick;
00784     }
00785   }
00786 
00787   this->SendPackets();
00788 }
00789 
00794 void ClientNetworkContentSocketHandler::DownloadContentInfo(ContentID cid)
00795 {
00796   /* When we tried to download it already, don't try again */
00797   if (this->requested.Contains(cid)) return;
00798 
00799   *this->requested.Append() = cid;
00800   assert(this->requested.Contains(cid));
00801   this->RequestContentList(1, &cid);
00802 }
00803 
00809 ContentInfo *ClientNetworkContentSocketHandler::GetContent(ContentID cid)
00810 {
00811   for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
00812     ContentInfo *ci = *iter;
00813     if (ci->id == cid) return ci;
00814   }
00815   return NULL;
00816 }
00817 
00818 
00823 void ClientNetworkContentSocketHandler::Select(ContentID cid)
00824 {
00825   ContentInfo *ci = this->GetContent(cid);
00826   if (ci == NULL || ci->state != ContentInfo::UNSELECTED) return;
00827 
00828   ci->state = ContentInfo::SELECTED;
00829   this->CheckDependencyState(ci);
00830 }
00831 
00836 void ClientNetworkContentSocketHandler::Unselect(ContentID cid)
00837 {
00838   ContentInfo *ci = this->GetContent(cid);
00839   if (ci == NULL || !ci->IsSelected()) return;
00840 
00841   ci->state = ContentInfo::UNSELECTED;
00842   this->CheckDependencyState(ci);
00843 }
00844 
00846 void ClientNetworkContentSocketHandler::SelectAll()
00847 {
00848   for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
00849     ContentInfo *ci = *iter;
00850     if (ci->state == ContentInfo::UNSELECTED) {
00851       ci->state = ContentInfo::SELECTED;
00852       this->CheckDependencyState(ci);
00853     }
00854   }
00855 }
00856 
00858 void ClientNetworkContentSocketHandler::SelectUpgrade()
00859 {
00860   for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
00861     ContentInfo *ci = *iter;
00862     if (ci->state == ContentInfo::UNSELECTED && ci->upgrade) {
00863       ci->state = ContentInfo::SELECTED;
00864       this->CheckDependencyState(ci);
00865     }
00866   }
00867 }
00868 
00870 void ClientNetworkContentSocketHandler::UnselectAll()
00871 {
00872   for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
00873     ContentInfo *ci = *iter;
00874     if (ci->IsSelected() && ci->state != ContentInfo::ALREADY_HERE) ci->state = ContentInfo::UNSELECTED;
00875   }
00876 }
00877 
00879 void ClientNetworkContentSocketHandler::ToggleSelectedState(const ContentInfo *ci)
00880 {
00881   switch (ci->state) {
00882     case ContentInfo::SELECTED:
00883     case ContentInfo::AUTOSELECTED:
00884       this->Unselect(ci->id);
00885       break;
00886 
00887     case ContentInfo::UNSELECTED:
00888       this->Select(ci->id);
00889       break;
00890 
00891     default:
00892       break;
00893   }
00894 }
00895 
00901 void ClientNetworkContentSocketHandler::ReverseLookupDependency(ConstContentVector &parents, const ContentInfo *child) const
00902 {
00903   for (ConstContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
00904     const ContentInfo *ci = *iter;
00905     if (ci == child) continue;
00906 
00907     for (uint i = 0; i < ci->dependency_count; i++) {
00908       if (ci->dependencies[i] == child->id) {
00909         *parents.Append() = ci;
00910         break;
00911       }
00912     }
00913   }
00914 }
00915 
00921 void ClientNetworkContentSocketHandler::ReverseLookupTreeDependency(ConstContentVector &tree, const ContentInfo *child) const
00922 {
00923   *tree.Append() = child;
00924 
00925   /* First find all direct parents. We can't use the "normal" iterator as
00926    * we are including stuff into the vector and as such the vector's data
00927    * store can be reallocated (and thus move), which means out iterating
00928    * pointer gets invalid. So fall back to the indices. */
00929   for (uint i = 0; i < tree.Length(); i++) {
00930     ConstContentVector parents;
00931     this->ReverseLookupDependency(parents, tree[i]);
00932 
00933     for (ConstContentIterator piter = parents.Begin(); piter != parents.End(); piter++) {
00934       tree.Include(*piter);
00935     }
00936   }
00937 }
00938 
00943 void ClientNetworkContentSocketHandler::CheckDependencyState(ContentInfo *ci)
00944 {
00945   if (ci->IsSelected() || ci->state == ContentInfo::ALREADY_HERE) {
00946     /* Selection is easy; just walk all children and set the
00947      * autoselected state. That way we can see what we automatically
00948      * selected and thus can unselect when a dependency is removed. */
00949     for (uint i = 0; i < ci->dependency_count; i++) {
00950       ContentInfo *c = this->GetContent(ci->dependencies[i]);
00951       if (c == NULL) {
00952         this->DownloadContentInfo(ci->dependencies[i]);
00953       } else if (c->state == ContentInfo::UNSELECTED) {
00954         c->state = ContentInfo::AUTOSELECTED;
00955         this->CheckDependencyState(c);
00956       }
00957     }
00958     return;
00959   }
00960 
00961   if (ci->state != ContentInfo::UNSELECTED) return;
00962 
00963   /* For unselection we need to find the parents of us. We need to
00964    * unselect them. After that we unselect all children that we
00965    * depend on and are not used as dependency for us, but only when
00966    * we automatically selected them. */
00967   ConstContentVector parents;
00968   this->ReverseLookupDependency(parents, ci);
00969   for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
00970     const ContentInfo *c = *iter;
00971     if (!c->IsSelected()) continue;
00972 
00973     this->Unselect(c->id);
00974   }
00975 
00976   for (uint i = 0; i < ci->dependency_count; i++) {
00977     const ContentInfo *c = this->GetContent(ci->dependencies[i]);
00978     if (c == NULL) {
00979       DownloadContentInfo(ci->dependencies[i]);
00980       continue;
00981     }
00982     if (c->state != ContentInfo::AUTOSELECTED) continue;
00983 
00984     /* Only unselect when WE are the only parent. */
00985     parents.Clear();
00986     this->ReverseLookupDependency(parents, c);
00987 
00988     /* First check whether anything depends on us */
00989     int sel_count = 0;
00990     bool force_selection = false;
00991     for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
00992       if ((*iter)->IsSelected()) sel_count++;
00993       if ((*iter)->state == ContentInfo::SELECTED) force_selection = true;
00994     }
00995     if (sel_count == 0) {
00996       /* Nothing depends on us */
00997       this->Unselect(c->id);
00998       continue;
00999     }
01000     /* Something manually selected depends directly on us */
01001     if (force_selection) continue;
01002 
01003     /* "Flood" search to find all items in the dependency graph*/
01004     parents.Clear();
01005     this->ReverseLookupTreeDependency(parents, c);
01006 
01007     /* Is there anything that is "force" selected?, if so... we're done. */
01008     for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
01009       if ((*iter)->state != ContentInfo::SELECTED) continue;
01010 
01011       force_selection = true;
01012       break;
01013     }
01014 
01015     /* So something depended directly on us */
01016     if (force_selection) continue;
01017 
01018     /* Nothing depends on us, mark the whole graph as unselected.
01019      * After that's done run over them once again to test their children
01020      * to unselect. Don't do it immediately because it'll do exactly what
01021      * we're doing now. */
01022     for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
01023       const ContentInfo *c = *iter;
01024       if (c->state == ContentInfo::AUTOSELECTED) this->Unselect(c->id);
01025     }
01026     for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
01027       this->CheckDependencyState(this->GetContent((*iter)->id));
01028     }
01029   }
01030 }
01031 
01033 void ClientNetworkContentSocketHandler::Clear()
01034 {
01035   for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) delete *iter;
01036 
01037   this->infos.Clear();
01038   this->requested.Clear();
01039 }
01040 
01041 /*** CALLBACK ***/
01042 
01043 void ClientNetworkContentSocketHandler::OnConnect(bool success)
01044 {
01045   for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
01046     ContentCallback *cb = *iter;
01047     cb->OnConnect(success);
01048     if (iter != this->callbacks.End() && *iter == cb) iter++;
01049   }
01050 }
01051 
01052 void ClientNetworkContentSocketHandler::OnDisconnect()
01053 {
01054   for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
01055     ContentCallback *cb = *iter;
01056     cb->OnDisconnect();
01057     if (iter != this->callbacks.End() && *iter == cb) iter++;
01058   }
01059 }
01060 
01061 void ClientNetworkContentSocketHandler::OnReceiveContentInfo(const ContentInfo *ci)
01062 {
01063   for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
01064     ContentCallback *cb = *iter;
01065     cb->OnReceiveContentInfo(ci);
01066     if (iter != this->callbacks.End() && *iter == cb) iter++;
01067   }
01068 }
01069 
01070 void ClientNetworkContentSocketHandler::OnDownloadProgress(const ContentInfo *ci, int bytes)
01071 {
01072   for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
01073     ContentCallback *cb = *iter;
01074     cb->OnDownloadProgress(ci, bytes);
01075     if (iter != this->callbacks.End() && *iter == cb) iter++;
01076   }
01077 }
01078 
01079 void ClientNetworkContentSocketHandler::OnDownloadComplete(ContentID cid)
01080 {
01081   ContentInfo *ci = this->GetContent(cid);
01082   if (ci != NULL) {
01083     ci->state = ContentInfo::ALREADY_HERE;
01084   }
01085 
01086   for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
01087     ContentCallback *cb = *iter;
01088     cb->OnDownloadComplete(cid);
01089     if (iter != this->callbacks.End() && *iter == cb) iter++;
01090   }
01091 }
01092 
01093 #endif /* ENABLE_NETWORK */