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

OpenTTD Source 12.0-beta1

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 ci->name = p->Recv_string(NETWORK_CONTENT_NAME_LENGTH);
60 ci->version = p->Recv_string(NETWORK_CONTENT_VERSION_LENGTH);
61 ci->url = p->Recv_string(NETWORK_CONTENT_URL_LENGTH);
62 ci->description = p->Recv_string(NETWORK_CONTENT_DESC_LENGTH, 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 uint dependency_count = p->Recv_uint8();
70 ci->dependencies.reserve(dependency_count);
71 for (uint i = 0; i < dependency_count; i++) ci->dependencies.push_back((ContentID)p->Recv_uint32());
72
73 uint tag_count = p->Recv_uint8();
74 ci->tags.reserve(tag_count);
75 for (uint i = 0; i < tag_count; i++) ci->tags.push_back(p->Recv_string(NETWORK_CONTENT_TAG_LENGTH));
76
77 if (!ci->IsValid()) {
78 delete ci;
79 this->CloseConnection();
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 (ci->name.empty()) ci->name = ici->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 */
154 *ici = *ci;
155 delete ci;
156
157 this->OnReceiveContentInfo(ici);
158 return true;
159 }
160 }
161
162 /* Missing content info? Don't list it */
163 if (ci->filesize == 0) {
164 delete ci;
165 return true;
166 }
167
168 this->infos.push_back(ci);
169
170 /* Incoming data means that we might need to reconsider dependencies */
171 for (ContentInfo *ici : this->infos) {
172 this->CheckDependencyState(ici);
173 }
174
175 this->OnReceiveContentInfo(ci);
176
177 return true;
178 }
179
184 void ClientNetworkContentSocketHandler::RequestContentList(ContentType type)
185 {
186 if (type == CONTENT_TYPE_END) {
187 this->RequestContentList(CONTENT_TYPE_BASE_GRAPHICS);
188 this->RequestContentList(CONTENT_TYPE_BASE_MUSIC);
189 this->RequestContentList(CONTENT_TYPE_BASE_SOUNDS);
190 this->RequestContentList(CONTENT_TYPE_SCENARIO);
191 this->RequestContentList(CONTENT_TYPE_HEIGHTMAP);
192 this->RequestContentList(CONTENT_TYPE_AI);
193 this->RequestContentList(CONTENT_TYPE_AI_LIBRARY);
194 this->RequestContentList(CONTENT_TYPE_GAME);
195 this->RequestContentList(CONTENT_TYPE_GAME_LIBRARY);
196 this->RequestContentList(CONTENT_TYPE_NEWGRF);
197 return;
198 }
199
200 this->Connect();
201
202 Packet *p = new Packet(PACKET_CONTENT_CLIENT_INFO_LIST);
203 p->Send_uint8 ((byte)type);
204 p->Send_uint32(_openttd_newgrf_version);
205
206 this->SendPacket(p);
207 }
208
214 void ClientNetworkContentSocketHandler::RequestContentList(uint count, const ContentID *content_ids)
215 {
216 this->Connect();
217
218 while (count > 0) {
219 /* We can "only" send a limited number of IDs in a single packet.
220 * A packet begins with the packet size and a byte for the type.
221 * Then this packet adds a uint16 for the count in this packet.
222 * The rest of the packet can be used for the IDs. */
223 uint p_count = std::min<uint>(count, (TCP_MTU - sizeof(PacketSize) - sizeof(byte) - sizeof(uint16)) / sizeof(uint32));
224
225 Packet *p = new Packet(PACKET_CONTENT_CLIENT_INFO_ID, TCP_MTU);
226 p->Send_uint16(p_count);
227
228 for (uint i = 0; i < p_count; i++) {
229 p->Send_uint32(content_ids[i]);
230 }
231
232 this->SendPacket(p);
233 count -= p_count;
234 content_ids += p_count;
235 }
236 }
237
243 void ClientNetworkContentSocketHandler::RequestContentList(ContentVector *cv, bool send_md5sum)
244 {
245 if (cv == nullptr) return;
246
247 this->Connect();
248
249 assert(cv->size() < 255);
250 assert(cv->size() < (TCP_MTU - sizeof(PacketSize) - sizeof(byte) - sizeof(uint8)) /
251 (sizeof(uint8) + sizeof(uint32) + (send_md5sum ? /*sizeof(ContentInfo::md5sum)*/16 : 0)));
252
253 Packet *p = new Packet(send_md5sum ? PACKET_CONTENT_CLIENT_INFO_EXTID_MD5 : PACKET_CONTENT_CLIENT_INFO_EXTID, TCP_MTU);
254 p->Send_uint8((uint8)cv->size());
255
256 for (const ContentInfo *ci : *cv) {
257 p->Send_uint8((byte)ci->type);
258 p->Send_uint32(ci->unique_id);
259 if (!send_md5sum) continue;
260
261 for (uint j = 0; j < sizeof(ci->md5sum); j++) {
262 p->Send_uint8(ci->md5sum[j]);
263 }
264 }
265
266 this->SendPacket(p);
267
268 for (ContentInfo *ci : *cv) {
269 bool found = false;
270 for (ContentInfo *ci2 : this->infos) {
271 if (ci->type == ci2->type && ci->unique_id == ci2->unique_id &&
272 (!send_md5sum || memcmp(ci->md5sum, ci2->md5sum, sizeof(ci->md5sum)) == 0)) {
273 found = true;
274 break;
275 }
276 }
277 if (!found) {
278 this->infos.push_back(ci);
279 } else {
280 delete ci;
281 }
282 }
283 }
284
291 void ClientNetworkContentSocketHandler::DownloadSelectedContent(uint &files, uint &bytes, bool fallback)
292 {
293 bytes = 0;
294
295 #ifdef __EMSCRIPTEN__
296 /* Emscripten is loaded via an HTTPS connection. As such, it is very
297 * difficult to make HTTP connections. So always use the TCP method of
298 * downloading content. */
299 fallback = true;
300 #endif
301
302 ContentIDList content;
303 for (const ContentInfo *ci : this->infos) {
304 if (!ci->IsSelected() || ci->state == ContentInfo::ALREADY_HERE) continue;
305
306 content.push_back(ci->id);
307 bytes += ci->filesize;
308 }
309
310 files = (uint)content.size();
311
312 /* If there's nothing to download, do nothing. */
313 if (files == 0) return;
314
315 if (_settings_client.network.no_http_content_downloads || fallback) {
316 this->DownloadSelectedContentFallback(content);
317 } else {
318 this->DownloadSelectedContentHTTP(content);
319 }
320 }
321
326 void ClientNetworkContentSocketHandler::DownloadSelectedContentHTTP(const ContentIDList &content)
327 {
328 uint count = (uint)content.size();
329
330 /* Allocate memory for the whole request.
331 * Requests are "id\nid\n..." (as strings), so assume the maximum ID,
332 * which is uint32 so 10 characters long. Then the newlines and
333 * multiply that all with the count and then add the '\0'. */
334 uint bytes = (10 + 1) * count + 1;
335 char *content_request = MallocT<char>(bytes);
336 const char *lastof = content_request + bytes - 1;
337
338 char *p = content_request;
339 for (const ContentID &id : content) {
340 p += seprintf(p, lastof, "%d\n", id);
341 }
342
343 this->http_response_index = -1;
344
345 new NetworkHTTPContentConnecter(NetworkContentMirrorConnectionString(), this, NETWORK_CONTENT_MIRROR_URL, content_request);
346 /* NetworkHTTPContentConnecter takes over freeing of content_request! */
347 }
348
353 void ClientNetworkContentSocketHandler::DownloadSelectedContentFallback(const ContentIDList &content)
354 {
355 uint count = (uint)content.size();
356 const ContentID *content_ids = content.data();
357 this->Connect();
358
359 while (count > 0) {
360 /* We can "only" send a limited number of IDs in a single packet.
361 * A packet begins with the packet size and a byte for the type.
362 * Then this packet adds a uint16 for the count in this packet.
363 * The rest of the packet can be used for the IDs. */
364 uint p_count = std::min<uint>(count, (TCP_MTU - sizeof(PacketSize) - sizeof(byte) - sizeof(uint16)) / sizeof(uint32));
365
366 Packet *p = new Packet(PACKET_CONTENT_CLIENT_CONTENT, TCP_MTU);
367 p->Send_uint16(p_count);
368
369 for (uint i = 0; i < p_count; i++) {
370 p->Send_uint32(content_ids[i]);
371 }
372
373 this->SendPacket(p);
374 count -= p_count;
375 content_ids += p_count;
376 }
377 }
378
386 static std::string GetFullFilename(const ContentInfo *ci, bool compressed)
387 {
388 Subdirectory dir = GetContentInfoSubDir(ci->type);
389 if (dir == NO_DIRECTORY) return {};
390
391 std::string buf = FioGetDirectory(SP_AUTODOWNLOAD_DIR, dir);
392 buf += ci->filename;
393 buf += compressed ? ".tar.gz" : ".tar";
394
395 return buf;
396 }
397
403 static bool GunzipFile(const ContentInfo *ci)
404 {
405 #if defined(WITH_ZLIB)
406 bool ret = true;
407
408 /* Need to open the file with fopen() to support non-ASCII on Windows. */
409 FILE *ftmp = fopen(GetFullFilename(ci, true).c_str(), "rb");
410 if (ftmp == nullptr) return false;
411 /* Duplicate the handle, and close the FILE*, to avoid double-closing the handle later. */
412 int fdup = dup(fileno(ftmp));
413 gzFile fin = gzdopen(fdup, "rb");
414 fclose(ftmp);
415
416 FILE *fout = fopen(GetFullFilename(ci, false).c_str(), "wb");
417
418 if (fin == nullptr || fout == nullptr) {
419 ret = false;
420 } else {
421 byte buff[8192];
422 for (;;) {
423 int read = gzread(fin, buff, sizeof(buff));
424 if (read == 0) {
425 /* If gzread() returns 0, either the end-of-file has been
426 * reached or an underlying read error has occurred.
427 *
428 * gzeof() can't be used, because:
429 * 1.2.5 - it is safe, 1 means 'everything was OK'
430 * 1.2.3.5, 1.2.4 - 0 or 1 is returned 'randomly'
431 * 1.2.3.3 - 1 is returned for truncated archive
432 *
433 * So we use gzerror(). When proper end of archive
434 * has been reached, then:
435 * errnum == Z_STREAM_END in 1.2.3.3,
436 * errnum == 0 in 1.2.4 and 1.2.5 */
437 int errnum;
438 gzerror(fin, &errnum);
439 if (errnum != 0 && errnum != Z_STREAM_END) ret = false;
440 break;
441 }
442 if (read < 0 || (size_t)read != fwrite(buff, 1, read, fout)) {
443 /* If gzread() returns -1, there was an error in archive */
444 ret = false;
445 break;
446 }
447 /* DO NOT DO THIS! It will fail to detect broken archive with 1.2.3.3!
448 * if (read < sizeof(buff)) break; */
449 }
450 }
451
452 if (fin != nullptr) {
453 gzclose(fin);
454 } else if (fdup != -1) {
455 /* Failing gzdopen does not close the passed file descriptor. */
456 close(fdup);
457 }
458 if (fout != nullptr) fclose(fout);
459
460 return ret;
461 #else
462 NOT_REACHED();
463 #endif /* defined(WITH_ZLIB) */
464 }
465
473 static inline ssize_t TransferOutFWrite(FILE *file, const char *buffer, size_t amount)
474 {
475 return fwrite(buffer, 1, amount, file);
476 }
477
478 bool ClientNetworkContentSocketHandler::Receive_SERVER_CONTENT(Packet *p)
479 {
480 if (this->curFile == nullptr) {
481 delete this->curInfo;
482 /* When we haven't opened a file this must be our first packet with metadata. */
483 this->curInfo = new ContentInfo;
484 this->curInfo->type = (ContentType)p->Recv_uint8();
485 this->curInfo->id = (ContentID)p->Recv_uint32();
486 this->curInfo->filesize = p->Recv_uint32();
487 this->curInfo->filename = p->Recv_string(NETWORK_CONTENT_FILENAME_LENGTH);
488
489 if (!this->BeforeDownload()) {
490 this->CloseConnection();
491 return false;
492 }
493 } else {
494 /* We have a file opened, thus are downloading internal content */
495 size_t toRead = p->RemainingBytesToTransfer();
496 if (toRead != 0 && (size_t)p->TransferOut(TransferOutFWrite, this->curFile) != toRead) {
497 CloseWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD);
498 ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE, WL_ERROR);
499 this->CloseConnection();
500 fclose(this->curFile);
501 this->curFile = nullptr;
502
503 return false;
504 }
505
506 this->OnDownloadProgress(this->curInfo, (int)toRead);
507
508 if (toRead == 0) this->AfterDownload();
509 }
510
511 return true;
512 }
513
518 bool ClientNetworkContentSocketHandler::BeforeDownload()
519 {
520 if (!this->curInfo->IsValid()) {
521 delete this->curInfo;
522 this->curInfo = nullptr;
523 return false;
524 }
525
526 if (this->curInfo->filesize != 0) {
527 /* The filesize is > 0, so we are going to download it */
528 std::string filename = GetFullFilename(this->curInfo, true);
529 if (filename.empty() || (this->curFile = fopen(filename.c_str(), "wb")) == nullptr) {
530 /* Unless that fails of course... */
531 CloseWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD);
532 ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE, WL_ERROR);
533 return false;
534 }
535 }
536 return true;
537 }
538
543 void ClientNetworkContentSocketHandler::AfterDownload()
544 {
545 /* We read nothing; that's our marker for end-of-stream.
546 * Now gunzip the tar and make it known. */
547 fclose(this->curFile);
548 this->curFile = nullptr;
549
550 if (GunzipFile(this->curInfo)) {
551 unlink(GetFullFilename(this->curInfo, true).c_str());
552
553 Subdirectory sd = GetContentInfoSubDir(this->curInfo->type);
554 if (sd == NO_DIRECTORY) NOT_REACHED();
555
556 TarScanner ts;
557 std::string fname = GetFullFilename(this->curInfo, false);
558 ts.AddFile(sd, fname);
559
560 if (this->curInfo->type == CONTENT_TYPE_BASE_MUSIC) {
561 /* Music can't be in a tar. So extract the tar! */
562 ExtractTar(fname, BASESET_DIR);
563 unlink(fname.c_str());
564 }
565
566 #ifdef __EMSCRIPTEN__
567 EM_ASM(if (window["openttd_syncfs"]) openttd_syncfs());
568 #endif
569
570 this->OnDownloadComplete(this->curInfo->id);
571 } else {
572 ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_EXTRACT, INVALID_STRING_ID, WL_ERROR);
573 }
574 }
575
576 /* Also called to just clean up the mess. */
577 void ClientNetworkContentSocketHandler::OnFailure()
578 {
579 /* If we fail, download the rest via the 'old' system. */
580 uint files, bytes;
581 this->DownloadSelectedContent(files, bytes, true);
582
583 this->http_response.clear();
584 this->http_response.shrink_to_fit();
585 this->http_response_index = -2;
586
587 if (this->curFile != nullptr) {
588 /* Revert the download progress when we are going for the old system. */
589 long size = ftell(this->curFile);
590 if (size > 0) this->OnDownloadProgress(this->curInfo, (int)-size);
591
592 fclose(this->curFile);
593 this->curFile = nullptr;
594 }
595 }
596
597 void ClientNetworkContentSocketHandler::OnReceiveData(const char *data, size_t length)
598 {
599 assert(data == nullptr || length != 0);
600
601 /* Ignore any latent data coming from a connection we closed. */
602 if (this->http_response_index == -2) return;
603
604 if (this->http_response_index == -1) {
605 if (data != nullptr) {
606 /* Append the rest of the response. */
607 this->http_response.insert(this->http_response.end(), data, data + length);
608 return;
609 } else {
610 /* Make sure the response is properly terminated. */
611 this->http_response.push_back('\0');
612
613 /* And prepare for receiving the rest of the data. */
614 this->http_response_index = 0;
615 }
616 }
617
618 if (data != nullptr) {
619 /* We have data, so write it to the file. */
620 if (fwrite(data, 1, length, this->curFile) != length) {
621 /* Writing failed somehow, let try via the old method. */
622 this->OnFailure();
623 } else {
624 /* Just received the data. */
625 this->OnDownloadProgress(this->curInfo, (int)length);
626 }
627 /* Nothing more to do now. */
628 return;
629 }
630
631 if (this->curFile != nullptr) {
632 /* We've finished downloading a file. */
633 this->AfterDownload();
634 }
635
636 if ((uint)this->http_response_index >= this->http_response.size()) {
637 /* It's not a real failure, but if there's
638 * nothing more to download it helps with
639 * cleaning up the stuff we allocated. */
640 this->OnFailure();
641 return;
642 }
643
644 delete this->curInfo;
645 /* When we haven't opened a file this must be our first packet with metadata. */
646 this->curInfo = new ContentInfo;
647
649 #define check_not_null(p) { if ((p) == nullptr) { this->OnFailure(); return; } }
650
651 #define check_and_terminate(p) { check_not_null(p); *(p) = '\0'; }
652
653 for (;;) {
654 char *str = this->http_response.data() + this->http_response_index;
655 char *p = strchr(str, '\n');
656 check_and_terminate(p);
657
658 /* Update the index for the next one */
659 this->http_response_index += (int)strlen(str) + 1;
660
661 /* Read the ID */
662 p = strchr(str, ',');
663 check_and_terminate(p);
664 this->curInfo->id = (ContentID)atoi(str);
665
666 /* Read the type */
667 str = p + 1;
668 p = strchr(str, ',');
669 check_and_terminate(p);
670 this->curInfo->type = (ContentType)atoi(str);
671
672 /* Read the file size */
673 str = p + 1;
674 p = strchr(str, ',');
675 check_and_terminate(p);
676 this->curInfo->filesize = atoi(str);
677
678 /* Read the URL */
679 str = p + 1;
680 /* Is it a fallback URL? If so, just continue with the next one. */
681 if (strncmp(str, "ottd", 4) == 0) {
682 if ((uint)this->http_response_index >= this->http_response.size()) {
683 /* Have we gone through all lines? */
684 this->OnFailure();
685 return;
686 }
687 continue;
688 }
689
690 p = strrchr(str, '/');
691 check_not_null(p);
692 p++; // Start after the '/'
693
694 char tmp[MAX_PATH];
695 if (strecpy(tmp, p, lastof(tmp)) == lastof(tmp)) {
696 this->OnFailure();
697 return;
698 }
699 /* Remove the extension from the string. */
700 for (uint i = 0; i < 2; i++) {
701 p = strrchr(tmp, '.');
702 check_and_terminate(p);
703 }
704
705 /* Copy the string, without extension, to the filename. */
706 this->curInfo->filename = tmp;
707
708 /* Request the next file. */
709 if (!this->BeforeDownload()) {
710 this->OnFailure();
711 return;
712 }
713
714 NetworkHTTPSocketHandler::Connect(str, this);
715 return;
716 }
717
718 #undef check
719 #undef check_and_terminate
720 }
721
725 ClientNetworkContentSocketHandler::ClientNetworkContentSocketHandler() :
726 NetworkContentSocketHandler(),
727 http_response_index(-2),
728 curFile(nullptr),
729 curInfo(nullptr),
730 isConnecting(false)
731 {
732 this->lastActivity = std::chrono::steady_clock::now();
733 }
734
736 ClientNetworkContentSocketHandler::~ClientNetworkContentSocketHandler()
737 {
738 delete this->curInfo;
739 if (this->curFile != nullptr) fclose(this->curFile);
740
741 for (ContentInfo *ci : this->infos) delete ci;
742 }
743
745 class NetworkContentConnecter : TCPConnecter {
746 public:
751 NetworkContentConnecter(const std::string &connection_string) : TCPConnecter(connection_string, NETWORK_CONTENT_SERVER_PORT) {}
752
753 void OnFailure() override
754 {
755 _network_content_client.isConnecting = false;
756 _network_content_client.OnConnect(false);
757 }
758
759 void OnConnect(SOCKET s) override
760 {
761 assert(_network_content_client.sock == INVALID_SOCKET);
762 _network_content_client.lastActivity = std::chrono::steady_clock::now();
763 _network_content_client.isConnecting = false;
764 _network_content_client.sock = s;
765 _network_content_client.Reopen();
766 _network_content_client.OnConnect(true);
767 }
768 };
769
773 void ClientNetworkContentSocketHandler::Connect()
774 {
775 if (this->sock != INVALID_SOCKET || this->isConnecting) return;
776 this->isConnecting = true;
777 new NetworkContentConnecter(NetworkContentServerConnectionString());
778 }
779
783 NetworkRecvStatus ClientNetworkContentSocketHandler::CloseConnection(bool error)
784 {
785 NetworkContentSocketHandler::CloseConnection();
786
787 if (this->sock == INVALID_SOCKET) return NETWORK_RECV_STATUS_OKAY;
788
789 this->CloseSocket();
790 this->OnDisconnect();
791
792 return NETWORK_RECV_STATUS_OKAY;
793 }
794
799 void ClientNetworkContentSocketHandler::SendReceive()
800 {
801 if (this->sock == INVALID_SOCKET || this->isConnecting) return;
802
803 if (std::chrono::steady_clock::now() > this->lastActivity + IDLE_TIMEOUT) {
804 this->CloseConnection();
805 return;
806 }
807
808 if (this->CanSendReceive()) {
809 if (this->ReceivePackets()) {
810 /* Only update activity once a packet is received, instead of every time we try it. */
811 this->lastActivity = std::chrono::steady_clock::now();
812 }
813 }
814
815 this->SendPackets();
816 }
817
822 void ClientNetworkContentSocketHandler::DownloadContentInfo(ContentID cid)
823 {
824 /* When we tried to download it already, don't try again */
825 if (std::find(this->requested.begin(), this->requested.end(), cid) != this->requested.end()) return;
826
827 this->requested.push_back(cid);
828 this->RequestContentList(1, &cid);
829 }
830
836 ContentInfo *ClientNetworkContentSocketHandler::GetContent(ContentID cid)
837 {
838 for (ContentInfo *ci : this->infos) {
839 if (ci->id == cid) return ci;
840 }
841 return nullptr;
842 }
843
844
849 void ClientNetworkContentSocketHandler::Select(ContentID cid)
850 {
851 ContentInfo *ci = this->GetContent(cid);
852 if (ci == nullptr || ci->state != ContentInfo::UNSELECTED) return;
853
854 ci->state = ContentInfo::SELECTED;
855 this->CheckDependencyState(ci);
856 }
857
862 void ClientNetworkContentSocketHandler::Unselect(ContentID cid)
863 {
864 ContentInfo *ci = this->GetContent(cid);
865 if (ci == nullptr || !ci->IsSelected()) return;
866
867 ci->state = ContentInfo::UNSELECTED;
868 this->CheckDependencyState(ci);
869 }
870
872 void ClientNetworkContentSocketHandler::SelectAll()
873 {
874 for (ContentInfo *ci : this->infos) {
875 if (ci->state == ContentInfo::UNSELECTED) {
876 ci->state = ContentInfo::SELECTED;
877 this->CheckDependencyState(ci);
878 }
879 }
880 }
881
883 void ClientNetworkContentSocketHandler::SelectUpgrade()
884 {
885 for (ContentInfo *ci : this->infos) {
886 if (ci->state == ContentInfo::UNSELECTED && ci->upgrade) {
887 ci->state = ContentInfo::SELECTED;
888 this->CheckDependencyState(ci);
889 }
890 }
891 }
892
894 void ClientNetworkContentSocketHandler::UnselectAll()
895 {
896 for (ContentInfo *ci : this->infos) {
897 if (ci->IsSelected() && ci->state != ContentInfo::ALREADY_HERE) ci->state = ContentInfo::UNSELECTED;
898 }
899 }
900
902 void ClientNetworkContentSocketHandler::ToggleSelectedState(const ContentInfo *ci)
903 {
904 switch (ci->state) {
905 case ContentInfo::SELECTED:
906 case ContentInfo::AUTOSELECTED:
907 this->Unselect(ci->id);
908 break;
909
910 case ContentInfo::UNSELECTED:
911 this->Select(ci->id);
912 break;
913
914 default:
915 break;
916 }
917 }
918
924 void ClientNetworkContentSocketHandler::ReverseLookupDependency(ConstContentVector &parents, const ContentInfo *child) const
925 {
926 for (const ContentInfo *ci : this->infos) {
927 if (ci == child) continue;
928
929 for (auto &dependency : ci->dependencies) {
930 if (dependency == child->id) {
931 parents.push_back(ci);
932 break;
933 }
934 }
935 }
936 }
937
943 void ClientNetworkContentSocketHandler::ReverseLookupTreeDependency(ConstContentVector &tree, const ContentInfo *child) const
944 {
945 tree.push_back(child);
946
947 /* First find all direct parents. We can't use the "normal" iterator as
948 * we are including stuff into the vector and as such the vector's data
949 * store can be reallocated (and thus move), which means out iterating
950 * pointer gets invalid. So fall back to the indices. */
951 for (uint i = 0; i < tree.size(); i++) {
952 ConstContentVector parents;
953 this->ReverseLookupDependency(parents, tree[i]);
954
955 for (const ContentInfo *ci : parents) {
956 include(tree, ci);
957 }
958 }
959 }
960
965 void ClientNetworkContentSocketHandler::CheckDependencyState(ContentInfo *ci)
966 {
967 if (ci->IsSelected() || ci->state == ContentInfo::ALREADY_HERE) {
968 /* Selection is easy; just walk all children and set the
969 * autoselected state. That way we can see what we automatically
970 * selected and thus can unselect when a dependency is removed. */
971 for (auto &dependency : ci->dependencies) {
972 ContentInfo *c = this->GetContent(dependency);
973 if (c == nullptr) {
974 this->DownloadContentInfo(dependency);
975 } else if (c->state == ContentInfo::UNSELECTED) {
976 c->state = ContentInfo::AUTOSELECTED;
977 this->CheckDependencyState(c);
978 }
979 }
980 return;
981 }
982
983 if (ci->state != ContentInfo::UNSELECTED) return;
984
985 /* For unselection we need to find the parents of us. We need to
986 * unselect them. After that we unselect all children that we
987 * depend on and are not used as dependency for us, but only when
988 * we automatically selected them. */
989 ConstContentVector parents;
990 this->ReverseLookupDependency(parents, ci);
991 for (const ContentInfo *c : parents) {
992 if (!c->IsSelected()) continue;
993
994 this->Unselect(c->id);
995 }
996
997 for (auto &dependency : ci->dependencies) {
998 const ContentInfo *c = this->GetContent(dependency);
999 if (c == nullptr) {
1000 DownloadContentInfo(dependency);
1001 continue;
1002 }
1003 if (c->state != ContentInfo::AUTOSELECTED) continue;
1004
1005 /* Only unselect when WE are the only parent. */
1006 parents.clear();
1007 this->ReverseLookupDependency(parents, c);
1008
1009 /* First check whether anything depends on us */
1010 int sel_count = 0;
1011 bool force_selection = false;
1012 for (const ContentInfo *parent_ci : parents) {
1013 if (parent_ci->IsSelected()) sel_count++;
1014 if (parent_ci->state == ContentInfo::SELECTED) force_selection = true;
1015 }
1016 if (sel_count == 0) {
1017 /* Nothing depends on us */
1018 this->Unselect(c->id);
1019 continue;
1020 }
1021 /* Something manually selected depends directly on us */
1022 if (force_selection) continue;
1023
1024 /* "Flood" search to find all items in the dependency graph*/
1025 parents.clear();
1026 this->ReverseLookupTreeDependency(parents, c);
1027
1028 /* Is there anything that is "force" selected?, if so... we're done. */
1029 for (const ContentInfo *parent_ci : parents) {
1030 if (parent_ci->state != ContentInfo::SELECTED) continue;
1031
1032 force_selection = true;
1033 break;
1034 }
1035
1036 /* So something depended directly on us */
1037 if (force_selection) continue;
1038
1039 /* Nothing depends on us, mark the whole graph as unselected.
1040 * After that's done run over them once again to test their children
1041 * to unselect. Don't do it immediately because it'll do exactly what
1042 * we're doing now. */
1043 for (const ContentInfo *c : parents) {
1044 if (c->state == ContentInfo::AUTOSELECTED) this->Unselect(c->id);
1045 }
1046 for (const ContentInfo *c : parents) {
1047 this->CheckDependencyState(this->GetContent(c->id));
1048 }
1049 }
1050 }
1051
1053 void ClientNetworkContentSocketHandler::Clear()
1054 {
1055 for (ContentInfo *c : this->infos) delete c;
1056
1057 this->infos.clear();
1058 this->requested.clear();
1059 }
1060
1061 /*** CALLBACK ***/
1062
1063 void ClientNetworkContentSocketHandler::OnConnect(bool success)
1064 {
1065 for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
1066 ContentCallback *cb = this->callbacks[i];
1067 /* the callback may remove itself from this->callbacks */
1068 cb->OnConnect(success);
1069 if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
1070 }
1071 }
1072
1073 void ClientNetworkContentSocketHandler::OnDisconnect()
1074 {
1075 for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
1076 ContentCallback *cb = this->callbacks[i];
1077 cb->OnDisconnect();
1078 if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
1079 }
1080 }
1081
1082 void ClientNetworkContentSocketHandler::OnReceiveContentInfo(const ContentInfo *ci)
1083 {
1084 for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
1085 ContentCallback *cb = this->callbacks[i];
1086 /* the callback may add items and/or remove itself from this->callbacks */
1087 cb->OnReceiveContentInfo(ci);
1088 if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
1089 }
1090 }
1091
1092 void ClientNetworkContentSocketHandler::OnDownloadProgress(const ContentInfo *ci, int bytes)
1093 {
1094 for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
1095 ContentCallback *cb = this->callbacks[i];
1096 cb->OnDownloadProgress(ci, bytes);
1097 if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
1098 }
1099 }
1100
1101 void ClientNetworkContentSocketHandler::OnDownloadComplete(ContentID cid)
1102 {
1103 ContentInfo *ci = this->GetContent(cid);
1104 if (ci != nullptr) {
1105 ci->state = ContentInfo::ALREADY_HERE;
1106 }
1107
1108 for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
1109 ContentCallback *cb = this->callbacks[i];
1110 /* the callback may remove itself from this->callbacks */
1111 cb->OnDownloadComplete(cid);
1112 if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
1113 }
1114 }
ContentInfo::IsSelected
bool IsSelected() const
Is the state either selected or autoselected?
Definition: tcp_content.cpp:27
network_content.h
NetworkContentConnecter::NetworkContentConnecter
NetworkContentConnecter(const std::string &connection_string)
Initiate the connecting.
Definition: network_content.cpp:751
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:1063
ContentInfo::name
std::string name
Name of the content.
Definition: tcp_content_type.h:65
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:683
ContentInfo::type
ContentType type
Type of content.
Definition: tcp_content_type.h:61
NetworkContentSocketHandler
Base socket handler for all Content TCP sockets.
Definition: tcp_content.h:22
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:924
SVS_ALLOW_NEWLINE
@ SVS_ALLOW_NEWLINE
Allow newlines.
Definition: string_type.h:51
TCPConnecter::connection_string
std::string connection_string
Current address we are connecting to (before resolving).
Definition: tcp.h:100
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:799
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:736
NetworkContentMirrorConnectionString
const char * NetworkContentMirrorConnectionString()
Get the connection string for the content mirror from the environment variable OTTD_CONTENT_MIRROR_CS...
Definition: config.cpp:66
CONTENT_TYPE_GAME_LIBRARY
@ CONTENT_TYPE_GAME_LIBRARY
The content consists of a GS library.
Definition: tcp_content_type.h:27
ContentInfo::upgrade
bool upgrade
This item is an upgrade.
Definition: tcp_content_type.h:74
ContentInfo::DOES_NOT_EXIST
@ DOES_NOT_EXIST
The content does not exist in the content system.
Definition: tcp_content_type.h:57
TCPConnecter
"Helper" class for creating TCP connections in a non-blocking manner
Definition: tcp.h:71
HasScenario
bool HasScenario(const ContentInfo *ci, bool md5sum)
Check whether we've got a given scenario based on its unique ID.
Definition: fios.cpp:737
BASESET_DIR
@ BASESET_DIR
Subdirectory for all base data (base sets, intro game)
Definition: fileio_type.h:116
ClientNetworkContentSocketHandler::lastActivity
std::chrono::steady_clock::time_point lastActivity
The last time there was network activity.
Definition: network_content.h:77
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_NEWGRF
@ CONTENT_TYPE_NEWGRF
The content consists of a NewGRF.
Definition: tcp_content_type.h:19
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:39
NetworkContentSocketHandler::ReceivePackets
bool ReceivePackets()
Receive a packet at TCP level.
Definition: tcp_content.cpp:129
ContentInfo::url
std::string url
URL related to the content.
Definition: tcp_content_type.h:67
ClientNetworkContentSocketHandler::CheckDependencyState
void CheckDependencyState(ContentInfo *ci)
Check the dependencies (recursively) of this content info.
Definition: network_content.cpp:965
NETWORK_CONTENT_VERSION_LENGTH
static const uint NETWORK_CONTENT_VERSION_LENGTH
The maximum length of a content's version, in bytes including '\0'.
Definition: config.h:69
ClientNetworkContentSocketHandler
Socket handler for the content server connection.
Definition: network_content.h:65
Packet::TransferOut
ssize_t TransferOut(F transfer_function, D destination, Args &&... args)
Transfer data from the packet to the given function.
Definition: packet.h:141
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:52
ContentCallback::OnDownloadComplete
virtual void OnDownloadComplete(ContentID cid)
We have finished downloading a file.
Definition: network_content.h:56
CONTENT_TYPE_END
@ CONTENT_TYPE_END
Helper to mark the end of the types.
Definition: tcp_content_type.h:28
ContentInfo::version
std::string version
Version of the content.
Definition: tcp_content_type.h:66
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_type.h:37
ClientNetworkContentSocketHandler::Clear
void Clear()
Clear all downloaded content information.
Definition: network_content.cpp:1053
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:129
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:44
ContentType
ContentType
The values in the enum are important; they are used as database 'keys'.
Definition: tcp_content_type.h:16
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:326
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:383
GetContentInfoSubDir
Subdirectory GetContentInfoSubDir(ContentType type)
Helper to get the subdirectory a ContentInfo is located in.
Definition: tcp_content.cpp:187
Packet::Send_uint32
void Send_uint32(uint32 data)
Package a 32 bits integer in the packet.
Definition: packet.cpp:150
NetworkSettings::no_http_content_downloads
bool no_http_content_downloads
do not do content downloads over HTTP
Definition: settings_type.h:298
Packet::Recv_string
std::string Recv_string(size_t length, StringValidationSettings settings=SVS_REPLACE_WITH_QUESTION_MARK)
Reads characters (bytes) from the packet until it finds a '\0', or reaches a maximum of length charac...
Definition: packet.cpp:380
ClientNetworkContentSocketHandler::RequestContentList
void RequestContentList(ContentType type)
Request the content list for the given type.
Definition: network_content.cpp:184
FGCM_ANY
@ FGCM_ANY
Use first found.
Definition: newgrf_config.h:203
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:386
CONTENT_TYPE_GAME
@ CONTENT_TYPE_GAME
The content consists of a game script.
Definition: tcp_content_type.h:26
ClientNetworkContentSocketHandler::OnDisconnect
void OnDisconnect() override
Callback for when the connection got disconnected.
Definition: network_content.cpp:1073
ContentInfo::UNSELECTED
@ UNSELECTED
The content has not been selected.
Definition: tcp_content_type.h:53
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_type.h:35
Packet::Recv_uint32
uint32 Recv_uint32()
Read a 32 bits integer from the packet.
Definition: packet.cpp:335
NetworkContentConnecter::OnFailure
void OnFailure() override
Callback for when the connection attempt failed.
Definition: network_content.cpp:753
NetworkSocketHandler::Reopen
void Reopen()
Reopen the socket so we can send/receive stuff again.
Definition: core.h:73
NETWORK_CONTENT_DESC_LENGTH
static const uint NETWORK_CONTENT_DESC_LENGTH
The maximum length of a content's description, in bytes including '\0'.
Definition: config.h:71
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:822
ClientNetworkContentSocketHandler::~ClientNetworkContentSocketHandler
~ClientNetworkContentSocketHandler()
Clear up the mess ;)
Definition: network_content.cpp:736
ClientNetworkContentSocketHandler::UnselectAll
void UnselectAll()
Unselect everything that we've not downloaded so far.
Definition: network_content.cpp:894
TransferOutFWrite
static ssize_t TransferOutFWrite(FILE *file, const char *buffer, size_t amount)
Simple wrapper around fwrite to be able to pass it to Packet's TransferOut.
Definition: network_content.cpp:473
ContentInfo
Container for all important information about a piece of content.
Definition: tcp_content_type.h:50
NETWORK_CONTENT_MIRROR_URL
static const char *const NETWORK_CONTENT_MIRROR_URL
URL of the HTTP mirror system.
Definition: config.h:21
ContentInfo::md5sum
byte md5sum[16]
The MD5 checksum.
Definition: tcp_content_type.h:70
ClientNetworkContentSocketHandler::OnReceiveData
void OnReceiveData(const char *data, size_t length) override
We're receiving data.
Definition: network_content.cpp:597
ClientNetworkContentSocketHandler::Unselect
void Unselect(ContentID cid)
Unselect a specific content id.
Definition: network_content.cpp:862
AI::HasAI
static bool HasAI(const struct ContentInfo *ci, bool md5sum)
Wrapper function for AIScanner::HasAI.
Definition: ai_core.cpp:367
ClientNetworkContentSocketHandler::OnFailure
void OnFailure() override
An error has occurred and the connection has been closed.
Definition: network_content.cpp:577
Packet::Send_uint16
void Send_uint16(uint16 data)
Package a 16 bits integer in the packet.
Definition: packet.cpp:139
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:81
NetworkContentServerConnectionString
const char * NetworkContentServerConnectionString()
Get the connection string for the content server from the environment variable OTTD_CONTENT_SERVER_CS...
Definition: config.cpp:56
NetworkTCPSocketHandler::CloseConnection
virtual NetworkRecvStatus CloseConnection(bool error=true)
This will put this socket handler in a close state.
Definition: tcp.cpp:65
ContentInfo::SELECTED
@ SELECTED
The content has been manually selected.
Definition: tcp_content_type.h:54
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
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:1101
ClientNetworkContentSocketHandler::OnDownloadProgress
void OnDownloadProgress(const ContentInfo *ci, int bytes) override
We have progress in the download of a file.
Definition: network_content.cpp:1092
TCP_MTU
static const uint16 TCP_MTU
Number of bytes we can pack in a single TCP packet.
Definition: config.h:47
PACKET_CONTENT_CLIENT_CONTENT
@ PACKET_CONTENT_CLIENT_CONTENT
Request a content file given an internal ID.
Definition: tcp_content_type.h:39
ContentInfo::tags
StringList tags
Tags associated with the content.
Definition: tcp_content_type.h:72
Packet
Internal entity of a packet.
Definition: packet.h:44
ClientNetworkContentSocketHandler::DownloadSelectedContent
void DownloadSelectedContent(uint &files, uint &bytes, bool fallback=false)
Actually begin downloading the content we selected.
Definition: network_content.cpp:291
BSWAP32
static uint32 BSWAP32(uint32 x)
Perform a 32 bits endianness bitswap on x.
Definition: bitmath_func.hpp:390
ClientNetworkContentSocketHandler::http_response
std::vector< char > http_response
The HTTP response to the requests we've been doing.
Definition: network_content.h:71
ContentInfo::dependencies
std::vector< ContentID > dependencies
The dependencies (unique server side ids)
Definition: tcp_content_type.h:71
NETWORK_CONTENT_SERVER_PORT
static const uint16 NETWORK_CONTENT_SERVER_PORT
The default port of the content server (TCP)
Definition: config.h:26
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:458
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:192
NETWORK_CONTENT_FILENAME_LENGTH
static const uint NETWORK_CONTENT_FILENAME_LENGTH
The maximum length of a content's filename, in bytes including '\0'.
Definition: config.h:67
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_type.h:36
CONTENT_TYPE_AI
@ CONTENT_TYPE_AI
The content consists of an AI.
Definition: tcp_content_type.h:20
_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
CONTENT_TYPE_BASE_GRAPHICS
@ CONTENT_TYPE_BASE_GRAPHICS
The content consists of base graphics.
Definition: tcp_content_type.h:18
NetworkTCPSocketHandler::SendPackets
SendPacketsState SendPackets(bool closing_down=false)
Sends all the buffered packets out for this client.
Definition: tcp.cpp:99
NetworkTCPSocketHandler::CanSendReceive
bool CanSendReceive()
Check whether this socket can send or receive something.
Definition: tcp.cpp:218
ContentInfo::ALREADY_HERE
@ ALREADY_HERE
The content is already at the client side.
Definition: tcp_content_type.h:56
PacketSize
uint16 PacketSize
Size of the whole packet.
Definition: packet.h:22
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:943
ClientNetworkContentSocketHandler::Connect
void Connect()
Connect with the content server.
Definition: network_content.cpp:773
ContentInfo::AUTOSELECTED
@ AUTOSELECTED
The content has been selected as dependency.
Definition: tcp_content_type.h:55
CONTENT_TYPE_AI_LIBRARY
@ CONTENT_TYPE_AI_LIBRARY
The content consists of an AI library.
Definition: tcp_content_type.h:21
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:902
NetworkRecvStatus
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition: core.h:22
WC_NETWORK_STATUS_WINDOW
@ WC_NETWORK_STATUS_WINDOW
Network status window; Window numbers:
Definition: window_type.h:477
ContentID
ContentID
Unique identifier for the content.
Definition: tcp_content_type.h:45
ContentInfo::filename
std::string filename
Filename (for the .tar.gz; only valid on download)
Definition: tcp_content_type.h:64
ClientNetworkContentSocketHandler::callbacks
std::vector< ContentCallback * > callbacks
Callbacks to notify "the world".
Definition: network_content.h:68
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
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:535
ClientNetworkContentSocketHandler::Select
void Select(ContentID cid)
Select a specific content id.
Definition: network_content.cpp:849
GunzipFile
static bool GunzipFile(const ContentInfo *ci)
Gunzip a given file and remove the .gz if successful.
Definition: network_content.cpp:403
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:306
NetworkContentConnecter
Connect to the content server.
Definition: network_content.cpp:745
ClientNetworkContentSocketHandler::DownloadSelectedContentFallback
void DownloadSelectedContentFallback(const ContentIDList &content)
Initiate downloading the content over the fallback protocol.
Definition: network_content.cpp:353
WL_ERROR
@ WL_ERROR
Errors (eg. saving/loading failed)
Definition: error.h:24
error
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:132
ClientNetworkContentSocketHandler::OnReceiveContentInfo
void OnReceiveContentInfo(const ContentInfo *ci) override
We received a content info.
Definition: network_content.cpp:1082
ContentInfo::state
State state
Whether the content info is selected (for download)
Definition: tcp_content_type.h:73
CONTENT_TYPE_BASE_SOUNDS
@ CONTENT_TYPE_BASE_SOUNDS
The content consists of base sounds.
Definition: tcp_content_type.h:24
ContentInfo::unique_id
uint32 unique_id
Unique ID; either GRF ID or shortname.
Definition: tcp_content_type.h:69
TarScanner
Helper for scanning for files with tar as extension.
Definition: fileio_func.h:61
ClientSettings::network
NetworkSettings network
settings related to the network
Definition: settings_type.h:594
ClientNetworkContentSocketHandler::SelectAll
void SelectAll()
Select everything we can select.
Definition: network_content.cpp:872
CloseWindowById
void CloseWindowById(WindowClass cls, WindowNumber number, bool force)
Close a window by its class and window number (if it is open).
Definition: window.cpp:1176
NETWORK_CONTENT_NAME_LENGTH
static const uint NETWORK_CONTENT_NAME_LENGTH
The maximum length of a content's name, in bytes including '\0'.
Definition: config.h:68
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_type.h:34
NETWORK_RECV_STATUS_OKAY
@ NETWORK_RECV_STATUS_OKAY
Everything is okay.
Definition: core.h:23
CONTENT_TYPE_SCENARIO
@ CONTENT_TYPE_SCENARIO
The content consists of a scenario.
Definition: tcp_content_type.h:22
ClientNetworkContentSocketHandler::BeforeDownload
bool BeforeDownload()
Handle the opening of the file before downloading.
Definition: network_content.cpp:518
FGCM_EXACT
@ FGCM_EXACT
Only find Grfs matching md5sum.
Definition: newgrf_config.h:199
NETWORK_CONTENT_TAG_LENGTH
static const uint NETWORK_CONTENT_TAG_LENGTH
The maximum length of a content's tag, in bytes including '\0'.
Definition: config.h:72
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::CloseConnection
NetworkRecvStatus CloseConnection(bool error=true) override
Disconnect from the content server.
Definition: network_content.cpp:783
NETWORK_CONTENT_URL_LENGTH
static const uint NETWORK_CONTENT_URL_LENGTH
The maximum length of a content's url, in bytes including '\0'.
Definition: config.h:70
ClientNetworkContentSocketHandler::SelectUpgrade
void SelectUpgrade()
Select everything that's an update for something we've got.
Definition: network_content.cpp:883
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: string.cpp:112
ContentInfo::id
ContentID id
Unique (server side) ID for the content.
Definition: tcp_content_type.h:62
Packet::RemainingBytesToTransfer
size_t RemainingBytesToTransfer() const
Get the amount of bytes that are still available for the Transfer functions.
Definition: packet.cpp:402
ClientNetworkContentSocketHandler::AfterDownload
void AfterDownload()
Handle the closing and extracting of a file after downloading it has been done.
Definition: network_content.cpp:543
NetworkContentConnecter::OnConnect
void OnConnect(SOCKET s) override
Callback when the connection succeeded.
Definition: network_content.cpp:759
ContentInfo::description
std::string description
Description of the content.
Definition: tcp_content_type.h:68
CONTENT_TYPE_HEIGHTMAP
@ CONTENT_TYPE_HEIGHTMAP
The content consists of a heightmap.
Definition: tcp_content_type.h:23
ClientNetworkContentSocketHandler::ClientNetworkContentSocketHandler
ClientNetworkContentSocketHandler()
Create a socket handler to handle the connection.
Definition: network_content.cpp:725
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:394
ClientNetworkContentSocketHandler::GetContent
ContentInfo * GetContent(ContentID cid)
Get the content info based on a ContentID.
Definition: network_content.cpp:836
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:478
WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD
@ WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD
Network content download status.
Definition: window_type.h:32
NetworkTCPSocketHandler::CloseSocket
void CloseSocket()
Close the actual socket of the connection.
Definition: tcp.cpp:53
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_type.h:63
ContentCallback::OnDownloadProgress
virtual void OnDownloadProgress(const ContentInfo *ci, int bytes)
We have progress in the download of a file.
Definition: network_content.h:50
CONTENT_TYPE_BASE_MUSIC
@ CONTENT_TYPE_BASE_MUSIC
The content consists of base music.
Definition: tcp_content_type.h:25