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/>.
11 * @file core/udp.h Basic functions to receive and send UDP packets.
14 #ifndef NETWORK_CORE_UDP_H
15 #define NETWORK_CORE_UDP_H
23 /** Enum with all types of UDP packets. The order MUST not be changed **/
25 PACKET_UDP_CLIENT_FIND_SERVER
, ///< Queries a game server for game information
26 PACKET_UDP_SERVER_RESPONSE
, ///< Reply of the game server with game information
27 PACKET_UDP_CLIENT_DETAIL_INFO
, ///< Queries a game server about details of the game, such as companies
28 PACKET_UDP_SERVER_DETAIL_INFO
, ///< Reply of the game server about details of the game, such as companies
29 PACKET_UDP_SERVER_REGISTER
, ///< Packet to register itself to the master server
30 PACKET_UDP_MASTER_ACK_REGISTER
, ///< Packet indicating registration has succeeded
31 PACKET_UDP_CLIENT_GET_LIST
, ///< Request for serverlist from master server
32 PACKET_UDP_MASTER_RESPONSE_LIST
, ///< Response from master server with server ip's + port's
33 PACKET_UDP_SERVER_UNREGISTER
, ///< Request to be removed from the server-list
34 PACKET_UDP_CLIENT_GET_NEWGRFS
, ///< Requests the name for a list of GRFs (GRF_ID and MD5)
35 PACKET_UDP_SERVER_NEWGRFS
, ///< Sends the list of NewGRF's requested.
36 PACKET_UDP_MASTER_SESSION_KEY
, ///< Sends a fresh session key to the client
37 PACKET_UDP_END
, ///< Must ALWAYS be on the end of this list!! (period)
40 /** The types of server lists we can get */
42 SLT_IPv4
= 0, ///< Get the IPv4 addresses
43 SLT_IPv6
= 1, ///< Get the IPv6 addresses
44 SLT_AUTODETECT
, ///< Autodetect the type based on the connection
46 SLT_END
= SLT_AUTODETECT
, ///< End of 'arrays' marker
49 /** Base socket handler for all UDP sockets */
50 class NetworkUDPSocketHandler
: public NetworkSocketHandler
{
52 /** The address to bind to. */
53 NetworkAddressList bind
;
54 /** The opened sockets. */
57 NetworkRecvStatus
CloseConnection(bool error
= true);
59 void ReceiveInvalidPacket(PacketUDPType
, NetworkAddress
*client_addr
);
62 * Queries to the server for information about the game.
63 * @param p The received packet.
64 * @param client_addr The origin of the packet.
66 virtual void Receive_CLIENT_FIND_SERVER(Packet
*p
, NetworkAddress
*client_addr
);
69 * Return of server information to the client.
70 * This packet has several legacy versions, so we list the version and size of each "field":
72 * Version: Bytes: Description:
73 * all 1 the version of this packet's structure
75 * 4+ 1 number of GRFs attached (n)
76 * 4+ n * 20 unique identifier for GRF files. Consists of:
77 * - one 4 byte variable with the GRF ID
78 * - 16 bytes (sent sequentially) for the MD5 checksum
81 * 3+ 4 current game date in days since 1-1-0 (DMY)
82 * 3+ 4 game introduction date in days since 1-1-0 (DMY)
84 * 2+ 1 maximum number of companies allowed on the server
85 * 2+ 1 number of companies on the server
86 * 2+ 1 maximum number of spectators allowed on the server
88 * 1+ var string with the name of the server
89 * 1+ var string with the revision of the server
90 * 1+ 1 the language run on the server
91 * (0 = any, 1 = English, 2 = German, 3 = French)
92 * 1+ 1 whether the server uses a password (0 = no, 1 = yes)
93 * 1+ 1 maximum number of clients allowed on the server
94 * 1+ 1 number of clients on the server
95 * 1+ 1 number of spectators on the server
96 * 1 & 2 2 current game date in days since 1-1-1920 (DMY)
97 * 1 & 2 2 game introduction date in days since 1-1-1920 (DMY)
98 * 1+ var string with the name of the map
99 * 1+ 2 width of the map in tiles
100 * 1+ 2 height of the map in tiles
102 * (0 = temperate, 1 = arctic, 2 = desert, 3 = toyland)
103 * 1+ 1 whether the server is dedicated (0 = no, 1 = yes)
104 * @param p The received packet.
105 * @param client_addr The origin of the packet.
107 virtual void Receive_SERVER_RESPONSE(Packet
*p
, NetworkAddress
*client_addr
);
110 * Query for detailed information about companies.
111 * @param p The received packet.
112 * @param client_addr The origin of the packet.
114 virtual void Receive_CLIENT_DETAIL_INFO(Packet
*p
, NetworkAddress
*client_addr
);
117 * Reply with detailed company information.
118 * uint8 Version of the packet.
119 * uint8 Number of companies.
121 * uint8 ID of the company.
122 * string Name of the company.
123 * uint32 Year the company was inaugurated.
127 * uint16 Performance (last quarter).
128 * bool Company is password protected.
129 * uint16 Number of trains.
130 * uint16 Number of lorries.
131 * uint16 Number of busses.
132 * uint16 Number of planes.
133 * uint16 Number of ships.
134 * uint16 Number of train stations.
135 * uint16 Number of lorry stations.
136 * uint16 Number of bus stops.
137 * uint16 Number of airports and heliports.
138 * uint16 Number of harbours.
139 * bool Company is an AI.
140 * @param p The received packet.
141 * @param client_addr The origin of the packet.
143 virtual void Receive_SERVER_DETAIL_INFO(Packet
*p
, NetworkAddress
*client_addr
);
146 * Registers the server to the master server.
147 * string The "welcome" message to root out other binary packets.
148 * uint8 Version of the protocol.
149 * uint16 The port to unregister.
150 * uint64 The session key.
151 * @param p The received packet.
152 * @param client_addr The origin of the packet.
154 virtual void Receive_SERVER_REGISTER(Packet
*p
, NetworkAddress
*client_addr
);
157 * The master server acknowledges the registration.
158 * @param p The received packet.
159 * @param client_addr The origin of the packet.
161 virtual void Receive_MASTER_ACK_REGISTER(Packet
*p
, NetworkAddress
*client_addr
);
164 * The client requests a list of servers.
165 * uint8 The protocol version.
166 * uint8 The type of server to look for: IPv4, IPv6 or based on the received packet.
167 * @param p The received packet.
168 * @param client_addr The origin of the packet.
170 virtual void Receive_CLIENT_GET_LIST(Packet
*p
, NetworkAddress
*client_addr
);
173 * The server sends a list of servers.
174 * uint8 The protocol version.
176 * 4 or 16 bytes of IPv4 or IPv6 address.
178 * @param p The received packet.
179 * @param client_addr The origin of the packet.
181 virtual void Receive_MASTER_RESPONSE_LIST(Packet
*p
, NetworkAddress
*client_addr
);
184 * A server unregisters itself at the master server.
185 * uint8 Version of the protocol.
186 * uint16 The port to unregister.
187 * @param p The received packet.
188 * @param client_addr The origin of the packet.
190 virtual void Receive_SERVER_UNREGISTER(Packet
*p
, NetworkAddress
*client_addr
);
193 * The client requests information about some NewGRFs.
194 * uint8 The number of NewGRFs information is requested about.
197 * 16 * uint8 MD5 checksum of the GRF.
198 * @param p The received packet.
199 * @param client_addr The origin of the packet.
201 virtual void Receive_CLIENT_GET_NEWGRFS(Packet
*p
, NetworkAddress
*client_addr
);
204 * The server returns information about some NewGRFs.
205 * uint8 The number of NewGRFs information is requested about.
208 * 16 * uint8 MD5 checksum of the GRF.
209 * string The name of the NewGRF.
210 * @param p The received packet.
211 * @param client_addr The origin of the packet.
213 virtual void Receive_SERVER_NEWGRFS(Packet
*p
, NetworkAddress
*client_addr
);
216 * The master server sends us a session key.
217 * uint64 The session key.
218 * @param p The received packet.
219 * @param client_addr The origin of the packet.
221 virtual void Receive_MASTER_SESSION_KEY(Packet
*p
, NetworkAddress
*client_addr
);
223 void HandleUDPPacket(Packet
*p
, NetworkAddress
*client_addr
);
226 * Function that is called for every GRFConfig that is read when receiving
227 * a NetworkGameInfo. Only grfid and md5sum are set, the rest is zero. This
228 * function must set all appropriate fields. This GRF is later appended to
229 * the grfconfig list of the NetworkGameInfo.
230 * @param config the GRF to handle
232 virtual void HandleIncomingNetworkGameInfoGRFConfig(GRFConfig
*config
) { NOT_REACHED(); }
234 NetworkUDPSocketHandler(NetworkAddressList
*bind
= NULL
);
236 /** On destructing of this class, the socket needs to be closed */
237 virtual ~NetworkUDPSocketHandler() { this->Close(); }
242 void SendPacket(Packet
*p
, NetworkAddress
*recv
, bool all
= false, bool broadcast
= false);
243 void ReceivePackets();
245 void SendNetworkGameInfo(Packet
*p
, const NetworkGameInfo
*info
);
246 void ReceiveNetworkGameInfo(Packet
*p
, NetworkGameInfo
*info
);
249 #endif /* ENABLE_NETWORK */
251 #endif /* NETWORK_CORE_UDP_H */