OpenTTD Source: src/network/network_client.h Source File

OpenTTD Source 1.11.0-RC1

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
16 class ClientNetworkGameSocketHandler : public ZeroedMemoryAllocator, public NetworkGameSocketHandler {
17 private:
18 struct PacketReader *savegame;
19 byte token;
20
22 enum ServerStatus {
23 STATUS_INACTIVE,
24 STATUS_COMPANY_INFO,
25 STATUS_JOIN,
26 STATUS_NEWGRFS_CHECK,
27 STATUS_AUTH_GAME,
28 STATUS_AUTH_COMPANY,
29 STATUS_AUTHORIZED,
30 STATUS_MAP_WAIT,
31 STATUS_MAP,
32 STATUS_ACTIVE,
33 STATUS_END,
34 };
35
36 ServerStatus status;
37
38 protected:
39 friend void NetworkExecuteLocalCommandQueue();
40 friend void NetworkClose(bool close_admins);
41 static ClientNetworkGameSocketHandler *my_client;
42
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_COMPANY_INFO(Packet *p) override;
47 NetworkRecvStatus Receive_SERVER_CLIENT_INFO(Packet *p) override;
48 NetworkRecvStatus Receive_SERVER_NEED_GAME_PASSWORD(Packet *p) override;
49 NetworkRecvStatus Receive_SERVER_NEED_COMPANY_PASSWORD(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_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;
70
71 static NetworkRecvStatus SendNewGRFsOk();
72 static NetworkRecvStatus SendGetMap();
73 static NetworkRecvStatus SendMapOk();
74 void CheckConnection();
75 public:
76 ClientNetworkGameSocketHandler(SOCKET s);
77 ~ClientNetworkGameSocketHandler();
78
79 NetworkRecvStatus CloseConnection(NetworkRecvStatus status) override;
80 void ClientError(NetworkRecvStatus res);
81
82 static NetworkRecvStatus SendCompanyInformationQuery();
83
84 static NetworkRecvStatus SendJoin();
85 static NetworkRecvStatus SendCommand(const CommandPacket *cp);
86 static NetworkRecvStatus SendError(NetworkErrorCode errorno);
87 static NetworkRecvStatus SendQuit();
88 static NetworkRecvStatus SendAck();
89
90 static NetworkRecvStatus SendGamePassword(const char *password);
91 static NetworkRecvStatus SendCompanyPassword(const char *password);
92
93 static NetworkRecvStatus SendChat(NetworkAction action, DestType type, int dest, const char *msg, int64 data);
94 static NetworkRecvStatus SendSetPassword(const char *password);
95 static NetworkRecvStatus SendSetName(const char *name);
96 static NetworkRecvStatus SendRCon(const char *password, const char *command);
97 static NetworkRecvStatus SendMove(CompanyID company, const char *password);
98
99 static bool IsConnected();
100
101 static void Send();
102 static bool Receive();
103 static bool GameLoop();
104 };
105
107 typedef ClientNetworkGameSocketHandler MyClient;
108
109 void NetworkClient_Connected();
110 void NetworkClientSetCompanyPassword(const char *password);
111
112 extern CompanyID _network_join_as;
113
114 extern const char *_network_join_server_password;
115 extern const char *_network_join_company_password;
116
117 #endif /* NETWORK_CLIENT_H */
ClientNetworkGameSocketHandler::Receive_SERVER_FRAME
NetworkRecvStatus Receive_SERVER_FRAME(Packet *p) override
Sends the current frame counter to the client: uint32 Frame counter uint32 Frame counter max (how far...
Definition: network_client.cpp:913
DestType
DestType
Destination of our chat messages.
Definition: network_type.h:81
ClientNetworkGameSocketHandler::SendNewGRFsOk
static NetworkRecvStatus SendNewGRFsOk()
Tell the server we got all the NewGRFs.
Definition: network_client.cpp:371
ClientNetworkGameSocketHandler::STATUS_END
@ STATUS_END
Must ALWAYS be on the end of this list!! (period)
Definition: network_client.h:33
ClientNetworkGameSocketHandler::Receive_SERVER_CHAT
NetworkRecvStatus Receive_SERVER_CHAT(Packet *p) override
Sends a chat-packet to the client: uint8 ID of the action (see NetworkAction).
Definition: network_client.cpp:978
ClientNetworkGameSocketHandler::SendSetName
static NetworkRecvStatus SendSetName(const char *name)
Tell the server that we like to change the name of the client.
Definition: network_client.cpp:488
ClientNetworkGameSocketHandler::Receive_SERVER_COMPANY_INFO
NetworkRecvStatus Receive_SERVER_COMPANY_INFO(Packet *p) override
Sends information about the companies (one packet per company): uint8 Version of the structure of thi...
Definition: network_client.cpp:571
ClientNetworkGameSocketHandler::STATUS_AUTH_COMPANY
@ STATUS_AUTH_COMPANY
Last action was requesting company password.
Definition: network_client.h:28
ClientNetworkGameSocketHandler::SendChat
static NetworkRecvStatus SendChat(NetworkAction action, DestType type, int dest, const char *msg, int64 data)
Send a chat-packet over the network.
Definition: network_client.cpp:447
ClientNetworkGameSocketHandler::Receive_SERVER_BANNED
NetworkRecvStatus Receive_SERVER_BANNED(Packet *p) override
Notification that the client trying to join is banned.
Definition: network_client.cpp:562
_network_join_server_password
const char * _network_join_server_password
Login password from -p argument.
Definition: network_client.cpp:329
NetworkAction
NetworkAction
Actions that can be used for NetworkTextMessage.
Definition: network_type.h:91
ClientNetworkGameSocketHandler::SendMove
static NetworkRecvStatus SendMove(CompanyID company, const char *password)
Ask the server to move us.
Definition: network_client.cpp:527
ClientNetworkGameSocketHandler::Receive_SERVER_COMPANY_UPDATE
NetworkRecvStatus Receive_SERVER_COMPANY_UPDATE(Packet *p) override
Update the clients knowledge of which company is password protected: uint16 Bitwise representation of...
Definition: network_client.cpp:1163
ClientNetworkGameSocketHandler::CheckConnection
void CheckConnection()
Check the connection's state, i.e.
Definition: network_client.cpp:1176
ClientNetworkGameSocketHandler::GameLoop
static bool GameLoop()
Actual game loop for the client.
Definition: network_client.cpp:266
ClientNetworkGameSocketHandler::Receive_SERVER_ERROR
NetworkRecvStatus Receive_SERVER_ERROR(Packet *p) override
The client made an error: uint8 Error code caused (see NetworkErrorCode).
Definition: network_client.cpp:668
ClientNetworkGameSocketHandler::my_client
static ClientNetworkGameSocketHandler * my_client
This is us!
Definition: network_client.h:41
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 Current frame.
Definition: network_client.cpp:811
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
ClientNetworkGameSocketHandler::CloseConnection
NetworkRecvStatus CloseConnection(NetworkRecvStatus status) override
Close the network connection due to the given status.
Definition: network_client.cpp:159
NetworkGameSocketHandler
Base socket handler for all TCP sockets.
Definition: tcp_game.h:149
ClientNetworkGameSocketHandler::Receive_SERVER_CLIENT_INFO
NetworkRecvStatus Receive_SERVER_CLIENT_INFO(Packet *p) override
Send information about a client: uint32 ID of the client (always unique on a server.
Definition: network_client.cpp:615
PacketReader
Read some packets, and when do use that data as initial load filter.
Definition: network_client.cpp:40
ClientNetworkGameSocketHandler::STATUS_INACTIVE
@ STATUS_INACTIVE
The client is not connected nor active.
Definition: network_client.h:23
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:1093
ClientNetworkGameSocketHandler::Receive_SERVER_NEED_COMPANY_PASSWORD
NetworkRecvStatus Receive_SERVER_NEED_COMPANY_PASSWORD(Packet *p) override
Indication to the client that the server needs a company password: uint32 Generation seed.
Definition: network_client.cpp:764
ClientNetworkGameSocketHandler::Receive_SERVER_NEED_GAME_PASSWORD
NetworkRecvStatus Receive_SERVER_NEED_GAME_PASSWORD(Packet *p) override
Indication to the client that the server needs a game password.
Definition: network_client.cpp:749
ClientNetworkGameSocketHandler::SendJoin
static NetworkRecvStatus SendJoin()
Tell the server we would like to join.
Definition: network_client.cpp:354
ClientNetworkGameSocketHandler::STATUS_MAP
@ STATUS_MAP
The client is downloading the map.
Definition: network_client.h:31
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:842
ClientNetworkGameSocketHandler::Receive_SERVER_FULL
NetworkRecvStatus Receive_SERVER_FULL(Packet *p) override
Notification that the server is full.
Definition: network_client.cpp:553
CommandPacket
Everything we need to know about a command to be able to execute it.
Definition: network_internal.h:149
ClientNetworkGameSocketHandler::Receive_SERVER_CHECK_NEWGRFS
NetworkRecvStatus Receive_SERVER_CHECK_NEWGRFS(Packet *p) override
Sends information about all used GRFs to the client: uint8 Amount of GRFs (the following data is repe...
Definition: network_client.cpp:716
ClientNetworkGameSocketHandler::IsConnected
static bool IsConnected()
Check whether the client is actually connected (and in the game).
Definition: network_client.cpp:540
ClientNetworkGameSocketHandler::Receive_SERVER_WELCOME
NetworkRecvStatus Receive_SERVER_WELCOME(Packet *p) override
The client is joined and ready to receive his map: uint32 Own client ID.
Definition: network_client.cpp:783
ZeroedMemoryAllocator
Base class that provides memory initialization on dynamically created objects.
Definition: alloc_type.hpp:85
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:1064
ClientNetworkGameSocketHandler::STATUS_COMPANY_INFO
@ STATUS_COMPANY_INFO
We are trying to get company information.
Definition: network_client.h:24
NetworkClientSetCompanyPassword
void NetworkClientSetCompanyPassword(const char *password)
Set/Reset company password on the client side.
Definition: network_client.cpp:1293
ClientNetworkGameSocketHandler::STATUS_JOIN
@ STATUS_JOIN
We are trying to join a server.
Definition: network_client.h:25
_network_join_company_password
const char * _network_join_company_password
Company password from -P argument.
Definition: network_client.cpp:331
ClientNetworkGameSocketHandler::ServerStatus
ServerStatus
Status of the connection with the server.
Definition: network_client.h:22
ClientNetworkGameSocketHandler::STATUS_MAP_WAIT
@ STATUS_MAP_WAIT
The client is waiting as someone else is downloading the map.
Definition: network_client.h:30
Packet
Internal entity of a packet.
Definition: packet.h:40
ClientNetworkGameSocketHandler::SendCompanyInformationQuery
static NetworkRecvStatus SendCompanyInformationQuery()
Make sure the server ID length is the same as a md5 hash.
Definition: network_client.cpp:342
ClientNetworkGameSocketHandler::ClientNetworkGameSocketHandler
ClientNetworkGameSocketHandler(SOCKET s)
Create a new socket for the client side of the game connection.
Definition: network_client.cpp:144
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:241
ClientNetworkGameSocketHandler::Receive_SERVER_CONFIG_UPDATE
NetworkRecvStatus Receive_SERVER_CONFIG_UPDATE(Packet *p) override
Update the clients knowledge of the max settings: uint8 Maximum number of companies allowed.
Definition: network_client.cpp:1153
ClientNetworkGameSocketHandler::SendSetPassword
static NetworkRecvStatus SendSetPassword(const char *password)
Tell the server that we like to change the password of the company.
Definition: network_client.cpp:475
ClientNetworkGameSocketHandler::SendRCon
static NetworkRecvStatus SendRCon(const char *password, const char *command)
Send a console command.
Definition: network_client.cpp:513
ClientNetworkGameSocketHandler::SendCommand
static NetworkRecvStatus SendCommand(const CommandPacket *cp)
Send a command to the server.
Definition: network_client.cpp:437
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 ID of th...
Definition: network_client.cpp:1027
ClientNetworkGameSocketHandler::STATUS_AUTHORIZED
@ STATUS_AUTHORIZED
The client is authorized at the server.
Definition: network_client.h:29
NetworkRecvStatus
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition: core.h:22
ClientNetworkGameSocketHandler::SendError
static NetworkRecvStatus SendError(NetworkErrorCode errorno)
Send an error-packet over the network.
Definition: network_client.cpp:462
ClientNetworkGameSocketHandler::Receive_SERVER_SYNC
NetworkRecvStatus Receive_SERVER_SYNC(Packet *p) override
Sends a sync-check to the client: uint32 Frame counter.
Definition: network_client.cpp:946
ClientNetworkGameSocketHandler::ClientError
void ClientError(NetworkRecvStatus res)
Handle an error coming from the client side.
Definition: network_client.cpp:191
ClientNetworkGameSocketHandler::token
byte token
The token we need to send back to the server to prove we're the right client.
Definition: network_client.h:19
network_internal.h
ClientNetworkGameSocketHandler::STATUS_AUTH_GAME
@ STATUS_AUTH_GAME
Last action was requesting game (server) password.
Definition: network_client.h:27
ClientNetworkGameSocketHandler::SendCompanyPassword
static NetworkRecvStatus SendCompanyPassword(const char *password)
Set the company password as requested.
Definition: network_client.cpp:394
ClientNetworkGameSocketHandler::NetworkExecuteLocalCommandQueue
friend void NetworkExecuteLocalCommandQueue()
Execute all commands on the local command queue that ought to be executed this frame.
Definition: network_command.cpp:191
ClientNetworkGameSocketHandler::~ClientNetworkGameSocketHandler
~ClientNetworkGameSocketHandler()
Clear whatever we assigned.
Definition: network_client.cpp:151
ClientNetworkGameSocketHandler::Receive_SERVER_COMMAND
NetworkRecvStatus Receive_SERVER_COMMAND(Packet *p) override
Sends a DoCommand to the client: uint8 ID of the company (0..MAX_COMPANIES-1).
Definition: network_client.cpp:959
ClientNetworkGameSocketHandler
Class for handling the client side of the game connection.
Definition: network_client.h:16
ClientNetworkGameSocketHandler::Receive_SERVER_SHUTDOWN
NetworkRecvStatus Receive_SERVER_SHUTDOWN(Packet *p) override
Let the clients know that the server is closing.
Definition: network_client.cpp:1080
ClientNetworkGameSocketHandler::STATUS_NEWGRFS_CHECK
@ STATUS_NEWGRFS_CHECK
Last action was checking NewGRFs.
Definition: network_client.h:26
ClientNetworkGameSocketHandler::SendGetMap
static NetworkRecvStatus SendGetMap()
Request the map from the server.
Definition: network_client.cpp:403
ClientNetworkGameSocketHandler::Receive_SERVER_RCON
NetworkRecvStatus Receive_SERVER_RCON(Packet *p) override
Send the result of an issues RCon command back to the client: uint16 Colour code.
Definition: network_client.cpp:1110
_network_join_as
CompanyID _network_join_as
Who would we like to join as.
Definition: network_client.cpp:326
ClientNetworkGameSocketHandler::SendMapOk
static NetworkRecvStatus SendMapOk()
Tell the server we received the complete map.
Definition: network_client.cpp:413
ClientNetworkGameSocketHandler::savegame
struct PacketReader * savegame
Packet reader for reading the savegame.
Definition: network_client.h:18
ClientNetworkGameSocketHandler::SendAck
static NetworkRecvStatus SendAck()
Send an acknowledgement from the server's ticks.
Definition: network_client.cpp:423
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:856
ClientNetworkGameSocketHandler::SendQuit
static NetworkRecvStatus SendQuit()
Tell the server we would like to quit.
Definition: network_client.cpp:500
ClientNetworkGameSocketHandler::NetworkClose
friend void NetworkClose(bool close_admins)
Close current connections.
Definition: network.cpp:520
ClientNetworkGameSocketHandler::STATUS_ACTIVE
@ STATUS_ACTIVE
The client is active within in the game.
Definition: network_client.h:32
ClientNetworkGameSocketHandler::status
ServerStatus status
Status of the connection with the server.
Definition: network_client.h:36
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:831
ClientNetworkGameSocketHandler::Receive_SERVER_QUIT
NetworkRecvStatus Receive_SERVER_QUIT(Packet *p) override
Notification that a client left the game: uint32 ID of the client.
Definition: network_client.cpp:1044
ClientNetworkGameSocketHandler::Receive_SERVER_MOVE
NetworkRecvStatus Receive_SERVER_MOVE(Packet *p) override
Move a client from one company into another: uint32 ID of the client.
Definition: network_client.cpp:1125
ClientNetworkGameSocketHandler::Send
static void Send()
Send the packets of this socket handler.
Definition: network_client.cpp:256
NetworkClient_Connected
void NetworkClient_Connected()
Is called after a client is connected to the server.
Definition: network_client.cpp:1203
NetworkErrorCode
NetworkErrorCode
The error codes we send around in the protocols.
Definition: network_type.h:110
ClientNetworkGameSocketHandler::SendGamePassword
static NetworkRecvStatus SendGamePassword(const char *password)
Set the game password as requested.
Definition: network_client.cpp:382
MyClient
ClientNetworkGameSocketHandler MyClient
Helper to make the code look somewhat nicer.
Definition: network_client.h:107
ClientNetworkGameSocketHandler::Receive_SERVER_WAIT
NetworkRecvStatus Receive_SERVER_WAIT(Packet *p) override
Notification that another client is currently receiving the map: uint8 Number of clients waiting in f...
Definition: network_client.cpp:798