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 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_GAME_INFO
, ///< We are trying to get the game information.
26 STATUS_COMPANY_INFO
, ///< We are trying to get company information.
27 STATUS_JOIN
, ///< We are trying to join a server.
28 STATUS_NEWGRFS_CHECK
, ///< Last action was checking NewGRFs.
29 STATUS_AUTH_GAME
, ///< Last action was requesting game (server) password.
30 STATUS_AUTH_COMPANY
, ///< Last action was requesting company password.
31 STATUS_AUTHORIZED
, ///< The client is authorized at the server.
32 STATUS_MAP_WAIT
, ///< The client is waiting as someone else is downloading the map.
33 STATUS_MAP
, ///< The client is downloading the map.
34 STATUS_ACTIVE
, ///< The client is active within in the game.
35 STATUS_END
, ///< Must ALWAYS be on the end of this list!! (period)
38 ServerStatus status
; ///< Status of the connection with the server.
41 friend void NetworkExecuteLocalCommandQueue();
42 friend void NetworkClose(bool close_admins
);
43 static ClientNetworkGameSocketHandler
*my_client
; ///< This is us!
45 NetworkRecvStatus
Receive_SERVER_FULL(Packet
*p
) override
;
46 NetworkRecvStatus
Receive_SERVER_BANNED(Packet
*p
) override
;
47 NetworkRecvStatus
Receive_SERVER_ERROR(Packet
*p
) override
;
48 NetworkRecvStatus
Receive_SERVER_GAME_INFO(Packet
*p
) override
;
49 NetworkRecvStatus
Receive_SERVER_COMPANY_INFO(Packet
*p
) override
;
50 NetworkRecvStatus
Receive_SERVER_CLIENT_INFO(Packet
*p
) override
;
51 NetworkRecvStatus
Receive_SERVER_NEED_GAME_PASSWORD(Packet
*p
) override
;
52 NetworkRecvStatus
Receive_SERVER_NEED_COMPANY_PASSWORD(Packet
*p
) override
;
53 NetworkRecvStatus
Receive_SERVER_WELCOME(Packet
*p
) override
;
54 NetworkRecvStatus
Receive_SERVER_WAIT(Packet
*p
) override
;
55 NetworkRecvStatus
Receive_SERVER_MAP_BEGIN(Packet
*p
) override
;
56 NetworkRecvStatus
Receive_SERVER_MAP_SIZE(Packet
*p
) override
;
57 NetworkRecvStatus
Receive_SERVER_MAP_DATA(Packet
*p
) override
;
58 NetworkRecvStatus
Receive_SERVER_MAP_DONE(Packet
*p
) override
;
59 NetworkRecvStatus
Receive_SERVER_JOIN(Packet
*p
) override
;
60 NetworkRecvStatus
Receive_SERVER_FRAME(Packet
*p
) override
;
61 NetworkRecvStatus
Receive_SERVER_SYNC(Packet
*p
) override
;
62 NetworkRecvStatus
Receive_SERVER_COMMAND(Packet
*p
) override
;
63 NetworkRecvStatus
Receive_SERVER_CHAT(Packet
*p
) override
;
64 NetworkRecvStatus
Receive_SERVER_QUIT(Packet
*p
) override
;
65 NetworkRecvStatus
Receive_SERVER_ERROR_QUIT(Packet
*p
) override
;
66 NetworkRecvStatus
Receive_SERVER_SHUTDOWN(Packet
*p
) override
;
67 NetworkRecvStatus
Receive_SERVER_NEWGAME(Packet
*p
) override
;
68 NetworkRecvStatus
Receive_SERVER_RCON(Packet
*p
) override
;
69 NetworkRecvStatus
Receive_SERVER_CHECK_NEWGRFS(Packet
*p
) override
;
70 NetworkRecvStatus
Receive_SERVER_MOVE(Packet
*p
) override
;
71 NetworkRecvStatus
Receive_SERVER_COMPANY_UPDATE(Packet
*p
) override
;
72 NetworkRecvStatus
Receive_SERVER_CONFIG_UPDATE(Packet
*p
) override
;
74 static NetworkRecvStatus
SendNewGRFsOk();
75 static NetworkRecvStatus
SendGetMap();
76 static NetworkRecvStatus
SendMapOk();
77 void CheckConnection();
79 ClientNetworkGameSocketHandler(SOCKET s
, const std::string
&connection_string
);
80 ~ClientNetworkGameSocketHandler();
82 NetworkRecvStatus
CloseConnection(NetworkRecvStatus status
) override
;
83 void ClientError(NetworkRecvStatus res
);
85 static NetworkRecvStatus
SendInformationQuery(bool request_company_info
);
87 static NetworkRecvStatus
SendJoin();
88 static NetworkRecvStatus
SendCommand(const CommandPacket
*cp
);
89 static NetworkRecvStatus
SendError(NetworkErrorCode errorno
);
90 static NetworkRecvStatus
SendQuit();
91 static NetworkRecvStatus
SendAck();
93 static NetworkRecvStatus
SendGamePassword(const std::string
&password
);
94 static NetworkRecvStatus
SendCompanyPassword(const std::string
&password
);
96 static NetworkRecvStatus
SendChat(NetworkAction action
, DestType type
, int dest
, const std::string
&msg
, int64 data
);
97 static NetworkRecvStatus
SendSetPassword(const std::string
&password
);
98 static NetworkRecvStatus
SendSetName(const std::string
&name
);
99 static NetworkRecvStatus
SendRCon(const std::string
&password
, const std::string
&command
);
100 static NetworkRecvStatus
SendMove(CompanyID company
, const std::string
&password
);
102 static bool IsConnected();
105 static bool Receive();
106 static bool GameLoop();
109 /** Helper to make the code look somewhat nicer. */
110 typedef ClientNetworkGameSocketHandler MyClient
;
112 void NetworkClient_Connected();
113 void NetworkClientSetCompanyPassword(const std::string
&password
);
115 /** Information required to join a server. */
116 struct NetworkJoinInfo
{
117 NetworkJoinInfo() : company(COMPANY_SPECTATOR
) {}
118 std::string connection_string
; ///< The address of the server to join.
119 CompanyID company
; ///< The company to join.
120 std::string server_password
; ///< The password of the server to join.
121 std::string company_password
; ///< The password of the company to join.
124 extern NetworkJoinInfo _network_join
;
126 #endif /* NETWORK_CLIENT_H */