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 
8 /**
9  * @file tcp_content.h Basic functions to receive and send TCP packets to/from the content server.
10  */
11 
12 #ifndef NETWORK_CORE_TCP_CONTENT_H
13 #define NETWORK_CORE_TCP_CONTENT_H
14 
15 #include "os_abstraction.h"
16 #include "tcp.h"
17 #include "packet.h"
18 #include "../../debug.h"
19 #include "tcp_content_type.h"
20 
21 /** Base socket handler for all Content TCP sockets */
22 class NetworkContentSocketHandler : public NetworkTCPSocketHandler {
23 protected:
24 	bool ReceiveInvalidPacket(PacketContentType type);
25 
26 	/**
27 	 * Client requesting a list of content info:
28 	 *  byte    type
29 	 *  uint32  openttd version (or 0xFFFFFFFF if using a list)
30 	 * Only if the above value is 0xFFFFFFFF:
31 	 *  uint8   count
32 	 *  string  branch-name ("vanilla" for upstream OpenTTD)
33 	 *  string  release version (like "12.0")
34 	 * @param p The packet that was just received.
35 	 * @return True upon success, otherwise false.
36 	 */
37 	virtual bool Receive_CLIENT_INFO_LIST(Packet *p);
38 
39 	/**
40 	 * Client requesting a list of content info:
41 	 *  uint16  count of ids
42 	 *  uint32  id (count times)
43 	 * @param p The packet that was just received.
44 	 * @return True upon success, otherwise false.
45 	 */
46 	virtual bool Receive_CLIENT_INFO_ID(Packet *p);
47 
48 	/**
49 	 * Client requesting a list of content info based on an external
50 	 * 'unique' id; GRF ID for NewGRFS, shortname and for base
51 	 * graphics and AIs.
52 	 * Scenarios and AI libraries are not supported
53 	 *  uint8   count of requests
54 	 *  for each request:
55 	 *    uint8 type
56 	 *    unique id (uint32)
57 	 * @param p The packet that was just received.
58 	 * @return True upon success, otherwise false.
59 	 */
60 	virtual bool Receive_CLIENT_INFO_EXTID(Packet *p);
61 
62 	/**
63 	 * Client requesting a list of content info based on an external
64 	 * 'unique' id; GRF ID + MD5 checksum for NewGRFS, shortname and
65 	 * xor-ed MD5 checksums for base graphics and AIs.
66 	 * Scenarios and AI libraries are not supported
67 	 *  uint8   count of requests
68 	 *  for each request:
69 	 *    uint8 type
70 	 *    unique id (uint32)
71 	 *    md5 (16 bytes)
72 	 * @param p The packet that was just received.
73 	 * @return True upon success, otherwise false.
74 	 */
75 	virtual bool Receive_CLIENT_INFO_EXTID_MD5(Packet *p);
76 
77 	/**
78 	 * Server sending list of content info:
79 	 *  byte    type (invalid ID == does not exist)
80 	 *  uint32  id
81 	 *  uint32  file_size
82 	 *  string  name (max 32 characters)
83 	 *  string  version (max 16 characters)
84 	 *  uint32  unique id
85 	 *  uint8   md5sum (16 bytes)
86 	 *  uint8   dependency count
87 	 *  uint32  unique id of dependency (dependency count times)
88 	 *  uint8   tag count
89 	 *  string  tag (max 32 characters for tag count times)
90 	 * @param p The packet that was just received.
91 	 * @return True upon success, otherwise false.
92 	 */
93 	virtual bool Receive_SERVER_INFO(Packet *p);
94 
95 	/**
96 	 * Client requesting the actual content:
97 	 *  uint16  count of unique ids
98 	 *  uint32  unique id (count times)
99 	 * @param p The packet that was just received.
100 	 * @return True upon success, otherwise false.
101 	 */
102 	virtual bool Receive_CLIENT_CONTENT(Packet *p);
103 
104 	/**
105 	 * Server sending list of content info:
106 	 *  uint32  unique id
107 	 *  uint32  file size (0 == does not exist)
108 	 *  string  file name (max 48 characters)
109 	 * After this initial packet, packets with the actual data are send using
110 	 * the same packet type.
111 	 * @param p The packet that was just received.
112 	 * @return True upon success, otherwise false.
113 	 */
114 	virtual bool Receive_SERVER_CONTENT(Packet *p);
115 
116 	bool HandlePacket(Packet *p);
117 public:
118 	/**
119 	 * Create a new cs socket handler for a given cs
120 	 * @param s  the socket we are connected with
121 	 * @param address IP etc. of the client
122 	 */
123 	NetworkContentSocketHandler(SOCKET s = INVALID_SOCKET) :
NetworkTCPSocketHandler(s)124 		NetworkTCPSocketHandler(s)
125 	{
126 	}
127 
128 	/** On destructing of this class, the socket needs to be closed */
~NetworkContentSocketHandler()129 	virtual ~NetworkContentSocketHandler()
130 	{
131 		/* Virtual functions get called statically in destructors, so make it explicit to remove any confusion. */
132 		this->CloseSocket();
133 	}
134 
135 	bool ReceivePackets();
136 };
137 
138 Subdirectory GetContentInfoSubDir(ContentType type);
139 
140 #endif /* NETWORK_CORE_TCP_CONTENT_H */
141