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

OpenTTD Source 15.0-RC2

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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 "../fileio_func.h"
17#include "../base_media_base.h"
18#include "../base_media_graphics.h"
19#include "../base_media_music.h"
20#include "../base_media_sounds.h"
21#include "../settings_type.h"
22#include "../strings_func.h"
23#include "../timer/timer.h"
24#include "../timer/timer_window.h"
25#include "../core/string_consumer.hpp"
26#include "network_content.h"
27
28#include "table/strings.h"
29
30#if defined(WITH_ZLIB)
31# include <zlib.h>
32# if defined(_WIN32)
33 /* Required for: dup, fileno, close */
34# include <io.h>
35# endif
36#endif
37
38#ifdef __EMSCRIPTEN__
39# include <emscripten.h>
40#endif
41
42#include "../safeguards.h"
43
44extern bool HasScenario(const ContentInfo &ci, bool md5sum);
45
47 ClientNetworkContentSocketHandler _network_content_client;
48
50 static bool HasGRFConfig(const ContentInfo &ci, bool md5sum)
51{
52 return FindGRFConfig(std::byteswap(ci.unique_id), md5sum ? FGCM_EXACT : FGCM_ANY, md5sum ? &ci.md5sum : nullptr) != nullptr;
53}
54
62 using HasContentProc = bool(const ContentInfo &ci, bool md5sum);
63
69 static HasContentProc *GetHasContentProcforContentType(ContentType type)
70{
71 switch (type) {
72 case CONTENT_TYPE_NEWGRF: return HasGRFConfig;
73 case CONTENT_TYPE_BASE_GRAPHICS: return BaseGraphics::HasSet;
74 case CONTENT_TYPE_BASE_MUSIC: return BaseMusic::HasSet;
75 case CONTENT_TYPE_BASE_SOUNDS: return BaseSounds::HasSet;
76 case CONTENT_TYPE_AI: return AI::HasAI;
77 case CONTENT_TYPE_AI_LIBRARY: return AI::HasAILibrary;
78 case CONTENT_TYPE_GAME: return Game::HasGame;
79 case CONTENT_TYPE_GAME_LIBRARY: return Game::HasGameLibrary;
80 case CONTENT_TYPE_SCENARIO: return HasScenario;
81 case CONTENT_TYPE_HEIGHTMAP: return HasScenario;
82 default: return nullptr;
83 }
84}
85
86 bool ClientNetworkContentSocketHandler::Receive_SERVER_INFO(Packet &p)
87{
88 auto ci = std::make_unique<ContentInfo>();
89 ci->type = (ContentType)p.Recv_uint8();
90 ci->id = (ContentID)p.Recv_uint32();
91 ci->filesize = p.Recv_uint32();
92
93 ci->name = p.Recv_string(NETWORK_CONTENT_NAME_LENGTH);
94 ci->version = p.Recv_string(NETWORK_CONTENT_VERSION_LENGTH);
95 ci->url = p.Recv_string(NETWORK_CONTENT_URL_LENGTH);
96 ci->description = p.Recv_string(NETWORK_CONTENT_DESC_LENGTH, {StringValidationSetting::ReplaceWithQuestionMark, StringValidationSetting::AllowNewline});
97
98 ci->unique_id = p.Recv_uint32();
99 p.Recv_bytes(ci->md5sum);
100
101 uint dependency_count = p.Recv_uint8();
102 ci->dependencies.reserve(dependency_count);
103 for (uint i = 0; i < dependency_count; i++) {
104 ContentID dependency_cid = (ContentID)p.Recv_uint32();
105 ci->dependencies.push_back(dependency_cid);
106 this->reverse_dependency_map.emplace(dependency_cid, ci->id);
107 }
108
109 uint tag_count = p.Recv_uint8();
110 ci->tags.reserve(tag_count);
111 for (uint i = 0; i < tag_count; i++) ci->tags.push_back(p.Recv_string(NETWORK_CONTENT_TAG_LENGTH));
112
113 if (!ci->IsValid()) {
114 this->CloseConnection();
115 return false;
116 }
117
118 HasContentProc *proc = GetHasContentProcforContentType(ci->type);
119 if (proc != nullptr) {
120 if (proc(*ci, true)) {
121 ci->state = ContentInfo::State::AlreadyHere;
122 } else {
123 ci->state = ContentInfo::State::Unselected;
124 if (proc(*ci, false)) ci->upgrade = true;
125 }
126 } else {
127 ci->state = ContentInfo::State::Unselected;
128 }
129
130 /* Something we don't have and has filesize 0 does not exist in the system */
131 if (ci->state == ContentInfo::State::Unselected && ci->filesize == 0) ci->state = ContentInfo::State::DoesNotExist;
132
133 /* Do we already have a stub for this? */
134 for (const auto &ici : this->infos) {
135 if (ici->type == ci->type && ici->unique_id == ci->unique_id && ci->md5sum == ici->md5sum) {
136 /* Preserve the name if possible */
137 if (ci->name.empty()) ci->name = ici->name;
138 if (ici->IsSelected()) ci->state = ici->state;
139
140 /*
141 * As ici might be selected by the content window we cannot delete that.
142 * However, we want to keep most of the values of ci, except the values
143 * we (just) already preserved.
144 */
145 *ici = *ci;
146
147 this->OnReceiveContentInfo(*ici);
148 return true;
149 }
150 }
151
152 /* Missing content info? Don't list it */
153 if (ci->filesize == 0) {
154 return true;
155 }
156
157 ContentInfo *info = this->infos.emplace_back(std::move(ci)).get();
158
159 /* Incoming data means that we might need to reconsider dependencies */
160 ConstContentVector parents;
161 this->ReverseLookupTreeDependency(parents, info);
162 for (const ContentInfo *ici : parents) {
163 this->CheckDependencyState(*ici);
164 }
165
166 this->OnReceiveContentInfo(*info);
167
168 return true;
169}
170
175 void ClientNetworkContentSocketHandler::RequestContentList(ContentType type)
176{
177 if (type == CONTENT_TYPE_END) {
178 this->RequestContentList(CONTENT_TYPE_BASE_GRAPHICS);
179 this->RequestContentList(CONTENT_TYPE_BASE_MUSIC);
180 this->RequestContentList(CONTENT_TYPE_BASE_SOUNDS);
181 this->RequestContentList(CONTENT_TYPE_SCENARIO);
182 this->RequestContentList(CONTENT_TYPE_HEIGHTMAP);
183 this->RequestContentList(CONTENT_TYPE_AI);
184 this->RequestContentList(CONTENT_TYPE_AI_LIBRARY);
185 this->RequestContentList(CONTENT_TYPE_GAME);
186 this->RequestContentList(CONTENT_TYPE_GAME_LIBRARY);
187 this->RequestContentList(CONTENT_TYPE_NEWGRF);
188 return;
189 }
190
191 this->Connect();
192
193 auto p = std::make_unique<Packet>(this, PACKET_CONTENT_CLIENT_INFO_LIST);
194 p->Send_uint8 ((uint8_t)type);
195 p->Send_uint32(0xffffffff);
196 p->Send_uint8 (1);
197 p->Send_string("vanilla");
198 p->Send_string(_openttd_content_version);
199
200 /* Patchpacks can extend the list with one. In BaNaNaS metadata you can
201 * add a branch in the 'compatibility' list, to filter on this. If you want
202 * your patchpack to be mentioned in the BaNaNaS web-interface, create an
203 * issue on https://github.com/OpenTTD/bananas-api asking for this.
204
205 p->Send_string("patchpack"); // Or what-ever the name of your patchpack is.
206 p->Send_string(_openttd_content_version_patchpack);
207
208 */
209
210 this->SendPacket(std::move(p));
211}
212
218 void ClientNetworkContentSocketHandler::RequestContentList(std::span<const ContentID> content_ids)
219{
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_t for the count in this packet.
223 * The rest of the packet can be used for the IDs. */
224 static constexpr size_t MAX_CONTENT_IDS_PER_PACKET = (TCP_MTU - sizeof(PacketSize) - sizeof(uint8_t) - sizeof(uint16_t)) / sizeof(uint32_t);
225
226 if (content_ids.empty()) return;
227
228 this->Connect();
229
230 for (auto it = std::begin(content_ids); it != std::end(content_ids); /* nothing */) {
231 auto last = std::ranges::next(it, MAX_CONTENT_IDS_PER_PACKET, std::end(content_ids));
232
233 auto p = std::make_unique<Packet>(this, PACKET_CONTENT_CLIENT_INFO_ID, TCP_MTU);
234 p->Send_uint16(std::distance(it, last));
235
236 for (; it != last; ++it) {
237 p->Send_uint32(*it);
238 }
239
240 this->SendPacket(std::move(p));
241 }
242}
243
249 void ClientNetworkContentSocketHandler::RequestContentList(ContentVector *cv, bool send_md5sum)
250{
251 if (cv == nullptr) return;
252
253 this->Connect();
254
255 assert(cv->size() < 255);
256 assert(cv->size() < (TCP_MTU - sizeof(PacketSize) - sizeof(uint8_t) - sizeof(uint8_t)) /
257 (sizeof(uint8_t) + sizeof(uint32_t) + (send_md5sum ? MD5_HASH_BYTES : 0)));
258
259 auto p = std::make_unique<Packet>(this, send_md5sum ? PACKET_CONTENT_CLIENT_INFO_EXTID_MD5 : PACKET_CONTENT_CLIENT_INFO_EXTID, TCP_MTU);
260 p->Send_uint8((uint8_t)cv->size());
261
262 for (const auto &ci : *cv) {
263 p->Send_uint8((uint8_t)ci->type);
264 p->Send_uint32(ci->unique_id);
265 if (!send_md5sum) continue;
266 p->Send_bytes(ci->md5sum);
267 }
268
269 this->SendPacket(std::move(p));
270
271 for (auto &ci : *cv) {
272 bool found = false;
273 for (const auto &ci2 : this->infos) {
274 if (ci->type == ci2->type && ci->unique_id == ci2->unique_id &&
275 (!send_md5sum || ci->md5sum == ci2->md5sum)) {
276 found = true;
277 break;
278 }
279 }
280 if (!found) {
281 this->infos.push_back(std::move(ci));
282 }
283 }
284}
285
292 void ClientNetworkContentSocketHandler::DownloadSelectedContent(uint &files, uint &bytes, bool fallback)
293{
294 bytes = 0;
295
296 ContentIDList content;
297 for (const auto &ci : this->infos) {
298 if (!ci->IsSelected() || ci->state == ContentInfo::State::AlreadyHere) continue;
299
300 content.push_back(ci->id);
301 bytes += ci->filesize;
302 }
303
304 files = (uint)content.size();
305
306 /* If there's nothing to download, do nothing. */
307 if (files == 0) return;
308
309 this->is_cancelled = false;
310
311 if (fallback) {
312 this->DownloadSelectedContentFallback(content);
313 } else {
314 this->DownloadSelectedContentHTTP(content);
315 }
316}
317
322 void ClientNetworkContentSocketHandler::DownloadSelectedContentHTTP(const ContentIDList &content)
323{
324 std::string content_request;
325 for (const ContentID &id : content) {
326 format_append(content_request, "{}\n", id);
327 }
328
329 this->http_response_index = -1;
330
331 NetworkHTTPSocketHandler::Connect(NetworkContentMirrorUriString(), this, std::move(content_request));
332}
333
338 void ClientNetworkContentSocketHandler::DownloadSelectedContentFallback(const ContentIDList &content)
339{
340 uint count = (uint)content.size();
341 const ContentID *content_ids = content.data();
342 this->Connect();
343
344 while (count > 0) {
345 /* We can "only" send a limited number of IDs in a single packet.
346 * A packet begins with the packet size and a byte for the type.
347 * Then this packet adds a uint16_t for the count in this packet.
348 * The rest of the packet can be used for the IDs. */
349 uint p_count = std::min<uint>(count, (TCP_MTU - sizeof(PacketSize) - sizeof(uint8_t) - sizeof(uint16_t)) / sizeof(uint32_t));
350
351 auto p = std::make_unique<Packet>(this, PACKET_CONTENT_CLIENT_CONTENT, TCP_MTU);
352 p->Send_uint16(p_count);
353
354 for (uint i = 0; i < p_count; i++) {
355 p->Send_uint32(content_ids[i]);
356 }
357
358 this->SendPacket(std::move(p));
359 count -= p_count;
360 content_ids += p_count;
361 }
362}
363
371 static std::string GetFullFilename(const ContentInfo &ci, bool compressed)
372{
373 Subdirectory dir = GetContentInfoSubDir(ci.type);
374 if (dir == NO_DIRECTORY) return {};
375
376 std::string buf = FioGetDirectory(SP_AUTODOWNLOAD_DIR, dir);
377 buf += ci.filename;
378 buf += compressed ? ".tar.gz" : ".tar";
379
380 return buf;
381}
382
388 static bool GunzipFile(const ContentInfo &ci)
389{
390#if defined(WITH_ZLIB)
391 bool ret = true;
392
393 /* Need to open the file with fopen() to support non-ASCII on Windows. */
394 auto ftmp = FileHandle::Open(GetFullFilename(ci, true), "rb");
395 if (!ftmp.has_value()) return false;
396 /* Duplicate the handle, and close the FILE*, to avoid double-closing the handle later. */
397 int fdup = dup(fileno(*ftmp));
398 gzFile fin = gzdopen(fdup, "rb");
399 ftmp.reset();
400
401 auto fout = FileHandle::Open(GetFullFilename(ci, false), "wb");
402
403 if (fin == nullptr || !fout.has_value()) {
404 ret = false;
405 } else {
406 uint8_t buff[8192];
407 for (;;) {
408 int read = gzread(fin, buff, sizeof(buff));
409 if (read == 0) {
410 /* If gzread() returns 0, either the end-of-file has been
411 * reached or an underlying read error has occurred.
412 *
413 * gzeof() can't be used, because:
414 * 1.2.5 - it is safe, 1 means 'everything was OK'
415 * 1.2.3.5, 1.2.4 - 0 or 1 is returned 'randomly'
416 * 1.2.3.3 - 1 is returned for truncated archive
417 *
418 * So we use gzerror(). When proper end of archive
419 * has been reached, then:
420 * errnum == Z_STREAM_END in 1.2.3.3,
421 * errnum == 0 in 1.2.4 and 1.2.5 */
422 int errnum;
423 gzerror(fin, &errnum);
424 if (errnum != 0 && errnum != Z_STREAM_END) ret = false;
425 break;
426 }
427 if (read < 0 || static_cast<size_t>(read) != fwrite(buff, 1, read, *fout)) {
428 /* If gzread() returns -1, there was an error in archive */
429 ret = false;
430 break;
431 }
432 /* DO NOT DO THIS! It will fail to detect broken archive with 1.2.3.3!
433 * if (read < sizeof(buff)) break; */
434 }
435 }
436
437 if (fin != nullptr) {
438 gzclose(fin);
439 } else if (fdup != -1) {
440 /* Failing gzdopen does not close the passed file descriptor. */
441 close(fdup);
442 }
443
444 return ret;
445#else
446 NOT_REACHED();
447#endif /* defined(WITH_ZLIB) */
448}
449
450 bool ClientNetworkContentSocketHandler::Receive_SERVER_CONTENT(Packet &p)
451{
452 if (!this->cur_file.has_value()) {
453 /* When we haven't opened a file this must be our first packet with metadata. */
454 this->cur_info = std::make_unique<ContentInfo>();
455 this->cur_info->type = (ContentType)p.Recv_uint8();
456 this->cur_info->id = (ContentID)p.Recv_uint32();
457 this->cur_info->filesize = p.Recv_uint32();
458 this->cur_info->filename = p.Recv_string(NETWORK_CONTENT_FILENAME_LENGTH);
459
460 if (!this->BeforeDownload()) {
461 this->CloseConnection();
462 return false;
463 }
464 } else {
465 /* We have a file opened, thus are downloading internal content */
466 ssize_t to_read = p.RemainingBytesToTransfer();
467 auto write_to_disk = [this](std::span<const uint8_t> buffer) {
468 return fwrite(buffer.data(), 1, buffer.size(), *this->cur_file);
469 };
470 if (to_read != 0 && p.TransferOut(write_to_disk) != to_read) {
471 CloseWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD);
472 ShowErrorMessage(
473 GetEncodedString(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD),
474 GetEncodedString(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE),
475 WL_ERROR);
476 this->CloseConnection();
477 this->cur_file.reset();
478
479 return false;
480 }
481
482 this->OnDownloadProgress(*this->cur_info, (int)to_read);
483
484 if (to_read == 0) this->AfterDownload();
485 }
486
487 return true;
488}
489
494 bool ClientNetworkContentSocketHandler::BeforeDownload()
495{
496 if (!this->cur_info->IsValid()) {
497 this->cur_info.reset();
498 return false;
499 }
500
501 if (this->cur_info->filesize != 0) {
502 /* The filesize is > 0, so we are going to download it */
503 std::string filename = GetFullFilename(*this->cur_info, true);
504 if (filename.empty() || !(this->cur_file = FileHandle::Open(filename, "wb")).has_value()) {
505 /* Unless that fails of course... */
506 CloseWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD);
507 ShowErrorMessage(
508 GetEncodedString(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD),
509 GetEncodedString(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE),
510 WL_ERROR);
511 return false;
512 }
513 }
514 return true;
515}
516
521 void ClientNetworkContentSocketHandler::AfterDownload()
522{
523 /* We read nothing; that's our marker for end-of-stream.
524 * Now gunzip the tar and make it known. */
525 this->cur_file.reset();
526
527 if (GunzipFile(*this->cur_info)) {
528 FioRemove(GetFullFilename(*this->cur_info, true));
529
530 Subdirectory sd = GetContentInfoSubDir(this->cur_info->type);
531 if (sd == NO_DIRECTORY) NOT_REACHED();
532
533 TarScanner ts;
534 std::string fname = GetFullFilename(*this->cur_info, false);
535 ts.AddFile(sd, fname);
536
537 if (this->cur_info->type == CONTENT_TYPE_BASE_MUSIC) {
538 /* Music can't be in a tar. So extract the tar! */
539 ExtractTar(fname, BASESET_DIR);
540 FioRemove(fname);
541 }
542
543#ifdef __EMSCRIPTEN__
544 EM_ASM(if (window["openttd_syncfs"]) openttd_syncfs());
545#endif
546
547 this->OnDownloadComplete(this->cur_info->id);
548 } else {
549 ShowErrorMessage(GetEncodedString(STR_CONTENT_ERROR_COULD_NOT_EXTRACT), {}, WL_ERROR);
550 }
551}
552
553 bool ClientNetworkContentSocketHandler::IsCancelled() const
554{
555 return this->is_cancelled;
556}
557
558/* Also called to just clean up the mess. */
559 void ClientNetworkContentSocketHandler::OnFailure()
560{
561 this->http_response.clear();
562 this->http_response.shrink_to_fit();
563 this->http_response_index = -2;
564
565 if (this->cur_file.has_value()) {
566 this->OnDownloadProgress(*this->cur_info, -1);
567
568 this->cur_file.reset();
569 }
570
571 /* If we fail, download the rest via the 'old' system. */
572 if (!this->is_cancelled) {
573 uint files, bytes;
574
575 this->DownloadSelectedContent(files, bytes, true);
576 }
577}
578
579 void ClientNetworkContentSocketHandler::OnReceiveData(std::unique_ptr<char[]> data, size_t length)
580{
581 assert(data.get() == nullptr || length != 0);
582
583 /* Ignore any latent data coming from a connection we closed. */
584 if (this->http_response_index == -2) {
585 return;
586 }
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.get(), data.get() + 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.get(), 1, length, *this->cur_file) != 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->cur_info, (int)length);
610 }
611
612 /* Nothing more to do now. */
613 return;
614 }
615
616 if (this->cur_file.has_value()) {
617 /* We've finished downloading a file. */
618 this->AfterDownload();
619 }
620
621 if ((uint)this->http_response_index >= this->http_response.size()) {
622 /* It's not a real failure, but if there's
623 * nothing more to download it helps with
624 * cleaning up the stuff we allocated. */
625 this->OnFailure();
626 return;
627 }
628
629 /* When we haven't opened a file this must be our first packet with metadata. */
630 this->cur_info = std::make_unique<ContentInfo>();
631
632 try {
633 for (;;) {
634 std::string_view buffer{this->http_response.data(), this->http_response.size()};
635 buffer.remove_prefix(this->http_response_index);
636 auto len = buffer.find('\n');
637 if (len == std::string_view::npos) throw std::exception{};
638 /* Update the index for the next one */
639 this->http_response_index += static_cast<int>(len + 1);
640
641 StringConsumer consumer{buffer.substr(0, len)};
642
643 /* Read the ID */
644 this->cur_info->id = static_cast<ContentID>(consumer.ReadIntegerBase<uint>(10));
645 if (!consumer.ReadIf(",")) throw std::exception{};
646
647 /* Read the type */
648 this->cur_info->type = static_cast<ContentType>(consumer.ReadIntegerBase<uint>(10));
649 if (!consumer.ReadIf(",")) throw std::exception{};
650
651 /* Read the file size */
652 this->cur_info->filesize = consumer.ReadIntegerBase<uint32_t>(10);
653 if (!consumer.ReadIf(",")) throw std::exception{};
654
655 /* Read the URL */
656 auto url = consumer.GetLeftData();
657
658 /* Is it a fallback URL? If so, just continue with the next one. */
659 if (consumer.ReadIf("ottd")) {
660 /* Have we gone through all lines? */
661 if (static_cast<size_t>(this->http_response_index) >= this->http_response.size()) throw std::exception{};
662 continue;
663 }
664
665 consumer.SkipUntilChar('/', StringConsumer::KEEP_SEPARATOR);
666 std::string_view filename;
667 /* Skip all but the last part. There must be at least one / though */
668 do {
669 if (!consumer.ReadIf("/")) throw std::exception{};
670 filename = consumer.ReadUntilChar('/', StringConsumer::KEEP_SEPARATOR);
671 } while (consumer.AnyBytesLeft());
672
673 /* Remove the extension from the string. */
674 for (uint i = 0; i < 2; i++) {
675 auto pos = filename.find_last_of('.');
676 if (pos == std::string::npos) throw std::exception{};
677 filename = filename.substr(0, pos);
678 }
679
680 /* Copy the string, without extension, to the filename. */
681 this->cur_info->filename = filename;
682
683 /* Request the next file. */
684 if (!this->BeforeDownload()) throw std::exception{};
685
686 NetworkHTTPSocketHandler::Connect(url, this);
687 break;
688 }
689 } catch (const std::exception&) {
690 this->OnFailure();
691 }
692}
693
695 class NetworkContentConnecter : public TCPConnecter {
696public:
701 NetworkContentConnecter(std::string_view connection_string) : TCPConnecter(connection_string, NETWORK_CONTENT_SERVER_PORT) {}
702
703 void OnFailure() override
704 {
705 _network_content_client.is_connecting = false;
706 _network_content_client.OnConnect(false);
707 }
708
709 void OnConnect(SOCKET s) override
710 {
711 assert(_network_content_client.sock == INVALID_SOCKET);
712 _network_content_client.last_activity = std::chrono::steady_clock::now();
713 _network_content_client.is_connecting = false;
714 _network_content_client.sock = s;
715 _network_content_client.Reopen();
716 _network_content_client.OnConnect(true);
717 }
718};
719
723 void ClientNetworkContentSocketHandler::Connect()
724{
725 if (this->sock != INVALID_SOCKET || this->is_connecting) return;
726
727 this->is_cancelled = false;
728 this->is_connecting = true;
729
730 TCPConnecter::Create<NetworkContentConnecter>(NetworkContentServerConnectionString());
731}
732
736 NetworkRecvStatus ClientNetworkContentSocketHandler::CloseConnection(bool)
737{
738 NetworkContentSocketHandler::CloseConnection();
739
740 if (this->sock == INVALID_SOCKET) return NETWORK_RECV_STATUS_OKAY;
741
742 this->CloseSocket();
743 this->OnDisconnect();
744
745 return NETWORK_RECV_STATUS_OKAY;
746}
747
751 void ClientNetworkContentSocketHandler::Cancel(void)
752{
753 this->is_cancelled = true;
754 this->CloseConnection();
755}
756
761 void ClientNetworkContentSocketHandler::SendReceive()
762{
763 if (this->sock == INVALID_SOCKET || this->is_connecting) return;
764
765 /* Close the connection to the content server after inactivity; there can still be downloads pending via HTTP. */
766 if (std::chrono::steady_clock::now() > this->last_activity + IDLE_TIMEOUT) {
767 this->CloseConnection();
768 return;
769 }
770
771 if (this->CanSendReceive()) {
772 if (this->ReceivePackets()) {
773 /* Only update activity once a packet is received, instead of every time we try it. */
774 this->last_activity = std::chrono::steady_clock::now();
775 }
776 }
777
778 this->SendPackets();
779}
780
782 static constexpr auto CONTENT_QUEUE_TIMEOUT = std::chrono::milliseconds(100);
783
784static TimeoutTimer<TimerWindow> _request_queue_timeout = {CONTENT_QUEUE_TIMEOUT, []() {
785 _network_content_client.RequestQueuedContentInfo();
786}};
787
792 void ClientNetworkContentSocketHandler::DownloadContentInfo(ContentID cid)
793{
794 /* When we tried to download it already, don't try again */
795 if (std::ranges::find(this->requested, cid) != this->requested.end()) return;
796
797 this->requested.push_back(cid);
798 this->queued.push_back(cid);
799 _request_queue_timeout.Reset();
800}
801
805 void ClientNetworkContentSocketHandler::RequestQueuedContentInfo()
806{
807 if (this->queued.empty()) return;
808
809 /* Wait until we've briefly stopped receiving data (which will contain more content) before making the request. */
810 if (std::chrono::steady_clock::now() <= this->last_activity + CONTENT_QUEUE_TIMEOUT) {
811 _request_queue_timeout.Reset();
812 return;
813 }
814
815 /* Move the queue locally so more ids can be queued for later. */
816 ContentIDList queue;
817 queue.swap(this->queued);
818
819 /* Remove ids that have since been received since the request was queued. */
820 queue.erase(std::remove_if(std::begin(queue), std::end(queue), [this](ContentID content_id) {
821 return std::ranges::find(this->infos, content_id, &ContentInfo::id) != std::end(this->infos);
822 }), std::end(queue));
823
824 this->RequestContentList(queue);
825}
826
832 ContentInfo *ClientNetworkContentSocketHandler::GetContent(ContentID cid) const
833{
834 for (const auto &ci : this->infos) {
835 if (ci->id == cid) return ci.get();
836 }
837 return nullptr;
838}
839
840
845 void ClientNetworkContentSocketHandler::Select(ContentID cid)
846{
847 ContentInfo *ci = this->GetContent(cid);
848 if (ci == nullptr || ci->state != ContentInfo::State::Unselected) return;
849
850 ci->state = ContentInfo::State::Selected;
851 this->CheckDependencyState(*ci);
852}
853
858 void ClientNetworkContentSocketHandler::Unselect(ContentID cid)
859{
860 ContentInfo *ci = this->GetContent(cid);
861 if (ci == nullptr || !ci->IsSelected()) return;
862
863 ci->state = ContentInfo::State::Unselected;
864 this->CheckDependencyState(*ci);
865}
866
868 void ClientNetworkContentSocketHandler::SelectAll()
869{
870 for (const auto &ci : this->infos) {
871 if (ci->state == ContentInfo::State::Unselected) {
872 ci->state = ContentInfo::State::Selected;
873 this->CheckDependencyState(*ci);
874 }
875 }
876}
877
879 void ClientNetworkContentSocketHandler::SelectUpgrade()
880{
881 for (const auto &ci : this->infos) {
882 if (ci->state == ContentInfo::State::Unselected && ci->upgrade) {
883 ci->state = ContentInfo::State::Selected;
884 this->CheckDependencyState(*ci);
885 }
886 }
887}
888
890 void ClientNetworkContentSocketHandler::UnselectAll()
891{
892 for (const auto &ci : this->infos) {
893 if (ci->IsSelected() && ci->state != ContentInfo::State::AlreadyHere) ci->state = ContentInfo::State::Unselected;
894 }
895}
896
898 void ClientNetworkContentSocketHandler::ToggleSelectedState(const ContentInfo &ci)
899{
900 switch (ci.state) {
901 case ContentInfo::State::Selected:
902 case ContentInfo::State::Autoselected:
903 this->Unselect(ci.id);
904 break;
905
906 case ContentInfo::State::Unselected:
907 this->Select(ci.id);
908 break;
909
910 default:
911 break;
912 }
913}
914
920 void ClientNetworkContentSocketHandler::ReverseLookupDependency(ConstContentVector &parents, const ContentInfo &child) const
921{
922 auto range = this->reverse_dependency_map.equal_range(child.id);
923
924 for (auto iter = range.first; iter != range.second; ++iter) {
925 parents.push_back(GetContent(iter->second));
926 }
927}
928
934 void ClientNetworkContentSocketHandler::ReverseLookupTreeDependency(ConstContentVector &tree, const ContentInfo *child) const
935{
936 tree.push_back(child);
937
938 /* First find all direct parents. We can't use the "normal" iterator as
939 * we are including stuff into the vector and as such the vector's data
940 * store can be reallocated (and thus move), which means our iterating
941 * pointer gets invalid. So fall back to the indices. */
942 for (size_t i = 0; i < tree.size(); i++) {
943 ConstContentVector parents;
944 this->ReverseLookupDependency(parents, *tree[i]);
945
946 for (const ContentInfo *ci : parents) {
947 include(tree, ci);
948 }
949 }
950}
951
956 void ClientNetworkContentSocketHandler::CheckDependencyState(const ContentInfo &ci)
957{
958 if (ci.IsSelected() || ci.state == ContentInfo::State::AlreadyHere) {
959 /* Selection is easy; just walk all children and set the
960 * autoselected state. That way we can see what we automatically
961 * selected and thus can unselect when a dependency is removed. */
962 for (auto &dependency : ci.dependencies) {
963 ContentInfo *c = this->GetContent(dependency);
964 if (c == nullptr) {
965 this->DownloadContentInfo(dependency);
966 } else if (c->state == ContentInfo::State::Unselected) {
967 c->state = ContentInfo::State::Autoselected;
968 this->CheckDependencyState(*c);
969 }
970 }
971 return;
972 }
973
974 if (ci.state != ContentInfo::State::Unselected) return;
975
976 /* For unselection we need to find the parents of us. We need to
977 * unselect them. After that we unselect all children that we
978 * depend on and are not used as dependency for us, but only when
979 * we automatically selected them. */
980 ConstContentVector parents;
981 this->ReverseLookupDependency(parents, ci);
982 for (const ContentInfo *c : parents) {
983 if (!c->IsSelected()) continue;
984
985 this->Unselect(c->id);
986 }
987
988 for (auto &dependency : ci.dependencies) {
989 const ContentInfo *c = this->GetContent(dependency);
990 if (c == nullptr) {
991 DownloadContentInfo(dependency);
992 continue;
993 }
994 if (c->state != ContentInfo::State::Autoselected) continue;
995
996 /* Only unselect when WE are the only parent. */
997 parents.clear();
998 this->ReverseLookupDependency(parents, *c);
999
1000 /* First check whether anything depends on us */
1001 int sel_count = 0;
1002 bool force_selection = false;
1003 for (const ContentInfo *parent_ci : parents) {
1004 if (parent_ci->IsSelected()) sel_count++;
1005 if (parent_ci->state == ContentInfo::State::Selected) force_selection = true;
1006 }
1007 if (sel_count == 0) {
1008 /* Nothing depends on us */
1009 this->Unselect(c->id);
1010 continue;
1011 }
1012 /* Something manually selected depends directly on us */
1013 if (force_selection) continue;
1014
1015 /* "Flood" search to find all items in the dependency graph*/
1016 parents.clear();
1017 this->ReverseLookupTreeDependency(parents, c);
1018
1019 /* Is there anything that is "force" selected?, if so... we're done. */
1020 for (const ContentInfo *parent_ci : parents) {
1021 if (parent_ci->state != ContentInfo::State::Selected) continue;
1022
1023 force_selection = true;
1024 break;
1025 }
1026
1027 /* So something depended directly on us */
1028 if (force_selection) continue;
1029
1030 /* Nothing depends on us, mark the whole graph as unselected.
1031 * After that's done run over them once again to test their children
1032 * to unselect. Don't do it immediately because it'll do exactly what
1033 * we're doing now. */
1034 for (const ContentInfo *parent : parents) {
1035 if (parent->state == ContentInfo::State::Autoselected) this->Unselect(parent->id);
1036 }
1037 for (const ContentInfo *parent : parents) {
1038 this->CheckDependencyState(*this->GetContent(parent->id));
1039 }
1040 }
1041}
1042
1044 void ClientNetworkContentSocketHandler::Clear()
1045{
1046 this->infos.clear();
1047 this->requested.clear();
1048 this->queued.clear();
1049 this->reverse_dependency_map.clear();
1050}
1051
1052/*** CALLBACK ***/
1053
1054 void ClientNetworkContentSocketHandler::OnConnect(bool success)
1055{
1056 for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
1057 ContentCallback *cb = this->callbacks[i];
1058 /* the callback may remove itself from this->callbacks */
1059 cb->OnConnect(success);
1060 if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
1061 }
1062}
1063
1064 void ClientNetworkContentSocketHandler::OnDisconnect()
1065{
1066 for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
1067 ContentCallback *cb = this->callbacks[i];
1068 cb->OnDisconnect();
1069 if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
1070 }
1071}
1072
1073 void ClientNetworkContentSocketHandler::OnReceiveContentInfo(const ContentInfo &ci)
1074{
1075 for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
1076 ContentCallback *cb = this->callbacks[i];
1077 /* the callback may add items and/or remove itself from this->callbacks */
1078 cb->OnReceiveContentInfo(ci);
1079 if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
1080 }
1081}
1082
1083 void ClientNetworkContentSocketHandler::OnDownloadProgress(const ContentInfo &ci, int bytes)
1084{
1085 for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
1086 ContentCallback *cb = this->callbacks[i];
1087 cb->OnDownloadProgress(ci, bytes);
1088 if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
1089 }
1090}
1091
1092 void ClientNetworkContentSocketHandler::OnDownloadComplete(ContentID cid)
1093{
1094 ContentInfo *ci = this->GetContent(cid);
1095 if (ci != nullptr) {
1096 ci->state = ContentInfo::State::AlreadyHere;
1097 }
1098
1099 for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
1100 ContentCallback *cb = this->callbacks[i];
1101 /* the callback may remove itself from this->callbacks */
1102 cb->OnDownloadComplete(cid);
1103 if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
1104 }
1105}
AI::HasAI
static bool HasAI(const ContentInfo &ci, bool md5sum)
Wrapper function for AIScanner::HasAI.
Definition ai_core.cpp:337
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:346
ClientNetworkContentSocketHandler
Socket handler for the content server connection.
Definition network_content.h:63
ClientNetworkContentSocketHandler::SelectUpgrade
void SelectUpgrade()
Select everything that's an update for something we've got.
Definition network_content.cpp:879
ClientNetworkContentSocketHandler::OnConnect
void OnConnect(bool success) override
Callback for when the connection has finished.
Definition network_content.cpp:1054
ClientNetworkContentSocketHandler::DownloadSelectedContentHTTP
void DownloadSelectedContentHTTP(const ContentIDList &content)
Initiate downloading the content over HTTP.
Definition network_content.cpp:322
ClientNetworkContentSocketHandler::DownloadSelectedContent
void DownloadSelectedContent(uint &files, uint &bytes, bool fallback=false)
Actually begin downloading the content we selected.
Definition network_content.cpp:292
ClientNetworkContentSocketHandler::ToggleSelectedState
void ToggleSelectedState(const ContentInfo &ci)
Toggle the state of a content info and check its dependencies.
Definition network_content.cpp:898
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:105
ClientNetworkContentSocketHandler::OnReceiveData
void OnReceiveData(std::unique_ptr< char[]> data, size_t length) override
We're receiving data.
Definition network_content.cpp:579
ClientNetworkContentSocketHandler::Select
void Select(ContentID cid)
Select a specific content id.
Definition network_content.cpp:845
ClientNetworkContentSocketHandler::OnReceiveContentInfo
void OnReceiveContentInfo(const ContentInfo &ci) override
We received a content info.
Definition network_content.cpp:1073
ClientNetworkContentSocketHandler::http_response_index
int http_response_index
Where we are, in the response, with handling it.
Definition network_content.h:72
ClientNetworkContentSocketHandler::CloseConnection
NetworkRecvStatus CloseConnection(bool error=true) override
Disconnect from the content server.
Definition network_content.cpp:736
ClientNetworkContentSocketHandler::requested
ContentIDList requested
ContentIDs we already requested (so we don't do it again)
Definition network_content.h:67
ClientNetworkContentSocketHandler::UnselectAll
void UnselectAll()
Unselect everything that we've not downloaded so far.
Definition network_content.cpp:890
ClientNetworkContentSocketHandler::RequestContentList
void RequestContentList(ContentType type)
Request the content list for the given type.
Definition network_content.cpp:175
ClientNetworkContentSocketHandler::is_connecting
bool is_connecting
Whether we're connecting.
Definition network_content.h:76
ClientNetworkContentSocketHandler::http_response
std::vector< char > http_response
The HTTP response to the requests we've been doing.
Definition network_content.h:71
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:934
ClientNetworkContentSocketHandler::queued
ContentIDList queued
ContentID queue to be requested.
Definition network_content.h:68
ClientNetworkContentSocketHandler::OnDownloadProgress
void OnDownloadProgress(const ContentInfo &ci, int bytes) override
We have progress in the download of a file.
Definition network_content.cpp:1083
ClientNetworkContentSocketHandler::DownloadContentInfo
void DownloadContentInfo(ContentID cid)
Download information of a given Content ID if not already tried.
Definition network_content.cpp:792
ClientNetworkContentSocketHandler::Receive_SERVER_INFO
bool Receive_SERVER_INFO(Packet &p) override
Server sending list of content info: uint8_t type (invalid ID == does not exist) uint32_t id uint32_t...
Definition network_content.cpp:86
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:761
ClientNetworkContentSocketHandler::OnDownloadComplete
void OnDownloadComplete(ContentID cid) override
We have finished downloading a file.
Definition network_content.cpp:1092
ClientNetworkContentSocketHandler::infos
ContentVector infos
All content info we received.
Definition network_content.h:69
ClientNetworkContentSocketHandler::OnFailure
void OnFailure() override
An error has occurred and the connection has been closed.
Definition network_content.cpp:559
ClientNetworkContentSocketHandler::ContentIDList
std::vector< ContentID > ContentIDList
List of content IDs to (possibly) select.
Definition network_content.h:65
ClientNetworkContentSocketHandler::GetContent
ContentInfo * GetContent(ContentID cid) const
Get the content info based on a ContentID.
Definition network_content.cpp:832
ClientNetworkContentSocketHandler::Receive_SERVER_CONTENT
bool Receive_SERVER_CONTENT(Packet &p) override
Server sending list of content info: uint32_t unique id uint32_t file size (0 == does not exist) stri...
Definition network_content.cpp:450
ClientNetworkContentSocketHandler::Connect
void Connect()
Connect with the content server.
Definition network_content.cpp:723
ClientNetworkContentSocketHandler::reverse_dependency_map
std::unordered_multimap< ContentID, ContentID > reverse_dependency_map
Content reverse dependency map.
Definition network_content.h:70
ClientNetworkContentSocketHandler::BeforeDownload
bool BeforeDownload()
Handle the opening of the file before downloading.
Definition network_content.cpp:494
ClientNetworkContentSocketHandler::CheckDependencyState
void CheckDependencyState(const ContentInfo &ci)
Check the dependencies (recursively) of this content info.
Definition network_content.cpp:956
ClientNetworkContentSocketHandler::Clear
void Clear()
Clear all downloaded content information.
Definition network_content.cpp:1044
ClientNetworkContentSocketHandler::RequestQueuedContentInfo
void RequestQueuedContentInfo()
Send a content request for queued content info download.
Definition network_content.cpp:805
ClientNetworkContentSocketHandler::cur_file
std::optional< FileHandle > cur_file
Currently downloaded file.
Definition network_content.h:74
ClientNetworkContentSocketHandler::last_activity
std::chrono::steady_clock::time_point last_activity
The last time there was network activity.
Definition network_content.h:78
ClientNetworkContentSocketHandler::AfterDownload
void AfterDownload()
Handle the closing and extracting of a file after downloading it has been done.
Definition network_content.cpp:521
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:920
ClientNetworkContentSocketHandler::IsCancelled
bool IsCancelled() const override
Check if there is a request to cancel the transfer.
Definition network_content.cpp:553
ClientNetworkContentSocketHandler::callbacks
std::vector< ContentCallback * > callbacks
Callbacks to notify "the world".
Definition network_content.h:66
ClientNetworkContentSocketHandler::DownloadSelectedContentFallback
void DownloadSelectedContentFallback(const ContentIDList &content)
Initiate downloading the content over the fallback protocol.
Definition network_content.cpp:338
ClientNetworkContentSocketHandler::Unselect
void Unselect(ContentID cid)
Unselect a specific content id.
Definition network_content.cpp:858
ClientNetworkContentSocketHandler::Cancel
void Cancel()
Cancel the current download.
Definition network_content.cpp:751
ClientNetworkContentSocketHandler::OnDisconnect
void OnDisconnect() override
Callback for when the connection got disconnected.
Definition network_content.cpp:1064
ClientNetworkContentSocketHandler::cur_info
std::unique_ptr< ContentInfo > cur_info
Information about the currently downloaded file.
Definition network_content.h:75
ClientNetworkContentSocketHandler::is_cancelled
bool is_cancelled
Whether the download has been cancelled.
Definition network_content.h:77
ClientNetworkContentSocketHandler::SelectAll
void SelectAll()
Select everything we can select.
Definition network_content.cpp:868
FileHandle::Open
static std::optional< FileHandle > Open(const std::string &filename, std::string_view mode)
Open an RAII file handle if possible.
Definition fileio.cpp:1171
Game::HasGame
static bool HasGame(const ContentInfo &ci, bool md5sum)
Wrapper function for GameScanner::HasGame.
Definition game_core.cpp:245
NetworkContentConnecter
Connect to the content server.
Definition network_content.cpp:695
NetworkContentConnecter::OnFailure
void OnFailure() override
Callback for when the connection attempt failed.
Definition network_content.cpp:703
NetworkContentConnecter::OnConnect
void OnConnect(SOCKET s) override
Callback when the connection succeeded.
Definition network_content.cpp:709
NetworkContentConnecter::NetworkContentConnecter
NetworkContentConnecter(std::string_view connection_string)
Initiate the connecting.
Definition network_content.cpp:701
NetworkContentSocketHandler::ReceivePackets
bool ReceivePackets()
Receive a packet at TCP level.
Definition tcp_content.cpp:133
NetworkHTTPSocketHandler::Connect
static void Connect(std::string_view uri, HTTPCallback *callback, std::string &&data="")
Connect to the given URI.
Definition http_curl.cpp:93
NetworkSocketHandler::Reopen
void Reopen()
Reopen the socket so we can send/receive stuff again.
Definition core.h:79
NetworkTCPSocketHandler::CloseConnection
virtual NetworkRecvStatus CloseConnection(bool error=true)
This will put this socket handler in a close state.
Definition tcp.cpp:41
NetworkTCPSocketHandler::sock
SOCKET sock
The socket currently connected to.
Definition tcp.h:38
NetworkTCPSocketHandler::SendPacket
virtual void SendPacket(std::unique_ptr< Packet > &&packet)
This function puts the packet in the send-queue and it is send as soon as possible.
Definition tcp.cpp:58
NetworkTCPSocketHandler::SendPackets
SendPacketsState SendPackets(bool closing_down=false)
Sends all the buffered packets out for this client.
Definition tcp.cpp:76
NetworkTCPSocketHandler::CloseSocket
void CloseSocket()
Close the actual socket of the connection.
Definition tcp.cpp:29
NetworkTCPSocketHandler::CanSendReceive
bool CanSendReceive()
Check whether this socket can send or receive something.
Definition tcp.cpp:194
StringConsumer
Parse data from a string / buffer.
Definition string_consumer.hpp:27
StringConsumer::KEEP_SEPARATOR
@ KEEP_SEPARATOR
Keep the separator in the data as next value to be read.
Definition string_consumer.hpp:695
StringConsumer::ReadIntegerBase
T ReadIntegerBase(int base, T def=0, bool clamp=false)
Read and parse an integer in number 'base', and advance the reader.
Definition string_consumer.hpp:891
TCPConnecter
"Helper" class for creating TCP connections in a non-blocking manner
Definition tcp.h:74
TCPConnecter::connection_string
std::string connection_string
Current address we are connecting to (before resolving).
Definition tcp.h:103
TarScanner
Helper for scanning for files with tar as extension.
Definition fileio_func.h:59
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:440
TimeoutTimer
A timeout timer will fire once after the interval.
Definition timer.h:116
TimeoutTimer::Reset
void Reset()
Reset the timer, so it will fire again after the timeout.
Definition timer.h:140
NetworkContentMirrorUriString
std::string_view NetworkContentMirrorUriString()
Get the URI string for the content mirror from the environment variable OTTD_CONTENT_MIRROR_URI,...
Definition config.cpp:53
NetworkContentServerConnectionString
std::string_view NetworkContentServerConnectionString()
Get the connection string for the content server from the environment variable OTTD_CONTENT_SERVER_CS...
Definition config.cpp:43
NETWORK_CONTENT_SERVER_PORT
static const uint16_t NETWORK_CONTENT_SERVER_PORT
The default port of the content server (TCP)
Definition config.h:24
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:65
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:64
TCP_MTU
static const size_t TCP_MTU
Number of bytes we can pack in a single TCP packet.
Definition config.h:45
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:62
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:66
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:67
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:63
include
bool include(Container &container, typename Container::const_reference &item)
Helper function to append an item to a container if it is not already contained.
Definition container_func.hpp:24
NetworkRecvStatus
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition core.h:23
NETWORK_RECV_STATUS_OKAY
@ NETWORK_RECV_STATUS_OKAY
Everything is okay.
Definition core.h:24
WL_ERROR
@ WL_ERROR
Errors (eg. saving/loading failed)
Definition error.h:26
ShowErrorMessage
void ShowErrorMessage(EncodedString &&summary_msg, int x, int y, CommandCost &cc)
Display an error message in a window.
Definition error_gui.cpp:290
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:590
FioRemove
bool FioRemove(const std::string &filename)
Remove a file.
Definition fileio.cpp:327
SP_AUTODOWNLOAD_DIR
@ SP_AUTODOWNLOAD_DIR
Search within the autodownload directory.
Definition fileio_type.h:124
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:88
NO_DIRECTORY
@ NO_DIRECTORY
A path without any base directory.
Definition fileio_type.h:107
BASESET_DIR
@ BASESET_DIR
Subdirectory for all base data (base sets, intro game)
Definition fileio_type.h:96
HasContentProc
bool(const ContentInfo &ci, bool md5sum) HasContentProc
Check whether a function piece of content is locally known.
Definition network_content.cpp:62
HasGRFConfig
static bool HasGRFConfig(const ContentInfo &ci, bool md5sum)
Wrapper function for the HasProc.
Definition network_content.cpp:50
_network_content_client
ClientNetworkContentSocketHandler _network_content_client
The client we use to connect to the server.
Definition network_content.cpp:47
GetHasContentProcforContentType
static HasContentProc * GetHasContentProcforContentType(ContentType type)
Get the has-content check function for the given content type.
Definition network_content.cpp:69
HasScenario
bool HasScenario(const ContentInfo &ci, bool md5sum)
Check whether we've got a given scenario based on its unique ID.
Definition fios.cpp:684
GunzipFile
static bool GunzipFile(const ContentInfo &ci)
Gunzip a given file and remove the .gz if successful.
Definition network_content.cpp:388
CONTENT_QUEUE_TIMEOUT
static constexpr auto CONTENT_QUEUE_TIMEOUT
Timeout after queueing content for it to try to be requested.
Definition network_content.cpp:782
GetFullFilename
static std::string GetFullFilename(const ContentInfo &ci, bool compressed)
Determine the full filename of a piece of content information.
Definition network_content.cpp:371
network_content.h
Part of the network protocol handling content distribution.
ContentVector
std::vector< std::unique_ptr< ContentInfo > > ContentVector
Vector with content info.
Definition network_content.h:20
ConstContentVector
std::vector< const ContentInfo * > ConstContentVector
Vector with constant content info.
Definition network_content.h:22
FindGRFConfig
const GRFConfig * FindGRFConfig(uint32_t grfid, FindGRFConfigMode mode, const MD5Hash *md5sum, uint32_t desired_version)
Find a NewGRF in the scanned list.
Definition newgrf_config.cpp:596
FGCM_ANY
@ FGCM_ANY
Use first found.
Definition newgrf_config.h:212
FGCM_EXACT
@ FGCM_EXACT
Only find Grfs matching md5sum.
Definition newgrf_config.h:208
PacketSize
uint16_t PacketSize
Size of the whole packet.
Definition packet.h:21
StringValidationSetting::ReplaceWithQuestionMark
@ ReplaceWithQuestionMark
Replace the unknown/bad bits with question marks.
StringValidationSetting::AllowNewline
@ AllowNewline
Allow newlines; replaces '\r ' with ' ' during processing.
GetEncodedString
EncodedString GetEncodedString(StringID str)
Encode a string with no parameters into an encoded string.
Definition strings.cpp:90
ContentCallback
Callbacks for notifying others about incoming data.
Definition network_content.h:25
ContentCallback::OnDownloadProgress
virtual void OnDownloadProgress(const ContentInfo &ci, int bytes)
We have progress in the download of a file.
Definition network_content.h:48
ContentCallback::OnDisconnect
virtual void OnDisconnect()
Callback for when the connection got disconnected.
Definition network_content.h:35
ContentCallback::OnReceiveContentInfo
virtual void OnReceiveContentInfo(const ContentInfo &ci)
We received a content info.
Definition network_content.h:41
ContentCallback::OnDownloadComplete
virtual void OnDownloadComplete(ContentID cid)
We have finished downloading a file.
Definition network_content.h:54
ContentCallback::OnConnect
virtual void OnConnect(bool success)
Callback for when the connection has finished.
Definition network_content.h:30
ContentInfo
Container for all important information about a piece of content.
Definition tcp_content_type.h:56
ContentInfo::unique_id
uint32_t unique_id
Unique ID; either GRF ID or shortname.
Definition tcp_content_type.h:75
ContentInfo::md5sum
MD5Hash md5sum
The MD5 checksum.
Definition tcp_content_type.h:76
ContentInfo::state
State state
Whether the content info is selected (for download)
Definition tcp_content_type.h:79
ContentInfo::id
ContentID id
Unique (server side) ID for the content.
Definition tcp_content_type.h:68
ContentInfo::filename
std::string filename
Filename (for the .tar.gz; only valid on download)
Definition tcp_content_type.h:70
ContentInfo::IsSelected
bool IsSelected() const
Is the state either selected or autoselected?
Definition tcp_content.cpp:32
ContentInfo::type
ContentType type
Type of content.
Definition tcp_content_type.h:67
ContentInfo::dependencies
std::vector< ContentID > dependencies
The dependencies (unique server side ids)
Definition tcp_content_type.h:77
ContentInfo::State::Unselected
@ Unselected
The content has not been selected.
ContentInfo::State::AlreadyHere
@ AlreadyHere
The content is already at the client side.
ContentInfo::State::Selected
@ Selected
The content has been manually selected.
ContentInfo::State::Autoselected
@ Autoselected
The content has been selected as dependency.
ContentInfo::State::DoesNotExist
@ DoesNotExist
The content does not exist in the content system.
Packet
Internal entity of a packet.
Definition packet.h:43
Packet::Recv_bytes
size_t Recv_bytes(std::span< uint8_t > span)
Extract at most the length of the span bytes from the packet into the span.
Definition packet.cpp:403
Packet::Recv_uint32
uint32_t Recv_uint32()
Read a 32 bits integer from the packet.
Definition packet.cpp:347
Packet::Recv_string
std::string Recv_string(size_t length, StringValidationSettings settings=StringValidationSetting::ReplaceWithQuestionMark)
Reads characters (bytes) from the packet until it finds a '\0', or reaches a maximum of length charac...
Definition packet.cpp:425
Packet::Recv_uint8
uint8_t Recv_uint8()
Read a 8 bits integer from the packet.
Definition packet.cpp:318
Packet::RemainingBytesToTransfer
size_t RemainingBytesToTransfer() const
Get the amount of bytes that are still available for the Transfer functions.
Definition packet.cpp:447
Packet::TransferOut
ssize_t TransferOut(F transfer_function)
Transfer data from the packet to the given function.
Definition packet.h:128
GetContentInfoSubDir
Subdirectory GetContentInfoSubDir(ContentType type)
Helper to get the subdirectory a ContentInfo is located in.
Definition tcp_content.cpp:190
ContentID
uint32_t ContentID
Unique identifier for the content.
Definition tcp_content_type.h:51
ContentType
ContentType
The values in the enum are important; they are used as database 'keys'.
Definition tcp_content_type.h:21
CONTENT_TYPE_AI_LIBRARY
@ CONTENT_TYPE_AI_LIBRARY
The content consists of an AI library.
Definition tcp_content_type.h:26
CONTENT_TYPE_BASE_SOUNDS
@ CONTENT_TYPE_BASE_SOUNDS
The content consists of base sounds.
Definition tcp_content_type.h:29
CONTENT_TYPE_GAME_LIBRARY
@ CONTENT_TYPE_GAME_LIBRARY
The content consists of a GS library.
Definition tcp_content_type.h:32
CONTENT_TYPE_BASE_GRAPHICS
@ CONTENT_TYPE_BASE_GRAPHICS
The content consists of base graphics.
Definition tcp_content_type.h:23
CONTENT_TYPE_AI
@ CONTENT_TYPE_AI
The content consists of an AI.
Definition tcp_content_type.h:25
CONTENT_TYPE_SCENARIO
@ CONTENT_TYPE_SCENARIO
The content consists of a scenario.
Definition tcp_content_type.h:27
CONTENT_TYPE_NEWGRF
@ CONTENT_TYPE_NEWGRF
The content consists of a NewGRF.
Definition tcp_content_type.h:24
CONTENT_TYPE_BASE_MUSIC
@ CONTENT_TYPE_BASE_MUSIC
The content consists of base music.
Definition tcp_content_type.h:30
CONTENT_TYPE_GAME
@ CONTENT_TYPE_GAME
The content consists of a game script.
Definition tcp_content_type.h:31
CONTENT_TYPE_END
@ CONTENT_TYPE_END
Helper to mark the end of the types.
Definition tcp_content_type.h:33
CONTENT_TYPE_HEIGHTMAP
@ CONTENT_TYPE_HEIGHTMAP
The content consists of a heightmap.
Definition tcp_content_type.h:28
PACKET_CONTENT_CLIENT_CONTENT
@ PACKET_CONTENT_CLIENT_CONTENT
Request a content file given an internal ID.
Definition tcp_content_type.h:45
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:40
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:43
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:41
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:42
CloseWindowById
void CloseWindowById(WindowClass cls, WindowNumber number, bool force, int data)
Close a window by its class and window number (if it is open).
Definition window.cpp:1193
WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD
@ WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD
Network content download status.
Definition window_type.h:45
WC_NETWORK_STATUS_WINDOW
@ WC_NETWORK_STATUS_WINDOW
Network status window; Window numbers:
Definition window_type.h:496