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 core/udp.h Basic functions to receive and send UDP packets. 10 */ 11 12 #ifndef NETWORK_CORE_UDP_H 13 #define NETWORK_CORE_UDP_H 14 15 #include "address.h" 16 #include "packet.h" 17 18 /** Enum with all types of UDP packets. The order MUST not be changed **/ 19 enum PacketUDPType { 20 PACKET_UDP_CLIENT_FIND_SERVER, ///< Queries a game server for game information 21 PACKET_UDP_SERVER_RESPONSE, ///< Reply of the game server with game information 22 PACKET_UDP_END, ///< Must ALWAYS be on the end of this list!! (period) 23 }; 24 25 /** Base socket handler for all UDP sockets */ 26 class NetworkUDPSocketHandler : public NetworkSocketHandler { 27 protected: 28 /** The address to bind to. */ 29 NetworkAddressList bind; 30 /** The opened sockets. */ 31 SocketList sockets; 32 33 void ReceiveInvalidPacket(PacketUDPType, NetworkAddress *client_addr); 34 35 /** 36 * Queries to the server for information about the game. 37 * @param p The received packet. 38 * @param client_addr The origin of the packet. 39 */ 40 virtual void Receive_CLIENT_FIND_SERVER(Packet *p, NetworkAddress *client_addr); 41 42 /** 43 * Response to a query letting the client know we are here. 44 * @param p The received packet. 45 * @param client_addr The origin of the packet. 46 */ 47 virtual void Receive_SERVER_RESPONSE(Packet *p, NetworkAddress *client_addr); 48 49 void HandleUDPPacket(Packet *p, NetworkAddress *client_addr); 50 public: 51 NetworkUDPSocketHandler(NetworkAddressList *bind = nullptr); 52 53 /** On destructing of this class, the socket needs to be closed */ ~NetworkUDPSocketHandler()54 virtual ~NetworkUDPSocketHandler() { this->CloseSocket(); } 55 56 bool Listen(); 57 void CloseSocket(); 58 59 void SendPacket(Packet *p, NetworkAddress *recv, bool all = false, bool broadcast = false); 60 void ReceivePackets(); 61 }; 62 63 #endif /* NETWORK_CORE_UDP_H */ 64