OpenTTD Source: src/network/network_content.cpp Source File

OpenTTD Source 1.11.0-RC1

network_content.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
10 #include "../stdafx.h"
11 #include "../rev.h"
12 #include "../ai/ai.hpp"
13 #include "../game/game.hpp"
14 #include "../window_func.h"
15 #include "../error.h"
16 #include "../base_media_base.h"
17 #include "../settings_type.h"
18 #include "network_content.h"
19
20 #include "table/strings.h"
21
22 #if defined(WITH_ZLIB)
23 #include <zlib.h>
24 #endif
25
26 #ifdef __EMSCRIPTEN__
27 # include <emscripten.h>
28 #endif
29
30 #include "../safeguards.h"
31
32 extern bool HasScenario(const ContentInfo *ci, bool md5sum);
33
35 ClientNetworkContentSocketHandler _network_content_client;
36
38 static bool HasGRFConfig(const ContentInfo *ci, bool md5sum)
39 {
40 return FindGRFConfig(BSWAP32(ci->unique_id), md5sum ? FGCM_EXACT : FGCM_ANY, md5sum ? ci->md5sum : nullptr) != nullptr;
41 }
42
50 typedef bool (*HasProc)(const ContentInfo *ci, bool md5sum);
51
52 bool ClientNetworkContentSocketHandler::Receive_SERVER_INFO(Packet *p)
53 {
54 ContentInfo *ci = new ContentInfo();
55 ci->type = (ContentType)p->Recv_uint8();
56 ci->id = (ContentID)p->Recv_uint32();
57 ci->filesize = p->Recv_uint32();
58
59 p->Recv_string(ci->name, lengthof(ci->name));
60 p->Recv_string(ci->version, lengthof(ci->version));
61 p->Recv_string(ci->url, lengthof(ci->url));
62 p->Recv_string(ci->description, lengthof(ci->description), SVS_REPLACE_WITH_QUESTION_MARK | SVS_ALLOW_NEWLINE);
63
64 ci->unique_id = p->Recv_uint32();
65 for (uint j = 0; j < sizeof(ci->md5sum); j++) {
66 ci->md5sum[j] = p->Recv_uint8();
67 }
68
69 ci->dependency_count = p->Recv_uint8();
70 ci->dependencies = MallocT<ContentID>(ci->dependency_count);
71 for (uint i = 0; i < ci->dependency_count; i++) ci->dependencies[i] = (ContentID)p->Recv_uint32();
72
73 ci->tag_count = p->Recv_uint8();
74 ci->tags = MallocT<char[32]>(ci->tag_count);
75 for (uint i = 0; i < ci->tag_count; i++) p->Recv_string(ci->tags[i], lengthof(*ci->tags));
76
77 if (!ci->IsValid()) {
78 delete ci;
79 this->Close();
80 return false;
81 }
82
83 /* Find the appropriate check function */
84 HasProc proc = nullptr;
85 switch (ci->type) {
86 case CONTENT_TYPE_NEWGRF:
87 proc = HasGRFConfig;
88 break;
89
90 case CONTENT_TYPE_BASE_GRAPHICS:
91 proc = BaseGraphics::HasSet;
92 break;
93
94 case CONTENT_TYPE_BASE_MUSIC:
95 proc = BaseMusic::HasSet;
96 break;
97
98 case CONTENT_TYPE_BASE_SOUNDS:
99 proc = BaseSounds::HasSet;
100 break;
101
102 case CONTENT_TYPE_AI:
103 proc = AI::HasAI; break;
104 break;
105
106 case CONTENT_TYPE_AI_LIBRARY:
107 proc = AI::HasAILibrary; break;
108 break;
109
110 case CONTENT_TYPE_GAME:
111 proc = Game::HasGame; break;
112 break;
113
114 case CONTENT_TYPE_GAME_LIBRARY:
115 proc = Game::HasGameLibrary; break;
116 break;
117
118 case CONTENT_TYPE_SCENARIO:
119 case CONTENT_TYPE_HEIGHTMAP:
120 proc = HasScenario;
121 break;
122
123 default:
124 break;
125 }
126
127 if (proc != nullptr) {
128 if (proc(ci, true)) {
129 ci->state = ContentInfo::ALREADY_HERE;
130 } else {
131 ci->state = ContentInfo::UNSELECTED;
132 if (proc(ci, false)) ci->upgrade = true;
133 }
134 } else {
135 ci->state = ContentInfo::UNSELECTED;
136 }
137
138 /* Something we don't have and has filesize 0 does not exist in the system */
139 if (ci->state == ContentInfo::UNSELECTED && ci->filesize == 0) ci->state = ContentInfo::DOES_NOT_EXIST;
140
141 /* Do we already have a stub for this? */
142 for (ContentInfo *ici : this->infos) {
143 if (ici->type == ci->type && ici->unique_id == ci->unique_id &&
144 memcmp(ci->md5sum, ici->md5sum, sizeof(ci->md5sum)) == 0) {
145 /* Preserve the name if possible */
146 if (StrEmpty(ci->name)) strecpy(ci->name, ici->name, lastof(ci->name));
147 if (ici->IsSelected()) ci->state = ici->state;
148
149 /*
150 * As ici might be selected by the content window we cannot delete that.
151 * However, we want to keep most of the values of ci, except the values
152 * we (just) already preserved.
153 * So transfer data and ownership of allocated memory from ci to ici.
154 */
155 ici->TransferFrom(ci);
156 delete ci;
157
158 this->OnReceiveContentInfo(ici);
159 return true;
160 }
161 }
162
163 /* Missing content info? Don't list it */
164 if (ci->filesize == 0) {
165 delete ci;
166 return true;
167 }
168
169 this->infos.push_back(ci);
170
171 /* Incoming data means that we might need to reconsider dependencies */
172 for (ContentInfo *ici : this->infos) {
173 this->CheckDependencyState(ici);
174 }
175
176 this->OnReceiveContentInfo(ci);
177
178 return true;
179 }
180
185 void ClientNetworkContentSocketHandler::RequestContentList(ContentType type)
186 {
187 if (type == CONTENT_TYPE_END) {
188 this->RequestContentList(CONTENT_TYPE_BASE_GRAPHICS);
189 this->RequestContentList(CONTENT_TYPE_BASE_MUSIC);
190 this->RequestContentList(CONTENT_TYPE_BASE_SOUNDS);
191 this->RequestContentList(CONTENT_TYPE_SCENARIO);
192 this->RequestContentList(CONTENT_TYPE_HEIGHTMAP);
193 this->RequestContentList(CONTENT_TYPE_AI);
194 this->RequestContentList(CONTENT_TYPE_AI_LIBRARY);
195 this->RequestContentList(CONTENT_TYPE_GAME);
196 this->RequestContentList(CONTENT_TYPE_GAME_LIBRARY);
197 this->RequestContentList(CONTENT_TYPE_NEWGRF);
198 return;
199 }
200
201 this->Connect();
202
203 Packet *p = new Packet(PACKET_CONTENT_CLIENT_INFO_LIST);
204 p->Send_uint8 ((byte)type);
205 p->Send_uint32(_openttd_newgrf_version);
206
207 this->SendPacket(p);
208 }
209
215 void ClientNetworkContentSocketHandler::RequestContentList(uint count, const ContentID *content_ids)
216 {
217 this->Connect();
218
219 while (count > 0) {
220 /* We can "only" send a limited number of IDs in a single packet.
221 * A packet begins with the packet size and a byte for the type.
222 * Then this packet adds a uint16 for the count in this packet.
223 * The rest of the packet can be used for the IDs. */
224 uint p_count = std::min<uint>(count, (SEND_MTU - sizeof(PacketSize) - sizeof(byte) - sizeof(uint16)) / sizeof(uint32));
225
226 Packet *p = new Packet(PACKET_CONTENT_CLIENT_INFO_ID);
227 p->Send_uint16(p_count);
228
229 for (uint i = 0; i < p_count; i++) {
230 p->Send_uint32(content_ids[i]);
231 }
232
233 this->SendPacket(p);
234 count -= p_count;
235 content_ids += p_count;
236 }
237 }
238
244 void ClientNetworkContentSocketHandler::RequestContentList(ContentVector *cv, bool send_md5sum)
245 {
246 if (cv == nullptr) return;
247
248 this->Connect();
249
250 assert(cv->size() < 255);
251 assert(cv->size() < (SEND_MTU - sizeof(PacketSize) - sizeof(byte) - sizeof(uint8)) /
252 (sizeof(uint8) + sizeof(uint32) + (send_md5sum ? /*sizeof(ContentInfo::md5sum)*/16 : 0)));
253
254 Packet *p = new Packet(send_md5sum ? PACKET_CONTENT_CLIENT_INFO_EXTID_MD5 : PACKET_CONTENT_CLIENT_INFO_EXTID);
255 p->Send_uint8((uint8)cv->size());
256
257 for (const ContentInfo *ci : *cv) {
258 p->Send_uint8((byte)ci->type);
259 p->Send_uint32(ci->unique_id);
260 if (!send_md5sum) continue;
261
262 for (uint j = 0; j < sizeof(ci->md5sum); j++) {
263 p->Send_uint8(ci->md5sum[j]);
264 }
265 }
266
267 this->SendPacket(p);
268
269 for (ContentInfo *ci : *cv) {
270 bool found = false;
271 for (ContentInfo *ci2 : this->infos) {
272 if (ci->type == ci2->type && ci->unique_id == ci2->unique_id &&
273 (!send_md5sum || memcmp(ci->md5sum, ci2->md5sum, sizeof(ci->md5sum)) == 0)) {
274 found = true;
275 break;
276 }
277 }
278 if (!found) {
279 this->infos.push_back(ci);
280 } else {
281 delete ci;
282 }
283 }
284 }
285
292 void ClientNetworkContentSocketHandler::DownloadSelectedContent(uint &files, uint &bytes, bool fallback)
293 {
294 bytes = 0;
295
296 #ifdef __EMSCRIPTEN__
297 /* Emscripten is loaded via an HTTPS connection. As such, it is very
298 * difficult to make HTTP connections. So always use the TCP method of
299 * downloading content. */
300 fallback = true;
301 #endif
302
303 ContentIDList content;
304 for (const ContentInfo *ci : this->infos) {
305 if (!ci->IsSelected() || ci->state == ContentInfo::ALREADY_HERE) continue;
306
307 content.push_back(ci->id);
308 bytes += ci->filesize;
309 }
310
311 files = (uint)content.size();
312
313 /* If there's nothing to download, do nothing. */
314 if (files == 0) return;
315
316 if (_settings_client.network.no_http_content_downloads || fallback) {
317 this->DownloadSelectedContentFallback(content);
318 } else {
319 this->DownloadSelectedContentHTTP(content);
320 }
321 }
322
327 void ClientNetworkContentSocketHandler::DownloadSelectedContentHTTP(const ContentIDList &content)
328 {
329 uint count = (uint)content.size();
330
331 /* Allocate memory for the whole request.
332 * Requests are "id\nid\n..." (as strings), so assume the maximum ID,
333 * which is uint32 so 10 characters long. Then the newlines and
334 * multiply that all with the count and then add the '\0'. */
335 uint bytes = (10 + 1) * count + 1;
336 char *content_request = MallocT<char>(bytes);
337 const char *lastof = content_request + bytes - 1;
338
339 char *p = content_request;
340 for (const ContentID &id : content) {
341 p += seprintf(p, lastof, "%d\n", id);
342 }
343
344 this->http_response_index = -1;
345
346 NetworkAddress address(NETWORK_CONTENT_MIRROR_HOST, NETWORK_CONTENT_MIRROR_PORT);
347 new NetworkHTTPContentConnecter(address, this, NETWORK_CONTENT_MIRROR_URL, content_request);
348 /* NetworkHTTPContentConnecter takes over freeing of content_request! */
349 }
350
355 void ClientNetworkContentSocketHandler::DownloadSelectedContentFallback(const ContentIDList &content)
356 {
357 uint count = (uint)content.size();
358 const ContentID *content_ids = content.data();
359 this->Connect();
360
361 while (count > 0) {
362 /* We can "only" send a limited number of IDs in a single packet.
363 * A packet begins with the packet size and a byte for the type.
364 * Then this packet adds a uint16 for the count in this packet.
365 * The rest of the packet can be used for the IDs. */
366 uint p_count = std::min<uint>(count, (SEND_MTU - sizeof(PacketSize) - sizeof(byte) - sizeof(uint16)) / sizeof(uint32));
367
368 Packet *p = new Packet(PACKET_CONTENT_CLIENT_CONTENT);
369 p->Send_uint16(p_count);
370
371 for (uint i = 0; i < p_count; i++) {
372 p->Send_uint32(content_ids[i]);
373 }
374
375 this->SendPacket(p);
376 count -= p_count;
377 content_ids += p_count;
378 }
379 }
380
388 static std::string GetFullFilename(const ContentInfo *ci, bool compressed)
389 {
390 Subdirectory dir = GetContentInfoSubDir(ci->type);
391 if (dir == NO_DIRECTORY) return {};
392
393 std::string buf = FioGetDirectory(SP_AUTODOWNLOAD_DIR, dir);
394 buf += ci->filename;
395 buf += compressed ? ".tar.gz" : ".tar";
396
397 return buf;
398 }
399
405 static bool GunzipFile(const ContentInfo *ci)
406 {
407 #if defined(WITH_ZLIB)
408 bool ret = true;
409
410 /* Need to open the file with fopen() to support non-ASCII on Windows. */
411 FILE *ftmp = fopen(GetFullFilename(ci, true).c_str(), "rb");
412 if (ftmp == nullptr) return false;
413 /* Duplicate the handle, and close the FILE*, to avoid double-closing the handle later. */
414 gzFile fin = gzdopen(dup(fileno(ftmp)), "rb");
415 fclose(ftmp);
416
417 FILE *fout = fopen(GetFullFilename(ci, false).c_str(), "wb");
418
419 if (fin == nullptr || fout == nullptr) {
420 ret = false;
421 } else {
422 byte buff[8192];
423 for (;;) {
424 int read = gzread(fin, buff, sizeof(buff));
425 if (read == 0) {
426 /* If gzread() returns 0, either the end-of-file has been
427 * reached or an underlying read error has occurred.
428 *
429 * gzeof() can't be used, because:
430 * 1.2.5 - it is safe, 1 means 'everything was OK'
431 * 1.2.3.5, 1.2.4 - 0 or 1 is returned 'randomly'
432 * 1.2.3.3 - 1 is returned for truncated archive
433 *
434 * So we use gzerror(). When proper end of archive
435 * has been reached, then:
436 * errnum == Z_STREAM_END in 1.2.3.3,
437 * errnum == 0 in 1.2.4 and 1.2.5 */
438 int errnum;
439 gzerror(fin, &errnum);
440 if (errnum != 0 && errnum != Z_STREAM_END) ret = false;
441 break;
442 }
443 if (read < 0 || (size_t)read != fwrite(buff, 1, read, fout)) {
444 /* If gzread() returns -1, there was an error in archive */
445 ret = false;
446 break;
447 }
448 /* DO NOT DO THIS! It will fail to detect broken archive with 1.2.3.3!
449 * if (read < sizeof(buff)) break; */
450 }
451 }
452
453 if (fin != nullptr) gzclose(fin);
454 if (fout != nullptr) fclose(fout);
455
456 return ret;
457 #else
458 NOT_REACHED();
459 #endif /* defined(WITH_ZLIB) */
460 }
461
462 bool ClientNetworkContentSocketHandler::Receive_SERVER_CONTENT(Packet *p)
463 {
464 if (this->curFile == nullptr) {
465 delete this->curInfo;
466 /* When we haven't opened a file this must be our first packet with metadata. */
467 this->curInfo = new ContentInfo;
468 this->curInfo->type = (ContentType)p->Recv_uint8();
469 this->curInfo->id = (ContentID)p->Recv_uint32();
470 this->curInfo->filesize = p->Recv_uint32();
471 p->Recv_string(this->curInfo->filename, lengthof(this->curInfo->filename));
472
473 if (!this->BeforeDownload()) {
474 this->Close();
475 return false;
476 }
477 } else {
478 /* We have a file opened, thus are downloading internal content */
479 size_t toRead = (size_t)(p->size - p->pos);
480 if (fwrite(p->buffer + p->pos, 1, toRead, this->curFile) != toRead) {
481 DeleteWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD);
482 ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE, WL_ERROR);
483 this->Close();
484 fclose(this->curFile);
485 this->curFile = nullptr;
486
487 return false;
488 }
489
490 this->OnDownloadProgress(this->curInfo, (int)toRead);
491
492 if (toRead == 0) this->AfterDownload();
493 }
494
495 return true;
496 }
497
502 bool ClientNetworkContentSocketHandler::BeforeDownload()
503 {
504 if (!this->curInfo->IsValid()) {
505 delete this->curInfo;
506 this->curInfo = nullptr;
507 return false;
508 }
509
510 if (this->curInfo->filesize != 0) {
511 /* The filesize is > 0, so we are going to download it */
512 std::string filename = GetFullFilename(this->curInfo, true);
513 if (filename.empty() || (this->curFile = fopen(filename.c_str(), "wb")) == nullptr) {
514 /* Unless that fails of course... */
515 DeleteWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD);
516 ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE, WL_ERROR);
517 return false;
518 }
519 }
520 return true;
521 }
522
527 void ClientNetworkContentSocketHandler::AfterDownload()
528 {
529 /* We read nothing; that's our marker for end-of-stream.
530 * Now gunzip the tar and make it known. */
531 fclose(this->curFile);
532 this->curFile = nullptr;
533
534 if (GunzipFile(this->curInfo)) {
535 unlink(GetFullFilename(this->curInfo, true).c_str());
536
537 Subdirectory sd = GetContentInfoSubDir(this->curInfo->type);
538 if (sd == NO_DIRECTORY) NOT_REACHED();
539
540 TarScanner ts;
541 std::string fname = GetFullFilename(this->curInfo, false);
542 ts.AddFile(sd, fname);
543
544 if (this->curInfo->type == CONTENT_TYPE_BASE_MUSIC) {
545 /* Music can't be in a tar. So extract the tar! */
546 ExtractTar(fname, BASESET_DIR);
547 unlink(fname.c_str());
548 }
549
550 #ifdef __EMSCRIPTEN__
551 EM_ASM(if (window["openttd_syncfs"]) openttd_syncfs());
552 #endif
553
554 this->OnDownloadComplete(this->curInfo->id);
555 } else {
556 ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_EXTRACT, INVALID_STRING_ID, WL_ERROR);
557 }
558 }
559
560 /* Also called to just clean up the mess. */
561 void ClientNetworkContentSocketHandler::OnFailure()
562 {
563 /* If we fail, download the rest via the 'old' system. */
564 uint files, bytes;
565 this->DownloadSelectedContent(files, bytes, true);
566
567 this->http_response.clear();
568 this->http_response.shrink_to_fit();
569 this->http_response_index = -2;
570
571 if (this->curFile != nullptr) {
572 /* Revert the download progress when we are going for the old system. */
573 long size = ftell(this->curFile);
574 if (size > 0) this->OnDownloadProgress(this->curInfo, (int)-size);
575
576 fclose(this->curFile);
577 this->curFile = nullptr;
578 }
579 }
580
581 void ClientNetworkContentSocketHandler::OnReceiveData(const char *data, size_t length)
582 {
583 assert(data == nullptr || length != 0);
584
585 /* Ignore any latent data coming from a connection we closed. */
586 if (this->http_response_index == -2) return;
587
588 if (this->http_response_index == -1) {
589 if (data != nullptr) {
590 /* Append the rest of the response. */
591 this->http_response.insert(this->http_response.end(), data, data + length);
592 return;
593 } else {
594 /* Make sure the response is properly terminated. */
595 this->http_response.push_back('\0');
596
597 /* And prepare for receiving the rest of the data. */
598 this->http_response_index = 0;
599 }
600 }
601
602 if (data != nullptr) {
603 /* We have data, so write it to the file. */
604 if (fwrite(data, 1, length, this->curFile) != length) {
605 /* Writing failed somehow, let try via the old method. */
606 this->OnFailure();
607 } else {
608 /* Just received the data. */
609 this->OnDownloadProgress(this->curInfo, (int)length);
610 }
611 /* Nothing more to do now. */
612 return;
613 }
614
615 if (this->curFile != nullptr) {
616 /* We've finished downloading a file. */
617 this->AfterDownload();
618 }
619
620 if ((uint)this->http_response_index >= this->http_response.size()) {
621 /* It's not a real failure, but if there's
622 * nothing more to download it helps with
623 * cleaning up the stuff we allocated. */
624 this->OnFailure();
625 return;
626 }
627
628 delete this->curInfo;
629 /* When we haven't opened a file this must be our first packet with metadata. */
630 this->curInfo = new ContentInfo;
631
633 #define check_not_null(p) { if ((p) == nullptr) { this->OnFailure(); return; } }
634
635 #define check_and_terminate(p) { check_not_null(p); *(p) = '\0'; }
636
637 for (;;) {
638 char *str = this->http_response.data() + this->http_response_index;
639 char *p = strchr(str, '\n');
640 check_and_terminate(p);
641
642 /* Update the index for the next one */
643 this->http_response_index += (int)strlen(str) + 1;
644
645 /* Read the ID */
646 p = strchr(str, ',');
647 check_and_terminate(p);
648 this->curInfo->id = (ContentID)atoi(str);
649
650 /* Read the type */
651 str = p + 1;
652 p = strchr(str, ',');
653 check_and_terminate(p);
654 this->curInfo->type = (ContentType)atoi(str);
655
656 /* Read the file size */
657 str = p + 1;
658 p = strchr(str, ',');
659 check_and_terminate(p);
660 this->curInfo->filesize = atoi(str);
661
662 /* Read the URL */
663 str = p + 1;
664 /* Is it a fallback URL? If so, just continue with the next one. */
665 if (strncmp(str, "ottd", 4) == 0) {
666 if ((uint)this->http_response_index >= this->http_response.size()) {
667 /* Have we gone through all lines? */
668 this->OnFailure();
669 return;
670 }
671 continue;
672 }
673
674 p = strrchr(str, '/');
675 check_not_null(p);
676 p++; // Start after the '/'
677
678 char tmp[MAX_PATH];
679 if (strecpy(tmp, p, lastof(tmp)) == lastof(tmp)) {
680 this->OnFailure();
681 return;
682 }
683 /* Remove the extension from the string. */
684 for (uint i = 0; i < 2; i++) {
685 p = strrchr(tmp, '.');
686 check_and_terminate(p);
687 }
688
689 /* Copy the string, without extension, to the filename. */
690 strecpy(this->curInfo->filename, tmp, lastof(this->curInfo->filename));
691
692 /* Request the next file. */
693 if (!this->BeforeDownload()) {
694 this->OnFailure();
695 return;
696 }
697
698 NetworkHTTPSocketHandler::Connect(str, this);
699 return;
700 }
701
702 #undef check
703 #undef check_and_terminate
704 }
705
709 ClientNetworkContentSocketHandler::ClientNetworkContentSocketHandler() :
710 NetworkContentSocketHandler(),
711 http_response_index(-2),
712 curFile(nullptr),
713 curInfo(nullptr),
714 isConnecting(false)
715 {
716 this->lastActivity = std::chrono::steady_clock::now();
717 }
718
720 ClientNetworkContentSocketHandler::~ClientNetworkContentSocketHandler()
721 {
722 delete this->curInfo;
723 if (this->curFile != nullptr) fclose(this->curFile);
724
725 for (ContentInfo *ci : this->infos) delete ci;
726 }
727
729 class NetworkContentConnecter : TCPConnecter {
730 public:
735 NetworkContentConnecter(const NetworkAddress &address) : TCPConnecter(address) {}
736
737 void OnFailure() override
738 {
739 _network_content_client.isConnecting = false;
740 _network_content_client.OnConnect(false);
741 }
742
743 void OnConnect(SOCKET s) override
744 {
745 assert(_network_content_client.sock == INVALID_SOCKET);
746 _network_content_client.lastActivity = std::chrono::steady_clock::now();
747 _network_content_client.isConnecting = false;
748 _network_content_client.sock = s;
749 _network_content_client.Reopen();
750 _network_content_client.OnConnect(true);
751 }
752 };
753
757 void ClientNetworkContentSocketHandler::Connect()
758 {
759 if (this->sock != INVALID_SOCKET || this->isConnecting) return;
760 this->isConnecting = true;
761 new NetworkContentConnecter(NetworkAddress(NETWORK_CONTENT_SERVER_HOST, NETWORK_CONTENT_SERVER_PORT, AF_UNSPEC));
762 }
763
767 void ClientNetworkContentSocketHandler::Close()
768 {
769 if (this->sock == INVALID_SOCKET) return;
770 NetworkContentSocketHandler::Close();
771
772 this->OnDisconnect();
773 }
774
779 void ClientNetworkContentSocketHandler::SendReceive()
780 {
781 if (this->sock == INVALID_SOCKET || this->isConnecting) return;
782
783 if (std::chrono::steady_clock::now() > this->lastActivity + IDLE_TIMEOUT) {
784 this->Close();
785 return;
786 }
787
788 if (this->CanSendReceive()) {
789 if (this->ReceivePackets()) {
790 /* Only update activity once a packet is received, instead of every time we try it. */
791 this->lastActivity = std::chrono::steady_clock::now();
792 }
793 }
794
795 this->SendPackets();
796 }
797
802 void ClientNetworkContentSocketHandler::DownloadContentInfo(ContentID cid)
803 {
804 /* When we tried to download it already, don't try again */
805 if (std::find(this->requested.begin(), this->requested.end(), cid) != this->requested.end()) return;
806
807 this->requested.push_back(cid);
808 this->RequestContentList(1, &cid);
809 }
810
816 ContentInfo *ClientNetworkContentSocketHandler::GetContent(ContentID cid)
817 {
818 for (ContentInfo *ci : this->infos) {
819 if (ci->id == cid) return ci;
820 }
821 return nullptr;
822 }
823
824
829 void ClientNetworkContentSocketHandler::Select(ContentID cid)
830 {
831 ContentInfo *ci = this->GetContent(cid);
832 if (ci == nullptr || ci->state != ContentInfo::UNSELECTED) return;
833
834 ci->state = ContentInfo::SELECTED;
835 this->CheckDependencyState(ci);
836 }
837
842 void ClientNetworkContentSocketHandler::Unselect(ContentID cid)
843 {
844 ContentInfo *ci = this->GetContent(cid);
845 if (ci == nullptr || !ci->IsSelected()) return;
846
847 ci->state = ContentInfo::UNSELECTED;
848 this->CheckDependencyState(ci);
849 }
850
852 void ClientNetworkContentSocketHandler::SelectAll()
853 {
854 for (ContentInfo *ci : this->infos) {
855 if (ci->state == ContentInfo::UNSELECTED) {
856 ci->state = ContentInfo::SELECTED;
857 this->CheckDependencyState(ci);
858 }
859 }
860 }
861
863 void ClientNetworkContentSocketHandler::SelectUpgrade()
864 {
865 for (ContentInfo *ci : this->infos) {
866 if (ci->state == ContentInfo::UNSELECTED && ci->upgrade) {
867 ci->state = ContentInfo::SELECTED;
868 this->CheckDependencyState(ci);
869 }
870 }
871 }
872
874 void ClientNetworkContentSocketHandler::UnselectAll()
875 {
876 for (ContentInfo *ci : this->infos) {
877 if (ci->IsSelected() && ci->state != ContentInfo::ALREADY_HERE) ci->state = ContentInfo::UNSELECTED;
878 }
879 }
880
882 void ClientNetworkContentSocketHandler::ToggleSelectedState(const ContentInfo *ci)
883 {
884 switch (ci->state) {
885 case ContentInfo::SELECTED:
886 case ContentInfo::AUTOSELECTED:
887 this->Unselect(ci->id);
888 break;
889
890 case ContentInfo::UNSELECTED:
891 this->Select(ci->id);
892 break;
893
894 default:
895 break;
896 }
897 }
898
904 void ClientNetworkContentSocketHandler::ReverseLookupDependency(ConstContentVector &parents, const ContentInfo *child) const
905 {
906 for (const ContentInfo *ci : this->infos) {
907 if (ci == child) continue;
908
909 for (uint i = 0; i < ci->dependency_count; i++) {
910 if (ci->dependencies[i] == child->id) {
911 parents.push_back(ci);
912 break;
913 }
914 }
915 }
916 }
917
923 void ClientNetworkContentSocketHandler::ReverseLookupTreeDependency(ConstContentVector &tree, const ContentInfo *child) const
924 {
925 tree.push_back(child);
926
927 /* First find all direct parents. We can't use the "normal" iterator as
928 * we are including stuff into the vector and as such the vector's data
929 * store can be reallocated (and thus move), which means out iterating
930 * pointer gets invalid. So fall back to the indices. */
931 for (uint i = 0; i < tree.size(); i++) {
932 ConstContentVector parents;
933 this->ReverseLookupDependency(parents, tree[i]);
934
935 for (const ContentInfo *ci : parents) {
936 include(tree, ci);
937 }
938 }
939 }
940
945 void ClientNetworkContentSocketHandler::CheckDependencyState(ContentInfo *ci)
946 {
947 if (ci->IsSelected() || ci->state == ContentInfo::ALREADY_HERE) {
948 /* Selection is easy; just walk all children and set the
949 * autoselected state. That way we can see what we automatically
950 * selected and thus can unselect when a dependency is removed. */
951 for (uint i = 0; i < ci->dependency_count; i++) {
952 ContentInfo *c = this->GetContent(ci->dependencies[i]);
953 if (c == nullptr) {
954 this->DownloadContentInfo(ci->dependencies[i]);
955 } else if (c->state == ContentInfo::UNSELECTED) {
956 c->state = ContentInfo::AUTOSELECTED;
957 this->CheckDependencyState(c);
958 }
959 }
960 return;
961 }
962
963 if (ci->state != ContentInfo::UNSELECTED) return;
964
965 /* For unselection we need to find the parents of us. We need to
966 * unselect them. After that we unselect all children that we
967 * depend on and are not used as dependency for us, but only when
968 * we automatically selected them. */
969 ConstContentVector parents;
970 this->ReverseLookupDependency(parents, ci);
971 for (const ContentInfo *c : parents) {
972 if (!c->IsSelected()) continue;
973
974 this->Unselect(c->id);
975 }
976
977 for (uint i = 0; i < ci->dependency_count; i++) {
978 const ContentInfo *c = this->GetContent(ci->dependencies[i]);
979 if (c == nullptr) {
980 DownloadContentInfo(ci->dependencies[i]);
981 continue;
982 }
983 if (c->state != ContentInfo::AUTOSELECTED) continue;
984
985 /* Only unselect when WE are the only parent. */
986 parents.clear();
987 this->ReverseLookupDependency(parents, c);
988
989 /* First check whether anything depends on us */
990 int sel_count = 0;
991 bool force_selection = false;
992 for (const ContentInfo *ci : parents) {
993 if (ci->IsSelected()) sel_count++;
994 if (ci->state == ContentInfo::SELECTED) force_selection = true;
995 }
996 if (sel_count == 0) {
997 /* Nothing depends on us */
998 this->Unselect(c->id);
999 continue;
1000 }
1001 /* Something manually selected depends directly on us */
1002 if (force_selection) continue;
1003
1004 /* "Flood" search to find all items in the dependency graph*/
1005 parents.clear();
1006 this->ReverseLookupTreeDependency(parents, c);
1007
1008 /* Is there anything that is "force" selected?, if so... we're done. */
1009 for (const ContentInfo *ci : parents) {
1010 if (ci->state != ContentInfo::SELECTED) continue;
1011
1012 force_selection = true;
1013 break;
1014 }
1015
1016 /* So something depended directly on us */
1017 if (force_selection) continue;
1018
1019 /* Nothing depends on us, mark the whole graph as unselected.
1020 * After that's done run over them once again to test their children
1021 * to unselect. Don't do it immediately because it'll do exactly what
1022 * we're doing now. */
1023 for (const ContentInfo *c : parents) {
1024 if (c->state == ContentInfo::AUTOSELECTED) this->Unselect(c->id);
1025 }
1026 for (const ContentInfo *c : parents) {
1027 this->CheckDependencyState(this->GetContent(c->id));
1028 }
1029 }
1030 }
1031
1033 void ClientNetworkContentSocketHandler::Clear()
1034 {
1035 for (ContentInfo *c : this->infos) delete c;
1036
1037 this->infos.clear();
1038 this->requested.clear();
1039 }
1040
1041 /*** CALLBACK ***/
1042
1043 void ClientNetworkContentSocketHandler::OnConnect(bool success)
1044 {
1045 for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
1046 ContentCallback *cb = this->callbacks[i];
1047 /* the callback may remove itself from this->callbacks */
1048 cb->OnConnect(success);
1049 if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
1050 }
1051 }
1052
1053 void ClientNetworkContentSocketHandler::OnDisconnect()
1054 {
1055 for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
1056 ContentCallback *cb = this->callbacks[i];
1057 cb->OnDisconnect();
1058 if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
1059 }
1060 }
1061
1062 void ClientNetworkContentSocketHandler::OnReceiveContentInfo(const ContentInfo *ci)
1063 {
1064 for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
1065 ContentCallback *cb = this->callbacks[i];
1066 /* the callback may add items and/or remove itself from this->callbacks */
1067 cb->OnReceiveContentInfo(ci);
1068 if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
1069 }
1070 }
1071
1072 void ClientNetworkContentSocketHandler::OnDownloadProgress(const ContentInfo *ci, int bytes)
1073 {
1074 for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
1075 ContentCallback *cb = this->callbacks[i];
1076 cb->OnDownloadProgress(ci, bytes);
1077 if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
1078 }
1079 }
1080
1081 void ClientNetworkContentSocketHandler::OnDownloadComplete(ContentID cid)
1082 {
1083 ContentInfo *ci = this->GetContent(cid);
1084 if (ci != nullptr) {
1085 ci->state = ContentInfo::ALREADY_HERE;
1086 }
1087
1088 for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
1089 ContentCallback *cb = this->callbacks[i];
1090 /* the callback may remove itself from this->callbacks */
1091 cb->OnDownloadComplete(cid);
1092 if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
1093 }
1094 }
ContentInfo::IsSelected
bool IsSelected() const
Is the state either selected or autoselected?
Definition: tcp_content.cpp:73
network_content.h
SP_AUTODOWNLOAD_DIR
@ SP_AUTODOWNLOAD_DIR
Search within the autodownload directory.
Definition: fileio_type.h:142
ContentCallback
Callbacks for notifying others about incoming data.
Definition: network_content.h:27
ClientNetworkContentSocketHandler::OnConnect
void OnConnect(bool success) override
Callback for when the connection has finished.
Definition: network_content.cpp:1043
ContentInfo::tag_count
uint8 tag_count
Number of tags.
Definition: tcp_content.h:77
ExtractTar
bool ExtractTar(const std::string &tar_filename, Subdirectory subdir)
Extract the tar with the given filename in the directory where the tar resides.
Definition: fileio.cpp:845
ContentInfo::type
ContentType type
Type of content.
Definition: tcp_content.h:65
NetworkContentSocketHandler
Base socket handler for all Content TCP sockets.
Definition: tcp_content.h:96
ClientNetworkContentSocketHandler::ReverseLookupDependency
void ReverseLookupDependency(ConstContentVector &parents, const ContentInfo *child) const
Reverse lookup the dependencies of (direct) parents over a given child.
Definition: network_content.cpp:904
SVS_ALLOW_NEWLINE
@ SVS_ALLOW_NEWLINE
Allow newlines.
Definition: string_type.h:51
ClientNetworkContentSocketHandler::SendReceive
void SendReceive()
Check whether we received/can send some data from/to the content server and when that's the case hand...
Definition: network_content.cpp:779
Packet::Recv_string
void Recv_string(char *buffer, size_t size, StringValidationSettings settings=SVS_REPLACE_WITH_QUESTION_MARK)
Reads a string till it finds a '\0' in the stream.
Definition: packet.cpp:286
FindGRFConfig
const GRFConfig * FindGRFConfig(uint32 grfid, FindGRFConfigMode mode, const uint8 *md5sum, uint32 desired_version)
Find a NewGRF in the scanned list.
Definition: newgrf_config.cpp:737
ContentType
ContentType
The values in the enum are important; they are used as database 'keys'.
Definition: tcp_content.h:21
Packet::buffer
byte * buffer
The buffer of this packet, of basically variable length up to SEND_MTU.
Definition: packet.h:52
ContentInfo::upgrade
bool upgrade
This item is an upgrade.
Definition: tcp_content.h:80
ContentInfo::DOES_NOT_EXIST
@ DOES_NOT_EXIST
The content does not exist in the content system.
Definition: tcp_content.h:61
TCPConnecter
"Helper" class for creating TCP connections in a non-blocking manner
Definition: tcp.h:64
NETWORK_CONTENT_MIRROR_PORT
static const uint16 NETWORK_CONTENT_MIRROR_PORT
The default port of the content mirror (TCP)
Definition: config.h:28
CONTENT_TYPE_BASE_SOUNDS
@ CONTENT_TYPE_BASE_SOUNDS
The content consists of base sounds.
Definition: tcp_content.h:29
HasScenario
bool HasScenario(const ContentInfo *ci, bool md5sum)
Check whether we've got a given scenario based on its unique ID.
Definition: fios.cpp:740
BASESET_DIR
@ BASESET_DIR
Subdirectory for all base data (base sets, intro game)
Definition: fileio_type.h:116
CONTENT_TYPE_AI
@ CONTENT_TYPE_AI
The content consists of an AI.
Definition: tcp_content.h:25
ContentInfo::tags
char(* tags)[32]
Malloced array of tags (strings)
Definition: tcp_content.h:78
ClientNetworkContentSocketHandler::lastActivity
std::chrono::steady_clock::time_point lastActivity
The last time there was network activity.
Definition: network_content.h:77
NETWORK_CONTENT_SERVER_HOST
static const char *const NETWORK_CONTENT_SERVER_HOST
DNS hostname of the content server.
Definition: config.h:18
ClientNetworkContentSocketHandler::http_response_index
int http_response_index
Where we are, in the response, with handling it.
Definition: network_content.h:72
ContentCallback::OnConnect
virtual void OnConnect(bool success)
Callback for when the connection has finished.
Definition: network_content.h:32
CONTENT_TYPE_BASE_GRAPHICS
@ CONTENT_TYPE_BASE_GRAPHICS
The content consists of base graphics.
Definition: tcp_content.h:23
PACKET_CONTENT_CLIENT_CONTENT
@ PACKET_CONTENT_CLIENT_CONTENT
Request a content file given an internal ID.
Definition: tcp_content.h:43
ContentVector
std::vector< ContentInfo * > ContentVector
Vector with content info.
Definition: network_content.h:17
include
bool include(std::vector< T > &vec, const T &item)
Helper function to append an item to a vector if it is not already contained Consider using std::set,...
Definition: smallvec_type.hpp:27
NetworkTCPSocketHandler::sock
SOCKET sock
The socket currently connected to.
Definition: tcp.h:34
NetworkContentSocketHandler::ReceivePackets
bool ReceivePackets()
Receive a packet at TCP level.
Definition: tcp_content.cpp:186
ClientNetworkContentSocketHandler::CheckDependencyState
void CheckDependencyState(ContentInfo *ci)
Check the dependencies (recursively) of this content info.
Definition: network_content.cpp:945
ClientNetworkContentSocketHandler
Socket handler for the content server connection.
Definition: network_content.h:65
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:79
ContentCallback::OnDownloadComplete
virtual void OnDownloadComplete(ContentID cid)
We have finished downloading a file.
Definition: network_content.h:56
ContentInfo::filename
char filename[48]
Filename (for the .tar.gz; only valid on download)
Definition: tcp_content.h:68
ClientNetworkContentSocketHandler::Clear
void Clear()
Clear all downloaded content information.
Definition: network_content.cpp:1033
ClientNetworkContentSocketHandler::isConnecting
bool isConnecting
Whether we're connecting.
Definition: network_content.h:76
Packet::Send_uint8
void Send_uint8(uint8 data)
Package a 8 bits integer in the packet.
Definition: packet.cpp:96
HasGRFConfig
static bool HasGRFConfig(const ContentInfo *ci, bool md5sum)
Wrapper function for the HasProc.
Definition: network_content.cpp:38
ContentInfo::IsValid
bool IsValid() const
Is the information from this content info valid?
Definition: tcp_content.cpp:90
CONTENT_TYPE_BASE_MUSIC
@ CONTENT_TYPE_BASE_MUSIC
The content consists of base music.
Definition: tcp_content.h:30
NetworkHTTPContentConnecter
Connect with a HTTP server and do ONE query.
Definition: tcp_http.h:75
ClientNetworkContentSocketHandler::curInfo
ContentInfo * curInfo
Information about the currently downloaded file.
Definition: network_content.h:75
ClientNetworkContentSocketHandler::DownloadSelectedContentHTTP
void DownloadSelectedContentHTTP(const ContentIDList &content)
Initiate downloading the content over HTTP.
Definition: network_content.cpp:327
ShowErrorMessage
void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel wl, int x=0, int y=0, const GRFFile *textref_stack_grffile=nullptr, uint textref_stack_size=0, const uint32 *textref_stack=nullptr)
Display an error message in a window.
Definition: error_gui.cpp:372
GetContentInfoSubDir
Subdirectory GetContentInfoSubDir(ContentType type)
Helper to get the subdirectory a ContentInfo is located in.
Definition: tcp_content.cpp:245
Packet::Send_uint32
void Send_uint32(uint32 data)
Package a 32 bits integer in the packet.
Definition: packet.cpp:117
NetworkSettings::no_http_content_downloads
bool no_http_content_downloads
do not do content downloads over HTTP
Definition: settings_type.h:287
ClientNetworkContentSocketHandler::RequestContentList
void RequestContentList(ContentType type)
Request the content list for the given type.
Definition: network_content.cpp:185
FGCM_ANY
@ FGCM_ANY
Use first found.
Definition: newgrf_config.h:197
ContentCallback::OnReceiveContentInfo
virtual void OnReceiveContentInfo(const ContentInfo *ci)
We received a content info.
Definition: network_content.h:43
GetFullFilename
static std::string GetFullFilename(const ContentInfo *ci, bool compressed)
Determine the full filename of a piece of content information.
Definition: network_content.cpp:388
ClientNetworkContentSocketHandler::OnDisconnect
void OnDisconnect() override
Callback for when the connection got disconnected.
Definition: network_content.cpp:1053
ContentInfo::UNSELECTED
@ UNSELECTED
The content has not been selected.
Definition: tcp_content.h:57
Packet::Recv_uint32
uint32 Recv_uint32()
Read a 32 bits integer from the packet.
Definition: packet.cpp:246
NetworkContentConnecter::OnFailure
void OnFailure() override
Callback for when the connection attempt failed.
Definition: network_content.cpp:737
NetworkSocketHandler::Reopen
void Reopen()
Reopen the socket so we can send/receive stuff again.
Definition: core.h:72
CONTENT_TYPE_HEIGHTMAP
@ CONTENT_TYPE_HEIGHTMAP
The content consists of a heightmap.
Definition: tcp_content.h:28
ClientNetworkContentSocketHandler::infos
ContentVector infos
All content info we received.
Definition: network_content.h:70
ClientNetworkContentSocketHandler::DownloadContentInfo
void DownloadContentInfo(ContentID cid)
Download information of a given Content ID if not already tried.
Definition: network_content.cpp:802
ClientNetworkContentSocketHandler::~ClientNetworkContentSocketHandler
~ClientNetworkContentSocketHandler()
Clear up the mess ;)
Definition: network_content.cpp:720
ClientNetworkContentSocketHandler::UnselectAll
void UnselectAll()
Unselect everything that we've not downloaded so far.
Definition: network_content.cpp:874
ContentInfo
Container for all important information about a piece of content.
Definition: tcp_content.h:54
Packet::size
PacketSize size
The size of the whole packet for received packets.
Definition: packet.h:48
SEND_MTU
static const uint16 SEND_MTU
Number of bytes we can pack in a single packet.
Definition: config.h:33
NETWORK_CONTENT_MIRROR_URL
static const char *const NETWORK_CONTENT_MIRROR_URL
URL of the HTTP mirror system.
Definition: config.h:22
ContentInfo::md5sum
byte md5sum[16]
The MD5 checksum.
Definition: tcp_content.h:74
ClientNetworkContentSocketHandler::OnReceiveData
void OnReceiveData(const char *data, size_t length) override
We're receiving data.
Definition: network_content.cpp:581
ClientNetworkContentSocketHandler::Unselect
void Unselect(ContentID cid)
Unselect a specific content id.
Definition: network_content.cpp:842
AI::HasAI
static bool HasAI(const struct ContentInfo *ci, bool md5sum)
Wrapper function for AIScanner::HasAI.
Definition: ai_core.cpp:367
CONTENT_TYPE_GAME
@ CONTENT_TYPE_GAME
The content consists of a game script.
Definition: tcp_content.h:31
CONTENT_TYPE_SCENARIO
@ CONTENT_TYPE_SCENARIO
The content consists of a scenario.
Definition: tcp_content.h:27
ClientNetworkContentSocketHandler::OnFailure
void OnFailure() override
An error has occurred and the connection has been closed.
Definition: network_content.cpp:561
ClientNetworkContentSocketHandler::Close
void Close() override
Disconnect from the content server.
Definition: network_content.cpp:767
Packet::Send_uint16
void Send_uint16(uint16 data)
Package a 16 bits integer in the packet.
Definition: packet.cpp:106
NetworkTCPSocketHandler::SendPacket
virtual void SendPacket(Packet *packet)
This function puts the packet in the send-queue and it is send as soon as possible.
Definition: tcp.cpp:61
StrEmpty
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:60
ContentInfo::SELECTED
@ SELECTED
The content has been manually selected.
Definition: tcp_content.h:58
CONTENT_TYPE_AI_LIBRARY
@ CONTENT_TYPE_AI_LIBRARY
The content consists of an AI library.
Definition: tcp_content.h:26
ClientNetworkContentSocketHandler::IDLE_TIMEOUT
static constexpr std::chrono::seconds IDLE_TIMEOUT
The idle timeout; when to close the connection because it's idle.
Definition: network_content.h:103
PACKET_CONTENT_CLIENT_INFO_ID
@ PACKET_CONTENT_CLIENT_INFO_ID
Queries the content server for information about a list of internal IDs.
Definition: tcp_content.h:39
ClientNetworkContentSocketHandler::curFile
FILE * curFile
Currently downloaded file.
Definition: network_content.h:74
HasProc
bool(* HasProc)(const ContentInfo *ci, bool md5sum)
Check whether a function piece of content is locally known.
Definition: network_content.cpp:50
ClientNetworkContentSocketHandler::OnDownloadComplete
void OnDownloadComplete(ContentID cid) override
We have finished downloading a file.
Definition: network_content.cpp:1081
ClientNetworkContentSocketHandler::OnDownloadProgress
void OnDownloadProgress(const ContentInfo *ci, int bytes) override
We have progress in the download of a file.
Definition: network_content.cpp:1072
ContentInfo::url
char url[96]
URL related to the content.
Definition: tcp_content.h:71
Packet
Internal entity of a packet.
Definition: packet.h:40
ClientNetworkContentSocketHandler::DownloadSelectedContent
void DownloadSelectedContent(uint &files, uint &bytes, bool fallback=false)
Actually begin downloading the content we selected.
Definition: network_content.cpp:292
TCPConnecter::address
NetworkAddress address
Address we're connecting to.
Definition: tcp.h:77
PacketReader::buf
byte * buf
Buffer we're going to write to/read from.
Definition: network_client.cpp:44
BSWAP32
static uint32 BSWAP32(uint32 x)
Perform a 32 bits endianness bitswap on x.
Definition: bitmath_func.hpp:380
CONTENT_TYPE_GAME_LIBRARY
@ CONTENT_TYPE_GAME_LIBRARY
The content consists of a GS library.
Definition: tcp_content.h:32
CONTENT_TYPE_NEWGRF
@ CONTENT_TYPE_NEWGRF
The content consists of a NewGRF.
Definition: tcp_content.h:24
ClientNetworkContentSocketHandler::http_response
std::vector< char > http_response
The HTTP response to the requests we've been doing.
Definition: network_content.h:71
NETWORK_CONTENT_SERVER_PORT
static const uint16 NETWORK_CONTENT_SERVER_PORT
The default port of the content server (TCP)
Definition: config.h:27
TarScanner::AddFile
bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename={}) override
Add a file with the given filename.
Definition: fileio.cpp:617
ContentInfo::description
char description[512]
Description of the content.
Definition: tcp_content.h:72
NetworkHTTPSocketHandler::Connect
static int Connect(char *uri, HTTPCallback *callback, const char *data=nullptr, int depth=0)
Connect to the given URI.
Definition: tcp_http.cpp:193
PACKET_CONTENT_CLIENT_INFO_LIST
@ PACKET_CONTENT_CLIENT_INFO_LIST
Queries the content server for a list of info of a given content type.
Definition: tcp_content.h:38
_network_content_client
ClientNetworkContentSocketHandler _network_content_client
The client we use to connect to the server.
Definition: network_content.cpp:35
ClientNetworkContentSocketHandler::ContentIDList
std::vector< ContentID > ContentIDList
List of content IDs to (possibly) select.
Definition: network_content.h:67
NetworkTCPSocketHandler::SendPackets
SendPacketsState SendPackets(bool closing_down=false)
Sends all the buffered packets out for this client.
Definition: tcp.cpp:95
NetworkAddress
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition: address.h:29
NetworkTCPSocketHandler::CanSendReceive
bool CanSendReceive()
Check whether this socket can send or receive something.
Definition: tcp.cpp:225
DeleteWindowById
void DeleteWindowById(WindowClass cls, WindowNumber number, bool force)
Delete a window by its class and window number (if it is open).
Definition: window.cpp:1165
ContentInfo::ALREADY_HERE
@ ALREADY_HERE
The content is already at the client side.
Definition: tcp_content.h:60
PacketSize
uint16 PacketSize
Size of the whole packet.
Definition: packet.h:19
ClientNetworkContentSocketHandler::ReverseLookupTreeDependency
void ReverseLookupTreeDependency(ConstContentVector &tree, const ContentInfo *child) const
Reverse lookup the dependencies of all parents over a given child.
Definition: network_content.cpp:923
ClientNetworkContentSocketHandler::Connect
void Connect()
Connect with the content server.
Definition: network_content.cpp:757
ContentInfo::AUTOSELECTED
@ AUTOSELECTED
The content has been selected as dependency.
Definition: tcp_content.h:59
NO_DIRECTORY
@ NO_DIRECTORY
A path without any base directory.
Definition: fileio_type.h:125
ClientNetworkContentSocketHandler::ToggleSelectedState
void ToggleSelectedState(const ContentInfo *ci)
Toggle the state of a content info and check its dependencies.
Definition: network_content.cpp:882
WC_NETWORK_STATUS_WINDOW
@ WC_NETWORK_STATUS_WINDOW
Network status window; Window numbers:
Definition: window_type.h:485
ClientNetworkContentSocketHandler::callbacks
std::vector< ContentCallback * > callbacks
Callbacks to notify "the world".
Definition: network_content.h:68
PACKET_CONTENT_CLIENT_INFO_EXTID
@ PACKET_CONTENT_CLIENT_INFO_EXTID
Queries the content server for information about a list of external IDs.
Definition: tcp_content.h:40
Game::HasGame
static bool HasGame(const struct ContentInfo *ci, bool md5sum)
Wrapper function for GameScanner::HasGame.
Definition: game_core.cpp:264
BaseMedia< GraphicsSet >::HasSet
static bool HasSet(const ContentInfo *ci, bool md5sum)
Check whether we have an set with the exact characteristics as ci.
Definition: base_media_func.h:300
ContentInfo::dependency_count
uint8 dependency_count
Number of dependencies.
Definition: tcp_content.h:75
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:442
ClientNetworkContentSocketHandler::Select
void Select(ContentID cid)
Select a specific content id.
Definition: network_content.cpp:829
GunzipFile
static bool GunzipFile(const ContentInfo *ci)
Gunzip a given file and remove the .gz if successful.
Definition: network_content.cpp:405
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:108
ClientNetworkContentSocketHandler::requested
ContentIDList requested
ContentIDs we already requested (so we don't do it again)
Definition: network_content.h:69
Packet::Recv_uint8
uint8 Recv_uint8()
Read a 8 bits integer from the packet.
Definition: packet.cpp:217
NetworkContentConnecter
Connect to the content server.
Definition: network_content.cpp:729
NETWORK_CONTENT_MIRROR_HOST
static const char *const NETWORK_CONTENT_MIRROR_HOST
DNS hostname of the HTTP-content mirror server.
Definition: config.h:20
ClientNetworkContentSocketHandler::DownloadSelectedContentFallback
void DownloadSelectedContentFallback(const ContentIDList &content)
Initiate downloading the content over the fallback protocol.
Definition: network_content.cpp:355
WL_ERROR
@ WL_ERROR
Errors (eg. saving/loading failed)
Definition: error.h:24
ContentInfo::version
char version[16]
Version of the content.
Definition: tcp_content.h:70
ClientNetworkContentSocketHandler::OnReceiveContentInfo
void OnReceiveContentInfo(const ContentInfo *ci) override
We received a content info.
Definition: network_content.cpp:1062
ContentInfo::state
State state
Whether the content info is selected (for download)
Definition: tcp_content.h:79
ContentInfo::unique_id
uint32 unique_id
Unique ID; either GRF ID or shortname.
Definition: tcp_content.h:73
TarScanner
Helper for scanning for files with tar as extension.
Definition: fileio_func.h:81
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:367
ClientSettings::network
NetworkSettings network
settings related to the network
Definition: settings_type.h:578
ClientNetworkContentSocketHandler::SelectAll
void SelectAll()
Select everything we can select.
Definition: network_content.cpp:852
ContentID
ContentID
Unique identifier for the content.
Definition: tcp_content.h:49
NetworkContentConnecter::NetworkContentConnecter
NetworkContentConnecter(const NetworkAddress &address)
Initiate the connecting.
Definition: network_content.cpp:735
ClientNetworkContentSocketHandler::BeforeDownload
bool BeforeDownload()
Handle the opening of the file before downloading.
Definition: network_content.cpp:502
NetworkContentSocketHandler::Close
void Close() override
Really close the socket.
Definition: tcp_content.cpp:144
ContentInfo::name
char name[32]
Name of the content.
Definition: tcp_content.h:69
FGCM_EXACT
@ FGCM_EXACT
Only find Grfs matching md5sum.
Definition: newgrf_config.h:193
ClientNetworkContentSocketHandler::Receive_SERVER_INFO
bool Receive_SERVER_INFO(Packet *p) override
Server sending list of content info: byte type (invalid ID == does not exist) uint32 id uint32 file_s...
Definition: network_content.cpp:52
ClientNetworkContentSocketHandler::SelectUpgrade
void SelectUpgrade()
Select everything that's an update for something we've got.
Definition: network_content.cpp:863
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: string.cpp:112
ContentInfo::dependencies
ContentID * dependencies
Malloced array of dependencies (unique server side ids)
Definition: tcp_content.h:76
ContentInfo::id
ContentID id
Unique (server side) ID for the content.
Definition: tcp_content.h:66
ClientNetworkContentSocketHandler::AfterDownload
void AfterDownload()
Handle the closing and extracting of a file after downloading it has been done.
Definition: network_content.cpp:527
NetworkContentConnecter::OnConnect
void OnConnect(SOCKET s) override
Callback when the connection succeeded.
Definition: network_content.cpp:743
ClientNetworkContentSocketHandler::ClientNetworkContentSocketHandler
ClientNetworkContentSocketHandler()
Create a socket handler to handle the connection.
Definition: network_content.cpp:709
ContentCallback::OnDisconnect
virtual void OnDisconnect()
Callback for when the connection got disconnected.
Definition: network_content.h:37
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:383
ClientNetworkContentSocketHandler::GetContent
ContentInfo * GetContent(ContentID cid)
Get the content info based on a ContentID.
Definition: network_content.cpp:816
SVS_REPLACE_WITH_QUESTION_MARK
@ SVS_REPLACE_WITH_QUESTION_MARK
Replace the unknown/bad bits with question marks.
Definition: string_type.h:50
ConstContentVector
std::vector< const ContentInfo * > ConstContentVector
Vector with constant content info.
Definition: network_content.h:19
ClientNetworkContentSocketHandler::Receive_SERVER_CONTENT
bool Receive_SERVER_CONTENT(Packet *p) override
Server sending list of content info: uint32 unique id uint32 file size (0 == does not exist) string f...
Definition: network_content.cpp:462
WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD
@ WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD
Network content download status.
Definition: window_type.h:33
CONTENT_TYPE_END
@ CONTENT_TYPE_END
Helper to mark the end of the types.
Definition: tcp_content.h:33
INVALID_STRING_ID
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition: strings_type.h:17
ContentInfo::filesize
uint32 filesize
Size of the file.
Definition: tcp_content.h:67
ContentCallback::OnDownloadProgress
virtual void OnDownloadProgress(const ContentInfo *ci, int bytes)
We have progress in the download of a file.
Definition: network_content.h:50
Packet::pos
PacketSize pos
The current read/write position in the packet.
Definition: packet.h:50
PACKET_CONTENT_CLIENT_INFO_EXTID_MD5
@ PACKET_CONTENT_CLIENT_INFO_EXTID_MD5
Queries the content server for information about a list of external IDs and MD5.
Definition: tcp_content.h:41