OpenTTD
tcp_game.h
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
12 #ifndef NETWORK_CORE_TCP_GAME_H
13 #define NETWORK_CORE_TCP_GAME_H
14 
15 #include "os_abstraction.h"
16 #include "tcp.h"
17 #include "../network_type.h"
18 #include "../../core/pool_type.hpp"
19 
25  /*
26  * These first three pair of packets (thus six in
27  * total) must remain in this order for backward
28  * and forward compatibility between clients that
29  * are trying to join directly.
30  */
31 
32  /* Packets sent by socket accepting code without ever constructing a client socket instance. */
35 
36  /* Packets used by the client to join and an error message when the revision is wrong. */
39 
40  /* Packets used for the pre-game lobby. */
43 
44  /*
45  * Packets after here assume that the client
46  * and server are running the same version. As
47  * such ordering is unimportant from here on.
48  *
49  * The following is the remainder of the packets
50  * sent as part of authenticating and getting
51  * the map and other important data.
52  */
53 
54  /* After the join step, the first is checking NewGRFs. */
57 
58  /* Checking the game, and then company passwords. */
63 
64  /* The server welcomes the authenticated client and sends information of other clients. */
67 
68  /* Getting the savegame/map. */
76 
78 
79  /*
80  * At this moment the client has the map and
81  * the client is fully authenticated. Now the
82  * normal communication starts.
83  */
84 
85  /* Game progress monitoring. */
89 
90  /* Sending commands around. */
93 
94  /* Human communication! */
97 
98  /* Remote console. */
101 
102  /* Moving a client.*/
105 
106  /* Configuration updates. */
111 
112  /* A server quitting this game. */
115 
116  /* A client quitting. */
121 
123 };
124 
126 struct CommandPacket;
127 
132  uint count;
133 
134 public:
136  CommandQueue() : first(nullptr), last(nullptr), count(0) {}
138  ~CommandQueue() { this->Free(); }
139  void Append(CommandPacket *p);
140  CommandPacket *Pop(bool ignore_paused = false);
141  CommandPacket *Peek(bool ignore_paused = false);
142  void Free();
144  uint Count() const { return this->count; }
145 };
146 
149 /* TODO: rewrite into a proper class */
150 private:
152 
153 protected:
154  NetworkRecvStatus ReceiveInvalidPacket(PacketGameType type);
155 
160  virtual NetworkRecvStatus Receive_SERVER_FULL(Packet *p);
161 
166  virtual NetworkRecvStatus Receive_SERVER_BANNED(Packet *p);
167 
176  virtual NetworkRecvStatus Receive_CLIENT_JOIN(Packet *p);
177 
183  virtual NetworkRecvStatus Receive_SERVER_ERROR(Packet *p);
184 
189  virtual NetworkRecvStatus Receive_CLIENT_COMPANY_INFO(Packet *p);
190 
217  virtual NetworkRecvStatus Receive_SERVER_COMPANY_INFO(Packet *p);
218 
226  virtual NetworkRecvStatus Receive_SERVER_CLIENT_INFO(Packet *p);
227 
232  virtual NetworkRecvStatus Receive_SERVER_NEED_GAME_PASSWORD(Packet *p);
233 
240  virtual NetworkRecvStatus Receive_SERVER_NEED_COMPANY_PASSWORD(Packet *p);
241 
248  virtual NetworkRecvStatus Receive_CLIENT_GAME_PASSWORD(Packet *p);
249 
256  virtual NetworkRecvStatus Receive_CLIENT_COMPANY_PASSWORD(Packet *p);
257 
265  virtual NetworkRecvStatus Receive_SERVER_WELCOME(Packet *p);
266 
272  virtual NetworkRecvStatus Receive_CLIENT_GETMAP(Packet *p);
273 
279  virtual NetworkRecvStatus Receive_SERVER_WAIT(Packet *p);
280 
286  virtual NetworkRecvStatus Receive_SERVER_MAP_BEGIN(Packet *p);
287 
293  virtual NetworkRecvStatus Receive_SERVER_MAP_SIZE(Packet *p);
294 
300  virtual NetworkRecvStatus Receive_SERVER_MAP_DATA(Packet *p);
301 
306  virtual NetworkRecvStatus Receive_SERVER_MAP_DONE(Packet *p);
307 
312  virtual NetworkRecvStatus Receive_CLIENT_MAP_OK(Packet *p);
313 
319  virtual NetworkRecvStatus Receive_SERVER_JOIN(Packet *p);
320 
330  virtual NetworkRecvStatus Receive_SERVER_FRAME(Packet *p);
331 
339  virtual NetworkRecvStatus Receive_SERVER_SYNC(Packet *p);
340 
347  virtual NetworkRecvStatus Receive_CLIENT_ACK(Packet *p);
348 
360  virtual NetworkRecvStatus Receive_CLIENT_COMMAND(Packet *p);
361 
374  virtual NetworkRecvStatus Receive_SERVER_COMMAND(Packet *p);
375 
385  virtual NetworkRecvStatus Receive_CLIENT_CHAT(Packet *p);
386 
395  virtual NetworkRecvStatus Receive_SERVER_CHAT(Packet *p);
396 
402  virtual NetworkRecvStatus Receive_CLIENT_SET_PASSWORD(Packet *p);
403 
409  virtual NetworkRecvStatus Receive_CLIENT_SET_NAME(Packet *p);
410 
415  virtual NetworkRecvStatus Receive_CLIENT_QUIT(Packet *p);
416 
422  virtual NetworkRecvStatus Receive_CLIENT_ERROR(Packet *p);
423 
429  virtual NetworkRecvStatus Receive_SERVER_QUIT(Packet *p);
430 
437  virtual NetworkRecvStatus Receive_SERVER_ERROR_QUIT(Packet *p);
438 
443  virtual NetworkRecvStatus Receive_SERVER_SHUTDOWN(Packet *p);
444 
449  virtual NetworkRecvStatus Receive_SERVER_NEWGAME(Packet *p);
450 
457  virtual NetworkRecvStatus Receive_SERVER_RCON(Packet *p);
458 
465  virtual NetworkRecvStatus Receive_CLIENT_RCON(Packet *p);
466 
474  virtual NetworkRecvStatus Receive_SERVER_CHECK_NEWGRFS(Packet *p);
475 
480  virtual NetworkRecvStatus Receive_CLIENT_NEWGRFS_CHECKED(Packet *p);
481 
488  virtual NetworkRecvStatus Receive_SERVER_MOVE(Packet *p);
489 
496  virtual NetworkRecvStatus Receive_CLIENT_MOVE(Packet *p);
497 
503  virtual NetworkRecvStatus Receive_SERVER_COMPANY_UPDATE(Packet *p);
504 
511  virtual NetworkRecvStatus Receive_SERVER_CONFIG_UPDATE(Packet *p);
512 
513  NetworkRecvStatus HandlePacket(Packet *p);
514 
515  NetworkGameSocketHandler(SOCKET s);
516 public:
518  uint32 last_frame;
521  uint last_packet;
522 
523  NetworkRecvStatus CloseConnection(bool error = true) override;
524 
529  virtual NetworkRecvStatus CloseConnection(NetworkRecvStatus status) = 0;
530  virtual ~NetworkGameSocketHandler() {}
531 
536  inline void SetInfo(NetworkClientInfo *info)
537  {
538  assert(info != nullptr && this->info == nullptr);
539  this->info = info;
540  }
541 
546  inline NetworkClientInfo *GetInfo() const
547  {
548  return this->info;
549  }
550 
551  NetworkRecvStatus ReceivePackets();
552 
553  const char *ReceiveCommand(Packet *p, CommandPacket *cp);
554  void SendCommand(Packet *p, const CommandPacket *cp);
555 };
556 
557 #endif /* NETWORK_CORE_TCP_GAME_H */
Client acknowledges that it has all required NewGRFs.
Definition: tcp_game.h:56
Information about a single company.
Definition: tcp_game.h:42
Server tells everyone that someone is moved to another company.
Definition: tcp_game.h:104
Container for all information known about a client.
Definition: network_base.h:23
Internal entity of a packet.
Definition: packet.h:40
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:108
A server tells that a client has hit an error and did quit.
Definition: tcp_game.h:120
The server is full and has no place for you.
Definition: tcp_game.h:33
Clients sends the (hashed) game password.
Definition: tcp_game.h:60
Base socket handler for all TCP sockets.
Definition: tcp.h:27
uint count
The number of items in the queue.
Definition: tcp_game.h:132
Server distributing the message of a client (or itself).
Definition: tcp_game.h:96
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:86
ClientID
&#39;Unique&#39; identifier to be given to clients
Definition: network_type.h:39
The client tells the server which frame it has executed.
Definition: tcp_game.h:87
Server tells the client what the random state should be.
Definition: tcp_game.h:88
NetworkClientInfo * info
Client info related to this socket.
Definition: tcp_game.h:151
Server welcomes you and gives you your ClientID.
Definition: tcp_game.h:65
Client asks the server to execute some command.
Definition: tcp_game.h:99
Server tells the client that it is beginning to send the map.
Definition: tcp_game.h:71
CommandQueue incoming_queue
The command-queue awaiting handling.
Definition: tcp_game.h:520
Response of the executed command on the server.
Definition: tcp_game.h:100
A server tells that a client has quit.
Definition: tcp_game.h:118
Server distributes a command to (all) the clients.
Definition: tcp_game.h:92
A queue of CommandPackets.
Definition: tcp_game.h:129
Server sends NewGRF IDs and MD5 checksums for the client to check.
Definition: tcp_game.h:55
uint32 last_frame
Last frame we have executed.
Definition: tcp_game.h:518
A client would like to be moved to another company.
Definition: tcp_game.h:103
Server sends you information about a client.
Definition: tcp_game.h:66
CommandQueue()
Initialise the command queue.
Definition: tcp_game.h:136
Client sends the (hashed) company password.
Definition: tcp_game.h:62
Server sends bits of the map to the client.
Definition: tcp_game.h:73
CommandPacket * last
The last packet in the queue; only valid when first != nullptr.
Definition: tcp_game.h:131
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition: core.h:22
A client (re)sets its company&#39;s password.
Definition: tcp_game.h:107
CommandPacket * first
The first packet in the queue.
Definition: tcp_game.h:130
void Free()
Free everything that is in the queue.
uint last_packet
Time we received the last frame.
Definition: tcp_game.h:521
NetworkClientInfo * GetInfo() const
Gets the client info of this socket handler.
Definition: tcp_game.h:546
Server sending an error message to the client.
Definition: tcp_game.h:38
A client reports an error to the server.
Definition: tcp_game.h:119
uint Count() const
Get the number of items in the queue.
Definition: tcp_game.h:144
PacketGameType
Enum with all types of TCP packets.
Definition: tcp_game.h:24
ClientID client_id
Client identifier.
Definition: tcp_game.h:517
uint32 last_frame_server
Last frame the server has executed.
Definition: tcp_game.h:519
Tells clients that a new client has joined.
Definition: tcp_game.h:77
Some network configuration important to the client changed.
Definition: tcp_game.h:110
Client requests the actual map.
Definition: tcp_game.h:69
void SetInfo(NetworkClientInfo *info)
Sets the client info for this socket handler.
Definition: tcp_game.h:536
The server is preparing to start a new game.
Definition: tcp_game.h:113
Client executed a command and sends it to the server.
Definition: tcp_game.h:91
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:112
Server tells it has just sent the last bits of the map to the client.
Definition: tcp_game.h:74
Server tells the client what the (compressed) size of the map is.
Definition: tcp_game.h:72
The client telling the server it wants to join.
Definition: tcp_game.h:37
Must ALWAYS be on the end of this list!! (period)
Definition: tcp_game.h:122
Server tells the client there are some people waiting for the map as well.
Definition: tcp_game.h:70
A client tells the server it is going to quit.
Definition: tcp_game.h:117
Everything we need to know about a command to be able to execute it.
Server requests the (hashed) game password.
Definition: tcp_game.h:59
Server requests the (hashed) company password.
Definition: tcp_game.h:61
Client said something that should be distributed.
Definition: tcp_game.h:95
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:34
Base socket handler for all TCP sockets.
Definition: tcp_game.h:148
~CommandQueue()
Clear the command queue.
Definition: tcp_game.h:138
Basic functions to receive and send TCP packets.
The server is shutting down.
Definition: tcp_game.h:114
Information (password) of a company changed.
Definition: tcp_game.h:109
Request information about all companies.
Definition: tcp_game.h:41
Client tells the server that it received the whole map.
Definition: tcp_game.h:75