OpenTTD: src/network/core/tcp_game.h Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

tcp_game.h
Go to the documentation of this file.
1 /* $Id: tcp_game.h 26056 2013-11-22 21:50:43Z rubidium $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
9
14 #ifndef NETWORK_CORE_TCP_GAME_H
15 #define NETWORK_CORE_TCP_GAME_H
16
17 #include "os_abstraction.h"
18 #include "tcp.h"
19 #include "../network_type.h"
20 #include "../../core/pool_type.hpp"
21
22 #ifdef ENABLE_NETWORK
23
28 enum PacketGameType {
29 /*
30 * These first three pair of packets (thus six in
31 * total) must remain in this order for backward
32 * and forward compatibility between clients that
33 * are trying to join directly.
34 */
35
36 /* Packets sent by socket accepting code without ever constructing a client socket instance. */
37 PACKET_SERVER_FULL,
38 PACKET_SERVER_BANNED,
39
40 /* Packets used by the client to join and an error message when the revision is wrong. */
41 PACKET_CLIENT_JOIN,
42 PACKET_SERVER_ERROR,
43
44 /* Packets used for the pre-game lobby. */
45 PACKET_CLIENT_COMPANY_INFO,
46 PACKET_SERVER_COMPANY_INFO,
47
48 /*
49 * Packets after here assume that the client
50 * and server are running the same version. As
51 * such ordering is unimportant from here on.
52 *
53 * The following is the remainder of the packets
54 * sent as part of authenticating and getting
55 * the map and other important data.
56 */
57
58 /* After the join step, the first is checking NewGRFs. */
59 PACKET_SERVER_CHECK_NEWGRFS,
60 PACKET_CLIENT_NEWGRFS_CHECKED,
61
62 /* Checking the game, and then company passwords. */
63 PACKET_SERVER_NEED_GAME_PASSWORD,
64 PACKET_CLIENT_GAME_PASSWORD,
65 PACKET_SERVER_NEED_COMPANY_PASSWORD,
66 PACKET_CLIENT_COMPANY_PASSWORD,
67
68 /* The server welcomes the authenticated client and sends information of other clients. */
69 PACKET_SERVER_WELCOME,
70 PACKET_SERVER_CLIENT_INFO,
71
72 /* Getting the savegame/map. */
73 PACKET_CLIENT_GETMAP,
74 PACKET_SERVER_WAIT,
75 PACKET_SERVER_MAP_BEGIN,
76 PACKET_SERVER_MAP_SIZE,
77 PACKET_SERVER_MAP_DATA,
78 PACKET_SERVER_MAP_DONE,
79 PACKET_CLIENT_MAP_OK,
80
81 PACKET_SERVER_JOIN,
82
83 /*
84 * At this moment the client has the map and
85 * the client is fully authenticated. Now the
86 * normal communication starts.
87 */
88
89 /* Game progress monitoring. */
90 PACKET_SERVER_FRAME,
91 PACKET_CLIENT_ACK,
92 PACKET_SERVER_SYNC,
93
94 /* Sending commands around. */
95 PACKET_CLIENT_COMMAND,
96 PACKET_SERVER_COMMAND,
97
98 /* Human communication! */
99 PACKET_CLIENT_CHAT,
100 PACKET_SERVER_CHAT,
101
102 /* Remote console. */
103 PACKET_CLIENT_RCON,
104 PACKET_SERVER_RCON,
105
106 /* Moving a client.*/
107 PACKET_CLIENT_MOVE,
108 PACKET_SERVER_MOVE,
109
110 /* Configuration updates. */
111 PACKET_CLIENT_SET_PASSWORD,
112 PACKET_CLIENT_SET_NAME,
113 PACKET_SERVER_COMPANY_UPDATE,
114 PACKET_SERVER_CONFIG_UPDATE,
115
116 /* A server quitting this game. */
117 PACKET_SERVER_NEWGAME,
118 PACKET_SERVER_SHUTDOWN,
119
120 /* A client quitting. */
121 PACKET_CLIENT_QUIT,
122 PACKET_SERVER_QUIT,
123 PACKET_CLIENT_ERROR,
124 PACKET_SERVER_ERROR_QUIT,
125
126 PACKET_END,
127 };
128
130 struct CommandPacket;
131
133 class CommandQueue {
134 CommandPacket *first;
135 CommandPacket *last;
136 uint count;
137
138 public:
140 CommandQueue() : first(NULL), last(NULL), count(0) {}
142 ~CommandQueue() { this->Free(); }
143 void Append(CommandPacket *p);
144 CommandPacket *Pop(bool ignore_paused = false);
145 CommandPacket *Peek(bool ignore_paused = false);
146 void Free();
148 uint Count() const { return this->count; }
149 };
150
152 class NetworkGameSocketHandler : public NetworkTCPSocketHandler {
153 /* TODO: rewrite into a proper class */
154 private:
155 NetworkClientInfo *info;
156
157 protected:
158 NetworkRecvStatus ReceiveInvalidPacket(PacketGameType type);
159
164 virtual NetworkRecvStatus Receive_SERVER_FULL(Packet *p);
165
170 virtual NetworkRecvStatus Receive_SERVER_BANNED(Packet *p);
171
180 virtual NetworkRecvStatus Receive_CLIENT_JOIN(Packet *p);
181
187 virtual NetworkRecvStatus Receive_SERVER_ERROR(Packet *p);
188
193 virtual NetworkRecvStatus Receive_CLIENT_COMPANY_INFO(Packet *p);
194
221 virtual NetworkRecvStatus Receive_SERVER_COMPANY_INFO(Packet *p);
222
230 virtual NetworkRecvStatus Receive_SERVER_CLIENT_INFO(Packet *p);
231
236 virtual NetworkRecvStatus Receive_SERVER_NEED_GAME_PASSWORD(Packet *p);
237
244 virtual NetworkRecvStatus Receive_SERVER_NEED_COMPANY_PASSWORD(Packet *p);
245
252 virtual NetworkRecvStatus Receive_CLIENT_GAME_PASSWORD(Packet *p);
253
260 virtual NetworkRecvStatus Receive_CLIENT_COMPANY_PASSWORD(Packet *p);
261
269 virtual NetworkRecvStatus Receive_SERVER_WELCOME(Packet *p);
270
276 virtual NetworkRecvStatus Receive_CLIENT_GETMAP(Packet *p);
277
283 virtual NetworkRecvStatus Receive_SERVER_WAIT(Packet *p);
284
290 virtual NetworkRecvStatus Receive_SERVER_MAP_BEGIN(Packet *p);
291
297 virtual NetworkRecvStatus Receive_SERVER_MAP_SIZE(Packet *p);
298
304 virtual NetworkRecvStatus Receive_SERVER_MAP_DATA(Packet *p);
305
310 virtual NetworkRecvStatus Receive_SERVER_MAP_DONE(Packet *p);
311
316 virtual NetworkRecvStatus Receive_CLIENT_MAP_OK(Packet *p);
317
323 virtual NetworkRecvStatus Receive_SERVER_JOIN(Packet *p);
324
334 virtual NetworkRecvStatus Receive_SERVER_FRAME(Packet *p);
335
343 virtual NetworkRecvStatus Receive_SERVER_SYNC(Packet *p);
344
351 virtual NetworkRecvStatus Receive_CLIENT_ACK(Packet *p);
352
364 virtual NetworkRecvStatus Receive_CLIENT_COMMAND(Packet *p);
365
378 virtual NetworkRecvStatus Receive_SERVER_COMMAND(Packet *p);
379
389 virtual NetworkRecvStatus Receive_CLIENT_CHAT(Packet *p);
390
399 virtual NetworkRecvStatus Receive_SERVER_CHAT(Packet *p);
400
406 virtual NetworkRecvStatus Receive_CLIENT_SET_PASSWORD(Packet *p);
407
413 virtual NetworkRecvStatus Receive_CLIENT_SET_NAME(Packet *p);
414
419 virtual NetworkRecvStatus Receive_CLIENT_QUIT(Packet *p);
420
426 virtual NetworkRecvStatus Receive_CLIENT_ERROR(Packet *p);
427
433 virtual NetworkRecvStatus Receive_SERVER_QUIT(Packet *p);
434
441 virtual NetworkRecvStatus Receive_SERVER_ERROR_QUIT(Packet *p);
442
447 virtual NetworkRecvStatus Receive_SERVER_SHUTDOWN(Packet *p);
448
453 virtual NetworkRecvStatus Receive_SERVER_NEWGAME(Packet *p);
454
461 virtual NetworkRecvStatus Receive_SERVER_RCON(Packet *p);
462
469 virtual NetworkRecvStatus Receive_CLIENT_RCON(Packet *p);
470
478 virtual NetworkRecvStatus Receive_SERVER_CHECK_NEWGRFS(Packet *p);
479
484 virtual NetworkRecvStatus Receive_CLIENT_NEWGRFS_CHECKED(Packet *p);
485
492 virtual NetworkRecvStatus Receive_SERVER_MOVE(Packet *p);
493
500 virtual NetworkRecvStatus Receive_CLIENT_MOVE(Packet *p);
501
507 virtual NetworkRecvStatus Receive_SERVER_COMPANY_UPDATE(Packet *p);
508
515 virtual NetworkRecvStatus Receive_SERVER_CONFIG_UPDATE(Packet *p);
516
517 NetworkRecvStatus HandlePacket(Packet *p);
518
519 NetworkGameSocketHandler(SOCKET s);
520 public:
521 ClientID client_id;
522 uint32 last_frame;
523 uint32 last_frame_server;
524 CommandQueue incoming_queue;
525 uint last_packet;
526
527 NetworkRecvStatus CloseConnection(bool error = true);
528
533 virtual NetworkRecvStatus CloseConnection(NetworkRecvStatus status) = 0;
534 virtual ~NetworkGameSocketHandler() {}
535
540 inline void SetInfo(NetworkClientInfo *info)
541 {
542 assert(info != NULL && this->info == NULL);
543 this->info = info;
544 }
545
550 inline NetworkClientInfo *GetInfo() const
551 {
552 return this->info;
553 }
554
555 NetworkRecvStatus ReceivePackets();
556
557 const char *ReceiveCommand(Packet *p, CommandPacket *cp);
558 void SendCommand(Packet *p, const CommandPacket *cp);
559 };
560
561 #endif /* ENABLE_NETWORK */
562
563 #endif /* NETWORK_CORE_TCP_GAME_H */