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/>.
8 /** @file network_internal.h Variables and function used internally. */
10 #ifndef NETWORK_INTERNAL_H
11 #define NETWORK_INTERNAL_H
13 #include "network_func.h"
14 #include "core/tcp_coordinator.h"
15 #include "core/tcp_game.h"
17 #include "../command_type.h"
18 #include "../command_func.h"
19 #include "../misc/endian_buffer.hpp"
23 * If this line is enable, every frame will have a sync test
24 * this is not needed in normal games. Normal is like 1 sync in 100
25 * frames. You can enable this if you have a lot of desyncs on a certain
27 * Remember: both client and server have to be compiled with this
28 * option enabled to make it to work. If one of the two has it disabled
29 * nothing will happen.
31 #define ENABLE_NETWORK_SYNC_EVERY_FRAME
34 * In theory sending 1 of the 2 seeds is enough to check for desyncs
35 * so in theory, this next define can be left off.
37 #define NETWORK_SEND_DOUBLE_SEED
38 #endif /* RANDOM_DEBUG */
41 * Helper variable to make the dedicated server go fast until the (first) join.
42 * Used to load the desync debug logs, i.e. for reproducing a desync.
43 * There's basically no need to ever enable this, unless you really know what
44 * you are doing, i.e. debugging a desync.
45 * See docs/desync.txt for details.
47 #ifdef DEBUG_DUMP_COMMANDS
48 extern bool _ddc_fastforward
;
50 #define _ddc_fastforward (false)
51 #endif /* DEBUG_DUMP_COMMANDS */
53 typedef class ServerNetworkGameSocketHandler NetworkClientSocket
;
55 /** Status of the clients during joining. */
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
,
64 NETWORK_JOIN_STATUS_GETTING_COMPANY_INFO
,
65 NETWORK_JOIN_STATUS_END
,
68 extern uint32_t _frame_counter_server
; // The frame_counter of the server, if in network-mode
69 extern uint32_t _frame_counter_max
; // To where we may go with our clients
70 extern uint32_t _frame_counter
;
72 extern uint32_t _last_sync_frame
; // Used in the server to store the last time a sync packet was sent to clients.
74 /* networking settings */
75 extern NetworkAddressList _broadcast_list
;
77 extern uint32_t _sync_seed_1
;
78 #ifdef NETWORK_SEND_DOUBLE_SEED
79 extern uint32_t _sync_seed_2
;
81 extern uint32_t _sync_frame
;
82 extern bool _network_first_time
;
83 /* Vars needed for the join-GUI */
84 extern NetworkJoinStatus _network_join_status
;
85 extern uint8_t _network_join_waiting
;
86 extern uint32_t _network_join_bytes
;
87 extern uint32_t _network_join_bytes_total
;
88 extern ConnectionType _network_server_connection_type
;
89 extern std::string _network_server_invite_code
;
91 /* Variable available for clients. */
92 extern std::string _network_server_name
;
94 extern uint8_t _network_reconnect
;
96 extern CompanyMask _network_company_passworded
;
98 void NetworkQueryServer(const std::string
&connection_string
);
100 void GetBindAddresses(NetworkAddressList
*addresses
, uint16_t port
);
101 struct NetworkGameList
*NetworkAddServer(const std::string
&connection_string
, bool manually
= true, bool never_expire
= false);
102 void NetworkRebuildHostList();
103 void UpdateNetworkGameWindow();
105 /* From network_command.cpp */
107 * Everything we need to know about a command to be able to execute it.
109 struct CommandPacket
{
110 CommandPacket() : company(INVALID_COMPANY
), frame(0), my_cmd(false) {}
111 CompanyID company
; ///< company that is executing the command
112 uint32_t frame
; ///< the frame in which this packet is executed
113 bool my_cmd
; ///< did the command originate from "me"
115 Commands cmd
; ///< command being executed.
116 StringID err_msg
; ///< string ID of error message to use.
117 CommandCallback
*callback
; ///< any callback function executed upon successful completion of the command.
118 CommandDataBuffer data
; ///< command parameters.
121 void NetworkDistributeCommands();
122 void NetworkExecuteLocalCommandQueue();
123 void NetworkFreeLocalCommandQueue();
124 void NetworkSyncCommandQueue(NetworkClientSocket
*cs
);
125 void NetworkReplaceCommandClientId(CommandPacket
&cp
, ClientID client_id
);
127 void ShowNetworkError(StringID error_string
);
128 void NetworkTextMessage(NetworkAction action
, TextColour colour
, bool self_send
, const std::string
&name
, const std::string
&str
= "", int64_t data
= 0, const std::string
&data_str
= "");
129 uint
NetworkCalculateLag(const NetworkClientSocket
*cs
);
130 StringID
GetNetworkErrorMsg(NetworkErrorCode err
);
131 bool NetworkMakeClientNameUnique(std::string
&new_name
);
132 std::string
GenerateCompanyPasswordHash(const std::string
&password
, const std::string
&password_server_id
, uint32_t password_game_seed
);
134 std::string_view
ParseCompanyFromConnectionString(const std::string
&connection_string
, CompanyID
*company_id
);
135 NetworkAddress
ParseConnectionString(const std::string
&connection_string
, uint16_t default_port
);
136 std::string
NormalizeConnectionString(const std::string
&connection_string
, uint16_t default_port
);
138 void ClientNetworkEmergencySave();
140 #endif /* NETWORK_INTERNAL_H */