src/network/core/udp.h
changeset 5624 6afe9d27430a
parent 5619 9f5a7152403a
child 5765 27dbbe543fcf
equal deleted inserted replaced
5623:d983df942a05 5624:6afe9d27430a
     4 #define NETWORK_CORE_UDP_H
     4 #define NETWORK_CORE_UDP_H
     5 
     5 
     6 #ifdef ENABLE_NETWORK
     6 #ifdef ENABLE_NETWORK
     7 
     7 
     8 #include "os_abstraction.h"
     8 #include "os_abstraction.h"
       
     9 #include "core.h"
     9 #include "game.h"
    10 #include "game.h"
    10 #include "packet.h"
    11 #include "packet.h"
    11 #include "../../newgrf_config.h"
    12 #include "../../newgrf_config.h"
    12 #include "../../debug.h"
    13 #include "../../debug.h"
    13 #include "../network_data.h"
       
    14 
    14 
    15 /**
    15 /**
    16  * @file udp.h Basic functions to receive and send UDP packets.
    16  * @file udp.h Basic functions to receive and send UDP packets.
    17  *
    17  *
    18  *
    18  *
    89 	PACKET_UDP_END                   ///< Must ALWAYS be on the end of this list!! (period)
    89 	PACKET_UDP_END                   ///< Must ALWAYS be on the end of this list!! (period)
    90 };
    90 };
    91 
    91 
    92 #define DECLARE_UDP_RECEIVE_COMMAND(type) virtual void NetworkPacketReceive_## type ##_command(Packet *p, const struct sockaddr_in *)
    92 #define DECLARE_UDP_RECEIVE_COMMAND(type) virtual void NetworkPacketReceive_## type ##_command(Packet *p, const struct sockaddr_in *)
    93 
    93 
    94 class NetworkUDPSocketHandler {
    94 /** Base socket handler for all UDP sockets */
    95 private:
    95 class NetworkUDPSocketHandler : public NetworkSocketHandler {
    96 	SOCKET udp;
       
    97 protected:
    96 protected:
    98 	NetworkClientState cs;
    97 	NetworkRecvStatus CloseConnection();
       
    98 
    99 	/* Declare all possible packets here. If it can be received by the
    99 	/* Declare all possible packets here. If it can be received by the
   100 	 * a specific handler, it has to be implemented. */
   100 	 * a specific handler, it has to be implemented. */
   101 	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER);
   101 	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER);
   102 	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_RESPONSE);
   102 	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_RESPONSE);
   103 	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_DETAIL_INFO);
   103 	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_DETAIL_INFO);
   119 	 * the grfconfig list of the NetworkGameInfo.
   119 	 * the grfconfig list of the NetworkGameInfo.
   120 	 * @param config the GRF to handle
   120 	 * @param config the GRF to handle
   121 	 */
   121 	 */
   122 	virtual void HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config) { NOT_REACHED(); }
   122 	virtual void HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config) { NOT_REACHED(); }
   123 public:
   123 public:
   124 	NetworkUDPSocketHandler();
       
   125 	virtual ~NetworkUDPSocketHandler() { this->Close(); }
   124 	virtual ~NetworkUDPSocketHandler() { this->Close(); }
   126 
   125 
   127 	bool IsListening() { return this->udp != INVALID_SOCKET; }
       
   128 	bool Listen(uint32 host, uint16 port, bool broadcast);
   126 	bool Listen(uint32 host, uint16 port, bool broadcast);
   129 	void Close();
   127 	void Close();
   130 
   128 
   131 	void SendPacket(Packet *p, const struct sockaddr_in *recv);
   129 	void SendPacket(Packet *p, const struct sockaddr_in *recv);
   132 	void ReceivePackets();
   130 	void ReceivePackets();