OpenTTD Source  20240919-master-gdf0233f4c2
network_client.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 
10 #ifndef NETWORK_CLIENT_H
11 #define NETWORK_CLIENT_H
12 
13 #include "network_internal.h"
14 
17 private:
18  std::unique_ptr<class NetworkAuthenticationClientHandler> authentication_handler;
19  std::string connection_string;
20  std::shared_ptr<struct PacketReader> savegame;
21  uint8_t token;
22 
24  enum ServerStatus {
35  };
36 
38 
39 protected:
40  friend void NetworkExecuteLocalCommandQueue();
41  friend void NetworkClose(bool close_admins);
43 
70 
74  static NetworkRecvStatus SendIdentify();
75  void CheckConnection();
76 public:
77  ClientNetworkGameSocketHandler(SOCKET s, const std::string &connection_string);
79 
82 
83  static NetworkRecvStatus SendJoin();
86  static NetworkRecvStatus SendQuit();
87  static NetworkRecvStatus SendAck();
88 
90 
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);
95 
96  static bool IsConnected();
97 
98  static void Send();
99  static bool Receive();
100  static bool GameLoop();
101 };
102 
105 
107 
111  std::string connection_string;
113  std::string server_password;
114 };
115 
117 
118 #endif /* NETWORK_CLIENT_H */
ClientNetworkGameSocketHandler::STATUS_ENCRYPTED
@ STATUS_ENCRYPTED
The game authentication has completed and from here on the connection to the server is encrypted.
Definition: network_client.h:28
ClientNetworkGameSocketHandler::SendNewGRFsOk
static NetworkRecvStatus SendNewGRFsOk()
Tell the server we got all the NewGRFs.
Definition: network_client.cpp:359
ClientNetworkGameSocketHandler::STATUS_END
@ STATUS_END
Must ALWAYS be on the end of this list!! (period)
Definition: network_client.h:34
ClientNetworkGameSocketHandler::Receive_SERVER_MAP_DONE
NetworkRecvStatus Receive_SERVER_MAP_DONE(Packet &p) override
Sends that all data of the map are sent to the client:
Definition: network_client.cpp:842
ClientNetworkGameSocketHandler::Receive_SERVER_EXTERNAL_CHAT
NetworkRecvStatus Receive_SERVER_EXTERNAL_CHAT(Packet &p) override
Sends a chat-packet for external source to the client: string Name of the source this message came fr...
Definition: network_client.cpp:1028
ClientNetworkGameSocketHandler::Receive_SERVER_MOVE
NetworkRecvStatus Receive_SERVER_MOVE(Packet &p) override
Move a client from one company into another: uint32_t ID of the client.
Definition: network_client.cpp:1155
ClientNetworkGameSocketHandler::savegame
std::shared_ptr< struct PacketReader > savegame
Packet reader for reading the savegame.
Definition: network_client.h:20
ClientNetworkGameSocketHandler::ClientNetworkGameSocketHandler
ClientNetworkGameSocketHandler(SOCKET s, const std::string &connection_string)
Create a new socket for the client side of the game connection.
Definition: network_client.cpp:146
NetworkAction
NetworkAction
Actions that can be used for NetworkTextMessage.
Definition: network_type.h:90
ClientNetworkGameSocketHandler::Receive_SERVER_QUIT
NetworkRecvStatus Receive_SERVER_QUIT(Packet &p) override
Notification that a client left the game: uint32_t ID of the client.
Definition: network_client.cpp:1065
ClientNetworkGameSocketHandler::Receive_SERVER_CLIENT_INFO
NetworkRecvStatus Receive_SERVER_CLIENT_INFO(Packet &p) override
Send information about a client: uint32_t ID of the client (always unique on a server.
Definition: network_client.cpp:563
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
ClientNetworkGameSocketHandler::CheckConnection
void CheckConnection()
Check the connection's state, i.e.
Definition: network_client.cpp:1202
NetworkJoinInfo::server_password
std::string server_password
The password of the server to join.
Definition: network_client.h:113
ClientNetworkGameSocketHandler::GameLoop
static bool GameLoop()
Actual game loop for the client.
Definition: network_client.cpp:268
ClientNetworkGameSocketHandler::SendChat
static NetworkRecvStatus SendChat(NetworkAction action, DestType type, int dest, const std::string &msg, int64_t data)
Send a chat-packet over the network.
Definition: network_client.cpp:438
ClientNetworkGameSocketHandler::Receive_SERVER_MAP_DATA
NetworkRecvStatus Receive_SERVER_MAP_DATA(Packet &p) override
Sends the data of the map to the client: Contains a part of the map (until max size of packet).
Definition: network_client.cpp:828
ClientNetworkGameSocketHandler::Receive_SERVER_ENABLE_ENCRYPTION
NetworkRecvStatus Receive_SERVER_ENABLE_ENCRYPTION(Packet &p) override
Indication to the client that authentication is complete and encryption has to be used from here on f...
Definition: network_client.cpp:743
NetworkJoinInfo::connection_string
std::string connection_string
The address of the server to join.
Definition: network_client.h:111
DestType
DestType
Destination of our chat messages.
Definition: network_type.h:79
ClientNetworkGameSocketHandler::SendCommand
static NetworkRecvStatus SendCommand(const CommandPacket &cp)
Send a command to the server.
Definition: network_client.cpp:426
ClientNetworkGameSocketHandler::my_client
static ClientNetworkGameSocketHandler * my_client
This is us!
Definition: network_client.h:42
ClientNetworkGameSocketHandler::Receive_SERVER_CONFIG_UPDATE
NetworkRecvStatus Receive_SERVER_CONFIG_UPDATE(Packet &p) override
Update the clients knowledge of the max settings: uint8_t Maximum number of companies allowed.
Definition: network_client.cpp:1185
ClientNetworkGameSocketHandler::Receive_SERVER_SYNC
NetworkRecvStatus Receive_SERVER_SYNC(Packet &p) override
Sends a sync-check to the client: uint32_t Frame counter.
Definition: network_client.cpp:941
ClientNetworkGameSocketHandler::CloseConnection
NetworkRecvStatus CloseConnection(NetworkRecvStatus status) override
Close the network connection due to the given status.
Definition: network_client.cpp:161
NetworkGameSocketHandler
Base socket handler for all TCP sockets.
Definition: tcp_game.h:141
ClientNetworkGameSocketHandler::Receive_SERVER_MAP_SIZE
NetworkRecvStatus Receive_SERVER_MAP_SIZE(Packet &p) override
Sends the size of the map to the client.
Definition: network_client.cpp:815
ClientNetworkGameSocketHandler::Receive_SERVER_ERROR
NetworkRecvStatus Receive_SERVER_ERROR(Packet &p) override
The client made an error: uint8_t Error code caused (see NetworkErrorCode).
Definition: network_client.cpp:624
ClientNetworkGameSocketHandler::STATUS_INACTIVE
@ STATUS_INACTIVE
The client is not connected nor active.
Definition: network_client.h:25
ClientNetworkGameSocketHandler::SendJoin
static NetworkRecvStatus SendJoin()
Tell the server we would like to join.
Definition: network_client.cpp:329
ClientNetworkGameSocketHandler::Receive_SERVER_SHUTDOWN
NetworkRecvStatus Receive_SERVER_SHUTDOWN(Packet &p) override
Let the clients know that the server is closing.
Definition: network_client.cpp:1105
ClientNetworkGameSocketHandler::Receive_SERVER_ERROR_QUIT
NetworkRecvStatus Receive_SERVER_ERROR_QUIT(Packet &p) override
Inform all clients that one client made an error and thus has quit/been disconnected: uint32_t ID of ...
Definition: network_client.cpp:1046
NetworkJoinInfo::company
CompanyID company
The company to join.
Definition: network_client.h:112
ClientNetworkGameSocketHandler::STATUS_MAP
@ STATUS_MAP
The client is downloading the map.
Definition: network_client.h:32
ClientNetworkGameSocketHandler::Receive_SERVER_FULL
NetworkRecvStatus Receive_SERVER_FULL(Packet &p) override
Notification that the server is full.
Definition: network_client.cpp:540
ClientNetworkGameSocketHandler::SendSetName
static NetworkRecvStatus SendSetName(const std::string &name)
Tell the server that we like to change the name of the client.
Definition: network_client.cpp:470
CommandPacket
Everything we need to know about a command to be able to execute it.
Definition: network_internal.h:94
ClientNetworkGameSocketHandler::IsConnected
static bool IsConnected()
Check whether the client is actually connected (and in the game).
Definition: network_client.cpp:528
ZeroedMemoryAllocator
Base class that provides memory initialization on dynamically created objects.
Definition: alloc_type.hpp:85
ClientNetworkGameSocketHandler::connection_string
std::string connection_string
Address we are connected to.
Definition: network_client.h:19
ClientNetworkGameSocketHandler::SendMove
static NetworkRecvStatus SendMove(CompanyID company)
Ask the server to move us.
Definition: network_client.cpp:514
ClientNetworkGameSocketHandler::STATUS_JOIN
@ STATUS_JOIN
We are trying to join a server.
Definition: network_client.h:26
ClientNetworkGameSocketHandler::Receive_SERVER_NEWGAME
NetworkRecvStatus Receive_SERVER_NEWGAME(Packet &p) override
Let the clients know that the server is loading a new map.
Definition: network_client.cpp:1120
ClientNetworkGameSocketHandler::ServerStatus
ServerStatus
Status of the connection with the server.
Definition: network_client.h:24
ClientNetworkGameSocketHandler::SendRCon
static NetworkRecvStatus SendRCon(const std::string &password, const std::string &command)
Send a console command.
Definition: network_client.cpp:499
ClientNetworkGameSocketHandler::Receive_SERVER_CHECK_NEWGRFS
NetworkRecvStatus Receive_SERVER_CHECK_NEWGRFS(Packet &p) override
Sends information about all used GRFs to the client: uint8_t Amount of GRFs (the following data is re...
Definition: network_client.cpp:673
ClientNetworkGameSocketHandler::STATUS_MAP_WAIT
@ STATUS_MAP_WAIT
The client is waiting as someone else is downloading the map.
Definition: network_client.h:31
Packet
Internal entity of a packet.
Definition: packet.h:42
ClientNetworkGameSocketHandler::Receive
static bool Receive()
Check whether we received/can send some data from/to the server and when that's the case handle it ap...
Definition: network_client.cpp:243
_network_join
NetworkJoinInfo _network_join
Information about the game to join to.
Definition: network_client.cpp:322
ClientNetworkGameSocketHandler::STATUS_AUTHORIZED
@ STATUS_AUTHORIZED
The client is authorized at the server.
Definition: network_client.h:30
NetworkJoinInfo
Information required to join a server.
Definition: network_client.h:109
NetworkRecvStatus
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition: core.h:23
ClientNetworkGameSocketHandler::SendError
static NetworkRecvStatus SendError(NetworkErrorCode errorno)
Send an error-packet over the network.
Definition: network_client.cpp:455
ClientNetworkGameSocketHandler::ClientError
void ClientError(NetworkRecvStatus res)
Handle an error coming from the client side.
Definition: network_client.cpp:189
network_internal.h
ClientNetworkGameSocketHandler::STATUS_AUTH_GAME
@ STATUS_AUTH_GAME
Last action was requesting game (server) password.
Definition: network_client.h:27
ClientNetworkGameSocketHandler::Receive_SERVER_WAIT
NetworkRecvStatus Receive_SERVER_WAIT(Packet &p) override
Notification that another client is currently receiving the map: uint8_t Number of clients waiting in...
Definition: network_client.cpp:775
ClientNetworkGameSocketHandler::NetworkExecuteLocalCommandQueue
friend void NetworkExecuteLocalCommandQueue()
Execute all commands on the local command queue that ought to be executed this frame.
Definition: network_command.cpp:245
ClientNetworkGameSocketHandler::Receive_SERVER_RCON
NetworkRecvStatus Receive_SERVER_RCON(Packet &p) override
Send the result of an issues RCon command back to the client: uint16_t Colour code.
Definition: network_client.cpp:1139
ClientNetworkGameSocketHandler::~ClientNetworkGameSocketHandler
~ClientNetworkGameSocketHandler()
Clear whatever we assigned.
Definition: network_client.cpp:153
ClientNetworkGameSocketHandler
Class for handling the client side of the game connection.
Definition: network_client.h:16
ClientNetworkGameSocketHandler::SendAuthResponse
static NetworkRecvStatus SendAuthResponse()
Set the game password as requested.
Definition: network_client.cpp:372
ClientNetworkGameSocketHandler::Receive_SERVER_FRAME
NetworkRecvStatus Receive_SERVER_FRAME(Packet &p) override
Sends the current frame counter to the client: uint32_t Frame counter uint32_t Frame counter max (how...
Definition: network_client.cpp:906
ClientNetworkGameSocketHandler::STATUS_NEWGRFS_CHECK
@ STATUS_NEWGRFS_CHECK
Last action was checking NewGRFs.
Definition: network_client.h:29
ClientNetworkGameSocketHandler::SendGetMap
static NetworkRecvStatus SendGetMap()
Request the map from the server.
Definition: network_client.cpp:384
COMPANY_SPECTATOR
@ COMPANY_SPECTATOR
The client is spectating.
Definition: company_type.h:35
ClientNetworkGameSocketHandler::SendMapOk
static NetworkRecvStatus SendMapOk()
Tell the server we received the complete map.
Definition: network_client.cpp:397
ClientNetworkGameSocketHandler::Receive_SERVER_WELCOME
NetworkRecvStatus Receive_SERVER_WELCOME(Packet &p) override
The client is joined and ready to receive their map: uint32_t Own client ID.
Definition: network_client.cpp:761
ClientNetworkGameSocketHandler::SendAck
static NetworkRecvStatus SendAck()
Send an acknowledgement from the server's ticks.
Definition: network_client.cpp:410
ClientNetworkGameSocketHandler::SendQuit
static NetworkRecvStatus SendQuit()
Tell the server we would like to quit.
Definition: network_client.cpp:484
ClientNetworkGameSocketHandler::NetworkClose
friend void NetworkClose(bool close_admins)
Close current connections.
Definition: network.cpp:594
ClientNetworkGameSocketHandler::STATUS_ACTIVE
@ STATUS_ACTIVE
The client is active within in the game.
Definition: network_client.h:33
ClientNetworkGameSocketHandler::status
ServerStatus status
Status of the connection with the server.
Definition: network_client.h:37
ClientNetworkGameSocketHandler::Receive_SERVER_CHAT
NetworkRecvStatus Receive_SERVER_CHAT(Packet &p) override
Sends a chat-packet to the client: uint8_t ID of the action (see NetworkAction).
Definition: network_client.cpp:977
ClientNetworkGameSocketHandler::authentication_handler
std::unique_ptr< class NetworkAuthenticationClientHandler > authentication_handler
The handler for the authentication.
Definition: network_client.h:18
ClientNetworkGameSocketHandler::token
uint8_t token
The token we need to send back to the server to prove we're the right client.
Definition: network_client.h:21
ClientNetworkGameSocketHandler::Receive_SERVER_JOIN
NetworkRecvStatus Receive_SERVER_JOIN(Packet &p) override
A client joined (PACKET_CLIENT_MAP_OK), what usually directly follows is a PACKET_SERVER_CLIENT_INFO:...
Definition: network_client.cpp:1087
ClientNetworkGameSocketHandler::Receive_SERVER_MAP_BEGIN
NetworkRecvStatus Receive_SERVER_MAP_BEGIN(Packet &p) override
Sends that the server will begin with sending the map to the client: uint32_t Current frame.
Definition: network_client.cpp:791
ClientNetworkGameSocketHandler::Send
static void Send()
Send the packets of this socket handler.
Definition: network_client.cpp:258
NetworkClient_Connected
void NetworkClient_Connected()
Is called after a client is connected to the server.
Definition: network_client.cpp:1229
ClientNetworkGameSocketHandler::Receive_SERVER_AUTH_REQUEST
NetworkRecvStatus Receive_SERVER_AUTH_REQUEST(Packet &p) override
Indication to the client that it needs to authenticate: uint8_t The NetworkAuthenticationMethod to us...
Definition: network_client.cpp:718
NetworkErrorCode
NetworkErrorCode
The error codes we send around in the protocols.
Definition: network_type.h:110
ClientNetworkGameSocketHandler::Receive_SERVER_COMMAND
NetworkRecvStatus Receive_SERVER_COMMAND(Packet &p) override
Sends a DoCommand to the client: uint8_t ID of the company (0..MAX_COMPANIES-1).
Definition: network_client.cpp:956
MyClient
ClientNetworkGameSocketHandler MyClient
Helper to make the code look somewhat nicer.
Definition: network_client.h:104
ClientNetworkGameSocketHandler::Receive_SERVER_BANNED
NetworkRecvStatus Receive_SERVER_BANNED(Packet &p) override
Notification that the client trying to join is banned.
Definition: network_client.cpp:550