# network_content.cpp

Go to the documentation of this file.

    00001 /* $Id: network_content.cpp 19081 2010-02-10 16:24:05Z 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     this->OnDownloadComplete(this->curInfo->id);
    00485   } else {
    00486     ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_EXTRACT, INVALID_STRING_ID, 0, 0);
    00487   }
    00488 }
    00489
    00490 /* Also called to just clean up the mess. */
    00491 void ClientNetworkContentSocketHandler::OnFailure()
    00492 {
    00493   /* If we fail, download the rest via the 'old' system. */
    00494   uint files, bytes;
    00495   this->DownloadSelectedContent(files, bytes, true);
    00496
    00497   this->http_response.Reset();
    00498   this->http_response_index = -2;
    00499
    00500   if (this->curFile != NULL) {
    00501     fclose(this->curFile);
    00502     this->curFile = NULL;
    00503   }
    00504 }
    00505
    00506 void ClientNetworkContentSocketHandler::OnReceiveData(const char *data, size_t length)
    00507 {
    00508   assert(data == NULL || length != 0);
    00509
    00510   /* Ignore any latent data coming from a connection we closed. */
    00511   if (this->http_response_index == -2) return;
    00512
    00513   if (this->http_response_index == -1) {
    00514     if (data != NULL) {
    00515       /* Append the rest of the response. */
    00516       memcpy(this->http_response.Append((uint)length), data, length);
    00517       return;
    00518     } else {
    00519       /* Make sure the response is properly terminated. */
    00520       *this->http_response.Append() = '\0';
    00521
    00522       /* And prepare for receiving the rest of the data. */
    00523       this->http_response_index = 0;
    00524     }
    00525   }
    00526
    00527   if (data != NULL) {
    00528     /* We have data, so write it to the file. */
    00529     if (fwrite(data, 1, length, this->curFile) != length) {
    00530       /* Writing failed somehow, let try via the old method. */
    00531       this->OnFailure();
    00532     } else {
    00533       /* Just received the data. */
    00534       this->OnDownloadProgress(this->curInfo, (uint)length);
    00535     }
    00536     /* Nothing more to do now. */
    00537     return;
    00538   }
    00539
    00540   if (this->curFile != NULL) {
    00541     /* We've finished downloading a file. */
    00542     this->AfterDownload();
    00543   }
    00544
    00545   if ((uint)this->http_response_index >= this->http_response.Length()) {
    00546     /* It's not a real failure, but if there's
    00547      * nothing more to download it helps with
    00548      * cleaning up the stuff we allocated. */
    00549     this->OnFailure();
    00550     return;
    00551   }
    00552
    00553   delete this->curInfo;
    00554   /* When we haven't opened a file this must be our first packet with metadata. */
    00555   this->curInfo = new ContentInfo;
    00556
    00558 #define check_not_null(p) { if ((p) == NULL) { this->OnFailure(); return; } }
    00559
    00560 #define check_and_terminate(p) { check_not_null(p); *(p) = '\0'; }
    00561
    00562   for (;;) {
    00563     char *str = this->http_response.Begin() + this->http_response_index;
    00564     char *p = strchr(str, '\n');
    00565     check_and_terminate(p);
    00566
    00567     /* Update the index for the next one */
    00568     this->http_response_index += (int)strlen(str) + 1;
    00569
    00570     /* Read the ID */
    00571     p = strchr(str, ',');
    00572     check_and_terminate(p);
    00573     this->curInfo->id = (ContentID)atoi(str);
    00574
    00575     /* Read the type */
    00576     str = p + 1;
    00577     p = strchr(str, ',');
    00578     check_and_terminate(p);
    00579     this->curInfo->type = (ContentType)atoi(str);
    00580
    00581     /* Read the file size */
    00582     str = p + 1;
    00583     p = strchr(str, ',');
    00584     check_and_terminate(p);
    00585     this->curInfo->filesize = atoi(str);
    00586
    00587     /* Read the URL */
    00588     str = p + 1;
    00589     /* Is it a fallback URL? If so, just continue with the next one. */
    00590     if (strncmp(str, "ottd", 4) == 0) {
    00591       if ((uint)this->http_response_index >= this->http_response.Length()) {
    00592         /* Have we gone through all lines? */
    00593         this->OnFailure();
    00594         return;
    00595       }
    00596       continue;
    00597     }
    00598
    00599     p = strrchr(str, '/');
    00600     check_not_null(p);
    00601
    00602     char tmp[MAX_PATH];
    00603     if (strecpy(tmp, p, lastof(tmp)) == lastof(tmp)) {
    00604       this->OnFailure();
    00605       return;
    00606     }
    00607     /* Remove the extension from the string. */
    00608     for (uint i = 0; i < 2; i++) {
    00609       p = strrchr(tmp, '.');
    00610       check_and_terminate(p);
    00611     }
    00612
    00613     /* Copy the string, without extension, to the filename. */
    00614     strecpy(this->curInfo->filename, tmp, lastof(this->curInfo->filename));
    00615
    00616     /* Request the next file. */
    00617     if (!this->BeforeDownload()) {
    00618       this->OnFailure();
    00619       return;
    00620     }
    00621
    00622     NetworkHTTPSocketHandler::Connect(str, this);
    00623     return;
    00624   }
    00625
    00626 #undef check
    00627 #undef check_and_terminate
    00628 }
    00629
    00635 ClientNetworkContentSocketHandler::ClientNetworkContentSocketHandler() :
    00636   NetworkContentSocketHandler(),
    00637   http_response_index(-2),
    00638   curFile(NULL),
    00639   curInfo(NULL),
    00640   isConnecting(false)
    00641 {
    00642 }
    00643
    00645 ClientNetworkContentSocketHandler::~ClientNetworkContentSocketHandler()
    00646 {
    00647   delete this->curInfo;
    00648   if (this->curFile != NULL) fclose(this->curFile);
    00649
    00650   for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) delete *iter;
    00651 }
    00652
    00653 class NetworkContentConnecter : TCPConnecter {
    00654 public:
    00655   NetworkContentConnecter(const NetworkAddress &address) : TCPConnecter(address) {}
    00656
    00657   virtual void OnFailure()
    00658   {
    00659     _network_content_client.isConnecting = false;
    00660     _network_content_client.OnConnect(false);
    00661   }
    00662
    00663   virtual void OnConnect(SOCKET s)
    00664   {
    00665     assert(_network_content_client.sock == INVALID_SOCKET);
    00666     _network_content_client.isConnecting = false;
    00667     _network_content_client.sock = s;
    00668     _network_content_client.Reopen();
    00669     _network_content_client.OnConnect(true);
    00670   }
    00671 };
    00672
    00676 void ClientNetworkContentSocketHandler::Connect()
    00677 {
    00678   this->lastActivity = _realtime_tick;
    00679
    00680   if (this->sock != INVALID_SOCKET || this->isConnecting) return;
    00681   this->isConnecting = true;
    00682   new NetworkContentConnecter(NetworkAddress(NETWORK_CONTENT_SERVER_HOST, NETWORK_CONTENT_SERVER_PORT, AF_UNSPEC));
    00683 }
    00684
    00688 void ClientNetworkContentSocketHandler::Close()
    00689 {
    00690   if (this->sock == INVALID_SOCKET) return;
    00691   NetworkContentSocketHandler::Close();
    00692
    00693   this->OnDisconnect();
    00694 }
    00695
    00700 void ClientNetworkContentSocketHandler::SendReceive()
    00701 {
    00702   if (this->sock == INVALID_SOCKET || this->isConnecting) return;
    00703
    00704   if (this->lastActivity + IDLE_TIMEOUT < _realtime_tick) {
    00705     this->Close();
    00706     return;
    00707   }
    00708
    00709   fd_set read_fd, write_fd;
    00710   struct timeval tv;
    00711
    00712   FD_ZERO(&read_fd);
    00713   FD_ZERO(&write_fd);
    00714
    00715   FD_SET(this->sock, &read_fd);
    00716   FD_SET(this->sock, &write_fd);
    00717
    00718   tv.tv_sec = tv.tv_usec = 0; // don't block at all.
    00719 #if !defined(__MORPHOS__) && !defined(__AMIGA__)
    00720   select(FD_SETSIZE, &read_fd, &write_fd, NULL, &tv);
    00721 #else
    00722   WaitSelect(FD_SETSIZE, &read_fd, &write_fd, NULL, &tv, NULL);
    00723 #endif
    00724   if (FD_ISSET(this->sock, &read_fd)) {
    00725     this->Recv_Packets();
    00726     this->lastActivity = _realtime_tick;
    00727   }
    00728
    00729   this->writable = !!FD_ISSET(this->sock, &write_fd);
    00730   this->Send_Packets();
    00731 }
    00732
    00737 void ClientNetworkContentSocketHandler::DownloadContentInfo(ContentID cid)
    00738 {
    00739   /* When we tried to download it already, don't try again */
    00740   if (this->requested.Contains(cid)) return;
    00741
    00742   *this->requested.Append() = cid;
    00743   assert(this->requested.Contains(cid));
    00744   this->RequestContentList(1, &cid);
    00745 }
    00746
    00752 ContentInfo *ClientNetworkContentSocketHandler::GetContent(ContentID cid)
    00753 {
    00754   for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
    00755     ContentInfo *ci = *iter;
    00756     if (ci->id == cid) return ci;
    00757   }
    00758   return NULL;
    00759 }
    00760
    00761
    00766 void ClientNetworkContentSocketHandler::Select(ContentID cid)
    00767 {
    00768   ContentInfo *ci = this->GetContent(cid);
    00769   if (ci == NULL || ci->state != ContentInfo::UNSELECTED) return;
    00770
    00771   ci->state = ContentInfo::SELECTED;
    00772   this->CheckDependencyState(ci);
    00773 }
    00774
    00779 void ClientNetworkContentSocketHandler::Unselect(ContentID cid)
    00780 {
    00781   ContentInfo *ci = this->GetContent(cid);
    00782   if (ci == NULL || !ci->IsSelected()) return;
    00783
    00784   ci->state = ContentInfo::UNSELECTED;
    00785   this->CheckDependencyState(ci);
    00786 }
    00787
    00789 void ClientNetworkContentSocketHandler::SelectAll()
    00790 {
    00791   for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
    00792     ContentInfo *ci = *iter;
    00793     if (ci->state == ContentInfo::UNSELECTED) {
    00794       ci->state = ContentInfo::SELECTED;
    00795       this->CheckDependencyState(ci);
    00796     }
    00797   }
    00798 }
    00799
    00801 void ClientNetworkContentSocketHandler::SelectUpgrade()
    00802 {
    00803   for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
    00804     ContentInfo *ci = *iter;
    00805     if (ci->state == ContentInfo::UNSELECTED && ci->upgrade) {
    00806       ci->state = ContentInfo::SELECTED;
    00807       this->CheckDependencyState(ci);
    00808     }
    00809   }
    00810 }
    00811
    00813 void ClientNetworkContentSocketHandler::UnselectAll()
    00814 {
    00815   for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
    00816     ContentInfo *ci = *iter;
    00817     if (ci->IsSelected() && ci->state != ContentInfo::ALREADY_HERE) ci->state = ContentInfo::UNSELECTED;
    00818   }
    00819 }
    00820
    00822 void ClientNetworkContentSocketHandler::ToggleSelectedState(const ContentInfo *ci)
    00823 {
    00824   switch (ci->state) {
    00825     case ContentInfo::SELECTED:
    00826     case ContentInfo::AUTOSELECTED:
    00827       this->Unselect(ci->id);
    00828       break;
    00829
    00830     case ContentInfo::UNSELECTED:
    00831       this->Select(ci->id);
    00832       break;
    00833
    00834     default:
    00835       break;
    00836   }
    00837 }
    00838
    00844 void ClientNetworkContentSocketHandler::ReverseLookupDependency(ConstContentVector &parents, const ContentInfo *child) const
    00845 {
    00846   for (ConstContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
    00847     const ContentInfo *ci = *iter;
    00848     if (ci == child) continue;
    00849
    00850     for (uint i = 0; i < ci->dependency_count; i++) {
    00851       if (ci->dependencies[i] == child->id) {
    00852         *parents.Append() = ci;
    00853         break;
    00854       }
    00855     }
    00856   }
    00857 }
    00858
    00864 void ClientNetworkContentSocketHandler::ReverseLookupTreeDependency(ConstContentVector &tree, const ContentInfo *child) const
    00865 {
    00866   *tree.Append() = child;
    00867
    00868   /* First find all direct parents */
    00869   for (ConstContentIterator iter = tree.Begin(); iter != tree.End(); iter++) {
    00870     ConstContentVector parents;
    00871     this->ReverseLookupDependency(parents, *iter);
    00872
    00873     for (ConstContentIterator piter = parents.Begin(); piter != parents.End(); piter++) {
    00874       tree.Include(*piter);
    00875     }
    00876   }
    00877 }
    00878
    00883 void ClientNetworkContentSocketHandler::CheckDependencyState(ContentInfo *ci)
    00884 {
    00885   if (ci->IsSelected() || ci->state == ContentInfo::ALREADY_HERE) {
    00886     /* Selection is easy; just walk all children and set the
    00887      * autoselected state. That way we can see what we automatically
    00888      * selected and thus can unselect when a dependency is removed. */
    00889     for (uint i = 0; i < ci->dependency_count; i++) {
    00890       ContentInfo *c = this->GetContent(ci->dependencies[i]);
    00891       if (c == NULL) {
    00892         this->DownloadContentInfo(ci->dependencies[i]);
    00893       } else if (c->state == ContentInfo::UNSELECTED) {
    00894         c->state = ContentInfo::AUTOSELECTED;
    00895         this->CheckDependencyState(c);
    00896       }
    00897     }
    00898     return;
    00899   }
    00900
    00901   if (ci->state != ContentInfo::UNSELECTED) return;
    00902
    00903   /* For unselection we need to find the parents of us. We need to
    00904    * unselect them. After that we unselect all children that we
    00905    * depend on and are not used as dependency for us, but only when
    00906    * we automatically selected them. */
    00907   ConstContentVector parents;
    00908   this->ReverseLookupDependency(parents, ci);
    00909   for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
    00910     const ContentInfo *c = *iter;
    00911     if (!c->IsSelected()) continue;
    00912
    00913     this->Unselect(c->id);
    00914   }
    00915
    00916   for (uint i = 0; i < ci->dependency_count; i++) {
    00917     const ContentInfo *c = this->GetContent(ci->dependencies[i]);
    00918     if (c == NULL) {
    00919       DownloadContentInfo(ci->dependencies[i]);
    00920       continue;
    00921     }
    00922     if (c->state != ContentInfo::AUTOSELECTED) continue;
    00923
    00924     /* Only unselect when WE are the only parent. */
    00925     parents.Clear();
    00926     this->ReverseLookupDependency(parents, c);
    00927
    00928     /* First check whether anything depends on us */
    00929     int sel_count = 0;
    00930     bool force_selection = false;
    00931     for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
    00932       if ((*iter)->IsSelected()) sel_count++;
    00933       if ((*iter)->state == ContentInfo::SELECTED) force_selection = true;
    00934     }
    00935     if (sel_count == 0) {
    00936       /* Nothing depends on us */
    00937       this->Unselect(c->id);
    00938       continue;
    00939     }
    00940     /* Something manually selected depends directly on us */
    00941     if (force_selection) continue;
    00942
    00943     /* "Flood" search to find all items in the dependency graph*/
    00944     parents.Clear();
    00945     this->ReverseLookupTreeDependency(parents, c);
    00946
    00947     /* Is there anything that is "force" selected?, if so... we're done. */
    00948     for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
    00949       if ((*iter)->state != ContentInfo::SELECTED) continue;
    00950
    00951       force_selection = true;
    00952       break;
    00953     }
    00954
    00955     /* So something depended directly on us */
    00956     if (force_selection) continue;
    00957
    00958     /* Nothing depends on us, mark the whole graph as unselected.
    00959      * After that's done run over them once again to test their children
    00960      * to unselect. Don't do it immediatelly because it'll do exactly what
    00961      * we're doing now. */
    00962     for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
    00963       const ContentInfo *c = *iter;
    00964       if (c->state == ContentInfo::AUTOSELECTED) this->Unselect(c->id);
    00965     }
    00966     for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
    00967       this->CheckDependencyState(this->GetContent((*iter)->id));
    00968     }
    00969   }
    00970 }
    00971
    00972 void ClientNetworkContentSocketHandler::Clear()
    00973 {
    00974   for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) delete *iter;
    00975
    00976   this->infos.Clear();
    00977   this->requested.Clear();
    00978 }
    00979
    00980 /*** CALLBACK ***/
    00981
    00982 void ClientNetworkContentSocketHandler::OnConnect(bool success)
    00983 {
    00984   for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
    00985     ContentCallback *cb = *iter;
    00986     cb->OnConnect(success);
    00987     if (iter != this->callbacks.End() && *iter == cb) iter++;
    00988   }
    00989 }
    00990
    00991 void ClientNetworkContentSocketHandler::OnDisconnect()
    00992 {
    00993   for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
    00994     ContentCallback *cb = *iter;
    00995     cb->OnDisconnect();
    00996     if (iter != this->callbacks.End() && *iter == cb) iter++;
    00997   }
    00998 }
    00999
    01000 void ClientNetworkContentSocketHandler::OnReceiveContentInfo(const ContentInfo *ci)
    01001 {
    01002   for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
    01003     ContentCallback *cb = *iter;
    01004     cb->OnReceiveContentInfo(ci);
    01005     if (iter != this->callbacks.End() && *iter == cb) iter++;
    01006   }
    01007 }
    01008
    01009 void ClientNetworkContentSocketHandler::OnDownloadProgress(const ContentInfo *ci, uint bytes)
    01010 {
    01011   for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
    01012     ContentCallback *cb = *iter;
    01013     cb->OnDownloadProgress(ci, bytes);
    01014     if (iter != this->callbacks.End() && *iter == cb) iter++;
    01015   }
    01016 }
    01017
    01018 void ClientNetworkContentSocketHandler::OnDownloadComplete(ContentID cid)
    01019 {
    01020   ContentInfo *ci = this->GetContent(cid);
    01021   if (ci != NULL) {
    01022     ci->state = ContentInfo::ALREADY_HERE;
    01023   }
    01024
    01025   for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
    01026     ContentCallback *cb = *iter;
    01027     cb->OnDownloadComplete(cid);
    01028     if (iter != this->callbacks.End() && *iter == cb) iter++;
    01029   }
    01030 }
    01031
    01032 #endif /* ENABLE_NETWORK */