10
10
#ifdef ENABLE_NETWORK
12
12
#include "os_abstraction.h"
14
15
#include "packet.h"
15
#include "../../tile_type.h"
18
* Enum with all types of UDP packets.
19
* The order of the first 4 packets MUST not be changed, as
20
* it protects old clients from joining newer servers
21
* (because SERVER_ERROR is the respond to a wrong revision)
28
PACKET_CLIENT_COMPANY_INFO,
29
PACKET_SERVER_COMPANY_INFO,
30
PACKET_SERVER_CLIENT_INFO,
31
PACKET_SERVER_NEED_PASSWORD,
32
PACKET_CLIENT_PASSWORD,
33
PACKET_SERVER_WELCOME,
42
PACKET_CLIENT_COMMAND,
43
PACKET_SERVER_COMMAND,
46
PACKET_CLIENT_SET_PASSWORD,
47
PACKET_CLIENT_SET_NAME,
51
PACKET_SERVER_ERROR_QUIT,
52
PACKET_SERVER_SHUTDOWN,
53
PACKET_SERVER_NEWGAME,
56
PACKET_SERVER_CHECK_NEWGRFS,
57
PACKET_CLIENT_NEWGRFS_CHECKED,
58
PACKET_END ///< Must ALWAYS be on the end of this list!! (period)
61
/** Packet that wraps a command */
62
struct CommandPacket {
63
CommandPacket *next; ///< the next command packet (if in queue)
64
PlayerByte player; ///< player that is executing the command
65
uint32 cmd; ///< command being executed
66
uint32 p1; ///< parameter p1
67
uint32 p2; ///< parameter p2
68
TileIndex tile; ///< tile command being executed on
69
char text[80]; ///< possible text sent for name changes etc
70
uint32 frame; ///< the frame in which this packet is executed
71
byte callback; ///< any callback function executed upon successful completion of the command
72
bool my_cmd; ///< did the command originate from "me"
75
/** Status of a client */
77
STATUS_INACTIVE, ///< The client is not connected nor active
78
STATUS_AUTHORIZING,///< The client is authorizing
79
STATUS_AUTH, ///< The client is authorized
80
STATUS_MAP_WAIT, ///< The client is waiting as someone else is downloading the map
81
STATUS_MAP, ///< The client is downloading the map
82
STATUS_DONE_MAP, ///< The client has downloaded the map
83
STATUS_PRE_ACTIVE, ///< The client is catching up the delayed frames
84
STATUS_ACTIVE, ///< The client is an active player in the game
87
17
/** Base socket handler for all TCP sockets */
88
18
class NetworkTCPSocketHandler : public NetworkSocketHandler {
89
/* TODO: rewrite into a proper class */
91
20
Packet *packet_queue; ///< Packets that are awaiting delivery
92
21
Packet *packet_recv; ///< Partially received packet
94
uint16 index; ///< Client index
95
uint32 last_frame; ///< Last frame we have executed
96
uint32 last_frame_server; ///< Last frame the server has executed
97
byte lag_test; ///< Byte used for lag-testing the client
99
ClientStatus status; ///< Status of this client
100
23
bool writable; ///< Can we write to this socket?
102
CommandPacket *command_queue; ///< The command-queue awaiting delivery
104
NetworkRecvStatus CloseConnection();
25
virtual NetworkRecvStatus CloseConnection();
108
26
void Send_Packet(Packet *packet);
109
27
bool Send_Packets();
110
28
bool IsPacketQueueEmpty();
112
30
Packet *Recv_Packet(NetworkRecvStatus *status);
32
NetworkTCPSocketHandler(SOCKET s = INVALID_SOCKET);
33
~NetworkTCPSocketHandler();
37
* "Helper" class for creating TCP connections in a non-blocking manner
41
class ThreadObject *thread; ///< Thread used to create the TCP connection
42
bool connected; ///< Whether we succeeded in making the connection
43
bool aborted; ///< Whether we bailed out (i.e. connection making failed)
44
bool killed; ///< Whether we got killed
45
SOCKET sock; ///< The socket we're connecting with
47
/** The actual connection function */
51
* Entry point for the new threads.
52
* @param param the TCPConnecter instance to call Connect on.
54
static void ThreadEntry(void *param);
57
/** Address we're connecting to */
58
NetworkAddress address;
62
* Create a new connecter for the given address
63
* @param address the (un)resolved address to connect to
65
TCPConnecter(const NetworkAddress &address);
66
/** Silence the warnings */
67
virtual ~TCPConnecter() {}
70
* Callback when the connection succeeded.
71
* @param s the socket that we opened
73
virtual void OnConnect(SOCKET s) {}
76
* Callback for when the connection attempt failed.
78
virtual void OnFailure() {}
81
* Check whether we need to call the callback, i.e. whether we
82
* have connected or aborted and call the appropriate callback
83
* for that. It's done this way to ease on the locking that
84
* would otherwise be needed everywhere.
86
static void CheckCallbacks();
88
/** Kill all connection attempts. */
89
static void KillAll();
115
92
#endif /* ENABLE_NETWORK */