OpenTTD
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 
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. */
39 
40  /* Packets used by the client to join and an error message when the revision is wrong. */
43 
44  /* Packets used for the pre-game lobby. */
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. */
61 
62  /* Checking the game, and then company passwords. */
67 
68  /* The server welcomes the authenticated client and sends information of other clients. */
71 
72  /* Getting the savegame/map. */
80 
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. */
93 
94  /* Sending commands around. */
97 
98  /* Human communication! */
101 
102  /* Remote console. */
105 
106  /* Moving a client.*/
109 
110  /* Configuration updates. */
115 
116  /* A server quitting this game. */
119 
120  /* A client quitting. */
125 
127 };
128 
130 struct CommandPacket;
131 
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 
153 /* TODO: rewrite into a proper class */
154 private:
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:
522  uint32 last_frame;
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 */
Client acknowledges that it has all required NewGRFs.
Definition: tcp_game.h:60
Information about a single company.
Definition: tcp_game.h:46
Server tells everyone that someone is moved to another company.
Definition: tcp_game.h:108
Container for all information known about a client.
Definition: network_base.h:27
Internal entity of a packet.
Definition: packet.h:44
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:112
A server tells that a client has hit an error and did quit.
Definition: tcp_game.h:124
The server is full and has no place for you.
Definition: tcp_game.h:37
Clients sends the (hashed) game password.
Definition: tcp_game.h:64
Base socket handler for all TCP sockets.
Definition: tcp.h:31
uint count
The number of items in the queue.
Definition: tcp_game.h:136
Server distributing the message of a client (or itself).
Definition: tcp_game.h:100
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:90
The client tells the server which frame it has executed.
Definition: tcp_game.h:91
Server tells the client what the random state should be.
Definition: tcp_game.h:92
NetworkClientInfo * info
Client info related to this socket.
Definition: tcp_game.h:155
Server welcomes you and gives you your ClientID.
Definition: tcp_game.h:69
Client asks the server to execute some command.
Definition: tcp_game.h:103
Server tells the client that it is beginning to send the map.
Definition: tcp_game.h:75
CommandQueue incoming_queue
The command-queue awaiting handling.
Definition: tcp_game.h:524
Response of the executed command on the server.
Definition: tcp_game.h:104
A server tells that a client has quit.
Definition: tcp_game.h:122
Server distributes a command to (all) the clients.
Definition: tcp_game.h:96
A queue of CommandPackets.
Definition: tcp_game.h:133
Server sends NewGRF IDs and MD5 checksums for the client to check.
Definition: tcp_game.h:59
uint32 last_frame
Last frame we have executed.
Definition: tcp_game.h:522
A client would like to be moved to another company.
Definition: tcp_game.h:107
Server sends you information about a client.
Definition: tcp_game.h:70
CommandQueue()
Initialise the command queue.
Definition: tcp_game.h:140
Client sends the (hashed) company password.
Definition: tcp_game.h:66
Server sends bits of the map to the client.
Definition: tcp_game.h:77
CommandPacket * last
The last packet in the queue; only valid when first != NULL.
Definition: tcp_game.h:135
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition: core.h:26
A client (re)sets its company&#39;s password.
Definition: tcp_game.h:111
CommandPacket * first
The first packet in the queue.
Definition: tcp_game.h:134
void Free()
Free everything that is in the queue.
uint last_packet
Time we received the last frame.
Definition: tcp_game.h:525
NetworkClientInfo * GetInfo() const
Gets the client info of this socket handler.
Definition: tcp_game.h:550
Server sending an error message to the client.
Definition: tcp_game.h:42
A client reports an error to the server.
Definition: tcp_game.h:123
uint Count() const
Get the number of items in the queue.
Definition: tcp_game.h:148
PacketGameType
Enum with all types of TCP packets.
Definition: tcp_game.h:28
ClientID client_id
Client identifier.
Definition: tcp_game.h:521
uint32 last_frame_server
Last frame the server has executed.
Definition: tcp_game.h:523
Tells clients that a new client has joined.
Definition: tcp_game.h:81
Some network configuration important to the client changed.
Definition: tcp_game.h:114
Client requests the actual map.
Definition: tcp_game.h:73
void SetInfo(NetworkClientInfo *info)
Sets the client info for this socket handler.
Definition: tcp_game.h:540
The server is preparing to start a new game.
Definition: tcp_game.h:117
Client executed a command and sends it to the server.
Definition: tcp_game.h:95
ClientID
&#39;Unique&#39; identifier to be given to clients
Definition: network_type.h:43
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:110
Server tells it has just sent the last bits of the map to the client.
Definition: tcp_game.h:78
Server tells the client what the (compressed) size of the map is.
Definition: tcp_game.h:76
The client telling the server it wants to join.
Definition: tcp_game.h:41
Must ALWAYS be on the end of this list!! (period)
Definition: tcp_game.h:126
Server tells the client there are some people waiting for the map as well.
Definition: tcp_game.h:74
A client tells the server it is going to quit.
Definition: tcp_game.h:121
Everything we need to know about a command to be able to execute it.
Server requests the (hashed) game password.
Definition: tcp_game.h:63
Server requests the (hashed) company password.
Definition: tcp_game.h:65
Client said something that should be distributed.
Definition: tcp_game.h:99
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:38
Base socket handler for all TCP sockets.
Definition: tcp_game.h:152
~CommandQueue()
Clear the command queue.
Definition: tcp_game.h:142
Basic functions to receive and send TCP packets.
The server is shutting down.
Definition: tcp_game.h:118
Information (password) of a company changed.
Definition: tcp_game.h:113
Request information about all companies.
Definition: tcp_game.h:45
Client tells the server that it received the whole map.
Definition: tcp_game.h:79