# network_content.cpp

Go to the documentation of this file.

    00001 /* $Id: network_content.cpp 19278 2010-02-27 16:41:10Z 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 "../window_func.h"
    00018 #include "../gui.h"
    00019 #include "../variables.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 TarListAddFile(const char *filename);
    00031 extern bool HasScenario(const ContentInfo *ci, bool md5sum);
    00032 ClientNetworkContentSocketHandler _network_content_client;
    00033
    00035 static bool HasGRFConfig(const ContentInfo *ci, bool md5sum)
    00036 {
    00037   return FindGRFConfig(BSWAP32(ci->unique_id), md5sum ? ci->md5sum : NULL) != NULL;
    00038 }
    00039
    00047 typedef bool (*HasProc)(const ContentInfo *ci, bool md5sum);
    00048
    00049 DEF_CONTENT_RECEIVE_COMMAND(Client, PACKET_CONTENT_SERVER_INFO)
    00050 {
    00051   ContentInfo *ci = new ContentInfo();
    00052   ci->type     = (ContentType)p->Recv_uint8();
    00053   ci->id       = (ContentID)p->Recv_uint32();
    00054   ci->filesize = p->Recv_uint32();
    00055
    00056   p->Recv_string(ci->name, lengthof(ci->name));
    00057   p->Recv_string(ci->version, lengthof(ci->name));
    00058   p->Recv_string(ci->url, lengthof(ci->url));
    00059   p->Recv_string(ci->description, lengthof(ci->description),  true);
    00060
    00061   ci->unique_id = p->Recv_uint32();
    00062   for (uint j = 0; j < sizeof(ci->md5sum); j++) {
    00063     ci->md5sum[j] = p->Recv_uint8();
    00064   }
    00065
    00066   ci->dependency_count = p->Recv_uint8();
    00067   ci->dependencies = MallocT<ContentID>(ci->dependency_count);
    00068   for (uint i = 0; i < ci->dependency_count; i++) ci->dependencies[i] = (ContentID)p->Recv_uint32();
    00069
    00070   ci->tag_count = p->Recv_uint8();
    00071   ci->tags = MallocT<char[32]>(ci->tag_count);
    00072   for (uint i = 0; i < ci->tag_count; i++) p->Recv_string(ci->tags[i], lengthof(*ci->tags));
    00073
    00074   if (!ci->IsValid()) {
    00075     delete ci;
    00076     this->Close();
    00077     return false;
    00078   }
    00079
    00080   /* Find the appropriate check function */
    00081   HasProc proc = NULL;
    00082   switch (ci->type) {
    00083     case CONTENT_TYPE_NEWGRF:
    00084       proc = HasGRFConfig;
    00085       break;
    00086
    00087     case CONTENT_TYPE_BASE_GRAPHICS:
    00088       proc = BaseGraphics::HasSet;
    00089       break;
    00090
    00091     case CONTENT_TYPE_BASE_MUSIC:
    00092       proc = BaseMusic::HasSet;
    00093       break;
    00094
    00095     case CONTENT_TYPE_BASE_SOUNDS:
    00096       proc = BaseSounds::HasSet;
    00097       break;
    00098
    00099     case CONTENT_TYPE_AI:
    00100     case CONTENT_TYPE_AI_LIBRARY:
    00101       proc = AI::HasAI; break;
    00102       break;
    00103
    00104     case CONTENT_TYPE_SCENARIO:
    00105     case CONTENT_TYPE_HEIGHTMAP:
    00106       proc = HasScenario;
    00107       break;
    00108
    00109     default:
    00110       break;
    00111   }
    00112
    00113   if (proc != NULL) {
    00114     if (proc(ci, true)) {
    00115       ci->state = ContentInfo::ALREADY_HERE;
    00116     } else {
    00117       ci->state = ContentInfo::UNSELECTED;
    00118       if (proc(ci, false)) ci->upgrade = true;
    00119     }
    00120   } else {
    00121     ci->state = ContentInfo::UNSELECTED;
    00122   }
    00123
    00124   /* Something we don't have and has filesize 0 does not exist in te system */
    00125   if (ci->state == ContentInfo::UNSELECTED && ci->filesize == 0) ci->state = ContentInfo::DOES_NOT_EXIST;
    00126
    00127   /* Do we already have a stub for this? */
    00128   for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
    00129     ContentInfo *ici = *iter;
    00130     if (ici->type == ci->type && ici->unique_id == ci->unique_id &&
    00131         memcmp(ci->md5sum, ici->md5sum, sizeof(ci->md5sum)) == 0) {
    00132       /* Preserve the name if possible */
    00133       if (StrEmpty(ci->name)) strecpy(ci->name, ici->name, lastof(ci->name));
    00134       if (ici->IsSelected()) ci->state = ici->state;
    00135
    00136       delete ici;
    00137       *iter = ci;
    00138
    00139       this->OnReceiveContentInfo(ci);
    00140       return true;
    00141     }
    00142   }
    00143
    00144   /* Missing content info? Don't list it */
    00145   if (ci->filesize == 0) {
    00146     delete ci;
    00147     return true;
    00148   }
    00149
    00150   *this->infos.Append() = ci;
    00151
    00152   /* Incoming data means that we might need to reconsider dependencies */
    00153   for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
    00154     this->CheckDependencyState(*iter);
    00155   }
    00156
    00157   this->OnReceiveContentInfo(ci);
    00158
    00159   return true;
    00160 }
    00161
    00162 void ClientNetworkContentSocketHandler::RequestContentList(ContentType type)
    00163 {
    00164   if (type == CONTENT_TYPE_END) {
    00165     this->RequestContentList(CONTENT_TYPE_BASE_GRAPHICS);
    00166     this->RequestContentList(CONTENT_TYPE_BASE_MUSIC);
    00167     this->RequestContentList(CONTENT_TYPE_BASE_SOUNDS);
    00168     this->RequestContentList(CONTENT_TYPE_SCENARIO);
    00169     this->RequestContentList(CONTENT_TYPE_HEIGHTMAP);
    00170 #ifdef ENABLE_AI
    00171     this->RequestContentList(CONTENT_TYPE_AI);
    00172     this->RequestContentList(CONTENT_TYPE_AI_LIBRARY);
    00173 #endif /* ENABLE_AI */
    00174     this->RequestContentList(CONTENT_TYPE_NEWGRF);
    00175     return;
    00176   }
    00177
    00178   this->Connect();
    00179
    00180   Packet *p = new Packet(PACKET_CONTENT_CLIENT_INFO_LIST);
    00181   p->Send_uint8 ((byte)type);
    00182   p->Send_uint32(_openttd_newgrf_version);
    00183
    00184   this->Send_Packet(p);
    00185 }
    00186
    00187 void ClientNetworkContentSocketHandler::RequestContentList(uint count, const ContentID *content_ids)
    00188 {
    00189   this->Connect();
    00190
    00191   while (count > 0) {
    00192     /* We can "only" send a limited number of IDs in a single packet.
    00193      * A packet begins with the packet size and a byte for the type.
    00194      * Then this packet adds a byte for the content type and a uint16
    00195      * for the count in this packet. The rest of the packet can be
    00196      * used for the IDs. */
    00197     uint p_count = min(count, (SEND_MTU - sizeof(PacketSize) - sizeof(byte) - sizeof(byte) - sizeof(uint16)) / sizeof(uint32));
    00198
    00199     Packet *p = new Packet(PACKET_CONTENT_CLIENT_INFO_ID);
    00200     p->Send_uint16(p_count);
    00201
    00202     for (uint i = 0; i < p_count; i++) {
    00203       p->Send_uint32(content_ids[i]);
    00204     }
    00205
    00206     this->Send_Packet(p);
    00207     count -= p_count;
    00208     content_ids += p_count;
    00209   }
    00210 }
    00211
    00212 void ClientNetworkContentSocketHandler::RequestContentList(ContentVector *cv, bool send_md5sum)
    00213 {
    00214   if (cv == NULL) return;
    00215
    00216   this->Connect();
    00217
    00218   /* 20 is sizeof(uint32) + sizeof(md5sum (byte[16])) */
    00219   assert(cv->Length() < 255);
    00220   assert(cv->Length() < (SEND_MTU - sizeof(PacketSize) - sizeof(byte) - sizeof(uint8)) / (send_md5sum ? 20 : sizeof(uint32)));
    00221
    00222   Packet *p = new Packet(send_md5sum ? PACKET_CONTENT_CLIENT_INFO_EXTID_MD5 : PACKET_CONTENT_CLIENT_INFO_EXTID);
    00223   p->Send_uint8(cv->Length());
    00224
    00225   for (ContentIterator iter = cv->Begin(); iter != cv->End(); iter++) {
    00226     const ContentInfo *ci = *iter;
    00227     p->Send_uint8((byte)ci->type);
    00228     p->Send_uint32(ci->unique_id);
    00229     if (!send_md5sum) continue;
    00230
    00231     for (uint j = 0; j < sizeof(ci->md5sum); j++) {
    00232       p->Send_uint8(ci->md5sum[j]);
    00233     }
    00234   }
    00235
    00236   this->Send_Packet(p);
    00237
    00238   for (ContentIterator iter = cv->Begin(); iter != cv->End(); iter++) {
    00239     ContentInfo *ci = *iter;
    00240     bool found = false;
    00241     for (ContentIterator iter2 = this->infos.Begin(); iter2 != this->infos.End(); iter2++) {
    00242       ContentInfo *ci2 = *iter2;
    00243       if (ci->type == ci2->type && ci->unique_id == ci2->unique_id &&
    00244           (!send_md5sum || memcmp(ci->md5sum, ci2->md5sum, sizeof(ci->md5sum)) == 0)) {
    00245         found = true;
    00246         break;
    00247       }
    00248     }
    00249     if (!found) {
    00250       *this->infos.Append() = ci;
    00251     } else {
    00252       delete ci;
    00253     }
    00254   }
    00255 }
    00256
    00257 void ClientNetworkContentSocketHandler::DownloadSelectedContent(uint &files, uint &bytes, bool fallback)
    00258 {
    00259   bytes = 0;
    00260
    00261   ContentIDList content;
    00262   for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
    00263     const ContentInfo *ci = *iter;
    00264     if (!ci->IsSelected() || ci->state == ContentInfo::ALREADY_HERE) continue;
    00265
    00266     *content.Append() = ci->id;
    00267     bytes += ci->filesize;
    00268   }
    00269
    00270   files = content.Length();
    00271
    00272   /* If there's nothing to download, do nothing. */
    00273   if (files == 0) return;
    00274
    00275   if (_settings_client.network.no_http_content_downloads || fallback) {
    00276     this->DownloadSelectedContentFallback(content);
    00277   } else {
    00278     this->DownloadSelectedContentHTTP(content);
    00279   }
    00280 }
    00281
    00282 void ClientNetworkContentSocketHandler::DownloadSelectedContentHTTP(const ContentIDList &content)
    00283 {
    00284   uint count = content.Length();
    00285
    00286   /* Allocate memory for the whole request.
    00287    * Requests are "id\nid\n..." (as strings), so assume the maximum ID,
    00288    * which is uint32 so 10 characters long. Then the newlines and
    00289    * multiply that all with the count and then add the '\0'. */
    00290   uint bytes = (10 + 1) * count + 1;
    00291   char *content_request = MallocT<char>(bytes);
    00292   const char *lastof = content_request + bytes - 1;
    00293
    00294   char *p = content_request;
    00295   for (const ContentID *id = content.Begin(); id != content.End(); id++) {
    00296     p += seprintf(p, lastof, "%d\n", *id);
    00297   }
    00298
    00299   this->http_response_index = -1;
    00300
    00301   NetworkAddress address(NETWORK_CONTENT_MIRROR_HOST, NETWORK_CONTENT_MIRROR_PORT);
    00302   new NetworkHTTPContentConnecter(address, this, NETWORK_CONTENT_MIRROR_URL, content_request);
    00303   /* NetworkHTTPContentConnecter takes over freeing of content_request! */
    00304 }
    00305
    00306 void ClientNetworkContentSocketHandler::DownloadSelectedContentFallback(const ContentIDList &content)
    00307 {
    00308   uint count = content.Length();
    00309   const ContentID *content_ids = content.Begin();
    00310   this->Connect();
    00311
    00312   while (count > 0) {
    00313     /* We can "only" send a limited number of IDs in a single packet.
    00314      * A packet begins with the packet size and a byte for the type.
    00315      * Then this packet adds a uint16 for the count in this packet.
    00316      * The rest of the packet can be used for the IDs. */
    00317     uint p_count = min(count, (SEND_MTU - sizeof(PacketSize) - sizeof(byte) - sizeof(uint16)) / sizeof(uint32));
    00318
    00319     Packet *p = new Packet(PACKET_CONTENT_CLIENT_CONTENT);
    00320     p->Send_uint16(p_count);
    00321
    00322     for (uint i = 0; i < p_count; i++) {
    00323       p->Send_uint32(content_ids[i]);
    00324     }
    00325
    00326     this->Send_Packet(p);
    00327     count -= p_count;
    00328     content_ids += p_count;
    00329   }
    00330 }
    00331
    00339 static char *GetFullFilename(const ContentInfo *ci, bool compressed)
    00340 {
    00341   Subdirectory dir;
    00342   switch (ci->type) {
    00343     default: return NULL;
    00344     case CONTENT_TYPE_BASE_GRAPHICS: dir = DATA_DIR;       break;
    00345     case CONTENT_TYPE_BASE_MUSIC:    dir = GM_DIR;         break;
    00346     case CONTENT_TYPE_BASE_SOUNDS:   dir = DATA_DIR;       break;
    00347     case CONTENT_TYPE_NEWGRF:        dir = DATA_DIR;       break;
    00348     case CONTENT_TYPE_AI:            dir = AI_DIR;         break;
    00349     case CONTENT_TYPE_AI_LIBRARY:    dir = AI_LIBRARY_DIR; break;
    00350     case CONTENT_TYPE_SCENARIO:      dir = SCENARIO_DIR;   break;
    00351     case CONTENT_TYPE_HEIGHTMAP:     dir = HEIGHTMAP_DIR;  break;
    00352   }
    00353
    00354   static char buf[MAX_PATH];
    00355   FioGetFullPath(buf, lengthof(buf), SP_AUTODOWNLOAD_DIR, dir, ci->filename);
    00356   strecat(buf, compressed ? ".tar.gz" : ".tar", lastof(buf));
    00357
    00358   return buf;
    00359 }
    00360
    00366 static bool GunzipFile(const ContentInfo *ci)
    00367 {
    00368 #if defined(WITH_ZLIB)
    00369   bool ret = true;
    00370   FILE *ftmp = fopen(GetFullFilename(ci, true), "rb");
    00371   gzFile fin = gzdopen(fileno(ftmp), "rb");
    00372   FILE *fout = fopen(GetFullFilename(ci, false), "wb");
    00373
    00374   if (fin == NULL || fout == NULL) {
    00375     ret = false;
    00376     goto exit;
    00377   }
    00378
    00379   byte buff[8192];
    00380   while (!gzeof(fin)) {
    00381     int read = gzread(fin, buff, sizeof(buff));
    00382     if (read < 0 || (size_t)read != fwrite(buff, 1, read, fout)) {
    00383       ret = false;
    00384       break;
    00385     }
    00386   }
    00387
    00388 exit:
    00389   if (fin != NULL) {
    00390     /* Closes ftmp too! */
    00391     gzclose(fin);
    00392   } else if (ftmp != NULL) {
    00393     /* In case the gz stream was opened correctly this will
    00394      * be closed by gzclose. */
    00395     fclose(ftmp);
    00396   }
    00397   if (fout != NULL) fclose(fout);
    00398
    00399   return ret;
    00400 #else
    00401   NOT_REACHED();
    00402 #endif /* defined(WITH_ZLIB) */
    00403 }
    00404
    00405 DEF_CONTENT_RECEIVE_COMMAND(Client, PACKET_CONTENT_SERVER_CONTENT)
    00406 {
    00407   if (this->curFile == NULL) {
    00408     delete this->curInfo;
    00409     /* When we haven't opened a file this must be our first packet with metadata. */
    00410     this->curInfo = new ContentInfo;
    00411     this->curInfo->type     = (ContentType)p->Recv_uint8();
    00412     this->curInfo->id       = (ContentID)p->Recv_uint32();
    00413     this->curInfo->filesize = p->Recv_uint32();
    00414     p->Recv_string(this->curInfo->filename, lengthof(this->curInfo->filename));
    00415
    00416     if (!this->BeforeDownload()) {
    00417       this->Close();
    00418       return false;
    00419     }
    00420   } else {
    00421     /* We have a file opened, thus are downloading internal content */
    00422     size_t toRead = (size_t)(p->size - p->pos);
    00423     if (fwrite(p->buffer + p->pos, 1, toRead, this->curFile) != toRead) {
    00424       DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
    00425       ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, 0, 0);
    00426       this->Close();
    00427       fclose(this->curFile);
    00428       this->curFile = NULL;
    00429
    00430       return false;
    00431     }
    00432
    00433     this->OnDownloadProgress(this->curInfo, (uint)toRead);
    00434
    00435     if (toRead == 0) this->AfterDownload();
    00436   }
    00437
    00438   return true;
    00439 }
    00440
    00445 bool ClientNetworkContentSocketHandler::BeforeDownload()
    00446 {
    00447   if (!this->curInfo->IsValid()) {
    00448     delete this->curInfo;
    00449     this->curInfo = NULL;
    00450     return false;
    00451   }
    00452
    00453   if (this->curInfo->filesize != 0) {
    00454     /* The filesize is > 0, so we are going to download it */
    00455     const char *filename = GetFullFilename(this->curInfo, true);
    00456     if (filename == NULL) {
    00457       /* Unless that fails ofcourse... */
    00458       DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
    00459       ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, 0, 0);
    00460       return false;
    00461     }
    00462
    00463     this->curFile = fopen(filename, "wb");
    00464   }
    00465   return true;
    00466 }
    00467
    00472 void ClientNetworkContentSocketHandler::AfterDownload()
    00473 {
    00474   /* We read nothing; that's our marker for end-of-stream.
    00475    * Now gunzip the tar and make it known. */
    00476   fclose(this->curFile);
    00477   this->curFile = NULL;
    00478
    00479   if (GunzipFile(this->curInfo)) {
    00480     unlink(GetFullFilename(this->curInfo, true));
    00481
    00482     TarListAddFile(GetFullFilename(this->curInfo, false));
    00483
    00484     if (this->curInfo->type == CONTENT_TYPE_BASE_MUSIC) {
    00485       /* Music can't be in a tar. So extract the tar! */
    00486       ExtractTar(GetFullFilename(this->curInfo, false));
    00487       unlink(GetFullFilename(this->curInfo, false));
    00488     }
    00489
    00490     this->OnDownloadComplete(this->curInfo->id);
    00491   } else {
    00492     ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_EXTRACT, INVALID_STRING_ID, 0, 0);
    00493   }
    00494 }
    00495
    00496 /* Also called to just clean up the mess. */
    00497 void ClientNetworkContentSocketHandler::OnFailure()
    00498 {
    00499   /* If we fail, download the rest via the 'old' system. */
    00500   uint files, bytes;
    00501   this->DownloadSelectedContent(files, bytes, true);
    00502
    00503   this->http_response.Reset();
    00504   this->http_response_index = -2;
    00505
    00506   if (this->curFile != NULL) {
    00507     fclose(this->curFile);
    00508     this->curFile = NULL;
    00509   }
    00510 }
    00511
    00512 void ClientNetworkContentSocketHandler::OnReceiveData(const char *data, size_t length)
    00513 {
    00514   assert(data == NULL || length != 0);
    00515
    00516   /* Ignore any latent data coming from a connection we closed. */
    00517   if (this->http_response_index == -2) return;
    00518
    00519   if (this->http_response_index == -1) {
    00520     if (data != NULL) {
    00521       /* Append the rest of the response. */
    00522       memcpy(this->http_response.Append((uint)length), data, length);
    00523       return;
    00524     } else {
    00525       /* Make sure the response is properly terminated. */
    00526       *this->http_response.Append() = '\0';
    00527
    00528       /* And prepare for receiving the rest of the data. */
    00529       this->http_response_index = 0;
    00530     }
    00531   }
    00532
    00533   if (data != NULL) {
    00534     /* We have data, so write it to the file. */
    00535     if (fwrite(data, 1, length, this->curFile) != length) {
    00536       /* Writing failed somehow, let try via the old method. */
    00537       this->OnFailure();
    00538     } else {
    00539       /* Just received the data. */
    00540       this->OnDownloadProgress(this->curInfo, (uint)length);
    00541     }
    00542     /* Nothing more to do now. */
    00543     return;
    00544   }
    00545
    00546   if (this->curFile != NULL) {
    00547     /* We've finished downloading a file. */
    00548     this->AfterDownload();
    00549   }
    00550
    00551   if ((uint)this->http_response_index >= this->http_response.Length()) {
    00552     /* It's not a real failure, but if there's
    00553      * nothing more to download it helps with
    00554      * cleaning up the stuff we allocated. */
    00555     this->OnFailure();
    00556     return;
    00557   }
    00558
    00559   delete this->curInfo;
    00560   /* When we haven't opened a file this must be our first packet with metadata. */
    00561   this->curInfo = new ContentInfo;
    00562
    00564 #define check_not_null(p) { if ((p) == NULL) { this->OnFailure(); return; } }
    00565
    00566 #define check_and_terminate(p) { check_not_null(p); *(p) = '\0'; }
    00567
    00568   for (;;) {
    00569     char *str = this->http_response.Begin() + this->http_response_index;
    00570     char *p = strchr(str, '\n');
    00571     check_and_terminate(p);
    00572
    00573     /* Update the index for the next one */
    00574     this->http_response_index += (int)strlen(str) + 1;
    00575
    00576     /* Read the ID */
    00577     p = strchr(str, ',');
    00578     check_and_terminate(p);
    00579     this->curInfo->id = (ContentID)atoi(str);
    00580
    00581     /* Read the type */
    00582     str = p + 1;
    00583     p = strchr(str, ',');
    00584     check_and_terminate(p);
    00585     this->curInfo->type = (ContentType)atoi(str);
    00586
    00587     /* Read the file size */
    00588     str = p + 1;
    00589     p = strchr(str, ',');
    00590     check_and_terminate(p);
    00591     this->curInfo->filesize = atoi(str);
    00592
    00593     /* Read the URL */
    00594     str = p + 1;
    00595     /* Is it a fallback URL? If so, just continue with the next one. */
    00596     if (strncmp(str, "ottd", 4) == 0) {
    00597       if ((uint)this->http_response_index >= this->http_response.Length()) {
    00598         /* Have we gone through all lines? */
    00599         this->OnFailure();
    00600         return;
    00601       }
    00602       continue;
    00603     }
    00604
    00605     p = strrchr(str, '/');
    00606     check_not_null(p);
    00607
    00608     char tmp[MAX_PATH];
    00609     if (strecpy(tmp, p, lastof(tmp)) == lastof(tmp)) {
    00610       this->OnFailure();
    00611       return;
    00612     }
    00613     /* Remove the extension from the string. */
    00614     for (uint i = 0; i < 2; i++) {
    00615       p = strrchr(tmp, '.');
    00616       check_and_terminate(p);
    00617     }
    00618
    00619     /* Copy the string, without extension, to the filename. */
    00620     strecpy(this->curInfo->filename, tmp, lastof(this->curInfo->filename));
    00621
    00622     /* Request the next file. */
    00623     if (!this->BeforeDownload()) {
    00624       this->OnFailure();
    00625       return;
    00626     }
    00627
    00628     NetworkHTTPSocketHandler::Connect(str, this);
    00629     return;
    00630   }
    00631
    00632 #undef check
    00633 #undef check_and_terminate
    00634 }
    00635
    00641 ClientNetworkContentSocketHandler::ClientNetworkContentSocketHandler() :
    00642   NetworkContentSocketHandler(),
    00643   http_response_index(-2),
    00644   curFile(NULL),
    00645   curInfo(NULL),
    00646   isConnecting(false)
    00647 {
    00648 }
    00649
    00651 ClientNetworkContentSocketHandler::~ClientNetworkContentSocketHandler()
    00652 {
    00653   delete this->curInfo;
    00654   if (this->curFile != NULL) fclose(this->curFile);
    00655
    00656   for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) delete *iter;
    00657 }
    00658
    00659 class NetworkContentConnecter : TCPConnecter {
    00660 public:
    00661   NetworkContentConnecter(const NetworkAddress &address) : TCPConnecter(address) {}
    00662
    00663   virtual void OnFailure()
    00664   {
    00665     _network_content_client.isConnecting = false;
    00666     _network_content_client.OnConnect(false);
    00667   }
    00668
    00669   virtual void OnConnect(SOCKET s)
    00670   {
    00671     assert(_network_content_client.sock == INVALID_SOCKET);
    00672     _network_content_client.isConnecting = false;
    00673     _network_content_client.sock = s;
    00674     _network_content_client.Reopen();
    00675     _network_content_client.OnConnect(true);
    00676   }
    00677 };
    00678
    00682 void ClientNetworkContentSocketHandler::Connect()
    00683 {
    00684   this->lastActivity = _realtime_tick;
    00685
    00686   if (this->sock != INVALID_SOCKET || this->isConnecting) return;
    00687   this->isConnecting = true;
    00688   new NetworkContentConnecter(NetworkAddress(NETWORK_CONTENT_SERVER_HOST, NETWORK_CONTENT_SERVER_PORT, AF_UNSPEC));
    00689 }
    00690
    00694 void ClientNetworkContentSocketHandler::Close()
    00695 {
    00696   if (this->sock == INVALID_SOCKET) return;
    00697   NetworkContentSocketHandler::Close();
    00698
    00699   this->OnDisconnect();
    00700 }
    00701
    00706 void ClientNetworkContentSocketHandler::SendReceive()
    00707 {
    00708   if (this->sock == INVALID_SOCKET || this->isConnecting) return;
    00709
    00710   if (this->lastActivity + IDLE_TIMEOUT < _realtime_tick) {
    00711     this->Close();
    00712     return;
    00713   }
    00714
    00715   fd_set read_fd, write_fd;
    00716   struct timeval tv;
    00717
    00718   FD_ZERO(&read_fd);
    00719   FD_ZERO(&write_fd);
    00720
    00721   FD_SET(this->sock, &read_fd);
    00722   FD_SET(this->sock, &write_fd);
    00723
    00724   tv.tv_sec = tv.tv_usec = 0; // don't block at all.
    00725 #if !defined(__MORPHOS__) && !defined(__AMIGA__)
    00726   select(FD_SETSIZE, &read_fd, &write_fd, NULL, &tv);
    00727 #else
    00728   WaitSelect(FD_SETSIZE, &read_fd, &write_fd, NULL, &tv, NULL);
    00729 #endif
    00730   if (FD_ISSET(this->sock, &read_fd)) {
    00731     this->Recv_Packets();
    00732     this->lastActivity = _realtime_tick;
    00733   }
    00734
    00735   this->writable = !!FD_ISSET(this->sock, &write_fd);
    00736   this->Send_Packets();
    00737 }
    00738
    00743 void ClientNetworkContentSocketHandler::DownloadContentInfo(ContentID cid)
    00744 {
    00745   /* When we tried to download it already, don't try again */
    00746   if (this->requested.Contains(cid)) return;
    00747
    00748   *this->requested.Append() = cid;
    00749   assert(this->requested.Contains(cid));
    00750   this->RequestContentList(1, &cid);
    00751 }
    00752
    00758 ContentInfo *ClientNetworkContentSocketHandler::GetContent(ContentID cid)
    00759 {
    00760   for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
    00761     ContentInfo *ci = *iter;
    00762     if (ci->id == cid) return ci;
    00763   }
    00764   return NULL;
    00765 }
    00766
    00767
    00772 void ClientNetworkContentSocketHandler::Select(ContentID cid)
    00773 {
    00774   ContentInfo *ci = this->GetContent(cid);
    00775   if (ci == NULL || ci->state != ContentInfo::UNSELECTED) return;
    00776
    00777   ci->state = ContentInfo::SELECTED;
    00778   this->CheckDependencyState(ci);
    00779 }
    00780
    00785 void ClientNetworkContentSocketHandler::Unselect(ContentID cid)
    00786 {
    00787   ContentInfo *ci = this->GetContent(cid);
    00788   if (ci == NULL || !ci->IsSelected()) return;
    00789
    00790   ci->state = ContentInfo::UNSELECTED;
    00791   this->CheckDependencyState(ci);
    00792 }
    00793
    00795 void ClientNetworkContentSocketHandler::SelectAll()
    00796 {
    00797   for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
    00798     ContentInfo *ci = *iter;
    00799     if (ci->state == ContentInfo::UNSELECTED) {
    00800       ci->state = ContentInfo::SELECTED;
    00801       this->CheckDependencyState(ci);
    00802     }
    00803   }
    00804 }
    00805
    00807 void ClientNetworkContentSocketHandler::SelectUpgrade()
    00808 {
    00809   for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
    00810     ContentInfo *ci = *iter;
    00811     if (ci->state == ContentInfo::UNSELECTED && ci->upgrade) {
    00812       ci->state = ContentInfo::SELECTED;
    00813       this->CheckDependencyState(ci);
    00814     }
    00815   }
    00816 }
    00817
    00819 void ClientNetworkContentSocketHandler::UnselectAll()
    00820 {
    00821   for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
    00822     ContentInfo *ci = *iter;
    00823     if (ci->IsSelected() && ci->state != ContentInfo::ALREADY_HERE) ci->state = ContentInfo::UNSELECTED;
    00824   }
    00825 }
    00826
    00828 void ClientNetworkContentSocketHandler::ToggleSelectedState(const ContentInfo *ci)
    00829 {
    00830   switch (ci->state) {
    00831     case ContentInfo::SELECTED:
    00832     case ContentInfo::AUTOSELECTED:
    00833       this->Unselect(ci->id);
    00834       break;
    00835
    00836     case ContentInfo::UNSELECTED:
    00837       this->Select(ci->id);
    00838       break;
    00839
    00840     default:
    00841       break;
    00842   }
    00843 }
    00844
    00850 void ClientNetworkContentSocketHandler::ReverseLookupDependency(ConstContentVector &parents, const ContentInfo *child) const
    00851 {
    00852   for (ConstContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
    00853     const ContentInfo *ci = *iter;
    00854     if (ci == child) continue;
    00855
    00856     for (uint i = 0; i < ci->dependency_count; i++) {
    00857       if (ci->dependencies[i] == child->id) {
    00858         *parents.Append() = ci;
    00859         break;
    00860       }
    00861     }
    00862   }
    00863 }
    00864
    00870 void ClientNetworkContentSocketHandler::ReverseLookupTreeDependency(ConstContentVector &tree, const ContentInfo *child) const
    00871 {
    00872   *tree.Append() = child;
    00873
    00874   /* First find all direct parents */
    00875   for (ConstContentIterator iter = tree.Begin(); iter != tree.End(); iter++) {
    00876     ConstContentVector parents;
    00877     this->ReverseLookupDependency(parents, *iter);
    00878
    00879     for (ConstContentIterator piter = parents.Begin(); piter != parents.End(); piter++) {
    00880       tree.Include(*piter);
    00881     }
    00882   }
    00883 }
    00884
    00889 void ClientNetworkContentSocketHandler::CheckDependencyState(ContentInfo *ci)
    00890 {
    00891   if (ci->IsSelected() || ci->state == ContentInfo::ALREADY_HERE) {
    00892     /* Selection is easy; just walk all children and set the
    00893      * autoselected state. That way we can see what we automatically
    00894      * selected and thus can unselect when a dependency is removed. */
    00895     for (uint i = 0; i < ci->dependency_count; i++) {
    00896       ContentInfo *c = this->GetContent(ci->dependencies[i]);
    00897       if (c == NULL) {
    00898         this->DownloadContentInfo(ci->dependencies[i]);
    00899       } else if (c->state == ContentInfo::UNSELECTED) {
    00900         c->state = ContentInfo::AUTOSELECTED;
    00901         this->CheckDependencyState(c);
    00902       }
    00903     }
    00904     return;
    00905   }
    00906
    00907   if (ci->state != ContentInfo::UNSELECTED) return;
    00908
    00909   /* For unselection we need to find the parents of us. We need to
    00910    * unselect them. After that we unselect all children that we
    00911    * depend on and are not used as dependency for us, but only when
    00912    * we automatically selected them. */
    00913   ConstContentVector parents;
    00914   this->ReverseLookupDependency(parents, ci);
    00915   for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
    00916     const ContentInfo *c = *iter;
    00917     if (!c->IsSelected()) continue;
    00918
    00919     this->Unselect(c->id);
    00920   }
    00921
    00922   for (uint i = 0; i < ci->dependency_count; i++) {
    00923     const ContentInfo *c = this->GetContent(ci->dependencies[i]);
    00924     if (c == NULL) {
    00925       DownloadContentInfo(ci->dependencies[i]);
    00926       continue;
    00927     }
    00928     if (c->state != ContentInfo::AUTOSELECTED) continue;
    00929
    00930     /* Only unselect when WE are the only parent. */
    00931     parents.Clear();
    00932     this->ReverseLookupDependency(parents, c);
    00933
    00934     /* First check whether anything depends on us */
    00935     int sel_count = 0;
    00936     bool force_selection = false;
    00937     for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
    00938       if ((*iter)->IsSelected()) sel_count++;
    00939       if ((*iter)->state == ContentInfo::SELECTED) force_selection = true;
    00940     }
    00941     if (sel_count == 0) {
    00942       /* Nothing depends on us */
    00943       this->Unselect(c->id);
    00944       continue;
    00945     }
    00946     /* Something manually selected depends directly on us */
    00947     if (force_selection) continue;
    00948
    00949     /* "Flood" search to find all items in the dependency graph*/
    00950     parents.Clear();
    00951     this->ReverseLookupTreeDependency(parents, c);
    00952
    00953     /* Is there anything that is "force" selected?, if so... we're done. */
    00954     for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
    00955       if ((*iter)->state != ContentInfo::SELECTED) continue;
    00956
    00957       force_selection = true;
    00958       break;
    00959     }
    00960
    00961     /* So something depended directly on us */
    00962     if (force_selection) continue;
    00963
    00964     /* Nothing depends on us, mark the whole graph as unselected.
    00965      * After that's done run over them once again to test their children
    00966      * to unselect. Don't do it immediatelly because it'll do exactly what
    00967      * we're doing now. */
    00968     for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
    00969       const ContentInfo *c = *iter;
    00970       if (c->state == ContentInfo::AUTOSELECTED) this->Unselect(c->id);
    00971     }
    00972     for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
    00973       this->CheckDependencyState(this->GetContent((*iter)->id));
    00974     }
    00975   }
    00976 }
    00977
    00978 void ClientNetworkContentSocketHandler::Clear()
    00979 {
    00980   for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) delete *iter;
    00981
    00982   this->infos.Clear();
    00983   this->requested.Clear();
    00984 }
    00985
    00986 /*** CALLBACK ***/
    00987
    00988 void ClientNetworkContentSocketHandler::OnConnect(bool success)
    00989 {
    00990   for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
    00991     ContentCallback *cb = *iter;
    00992     cb->OnConnect(success);
    00993     if (iter != this->callbacks.End() && *iter == cb) iter++;
    00994   }
    00995 }
    00996
    00997 void ClientNetworkContentSocketHandler::OnDisconnect()
    00998 {
    00999   for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
    01000     ContentCallback *cb = *iter;
    01001     cb->OnDisconnect();
    01002     if (iter != this->callbacks.End() && *iter == cb) iter++;
    01003   }
    01004 }
    01005
    01006 void ClientNetworkContentSocketHandler::OnReceiveContentInfo(const ContentInfo *ci)
    01007 {
    01008   for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
    01009     ContentCallback *cb = *iter;
    01010     cb->OnReceiveContentInfo(ci);
    01011     if (iter != this->callbacks.End() && *iter == cb) iter++;
    01012   }
    01013 }
    01014
    01015 void ClientNetworkContentSocketHandler::OnDownloadProgress(const ContentInfo *ci, uint bytes)
    01016 {
    01017   for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
    01018     ContentCallback *cb = *iter;
    01019     cb->OnDownloadProgress(ci, bytes);
    01020     if (iter != this->callbacks.End() && *iter == cb) iter++;
    01021   }
    01022 }
    01023
    01024 void ClientNetworkContentSocketHandler::OnDownloadComplete(ContentID cid)
    01025 {
    01026   ContentInfo *ci = this->GetContent(cid);
    01027   if (ci != NULL) {
    01028     ci->state = ContentInfo::ALREADY_HERE;
    01029   }
    01030
    01031   for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
    01032     ContentCallback *cb = *iter;
    01033     cb->OnDownloadComplete(cid);
    01034     if (iter != this->callbacks.End() && *iter == cb) iter++;
    01035   }
    01036 }
    01037
    01038 #endif /* ENABLE_NETWORK */