# udp.h

Go to the documentation of this file.

    00001 /* $Id: udp.h 18809 2010-01-15 16:41:15Z rubidium $ */
    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 "address.h"
    00075 #include "game.h"
    00076 #include "packet.h"
    00077
    00078 #ifdef ENABLE_NETWORK
    00079
    00081 enum PacketUDPType {
    00082   PACKET_UDP_CLIENT_FIND_SERVER,
    00083   PACKET_UDP_SERVER_RESPONSE,
    00084   PACKET_UDP_CLIENT_DETAIL_INFO,
    00085   PACKET_UDP_SERVER_DETAIL_INFO,
    00086   PACKET_UDP_SERVER_REGISTER,
    00087   PACKET_UDP_MASTER_ACK_REGISTER,
    00088   PACKET_UDP_CLIENT_GET_LIST,
    00089   PACKET_UDP_MASTER_RESPONSE_LIST,
    00090   PACKET_UDP_SERVER_UNREGISTER,
    00091   PACKET_UDP_CLIENT_GET_NEWGRFS,
    00092   PACKET_UDP_SERVER_NEWGRFS,
    00093   PACKET_UDP_MASTER_SESSION_KEY,
    00094   PACKET_UDP_END
    00095 };
    00096
    00098 enum ServerListType {
    00099   SLT_IPv4 = 0,
    00100   SLT_IPv6 = 1,
    00101   SLT_AUTODETECT,
    00102
    00103   SLT_END = SLT_AUTODETECT
    00104 };
    00105
    00106 #define DECLARE_UDP_RECEIVE_COMMAND(type) virtual void NetworkPacketReceive_## type ##_command(Packet *p, NetworkAddress *client_addr)
    00107 #define DEF_UDP_RECEIVE_COMMAND(cls, type) void cls ##NetworkUDPSocketHandler::NetworkPacketReceive_ ## type ## _command(Packet *p, NetworkAddress *client_addr)
    00108
    00110 class NetworkUDPSocketHandler : public NetworkSocketHandler {
    00111 protected:
    00113   NetworkAddressList bind;
    00115   SocketList sockets;
    00116
    00117   NetworkRecvStatus CloseConnection(bool error = true);
    00118
    00119   /* Declare all possible packets here. If it can be received by the
    00120    * a specific handler, it has to be implemented. */
    00121   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER);
    00122   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_RESPONSE);
    00123   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_DETAIL_INFO);
    00124   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_DETAIL_INFO);
    00125   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_REGISTER);
    00126   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_ACK_REGISTER);
    00127   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_GET_LIST);
    00128   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_RESPONSE_LIST);
    00129   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_UNREGISTER);
    00130   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_GET_NEWGRFS);
    00131   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_NEWGRFS);
    00132   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_SESSION_KEY);
    00133
    00134   void HandleUDPPacket(Packet *p, NetworkAddress *client_addr);
    00135
    00143   virtual void HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config) { NOT_REACHED(); }
    00144 public:
    00145   NetworkUDPSocketHandler(NetworkAddressList *bind = NULL);
    00146
    00148   virtual ~NetworkUDPSocketHandler() { this->Close(); }
    00149
    00150   bool Listen();
    00151   void Close();
    00152
    00153   void SendPacket(Packet *p, NetworkAddress *recv, bool all = false, bool broadcast = false);
    00154   void ReceivePackets();
    00155
    00156   void Send_NetworkGameInfo(Packet *p, const NetworkGameInfo *info);
    00157   void Recv_NetworkGameInfo(Packet *p, NetworkGameInfo *info);
    00158 };
    00159
    00160 #endif /* ENABLE_NETWORK */
    00161
    00162 #endif /* NETWORK_CORE_UDP_H */