OpenTTD

network_client.h
Go to the documentation of this file.
1 /* $Id: network_client.h 24900 2013-01-08 22:46:42Z planetmaker $ */
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_CLIENT_H
13 #define NETWORK_CLIENT_H
14
15 #ifdef ENABLE_NETWORK
16
17 #include "network_internal.h"
18
20 class ClientNetworkGameSocketHandler : public ZeroedMemoryAllocator, public NetworkGameSocketHandler {
21 private:
22 struct PacketReader *savegame;
23 byte token;
24
26 enum ServerStatus {
27 STATUS_INACTIVE,
28 STATUS_COMPANY_INFO,
29 STATUS_JOIN,
30 STATUS_NEWGRFS_CHECK,
31 STATUS_AUTH_GAME,
32 STATUS_AUTH_COMPANY,
33 STATUS_AUTHORIZED,
34 STATUS_MAP_WAIT,
35 STATUS_MAP,
36 STATUS_ACTIVE,
37 STATUS_END,
38 };
39
40 ServerStatus status;
41
42 protected:
43 friend void NetworkExecuteLocalCommandQueue();
44 friend void NetworkClose(bool close_admins);
45 static ClientNetworkGameSocketHandler *my_client;
46
47 virtual NetworkRecvStatus Receive_SERVER_FULL(Packet *p);
48 virtual NetworkRecvStatus Receive_SERVER_BANNED(Packet *p);
49 virtual NetworkRecvStatus Receive_SERVER_ERROR(Packet *p);
50 virtual NetworkRecvStatus Receive_SERVER_COMPANY_INFO(Packet *p);
51 virtual NetworkRecvStatus Receive_SERVER_CLIENT_INFO(Packet *p);
52 virtual NetworkRecvStatus Receive_SERVER_NEED_GAME_PASSWORD(Packet *p);
53 virtual NetworkRecvStatus Receive_SERVER_NEED_COMPANY_PASSWORD(Packet *p);
54 virtual NetworkRecvStatus Receive_SERVER_WELCOME(Packet *p);
55 virtual NetworkRecvStatus Receive_SERVER_WAIT(Packet *p);
56 virtual NetworkRecvStatus Receive_SERVER_MAP_BEGIN(Packet *p);
57 virtual NetworkRecvStatus Receive_SERVER_MAP_SIZE(Packet *p);
58 virtual NetworkRecvStatus Receive_SERVER_MAP_DATA(Packet *p);
59 virtual NetworkRecvStatus Receive_SERVER_MAP_DONE(Packet *p);
60 virtual NetworkRecvStatus Receive_SERVER_JOIN(Packet *p);
61 virtual NetworkRecvStatus Receive_SERVER_FRAME(Packet *p);
62 virtual NetworkRecvStatus Receive_SERVER_SYNC(Packet *p);
63 virtual NetworkRecvStatus Receive_SERVER_COMMAND(Packet *p);
64 virtual NetworkRecvStatus Receive_SERVER_CHAT(Packet *p);
65 virtual NetworkRecvStatus Receive_SERVER_QUIT(Packet *p);
66 virtual NetworkRecvStatus Receive_SERVER_ERROR_QUIT(Packet *p);
67 virtual NetworkRecvStatus Receive_SERVER_SHUTDOWN(Packet *p);
68 virtual NetworkRecvStatus Receive_SERVER_NEWGAME(Packet *p);
69 virtual NetworkRecvStatus Receive_SERVER_RCON(Packet *p);
70 virtual NetworkRecvStatus Receive_SERVER_CHECK_NEWGRFS(Packet *p);
71 virtual NetworkRecvStatus Receive_SERVER_MOVE(Packet *p);
72 virtual NetworkRecvStatus Receive_SERVER_COMPANY_UPDATE(Packet *p);
73 virtual NetworkRecvStatus Receive_SERVER_CONFIG_UPDATE(Packet *p);
74
75 static NetworkRecvStatus SendNewGRFsOk();
76 static NetworkRecvStatus SendGetMap();
77 static NetworkRecvStatus SendMapOk();
78 void CheckConnection();
79 public:
80 ClientNetworkGameSocketHandler(SOCKET s);
81 ~ClientNetworkGameSocketHandler();
82
83 NetworkRecvStatus CloseConnection(NetworkRecvStatus status);
84 void ClientError(NetworkRecvStatus res);
85
86 static NetworkRecvStatus SendCompanyInformationQuery();
87
88 static NetworkRecvStatus SendJoin();
89 static NetworkRecvStatus SendCommand(const CommandPacket *cp);
90 static NetworkRecvStatus SendError(NetworkErrorCode errorno);
91 static NetworkRecvStatus SendQuit();
92 static NetworkRecvStatus SendAck();
93
94 static NetworkRecvStatus SendGamePassword(const char *password);
95 static NetworkRecvStatus SendCompanyPassword(const char *password);
96
97 static NetworkRecvStatus SendChat(NetworkAction action, DestType type, int dest, const char *msg, int64 data);
98 static NetworkRecvStatus SendSetPassword(const char *password);
99 static NetworkRecvStatus SendSetName(const char *name);
100 static NetworkRecvStatus SendRCon(const char *password, const char *command);
101 static NetworkRecvStatus SendMove(CompanyID company, const char *password);
102
103 static bool IsConnected();
104
105 static void Send();
106 static bool Receive();
107 static bool GameLoop();
108 };
109
111 typedef ClientNetworkGameSocketHandler MyClient;
112
113 void NetworkClient_Connected();
114 void NetworkClientSetCompanyPassword(const char *password);
115
116 extern CompanyID _network_join_as;
117
118 extern const char *_network_join_server_password;
119 extern const char *_network_join_company_password;
120
121 #endif /* ENABLE_NETWORK */
122
123 #endif /* NETWORK_CLIENT_H */
ClientNetworkGameSocketHandler::STATUS_COMPANY_INFO
We are trying to get company information.
Definition: network_client.h:28
ClientNetworkGameSocketHandler::STATUS_INACTIVE
The client is not connected nor active.
Definition: network_client.h:27
ClientNetworkGameSocketHandler::STATUS_END
Must ALWAYS be on the end of this list!! (period)
Definition: network_client.h:37
ClientNetworkGameSocketHandler::Receive_SERVER_WAIT
virtual NetworkRecvStatus Receive_SERVER_WAIT(Packet *p)
Notification that another client is currently receiving the map: uint8 Number of clients waiting in f...
Definition: network_client.cpp:760
ClientNetworkGameSocketHandler::savegame
struct PacketReader * savegame
Packet reader for reading the savegame.
Definition: network_client.h:22
Packet
Internal entity of a packet.
Definition: packet.h:44
ClientNetworkGameSocketHandler::SendJoin
static NetworkRecvStatus SendJoin()
Tell the server we would like to join.
Definition: network_client.cpp:326
ClientNetworkGameSocketHandler::Receive
static bool Receive()
Check whether we received/can send some data from/to the server and when that&#39;s the case handle it ap...
Definition: network_client.cpp:213
ClientNetworkGameSocketHandler::STATUS_AUTHORIZED
The client is authorized at the server.
Definition: network_client.h:33
ClientNetworkGameSocketHandler::Receive_SERVER_JOIN
virtual NetworkRecvStatus Receive_SERVER_JOIN(Packet *p)
A client joined (PACKET_CLIENT_MAP_OK), what usually directly follows is a PACKET_SERVER_CLIENT_INFO:...
Definition: network_client.cpp:1030
ClientNetworkGameSocketHandler::ServerStatus
ServerStatus
Status of the connection with the server.
Definition: network_client.h:26
ClientNetworkGameSocketHandler::Receive_SERVER_SHUTDOWN
virtual NetworkRecvStatus Receive_SERVER_SHUTDOWN(Packet *p)
Let the clients know that the server is closing.
Definition: network_client.cpp:1046
ClientNetworkGameSocketHandler::SendGamePassword
static NetworkRecvStatus SendGamePassword(const char *password)
Set the game password as requested.
Definition: network_client.cpp:354
ClientNetworkGameSocketHandler::Receive_SERVER_CHAT
virtual NetworkRecvStatus Receive_SERVER_CHAT(Packet *p)
Sends a chat-packet to the client: uint8 ID of the action (see NetworkAction).
Definition: network_client.cpp:940
ClientNetworkGameSocketHandler::Receive_SERVER_NEWGAME
virtual NetworkRecvStatus Receive_SERVER_NEWGAME(Packet *p)
Let the clients know that the server is loading a new map.
Definition: network_client.cpp:1057
ClientNetworkGameSocketHandler::ClientError
void ClientError(NetworkRecvStatus res)
Handle an error coming from the client side.
Definition: network_client.cpp:173
ClientNetworkGameSocketHandler::SendMapOk
static NetworkRecvStatus SendMapOk()
Tell the server we received the complete map.
Definition: network_client.cpp:385
NetworkErrorCode
NetworkErrorCode
The error codes we send around in the protocols.
Definition: network_type.h:104
ClientNetworkGameSocketHandler::Receive_SERVER_FULL
virtual NetworkRecvStatus Receive_SERVER_FULL(Packet *p)
Notification that the server is full.
Definition: network_client.cpp:525
ClientNetworkGameSocketHandler::SendNewGRFsOk
static NetworkRecvStatus SendNewGRFsOk()
Tell the server we got all the NewGRFs.
Definition: network_client.cpp:343
ClientNetworkGameSocketHandler::Receive_SERVER_CLIENT_INFO
virtual NetworkRecvStatus Receive_SERVER_CLIENT_INFO(Packet *p)
Send information about a client: uint32 ID of the client (always unique on a server.
Definition: network_client.cpp:587
_network_join_server_password
const char * _network_join_server_password
Login password from -p argument.
Definition: network_client.cpp:301
ClientNetworkGameSocketHandler::ClientNetworkGameSocketHandler
ClientNetworkGameSocketHandler(SOCKET s)
Create a new socket for the client side of the game connection.
Definition: network_client.cpp:126
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:447
ClientNetworkGameSocketHandler::Receive_SERVER_QUIT
virtual NetworkRecvStatus Receive_SERVER_QUIT(Packet *p)
Notification that a client left the game: uint32 ID of the client.
Definition: network_client.cpp:1010
ClientNetworkGameSocketHandler
Class for handling the client side of the game connection.
Definition: network_client.h:20
ClientNetworkGameSocketHandler::Receive_SERVER_CHECK_NEWGRFS
virtual NetworkRecvStatus Receive_SERVER_CHECK_NEWGRFS(Packet *p)
Sends information about all used GRFs to the client: uint8 Amount of GRFs (the following data is repe...
Definition: network_client.cpp:678
ClientNetworkGameSocketHandler::Receive_SERVER_WELCOME
virtual NetworkRecvStatus Receive_SERVER_WELCOME(Packet *p)
The client is joined and ready to receive his map: uint32 Own client ID.
Definition: network_client.cpp:745
ClientNetworkGameSocketHandler::Receive_SERVER_ERROR
virtual NetworkRecvStatus Receive_SERVER_ERROR(Packet *p)
The client made an error: uint8 Error code caused (see NetworkErrorCode).
Definition: network_client.cpp:640
ClientNetworkGameSocketHandler::Receive_SERVER_COMMAND
virtual NetworkRecvStatus Receive_SERVER_COMMAND(Packet *p)
Sends a DoCommand to the client: uint8 ID of the company (0..MAX_COMPANIES-1).
Definition: network_client.cpp:921
_network_join_as
CompanyID _network_join_as
Who would we like to join as.
Definition: network_client.cpp:298
ClientNetworkGameSocketHandler::~ClientNetworkGameSocketHandler
~ClientNetworkGameSocketHandler()
Clear whatever we assigned.
Definition: network_client.cpp:133
ClientNetworkGameSocketHandler::IsConnected
static bool IsConnected()
Check whether the client is actually connected (and in the game).
Definition: network_client.cpp:512
ClientNetworkGameSocketHandler::SendAck
static NetworkRecvStatus SendAck()
Send an acknowledgement from the server&#39;s ticks.
Definition: network_client.cpp:395
NetworkClientSetCompanyPassword
void NetworkClientSetCompanyPassword(const char *password)
Set/Reset company password on the client side.
Definition: network_client.cpp:1261
ClientNetworkGameSocketHandler::SendCompanyPassword
static NetworkRecvStatus SendCompanyPassword(const char *password)
Set the company password as requested.
Definition: network_client.cpp:366
ClientNetworkGameSocketHandler::NetworkExecuteLocalCommandQueue
friend void NetworkExecuteLocalCommandQueue()
Execute all commands on the local command queue that ought to be executed this frame.
Definition: network_command.cpp:195
PacketReader
Read some packets, and when do use that data as initial load filter.
Definition: network_client.cpp:43
ClientNetworkGameSocketHandler::CheckConnection
void CheckConnection()
Check the connection&#39;s state, i.e.
Definition: network_client.cpp:1138
ClientNetworkGameSocketHandler::SendError
static NetworkRecvStatus SendError(NetworkErrorCode errorno)
Send an error-packet over the network.
Definition: network_client.cpp:434
ClientNetworkGameSocketHandler::STATUS_MAP_WAIT
The client is waiting as someone else is downloading the map.
Definition: network_client.h:34
ClientNetworkGameSocketHandler::NetworkClose
friend void NetworkClose(bool close_admins)
Close current connections.
Definition: network.cpp:530
ClientNetworkGameSocketHandler::STATUS_NEWGRFS_CHECK
Last action was checking NewGRFs.
Definition: network_client.h:30
ClientNetworkGameSocketHandler::my_client
static ClientNetworkGameSocketHandler * my_client
This is us!
Definition: network_client.h:45
NetworkRecvStatus
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition: core.h:26
ClientNetworkGameSocketHandler::Receive_SERVER_COMPANY_INFO
virtual NetworkRecvStatus Receive_SERVER_COMPANY_INFO(Packet *p)
Sends information about the companies (one packet per company): uint8 Version of the structure of thi...
Definition: network_client.cpp:543
ClientNetworkGameSocketHandler::Receive_SERVER_COMPANY_UPDATE
virtual NetworkRecvStatus Receive_SERVER_COMPANY_UPDATE(Packet *p)
Update the clients knowledge of which company is password protected: uint16 Bitwise representation of...
Definition: network_client.cpp:1125
ClientNetworkGameSocketHandler::Receive_SERVER_MAP_DATA
virtual NetworkRecvStatus Receive_SERVER_MAP_DATA(Packet *p)
Sends the data of the map to the client: Contains a part of the map (until max size of packet)...
Definition: network_client.cpp:804
ZeroedMemoryAllocator
Base class that provides memory initialization on dynamically created objects.
Definition: alloc_type.hpp:150
ClientNetworkGameSocketHandler::status
ServerStatus status
Status of the connection with the server.
Definition: network_client.h:40
ClientNetworkGameSocketHandler::CloseConnection
NetworkRecvStatus CloseConnection(NetworkRecvStatus status)
Close the network connection due to the given status.
Definition: network_client.cpp:141
ClientNetworkGameSocketHandler::SendRCon
static NetworkRecvStatus SendRCon(const char *password, const char *command)
Send a console command.
Definition: network_client.cpp:485
ClientNetworkGameSocketHandler::SendMove
static NetworkRecvStatus SendMove(CompanyID company, const char *password)
Ask the server to move us.
Definition: network_client.cpp:499
ClientNetworkGameSocketHandler::Receive_SERVER_NEED_COMPANY_PASSWORD
virtual NetworkRecvStatus Receive_SERVER_NEED_COMPANY_PASSWORD(Packet *p)
Indication to the client that the server needs a company password: uint32 Generation seed...
Definition: network_client.cpp:726
ClientNetworkGameSocketHandler::Receive_SERVER_BANNED
virtual NetworkRecvStatus Receive_SERVER_BANNED(Packet *p)
Notification that the client trying to join is banned.
Definition: network_client.cpp:534
ClientNetworkGameSocketHandler::Receive_SERVER_MOVE
virtual NetworkRecvStatus Receive_SERVER_MOVE(Packet *p)
Move a client from one company into another: uint32 ID of the client.
Definition: network_client.cpp:1087
ClientNetworkGameSocketHandler::SendCompanyInformationQuery
static NetworkRecvStatus SendCompanyInformationQuery()
Query the server for company information.
Definition: network_client.cpp:314
ClientNetworkGameSocketHandler::Receive_SERVER_SYNC
virtual NetworkRecvStatus Receive_SERVER_SYNC(Packet *p)
Sends a sync-check to the client: uint32 Frame counter.
Definition: network_client.cpp:908
ClientNetworkGameSocketHandler::Receive_SERVER_ERROR_QUIT
virtual NetworkRecvStatus Receive_SERVER_ERROR_QUIT(Packet *p)
Inform all clients that one client made an error and thus has quit/been disconnected: uint32 ID of th...
Definition: network_client.cpp:993
network_internal.h
Variables and function used internally.
NetworkClient_Connected
void NetworkClient_Connected()
Is called after a client is connected to the server.
Definition: network_client.cpp:1170
ClientNetworkGameSocketHandler::Receive_SERVER_NEED_GAME_PASSWORD
virtual NetworkRecvStatus Receive_SERVER_NEED_GAME_PASSWORD(Packet *p)
Indication to the client that the server needs a game password.
Definition: network_client.cpp:711
ClientNetworkGameSocketHandler::Receive_SERVER_MAP_BEGIN
virtual NetworkRecvStatus Receive_SERVER_MAP_BEGIN(Packet *p)
Sends that the server will begin with sending the map to the client: uint32 Current frame...
Definition: network_client.cpp:773
ClientNetworkGameSocketHandler::STATUS_ACTIVE
The client is active within in the game.
Definition: network_client.h:36
ClientNetworkGameSocketHandler::Receive_SERVER_RCON
virtual NetworkRecvStatus Receive_SERVER_RCON(Packet *p)
Send the result of an issues RCon command back to the client: uint16 Colour code. ...
Definition: network_client.cpp:1072
ClientNetworkGameSocketHandler::SendQuit
static NetworkRecvStatus SendQuit()
Tell the server we would like to quit.
Definition: network_client.cpp:472
ClientNetworkGameSocketHandler::STATUS_JOIN
We are trying to join a server.
Definition: network_client.h:29
CommandPacket
Everything we need to know about a command to be able to execute it.
Definition: network_internal.h:153
ClientNetworkGameSocketHandler::Send
static void Send()
Send the packets of this socket handler.
Definition: network_client.cpp:228
ClientNetworkGameSocketHandler::Receive_SERVER_MAP_DONE
virtual NetworkRecvStatus Receive_SERVER_MAP_DONE(Packet *p)
Sends that all data of the map are sent to the client:
Definition: network_client.cpp:818
_network_join_company_password
const char * _network_join_company_password
Company password from -P argument.
Definition: network_client.cpp:303
MyClient
ClientNetworkGameSocketHandler MyClient
Helper to make the code look somewhat nicer.
Definition: network_client.h:111
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:460
ClientNetworkGameSocketHandler::STATUS_MAP
The client is downloading the map.
Definition: network_client.h:35
ClientNetworkGameSocketHandler::SendGetMap
static NetworkRecvStatus SendGetMap()
Request the map from the server.
Definition: network_client.cpp:375
ClientNetworkGameSocketHandler::SendCommand
static NetworkRecvStatus SendCommand(const CommandPacket *cp)
Send a command to the server.
Definition: network_client.cpp:409
ClientNetworkGameSocketHandler::STATUS_AUTH_GAME
Last action was requesting game (server) password.
Definition: network_client.h:31
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
ClientNetworkGameSocketHandler::Receive_SERVER_FRAME
virtual NetworkRecvStatus Receive_SERVER_FRAME(Packet *p)
Sends the current frame counter to the client: uint32 Frame counter uint32 Frame counter max (how far...
Definition: network_client.cpp:875
ClientNetworkGameSocketHandler::STATUS_AUTH_COMPANY
Last action was requesting company password.
Definition: network_client.h:32
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:419
ClientNetworkGameSocketHandler::Receive_SERVER_MAP_SIZE
virtual NetworkRecvStatus Receive_SERVER_MAP_SIZE(Packet *p)
Sends the size of the map to the client.
Definition: network_client.cpp:793
ClientNetworkGameSocketHandler::GameLoop
static bool GameLoop()
Actual game loop for the client.
Definition: network_client.cpp:238
ClientNetworkGameSocketHandler::token
byte token
The token we need to send back to the server to prove we&#39;re the right client.
Definition: network_client.h:23
DestType
DestType
Destination of our chat messages.
Definition: network_type.h:82
NetworkAction
NetworkAction
Actions that can be used for NetworkTextMessage.
Definition: network_type.h:89
ClientNetworkGameSocketHandler::Receive_SERVER_CONFIG_UPDATE
virtual NetworkRecvStatus Receive_SERVER_CONFIG_UPDATE(Packet *p)
Update the clients knowledge of the max settings: uint8 Maximum number of companies allowed...
Definition: network_client.cpp:1115