OpenTTD
tcp_content.cpp
Go to the documentation of this file.
1 /* $Id: tcp_content.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
14 #ifdef ENABLE_NETWORK
15 
16 #include "../../stdafx.h"
17 #ifndef OPENTTD_MSU
18 #include "../../textfile_gui.h"
19 #include "../../newgrf_config.h"
20 #include "../../base_media_base.h"
21 #include "../../ai/ai.hpp"
22 #include "../../game/game.hpp"
23 #include "../../fios.h"
24 #endif /* OPENTTD_MSU */
25 #include "tcp_content.h"
26 
27 #include "../../safeguards.h"
28 
31 {
32  memset(this, 0, sizeof(*this));
33 }
34 
37 {
38  free(this->dependencies);
39  free(this->tags);
40 }
41 
47 {
48  if (other != this) {
49  free(this->dependencies);
50  free(this->tags);
51  memcpy(this, other, sizeof(ContentInfo));
52  other->dependencies = NULL;
53  other->tags = NULL;
54  }
55 }
56 
61 size_t ContentInfo::Size() const
62 {
63  size_t len = 0;
64  for (uint i = 0; i < this->tag_count; i++) len += strlen(this->tags[i]) + 1;
65 
66  /* The size is never larger than the content info size plus the size of the
67  * tags and dependencies */
68  return sizeof(*this) +
69  sizeof(this->dependency_count) +
70  sizeof(*this->dependencies) * this->dependency_count;
71 }
72 
78 {
79  switch (this->state) {
83  return true;
84 
85  default:
86  return false;
87  }
88 }
89 
95 {
96  return this->state < ContentInfo::INVALID && this->type >= CONTENT_TYPE_BEGIN && this->type < CONTENT_TYPE_END;
97 }
98 
99 #ifndef OPENTTD_MSU
100 
106 {
107  if (this->state == INVALID) return NULL;
108  const char *tmp;
109  switch (this->type) {
110  default: NOT_REACHED();
111  case CONTENT_TYPE_AI:
112  tmp = AI::GetScannerInfo()->FindMainScript(this, true);
113  break;
115  tmp = AI::GetScannerLibrary()->FindMainScript(this, true);
116  break;
117  case CONTENT_TYPE_GAME:
118  tmp = Game::GetScannerInfo()->FindMainScript(this, true);
119  break;
121  tmp = Game::GetScannerLibrary()->FindMainScript(this, true);
122  break;
123  case CONTENT_TYPE_NEWGRF: {
124  const GRFConfig *gc = FindGRFConfig(BSWAP32(this->unique_id), FGCM_EXACT, this->md5sum);
125  tmp = gc != NULL ? gc->filename : NULL;
126  break;
127  }
130  break;
132  tmp = TryGetBaseSetFile(this, true, BaseSounds::GetAvailableSets());
133  break;
135  tmp = TryGetBaseSetFile(this, true, BaseMusic::GetAvailableSets());
136  break;
139  extern const char *FindScenario(const ContentInfo *ci, bool md5sum);
140  tmp = FindScenario(this, true);
141  break;
142  }
143  if (tmp == NULL) return NULL;
144  return ::GetTextfile(type, GetContentInfoSubDir(this->type), tmp);
145 }
146 #endif /* OPENTTD_MSU */
147 
149 {
150  CloseConnection();
151  if (this->sock == INVALID_SOCKET) return;
152 
153  closesocket(this->sock);
154  this->sock = INVALID_SOCKET;
155 }
156 
164 {
166 
167  switch (this->HasClientQuit() ? PACKET_CONTENT_END : type) {
168  case PACKET_CONTENT_CLIENT_INFO_LIST: return this->Receive_CLIENT_INFO_LIST(p);
169  case PACKET_CONTENT_CLIENT_INFO_ID: return this->Receive_CLIENT_INFO_ID(p);
170  case PACKET_CONTENT_CLIENT_INFO_EXTID: return this->Receive_CLIENT_INFO_EXTID(p);
171  case PACKET_CONTENT_CLIENT_INFO_EXTID_MD5: return this->Receive_CLIENT_INFO_EXTID_MD5(p);
172  case PACKET_CONTENT_SERVER_INFO: return this->Receive_SERVER_INFO(p);
173  case PACKET_CONTENT_CLIENT_CONTENT: return this->Receive_CLIENT_CONTENT(p);
174  case PACKET_CONTENT_SERVER_CONTENT: return this->Receive_SERVER_CONTENT(p);
175 
176  default:
177  if (this->HasClientQuit()) {
178  DEBUG(net, 0, "[tcp/content] received invalid packet type %d from %s", type, this->client_addr.GetAddressAsString());
179  } else {
180  DEBUG(net, 0, "[tcp/content] received illegal packet from %s", this->client_addr.GetAddressAsString());
181  }
182  return false;
183  }
184 }
185 
191 {
192  /*
193  * We read only a few of the packets. This as receiving packets can be expensive
194  * due to the re-resolving of the parent/child relations and checking the toggle
195  * state of all bits. We cannot do this all in one go, as we want to show the
196  * user what we already received. Otherwise, it can take very long before any
197  * progress is shown to the end user that something has been received.
198  * It is also the case that we request extra content from the content server in
199  * case there is an unknown (in the content list) piece of content. These will
200  * come in after the main lists have been requested. As a result, we won't be
201  * getting everything reliably in one batch. Thus, we need to make subsequent
202  * updates in that case as well.
203  *
204  * As a result, we simple handle an arbitrary number of packets in one cycle,
205  * and let the rest be handled in subsequent cycles. These are ran, almost,
206  * immediately after this cycle so in speed it does not matter much, except
207  * that the user inferface will appear better responding.
208  *
209  * What arbitrary number to choose is the ultimate question though.
210  */
211  Packet *p;
212  static const int MAX_PACKETS_TO_RECEIVE = 42;
213  int i = MAX_PACKETS_TO_RECEIVE;
214  while (--i != 0 && (p = this->ReceivePacket()) != NULL) {
215  bool cont = this->HandlePacket(p);
216  delete p;
217  if (!cont) return true;
218  }
219 
220  return i != MAX_PACKETS_TO_RECEIVE - 1;
221 }
222 
223 
230 {
231  DEBUG(net, 0, "[tcp/content] received illegal packet type %d from %s", type, this->client_addr.GetAddressAsString());
232  return false;
233 }
234 
242 
243 #ifndef OPENTTD_MSU
244 
250 {
251  switch (type) {
252  default: return NO_DIRECTORY;
253  case CONTENT_TYPE_AI: return AI_DIR;
255  case CONTENT_TYPE_GAME: return GAME_DIR;
257  case CONTENT_TYPE_NEWGRF: return NEWGRF_DIR;
258 
262  return BASESET_DIR;
263 
264  case CONTENT_TYPE_SCENARIO: return SCENARIO_DIR;
266  }
267 }
268 #endif /* OPENTTD_MSU */
269 
270 #endif /* ENABLE_NETWORK */
Helper to mark the end of the types.
Definition: tcp_content.h:37
The content consists of base graphics.
Definition: tcp_content.h:27
bool IsSelected() const
Is the state either selected or autoselected?
Definition: tcp_content.cpp:77
uint32 unique_id
Unique ID; either GRF ID or shortname.
Definition: tcp_content.h:77
Internal entity of a packet.
Definition: packet.h:44
Queries the content server for information about a list of external IDs.
Definition: tcp_content.h:44
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:110
The content consists of a scenario.
Definition: tcp_content.h:31
Subdirectory of scenario for heightmaps.
Definition: fileio_type.h:115
uint8 dependency_count
Number of dependencies.
Definition: tcp_content.h:79
char(* tags)[32]
Malloced array of tags (strings)
Definition: tcp_content.h:82
static GameScannerInfo * GetScannerInfo()
Gets the ScriptScanner instance that is used to find Game scripts.
Definition: game_core.cpp:271
static AIScannerLibrary * GetScannerLibrary()
Gets the ScriptScanner instance that is used to find AI Libraries.
Definition: ai_core.cpp:385
ContentID * dependencies
Malloced array of dependencies (unique server side ids)
Definition: tcp_content.h:80
Subdirectory for all game scripts.
Definition: fileio_type.h:123
The content has been selected as dependency.
Definition: tcp_content.h:63
const GRFConfig * FindGRFConfig(uint32 grfid, FindGRFConfigMode mode, const uint8 *md5sum, uint32 desired_version)
Find a NewGRF in the scanned list.
Subdirectory for all base data (base sets, intro game)
Definition: fileio_type.h:118
Queries the content server for information about a list of internal IDs.
Definition: tcp_content.h:43
bool ReceivePackets()
Receive a packet at TCP level.
The content consists of a game script.
Definition: tcp_content.h:35
virtual bool Receive_CLIENT_CONTENT(Packet *p)
Client requesting the actual content: uint16 count of unique ids uint32 unique id (count times) ...
Base directory for all scenarios.
Definition: fileio_type.h:114
const char * FindScenario(const ContentInfo *ci, bool md5sum)
Find a given scenario based on its unique ID.
Definition: fios.cpp:746
The content&#39;s invalid.
Definition: tcp_content.h:66
uint8 tag_count
Number of tags.
Definition: tcp_content.h:81
Information about GRF, used in the game and (part of it) in savegames.
Reply of content server with information about content.
Definition: tcp_content.h:46
virtual bool Receive_CLIENT_INFO_EXTID(Packet *p)
Client requesting a list of content info based on an external &#39;unique&#39; id; GRF ID for NewGRFS...
A path without any base directory.
Definition: fileio_type.h:127
size_t Size() const
Get the size of the data as send over the network.
Definition: tcp_content.cpp:61
The content is already at the client side.
Definition: tcp_content.h:64
bool ReceiveInvalidPacket(PacketContentType type)
Helper for logging receiving invalid packets.
ContentInfo()
Clear everything in the struct.
Definition: tcp_content.cpp:30
State state
Whether the content info is selected (for download)
Definition: tcp_content.h:83
Subdirectory for all GS libraries.
Definition: fileio_type.h:124
Basic functions to receive and send TCP packets to/from the content server.
void TransferFrom(ContentInfo *other)
Copy data from other ContentInfo and take ownership of allocated stuff.
Definition: tcp_content.cpp:46
TextfileType
Additional text files accompanying Tar archives.
Definition: textfile_type.h:16
Queries the content server for a list of info of a given content type.
Definition: tcp_content.h:42
virtual bool Receive_CLIENT_INFO_LIST(Packet *p)
Client requesting a list of content info: byte type uint32 openttd version.
byte md5sum[16]
The MD5 checksum.
Definition: tcp_content.h:78
The content consists of a GS library.
Definition: tcp_content.h:36
Subdirectory for all NewGRFs.
Definition: fileio_type.h:119
The content consists of a NewGRF.
Definition: tcp_content.h:28
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:39
ContentType
The values in the enum are important; they are used as database &#39;keys&#39;.
Definition: tcp_content.h:25
Subdirectory for all AI libraries.
Definition: fileio_type.h:122
PacketContentType
Enum with all types of TCP content packets.
Definition: tcp_content.h:41
Queries the content server for information about a list of external IDs and MD5.
Definition: tcp_content.h:45
The content consists of an AI library.
Definition: tcp_content.h:30
uint8 Recv_uint8()
Read a 8 bits integer from the packet.
Definition: packet.cpp:221
static GraphicsSet * GetAvailableSets()
Return the available sets.
Request a content file given an internal ID.
Definition: tcp_content.h:47
~ContentInfo()
Free everything allocated.
Definition: tcp_content.cpp:36
The content consists of a heightmap.
Definition: tcp_content.h:32
The content consists of an AI.
Definition: tcp_content.h:29
const char * GetTextfile(TextfileType type) const
Search a textfile file next to this file in the content list.
ContentType type
Type of content.
Definition: tcp_content.h:69
Subdirectory for all AI files.
Definition: fileio_type.h:121
const char * FindMainScript(const ContentInfo *ci, bool md5sum)
Find a script of a ContentInfo.
const char * TryGetBaseSetFile(const ContentInfo *ci, bool md5sum, const Tbase_set *s)
Check whether there&#39;s a base set matching some information.
char * filename
Filename - either with or without full path.
Helper to mark the begin of the types.
Definition: tcp_content.h:26
The content consists of base music.
Definition: tcp_content.h:34
virtual bool Receive_SERVER_INFO(Packet *p)
Server sending list of content info: byte type (invalid ID == does not exist) uint32 id uint32 file_s...
static GameScannerLibrary * GetScannerLibrary()
Gets the ScriptScanner instance that is used to find Game Libraries.
Definition: game_core.cpp:275
virtual void Close()
Really close the socket.
virtual bool Receive_CLIENT_INFO_ID(Packet *p)
Client requesting a list of content info: uint16 count of ids uint32 id (count times) ...
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:114
The content has been manually selected.
Definition: tcp_content.h:62
static uint32 BSWAP32(uint32 x)
Perform a 32 bits endianness bitswap on x.
const char * GetTextfile(TextfileType type, Subdirectory dir, const char *filename)
Search a textfile file next to the given content.
Only find Grfs matching md5sum.
Subdirectory GetContentInfoSubDir(ContentType type)
Helper to get the subdirectory a ContentInfo is located in.
static AIScannerInfo * GetScannerInfo()
Gets the ScriptScanner instance that is used to find AIs.
Definition: ai_core.cpp:380
Must ALWAYS be on the end of this list!! (period)
Definition: tcp_content.h:49
virtual bool Receive_SERVER_CONTENT(Packet *p)
Server sending list of content info: uint32 unique id uint32 file size (0 == does not exist) string f...
virtual bool Receive_CLIENT_INFO_EXTID_MD5(Packet *p)
Client requesting a list of content info based on an external &#39;unique&#39; id; GRF ID + MD5 checksum for ...
Container for all important information about a piece of content.
Definition: tcp_content.h:58
bool IsValid() const
Is the information from this content info valid?
Definition: tcp_content.cpp:94
Reply with the content of the given ID.
Definition: tcp_content.h:48
The content consists of base sounds.
Definition: tcp_content.h:33
bool HandlePacket(Packet *p)
Handle the given packet, i.e.