OpenTTD Source 20260311-master-g511d3794ce
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
9
10#ifndef NETWORK_INTERNAL_H
11#define NETWORK_INTERNAL_H
12
13#include "network_func.h"
15#include "core/tcp_game.h"
16
17#include "../command_type.h"
18#include "../command_func.h"
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
54
55extern uint32_t _frame_counter_server; // The frame_counter of the server, if in network-mode
56extern uint32_t _frame_counter_max; // To where we may go with our clients
57extern uint32_t _frame_counter;
58
59extern uint32_t _last_sync_frame; // Used in the server to store the last time a sync packet was sent to clients.
60
61/* networking settings */
63
64extern uint32_t _sync_seed_1;
65#ifdef NETWORK_SEND_DOUBLE_SEED
66extern uint32_t _sync_seed_2;
67#endif
68extern uint32_t _sync_frame;
69extern bool _network_first_time;
70/* Vars needed for the join-GUI */
72extern uint8_t _network_join_waiting;
73extern uint32_t _network_join_bytes;
74extern uint32_t _network_join_bytes_total;
76extern std::string _network_server_invite_code;
77
78/* Variable available for clients. */
79extern std::string _network_server_name;
80
81extern uint8_t _network_reconnect;
82
83void NetworkQueryServer(std::string_view connection_string);
84
85void GetBindAddresses(NetworkAddressList *addresses, uint16_t port);
86struct NetworkGame *NetworkAddServer(std::string_view connection_string, bool manually = true, bool never_expire = false);
89
90/* From network_command.cpp */
95 CompanyID company = CompanyID::Invalid();
96 uint32_t frame = 0;
97 bool my_cmd = false;
98
103};
104
108void NetworkSyncCommandQueue(NetworkClientSocket *cs);
110
111void ShowNetworkError(StringID error_string);
112void NetworkTextMessage(NetworkAction action, TextColour colour, bool self_send, std::string_view name, std::string_view str = {}, StringParameter &&data = {});
113uint NetworkCalculateLag(const NetworkClientSocket *cs);
115bool NetworkMakeClientNameUnique(std::string &new_name);
116
117std::string_view ParseCompanyFromConnectionString(std::string_view connection_string, CompanyID *company_id);
118NetworkAddress ParseConnectionString(std::string_view connection_string, uint16_t default_port);
119std::string NormalizeConnectionString(std::string_view connection_string, uint16_t default_port);
120
122
123#endif /* NETWORK_INTERNAL_H */
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
Definition address.h:20
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition address.h:28
Class for handling the server side of the game connection.
Functions related to commands.
Types related to commands.
void CommandCallback(Commands cmd, const CommandCost &result, TileIndex tile)
Define a callback function for the client, after the command is finished.
std::vector< uint8_t > CommandDataBuffer
Storage buffer for serialized command data.
Commands
List of commands.
Endian-aware buffer.
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:307
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
uint32_t _frame_counter_server
The frame_counter of the server, if in network-mode.
Definition network.cpp:78
NetworkAddressList _broadcast_list
List of broadcast addresses.
Definition network.cpp:82
uint32_t _frame_counter
The current frame.
Definition network.cpp:80
uint8_t _network_reconnect
Reconnect timeout.
Definition network.cpp:74
uint32_t _sync_seed_1
Seed to compare during sync checks.
Definition network.cpp:83
uint32_t _sync_frame
The frame to perform the sync check.
Definition network.cpp:87
bool _network_first_time
Whether we have finished joining or not.
Definition network.cpp:88
uint32_t _frame_counter_max
To where we may go with our clients.
Definition network.cpp:79
std::string _network_server_name
The current name of the server you are on.
ConnectionType _network_server_connection_type
What type of connection the Game Coordinator detected we are on.
std::string _network_server_invite_code
Our invite code as indicated by the Game Coordinator.
Network functions used by other parts of OpenTTD.
NetworkJoinStatus _network_join_status
The status of joining.
uint8_t _network_join_waiting
The number of clients waiting in front of us.
uint32_t _network_join_bytes_total
The total number of bytes to download.
uint32_t _network_join_bytes
The number of bytes we already downloaded.
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:550
StringID GetNetworkErrorMsg(NetworkErrorCode err)
Retrieve a short translateable string of the error code.
Definition network.cpp:312
NetworkJoinStatus
Status of the clients during joining.
@ Waiting
Waiting for other clients to finish downloading the map.
@ Processing
Loading the savegame.
@ Registering
Creating a new company.
@ End
Sentinel for end-of-enumeration.
@ Authorizing
Starting authorizing the client to join the game and optionally company.
@ Downloading
Downloading the map from the server.
@ Connecting
Opening the connection to the server.
void NetworkFreeLocalCommandQueue()
Free the local command queues.
void UpdateNetworkGameWindow()
Update the network new window because a new server is found on the network.
void NetworkExecuteLocalCommandQueue()
Execute all commands on the local command queue that ought to be executed this frame.
void NetworkQueryServer(std::string_view connection_string)
Query a server to fetch the game-info.
Definition network.cpp:666
void ClientNetworkEmergencySave()
Create an emergency savegame when the network connection is lost.
void NetworkDistributeCommands()
Distribute the commands of ourself and the clients.
bool NetworkMakeClientNameUnique(std::string &new_name)
Check whether a name is unique, and otherwise try to make it unique.
std::string NormalizeConnectionString(std::string_view connection_string, uint16_t default_port)
Normalize a connection string.
Definition network.cpp:535
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:688
void NetworkRebuildHostList()
Generates the list of manually added hosts from NetworkGame and dumps them into the array _network_ho...
Definition network.cpp:730
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:471
void NetworkSyncCommandQueue(NetworkClientSocket *cs)
Sync our local command queue to the command queue of the given socket.
void GetBindAddresses(NetworkAddressList *addresses, uint16_t port)
Get the addresses to bind to.
Definition network.cpp:714
void NetworkReplaceCommandClientId(CommandPacket &cp, ClientID client_id)
Insert a client ID into the command data in a command packet.
NetworkErrorCode
The error codes we send around in the protocols.
NetworkAction
Actions that can be used for NetworkTextMessage.
ClientID
'Unique' identifier to be given to clients
Types related to strings.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Everything we need to know about a command to be able to execute it.
StringID err_msg
string ID of error message to use.
CommandDataBuffer data
command parameters.
CommandCallback * callback
any callback function executed upon successful completion of the command.
uint32_t frame
the frame in which this packet is executed
CompanyID company
company that is executing the command
bool my_cmd
did the command originate from "me"
Commands cmd
command being executed.
Structure with information shown in the game list (GUI).
std::string connection_string
Address of the server.
bool manually
True if the server was added manually.
The data required to format and validate a single parameter of a string.
Basic functions to receive and send TCP packets to/from the Game Coordinator server.
ConnectionType
The type of connection the Game Coordinator can detect we have.
Basic functions to receive and send TCP packets for game purposes.