# udp.h

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00071 #ifndef NETWORK_CORE_UDP_H
    00072 #define NETWORK_CORE_UDP_H
    00073
    00074 #include "os_abstraction.h"
    00075 #include "address.h"
    00076 #include "core.h"
    00077 #include "game.h"
    00078 #include "packet.h"
    00079 #include "../../debug.h"
    00080
    00081 #ifdef ENABLE_NETWORK
    00082
    00084 enum PacketUDPType {
    00085   PACKET_UDP_CLIENT_FIND_SERVER,
    00086   PACKET_UDP_SERVER_RESPONSE,
    00087   PACKET_UDP_CLIENT_DETAIL_INFO,
    00088   PACKET_UDP_SERVER_DETAIL_INFO,
    00089   PACKET_UDP_SERVER_REGISTER,
    00090   PACKET_UDP_MASTER_ACK_REGISTER,
    00091   PACKET_UDP_CLIENT_GET_LIST,
    00092   PACKET_UDP_MASTER_RESPONSE_LIST,
    00093   PACKET_UDP_SERVER_UNREGISTER,
    00094   PACKET_UDP_CLIENT_GET_NEWGRFS,
    00095   PACKET_UDP_SERVER_NEWGRFS,
    00096   PACKET_UDP_MASTER_SESSION_KEY,
    00097   PACKET_UDP_END
    00098 };
    00099
    00101 enum ServerListType {
    00102   SLT_IPv4 = 0,
    00103   SLT_IPv6 = 1,
    00104   SLT_AUTODETECT,
    00105
    00106   SLT_END = SLT_AUTODETECT
    00107 };
    00108
    00109 #define DECLARE_UDP_RECEIVE_COMMAND(type) virtual void NetworkPacketReceive_## type ##_command(Packet *p, NetworkAddress *client_addr)
    00110 #define DEF_UDP_RECEIVE_COMMAND(cls, type) void cls ##NetworkUDPSocketHandler::NetworkPacketReceive_ ## type ## _command(Packet *p, NetworkAddress *client_addr)
    00111
    00113 class NetworkUDPSocketHandler : public NetworkSocketHandler {
    00114 protected:
    00116   NetworkAddressList bind;
    00118   SocketList sockets;
    00119
    00120   NetworkRecvStatus CloseConnection(bool error = true);
    00121
    00122   /* Declare all possible packets here. If it can be received by the
    00123    * a specific handler, it has to be implemented. */
    00124   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER);
    00125   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_RESPONSE);
    00126   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_DETAIL_INFO);
    00127   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_DETAIL_INFO);
    00128   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_REGISTER);
    00129   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_ACK_REGISTER);
    00130   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_GET_LIST);
    00131   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_RESPONSE_LIST);
    00132   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_UNREGISTER);
    00133   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_GET_NEWGRFS);
    00134   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_NEWGRFS);
    00135   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_SESSION_KEY);
    00136
    00137   void HandleUDPPacket(Packet *p, NetworkAddress *client_addr);
    00138
    00146   virtual void HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config) { NOT_REACHED(); }
    00147 public:
    00148   NetworkUDPSocketHandler(NetworkAddressList *bind = NULL);
    00149
    00151   virtual ~NetworkUDPSocketHandler() { this->Close(); }
    00152
    00153   bool Listen();
    00154   void Close();
    00155
    00156   void SendPacket(Packet *p, NetworkAddress *recv, bool all = false, bool broadcast = false);
    00157   void ReceivePackets();
    00158
    00159   void Send_NetworkGameInfo(Packet *p, const NetworkGameInfo *info);
    00160   void Recv_NetworkGameInfo(Packet *p, NetworkGameInfo *info);
    00161 };
    00162
    00163 #endif /* ENABLE_NETWORK */
    00164
    00165 #endif /* NETWORK_CORE_UDP_H */