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 */
40 typedef class ServerNetworkGameSocketHandler NetworkClientSocket
;
42 /** Status of the clients during joining. */
43 enum NetworkJoinStatus
{
44 NETWORK_JOIN_STATUS_CONNECTING
,
45 NETWORK_JOIN_STATUS_AUTHORIZING
,
46 NETWORK_JOIN_STATUS_WAITING
,
47 NETWORK_JOIN_STATUS_DOWNLOADING
,
48 NETWORK_JOIN_STATUS_PROCESSING
,
49 NETWORK_JOIN_STATUS_REGISTERING
,
51 NETWORK_JOIN_STATUS_GETTING_COMPANY_INFO
,
52 NETWORK_JOIN_STATUS_END
,
55 extern uint32_t _frame_counter_server
; // The frame_counter of the server, if in network-mode
56 extern uint32_t _frame_counter_max
; // To where we may go with our clients
57 extern uint32_t _frame_counter
;
59 extern uint32_t _last_sync_frame
; // Used in the server to store the last time a sync packet was sent to clients.
61 /* networking settings */
62 extern NetworkAddressList _broadcast_list
;
64 extern uint32_t _sync_seed_1
;
65 #ifdef NETWORK_SEND_DOUBLE_SEED
66 extern uint32_t _sync_seed_2
;
68 extern uint32_t _sync_frame
;
69 extern bool _network_first_time
;
70 /* Vars needed for the join-GUI */
71 extern NetworkJoinStatus _network_join_status
;
72 extern uint8_t _network_join_waiting
;
73 extern uint32_t _network_join_bytes
;
74 extern uint32_t _network_join_bytes_total
;
75 extern ConnectionType _network_server_connection_type
;
76 extern std::string _network_server_invite_code
;
78 /* Variable available for clients. */
79 extern std::string _network_server_name
;
81 extern uint8_t _network_reconnect
;
83 void NetworkQueryServer(const std::string
&connection_string
);
85 void GetBindAddresses(NetworkAddressList
*addresses
, uint16_t port
);
86 struct NetworkGameList
*NetworkAddServer(const std::string
&connection_string
, bool manually
= true, bool never_expire
= false);
87 void NetworkRebuildHostList();
88 void UpdateNetworkGameWindow();
90 /* From network_command.cpp */
92 * Everything we need to know about a command to be able to execute it.
94 struct CommandPacket
{
95 CommandPacket() : company(INVALID_COMPANY
), frame(0), my_cmd(false) {}
96 CompanyID company
; ///< company that is executing the command
97 uint32_t frame
; ///< the frame in which this packet is executed
98 bool my_cmd
; ///< did the command originate from "me"
100 Commands cmd
; ///< command being executed.
101 StringID err_msg
; ///< string ID of error message to use.
102 CommandCallback
*callback
; ///< any callback function executed upon successful completion of the command.
103 CommandDataBuffer data
; ///< command parameters.
106 void NetworkDistributeCommands();
107 void NetworkExecuteLocalCommandQueue();
108 void NetworkFreeLocalCommandQueue();
109 void NetworkSyncCommandQueue(NetworkClientSocket
*cs
);
110 void NetworkReplaceCommandClientId(CommandPacket
&cp
, ClientID client_id
);
112 void ShowNetworkError(StringID error_string
);
113 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
= "");
114 uint
NetworkCalculateLag(const NetworkClientSocket
*cs
);
115 StringID
GetNetworkErrorMsg(NetworkErrorCode err
);
116 bool NetworkMakeClientNameUnique(std::string
&new_name
);
118 std::string_view
ParseCompanyFromConnectionString(const std::string
&connection_string
, CompanyID
*company_id
);
119 NetworkAddress
ParseConnectionString(const std::string
&connection_string
, uint16_t default_port
);
120 std::string
NormalizeConnectionString(const std::string
&connection_string
, uint16_t default_port
);
122 void ClientNetworkEmergencySave();
124 #endif /* NETWORK_INTERNAL_H */