network_content.cpp

Go to the documentation of this file.
00001 /* $Id: network_content.cpp 19623 2010-04-13 21:32:29Z 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     p++; // Start after the '/'
00608 
00609     char tmp[MAX_PATH];
00610     if (strecpy(tmp, p, lastof(tmp)) == lastof(tmp)) {
00611       this->OnFailure();
00612       return;
00613     }
00614     /* Remove the extension from the string. */
00615     for (uint i = 0; i < 2; i++) {
00616       p = strrchr(tmp, '.');
00617       check_and_terminate(p);
00618     }
00619 
00620     /* Copy the string, without extension, to the filename. */
00621     strecpy(this->curInfo->filename, tmp, lastof(this->curInfo->filename));
00622 
00623     /* Request the next file. */
00624     if (!this->BeforeDownload()) {
00625       this->OnFailure();
00626       return;
00627     }
00628 
00629     NetworkHTTPSocketHandler::Connect(str, this);
00630     return;
00631   }
00632 
00633 #undef check
00634 #undef check_and_terminate
00635 }
00636 
00642 ClientNetworkContentSocketHandler::ClientNetworkContentSocketHandler() :
00643   NetworkContentSocketHandler(),
00644   http_response_index(-2),
00645   curFile(NULL),
00646   curInfo(NULL),
00647   isConnecting(false)
00648 {
00649 }
00650 
00652 ClientNetworkContentSocketHandler::~ClientNetworkContentSocketHandler()
00653 {
00654   delete this->curInfo;
00655   if (this->curFile != NULL) fclose(this->curFile);
00656 
00657   for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) delete *iter;
00658 }
00659 
00660 class NetworkContentConnecter : TCPConnecter {
00661 public:
00662   NetworkContentConnecter(const NetworkAddress &address) : TCPConnecter(address) {}
00663 
00664   virtual void OnFailure()
00665   {
00666     _network_content_client.isConnecting = false;
00667     _network_content_client.OnConnect(false);
00668   }
00669 
00670   virtual void OnConnect(SOCKET s)
00671   {
00672     assert(_network_content_client.sock == INVALID_SOCKET);
00673     _network_content_client.isConnecting = false;
00674     _network_content_client.sock = s;
00675     _network_content_client.Reopen();
00676     _network_content_client.OnConnect(true);
00677   }
00678 };
00679 
00683 void ClientNetworkContentSocketHandler::Connect()
00684 {
00685   this->lastActivity = _realtime_tick;
00686 
00687   if (this->sock != INVALID_SOCKET || this->isConnecting) return;
00688   this->isConnecting = true;
00689   new NetworkContentConnecter(NetworkAddress(NETWORK_CONTENT_SERVER_HOST, NETWORK_CONTENT_SERVER_PORT, AF_UNSPEC));
00690 }
00691 
00695 void ClientNetworkContentSocketHandler::Close()
00696 {
00697   if (this->sock == INVALID_SOCKET) return;
00698   NetworkContentSocketHandler::Close();
00699 
00700   this->OnDisconnect();
00701 }
00702 
00707 void ClientNetworkContentSocketHandler::SendReceive()
00708 {
00709   if (this->sock == INVALID_SOCKET || this->isConnecting) return;
00710 
00711   if (this->lastActivity + IDLE_TIMEOUT < _realtime_tick) {
00712     this->Close();
00713     return;
00714   }
00715 
00716   fd_set read_fd, write_fd;
00717   struct timeval tv;
00718 
00719   FD_ZERO(&read_fd);
00720   FD_ZERO(&write_fd);
00721 
00722   FD_SET(this->sock, &read_fd);
00723   FD_SET(this->sock, &write_fd);
00724 
00725   tv.tv_sec = tv.tv_usec = 0; // don't block at all.
00726 #if !defined(__MORPHOS__) && !defined(__AMIGA__)
00727   select(FD_SETSIZE, &read_fd, &write_fd, NULL, &tv);
00728 #else
00729   WaitSelect(FD_SETSIZE, &read_fd, &write_fd, NULL, &tv, NULL);
00730 #endif
00731   if (FD_ISSET(this->sock, &read_fd)) {
00732     this->Recv_Packets();
00733     this->lastActivity = _realtime_tick;
00734   }
00735 
00736   this->writable = !!FD_ISSET(this->sock, &write_fd);
00737   this->Send_Packets();
00738 }
00739 
00744 void ClientNetworkContentSocketHandler::DownloadContentInfo(ContentID cid)
00745 {
00746   /* When we tried to download it already, don't try again */
00747   if (this->requested.Contains(cid)) return;
00748 
00749   *this->requested.Append() = cid;
00750   assert(this->requested.Contains(cid));
00751   this->RequestContentList(1, &cid);
00752 }
00753 
00759 ContentInfo *ClientNetworkContentSocketHandler::GetContent(ContentID cid)
00760 {
00761   for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
00762     ContentInfo *ci = *iter;
00763     if (ci->id == cid) return ci;
00764   }
00765   return NULL;
00766 }
00767 
00768 
00773 void ClientNetworkContentSocketHandler::Select(ContentID cid)
00774 {
00775   ContentInfo *ci = this->GetContent(cid);
00776   if (ci == NULL || ci->state != ContentInfo::UNSELECTED) return;
00777 
00778   ci->state = ContentInfo::SELECTED;
00779   this->CheckDependencyState(ci);
00780 }
00781 
00786 void ClientNetworkContentSocketHandler::Unselect(ContentID cid)
00787 {
00788   ContentInfo *ci = this->GetContent(cid);
00789   if (ci == NULL || !ci->IsSelected()) return;
00790 
00791   ci->state = ContentInfo::UNSELECTED;
00792   this->CheckDependencyState(ci);
00793 }
00794 
00796 void ClientNetworkContentSocketHandler::SelectAll()
00797 {
00798   for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
00799     ContentInfo *ci = *iter;
00800     if (ci->state == ContentInfo::UNSELECTED) {
00801       ci->state = ContentInfo::SELECTED;
00802       this->CheckDependencyState(ci);
00803     }
00804   }
00805 }
00806 
00808 void ClientNetworkContentSocketHandler::SelectUpgrade()
00809 {
00810   for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
00811     ContentInfo *ci = *iter;
00812     if (ci->state == ContentInfo::UNSELECTED && ci->upgrade) {
00813       ci->state = ContentInfo::SELECTED;
00814       this->CheckDependencyState(ci);
00815     }
00816   }
00817 }
00818 
00820 void ClientNetworkContentSocketHandler::UnselectAll()
00821 {
00822   for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
00823     ContentInfo *ci = *iter;
00824     if (ci->IsSelected() && ci->state != ContentInfo::ALREADY_HERE) ci->state = ContentInfo::UNSELECTED;
00825   }
00826 }
00827 
00829 void ClientNetworkContentSocketHandler::ToggleSelectedState(const ContentInfo *ci)
00830 {
00831   switch (ci->state) {
00832     case ContentInfo::SELECTED:
00833     case ContentInfo::AUTOSELECTED:
00834       this->Unselect(ci->id);
00835       break;
00836 
00837     case ContentInfo::UNSELECTED:
00838       this->Select(ci->id);
00839       break;
00840 
00841     default:
00842       break;
00843   }
00844 }
00845 
00851 void ClientNetworkContentSocketHandler::ReverseLookupDependency(ConstContentVector &parents, const ContentInfo *child) const
00852 {
00853   for (ConstContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
00854     const ContentInfo *ci = *iter;
00855     if (ci == child) continue;
00856 
00857     for (uint i = 0; i < ci->dependency_count; i++) {
00858       if (ci->dependencies[i] == child->id) {
00859         *parents.Append() = ci;
00860         break;
00861       }
00862     }
00863   }
00864 }
00865 
00871 void ClientNetworkContentSocketHandler::ReverseLookupTreeDependency(ConstContentVector &tree, const ContentInfo *child) const
00872 {
00873   *tree.Append() = child;
00874 
00875   /* First find all direct parents */
00876   for (ConstContentIterator iter = tree.Begin(); iter != tree.End(); iter++) {
00877     ConstContentVector parents;
00878     this->ReverseLookupDependency(parents, *iter);
00879 
00880     for (ConstContentIterator piter = parents.Begin(); piter != parents.End(); piter++) {
00881       tree.Include(*piter);
00882     }
00883   }
00884 }
00885 
00890 void ClientNetworkContentSocketHandler::CheckDependencyState(ContentInfo *ci)
00891 {
00892   if (ci->IsSelected() || ci->state == ContentInfo::ALREADY_HERE) {
00893     /* Selection is easy; just walk all children and set the
00894      * autoselected state. That way we can see what we automatically
00895      * selected and thus can unselect when a dependency is removed. */
00896     for (uint i = 0; i < ci->dependency_count; i++) {
00897       ContentInfo *c = this->GetContent(ci->dependencies[i]);
00898       if (c == NULL) {
00899         this->DownloadContentInfo(ci->dependencies[i]);
00900       } else if (c->state == ContentInfo::UNSELECTED) {
00901         c->state = ContentInfo::AUTOSELECTED;
00902         this->CheckDependencyState(c);
00903       }
00904     }
00905     return;
00906   }
00907 
00908   if (ci->state != ContentInfo::UNSELECTED) return;
00909 
00910   /* For unselection we need to find the parents of us. We need to
00911    * unselect them. After that we unselect all children that we
00912    * depend on and are not used as dependency for us, but only when
00913    * we automatically selected them. */
00914   ConstContentVector parents;
00915   this->ReverseLookupDependency(parents, ci);
00916   for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
00917     const ContentInfo *c = *iter;
00918     if (!c->IsSelected()) continue;
00919 
00920     this->Unselect(c->id);
00921   }
00922 
00923   for (uint i = 0; i < ci->dependency_count; i++) {
00924     const ContentInfo *c = this->GetContent(ci->dependencies[i]);
00925     if (c == NULL) {
00926       DownloadContentInfo(ci->dependencies[i]);
00927       continue;
00928     }
00929     if (c->state != ContentInfo::AUTOSELECTED) continue;
00930 
00931     /* Only unselect when WE are the only parent. */
00932     parents.Clear();
00933     this->ReverseLookupDependency(parents, c);
00934 
00935     /* First check whether anything depends on us */
00936     int sel_count = 0;
00937     bool force_selection = false;
00938     for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
00939       if ((*iter)->IsSelected()) sel_count++;
00940       if ((*iter)->state == ContentInfo::SELECTED) force_selection = true;
00941     }
00942     if (sel_count == 0) {
00943       /* Nothing depends on us */
00944       this->Unselect(c->id);
00945       continue;
00946     }
00947     /* Something manually selected depends directly on us */
00948     if (force_selection) continue;
00949 
00950     /* "Flood" search to find all items in the dependency graph*/
00951     parents.Clear();
00952     this->ReverseLookupTreeDependency(parents, c);
00953 
00954     /* Is there anything that is "force" selected?, if so... we're done. */
00955     for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
00956       if ((*iter)->state != ContentInfo::SELECTED) continue;
00957 
00958       force_selection = true;
00959       break;
00960     }
00961 
00962     /* So something depended directly on us */
00963     if (force_selection) continue;
00964 
00965     /* Nothing depends on us, mark the whole graph as unselected.
00966      * After that's done run over them once again to test their children
00967      * to unselect. Don't do it immediatelly because it'll do exactly what
00968      * we're doing now. */
00969     for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
00970       const ContentInfo *c = *iter;
00971       if (c->state == ContentInfo::AUTOSELECTED) this->Unselect(c->id);
00972     }
00973     for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
00974       this->CheckDependencyState(this->GetContent((*iter)->id));
00975     }
00976   }
00977 }
00978 
00979 void ClientNetworkContentSocketHandler::Clear()
00980 {
00981   for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) delete *iter;
00982 
00983   this->infos.Clear();
00984   this->requested.Clear();
00985 }
00986 
00987 /*** CALLBACK ***/
00988 
00989 void ClientNetworkContentSocketHandler::OnConnect(bool success)
00990 {
00991   for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
00992     ContentCallback *cb = *iter;
00993     cb->OnConnect(success);
00994     if (iter != this->callbacks.End() && *iter == cb) iter++;
00995   }
00996 }
00997 
00998 void ClientNetworkContentSocketHandler::OnDisconnect()
00999 {
01000   for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
01001     ContentCallback *cb = *iter;
01002     cb->OnDisconnect();
01003     if (iter != this->callbacks.End() && *iter == cb) iter++;
01004   }
01005 }
01006 
01007 void ClientNetworkContentSocketHandler::OnReceiveContentInfo(const ContentInfo *ci)
01008 {
01009   for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
01010     ContentCallback *cb = *iter;
01011     cb->OnReceiveContentInfo(ci);
01012     if (iter != this->callbacks.End() && *iter == cb) iter++;
01013   }
01014 }
01015 
01016 void ClientNetworkContentSocketHandler::OnDownloadProgress(const ContentInfo *ci, uint bytes)
01017 {
01018   for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
01019     ContentCallback *cb = *iter;
01020     cb->OnDownloadProgress(ci, bytes);
01021     if (iter != this->callbacks.End() && *iter == cb) iter++;
01022   }
01023 }
01024 
01025 void ClientNetworkContentSocketHandler::OnDownloadComplete(ContentID cid)
01026 {
01027   ContentInfo *ci = this->GetContent(cid);
01028   if (ci != NULL) {
01029     ci->state = ContentInfo::ALREADY_HERE;
01030   }
01031 
01032   for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
01033     ContentCallback *cb = *iter;
01034     cb->OnDownloadComplete(cid);
01035     if (iter != this->callbacks.End() && *iter == cb) iter++;
01036   }
01037 }
01038 
01039 #endif /* ENABLE_NETWORK */

Generated on Sat Apr 17 23:24:49 2010 for OpenTTD by  doxygen 1.6.1