Fix: Data races on cursor state in OpenGL backends
[openttd-github.git] / src / network / core / tcp_content.h
blobbe1cc6e77ea8202c51a0f2281c1b5dd389e26e03
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 */
8 /**
9 * @file tcp_content.h Basic functions to receive and send TCP packets to/from the content server.
12 #ifndef NETWORK_CORE_TCP_CONTENT_H
13 #define NETWORK_CORE_TCP_CONTENT_H
15 #include "os_abstraction.h"
16 #include "tcp.h"
17 #include "packet.h"
18 #include "../../debug.h"
20 /** The values in the enum are important; they are used as database 'keys' */
21 enum ContentType {
22 CONTENT_TYPE_BEGIN = 1, ///< Helper to mark the begin of the types
23 CONTENT_TYPE_BASE_GRAPHICS = 1, ///< The content consists of base graphics
24 CONTENT_TYPE_NEWGRF = 2, ///< The content consists of a NewGRF
25 CONTENT_TYPE_AI = 3, ///< The content consists of an AI
26 CONTENT_TYPE_AI_LIBRARY = 4, ///< The content consists of an AI library
27 CONTENT_TYPE_SCENARIO = 5, ///< The content consists of a scenario
28 CONTENT_TYPE_HEIGHTMAP = 6, ///< The content consists of a heightmap
29 CONTENT_TYPE_BASE_SOUNDS = 7, ///< The content consists of base sounds
30 CONTENT_TYPE_BASE_MUSIC = 8, ///< The content consists of base music
31 CONTENT_TYPE_GAME = 9, ///< The content consists of a game script
32 CONTENT_TYPE_GAME_LIBRARY = 10, ///< The content consists of a GS library
33 CONTENT_TYPE_END, ///< Helper to mark the end of the types
36 /** Enum with all types of TCP content packets. The order MUST not be changed **/
37 enum PacketContentType {
38 PACKET_CONTENT_CLIENT_INFO_LIST, ///< Queries the content server for a list of info of a given content type
39 PACKET_CONTENT_CLIENT_INFO_ID, ///< Queries the content server for information about a list of internal IDs
40 PACKET_CONTENT_CLIENT_INFO_EXTID, ///< Queries the content server for information about a list of external IDs
41 PACKET_CONTENT_CLIENT_INFO_EXTID_MD5, ///< Queries the content server for information about a list of external IDs and MD5
42 PACKET_CONTENT_SERVER_INFO, ///< Reply of content server with information about content
43 PACKET_CONTENT_CLIENT_CONTENT, ///< Request a content file given an internal ID
44 PACKET_CONTENT_SERVER_CONTENT, ///< Reply with the content of the given ID
45 PACKET_CONTENT_END, ///< Must ALWAYS be on the end of this list!! (period)
48 /** Unique identifier for the content. */
49 enum ContentID {
50 INVALID_CONTENT_ID = UINT32_MAX, ///< Sentinel for invalid content.
53 /** Container for all important information about a piece of content. */
54 struct ContentInfo {
55 /** The state the content can be in. */
56 enum State {
57 UNSELECTED, ///< The content has not been selected
58 SELECTED, ///< The content has been manually selected
59 AUTOSELECTED, ///< The content has been selected as dependency
60 ALREADY_HERE, ///< The content is already at the client side
61 DOES_NOT_EXIST, ///< The content does not exist in the content system
62 INVALID, ///< The content's invalid
65 ContentType type; ///< Type of content
66 ContentID id; ///< Unique (server side) ID for the content
67 uint32 filesize; ///< Size of the file
68 char filename[48]; ///< Filename (for the .tar.gz; only valid on download)
69 char name[32]; ///< Name of the content
70 char version[16]; ///< Version of the content
71 char url[96]; ///< URL related to the content
72 char description[512]; ///< Description of the content
73 uint32 unique_id; ///< Unique ID; either GRF ID or shortname
74 byte md5sum[16]; ///< The MD5 checksum
75 uint8 dependency_count; ///< Number of dependencies
76 ContentID *dependencies; ///< Malloced array of dependencies (unique server side ids)
77 uint8 tag_count; ///< Number of tags
78 char (*tags)[32]; ///< Malloced array of tags (strings)
79 State state; ///< Whether the content info is selected (for download)
80 bool upgrade; ///< This item is an upgrade
82 ContentInfo();
83 ~ContentInfo();
85 void TransferFrom(ContentInfo *other);
87 size_t Size() const;
88 bool IsSelected() const;
89 bool IsValid() const;
90 #ifndef OPENTTD_MSU
91 const char *GetTextfile(TextfileType type) const;
92 #endif /* OPENTTD_MSU */
95 /** Base socket handler for all Content TCP sockets */
96 class NetworkContentSocketHandler : public NetworkTCPSocketHandler {
97 protected:
98 NetworkAddress client_addr; ///< The address we're connected to.
99 void Close() override;
101 bool ReceiveInvalidPacket(PacketContentType type);
104 * Client requesting a list of content info:
105 * byte type
106 * uint32 openttd version
107 * @param p The packet that was just received.
108 * @return True upon success, otherwise false.
110 virtual bool Receive_CLIENT_INFO_LIST(Packet *p);
113 * Client requesting a list of content info:
114 * uint16 count of ids
115 * uint32 id (count times)
116 * @param p The packet that was just received.
117 * @return True upon success, otherwise false.
119 virtual bool Receive_CLIENT_INFO_ID(Packet *p);
122 * Client requesting a list of content info based on an external
123 * 'unique' id; GRF ID for NewGRFS, shortname and for base
124 * graphics and AIs.
125 * Scenarios and AI libraries are not supported
126 * uint8 count of requests
127 * for each request:
128 * uint8 type
129 * unique id (uint32)
130 * @param p The packet that was just received.
131 * @return True upon success, otherwise false.
133 virtual bool Receive_CLIENT_INFO_EXTID(Packet *p);
136 * Client requesting a list of content info based on an external
137 * 'unique' id; GRF ID + MD5 checksum for NewGRFS, shortname and
138 * xor-ed MD5 checksums for base graphics and AIs.
139 * Scenarios and AI libraries are not supported
140 * uint8 count of requests
141 * for each request:
142 * uint8 type
143 * unique id (uint32)
144 * md5 (16 bytes)
145 * @param p The packet that was just received.
146 * @return True upon success, otherwise false.
148 virtual bool Receive_CLIENT_INFO_EXTID_MD5(Packet *p);
151 * Server sending list of content info:
152 * byte type (invalid ID == does not exist)
153 * uint32 id
154 * uint32 file_size
155 * string name (max 32 characters)
156 * string version (max 16 characters)
157 * uint32 unique id
158 * uint8 md5sum (16 bytes)
159 * uint8 dependency count
160 * uint32 unique id of dependency (dependency count times)
161 * uint8 tag count
162 * string tag (max 32 characters for tag count times)
163 * @param p The packet that was just received.
164 * @return True upon success, otherwise false.
166 virtual bool Receive_SERVER_INFO(Packet *p);
169 * Client requesting the actual content:
170 * uint16 count of unique ids
171 * uint32 unique id (count times)
172 * @param p The packet that was just received.
173 * @return True upon success, otherwise false.
175 virtual bool Receive_CLIENT_CONTENT(Packet *p);
178 * Server sending list of content info:
179 * uint32 unique id
180 * uint32 file size (0 == does not exist)
181 * string file name (max 48 characters)
182 * After this initial packet, packets with the actual data are send using
183 * the same packet type.
184 * @param p The packet that was just received.
185 * @return True upon success, otherwise false.
187 virtual bool Receive_SERVER_CONTENT(Packet *p);
189 bool HandlePacket(Packet *p);
190 public:
192 * Create a new cs socket handler for a given cs
193 * @param s the socket we are connected with
194 * @param address IP etc. of the client
196 NetworkContentSocketHandler(SOCKET s = INVALID_SOCKET, const NetworkAddress &address = NetworkAddress()) :
197 NetworkTCPSocketHandler(s),
198 client_addr(address)
202 /** On destructing of this class, the socket needs to be closed */
203 virtual ~NetworkContentSocketHandler() { this->Close(); }
205 bool ReceivePackets();
208 #ifndef OPENTTD_MSU
209 Subdirectory GetContentInfoSubDir(ContentType type);
210 #endif /* OPENTTD_MSU */
212 #endif /* NETWORK_CORE_TCP_CONTENT_H */