2009-08-21 22:21:05 +02:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2007-01-02 18:34:03 +01:00
|
|
|
/**
|
2011-05-01 14:18:34 +02:00
|
|
|
* @file core/udp.h Basic functions to receive and send UDP packets.
|
2007-01-02 18:34:03 +01:00
|
|
|
*/
|
|
|
|
|
2007-02-23 09:37:33 +01:00
|
|
|
#ifndef NETWORK_CORE_UDP_H
|
|
|
|
#define NETWORK_CORE_UDP_H
|
|
|
|
|
2009-04-02 21:21:26 +02:00
|
|
|
#include "address.h"
|
2007-02-23 09:37:33 +01:00
|
|
|
#include "packet.h"
|
|
|
|
|
2007-01-02 18:34:03 +01:00
|
|
|
/** Enum with all types of UDP packets. The order MUST not be changed **/
|
2024-02-04 12:31:56 +01:00
|
|
|
enum PacketUDPType : uint8_t {
|
2007-01-02 18:34:03 +01:00
|
|
|
PACKET_UDP_CLIENT_FIND_SERVER, ///< Queries a game server for game information
|
|
|
|
PACKET_UDP_SERVER_RESPONSE, ///< Reply of the game server with game information
|
2011-12-19 18:48:04 +01:00
|
|
|
PACKET_UDP_END, ///< Must ALWAYS be on the end of this list!! (period)
|
2007-01-02 18:34:03 +01:00
|
|
|
};
|
|
|
|
|
2007-01-12 21:19:49 +01:00
|
|
|
/** Base socket handler for all UDP sockets */
|
|
|
|
class NetworkUDPSocketHandler : public NetworkSocketHandler {
|
2007-01-12 15:30:01 +01:00
|
|
|
protected:
|
2009-04-07 22:27:13 +02:00
|
|
|
/** The address to bind to. */
|
|
|
|
NetworkAddressList bind;
|
|
|
|
/** The opened sockets. */
|
|
|
|
SocketList sockets;
|
|
|
|
|
2024-02-03 19:43:38 +01:00
|
|
|
void ReceiveInvalidPacket(PacketUDPType, NetworkAddress &client_addr);
|
2011-05-01 14:18:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Queries to the server for information about the game.
|
|
|
|
* @param p The received packet.
|
|
|
|
* @param client_addr The origin of the packet.
|
|
|
|
*/
|
2024-02-03 19:43:38 +01:00
|
|
|
virtual void Receive_CLIENT_FIND_SERVER(Packet &p, NetworkAddress &client_addr);
|
2011-05-01 14:18:34 +02:00
|
|
|
|
|
|
|
/**
|
2021-05-06 13:12:11 +02:00
|
|
|
* Response to a query letting the client know we are here.
|
2011-05-01 14:18:34 +02:00
|
|
|
* @param p The received packet.
|
|
|
|
* @param client_addr The origin of the packet.
|
|
|
|
*/
|
2024-02-03 19:43:38 +01:00
|
|
|
virtual void Receive_SERVER_RESPONSE(Packet &p, NetworkAddress &client_addr);
|
2011-05-01 14:18:34 +02:00
|
|
|
|
2024-02-03 19:43:38 +01:00
|
|
|
void HandleUDPPacket(Packet &p, NetworkAddress &client_addr);
|
2007-01-12 15:30:01 +01:00
|
|
|
public:
|
2019-04-10 23:07:06 +02:00
|
|
|
NetworkUDPSocketHandler(NetworkAddressList *bind = nullptr);
|
2009-04-07 22:27:13 +02:00
|
|
|
|
2007-01-28 21:47:25 +01:00
|
|
|
/** On destructing of this class, the socket needs to be closed */
|
2021-05-13 11:46:51 +02:00
|
|
|
virtual ~NetworkUDPSocketHandler() { this->CloseSocket(); }
|
2007-01-12 15:30:01 +01:00
|
|
|
|
2009-04-07 22:27:13 +02:00
|
|
|
bool Listen();
|
2021-05-13 11:46:51 +02:00
|
|
|
void CloseSocket();
|
2007-01-12 15:30:01 +01:00
|
|
|
|
2024-02-03 19:43:38 +01:00
|
|
|
void SendPacket(Packet &p, NetworkAddress &recv, bool all = false, bool broadcast = false);
|
2007-01-12 15:30:01 +01:00
|
|
|
void ReceivePackets();
|
|
|
|
};
|
2007-01-02 18:34:03 +01:00
|
|
|
|
|
|
|
#endif /* NETWORK_CORE_UDP_H */
|