2007-01-04 19:50:40 +01:00
|
|
|
/* $Id$ */
|
|
|
|
|
2007-02-23 09:37:33 +01:00
|
|
|
/**
|
|
|
|
* @file core.h Base for all network types (UDP and TCP)
|
|
|
|
*/
|
|
|
|
|
2007-01-04 19:50:40 +01:00
|
|
|
#ifndef NETWORK_CORE_H
|
|
|
|
#define NETWORK_CORE_H
|
|
|
|
|
|
|
|
#ifdef ENABLE_NETWORK
|
|
|
|
|
2007-01-12 21:19:49 +01:00
|
|
|
#include "os_abstraction.h"
|
2007-01-30 18:12:46 +01:00
|
|
|
#include "../../newgrf_config.h"
|
2007-01-12 21:19:49 +01:00
|
|
|
|
2007-03-07 12:47:46 +01:00
|
|
|
bool NetworkCoreInitialize();
|
|
|
|
void NetworkCoreShutdown();
|
2007-01-04 19:50:40 +01:00
|
|
|
|
2007-01-28 21:47:25 +01:00
|
|
|
/** Status of a network client; reasons why a client has quit */
|
2007-03-07 13:11:48 +01:00
|
|
|
enum NetworkRecvStatus {
|
2007-01-12 21:19:49 +01:00
|
|
|
NETWORK_RECV_STATUS_OKAY, ///< Everything is okay
|
|
|
|
NETWORK_RECV_STATUS_DESYNC, ///< A desync did occur
|
2007-01-30 18:22:56 +01:00
|
|
|
NETWORK_RECV_STATUS_NEWGRF_MISMATCH, ///< We did not have the required NewGRFs
|
2007-01-12 21:19:49 +01:00
|
|
|
NETWORK_RECV_STATUS_SAVEGAME, ///< Something went wrong (down)loading the savegame
|
|
|
|
NETWORK_RECV_STATUS_CONN_LOST, ///< The conection is 'just' lost
|
|
|
|
NETWORK_RECV_STATUS_MALFORMED_PACKET, ///< We apparently send a malformed packet
|
|
|
|
NETWORK_RECV_STATUS_SERVER_ERROR, ///< The server told us we made an error
|
|
|
|
NETWORK_RECV_STATUS_SERVER_FULL, ///< The server is full
|
|
|
|
NETWORK_RECV_STATUS_SERVER_BANNED, ///< The server has banned us
|
|
|
|
NETWORK_RECV_STATUS_CLOSE_QUERY, ///< Done quering the server
|
2007-03-07 13:11:48 +01:00
|
|
|
};
|
2007-01-12 21:19:49 +01:00
|
|
|
|
2007-01-30 18:12:46 +01:00
|
|
|
/** Forward declaration due to circular dependencies */
|
2007-01-31 10:51:22 +01:00
|
|
|
struct Packet;
|
2007-01-30 18:12:46 +01:00
|
|
|
|
2007-01-12 21:19:49 +01:00
|
|
|
/**
|
|
|
|
* SocketHandler for all network sockets in OpenTTD.
|
|
|
|
*/
|
|
|
|
class NetworkSocketHandler {
|
|
|
|
public:
|
|
|
|
/* TODO: make socket & has_quit protected once the TCP stuff
|
|
|
|
*is in a real class too */
|
|
|
|
bool has_quit; ///< Whether the current client has quit/send a bad packet
|
|
|
|
SOCKET sock; ///< The socket currently connected to
|
|
|
|
public:
|
2007-01-28 21:47:25 +01:00
|
|
|
/** Create a new unbound socket */
|
2009-01-14 13:50:13 +01:00
|
|
|
NetworkSocketHandler(SOCKET s = INVALID_SOCKET) { this->sock = s; this->has_quit = false; }
|
2007-01-28 21:47:25 +01:00
|
|
|
|
|
|
|
/** Close the socket when distructing the socket handler */
|
2007-01-12 21:19:49 +01:00
|
|
|
virtual ~NetworkSocketHandler() { this->Close(); }
|
|
|
|
|
|
|
|
/** Really close the socket */
|
|
|
|
virtual void Close() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close the current connection; for TCP this will be mostly equivalent
|
|
|
|
* to Close(), but for UDP it just means the packet has to be dropped.
|
|
|
|
* @return new status of the connection.
|
|
|
|
*/
|
|
|
|
virtual NetworkRecvStatus CloseConnection() { this->has_quit = true; return NETWORK_RECV_STATUS_OKAY; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether this socket is currently bound to a socket.
|
|
|
|
* @return true when the socket is bound, false otherwise
|
|
|
|
*/
|
2008-05-30 20:20:26 +02:00
|
|
|
bool IsConnected() const { return this->sock != INVALID_SOCKET; }
|
2007-01-12 21:19:49 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the current client connected to the socket has quit.
|
|
|
|
* In the case of UDP, for example, once a client quits (send bad
|
|
|
|
* data), the socket in not closed; only the packet is dropped.
|
|
|
|
* @return true when the current client has quit, false otherwise
|
|
|
|
*/
|
2008-05-30 20:20:26 +02:00
|
|
|
bool HasClientQuit() const { return this->has_quit; }
|
2007-01-30 18:12:46 +01:00
|
|
|
|
|
|
|
void Send_GRFIdentifier(Packet *p, const GRFIdentifier *grf);
|
|
|
|
void Recv_GRFIdentifier(Packet *p, GRFIdentifier *grf);
|
2008-12-22 19:40:57 +01:00
|
|
|
void Send_CompanyInformation(Packet *p, const struct Company *c, const struct NetworkCompanyStats *stats);
|
2007-01-12 21:19:49 +01:00
|
|
|
};
|
|
|
|
|
2007-01-04 19:50:40 +01:00
|
|
|
#endif /* ENABLE_NETWORK */
|
|
|
|
|
|
|
|
#endif /* NETWORK_CORE_H */
|