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

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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#include "../strings_type.h"
21
22#ifdef RANDOM_DEBUG
32#define ENABLE_NETWORK_SYNC_EVERY_FRAME
33
38#define NETWORK_SEND_DOUBLE_SEED
39#endif /* RANDOM_DEBUG */
40
41typedef class ServerNetworkGameSocketHandler NetworkClientSocket;
42
44 enum NetworkJoinStatus : uint8_t {
45 NETWORK_JOIN_STATUS_CONNECTING,
46 NETWORK_JOIN_STATUS_AUTHORIZING,
47 NETWORK_JOIN_STATUS_WAITING,
48 NETWORK_JOIN_STATUS_DOWNLOADING,
49 NETWORK_JOIN_STATUS_PROCESSING,
50 NETWORK_JOIN_STATUS_REGISTERING,
51
52 NETWORK_JOIN_STATUS_GETTING_COMPANY_INFO,
53 NETWORK_JOIN_STATUS_END,
54};
55
56extern uint32_t _frame_counter_server; // The frame_counter of the server, if in network-mode
57extern uint32_t _frame_counter_max; // To where we may go with our clients
58extern uint32_t _frame_counter;
59
60extern uint32_t _last_sync_frame; // Used in the server to store the last time a sync packet was sent to clients.
61
62/* networking settings */
63extern NetworkAddressList _broadcast_list;
64
65extern uint32_t _sync_seed_1;
66#ifdef NETWORK_SEND_DOUBLE_SEED
67extern uint32_t _sync_seed_2;
68#endif
69extern uint32_t _sync_frame;
70extern bool _network_first_time;
71/* Vars needed for the join-GUI */
72extern NetworkJoinStatus _network_join_status;
73extern uint8_t _network_join_waiting;
74extern uint32_t _network_join_bytes;
75extern uint32_t _network_join_bytes_total;
76extern ConnectionType _network_server_connection_type;
77extern std::string _network_server_invite_code;
78
79/* Variable available for clients. */
80extern std::string _network_server_name;
81
82extern uint8_t _network_reconnect;
83
84void NetworkQueryServer(std::string_view connection_string);
85
86void GetBindAddresses(NetworkAddressList *addresses, uint16_t port);
87struct NetworkGame *NetworkAddServer(std::string_view connection_string, bool manually = true, bool never_expire = false);
88void NetworkRebuildHostList();
89void UpdateNetworkGameWindow();
90
91/* From network_command.cpp */
95 struct CommandPacket {
96 CompanyID company = CompanyID::Invalid();
97 uint32_t frame = 0;
98 bool my_cmd = false;
99
100 Commands cmd{};
101 StringID err_msg{};
102 CommandCallback *callback = nullptr;
103 CommandDataBuffer data{};
104};
105
106void NetworkDistributeCommands();
107void NetworkExecuteLocalCommandQueue();
108void NetworkFreeLocalCommandQueue();
109void NetworkSyncCommandQueue(NetworkClientSocket *cs);
110void NetworkReplaceCommandClientId(CommandPacket &cp, ClientID client_id);
111
112void ShowNetworkError(StringID error_string);
113void NetworkTextMessage(NetworkAction action, TextColour colour, bool self_send, std::string_view name, std::string_view str = {}, StringParameter &&data = {});
114uint NetworkCalculateLag(const NetworkClientSocket *cs);
115 StringID GetNetworkErrorMsg(NetworkErrorCode err);
116bool NetworkMakeClientNameUnique(std::string &new_name);
117
118std::string_view ParseCompanyFromConnectionString(std::string_view connection_string, CompanyID *company_id);
119 NetworkAddress ParseConnectionString(std::string_view connection_string, uint16_t default_port);
120std::string NormalizeConnectionString(std::string_view connection_string, uint16_t default_port);
121
122void ClientNetworkEmergencySave();
123
124#endif /* NETWORK_INTERNAL_H */
NetworkAddressList
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
Definition address.h:20
NetworkAddress
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition address.h:28
ServerNetworkGameSocketHandler
Class for handling the server side of the game connection.
Definition network_server.h:24
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:495
CommandDataBuffer
std::vector< uint8_t > CommandDataBuffer
Storage buffer for serialized command data.
Definition command_type.h:481
Commands
Commands
List of commands.
Definition command_type.h:196
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:307
network_func.h
Network functions used by other parts of OpenTTD.
_last_sync_frame
uint32_t _last_sync_frame
Used in the server to store the last time a sync packet was sent to clients.
Definition network.cpp:81
ParseConnectionString
NetworkAddress ParseConnectionString(std::string_view connection_string, uint16_t default_port)
Convert a string containing either "hostname" or "hostname:ip" to a NetworkAddress.
Definition network.cpp:555
GetNetworkErrorMsg
StringID GetNetworkErrorMsg(NetworkErrorCode err)
Retrieve the string id of an internal error number.
Definition network.cpp:311
_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
_frame_counter_server
uint32_t _frame_counter_server
The frame_counter of the server, if in network-mode.
Definition network.cpp:78
NetworkJoinStatus
NetworkJoinStatus
Status of the clients during joining.
Definition network_internal.h:44
_broadcast_list
NetworkAddressList _broadcast_list
List of broadcast addresses.
Definition network.cpp:82
_network_join_status
NetworkJoinStatus _network_join_status
The status of joining.
Definition network_gui.cpp:1987
NetworkFreeLocalCommandQueue
void NetworkFreeLocalCommandQueue()
Free the local command queues.
Definition network_command.cpp:290
UpdateNetworkGameWindow
void UpdateNetworkGameWindow()
Update the network new window because a new server is found on the network.
Definition network_gui.cpp:66
_network_join_waiting
uint8_t _network_join_waiting
The number of clients waiting in front of us.
Definition network_gui.cpp:1988
_frame_counter
uint32_t _frame_counter
The current frame.
Definition network.cpp:80
_network_reconnect
uint8_t _network_reconnect
Reconnect timeout.
Definition network.cpp:74
NetworkExecuteLocalCommandQueue
void NetworkExecuteLocalCommandQueue()
Execute all commands on the local command queue that ought to be executed this frame.
Definition network_command.cpp:256
NetworkQueryServer
void NetworkQueryServer(std::string_view connection_string)
Query a server to fetch the game-info.
Definition network.cpp:671
ClientNetworkEmergencySave
void ClientNetworkEmergencySave()
Create an emergency savegame when the network connection is lost.
Definition network_client.cpp:91
NetworkDistributeCommands
void NetworkDistributeCommands()
Distribute the commands of ourself and the clients.
Definition network_command.cpp:357
NetworkMakeClientNameUnique
bool NetworkMakeClientNameUnique(std::string &new_name)
Check whether a name is unique, and otherwise try to make it unique.
Definition network_server.cpp:1608
_sync_seed_1
uint32_t _sync_seed_1
Seed to compare during sync checks.
Definition network.cpp:83
_sync_frame
uint32_t _sync_frame
The frame to perform the sync check.
Definition network.cpp:87
NormalizeConnectionString
std::string NormalizeConnectionString(std::string_view connection_string, uint16_t default_port)
Normalize a connection string.
Definition network.cpp:540
NetworkAddServer
struct NetworkGame * NetworkAddServer(std::string_view 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:693
ParseCompanyFromConnectionString
std::string_view ParseCompanyFromConnectionString(std::string_view connection_string, CompanyID *company_id)
Parse the company part ("#company" postfix) of a connecting string.
Definition network.cpp:476
_network_join_bytes_total
uint32_t _network_join_bytes_total
The total number of bytes to download.
Definition network_gui.cpp:1990
NetworkSyncCommandQueue
void NetworkSyncCommandQueue(NetworkClientSocket *cs)
Sync our local command queue to the command queue of the given socket.
Definition network_command.cpp:245
_network_first_time
bool _network_first_time
Whether we have finished joining or not.
Definition network.cpp:88
_network_server_name
std::string _network_server_name
The current name of the server you are on.
Definition network_client.cpp:275
_network_join_bytes
uint32_t _network_join_bytes
The number of bytes we already downloaded.
Definition network_gui.cpp:1989
GetBindAddresses
void GetBindAddresses(NetworkAddressList *addresses, uint16_t port)
Get the addresses to bind to.
Definition network.cpp:719
NetworkReplaceCommandClientId
void NetworkReplaceCommandClientId(CommandPacket &cp, ClientID client_id)
Insert a client ID into the command data in a command packet.
Definition network_command.cpp:444
_frame_counter_max
uint32_t _frame_counter_max
To where we may go with our clients.
Definition network.cpp:79
_network_server_invite_code
std::string _network_server_invite_code
Our invite code as indicated by the Game Coordinator.
Definition network_coordinator.cpp:32
NetworkErrorCode
NetworkErrorCode
The error codes we send around in the protocols.
Definition network_type.h:99
NetworkAction
NetworkAction
Actions that can be used for NetworkTextMessage.
Definition network_type.h:79
ClientID
ClientID
'Unique' identifier to be given to clients
Definition network_type.h:43
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:19
CommandPacket
Everything we need to know about a command to be able to execute it.
Definition network_internal.h:95
CommandPacket::err_msg
StringID err_msg
string ID of error message to use.
Definition network_internal.h:101
CommandPacket::data
CommandDataBuffer data
command parameters.
Definition network_internal.h:103
CommandPacket::callback
CommandCallback * callback
any callback function executed upon successful completion of the command.
Definition network_internal.h:102
CommandPacket::frame
uint32_t frame
the frame in which this packet is executed
Definition network_internal.h:97
CommandPacket::company
CompanyID company
company that is executing the command
Definition network_internal.h:96
CommandPacket::my_cmd
bool my_cmd
did the command originate from "me"
Definition network_internal.h:98
CommandPacket::cmd
Commands cmd
command being executed.
Definition network_internal.h:100
NetworkGame
Structure with information shown in the game list (GUI)
Definition network_gamelist.h:27
NetworkGame::connection_string
std::string connection_string
Address of the server.
Definition network_gamelist.h:31
NetworkGame::manually
bool manually
True if the server was added manually.
Definition network_gamelist.h:33
PoolID< uint8_t, struct CompanyIDTag, 0xF, 0xFF >
StringParameter
The data required to format and validate a single parameter of a string.
Definition strings_type.h:78
tcp_coordinator.h
Basic functions to receive and send TCP packets to/from the Game Coordinator server.
ConnectionType
ConnectionType
The type of connection the Game Coordinator can detect we have.
Definition tcp_coordinator.h:52
tcp_game.h
Basic functions to receive and send TCP packets for game purposes.