rubidium@5523: /* $Id$ */ rubidium@5523: rubidium@5523: #ifndef NETWORK_CORE_H rubidium@5523: #define NETWORK_CORE_H rubidium@5523: rubidium@5523: #ifdef ENABLE_NETWORK rubidium@5523: rubidium@5624: #include "os_abstraction.h" rubidium@5870: #include "../../newgrf_config.h" rubidium@5624: rubidium@5864: /** rubidium@5864: * @file core.h Base for all network types (UDP and TCP) rubidium@5864: */ rubidium@5864: rubidium@5524: bool NetworkCoreInitialize(void); rubidium@5523: void NetworkCoreShutdown(void); rubidium@5523: rubidium@5864: /** Status of a network client; reasons why a client has quit */ rubidium@5624: typedef enum { rubidium@5624: NETWORK_RECV_STATUS_OKAY, ///< Everything is okay rubidium@5624: NETWORK_RECV_STATUS_DESYNC, ///< A desync did occur rubidium@5624: NETWORK_RECV_STATUS_SAVEGAME, ///< Something went wrong (down)loading the savegame rubidium@5624: NETWORK_RECV_STATUS_CONN_LOST, ///< The conection is 'just' lost rubidium@5624: NETWORK_RECV_STATUS_MALFORMED_PACKET, ///< We apparently send a malformed packet rubidium@5624: NETWORK_RECV_STATUS_SERVER_ERROR, ///< The server told us we made an error rubidium@5624: NETWORK_RECV_STATUS_SERVER_FULL, ///< The server is full rubidium@5624: NETWORK_RECV_STATUS_SERVER_BANNED, ///< The server has banned us rubidium@5624: NETWORK_RECV_STATUS_CLOSE_QUERY, ///< Done quering the server rubidium@5624: } NetworkRecvStatus; rubidium@5624: rubidium@5870: /** Forward declaration due to circular dependencies */ rubidium@5870: class Packet; rubidium@5870: rubidium@5624: /** rubidium@5624: * SocketHandler for all network sockets in OpenTTD. rubidium@5624: */ rubidium@5624: class NetworkSocketHandler { rubidium@5624: public: rubidium@5624: /* TODO: make socket & has_quit protected once the TCP stuff rubidium@5624: *is in a real class too */ rubidium@5624: bool has_quit; ///< Whether the current client has quit/send a bad packet rubidium@5624: SOCKET sock; ///< The socket currently connected to rubidium@5624: public: rubidium@5864: /** Create a new unbound socket */ rubidium@5624: NetworkSocketHandler() { this->sock = INVALID_SOCKET; this->has_quit = false; } rubidium@5864: rubidium@5864: /** Close the socket when distructing the socket handler */ rubidium@5624: virtual ~NetworkSocketHandler() { this->Close(); } rubidium@5624: rubidium@5624: /** Really close the socket */ rubidium@5624: virtual void Close() {} rubidium@5624: rubidium@5624: /** rubidium@5624: * Close the current connection; for TCP this will be mostly equivalent rubidium@5624: * to Close(), but for UDP it just means the packet has to be dropped. rubidium@5624: * @return new status of the connection. rubidium@5624: */ rubidium@5624: virtual NetworkRecvStatus CloseConnection() { this->has_quit = true; return NETWORK_RECV_STATUS_OKAY; } rubidium@5624: rubidium@5624: /** rubidium@5624: * Whether this socket is currently bound to a socket. rubidium@5624: * @return true when the socket is bound, false otherwise rubidium@5624: */ rubidium@5624: bool IsConnected() { return this->sock != INVALID_SOCKET; } rubidium@5624: rubidium@5624: /** rubidium@5624: * Whether the current client connected to the socket has quit. rubidium@5624: * In the case of UDP, for example, once a client quits (send bad rubidium@5624: * data), the socket in not closed; only the packet is dropped. rubidium@5624: * @return true when the current client has quit, false otherwise rubidium@5624: */ rubidium@5624: bool HasClientQuit() { return this->has_quit; } rubidium@5870: rubidium@5870: void Send_GRFIdentifier(Packet *p, const GRFIdentifier *grf); rubidium@5870: void Recv_GRFIdentifier(Packet *p, GRFIdentifier *grf); rubidium@5624: }; rubidium@5624: rubidium@5523: #endif /* ENABLE_NETWORK */ rubidium@5523: rubidium@5523: #endif /* NETWORK_CORE_H */