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/>.
8 /** @file network_client.h Client part of the network protocol. */
10 #ifndef NETWORK_CLIENT_H
11 #define NETWORK_CLIENT_H
13 #include "network_internal.h"
15 /** Class for handling the client side of the game connection. */
16 class ClientNetworkGameSocketHandler
: public ZeroedMemoryAllocator
, public NetworkGameSocketHandler
{
18 std::string connection_string
; ///< Address we are connected to.
19 std::shared_ptr
<struct PacketReader
> savegame
; ///< Packet reader for reading the savegame.
20 byte token
; ///< The token we need to send back to the server to prove we're the right client.
22 /** Status of the connection with the server. */
24 STATUS_INACTIVE
, ///< The client is not connected nor active.
25 STATUS_JOIN
, ///< We are trying to join a server.
26 STATUS_NEWGRFS_CHECK
, ///< Last action was checking NewGRFs.
27 STATUS_AUTH_GAME
, ///< Last action was requesting game (server) password.
28 STATUS_AUTH_COMPANY
, ///< Last action was requesting company password.
29 STATUS_AUTHORIZED
, ///< The client is authorized at the server.
30 STATUS_MAP_WAIT
, ///< The client is waiting as someone else is downloading the map.
31 STATUS_MAP
, ///< The client is downloading the map.
32 STATUS_ACTIVE
, ///< The client is active within in the game.
33 STATUS_END
, ///< Must ALWAYS be on the end of this list!! (period)
36 ServerStatus status
; ///< Status of the connection with the server.
39 friend void NetworkExecuteLocalCommandQueue();
40 friend void NetworkClose(bool close_admins
);
41 static ClientNetworkGameSocketHandler
*my_client
; ///< This is us!
43 NetworkRecvStatus
Receive_SERVER_FULL(Packet
&p
) override
;
44 NetworkRecvStatus
Receive_SERVER_BANNED(Packet
&p
) override
;
45 NetworkRecvStatus
Receive_SERVER_ERROR(Packet
&p
) override
;
46 NetworkRecvStatus
Receive_SERVER_CLIENT_INFO(Packet
&p
) override
;
47 NetworkRecvStatus
Receive_SERVER_NEED_GAME_PASSWORD(Packet
&p
) override
;
48 NetworkRecvStatus
Receive_SERVER_NEED_COMPANY_PASSWORD(Packet
&p
) override
;
49 NetworkRecvStatus
Receive_SERVER_WELCOME(Packet
&p
) override
;
50 NetworkRecvStatus
Receive_SERVER_WAIT(Packet
&p
) override
;
51 NetworkRecvStatus
Receive_SERVER_MAP_BEGIN(Packet
&p
) override
;
52 NetworkRecvStatus
Receive_SERVER_MAP_SIZE(Packet
&p
) override
;
53 NetworkRecvStatus
Receive_SERVER_MAP_DATA(Packet
&p
) override
;
54 NetworkRecvStatus
Receive_SERVER_MAP_DONE(Packet
&p
) override
;
55 NetworkRecvStatus
Receive_SERVER_JOIN(Packet
&p
) override
;
56 NetworkRecvStatus
Receive_SERVER_FRAME(Packet
&p
) override
;
57 NetworkRecvStatus
Receive_SERVER_SYNC(Packet
&p
) override
;
58 NetworkRecvStatus
Receive_SERVER_COMMAND(Packet
&p
) override
;
59 NetworkRecvStatus
Receive_SERVER_CHAT(Packet
&p
) override
;
60 NetworkRecvStatus
Receive_SERVER_EXTERNAL_CHAT(Packet
&p
) override
;
61 NetworkRecvStatus
Receive_SERVER_QUIT(Packet
&p
) override
;
62 NetworkRecvStatus
Receive_SERVER_ERROR_QUIT(Packet
&p
) override
;
63 NetworkRecvStatus
Receive_SERVER_SHUTDOWN(Packet
&p
) override
;
64 NetworkRecvStatus
Receive_SERVER_NEWGAME(Packet
&p
) override
;
65 NetworkRecvStatus
Receive_SERVER_RCON(Packet
&p
) override
;
66 NetworkRecvStatus
Receive_SERVER_CHECK_NEWGRFS(Packet
&p
) override
;
67 NetworkRecvStatus
Receive_SERVER_MOVE(Packet
&p
) override
;
68 NetworkRecvStatus
Receive_SERVER_COMPANY_UPDATE(Packet
&p
) override
;
69 NetworkRecvStatus
Receive_SERVER_CONFIG_UPDATE(Packet
&p
) override
;
71 static NetworkRecvStatus
SendNewGRFsOk();
72 static NetworkRecvStatus
SendGetMap();
73 static NetworkRecvStatus
SendMapOk();
74 void CheckConnection();
76 ClientNetworkGameSocketHandler(SOCKET s
, const std::string
&connection_string
);
77 ~ClientNetworkGameSocketHandler();
79 NetworkRecvStatus
CloseConnection(NetworkRecvStatus status
) override
;
80 void ClientError(NetworkRecvStatus res
);
82 static NetworkRecvStatus
SendJoin();
83 static NetworkRecvStatus
SendCommand(const CommandPacket
&cp
);
84 static NetworkRecvStatus
SendError(NetworkErrorCode errorno
);
85 static NetworkRecvStatus
SendQuit();
86 static NetworkRecvStatus
SendAck();
88 static NetworkRecvStatus
SendGamePassword(const std::string
&password
);
89 static NetworkRecvStatus
SendCompanyPassword(const std::string
&password
);
91 static NetworkRecvStatus
SendChat(NetworkAction action
, DestType type
, int dest
, const std::string
&msg
, int64_t data
);
92 static NetworkRecvStatus
SendSetPassword(const std::string
&password
);
93 static NetworkRecvStatus
SendSetName(const std::string
&name
);
94 static NetworkRecvStatus
SendRCon(const std::string
&password
, const std::string
&command
);
95 static NetworkRecvStatus
SendMove(CompanyID company
, const std::string
&password
);
97 static bool IsConnected();
100 static bool Receive();
101 static bool GameLoop();
104 /** Helper to make the code look somewhat nicer. */
105 typedef ClientNetworkGameSocketHandler MyClient
;
107 void NetworkClient_Connected();
108 void NetworkClientSetCompanyPassword(const std::string
&password
);
110 /** Information required to join a server. */
111 struct NetworkJoinInfo
{
112 NetworkJoinInfo() : company(COMPANY_SPECTATOR
) {}
113 std::string connection_string
; ///< The address of the server to join.
114 CompanyID company
; ///< The company to join.
115 std::string server_password
; ///< The password of the server to join.
116 std::string company_password
; ///< The password of the company to join.
119 extern NetworkJoinInfo _network_join
;
121 #endif /* NETWORK_CLIENT_H */