OpenTTD

network_server.h
Go to the documentation of this file.
1 /* $Id: network_server.h 26576 2014-05-11 12:52:21Z rubidium $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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 */
9
12 #ifndef NETWORK_SERVER_H
13 #define NETWORK_SERVER_H
14
15 #ifdef ENABLE_NETWORK
16
17 #include "network_internal.h"
18 #include "core/tcp_listen.h"
19 #include "../thread/thread.h"
20
21 class ServerNetworkGameSocketHandler;
23 typedef ServerNetworkGameSocketHandler NetworkClientSocket;
25 typedef Pool<NetworkClientSocket, ClientIndex, 8, MAX_CLIENT_SLOTS, PT_NCLIENT> NetworkClientSocketPool;
26 extern NetworkClientSocketPool _networkclientsocket_pool;
27
29 class ServerNetworkGameSocketHandler : public NetworkClientSocketPool::PoolItem<&_networkclientsocket_pool>, public NetworkGameSocketHandler, public TCPListenHandler<ServerNetworkGameSocketHandler, PACKET_SERVER_FULL, PACKET_SERVER_BANNED> {
30 protected:
31 virtual NetworkRecvStatus Receive_CLIENT_JOIN(Packet *p);
32 virtual NetworkRecvStatus Receive_CLIENT_COMPANY_INFO(Packet *p);
33 virtual NetworkRecvStatus Receive_CLIENT_GAME_PASSWORD(Packet *p);
34 virtual NetworkRecvStatus Receive_CLIENT_COMPANY_PASSWORD(Packet *p);
35 virtual NetworkRecvStatus Receive_CLIENT_GETMAP(Packet *p);
36 virtual NetworkRecvStatus Receive_CLIENT_MAP_OK(Packet *p);
37 virtual NetworkRecvStatus Receive_CLIENT_ACK(Packet *p);
38 virtual NetworkRecvStatus Receive_CLIENT_COMMAND(Packet *p);
39 virtual NetworkRecvStatus Receive_CLIENT_CHAT(Packet *p);
40 virtual NetworkRecvStatus Receive_CLIENT_SET_PASSWORD(Packet *p);
41 virtual NetworkRecvStatus Receive_CLIENT_SET_NAME(Packet *p);
42 virtual NetworkRecvStatus Receive_CLIENT_QUIT(Packet *p);
43 virtual NetworkRecvStatus Receive_CLIENT_ERROR(Packet *p);
44 virtual NetworkRecvStatus Receive_CLIENT_RCON(Packet *p);
45 virtual NetworkRecvStatus Receive_CLIENT_NEWGRFS_CHECKED(Packet *p);
46 virtual NetworkRecvStatus Receive_CLIENT_MOVE(Packet *p);
47
48 NetworkRecvStatus SendCompanyInfo();
49 NetworkRecvStatus SendNewGRFCheck();
50 NetworkRecvStatus SendWelcome();
51 NetworkRecvStatus SendWait();
52 NetworkRecvStatus SendNeedGamePassword();
53 NetworkRecvStatus SendNeedCompanyPassword();
54
55 public:
57 enum ClientStatus {
58 STATUS_INACTIVE,
59 STATUS_NEWGRFS_CHECK,
60 STATUS_AUTH_GAME,
61 STATUS_AUTH_COMPANY,
62 STATUS_AUTHORIZED,
63 STATUS_MAP_WAIT,
64 STATUS_MAP,
65 STATUS_DONE_MAP,
66 STATUS_PRE_ACTIVE,
67 STATUS_ACTIVE,
68 STATUS_END,
69 };
70
71 byte lag_test;
72 byte last_token;
73 uint32 last_token_frame;
74 ClientStatus status;
75 CommandQueue outgoing_queue;
76 int receive_limit;
77
78 struct PacketWriter *savegame;
79 NetworkAddress client_address;
80
81 ServerNetworkGameSocketHandler(SOCKET s);
82 ~ServerNetworkGameSocketHandler();
83
84 virtual Packet *ReceivePacket();
85 NetworkRecvStatus CloseConnection(NetworkRecvStatus status);
86 void GetClientName(char *client_name, const char *last) const;
87
88 NetworkRecvStatus SendMap();
89 NetworkRecvStatus SendErrorQuit(ClientID client_id, NetworkErrorCode errorno);
90 NetworkRecvStatus SendQuit(ClientID client_id);
91 NetworkRecvStatus SendShutdown();
92 NetworkRecvStatus SendNewGame();
93 NetworkRecvStatus SendRConResult(uint16 colour, const char *command);
94 NetworkRecvStatus SendMove(ClientID client_id, CompanyID company_id);
95
96 NetworkRecvStatus SendClientInfo(NetworkClientInfo *ci);
97 NetworkRecvStatus SendError(NetworkErrorCode error);
98 NetworkRecvStatus SendChat(NetworkAction action, ClientID client_id, bool self_send, const char *msg, int64 data);
99 NetworkRecvStatus SendJoin(ClientID client_id);
100 NetworkRecvStatus SendFrame();
101 NetworkRecvStatus SendSync();
102 NetworkRecvStatus SendCommand(const CommandPacket *cp);
103 NetworkRecvStatus SendCompanyUpdate();
104 NetworkRecvStatus SendConfigUpdate();
105
106 static void Send();
107 static void AcceptConnection(SOCKET s, const NetworkAddress &address);
108 static bool AllowConnection();
109
114 static const char *GetName()
115 {
116 return "server";
117 }
118
119 const char *GetClientIP();
120
121 static ServerNetworkGameSocketHandler *GetByClientID(ClientID client_id);
122 };
123
124 void NetworkServer_Tick(bool send_frame);
125 void NetworkServerSetCompanyPassword(CompanyID company_id, const char *password, bool already_hashed = true);
126 void NetworkServerUpdateCompanyPassworded(CompanyID company_id, bool passworded);
127
133 #define FOR_ALL_CLIENT_SOCKETS_FROM(var, start) FOR_ALL_ITEMS_FROM(NetworkClientSocket, clientsocket_index, var, start)
134
139 #define FOR_ALL_CLIENT_SOCKETS(var) FOR_ALL_CLIENT_SOCKETS_FROM(var, 0)
140
141 #else /* ENABLE_NETWORK */
142 /* Network function stubs when networking is disabled */
143
144 static inline void NetworkServerMonthlyLoop() {}
145 static inline void NetworkServerYearlyLoop() {}
146
147 #endif /* ENABLE_NETWORK */
148
149 #endif /* NETWORK_SERVER_H */
ServerNetworkGameSocketHandler::SendMap
NetworkRecvStatus SendMap()
This sends the map to the client.
Definition: network_server.cpp:576
ServerNetworkGameSocketHandler::Receive_CLIENT_ACK
virtual NetworkRecvStatus Receive_CLIENT_ACK(Packet *p)
Tell the server we are done with this frame: uint32 Current frame counter of the client.
Definition: network_server.cpp:1212
ServerNetworkGameSocketHandler::STATUS_PRE_ACTIVE
The client is catching up the delayed frames.
Definition: network_server.h:66
ServerNetworkGameSocketHandler::AllowConnection
static bool AllowConnection()
Whether an connection is allowed or not at this moment.
Definition: network_server.cpp:314
ServerNetworkGameSocketHandler::SendNeedCompanyPassword
NetworkRecvStatus SendNeedCompanyPassword()
Request the company password.
Definition: network_server.cpp:509
ServerNetworkGameSocketHandler::SendCompanyUpdate
NetworkRecvStatus SendCompanyUpdate()
Send an update about the company password states.
Definition: network_server.cpp:844
NetworkClientInfo
Container for all information known about a client.
Definition: network_base.h:27
ServerNetworkGameSocketHandler::STATUS_DONE_MAP
The client has downloaded the map.
Definition: network_server.h:65
ServerNetworkGameSocketHandler::SendJoin
NetworkRecvStatus SendJoin(ClientID client_id)
Tell that a client joined.
Definition: network_server.cpp:680
Packet
Internal entity of a packet.
Definition: packet.h:44
ServerNetworkGameSocketHandler::Receive_CLIENT_RCON
virtual NetworkRecvStatus Receive_CLIENT_RCON(Packet *p)
Send an RCon command to the server: string RCon password.
Definition: network_server.cpp:1456
ServerNetworkGameSocketHandler::Receive_CLIENT_GETMAP
virtual NetworkRecvStatus Receive_CLIENT_GETMAP(Packet *p)
Request the map from the server.
Definition: network_server.cpp:1018
ServerNetworkGameSocketHandler::SendError
NetworkRecvStatus SendError(NetworkErrorCode error)
Send an error to the client, and close its connection.
Definition: network_server.cpp:431
ServerNetworkGameSocketHandler::last_token
byte last_token
The last random token we did send to verify the client is listening.
Definition: network_server.h:72
ServerNetworkGameSocketHandler::ServerNetworkGameSocketHandler
ServerNetworkGameSocketHandler(SOCKET s)
Create a new socket for the server side of the game connection.
Definition: network_server.cpp:223
ServerNetworkGameSocketHandler::receive_limit
int receive_limit
Amount of bytes that we can receive at this moment.
Definition: network_server.h:76
ServerNetworkGameSocketHandler::CloseConnection
NetworkRecvStatus CloseConnection(NetworkRecvStatus status)
Close the network connection due to the given status.
Definition: network_server.cpp:262
NetworkErrorCode
NetworkErrorCode
The error codes we send around in the protocols.
Definition: network_type.h:104
ServerNetworkGameSocketHandler::SendCommand
NetworkRecvStatus SendCommand(const CommandPacket *cp)
Send a command to the client to execute.
Definition: network_server.cpp:731
ServerNetworkGameSocketHandler::STATUS_MAP
The client is downloading the map.
Definition: network_server.h:64
NetworkClientSocket
ServerNetworkGameSocketHandler NetworkClientSocket
Make the code look slightly nicer/simpler.
Definition: network_server.h:21
ServerNetworkGameSocketHandler::status
ClientStatus status
Status of this client.
Definition: network_server.h:74
ServerNetworkGameSocketHandler::SendClientInfo
NetworkRecvStatus SendClientInfo(NetworkClientInfo *ci)
Send the client information about a client.
Definition: network_server.cpp:351
ServerNetworkGameSocketHandler::SendWait
NetworkRecvStatus SendWait()
Tell the client that its put in a waiting queue.
Definition: network_server.cpp:557
TCPListenHandler
Template for TCP listeners.
Definition: tcp_listen.h:32
ServerNetworkGameSocketHandler::STATUS_AUTH_COMPANY
The client is authorizing with company password.
Definition: network_server.h:61
ServerNetworkGameSocketHandler::Receive_CLIENT_QUIT
virtual NetworkRecvStatus Receive_CLIENT_QUIT(Packet *p)
The client is quitting the game.
Definition: network_server.cpp:1185
ServerNetworkGameSocketHandler::STATUS_ACTIVE
The client is active within in the game.
Definition: network_server.h:67
ServerNetworkGameSocketHandler::SendChat
NetworkRecvStatus SendChat(NetworkAction action, ClientID client_id, bool self_send, const char *msg, int64 data)
Send a chat message.
Definition: network_server.cpp:751
NetworkAddress
Wrapper for (un)resolved network addresses; there&#39;s no reason to transform a numeric IP to a string a...
Definition: address.h:31
ServerNetworkGameSocketHandler::Receive_CLIENT_CHAT
virtual NetworkRecvStatus Receive_CLIENT_CHAT(Packet *p)
Sends a chat-packet to the server: uint8 ID of the action (see NetworkAction).
Definition: network_server.cpp:1381
ServerNetworkGameSocketHandler::Receive_CLIENT_COMPANY_PASSWORD
virtual NetworkRecvStatus Receive_CLIENT_COMPANY_PASSWORD(Packet *p)
Send a password to the server to authorize uint8 Password type (see NetworkPasswordType).
Definition: network_server.cpp:996
ServerNetworkGameSocketHandler::SendRConResult
NetworkRecvStatus SendRConResult(uint16 colour, const char *command)
Send the result of a console action.
Definition: network_server.cpp:818
ServerNetworkGameSocketHandler::SendNewGRFCheck
NetworkRecvStatus SendNewGRFCheck()
Send the check for the NewGRFs.
Definition: network_server.cpp:474
ServerNetworkGameSocketHandler::Receive_CLIENT_NEWGRFS_CHECKED
virtual NetworkRecvStatus Receive_CLIENT_NEWGRFS_CHECKED(Packet *p)
Tell the server that we have the required GRFs.
Definition: network_server.cpp:874
ServerNetworkGameSocketHandler
Class for handling the server side of the game connection.
Definition: network_server.h:29
ServerNetworkGameSocketHandler::Receive_CLIENT_GAME_PASSWORD
virtual NetworkRecvStatus Receive_CLIENT_GAME_PASSWORD(Packet *p)
Send a password to the server to authorize: uint8 Password type (see NetworkPasswordType).
Definition: network_server.cpp:971
ServerNetworkGameSocketHandler::STATUS_INACTIVE
The client is not connected nor active.
Definition: network_server.h:58
ServerNetworkGameSocketHandler::Receive_CLIENT_JOIN
virtual NetworkRecvStatus Receive_CLIENT_JOIN(Packet *p)
Try to join the server: string OpenTTD revision (norev000 if no revision).
Definition: network_server.cpp:895
CommandQueue
A queue of CommandPackets.
Definition: tcp_game.h:133
ServerNetworkGameSocketHandler::GetClientName
void GetClientName(char *client_name, const char *last) const
Get the name of the client, if the user did not send it yet, Client #<no> is used.
Definition: network_server.cpp:2144
ServerNetworkGameSocketHandler::Receive_CLIENT_SET_NAME
virtual NetworkRecvStatus Receive_CLIENT_SET_NAME(Packet *p)
Gives the client a new name: string New name of the client.
Definition: network_server.cpp:1430
ServerNetworkGameSocketHandler::client_address
NetworkAddress client_address
IP-address of the client (so he can be banned)
Definition: network_server.h:79
ServerNetworkGameSocketHandler::GetByClientID
static ServerNetworkGameSocketHandler * GetByClientID(ClientID client_id)
Return the client state given it&#39;s client-identifier.
Definition: network.cpp:142
NetworkServerYearlyLoop
void NetworkServerYearlyLoop()
Yearly "callback".
Definition: network_server.cpp:1931
ServerNetworkGameSocketHandler::STATUS_MAP_WAIT
The client is waiting as someone else is downloading the map.
Definition: network_server.h:63
tcp_listen.h
Basic functions to listen for TCP connections.
ServerNetworkGameSocketHandler::AcceptConnection
static void AcceptConnection(SOCKET s, const NetworkAddress &address)
Handle the accepting of a connection to the server.
Definition: network.cpp:507
ServerNetworkGameSocketHandler::SendNeedGamePassword
NetworkRecvStatus SendNeedGamePassword()
Request the game password.
Definition: network_server.cpp:494
ServerNetworkGameSocketHandler::STATUS_AUTH_GAME
The client is authorizing with game (server) password.
Definition: network_server.h:60
ServerNetworkGameSocketHandler::STATUS_END
Must ALWAYS be on the end of this list!! (period).
Definition: network_server.h:68
NetworkRecvStatus
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition: core.h:26
PacketWriter
Writing a savegame directly to a number of packets.
Definition: network_server.cpp:58
NetworkServerUpdateCompanyPassworded
void NetworkServerUpdateCompanyPassworded(CompanyID company_id, bool passworded)
Tell that a particular company is (not) passworded.
Definition: network_server.cpp:2010
ServerNetworkGameSocketHandler::STATUS_NEWGRFS_CHECK
The client is checking NewGRFs.
Definition: network_server.h:59
ServerNetworkGameSocketHandler::lag_test
byte lag_test
Byte used for lag-testing the client.
Definition: network_server.h:71
ServerNetworkGameSocketHandler::SendFrame
NetworkRecvStatus SendFrame()
Tell the client that they may run to a particular frame.
Definition: network_server.cpp:691
Pool::PoolItem
Base class for all PoolItems.
Definition: pool_type.hpp:146
ServerNetworkGameSocketHandler::Receive_CLIENT_COMMAND
virtual NetworkRecvStatus Receive_CLIENT_COMMAND(Packet *p)
The client has done a command and wants us to handle it.
Definition: network_server.cpp:1087
ServerNetworkGameSocketHandler::ClientStatus
ClientStatus
Status of a client.
Definition: network_server.h:57
ServerNetworkGameSocketHandler::SendNewGame
NetworkRecvStatus SendNewGame()
Tell the client we&#39;re starting a new game.
Definition: network_server.cpp:806
Pool
Base class for all pools.
Definition: pool_type.hpp:83
ServerNetworkGameSocketHandler::SendQuit
NetworkRecvStatus SendQuit(ClientID client_id)
Tell the client another client quit.
Definition: network_server.cpp:787
NetworkGameSocketHandler::client_id
ClientID client_id
Client identifier.
Definition: tcp_game.h:521
ServerNetworkGameSocketHandler::Receive_CLIENT_ERROR
virtual NetworkRecvStatus Receive_CLIENT_ERROR(Packet *p)
The client made an error and is quitting the game.
Definition: network_server.cpp:1151
ServerNetworkGameSocketHandler::SendSync
NetworkRecvStatus SendSync()
Request the client to sync.
Definition: network_server.cpp:714
ServerNetworkGameSocketHandler::SendWelcome
NetworkRecvStatus SendWelcome()
Send the client a welcome message with some basic information.
Definition: network_server.cpp:526
ServerNetworkGameSocketHandler::GetName
static const char * GetName()
Get the name used by the listener.
Definition: network_server.h:114
ServerNetworkGameSocketHandler::SendCompanyInfo
NetworkRecvStatus SendCompanyInfo()
Send the client information about the companies.
Definition: network_server.cpp:365
NetworkServerSetCompanyPassword
void NetworkServerSetCompanyPassword(CompanyID company_id, const char *password, bool already_hashed=true)
Set/Reset a company password on the server end.
Definition: network_server.cpp:1776
network_internal.h
Variables and function used internally.
ServerNetworkGameSocketHandler::SendConfigUpdate
NetworkRecvStatus SendConfigUpdate()
Send an update about the max company/spectator counts.
Definition: network_server.cpp:854
ServerNetworkGameSocketHandler::GetClientIP
const char * GetClientIP()
Get the IP address/hostname of the connected client.
Definition: network_server.cpp:1956
ServerNetworkGameSocketHandler::STATUS_AUTHORIZED
The client is authorized.
Definition: network_server.h:62
ClientID
ClientID
&#39;Unique&#39; identifier to be given to clients
Definition: network_type.h:43
error
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:110
ServerNetworkGameSocketHandler::Receive_CLIENT_COMPANY_INFO
virtual NetworkRecvStatus Receive_CLIENT_COMPANY_INFO(Packet *p)
Request company information (in detail).
Definition: network_server.cpp:869
ServerNetworkGameSocketHandler::SendShutdown
NetworkRecvStatus SendShutdown()
Tell the client we&#39;re shutting down.
Definition: network_server.cpp:798
_networkclientsocket_pool
NetworkClientSocketPool _networkclientsocket_pool
The pool with clients.
CommandPacket
Everything we need to know about a command to be able to execute it.
Definition: network_internal.h:153
ServerNetworkGameSocketHandler::SendMove
NetworkRecvStatus SendMove(ClientID client_id, CompanyID company_id)
Tell that a client moved to another company.
Definition: network_server.cpp:833
ServerNetworkGameSocketHandler::SendErrorQuit
NetworkRecvStatus SendErrorQuit(ClientID client_id, NetworkErrorCode errorno)
Tell the client another client quit with an error.
Definition: network_server.cpp:772
ServerNetworkGameSocketHandler::last_token_frame
uint32 last_token_frame
The last frame we received the right token.
Definition: network_server.h:73
NetworkServer_Tick
void NetworkServer_Tick(bool send_frame)
This is called every tick if this is a _network_server.
Definition: network_server.cpp:1805
NetworkServerMonthlyLoop
void NetworkServerMonthlyLoop()
Monthly "callback".
Definition: network_server.cpp:1938
NetworkGameSocketHandler
Base socket handler for all TCP sockets.
Definition: tcp_game.h:152
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:20
ServerNetworkGameSocketHandler::~ServerNetworkGameSocketHandler
~ServerNetworkGameSocketHandler()
Clear everything related to this client.
Definition: network_server.cpp:238
NetworkClientSocketPool
Pool< NetworkClientSocket, ClientIndex, 8, MAX_CLIENT_SLOTS, PT_NCLIENT > NetworkClientSocketPool
Pool with all client sockets.
Definition: network_server.h:25
ServerNetworkGameSocketHandler::Send
static void Send()
Send the packets for the server sockets.
Definition: network_server.cpp:327
ServerNetworkGameSocketHandler::Receive_CLIENT_MOVE
virtual NetworkRecvStatus Receive_CLIENT_MOVE(Packet *p)
Request the server to move this client into another company: uint8 ID of the company the client wants...
Definition: network_server.cpp:1481
NetworkAction
NetworkAction
Actions that can be used for NetworkTextMessage.
Definition: network_type.h:89
ServerNetworkGameSocketHandler::ReceivePacket
virtual Packet * ReceivePacket()
Receives a packet for the given client.
Definition: network_server.cpp:249
ServerNetworkGameSocketHandler::savegame
struct PacketWriter * savegame
Writer used to write the savegame.
Definition: network_server.h:78
ServerNetworkGameSocketHandler::Receive_CLIENT_MAP_OK
virtual NetworkRecvStatus Receive_CLIENT_MAP_OK(Packet *p)
Tell the server that we are done receiving/loading the map.
Definition: network_server.cpp:1040
ServerNetworkGameSocketHandler::Receive_CLIENT_SET_PASSWORD
virtual NetworkRecvStatus Receive_CLIENT_SET_PASSWORD(Packet *p)
Set the password for the clients current company: string The password.
Definition: network_server.cpp:1413
ServerNetworkGameSocketHandler::outgoing_queue
CommandQueue outgoing_queue
The command-queue awaiting delivery.
Definition: network_server.h:75