1
/* $Id: network_internal.h 11840 2008-01-13 21:51:53Z rubidium $ */
1
/* $Id: network_internal.h 15718 2009-03-15 00:32:18Z rubidium $ */
3
/** @file network_internal.h Variables and function used internally. */
3
5
#ifndef NETWORK_INTERNAL_H
4
6
#define NETWORK_INTERNAL_H
6
8
#ifdef ENABLE_NETWORK
8
#include "../player_type.h"
9
#include "../economy_type.h"
11
#include "network_func.h"
12
#include "network_base.h"
13
#include "core/os_abstraction.h"
14
#include "core/core.h"
10
15
#include "core/config.h"
11
#include "core/game.h"
13
// If this line is enable, every frame will have a sync test
14
// this is not needed in normal games. Normal is like 1 sync in 100
15
// frames. You can enable this if you have a lot of desyncs on a certain
17
// Remember: both client and server have to be compiled with this
18
// option enabled to make it to work. If one of the two has it disabled
19
// nothing will happen.
16
#include "core/packet.h"
17
#include "core/tcp_game.h"
19
#include "../command_type.h"
22
* If this line is enable, every frame will have a sync test
23
* this is not needed in normal games. Normal is like 1 sync in 100
24
* frames. You can enable this if you have a lot of desyncs on a certain
26
* Remember: both client and server have to be compiled with this
27
* option enabled to make it to work. If one of the two has it disabled
28
* nothing will happen.
20
30
//#define ENABLE_NETWORK_SYNC_EVERY_FRAME
22
// In theory sending 1 of the 2 seeds is enough to check for desyncs
23
// so in theory, this next define can be left off.
33
* In theory sending 1 of the 2 seeds is enough to check for desyncs
34
* so in theory, this next define can be left off.
24
36
//#define NETWORK_SEND_DOUBLE_SEED
26
// How many clients can we have? Like.. MAX_PLAYERS - 1 is the amount of
27
// players that can really play.. so.. a max of 4 spectators.. gives us..
29
#define MAX_CLIENTS (MAX_PLAYERS + 3)
32
// Do not change this next line. It should _ALWAYS_ be MAX_CLIENTS + 1
33
#define MAX_CLIENT_INFO (MAX_CLIENTS + 1)
35
#define MAX_INTERFACES 9
38
// How many vehicle/station types we put over the network
39
#define NETWORK_VEHICLE_TYPES 5
40
#define NETWORK_STATION_TYPES 5
42
struct NetworkPlayerInfo {
43
char company_name[NETWORK_NAME_LENGTH]; // Company name
44
char password[NETWORK_PASSWORD_LENGTH]; // The password for the player
45
Year inaugurated_year; // What year the company started in
46
Money company_value; // The company value
47
Money money; // The amount of money the company has
48
Money income; // How much did the company earned last year
49
uint16 performance; // What was his performance last month?
50
bool use_password; // Is there a password
51
uint16 num_vehicle[NETWORK_VEHICLE_TYPES]; // How many vehicles are there of this type?
52
uint16 num_station[NETWORK_STATION_TYPES]; // How many stations are there of this type?
53
char players[NETWORK_PLAYERS_LENGTH]; // The players that control this company (Name1, name2, ..)
54
uint16 months_empty; // How many months the company is empty
57
struct NetworkClientInfo {
58
uint16 client_index; // Index of the client (same as ClientState->index)
59
char client_name[NETWORK_CLIENT_NAME_LENGTH]; // Name of the client
60
byte client_lang; // The language of the client
61
PlayerID client_playas; // As which player is this client playing (PlayerID)
62
uint32 client_ip; // IP-address of the client (so he can be banned)
63
Date join_date; // Gamedate the player has joined
64
char unique_id[NETWORK_UNIQUE_ID_LENGTH]; // Every play sends an unique id so we can indentify him
67
45
enum NetworkJoinStatus {
68
46
NETWORK_JOIN_STATUS_CONNECTING,
108
86
NETLANG_UKRAINIAN,
112
VARDEF NetworkGameInfo _network_game_info;
113
VARDEF NetworkPlayerInfo _network_player_info[MAX_PLAYERS];
114
VARDEF NetworkClientInfo _network_client_info[MAX_CLIENT_INFO];
116
VARDEF char _network_player_name[NETWORK_CLIENT_NAME_LENGTH];
117
VARDEF char _network_default_ip[NETWORK_HOSTNAME_LENGTH];
119
VARDEF uint16 _network_own_client_index;
120
VARDEF char _network_unique_id[NETWORK_UNIQUE_ID_LENGTH]; // Our own unique ID
122
VARDEF uint32 _frame_counter_server; // The frame_counter of the server, if in network-mode
123
VARDEF uint32 _frame_counter_max; // To where we may go with our clients
125
VARDEF uint32 _last_sync_frame; // Used in the server to store the last time a sync packet was sent to clients.
127
// networking settings
128
VARDEF uint32 _broadcast_list[MAX_INTERFACES + 1];
130
VARDEF uint16 _network_server_port;
131
/* We use bind_ip and bind_ip_host, where bind_ip_host is the readable form of
132
bind_ip_host, and bind_ip the numeric value, because we want a nice number
133
in the openttd.cfg, but we wants to use the uint32 internally.. */
134
VARDEF uint32 _network_server_bind_ip;
135
VARDEF char _network_server_bind_ip_host[NETWORK_HOSTNAME_LENGTH];
136
VARDEF bool _is_network_server; // Does this client wants to be a network-server?
137
VARDEF char _network_server_name[NETWORK_NAME_LENGTH];
138
VARDEF char _network_server_password[NETWORK_PASSWORD_LENGTH];
139
VARDEF char _network_rcon_password[NETWORK_PASSWORD_LENGTH];
140
VARDEF char _network_default_company_pass[NETWORK_PASSWORD_LENGTH];
142
VARDEF uint16 _network_max_join_time; ///< Time a client can max take to join
143
VARDEF bool _network_pause_on_join; ///< Pause the game when a client tries to join (more chance of succeeding join)
145
VARDEF uint16 _redirect_console_to_client;
147
VARDEF uint16 _network_sync_freq;
148
VARDEF uint8 _network_frame_freq;
150
VARDEF uint32 _sync_seed_1, _sync_seed_2;
151
VARDEF uint32 _sync_frame;
152
VARDEF bool _network_first_time;
153
// Vars needed for the join-GUI
154
VARDEF NetworkJoinStatus _network_join_status;
155
VARDEF uint8 _network_join_waiting;
156
VARDEF uint16 _network_join_kbytes;
157
VARDEF uint16 _network_join_kbytes_total;
159
VARDEF char _network_last_host[NETWORK_HOSTNAME_LENGTH];
160
VARDEF short _network_last_port;
161
VARDEF uint32 _network_last_host_ip;
162
VARDEF uint8 _network_reconnect;
164
VARDEF bool _network_udp_server;
165
VARDEF uint16 _network_udp_broadcast;
167
VARDEF byte _network_lan_internet;
169
VARDEF bool _network_need_advertise;
170
VARDEF uint32 _network_last_advertise_frame;
171
VARDEF uint8 _network_advertise_retries;
173
VARDEF bool _network_autoclean_companies;
174
VARDEF uint8 _network_autoclean_unprotected; // Remove a company after X months
175
VARDEF uint8 _network_autoclean_protected; // Unprotect a company after X months
177
VARDEF Year _network_restart_game_year; // If this year is reached, the server automaticly restarts
178
VARDEF uint8 _network_min_players; // Minimum number of players for game to unpause
180
void NetworkTCPQueryServer(const char* host, unsigned short port);
182
byte NetworkSpectatorCount();
184
VARDEF char *_network_host_list[10];
185
VARDEF char *_network_ban_list[25];
187
void ParseConnectionString(const char **player, const char **port, char *connection_string);
188
void NetworkUpdateClientInfo(uint16 client_index);
97
extern uint32 _frame_counter_server; // The frame_counter of the server, if in network-mode
98
extern uint32 _frame_counter_max; // To where we may go with our clients
99
extern uint32 _frame_counter;
101
extern uint32 _last_sync_frame; // Used in the server to store the last time a sync packet was sent to clients.
103
/* networking settings */
104
extern uint32 _broadcast_list[MAX_INTERFACES + 1];
106
extern uint32 _network_server_bind_ip;
108
extern uint32 _sync_seed_1, _sync_seed_2;
109
extern uint32 _sync_frame;
110
extern bool _network_first_time;
111
/* Vars needed for the join-GUI */
112
extern NetworkJoinStatus _network_join_status;
113
extern uint8 _network_join_waiting;
114
extern uint32 _network_join_bytes;
115
extern uint32 _network_join_bytes_total;
117
extern uint8 _network_reconnect;
119
extern bool _network_udp_server;
120
extern uint16 _network_udp_broadcast;
122
extern uint8 _network_advertise_retries;
124
extern CompanyMask _network_company_passworded;
126
void NetworkTCPQueryServer(NetworkAddress address);
189
128
void NetworkAddServer(const char *b);
190
129
void NetworkRebuildHostList();
191
bool NetworkChangeCompanyPassword(byte argc, char *argv[]);
192
void NetworkPopulateCompanyInfo();
193
130
void UpdateNetworkGameWindow(bool unselect);
194
void CheckMinPlayers();
195
void NetworkStartDebugLog(const char *hostname, uint16 port);
197
void NetworkUDPCloseAll();
198
void NetworkGameLoop();
199
void NetworkUDPGameLoop();
200
bool NetworkServerStart();
201
bool NetworkClientConnectGame(const char *host, uint16 port);
202
void NetworkReboot();
203
void NetworkDisconnect();
205
132
bool IsNetworkCompatibleVersion(const char *version);
134
/* From network_command.cpp */
136
* Everything we need to know about a command to be able to execute it.
138
struct CommandPacket : CommandContainer {
139
CommandPacket *next; ///< the next command packet (if in queue)
140
CompanyByte company; ///< company that is executing the command
141
uint32 frame; ///< the frame in which this packet is executed
142
bool my_cmd; ///< did the command originate from "me"
145
void NetworkAddCommandQueue(CommandPacket cp, NetworkClientSocket *cs = NULL);
146
void NetworkExecuteLocalCommandQueue();
147
void NetworkFreeLocalCommandQueue();
150
void NetworkCloseClient(NetworkClientSocket *cs);
151
void NetworkTextMessage(NetworkAction action, ConsoleColour colour, bool self_send, const char *name, const char *str = "", int64 data = 0);
152
void NetworkGetClientName(char *clientname, size_t size, const NetworkClientSocket *cs);
153
uint NetworkCalculateLag(const NetworkClientSocket *cs);
154
byte NetworkGetCurrentLanguageIndex();
155
NetworkClientSocket *NetworkFindClientStateFromClientID(ClientID client_id);
156
StringID GetNetworkErrorMsg(NetworkErrorCode err);
157
bool NetworkFindName(char new_name[NETWORK_CLIENT_NAME_LENGTH]);
159
/* Macros to make life a bit more easier */
160
#define DEF_CLIENT_RECEIVE_COMMAND(type) NetworkRecvStatus NetworkPacketReceive_ ## type ## _command(Packet *p)
161
#define DEF_CLIENT_SEND_COMMAND(type) void NetworkPacketSend_ ## type ## _command()
162
#define DEF_CLIENT_SEND_COMMAND_PARAM(type) void NetworkPacketSend_ ## type ## _command
163
#define DEF_SERVER_RECEIVE_COMMAND(type) void NetworkPacketReceive_ ## type ## _command(NetworkClientSocket *cs, Packet *p)
164
#define DEF_SERVER_SEND_COMMAND(type) void NetworkPacketSend_ ## type ## _command(NetworkClientSocket *cs)
165
#define DEF_SERVER_SEND_COMMAND_PARAM(type) void NetworkPacketSend_ ## type ## _command
167
#define SEND_COMMAND(type) NetworkPacketSend_ ## type ## _command
168
#define RECEIVE_COMMAND(type) NetworkPacketReceive_ ## type ## _command
207
170
#endif /* ENABLE_NETWORK */
208
171
#endif /* NETWORK_INTERNAL_H */