# udp.h

Go to the documentation of this file.

    00001 /* $Id: udp.h 8857 2007-02-23 08:37:33Z celestar $ */
    00002
    00064 #ifndef NETWORK_CORE_UDP_H
    00065 #define NETWORK_CORE_UDP_H
    00066
    00067 #ifdef ENABLE_NETWORK
    00068
    00069 #include "os_abstraction.h"
    00070 #include "core.h"
    00071 #include "game.h"
    00072 #include "packet.h"
    00073 #include "../../debug.h"
    00074
    00076 enum PacketUDPType {
    00077   PACKET_UDP_CLIENT_FIND_SERVER,
    00078   PACKET_UDP_SERVER_RESPONSE,
    00079   PACKET_UDP_CLIENT_DETAIL_INFO,
    00080   PACKET_UDP_SERVER_DETAIL_INFO,
    00081   PACKET_UDP_SERVER_REGISTER,
    00082   PACKET_UDP_MASTER_ACK_REGISTER,
    00083   PACKET_UDP_CLIENT_GET_LIST,
    00084   PACKET_UDP_MASTER_RESPONSE_LIST,
    00085   PACKET_UDP_SERVER_UNREGISTER,
    00086   PACKET_UDP_CLIENT_GET_NEWGRFS,
    00087   PACKET_UDP_SERVER_NEWGRFS,
    00088   PACKET_UDP_END
    00089 };
    00090
    00091 #define DECLARE_UDP_RECEIVE_COMMAND(type) virtual void NetworkPacketReceive_## type ##_command(Packet *p, const struct sockaddr_in *)
    00092 #define DEF_UDP_RECEIVE_COMMAND(cls, type) void cls ##NetworkUDPSocketHandler::NetworkPacketReceive_ ## type ## _command(Packet *p, const struct sockaddr_in *client_addr)
    00093
    00095 class NetworkUDPSocketHandler : public NetworkSocketHandler {
    00096 protected:
    00097   NetworkRecvStatus CloseConnection();
    00098
    00099   /* Declare all possible packets here. If it can be received by the
    00100    * a specific handler, it has to be implemented. */
    00101   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER);
    00102   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_RESPONSE);
    00103   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_DETAIL_INFO);
    00104   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_DETAIL_INFO);
    00105   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_REGISTER);
    00106   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_ACK_REGISTER);
    00107   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_GET_LIST);
    00108   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_RESPONSE_LIST);
    00109   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_UNREGISTER);
    00110   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_GET_NEWGRFS);
    00111   DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_NEWGRFS);
    00112
    00113   void HandleUDPPacket(Packet *p, const struct sockaddr_in *client_addr);
    00114
    00122   virtual void HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config) { NOT_REACHED(); }
    00123 public:
    00125   virtual ~NetworkUDPSocketHandler() { this->Close(); }
    00126
    00127   bool Listen(uint32 host, uint16 port, bool broadcast);
    00128   void Close();
    00129
    00130   void SendPacket(Packet *p, const struct sockaddr_in *recv);
    00131   void ReceivePackets();
    00132
    00133   void Send_NetworkGameInfo(Packet *p, const NetworkGameInfo *info);
    00134   void Recv_NetworkGameInfo(Packet *p, NetworkGameInfo *info);
    00135 };
    00136
    00137 #endif /* ENABLE_NETWORK */
    00138
    00139 #endif /* NETWORK_CORE_UDP_H */