src/network/core/core.h
changeset 5624 6afe9d27430a
parent 5524 93460e0fed07
child 5864 a95a2849b0e1
equal deleted inserted replaced
5623:d983df942a05 5624:6afe9d27430a
     3 #ifndef NETWORK_CORE_H
     3 #ifndef NETWORK_CORE_H
     4 #define NETWORK_CORE_H
     4 #define NETWORK_CORE_H
     5 
     5 
     6 #ifdef ENABLE_NETWORK
     6 #ifdef ENABLE_NETWORK
     7 
     7 
       
     8 #include "os_abstraction.h"
       
     9 
     8 bool NetworkCoreInitialize(void);
    10 bool NetworkCoreInitialize(void);
     9 void NetworkCoreShutdown(void);
    11 void NetworkCoreShutdown(void);
       
    12 
       
    13 typedef enum {
       
    14 	NETWORK_RECV_STATUS_OKAY,             ///< Everything is okay
       
    15 	NETWORK_RECV_STATUS_DESYNC,           ///< A desync did occur
       
    16 	NETWORK_RECV_STATUS_SAVEGAME,         ///< Something went wrong (down)loading the savegame
       
    17 	NETWORK_RECV_STATUS_CONN_LOST,        ///< The conection is 'just' lost
       
    18 	NETWORK_RECV_STATUS_MALFORMED_PACKET, ///< We apparently send a malformed packet
       
    19 	NETWORK_RECV_STATUS_SERVER_ERROR,     ///< The server told us we made an error
       
    20 	NETWORK_RECV_STATUS_SERVER_FULL,      ///< The server is full
       
    21 	NETWORK_RECV_STATUS_SERVER_BANNED,    ///< The server has banned us
       
    22 	NETWORK_RECV_STATUS_CLOSE_QUERY,      ///< Done quering the server
       
    23 } NetworkRecvStatus;
       
    24 
       
    25 /**
       
    26  * SocketHandler for all network sockets in OpenTTD.
       
    27  */
       
    28 class NetworkSocketHandler {
       
    29 public:
       
    30 	/* TODO: make socket & has_quit protected once the TCP stuff
       
    31 	 *is in a real class too */
       
    32 	bool has_quit; ///< Whether the current client has quit/send a bad packet
       
    33 	SOCKET sock;   ///< The socket currently connected to
       
    34 public:
       
    35 	NetworkSocketHandler() { this->sock = INVALID_SOCKET; this->has_quit = false; }
       
    36 	virtual ~NetworkSocketHandler() { this->Close(); }
       
    37 
       
    38 	/** Really close the socket */
       
    39 	virtual void Close() {}
       
    40 
       
    41 	/**
       
    42 	 * Close the current connection; for TCP this will be mostly equivalent
       
    43 	 * to Close(), but for UDP it just means the packet has to be dropped.
       
    44 	 * @return new status of the connection.
       
    45 	 */
       
    46 	virtual NetworkRecvStatus CloseConnection() { this->has_quit = true; return NETWORK_RECV_STATUS_OKAY; }
       
    47 
       
    48 	/**
       
    49 	 * Whether this socket is currently bound to a socket.
       
    50 	 * @return true when the socket is bound, false otherwise
       
    51 	 */
       
    52 	bool IsConnected() { return this->sock != INVALID_SOCKET; }
       
    53 
       
    54 	/**
       
    55 	 * Whether the current client connected to the socket has quit.
       
    56 	 * In the case of UDP, for example, once a client quits (send bad
       
    57 	 * data), the socket in not closed; only the packet is dropped.
       
    58 	 * @return true when the current client has quit, false otherwise
       
    59 	 */
       
    60 	bool HasClientQuit() { return this->has_quit; }
       
    61 };
    10 
    62 
    11 #endif /* ENABLE_NETWORK */
    63 #endif /* ENABLE_NETWORK */
    12 
    64 
    13 #endif /* NETWORK_CORE_H */
    65 #endif /* NETWORK_CORE_H */