(svn r27729) -Codechange: Do not count static NewGRF when checking for the maximum...
[openttd.git] / src / network / core / tcp_content.h
bloba506439da945c7e883cdb5b1eecfed6679b8c2ad
1 /* $Id$ */
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 */
10 /**
11 * @file tcp_content.h Basic functions to receive and send TCP packets to/from the content server.
14 #ifndef NETWORK_CORE_TCP_CONTENT_H
15 #define NETWORK_CORE_TCP_CONTENT_H
17 #include "os_abstraction.h"
18 #include "tcp.h"
19 #include "packet.h"
20 #include "../../debug.h"
22 #ifdef ENABLE_NETWORK
24 /** The values in the enum are important; they are used as database 'keys' */
25 enum ContentType {
26 CONTENT_TYPE_BEGIN = 1, ///< Helper to mark the begin of the types
27 CONTENT_TYPE_BASE_GRAPHICS = 1, ///< The content consists of base graphics
28 CONTENT_TYPE_NEWGRF = 2, ///< The content consists of a NewGRF
29 CONTENT_TYPE_AI = 3, ///< The content consists of an AI
30 CONTENT_TYPE_AI_LIBRARY = 4, ///< The content consists of an AI library
31 CONTENT_TYPE_SCENARIO = 5, ///< The content consists of a scenario
32 CONTENT_TYPE_HEIGHTMAP = 6, ///< The content consists of a heightmap
33 CONTENT_TYPE_BASE_SOUNDS = 7, ///< The content consists of base sounds
34 CONTENT_TYPE_BASE_MUSIC = 8, ///< The content consists of base music
35 CONTENT_TYPE_GAME = 9, ///< The content consists of a game script
36 CONTENT_TYPE_GAME_LIBRARY = 10, ///< The content consists of a GS library
37 CONTENT_TYPE_END, ///< Helper to mark the end of the types
40 /** Enum with all types of TCP content packets. The order MUST not be changed **/
41 enum PacketContentType {
42 PACKET_CONTENT_CLIENT_INFO_LIST, ///< Queries the content server for a list of info of a given content type
43 PACKET_CONTENT_CLIENT_INFO_ID, ///< Queries the content server for information about a list of internal IDs
44 PACKET_CONTENT_CLIENT_INFO_EXTID, ///< Queries the content server for information about a list of external IDs
45 PACKET_CONTENT_CLIENT_INFO_EXTID_MD5, ///< Queries the content server for information about a list of external IDs and MD5
46 PACKET_CONTENT_SERVER_INFO, ///< Reply of content server with information about content
47 PACKET_CONTENT_CLIENT_CONTENT, ///< Request a content file given an internal ID
48 PACKET_CONTENT_SERVER_CONTENT, ///< Reply with the content of the given ID
49 PACKET_CONTENT_END, ///< Must ALWAYS be on the end of this list!! (period)
52 /** Unique identifier for the content. */
53 enum ContentID {
54 INVALID_CONTENT_ID = UINT32_MAX, ///< Sentinel for invalid content.
57 /** Container for all important information about a piece of content. */
58 struct ContentInfo {
59 /** The state the content can be in. */
60 enum State {
61 UNSELECTED, ///< The content has not been selected
62 SELECTED, ///< The content has been manually selected
63 AUTOSELECTED, ///< The content has been selected as dependency
64 ALREADY_HERE, ///< The content is already at the client side
65 DOES_NOT_EXIST, ///< The content does not exist in the content system
66 INVALID, ///< The content's invalid
69 ContentType type; ///< Type of content
70 ContentID id; ///< Unique (server side) ID for the content
71 uint32 filesize; ///< Size of the file
72 char filename[48]; ///< Filename (for the .tar.gz; only valid on download)
73 char name[32]; ///< Name of the content
74 char version[16]; ///< Version of the content
75 char url[96]; ///< URL related to the content
76 char description[512]; ///< Description of the content
77 uint32 unique_id; ///< Unique ID; either GRF ID or shortname
78 byte md5sum[16]; ///< The MD5 checksum
79 uint8 dependency_count; ///< Number of dependencies
80 ContentID *dependencies; ///< Malloced array of dependencies (unique server side ids)
81 uint8 tag_count; ///< Number of tags
82 char (*tags)[32]; ///< Malloced array of tags (strings)
83 State state; ///< Whether the content info is selected (for download)
84 bool upgrade; ///< This item is an upgrade
86 ContentInfo();
87 ~ContentInfo();
89 void TransferFrom(ContentInfo *other);
91 size_t Size() const;
92 bool IsSelected() const;
93 bool IsValid() const;
94 #ifndef OPENTTD_MSU
95 const char *GetTextfile(TextfileType type) const;
96 #endif /* OPENTTD_MSU */
99 /** Base socket handler for all Content TCP sockets */
100 class NetworkContentSocketHandler : public NetworkTCPSocketHandler {
101 protected:
102 NetworkAddress client_addr; ///< The address we're connected to.
103 virtual void Close();
105 bool ReceiveInvalidPacket(PacketContentType type);
108 * Client requesting a list of content info:
109 * byte type
110 * uint32 openttd version
111 * @param p The packet that was just received.
112 * @return True upon success, otherwise false.
114 virtual bool Receive_CLIENT_INFO_LIST(Packet *p);
117 * Client requesting a list of content info:
118 * uint16 count of ids
119 * uint32 id (count times)
120 * @param p The packet that was just received.
121 * @return True upon success, otherwise false.
123 virtual bool Receive_CLIENT_INFO_ID(Packet *p);
126 * Client requesting a list of content info based on an external
127 * 'unique' id; GRF ID for NewGRFS, shortname and for base
128 * graphics and AIs.
129 * Scenarios and AI libraries are not supported
130 * uint8 count of requests
131 * for each request:
132 * uint8 type
133 * unique id (uint32)
134 * @param p The packet that was just received.
135 * @return True upon success, otherwise false.
137 virtual bool Receive_CLIENT_INFO_EXTID(Packet *p);
140 * Client requesting a list of content info based on an external
141 * 'unique' id; GRF ID + MD5 checksum for NewGRFS, shortname and
142 * xor-ed MD5 checksums for base graphics and AIs.
143 * Scenarios and AI libraries are not supported
144 * uint8 count of requests
145 * for each request:
146 * uint8 type
147 * unique id (uint32)
148 * md5 (16 bytes)
149 * @param p The packet that was just received.
150 * @return True upon success, otherwise false.
152 virtual bool Receive_CLIENT_INFO_EXTID_MD5(Packet *p);
155 * Server sending list of content info:
156 * byte type (invalid ID == does not exist)
157 * uint32 id
158 * uint32 file_size
159 * string name (max 32 characters)
160 * string version (max 16 characters)
161 * uint32 unique id
162 * uint8 md5sum (16 bytes)
163 * uint8 dependency count
164 * uint32 unique id of dependency (dependency count times)
165 * uint8 tag count
166 * string tag (max 32 characters for tag count times)
167 * @param p The packet that was just received.
168 * @return True upon success, otherwise false.
170 virtual bool Receive_SERVER_INFO(Packet *p);
173 * Client requesting the actual content:
174 * uint16 count of unique ids
175 * uint32 unique id (count times)
176 * @param p The packet that was just received.
177 * @return True upon success, otherwise false.
179 virtual bool Receive_CLIENT_CONTENT(Packet *p);
182 * Server sending list of content info:
183 * uint32 unique id
184 * uint32 file size (0 == does not exist)
185 * string file name (max 48 characters)
186 * After this initial packet, packets with the actual data are send using
187 * the same packet type.
188 * @param p The packet that was just received.
189 * @return True upon success, otherwise false.
191 virtual bool Receive_SERVER_CONTENT(Packet *p);
193 bool HandlePacket(Packet *p);
194 public:
196 * Create a new cs socket handler for a given cs
197 * @param s the socket we are connected with
198 * @param address IP etc. of the client
200 NetworkContentSocketHandler(SOCKET s = INVALID_SOCKET, const NetworkAddress &address = NetworkAddress()) :
201 NetworkTCPSocketHandler(s),
202 client_addr(address)
206 /** On destructing of this class, the socket needs to be closed */
207 virtual ~NetworkContentSocketHandler() { this->Close(); }
209 bool ReceivePackets();
212 #ifndef OPENTTD_MSU
213 Subdirectory GetContentInfoSubDir(ContentType type);
214 #endif /* OPENTTD_MSU */
216 #endif /* ENABLE_NETWORK */
218 #endif /* NETWORK_CORE_TCP_CONTENT_H */