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

OpenTTD Source 13.1

network_internal.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_INTERNAL_H
11 #define NETWORK_INTERNAL_H
12
13 #include "network_func.h"
14 #include "core/tcp_coordinator.h"
15 #include "core/tcp_game.h"
16
17 #include "../command_type.h"
18 #include "../command_func.h"
19 #include "../misc/endian_buffer.hpp"
20
21 #ifdef RANDOM_DEBUG
22
31 #define ENABLE_NETWORK_SYNC_EVERY_FRAME
32
37 #define NETWORK_SEND_DOUBLE_SEED
38 #endif /* RANDOM_DEBUG */
39
47 #ifdef DEBUG_DUMP_COMMANDS
48 extern bool _ddc_fastforward;
49 #else
50 #define _ddc_fastforward (false)
51 #endif /* DEBUG_DUMP_COMMANDS */
52
53 typedef class ServerNetworkGameSocketHandler NetworkClientSocket;
54
56 enum NetworkJoinStatus {
57 NETWORK_JOIN_STATUS_CONNECTING,
58 NETWORK_JOIN_STATUS_AUTHORIZING,
59 NETWORK_JOIN_STATUS_WAITING,
60 NETWORK_JOIN_STATUS_DOWNLOADING,
61 NETWORK_JOIN_STATUS_PROCESSING,
62 NETWORK_JOIN_STATUS_REGISTERING,
63
64 NETWORK_JOIN_STATUS_GETTING_COMPANY_INFO,
65 NETWORK_JOIN_STATUS_END,
66 };
67
68 extern uint32 _frame_counter_server; // The frame_counter of the server, if in network-mode
69 extern uint32 _frame_counter_max; // To where we may go with our clients
70 extern uint32 _frame_counter;
71
72 extern uint32 _last_sync_frame; // Used in the server to store the last time a sync packet was sent to clients.
73
74 /* networking settings */
75 extern NetworkAddressList _broadcast_list;
76
77 extern uint32 _sync_seed_1;
78 #ifdef NETWORK_SEND_DOUBLE_SEED
79 extern uint32 _sync_seed_2;
80 #endif
81 extern uint32 _sync_frame;
82 extern bool _network_first_time;
83 /* Vars needed for the join-GUI */
84 extern NetworkJoinStatus _network_join_status;
85 extern uint8 _network_join_waiting;
86 extern uint32 _network_join_bytes;
87 extern uint32 _network_join_bytes_total;
88 extern ConnectionType _network_server_connection_type;
89 extern std::string _network_server_invite_code;
90
91 /* Variable available for clients. */
92 extern std::string _network_server_name;
93
94 extern uint8 _network_reconnect;
95
96 extern CompanyMask _network_company_passworded;
97
98 void NetworkQueryServer(const std::string &connection_string);
99
100 void GetBindAddresses(NetworkAddressList *addresses, uint16 port);
101 struct NetworkGameList *NetworkAddServer(const std::string &connection_string, bool manually = true, bool never_expire = false);
102 void NetworkRebuildHostList();
103 void UpdateNetworkGameWindow();
104
105 /* From network_command.cpp */
109 struct CommandPacket {
111 CommandPacket() : next(nullptr), company(INVALID_COMPANY), frame(0), my_cmd(false) {}
112 CommandPacket *next;
113 CompanyID company;
114 uint32 frame;
115 bool my_cmd;
116
117 Commands cmd;
118 StringID err_msg;
119 CommandCallback *callback;
120 CommandDataBuffer data;
121 };
122
123 void NetworkDistributeCommands();
124 void NetworkExecuteLocalCommandQueue();
125 void NetworkFreeLocalCommandQueue();
126 void NetworkSyncCommandQueue(NetworkClientSocket *cs);
127 void NetworkReplaceCommandClientId(CommandPacket &cp, ClientID client_id);
128
129 void ShowNetworkError(StringID error_string);
130 void NetworkTextMessage(NetworkAction action, TextColour colour, bool self_send, const std::string &name, const std::string &str = "", int64 data = 0, const std::string &data_str = "");
131 uint NetworkCalculateLag(const NetworkClientSocket *cs);
132 StringID GetNetworkErrorMsg(NetworkErrorCode err);
133 bool NetworkMakeClientNameUnique(std::string &new_name);
134 std::string GenerateCompanyPasswordHash(const std::string &password, const std::string &password_server_id, uint32 password_game_seed);
135
136 std::string_view ParseCompanyFromConnectionString(const std::string &connection_string, CompanyID *company_id);
137 NetworkAddress ParseConnectionString(const std::string &connection_string, uint16 default_port);
138 std::string NormalizeConnectionString(const std::string &connection_string, uint16 default_port);
139
140 #endif /* NETWORK_INTERNAL_H */
NetworkGameList::connection_string
std::string connection_string
Address of the server.
Definition: network_gamelist.h:31
_network_join_waiting
uint8 _network_join_waiting
The number of clients waiting in front of us.
Definition: network_gui.cpp:2153
Commands
Commands
List of commands.
Definition: command_type.h:176
CommandDataBuffer
std::vector< byte > CommandDataBuffer
Storage buffer for serialized command data.
Definition: command_type.h:451
NetworkQueryServer
void NetworkQueryServer(const std::string &connection_string)
Query a server to fetch the game-info.
Definition: network.cpp:655
_network_reconnect
uint8 _network_reconnect
Reconnect timeout.
Definition: network.cpp:66
NormalizeConnectionString
std::string NormalizeConnectionString(const std::string &connection_string, uint16 default_port)
Normalize a connection string.
Definition: network.cpp:527
NetworkAction
NetworkAction
Actions that can be used for NetworkTextMessage.
Definition: network_type.h:99
CommandPacket::frame
uint32 frame
the frame in which this packet is executed
Definition: network_internal.h:114
NetworkJoinStatus
NetworkJoinStatus
Status of the clients during joining.
Definition: network_internal.h:56
_network_join_bytes
uint32 _network_join_bytes
The number of bytes we already downloaded.
Definition: network_gui.cpp:2154
_network_server_invite_code
std::string _network_server_invite_code
Our invite code as indicated by the Game Coordinator.
Definition: network_coordinator.cpp:32
CommandPacket::err_msg
StringID err_msg
string ID of error message to use.
Definition: network_internal.h:118
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:257
NetworkDistributeCommands
void NetworkDistributeCommands()
Distribute the commands of ourself and the clients.
Definition: network_command.cpp:406
ParseCompanyFromConnectionString
std::string_view ParseCompanyFromConnectionString(const std::string &connection_string, CompanyID *company_id)
Parse the company part ("#company" postfix) of a connecting string.
Definition: network.cpp:463
_network_first_time
bool _network_first_time
Whether we have finished joining or not.
Definition: network.cpp:80
CommandCallback
void CommandCallback(Commands cmd, const CommandCost &result, TileIndex tile)
Define a callback function for the client, after the command is finished.
Definition: command_type.h:465
CommandPacket::next
CommandPacket * next
the next command packet (if in queue)
Definition: network_internal.h:112
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
NetworkReplaceCommandClientId
static void NetworkReplaceCommandClientId(CommandPacket &cp, ClientID client_id)
Insert a client ID into the command data in a command packet.
Definition: network_command.cpp:476
CommandPacket::company
CompanyID company
company that is executing the command
Definition: network_internal.h:113
CommandPacket
Everything we need to know about a command to be able to execute it.
Definition: network_internal.h:109
NetworkAddressList
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
Definition: address.h:21
ClientID
ClientID
'Unique' identifier to be given to clients
Definition: network_type.h:47
NetworkAddServer
struct NetworkGameList * NetworkAddServer(const std::string &connection_string, bool manually=true, bool never_expire=false)
Validates an address entered as a string and adds the server to the list.
Definition: network.cpp:675
GenerateCompanyPasswordHash
std::string GenerateCompanyPasswordHash(const std::string &password, const std::string &password_server_id, uint32 password_game_seed)
Hash the given password using server ID and game seed.
Definition: network.cpp:177
NetworkGameList::manually
bool manually
True if the server was added manually.
Definition: network_gamelist.h:33
_network_server_name
std::string _network_server_name
The current name of the server you are on.
Definition: network_client.cpp:320
_network_company_passworded
CompanyMask _network_company_passworded
Bitmask of the password status of all companies.
Definition: network.cpp:81
CommandPacket::my_cmd
bool my_cmd
did the command originate from "me"
Definition: network_internal.h:115
GetBindAddresses
void GetBindAddresses(NetworkAddressList *addresses, uint16 port)
Get the addresses to bind to.
Definition: network.cpp:701
ConnectionType
ConnectionType
The type of connection the Game Coordinator can detect we have.
Definition: tcp_coordinator.h:52
ParseConnectionString
NetworkAddress ParseConnectionString(const std::string &connection_string, uint16 default_port)
Convert a string containing either "hostname" or "hostname:ip" to a NetworkAddress.
Definition: network.cpp:542
ServerNetworkGameSocketHandler
Class for handling the server side of the game connection.
Definition: network_server.h:24
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
_ddc_fastforward
#define _ddc_fastforward
Helper variable to make the dedicated server go fast until the (first) join.
Definition: network_internal.h:50
NetworkAddress
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition: address.h:30
_last_sync_frame
uint32 _last_sync_frame
Used in the server to store the last time a sync packet was sent to clients.
Definition: network.cpp:73
CommandPacket::CommandPacket
CommandPacket()
Make sure the pointer is nullptr.
Definition: network_internal.h:111
CommandPacket::data
CommandDataBuffer data
command parameters.
Definition: network_internal.h:120
NetworkSyncCommandQueue
void NetworkSyncCommandQueue(NetworkClientSocket *cs)
Sync our local command queue to the command queue of the given socket.
Definition: network_command.cpp:304
_sync_seed_1
uint32 _sync_seed_1
Seed to compare during sync checks.
Definition: network.cpp:75
tcp_coordinator.h
_sync_frame
uint32 _sync_frame
The frame to perform the sync check.
Definition: network.cpp:79
NetworkGameList
Structure with information shown in the game list (GUI)
Definition: network_gamelist.h:27
_network_join_status
NetworkJoinStatus _network_join_status
The status of joining.
Definition: network_gui.cpp:2152
_frame_counter
uint32 _frame_counter
The current frame.
Definition: network.cpp:72
_network_server_connection_type
ConnectionType _network_server_connection_type
What type of connection the Game Coordinator detected we are on.
Definition: network_coordinator.cpp:31
INVALID_COMPANY
@ INVALID_COMPANY
An invalid company.
Definition: company_type.h:30
NetworkExecuteLocalCommandQueue
void NetworkExecuteLocalCommandQueue()
Execute all commands on the local command queue that ought to be executed this frame.
Definition: network_command.cpp:316
GetNetworkErrorMsg
StringID GetNetworkErrorMsg(NetworkErrorCode err)
Retrieve the string id of an internal error number.
Definition: network.cpp:301
_broadcast_list
NetworkAddressList _broadcast_list
List of broadcast addresses.
Definition: network.cpp:74
UpdateNetworkGameWindow
void UpdateNetworkGameWindow()
Update the network new window because a new server is found on the network.
Definition: network_gui.cpp:67
tcp_game.h
NetworkMakeClientNameUnique
bool NetworkMakeClientNameUnique(std::string &new_name)
Check whether a name is unique, and otherwise try to make it unique.
Definition: network_server.cpp:1588
CommandPacket::callback
CommandCallback * callback
any callback function executed upon successful completion of the command.
Definition: network_internal.h:119
_frame_counter_server
uint32 _frame_counter_server
The frame_counter of the server, if in network-mode.
Definition: network.cpp:70
NetworkFreeLocalCommandQueue
void NetworkFreeLocalCommandQueue()
Free the local command queues.
Definition: network_command.cpp:352
_network_join_bytes_total
uint32 _network_join_bytes_total
The total number of bytes to download.
Definition: network_gui.cpp:2155
_frame_counter_max
uint32 _frame_counter_max
To where we may go with our clients.
Definition: network.cpp:71
network_func.h
NetworkErrorCode
NetworkErrorCode
The error codes we send around in the protocols.
Definition: network_type.h:119
CommandPacket::cmd
Commands cmd
command being executed.
Definition: network_internal.h:117