OpenTTD
tcp_game.h
Go to the documentation of this file.
1 /* $Id$ */
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 
27  /*
28  * These first three pair of packets (thus six in
29  * total) must remain in this order for backward
30  * and forward compatibility between clients that
31  * are trying to join directly.
32  */
33 
34  /* Packets sent by socket accepting code without ever constructing a client socket instance. */
37 
38  /* Packets used by the client to join and an error message when the revision is wrong. */
41 
42  /* Packets used for the pre-game lobby. */
45 
46  /*
47  * Packets after here assume that the client
48  * and server are running the same version. As
49  * such ordering is unimportant from here on.
50  *
51  * The following is the remainder of the packets
52  * sent as part of authenticating and getting
53  * the map and other important data.
54  */
55 
56  /* After the join step, the first is checking NewGRFs. */
59 
60  /* Checking the game, and then company passwords. */
65 
66  /* The server welcomes the authenticated client and sends information of other clients. */
69 
70  /* Getting the savegame/map. */
78 
80 
81  /*
82  * At this moment the client has the map and
83  * the client is fully authenticated. Now the
84  * normal communication starts.
85  */
86 
87  /* Game progress monitoring. */
91 
92  /* Sending commands around. */
95 
96  /* Human communication! */
99 
100  /* Remote console. */
103 
104  /* Moving a client.*/
107 
108  /* Configuration updates. */
113 
114  /* A server quitting this game. */
117 
118  /* A client quitting. */
123 
125 };
126 
128 struct CommandPacket;
129 
134  uint count;
135 
136 public:
138  CommandQueue() : first(nullptr), last(nullptr), count(0) {}
140  ~CommandQueue() { this->Free(); }
141  void Append(CommandPacket *p);
142  CommandPacket *Pop(bool ignore_paused = false);
143  CommandPacket *Peek(bool ignore_paused = false);
144  void Free();
146  uint Count() const { return this->count; }
147 };
148 
151 /* TODO: rewrite into a proper class */
152 private:
154 
155 protected:
156  NetworkRecvStatus ReceiveInvalidPacket(PacketGameType type);
157 
162  virtual NetworkRecvStatus Receive_SERVER_FULL(Packet *p);
163 
168  virtual NetworkRecvStatus Receive_SERVER_BANNED(Packet *p);
169 
178  virtual NetworkRecvStatus Receive_CLIENT_JOIN(Packet *p);
179 
185  virtual NetworkRecvStatus Receive_SERVER_ERROR(Packet *p);
186 
191  virtual NetworkRecvStatus Receive_CLIENT_COMPANY_INFO(Packet *p);
192 
219  virtual NetworkRecvStatus Receive_SERVER_COMPANY_INFO(Packet *p);
220 
228  virtual NetworkRecvStatus Receive_SERVER_CLIENT_INFO(Packet *p);
229 
234  virtual NetworkRecvStatus Receive_SERVER_NEED_GAME_PASSWORD(Packet *p);
235 
242  virtual NetworkRecvStatus Receive_SERVER_NEED_COMPANY_PASSWORD(Packet *p);
243 
250  virtual NetworkRecvStatus Receive_CLIENT_GAME_PASSWORD(Packet *p);
251 
258  virtual NetworkRecvStatus Receive_CLIENT_COMPANY_PASSWORD(Packet *p);
259 
267  virtual NetworkRecvStatus Receive_SERVER_WELCOME(Packet *p);
268 
274  virtual NetworkRecvStatus Receive_CLIENT_GETMAP(Packet *p);
275 
281  virtual NetworkRecvStatus Receive_SERVER_WAIT(Packet *p);
282 
288  virtual NetworkRecvStatus Receive_SERVER_MAP_BEGIN(Packet *p);
289 
295  virtual NetworkRecvStatus Receive_SERVER_MAP_SIZE(Packet *p);
296 
302  virtual NetworkRecvStatus Receive_SERVER_MAP_DATA(Packet *p);
303 
308  virtual NetworkRecvStatus Receive_SERVER_MAP_DONE(Packet *p);
309 
314  virtual NetworkRecvStatus Receive_CLIENT_MAP_OK(Packet *p);
315 
321  virtual NetworkRecvStatus Receive_SERVER_JOIN(Packet *p);
322 
332  virtual NetworkRecvStatus Receive_SERVER_FRAME(Packet *p);
333 
341  virtual NetworkRecvStatus Receive_SERVER_SYNC(Packet *p);
342 
349  virtual NetworkRecvStatus Receive_CLIENT_ACK(Packet *p);
350 
362  virtual NetworkRecvStatus Receive_CLIENT_COMMAND(Packet *p);
363 
376  virtual NetworkRecvStatus Receive_SERVER_COMMAND(Packet *p);
377 
387  virtual NetworkRecvStatus Receive_CLIENT_CHAT(Packet *p);
388 
397  virtual NetworkRecvStatus Receive_SERVER_CHAT(Packet *p);
398 
404  virtual NetworkRecvStatus Receive_CLIENT_SET_PASSWORD(Packet *p);
405 
411  virtual NetworkRecvStatus Receive_CLIENT_SET_NAME(Packet *p);
412 
417  virtual NetworkRecvStatus Receive_CLIENT_QUIT(Packet *p);
418 
424  virtual NetworkRecvStatus Receive_CLIENT_ERROR(Packet *p);
425 
431  virtual NetworkRecvStatus Receive_SERVER_QUIT(Packet *p);
432 
439  virtual NetworkRecvStatus Receive_SERVER_ERROR_QUIT(Packet *p);
440 
445  virtual NetworkRecvStatus Receive_SERVER_SHUTDOWN(Packet *p);
446 
451  virtual NetworkRecvStatus Receive_SERVER_NEWGAME(Packet *p);
452 
459  virtual NetworkRecvStatus Receive_SERVER_RCON(Packet *p);
460 
467  virtual NetworkRecvStatus Receive_CLIENT_RCON(Packet *p);
468 
476  virtual NetworkRecvStatus Receive_SERVER_CHECK_NEWGRFS(Packet *p);
477 
482  virtual NetworkRecvStatus Receive_CLIENT_NEWGRFS_CHECKED(Packet *p);
483 
490  virtual NetworkRecvStatus Receive_SERVER_MOVE(Packet *p);
491 
498  virtual NetworkRecvStatus Receive_CLIENT_MOVE(Packet *p);
499 
505  virtual NetworkRecvStatus Receive_SERVER_COMPANY_UPDATE(Packet *p);
506 
513  virtual NetworkRecvStatus Receive_SERVER_CONFIG_UPDATE(Packet *p);
514 
515  NetworkRecvStatus HandlePacket(Packet *p);
516 
517  NetworkGameSocketHandler(SOCKET s);
518 public:
520  uint32 last_frame;
523  uint last_packet;
524 
525  NetworkRecvStatus CloseConnection(bool error = true) override;
526 
531  virtual NetworkRecvStatus CloseConnection(NetworkRecvStatus status) = 0;
532  virtual ~NetworkGameSocketHandler() {}
533 
538  inline void SetInfo(NetworkClientInfo *info)
539  {
540  assert(info != nullptr && this->info == nullptr);
541  this->info = info;
542  }
543 
548  inline NetworkClientInfo *GetInfo() const
549  {
550  return this->info;
551  }
552 
553  NetworkRecvStatus ReceivePackets();
554 
555  const char *ReceiveCommand(Packet *p, CommandPacket *cp);
556  void SendCommand(Packet *p, const CommandPacket *cp);
557 };
558 
559 #endif /* NETWORK_CORE_TCP_GAME_H */
Client acknowledges that it has all required NewGRFs.
Definition: tcp_game.h:58
Information about a single company.
Definition: tcp_game.h:44
Server tells everyone that someone is moved to another company.
Definition: tcp_game.h:106
Container for all information known about a client.
Definition: network_base.h:25
Internal entity of a packet.
Definition: packet.h:42
Network stuff has many things that needs to be included and/or implemented by default.
CommandPacket * Pop(bool ignore_paused=false)
Return the first item in the queue and remove it from the queue.
A client changes its name.
Definition: tcp_game.h:110
A server tells that a client has hit an error and did quit.
Definition: tcp_game.h:122
The server is full and has no place for you.
Definition: tcp_game.h:35
Clients sends the (hashed) game password.
Definition: tcp_game.h:62
Base socket handler for all TCP sockets.
Definition: tcp.h:29
uint count
The number of items in the queue.
Definition: tcp_game.h:134
Server distributing the message of a client (or itself).
Definition: tcp_game.h:98
void Append(CommandPacket *p)
Append a CommandPacket at the end of the queue.
Server tells the client what frame it is in, and thus to where the client may progress.
Definition: tcp_game.h:88
ClientID
&#39;Unique&#39; identifier to be given to clients
Definition: network_type.h:41
The client tells the server which frame it has executed.
Definition: tcp_game.h:89
Server tells the client what the random state should be.
Definition: tcp_game.h:90
NetworkClientInfo * info
Client info related to this socket.
Definition: tcp_game.h:153
Server welcomes you and gives you your ClientID.
Definition: tcp_game.h:67
Client asks the server to execute some command.
Definition: tcp_game.h:101
Server tells the client that it is beginning to send the map.
Definition: tcp_game.h:73
CommandQueue incoming_queue
The command-queue awaiting handling.
Definition: tcp_game.h:522
Response of the executed command on the server.
Definition: tcp_game.h:102
A server tells that a client has quit.
Definition: tcp_game.h:120
Server distributes a command to (all) the clients.
Definition: tcp_game.h:94
A queue of CommandPackets.
Definition: tcp_game.h:131
Server sends NewGRF IDs and MD5 checksums for the client to check.
Definition: tcp_game.h:57
uint32 last_frame
Last frame we have executed.
Definition: tcp_game.h:520
A client would like to be moved to another company.
Definition: tcp_game.h:105
Server sends you information about a client.
Definition: tcp_game.h:68
CommandQueue()
Initialise the command queue.
Definition: tcp_game.h:138
Client sends the (hashed) company password.
Definition: tcp_game.h:64
Server sends bits of the map to the client.
Definition: tcp_game.h:75
CommandPacket * last
The last packet in the queue; only valid when first != nullptr.
Definition: tcp_game.h:133
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition: core.h:24
A client (re)sets its company&#39;s password.
Definition: tcp_game.h:109
CommandPacket * first
The first packet in the queue.
Definition: tcp_game.h:132
void Free()
Free everything that is in the queue.
uint last_packet
Time we received the last frame.
Definition: tcp_game.h:523
NetworkClientInfo * GetInfo() const
Gets the client info of this socket handler.
Definition: tcp_game.h:548
Server sending an error message to the client.
Definition: tcp_game.h:40
A client reports an error to the server.
Definition: tcp_game.h:121
uint Count() const
Get the number of items in the queue.
Definition: tcp_game.h:146
PacketGameType
Enum with all types of TCP packets.
Definition: tcp_game.h:26
ClientID client_id
Client identifier.
Definition: tcp_game.h:519
uint32 last_frame_server
Last frame the server has executed.
Definition: tcp_game.h:521
Tells clients that a new client has joined.
Definition: tcp_game.h:79
Some network configuration important to the client changed.
Definition: tcp_game.h:112
Client requests the actual map.
Definition: tcp_game.h:71
void SetInfo(NetworkClientInfo *info)
Sets the client info for this socket handler.
Definition: tcp_game.h:538
The server is preparing to start a new game.
Definition: tcp_game.h:115
Client executed a command and sends it to the server.
Definition: tcp_game.h:93
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:114
Server tells it has just sent the last bits of the map to the client.
Definition: tcp_game.h:76
Server tells the client what the (compressed) size of the map is.
Definition: tcp_game.h:74
The client telling the server it wants to join.
Definition: tcp_game.h:39
Must ALWAYS be on the end of this list!! (period)
Definition: tcp_game.h:124
Server tells the client there are some people waiting for the map as well.
Definition: tcp_game.h:72
A client tells the server it is going to quit.
Definition: tcp_game.h:119
Everything we need to know about a command to be able to execute it.
Server requests the (hashed) game password.
Definition: tcp_game.h:61
Server requests the (hashed) company password.
Definition: tcp_game.h:63
Client said something that should be distributed.
Definition: tcp_game.h:97
CommandPacket * Peek(bool ignore_paused=false)
Return the first item in the queue, but don&#39;t remove it.
The server has banned you.
Definition: tcp_game.h:36
Base socket handler for all TCP sockets.
Definition: tcp_game.h:150
~CommandQueue()
Clear the command queue.
Definition: tcp_game.h:140
Basic functions to receive and send TCP packets.
The server is shutting down.
Definition: tcp_game.h:116
Information (password) of a company changed.
Definition: tcp_game.h:111
Request information about all companies.
Definition: tcp_game.h:43
Client tells the server that it received the whole map.
Definition: tcp_game.h:77