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::unique_ptr
<class NetworkAuthenticationClientHandler
> authentication_handler
; ///< The handler for the authentication.
19 std::string connection_string
; ///< Address we are connected to.
20 std::shared_ptr
<struct PacketReader
> savegame
; ///< Packet reader for reading the savegame.
21 uint8_t token
; ///< The token we need to send back to the server to prove we're the right client.
23 /** Status of the connection with the server. */
25 STATUS_INACTIVE
, ///< The client is not connected nor active.
26 STATUS_JOIN
, ///< We are trying to join a server.
27 STATUS_AUTH_GAME
, ///< Last action was requesting game (server) password.
28 STATUS_ENCRYPTED
, ///< The game authentication has completed and from here on the connection to the server is encrypted.
29 STATUS_NEWGRFS_CHECK
, ///< Last action was checking NewGRFs.
30 STATUS_AUTHORIZED
, ///< The client is authorized at the server.
31 STATUS_MAP_WAIT
, ///< The client is waiting as someone else is downloading the map.
32 STATUS_MAP
, ///< The client is downloading the map.
33 STATUS_ACTIVE
, ///< The client is active within in the game.
34 STATUS_END
, ///< Must ALWAYS be on the end of this list!! (period)
37 ServerStatus status
; ///< Status of the connection with the server.
40 friend void NetworkExecuteLocalCommandQueue();
41 friend void NetworkClose(bool close_admins
);
42 static ClientNetworkGameSocketHandler
*my_client
; ///< This is us!
44 NetworkRecvStatus
Receive_SERVER_FULL(Packet
&p
) override
;
45 NetworkRecvStatus
Receive_SERVER_BANNED(Packet
&p
) override
;
46 NetworkRecvStatus
Receive_SERVER_ERROR(Packet
&p
) override
;
47 NetworkRecvStatus
Receive_SERVER_CLIENT_INFO(Packet
&p
) override
;
48 NetworkRecvStatus
Receive_SERVER_AUTH_REQUEST(Packet
&p
) override
;
49 NetworkRecvStatus
Receive_SERVER_ENABLE_ENCRYPTION(Packet
&p
) override
;
50 NetworkRecvStatus
Receive_SERVER_WELCOME(Packet
&p
) override
;
51 NetworkRecvStatus
Receive_SERVER_WAIT(Packet
&p
) override
;
52 NetworkRecvStatus
Receive_SERVER_MAP_BEGIN(Packet
&p
) override
;
53 NetworkRecvStatus
Receive_SERVER_MAP_SIZE(Packet
&p
) override
;
54 NetworkRecvStatus
Receive_SERVER_MAP_DATA(Packet
&p
) override
;
55 NetworkRecvStatus
Receive_SERVER_MAP_DONE(Packet
&p
) override
;
56 NetworkRecvStatus
Receive_SERVER_JOIN(Packet
&p
) override
;
57 NetworkRecvStatus
Receive_SERVER_FRAME(Packet
&p
) override
;
58 NetworkRecvStatus
Receive_SERVER_SYNC(Packet
&p
) override
;
59 NetworkRecvStatus
Receive_SERVER_COMMAND(Packet
&p
) override
;
60 NetworkRecvStatus
Receive_SERVER_CHAT(Packet
&p
) override
;
61 NetworkRecvStatus
Receive_SERVER_EXTERNAL_CHAT(Packet
&p
) override
;
62 NetworkRecvStatus
Receive_SERVER_QUIT(Packet
&p
) override
;
63 NetworkRecvStatus
Receive_SERVER_ERROR_QUIT(Packet
&p
) override
;
64 NetworkRecvStatus
Receive_SERVER_SHUTDOWN(Packet
&p
) override
;
65 NetworkRecvStatus
Receive_SERVER_NEWGAME(Packet
&p
) override
;
66 NetworkRecvStatus
Receive_SERVER_RCON(Packet
&p
) override
;
67 NetworkRecvStatus
Receive_SERVER_CHECK_NEWGRFS(Packet
&p
) override
;
68 NetworkRecvStatus
Receive_SERVER_MOVE(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 static NetworkRecvStatus
SendIdentify();
75 void CheckConnection();
77 ClientNetworkGameSocketHandler(SOCKET s
, const std::string
&connection_string
);
78 ~ClientNetworkGameSocketHandler();
80 NetworkRecvStatus
CloseConnection(NetworkRecvStatus status
) override
;
81 void ClientError(NetworkRecvStatus res
);
83 static NetworkRecvStatus
SendJoin();
84 static NetworkRecvStatus
SendCommand(const CommandPacket
&cp
);
85 static NetworkRecvStatus
SendError(NetworkErrorCode errorno
);
86 static NetworkRecvStatus
SendQuit();
87 static NetworkRecvStatus
SendAck();
89 static NetworkRecvStatus
SendAuthResponse();
91 static NetworkRecvStatus
SendChat(NetworkAction action
, DestType type
, int dest
, const std::string
&msg
, int64_t data
);
92 static NetworkRecvStatus
SendSetName(const std::string
&name
);
93 static NetworkRecvStatus
SendRCon(const std::string
&password
, const std::string
&command
);
94 static NetworkRecvStatus
SendMove(CompanyID company
);
96 static bool IsConnected();
99 static bool Receive();
100 static bool GameLoop();
103 /** Helper to make the code look somewhat nicer. */
104 typedef ClientNetworkGameSocketHandler MyClient
;
106 void NetworkClient_Connected();
108 /** Information required to join a server. */
109 struct NetworkJoinInfo
{
110 NetworkJoinInfo() : company(COMPANY_SPECTATOR
) {}
111 std::string connection_string
; ///< The address of the server to join.
112 CompanyID company
; ///< The company to join.
113 std::string server_password
; ///< The password of the server to join.
116 extern NetworkJoinInfo _network_join
;
118 #endif /* NETWORK_CLIENT_H */