OpenTTD Source: src/network/core/tcp_content.cpp Source File

OpenTTD Source 15.0-beta1

tcp_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
12#include "../../stdafx.h"
13#include "../../textfile_gui.h"
14#include "../../newgrf_config.h"
15#include "../../base_media_base.h"
16#include "../../ai/ai.hpp"
17#include "../../game/game.hpp"
18#include "../../fios.h"
19#include "tcp_content.h"
20
21#include "../../safeguards.h"
22
27 bool ContentInfo::IsSelected() const
28{
29 switch (this->state) {
30 case ContentInfo::SELECTED:
31 case ContentInfo::AUTOSELECTED:
32 case ContentInfo::ALREADY_HERE:
33 return true;
34
35 default:
36 return false;
37 }
38}
39
44 bool ContentInfo::IsValid() const
45{
46 return this->state < ContentInfo::INVALID && this->type >= CONTENT_TYPE_BEGIN && this->type < CONTENT_TYPE_END;
47}
48
54std::optional<std::string> ContentInfo::GetTextfile(TextfileType type) const
55{
56 if (this->state == INVALID) return std::nullopt;
57 const char *tmp;
58 switch (this->type) {
59 default: NOT_REACHED();
60 case CONTENT_TYPE_AI:
61 tmp = AI::GetScannerInfo()->FindMainScript(this, true);
62 break;
63 case CONTENT_TYPE_AI_LIBRARY:
64 tmp = AI::GetScannerLibrary()->FindMainScript(this, true);
65 break;
66 case CONTENT_TYPE_GAME:
67 tmp = Game::GetScannerInfo()->FindMainScript(this, true);
68 break;
69 case CONTENT_TYPE_GAME_LIBRARY:
70 tmp = Game::GetScannerLibrary()->FindMainScript(this, true);
71 break;
72 case CONTENT_TYPE_NEWGRF: {
73 const GRFConfig *gc = FindGRFConfig(BSWAP32(this->unique_id), FGCM_EXACT, &this->md5sum);
74 tmp = gc != nullptr ? gc->filename.c_str() : nullptr;
75 break;
76 }
77 case CONTENT_TYPE_BASE_GRAPHICS:
78 tmp = TryGetBaseSetFile(this, true, BaseGraphics::GetAvailableSets());
79 break;
80 case CONTENT_TYPE_BASE_SOUNDS:
81 tmp = TryGetBaseSetFile(this, true, BaseSounds::GetAvailableSets());
82 break;
83 case CONTENT_TYPE_BASE_MUSIC:
84 tmp = TryGetBaseSetFile(this, true, BaseMusic::GetAvailableSets());
85 break;
86 case CONTENT_TYPE_SCENARIO:
87 case CONTENT_TYPE_HEIGHTMAP:
88 tmp = FindScenario(this, true);
89 break;
90 }
91 if (tmp == nullptr) return std::nullopt;
92 return ::GetTextfile(type, GetContentInfoSubDir(this->type), tmp);
93}
94
101 bool NetworkContentSocketHandler::HandlePacket(Packet &p)
102{
103 PacketContentType type = (PacketContentType)p.Recv_uint8();
104
105 switch (this->HasClientQuit() ? PACKET_CONTENT_END : type) {
106 case PACKET_CONTENT_CLIENT_INFO_LIST: return this->Receive_CLIENT_INFO_LIST(p);
107 case PACKET_CONTENT_CLIENT_INFO_ID: return this->Receive_CLIENT_INFO_ID(p);
108 case PACKET_CONTENT_CLIENT_INFO_EXTID: return this->Receive_CLIENT_INFO_EXTID(p);
109 case PACKET_CONTENT_CLIENT_INFO_EXTID_MD5: return this->Receive_CLIENT_INFO_EXTID_MD5(p);
110 case PACKET_CONTENT_SERVER_INFO: return this->Receive_SERVER_INFO(p);
111 case PACKET_CONTENT_CLIENT_CONTENT: return this->Receive_CLIENT_CONTENT(p);
112 case PACKET_CONTENT_SERVER_CONTENT: return this->Receive_SERVER_CONTENT(p);
113
114 default:
115 if (this->HasClientQuit()) {
116 Debug(net, 0, "[tcp/content] Received invalid packet type {}", type);
117 } else {
118 Debug(net, 0, "[tcp/content] Received illegal packet");
119 }
120 return false;
121 }
122}
123
128 bool NetworkContentSocketHandler::ReceivePackets()
129{
130 /*
131 * We read only a few of the packets. This as receiving packets can be expensive
132 * due to the re-resolving of the parent/child relations and checking the toggle
133 * state of all bits. We cannot do this all in one go, as we want to show the
134 * user what we already received. Otherwise, it can take very long before any
135 * progress is shown to the end user that something has been received.
136 * It is also the case that we request extra content from the content server in
137 * case there is an unknown (in the content list) piece of content. These will
138 * come in after the main lists have been requested. As a result, we won't be
139 * getting everything reliably in one batch. Thus, we need to make subsequent
140 * updates in that case as well.
141 *
142 * As a result, we simple handle an arbitrary number of packets in one cycle,
143 * and let the rest be handled in subsequent cycles. These are ran, almost,
144 * immediately after this cycle so in speed it does not matter much, except
145 * that the user inferface will appear better responding.
146 *
147 * What arbitrary number to choose is the ultimate question though.
148 */
149 std::unique_ptr<Packet> p;
150 static const int MAX_PACKETS_TO_RECEIVE = 42;
151 int i = MAX_PACKETS_TO_RECEIVE;
152 while (--i != 0 && (p = this->ReceivePacket()) != nullptr) {
153 bool cont = this->HandlePacket(*p);
154 if (!cont) return true;
155 }
156
157 return i != MAX_PACKETS_TO_RECEIVE - 1;
158}
159
160
166 bool NetworkContentSocketHandler::ReceiveInvalidPacket(PacketContentType type)
167{
168 Debug(net, 0, "[tcp/content] Received illegal packet type {}", type);
169 return false;
170}
171
172 bool NetworkContentSocketHandler::Receive_CLIENT_INFO_LIST(Packet &) { return this->ReceiveInvalidPacket(PACKET_CONTENT_CLIENT_INFO_LIST); }
173 bool NetworkContentSocketHandler::Receive_CLIENT_INFO_ID(Packet &) { return this->ReceiveInvalidPacket(PACKET_CONTENT_CLIENT_INFO_ID); }
174 bool NetworkContentSocketHandler::Receive_CLIENT_INFO_EXTID(Packet &) { return this->ReceiveInvalidPacket(PACKET_CONTENT_CLIENT_INFO_EXTID); }
175 bool NetworkContentSocketHandler::Receive_CLIENT_INFO_EXTID_MD5(Packet &) { return this->ReceiveInvalidPacket(PACKET_CONTENT_CLIENT_INFO_EXTID_MD5); }
176 bool NetworkContentSocketHandler::Receive_SERVER_INFO(Packet &) { return this->ReceiveInvalidPacket(PACKET_CONTENT_SERVER_INFO); }
177 bool NetworkContentSocketHandler::Receive_CLIENT_CONTENT(Packet &) { return this->ReceiveInvalidPacket(PACKET_CONTENT_CLIENT_CONTENT); }
178 bool NetworkContentSocketHandler::Receive_SERVER_CONTENT(Packet &) { return this->ReceiveInvalidPacket(PACKET_CONTENT_SERVER_CONTENT); }
179
185 Subdirectory GetContentInfoSubDir(ContentType type)
186{
187 switch (type) {
188 default: return NO_DIRECTORY;
189 case CONTENT_TYPE_AI: return AI_DIR;
190 case CONTENT_TYPE_AI_LIBRARY: return AI_LIBRARY_DIR;
191 case CONTENT_TYPE_GAME: return GAME_DIR;
192 case CONTENT_TYPE_GAME_LIBRARY: return GAME_LIBRARY_DIR;
193 case CONTENT_TYPE_NEWGRF: return NEWGRF_DIR;
194
195 case CONTENT_TYPE_BASE_GRAPHICS:
196 case CONTENT_TYPE_BASE_SOUNDS:
197 case CONTENT_TYPE_BASE_MUSIC:
198 return BASESET_DIR;
199
200 case CONTENT_TYPE_SCENARIO: return SCENARIO_DIR;
201 case CONTENT_TYPE_HEIGHTMAP: return HEIGHTMAP_DIR;
202 }
203}
TryGetBaseSetFile
const char * TryGetBaseSetFile(const ContentInfo *ci, bool md5sum, const Tbase_set *s)
Check whether there's a base set matching some information.
Definition base_media_func.h:319
BSWAP32
static uint32_t BSWAP32(uint32_t x)
Perform a 32 bits endianness bitswap on x.
Definition bitmath_func.hpp:364
AI::GetScannerLibrary
static AIScannerLibrary * GetScannerLibrary()
Gets the ScriptScanner instance that is used to find AI Libraries.
Definition ai_core.cpp:360
AI::GetScannerInfo
static AIScannerInfo * GetScannerInfo()
Gets the ScriptScanner instance that is used to find AIs.
Definition ai_core.cpp:355
BaseMedia< GraphicsSet >::GetAvailableSets
static GraphicsSet * GetAvailableSets()
Return the available sets.
Definition base_media_func.h:404
Game::GetScannerLibrary
static GameScannerLibrary * GetScannerLibrary()
Gets the ScriptScanner instance that is used to find Game Libraries.
Definition game_core.cpp:267
Game::GetScannerInfo
static GameScannerInfo * GetScannerInfo()
Gets the ScriptScanner instance that is used to find Game scripts.
Definition game_core.cpp:263
NetworkContentSocketHandler::Receive_CLIENT_CONTENT
virtual bool Receive_CLIENT_CONTENT(Packet &p)
Client requesting the actual content: uint16_t count of unique ids uint32_t unique id (count times)
Definition tcp_content.cpp:177
NetworkContentSocketHandler::Receive_CLIENT_INFO_EXTID
virtual bool Receive_CLIENT_INFO_EXTID(Packet &p)
Client requesting a list of content info based on an external 'unique' id; GRF ID for NewGRFS,...
Definition tcp_content.cpp:174
NetworkContentSocketHandler::Receive_CLIENT_INFO_EXTID_MD5
virtual bool Receive_CLIENT_INFO_EXTID_MD5(Packet &p)
Client requesting a list of content info based on an external 'unique' id; GRF ID + MD5 checksum for ...
Definition tcp_content.cpp:175
NetworkContentSocketHandler::Receive_SERVER_INFO
virtual bool Receive_SERVER_INFO(Packet &p)
Server sending list of content info: uint8_t type (invalid ID == does not exist) uint32_t id uint32_t...
Definition tcp_content.cpp:176
NetworkContentSocketHandler::Receive_CLIENT_INFO_ID
virtual bool Receive_CLIENT_INFO_ID(Packet &p)
Client requesting a list of content info: uint16_t count of ids uint32_t id (count times)
Definition tcp_content.cpp:173
NetworkContentSocketHandler::Receive_SERVER_CONTENT
virtual bool Receive_SERVER_CONTENT(Packet &p)
Server sending list of content info: uint32_t unique id uint32_t file size (0 == does not exist) stri...
Definition tcp_content.cpp:178
NetworkContentSocketHandler::ReceivePackets
bool ReceivePackets()
Receive a packet at TCP level.
Definition tcp_content.cpp:128
NetworkContentSocketHandler::Receive_CLIENT_INFO_LIST
virtual bool Receive_CLIENT_INFO_LIST(Packet &p)
Client requesting a list of content info: uint8_t type uint32_t openttd version (or 0xFFFFFFFF if usi...
Definition tcp_content.cpp:172
NetworkContentSocketHandler::ReceiveInvalidPacket
bool ReceiveInvalidPacket(PacketContentType type)
Helper for logging receiving invalid packets.
Definition tcp_content.cpp:166
NetworkContentSocketHandler::HandlePacket
bool HandlePacket(Packet &p)
Handle the given packet, i.e.
Definition tcp_content.cpp:101
NetworkSocketHandler::HasClientQuit
bool HasClientQuit() const
Whether the current client connected to the socket has quit.
Definition core.h:74
NetworkTCPSocketHandler::ReceivePacket
virtual std::unique_ptr< Packet > ReceivePacket()
Receives a packet for the given client.
Definition tcp.cpp:129
ScriptScanner::FindMainScript
const char * FindMainScript(const ContentInfo *ci, bool md5sum)
Find a script of a ContentInfo.
Definition script_scanner.cpp:243
Debug
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition debug.h:37
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:115
NO_DIRECTORY
@ NO_DIRECTORY
A path without any base directory.
Definition fileio_type.h:133
AI_LIBRARY_DIR
@ AI_LIBRARY_DIR
Subdirectory for all AI libraries.
Definition fileio_type.h:127
GAME_LIBRARY_DIR
@ GAME_LIBRARY_DIR
Subdirectory for all GS libraries.
Definition fileio_type.h:129
AI_DIR
@ AI_DIR
Subdirectory for all AI files.
Definition fileio_type.h:126
SCENARIO_DIR
@ SCENARIO_DIR
Base directory for all scenarios.
Definition fileio_type.h:119
HEIGHTMAP_DIR
@ HEIGHTMAP_DIR
Subdirectory of scenario for heightmaps.
Definition fileio_type.h:120
NEWGRF_DIR
@ NEWGRF_DIR
Subdirectory for all NewGRFs.
Definition fileio_type.h:124
BASESET_DIR
@ BASESET_DIR
Subdirectory for all base data (base sets, intro game)
Definition fileio_type.h:123
GAME_DIR
@ GAME_DIR
Subdirectory for all game scripts.
Definition fileio_type.h:128
FindScenario
const char * FindScenario(const ContentInfo *ci, bool md5sum)
Find a given scenario based on its unique ID.
Definition fios.cpp:690
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:670
FGCM_EXACT
@ FGCM_EXACT
Only find Grfs matching md5sum.
Definition newgrf_config.h:203
ContentInfo::unique_id
uint32_t unique_id
Unique ID; either GRF ID or shortname.
Definition tcp_content_type.h:71
ContentInfo::IsValid
bool IsValid() const
Is the information from this content info valid?
Definition tcp_content.cpp:44
ContentInfo::md5sum
MD5Hash md5sum
The MD5 checksum.
Definition tcp_content_type.h:72
ContentInfo::state
State state
Whether the content info is selected (for download)
Definition tcp_content_type.h:75
ContentInfo::GetTextfile
std::optional< std::string > GetTextfile(TextfileType type) const
Search a textfile file next to this file in the content list.
Definition tcp_content.cpp:54
ContentInfo::ALREADY_HERE
@ ALREADY_HERE
The content is already at the client side.
Definition tcp_content_type.h:58
ContentInfo::AUTOSELECTED
@ AUTOSELECTED
The content has been selected as dependency.
Definition tcp_content_type.h:57
ContentInfo::INVALID
@ INVALID
The content's invalid.
Definition tcp_content_type.h:60
ContentInfo::SELECTED
@ SELECTED
The content has been manually selected.
Definition tcp_content_type.h:56
ContentInfo::IsSelected
bool IsSelected() const
Is the state either selected or autoselected?
Definition tcp_content.cpp:27
ContentInfo::type
ContentType type
Type of content.
Definition tcp_content_type.h:63
GRFConfig
Information about GRF, used in the game and (part of it) in savegames.
Definition newgrf_config.h:154
GRFConfig::filename
std::string filename
Filename - either with or without full path.
Definition newgrf_config.h:165
Packet
Internal entity of a packet.
Definition packet.h:42
Packet::Recv_uint8
uint8_t Recv_uint8()
Read a 8 bits integer from the packet.
Definition packet.cpp:318
GetContentInfoSubDir
Subdirectory GetContentInfoSubDir(ContentType type)
Helper to get the subdirectory a ContentInfo is located in.
Definition tcp_content.cpp:185
tcp_content.h
Basic functions to receive and send TCP packets to/from the content server.
ContentType
ContentType
The values in the enum are important; they are used as database 'keys'.
Definition tcp_content_type.h:18
CONTENT_TYPE_AI_LIBRARY
@ CONTENT_TYPE_AI_LIBRARY
The content consists of an AI library.
Definition tcp_content_type.h:23
CONTENT_TYPE_BASE_SOUNDS
@ CONTENT_TYPE_BASE_SOUNDS
The content consists of base sounds.
Definition tcp_content_type.h:26
CONTENT_TYPE_GAME_LIBRARY
@ CONTENT_TYPE_GAME_LIBRARY
The content consists of a GS library.
Definition tcp_content_type.h:29
CONTENT_TYPE_BASE_GRAPHICS
@ CONTENT_TYPE_BASE_GRAPHICS
The content consists of base graphics.
Definition tcp_content_type.h:20
CONTENT_TYPE_AI
@ CONTENT_TYPE_AI
The content consists of an AI.
Definition tcp_content_type.h:22
CONTENT_TYPE_SCENARIO
@ CONTENT_TYPE_SCENARIO
The content consists of a scenario.
Definition tcp_content_type.h:24
CONTENT_TYPE_NEWGRF
@ CONTENT_TYPE_NEWGRF
The content consists of a NewGRF.
Definition tcp_content_type.h:21
CONTENT_TYPE_BEGIN
@ CONTENT_TYPE_BEGIN
Helper to mark the begin of the types.
Definition tcp_content_type.h:19
CONTENT_TYPE_BASE_MUSIC
@ CONTENT_TYPE_BASE_MUSIC
The content consists of base music.
Definition tcp_content_type.h:27
CONTENT_TYPE_GAME
@ CONTENT_TYPE_GAME
The content consists of a game script.
Definition tcp_content_type.h:28
CONTENT_TYPE_END
@ CONTENT_TYPE_END
Helper to mark the end of the types.
Definition tcp_content_type.h:30
CONTENT_TYPE_HEIGHTMAP
@ CONTENT_TYPE_HEIGHTMAP
The content consists of a heightmap.
Definition tcp_content_type.h:25
PacketContentType
PacketContentType
Enum with all types of TCP content packets.
Definition tcp_content_type.h:35
PACKET_CONTENT_CLIENT_CONTENT
@ PACKET_CONTENT_CLIENT_CONTENT
Request a content file given an internal ID.
Definition tcp_content_type.h:41
PACKET_CONTENT_END
@ PACKET_CONTENT_END
Must ALWAYS be on the end of this list!! (period)
Definition tcp_content_type.h:43
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:36
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:39
PACKET_CONTENT_SERVER_INFO
@ PACKET_CONTENT_SERVER_INFO
Reply of content server with information about content.
Definition tcp_content_type.h:40
PACKET_CONTENT_SERVER_CONTENT
@ PACKET_CONTENT_SERVER_CONTENT
Reply with the content of the given ID.
Definition tcp_content_type.h:42
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:37
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:38
TextfileType
TextfileType
Additional text files accompanying Tar archives.
Definition textfile_type.h:14