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

OpenTTD Source 13.1

network_server.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_SERVER_H
11 #define NETWORK_SERVER_H
12
13 #include "network_internal.h"
14 #include "core/tcp_listen.h"
15
16 class ServerNetworkGameSocketHandler;
18 typedef ServerNetworkGameSocketHandler NetworkClientSocket;
20 typedef Pool<NetworkClientSocket, ClientIndex, 8, MAX_CLIENT_SLOTS, PT_NCLIENT> NetworkClientSocketPool;
21 extern NetworkClientSocketPool _networkclientsocket_pool;
22
24 class ServerNetworkGameSocketHandler : public NetworkClientSocketPool::PoolItem<&_networkclientsocket_pool>, public NetworkGameSocketHandler, public TCPListenHandler<ServerNetworkGameSocketHandler, PACKET_SERVER_FULL, PACKET_SERVER_BANNED> {
25 protected:
26 NetworkRecvStatus Receive_CLIENT_JOIN(Packet *p) override;
27 NetworkRecvStatus Receive_CLIENT_GAME_INFO(Packet *p) override;
28 NetworkRecvStatus Receive_CLIENT_GAME_PASSWORD(Packet *p) override;
29 NetworkRecvStatus Receive_CLIENT_COMPANY_PASSWORD(Packet *p) override;
30 NetworkRecvStatus Receive_CLIENT_GETMAP(Packet *p) override;
31 NetworkRecvStatus Receive_CLIENT_MAP_OK(Packet *p) override;
32 NetworkRecvStatus Receive_CLIENT_ACK(Packet *p) override;
33 NetworkRecvStatus Receive_CLIENT_COMMAND(Packet *p) override;
34 NetworkRecvStatus Receive_CLIENT_CHAT(Packet *p) override;
35 NetworkRecvStatus Receive_CLIENT_SET_PASSWORD(Packet *p) override;
36 NetworkRecvStatus Receive_CLIENT_SET_NAME(Packet *p) override;
37 NetworkRecvStatus Receive_CLIENT_QUIT(Packet *p) override;
38 NetworkRecvStatus Receive_CLIENT_ERROR(Packet *p) override;
39 NetworkRecvStatus Receive_CLIENT_RCON(Packet *p) override;
40 NetworkRecvStatus Receive_CLIENT_NEWGRFS_CHECKED(Packet *p) override;
41 NetworkRecvStatus Receive_CLIENT_MOVE(Packet *p) override;
42
43 NetworkRecvStatus SendGameInfo();
44 NetworkRecvStatus SendNewGRFCheck();
45 NetworkRecvStatus SendWelcome();
46 NetworkRecvStatus SendNeedGamePassword();
47 NetworkRecvStatus SendNeedCompanyPassword();
48
49 public:
51 enum ClientStatus {
52 STATUS_INACTIVE,
53 STATUS_NEWGRFS_CHECK,
54 STATUS_AUTH_GAME,
55 STATUS_AUTH_COMPANY,
56 STATUS_AUTHORIZED,
57 STATUS_MAP_WAIT,
58 STATUS_MAP,
59 STATUS_DONE_MAP,
60 STATUS_PRE_ACTIVE,
61 STATUS_ACTIVE,
62 STATUS_END,
63 };
64
65 byte lag_test;
66 byte last_token;
67 uint32 last_token_frame;
68 ClientStatus status;
69 CommandQueue outgoing_queue;
70 size_t receive_limit;
71
72 struct PacketWriter *savegame;
73 NetworkAddress client_address;
74
75 ServerNetworkGameSocketHandler(SOCKET s);
76 ~ServerNetworkGameSocketHandler();
77
78 virtual Packet *ReceivePacket() override;
79 NetworkRecvStatus CloseConnection(NetworkRecvStatus status) override;
80 std::string GetClientName() const;
81
82 void CheckNextClientToSendMap(NetworkClientSocket *ignore_cs = nullptr);
83
84 NetworkRecvStatus SendWait();
85 NetworkRecvStatus SendMap();
86 NetworkRecvStatus SendErrorQuit(ClientID client_id, NetworkErrorCode errorno);
87 NetworkRecvStatus SendQuit(ClientID client_id);
88 NetworkRecvStatus SendShutdown();
89 NetworkRecvStatus SendNewGame();
90 NetworkRecvStatus SendRConResult(uint16 colour, const std::string &command);
91 NetworkRecvStatus SendMove(ClientID client_id, CompanyID company_id);
92
93 NetworkRecvStatus SendClientInfo(NetworkClientInfo *ci);
94 NetworkRecvStatus SendError(NetworkErrorCode error, const std::string &reason = {});
95 NetworkRecvStatus SendChat(NetworkAction action, ClientID client_id, bool self_send, const std::string &msg, int64 data);
96 NetworkRecvStatus SendExternalChat(const std::string &source, TextColour colour, const std::string &user, const std::string &msg);
97 NetworkRecvStatus SendJoin(ClientID client_id);
98 NetworkRecvStatus SendFrame();
99 NetworkRecvStatus SendSync();
100 NetworkRecvStatus SendCommand(const CommandPacket *cp);
101 NetworkRecvStatus SendCompanyUpdate();
102 NetworkRecvStatus SendConfigUpdate();
103
104 static void Send();
105 static void AcceptConnection(SOCKET s, const NetworkAddress &address);
106 static bool AllowConnection();
107
112 static const char *GetName()
113 {
114 return "server";
115 }
116
117 const std::string &GetClientIP();
118
119 static ServerNetworkGameSocketHandler *GetByClientID(ClientID client_id);
120 };
121
122 void NetworkServer_Tick(bool send_frame);
123 void NetworkServerSetCompanyPassword(CompanyID company_id, const std::string &password, bool already_hashed = true);
124 void NetworkServerUpdateCompanyPassworded(CompanyID company_id, bool passworded);
125
126 #endif /* NETWORK_SERVER_H */
ServerNetworkGameSocketHandler::Receive_CLIENT_QUIT
NetworkRecvStatus Receive_CLIENT_QUIT(Packet *p) override
The client is quitting the game.
Definition: network_server.cpp:1107
ServerNetworkGameSocketHandler::SendGameInfo
NetworkRecvStatus SendGameInfo()
Send the client information about the server.
Definition: network_server.cpp:357
ServerNetworkGameSocketHandler::SendChat
NetworkRecvStatus SendChat(NetworkAction action, ClientID client_id, bool self_send, const std::string &msg, int64 data)
Send a chat message.
Definition: network_server.cpp:659
ServerNetworkGameSocketHandler::STATUS_PRE_ACTIVE
@ STATUS_PRE_ACTIVE
The client is catching up the delayed frames.
Definition: network_server.h:60
NetworkAction
NetworkAction
Actions that can be used for NetworkTextMessage.
Definition: network_type.h:99
ServerNetworkGameSocketHandler::Receive_CLIENT_SET_PASSWORD
NetworkRecvStatus Receive_CLIENT_SET_PASSWORD(Packet *p) override
Set the password for the clients current company: string The password.
Definition: network_server.cpp:1339
ServerNetworkGameSocketHandler::receive_limit
size_t receive_limit
Amount of bytes that we can receive at this moment.
Definition: network_server.h:70
ServerNetworkGameSocketHandler::last_token_frame
uint32 last_token_frame
The last frame we received the right token.
Definition: network_server.h:67
ServerNetworkGameSocketHandler::GetName
static const char * GetName()
Get the name used by the listener.
Definition: network_server.h:112
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:257
tcp_listen.h
ServerNetworkGameSocketHandler::ReceivePacket
virtual Packet * ReceivePacket() override
Receives a packet for the given client.
Definition: network_server.cpp:236
ServerNetworkGameSocketHandler::STATUS_MAP
@ STATUS_MAP
The client is downloading the map.
Definition: network_server.h:58
ServerNetworkGameSocketHandler::STATUS_MAP_WAIT
@ STATUS_MAP_WAIT
The client is waiting as someone else is downloading the map.
Definition: network_server.h:57
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
NetworkGameSocketHandler
Base socket handler for all TCP sockets.
Definition: tcp_game.h:154
ServerNetworkGameSocketHandler::Receive_CLIENT_MAP_OK
NetworkRecvStatus Receive_CLIENT_MAP_OK(Packet *p) override
Tell the server that we are done receiving/loading the map.
Definition: network_server.cpp:966
ServerNetworkGameSocketHandler::STATUS_ACTIVE
@ STATUS_ACTIVE
The client is active within in the game.
Definition: network_server.h:61
ServerNetworkGameSocketHandler::SendCommand
NetworkRecvStatus SendCommand(const CommandPacket *cp)
Send a command to the client to execute.
Definition: network_server.cpp:639
ServerNetworkGameSocketHandler::Receive_CLIENT_COMPANY_PASSWORD
NetworkRecvStatus Receive_CLIENT_COMPANY_PASSWORD(Packet *p) override
Send a password to the server to authorize uint8 Password type (see NetworkPasswordType).
Definition: network_server.cpp:924
ServerNetworkGameSocketHandler::Receive_CLIENT_NEWGRFS_CHECKED
NetworkRecvStatus Receive_CLIENT_NEWGRFS_CHECKED(Packet *p) override
Tell the server that we have the required GRFs.
Definition: network_server.cpp:804
ServerNetworkGameSocketHandler::GetClientName
std::string GetClientName() const
Get the name of the client, if the user did not send it yet, Client ID is used.
Definition: network_server.cpp:2036
TCPListenHandler
Template for TCP listeners.
Definition: tcp_listen.h:28
ServerNetworkGameSocketHandler::~ServerNetworkGameSocketHandler
~ServerNetworkGameSocketHandler()
Clear everything related to this client.
Definition: network_server.cpp:225
ServerNetworkGameSocketHandler::ClientStatus
ClientStatus
Status of a client.
Definition: network_server.h:51
ServerNetworkGameSocketHandler::STATUS_INACTIVE
@ STATUS_INACTIVE
The client is not connected nor active.
Definition: network_server.h:52
ServerNetworkGameSocketHandler::Send
static void Send()
Send the packets for the server sockets.
Definition: network_server.cpp:320
PacketWriter
Writing a savegame directly to a number of packets.
Definition: network_server.cpp:59
CommandPacket
Everything we need to know about a command to be able to execute it.
Definition: network_internal.h:109
ServerNetworkGameSocketHandler::SendSync
NetworkRecvStatus SendSync()
Request the client to sync.
Definition: network_server.cpp:622
ServerNetworkGameSocketHandler::SendConfigUpdate
NetworkRecvStatus SendConfigUpdate()
Send an update about the max company/spectator counts.
Definition: network_server.cpp:784
ServerNetworkGameSocketHandler::Receive_CLIENT_GAME_INFO
NetworkRecvStatus Receive_CLIENT_GAME_INFO(Packet *p) override
Request game information.
Definition: network_server.cpp:799
ClientID
ClientID
'Unique' identifier to be given to clients
Definition: network_type.h:47
ServerNetworkGameSocketHandler::SendMove
NetworkRecvStatus SendMove(ClientID client_id, CompanyID company_id)
Tell that a client moved to another company.
Definition: network_server.cpp:763
ServerNetworkGameSocketHandler::Receive_CLIENT_JOIN
NetworkRecvStatus Receive_CLIENT_JOIN(Packet *p) override
Try to join the server: string OpenTTD revision (norev000 if no revision).
Definition: network_server.cpp:825
ServerNetworkGameSocketHandler::SendJoin
NetworkRecvStatus SendJoin(ClientID client_id)
Tell that a client joined.
Definition: network_server.cpp:588
ServerNetworkGameSocketHandler::SendFrame
NetworkRecvStatus SendFrame()
Tell the client that they may run to a particular frame.
Definition: network_server.cpp:599
ServerNetworkGameSocketHandler::SendRConResult
NetworkRecvStatus SendRConResult(uint16 colour, const std::string &command)
Send the result of a console action.
Definition: network_server.cpp:748
NetworkServerUpdateCompanyPassworded
void NetworkServerUpdateCompanyPassworded(CompanyID company_id, bool passworded)
Tell that a particular company is (not) passworded.
Definition: network_server.cpp:1895
CommandQueue
A queue of CommandPackets.
Definition: tcp_game.h:135
ServerNetworkGameSocketHandler::SendExternalChat
NetworkRecvStatus SendExternalChat(const std::string &source, TextColour colour, const std::string &user, const std::string &msg)
Send a chat message from external source.
Definition: network_server.cpp:682
ServerNetworkGameSocketHandler::SendCompanyUpdate
NetworkRecvStatus SendCompanyUpdate()
Send an update about the company password states.
Definition: network_server.cpp:774
ServerNetworkGameSocketHandler::SendWelcome
NetworkRecvStatus SendWelcome()
Send the client a welcome message with some basic information.
Definition: network_server.cpp:467
ServerNetworkGameSocketHandler::Receive_CLIENT_MOVE
NetworkRecvStatus Receive_CLIENT_MOVE(Packet *p) override
Request the server to move this client into another company: uint8 ID of the company the client wants...
Definition: network_server.cpp:1407
ServerNetworkGameSocketHandler::GetByClientID
static ServerNetworkGameSocketHandler * GetByClientID(ClientID client_id)
Return the client state given it's client-identifier.
Definition: network.cpp:128
ServerNetworkGameSocketHandler::SendError
NetworkRecvStatus SendError(NetworkErrorCode error, const std::string &reason={})
Send an error to the client, and close its connection.
Definition: network_server.cpp:372
ServerNetworkGameSocketHandler::status
ClientStatus status
Status of this client.
Definition: network_server.h:68
ServerNetworkGameSocketHandler::savegame
struct PacketWriter * savegame
Writer used to write the savegame.
Definition: network_server.h:72
Packet
Internal entity of a packet.
Definition: packet.h:44
ServerNetworkGameSocketHandler::SendClientInfo
NetworkRecvStatus SendClientInfo(NetworkClientInfo *ci)
Send the client information about a client.
Definition: network_server.cpp:343
ServerNetworkGameSocketHandler::AcceptConnection
static void AcceptConnection(SOCKET s, const NetworkAddress &address)
Handle the accepting of a connection to the server.
Definition: network.cpp:554
ServerNetworkGameSocketHandler::STATUS_DONE_MAP
@ STATUS_DONE_MAP
The client has downloaded the map.
Definition: network_server.h:59
ServerNetworkGameSocketHandler::SendQuit
NetworkRecvStatus SendQuit(ClientID client_id)
Tell the client another client quit.
Definition: network_server.cpp:717
ServerNetworkGameSocketHandler::lag_test
byte lag_test
Byte used for lag-testing the client.
Definition: network_server.h:65
ServerNetworkGameSocketHandler::SendErrorQuit
NetworkRecvStatus SendErrorQuit(ClientID client_id, NetworkErrorCode errorno)
Tell the client another client quit with an error.
Definition: network_server.cpp:702
ServerNetworkGameSocketHandler
Class for handling the server side of the game connection.
Definition: network_server.h:24
NetworkServer_Tick
void NetworkServer_Tick(bool send_frame)
This is called every tick if this is a _network_server.
Definition: network_server.cpp:1681
ServerNetworkGameSocketHandler::Receive_CLIENT_COMMAND
NetworkRecvStatus Receive_CLIENT_COMMAND(Packet *p) override
The client has done a command and wants us to handle it.
Definition: network_server.cpp:1013
NetworkAddress
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition: address.h:30
ServerNetworkGameSocketHandler::GetClientIP
const std::string & GetClientIP()
Get the IP address/hostname of the connected client.
Definition: network_server.cpp:1838
Pool
Base class for all pools.
Definition: pool_type.hpp:81
ServerNetworkGameSocketHandler::Receive_CLIENT_SET_NAME
NetworkRecvStatus Receive_CLIENT_SET_NAME(Packet *p) override
Gives the client a new name: string New name of the client.
Definition: network_server.cpp:1353
ServerNetworkGameSocketHandler::SendNewGame
NetworkRecvStatus SendNewGame()
Tell the client we're starting a new game.
Definition: network_server.cpp:736
NetworkRecvStatus
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition: core.h:22
ServerNetworkGameSocketHandler::STATUS_NEWGRFS_CHECK
@ STATUS_NEWGRFS_CHECK
The client is checking NewGRFs.
Definition: network_server.h:53
ServerNetworkGameSocketHandler::AllowConnection
static bool AllowConnection()
Whether an connection is allowed or not at this moment.
Definition: network_server.cpp:307
ServerNetworkGameSocketHandler::Receive_CLIENT_GETMAP
NetworkRecvStatus Receive_CLIENT_GETMAP(Packet *p) override
Request the map from the server.
Definition: network_server.cpp:945
network_internal.h
ServerNetworkGameSocketHandler::SendMap
NetworkRecvStatus SendMap()
This sends the map to the client.
Definition: network_server.cpp:542
ServerNetworkGameSocketHandler::SendNewGRFCheck
NetworkRecvStatus SendNewGRFCheck()
Send the check for the NewGRFs.
Definition: network_server.cpp:415
ServerNetworkGameSocketHandler::STATUS_AUTH_COMPANY
@ STATUS_AUTH_COMPANY
The client is authorizing with company password.
Definition: network_server.h:55
ServerNetworkGameSocketHandler::SendNeedGamePassword
NetworkRecvStatus SendNeedGamePassword()
Request the game password.
Definition: network_server.cpp:435
error
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:134
NetworkGameSocketHandler::client_id
ClientID client_id
Client identifier.
Definition: tcp_game.h:512
ServerNetworkGameSocketHandler::Receive_CLIENT_ACK
NetworkRecvStatus Receive_CLIENT_ACK(Packet *p) override
Tell the server we are done with this frame: uint32 Current frame counter of the client.
Definition: network_server.cpp:1129
NetworkClientSocketPool
Pool< NetworkClientSocket, ClientIndex, 8, MAX_CLIENT_SLOTS, PT_NCLIENT > NetworkClientSocketPool
Pool with all client sockets.
Definition: network_server.h:20
ServerNetworkGameSocketHandler::STATUS_END
@ STATUS_END
Must ALWAYS be on the end of this list!! (period).
Definition: network_server.h:62
ServerNetworkGameSocketHandler::ServerNetworkGameSocketHandler
ServerNetworkGameSocketHandler(SOCKET s)
Create a new socket for the server side of the game connection.
Definition: network_server.cpp:210
ServerNetworkGameSocketHandler::Receive_CLIENT_RCON
NetworkRecvStatus Receive_CLIENT_RCON(Packet *p) override
Send an RCon command to the server: string RCon password.
Definition: network_server.cpp:1385
ServerNetworkGameSocketHandler::client_address
NetworkAddress client_address
IP-address of the client (so they can be banned)
Definition: network_server.h:73
NetworkClientSocket
ServerNetworkGameSocketHandler NetworkClientSocket
Make the code look slightly nicer/simpler.
Definition: network_server.h:16
ServerNetworkGameSocketHandler::Receive_CLIENT_CHAT
NetworkRecvStatus Receive_CLIENT_CHAT(Packet *p) override
Sends a chat-packet to the server: uint8 ID of the action (see NetworkAction).
Definition: network_server.cpp:1311
ServerNetworkGameSocketHandler::Receive_CLIENT_GAME_PASSWORD
NetworkRecvStatus Receive_CLIENT_GAME_PASSWORD(Packet *p) override
Send a password to the server to authorize: uint8 Password type (see NetworkPasswordType).
Definition: network_server.cpp:900
ServerNetworkGameSocketHandler::SendShutdown
NetworkRecvStatus SendShutdown()
Tell the client we're shutting down.
Definition: network_server.cpp:728
ServerNetworkGameSocketHandler::last_token
byte last_token
The last random token we did send to verify the client is listening.
Definition: network_server.h:66
ServerNetworkGameSocketHandler::outgoing_queue
CommandQueue outgoing_queue
The command-queue awaiting delivery.
Definition: network_server.h:69
ServerNetworkGameSocketHandler::SendNeedCompanyPassword
NetworkRecvStatus SendNeedCompanyPassword()
Request the company password.
Definition: network_server.cpp:450
Pool::PoolItem
Base class for all PoolItems.
Definition: pool_type.hpp:234
_networkclientsocket_pool
NetworkClientSocketPool _networkclientsocket_pool
Make very sure the preconditions given in network_type.h are actually followed.
NetworkErrorCode
NetworkErrorCode
The error codes we send around in the protocols.
Definition: network_type.h:119
NetworkClientInfo
Container for all information known about a client.
Definition: network_base.h:24
NetworkServerSetCompanyPassword
void NetworkServerSetCompanyPassword(CompanyID company_id, const std::string &password, bool already_hashed=true)
Set/Reset a company password on the server end.
Definition: network_server.cpp:1651
ServerNetworkGameSocketHandler::CloseConnection
NetworkRecvStatus CloseConnection(NetworkRecvStatus status) override
Close the network connection due to the given status.
Definition: network_server.cpp:249
ServerNetworkGameSocketHandler::STATUS_AUTH_GAME
@ STATUS_AUTH_GAME
The client is authorizing with game (server) password.
Definition: network_server.h:54
ServerNetworkGameSocketHandler::SendWait
NetworkRecvStatus SendWait()
Tell the client that its put in a waiting queue.
Definition: network_server.cpp:497
ServerNetworkGameSocketHandler::STATUS_AUTHORIZED
@ STATUS_AUTHORIZED
The client is authorized.
Definition: network_server.h:56
ServerNetworkGameSocketHandler::Receive_CLIENT_ERROR
NetworkRecvStatus Receive_CLIENT_ERROR(Packet *p) override
The client made an error and is quitting the game.
Definition: network_server.cpp:1078