src/network/network_internal.h
branchNewGRF_ports
changeset 10991 d8811e327d12
parent 10724 68a692eacf22
equal deleted inserted replaced
10731:67db0d431d5e 10991:d8811e327d12
     5 #ifndef NETWORK_INTERNAL_H
     5 #ifndef NETWORK_INTERNAL_H
     6 #define NETWORK_INTERNAL_H
     6 #define NETWORK_INTERNAL_H
     7 
     7 
     8 #ifdef ENABLE_NETWORK
     8 #ifdef ENABLE_NETWORK
     9 
     9 
    10 #include "../player_type.h"
    10 #include "network.h"
    11 #include "../economy_type.h"
    11 #include "network_func.h"
       
    12 #include "core/os_abstraction.h"
       
    13 #include "core/core.h"
    12 #include "core/config.h"
    14 #include "core/config.h"
    13 #include "core/game.h"
    15 #include "core/packet.h"
       
    16 #include "core/tcp.h"
    14 
    17 
    15 /**
    18 /**
    16  * If this line is enable, every frame will have a sync test
    19  * If this line is enable, every frame will have a sync test
    17  *  this is not needed in normal games. Normal is like 1 sync in 100
    20  *  this is not needed in normal games. Normal is like 1 sync in 100
    18  *  frames. You can enable this if you have a lot of desyncs on a certain
    21  *  frames. You can enable this if you have a lot of desyncs on a certain
    27  * In theory sending 1 of the 2 seeds is enough to check for desyncs
    30  * In theory sending 1 of the 2 seeds is enough to check for desyncs
    28  *   so in theory, this next define can be left off.
    31  *   so in theory, this next define can be left off.
    29  */
    32  */
    30 //#define NETWORK_SEND_DOUBLE_SEED
    33 //#define NETWORK_SEND_DOUBLE_SEED
    31 
    34 
       
    35 #define MAX_TEXT_MSG_LEN 1024 /* long long long long sentences :-) */
    32 
    36 
    33 enum {
    37 enum MapPacket {
    34 	/**
    38 	MAP_PACKET_START,
    35 	 * How many clients can we have? Like.. MAX_PLAYERS - 1 is the amount of
    39 	MAP_PACKET_NORMAL,
    36 	 *  players that can really play.. so.. a max of 4 spectators.. gives us..
    40 	MAP_PACKET_END,
    37 	 *  MAX_PLAYERS + 3
       
    38 	 */
       
    39 	MAX_CLIENTS = MAX_PLAYERS + 3,
       
    40 
       
    41 	/** Do not change this next line. It should _ALWAYS_ be MAX_CLIENTS + 1 */
       
    42 	MAX_CLIENT_INFO = MAX_CLIENTS + 1,
       
    43 
       
    44 	/** Maximum number of internet interfaces supported. */
       
    45 	MAX_INTERFACES = 9,
       
    46 
       
    47 	/** How many vehicle/station types we put over the network */
       
    48 	NETWORK_VEHICLE_TYPES = 5,
       
    49 	NETWORK_STATION_TYPES = 5,
       
    50 };
    41 };
    51 
    42 
    52 struct NetworkPlayerInfo {
       
    53 	char company_name[NETWORK_NAME_LENGTH];         ///< Company name
       
    54 	char password[NETWORK_PASSWORD_LENGTH];         ///< The password for the player
       
    55 	Year inaugurated_year;                          ///< What year the company started in
       
    56 	Money company_value;                            ///< The company value
       
    57 	Money money;                                    ///< The amount of money the company has
       
    58 	Money income;                                   ///< How much did the company earned last year
       
    59 	uint16 performance;                             ///< What was his performance last month?
       
    60 	bool use_password;                              ///< Is there a password
       
    61 	uint16 num_vehicle[NETWORK_VEHICLE_TYPES];      ///< How many vehicles are there of this type?
       
    62 	uint16 num_station[NETWORK_STATION_TYPES];      ///< How many stations are there of this type?
       
    63 	char players[NETWORK_PLAYERS_LENGTH];           ///< The players that control this company (Name1, name2, ..)
       
    64 	uint16 months_empty;                            ///< How many months the company is empty
       
    65 };
       
    66 
       
    67 struct NetworkClientInfo {
       
    68 	uint16 client_index;                            ///< Index of the client (same as ClientState->index)
       
    69 	char client_name[NETWORK_CLIENT_NAME_LENGTH];   ///< Name of the client
       
    70 	byte client_lang;                               ///< The language of the client
       
    71 	PlayerID client_playas;                         ///< As which player is this client playing (PlayerID)
       
    72 	uint32 client_ip;                               ///< IP-address of the client (so he can be banned)
       
    73 	Date join_date;                                 ///< Gamedate the player has joined
       
    74 	char unique_id[NETWORK_UNIQUE_ID_LENGTH];       ///< Every play sends an unique id so we can indentify him
       
    75 };
       
    76 
    43 
    77 enum NetworkJoinStatus {
    44 enum NetworkJoinStatus {
    78 	NETWORK_JOIN_STATUS_CONNECTING,
    45 	NETWORK_JOIN_STATUS_CONNECTING,
    79 	NETWORK_JOIN_STATUS_AUTHORIZING,
    46 	NETWORK_JOIN_STATUS_AUTHORIZING,
    80 	NETWORK_JOIN_STATUS_WAITING,
    47 	NETWORK_JOIN_STATUS_WAITING,
   124 	NETLANG_GREEK,
    91 	NETLANG_GREEK,
   125 	NETLANG_LATVIAN,
    92 	NETLANG_LATVIAN,
   126 	NETLANG_COUNT
    93 	NETLANG_COUNT
   127 };
    94 };
   128 
    95 
   129 VARDEF NetworkGameInfo _network_game_info;
    96 extern NetworkPlayerInfo _network_player_info[MAX_PLAYERS];
   130 VARDEF NetworkPlayerInfo _network_player_info[MAX_PLAYERS];
       
   131 VARDEF NetworkClientInfo _network_client_info[MAX_CLIENT_INFO];
       
   132 
    97 
   133 VARDEF char _network_player_name[NETWORK_CLIENT_NAME_LENGTH];
    98 extern uint32 _frame_counter_server; // The frame_counter of the server, if in network-mode
   134 VARDEF char _network_default_ip[NETWORK_HOSTNAME_LENGTH];
    99 extern uint32 _frame_counter_max; // To where we may go with our clients
   135 
   100 
   136 VARDEF uint16 _network_own_client_index;
   101 extern uint32 _last_sync_frame; // Used in the server to store the last time a sync packet was sent to clients.
   137 VARDEF char _network_unique_id[NETWORK_UNIQUE_ID_LENGTH]; // Our own unique ID
       
   138 
       
   139 VARDEF uint32 _frame_counter_server; // The frame_counter of the server, if in network-mode
       
   140 VARDEF uint32 _frame_counter_max; // To where we may go with our clients
       
   141 
       
   142 VARDEF uint32 _last_sync_frame; // Used in the server to store the last time a sync packet was sent to clients.
       
   143 
   102 
   144 // networking settings
   103 // networking settings
   145 VARDEF uint32 _broadcast_list[MAX_INTERFACES + 1];
   104 extern uint32 _broadcast_list[MAX_INTERFACES + 1];
   146 
   105 
   147 VARDEF uint16 _network_server_port;
   106 extern uint32 _network_server_bind_ip;
   148 /* We use bind_ip and bind_ip_host, where bind_ip_host is the readable form of
       
   149     bind_ip_host, and bind_ip the numeric value, because we want a nice number
       
   150     in the openttd.cfg, but we wants to use the uint32 internally.. */
       
   151 VARDEF uint32 _network_server_bind_ip;
       
   152 VARDEF char _network_server_bind_ip_host[NETWORK_HOSTNAME_LENGTH];
       
   153 VARDEF bool _is_network_server; // Does this client wants to be a network-server?
       
   154 VARDEF char _network_server_name[NETWORK_NAME_LENGTH];
       
   155 VARDEF char _network_server_password[NETWORK_PASSWORD_LENGTH];
       
   156 VARDEF char _network_rcon_password[NETWORK_PASSWORD_LENGTH];
       
   157 VARDEF char _network_default_company_pass[NETWORK_PASSWORD_LENGTH];
       
   158 
   107 
   159 VARDEF uint16 _network_max_join_time;             ///< Time a client can max take to join
   108 extern uint32 _sync_seed_1, _sync_seed_2;
   160 VARDEF bool _network_pause_on_join;               ///< Pause the game when a client tries to join (more chance of succeeding join)
   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 uint16 _network_join_kbytes;
       
   115 extern uint16 _network_join_kbytes_total;
   161 
   116 
   162 VARDEF uint16 _redirect_console_to_client;
   117 extern uint32 _network_last_host_ip;
       
   118 extern uint8 _network_reconnect;
   163 
   119 
   164 VARDEF uint16 _network_sync_freq;
   120 extern bool _network_udp_server;
   165 VARDEF uint8 _network_frame_freq;
   121 extern uint16 _network_udp_broadcast;
   166 
   122 
   167 VARDEF uint32 _sync_seed_1, _sync_seed_2;
   123 extern uint8 _network_advertise_retries;
   168 VARDEF uint32 _sync_frame;
       
   169 VARDEF bool _network_first_time;
       
   170 // Vars needed for the join-GUI
       
   171 VARDEF NetworkJoinStatus _network_join_status;
       
   172 VARDEF uint8 _network_join_waiting;
       
   173 VARDEF uint16 _network_join_kbytes;
       
   174 VARDEF uint16 _network_join_kbytes_total;
       
   175 
   124 
   176 VARDEF char _network_last_host[NETWORK_HOSTNAME_LENGTH];
   125 // following externs are instantiated at network.cpp
   177 VARDEF short _network_last_port;
   126 extern CommandPacket *_local_command_queue;
   178 VARDEF uint32 _network_last_host_ip;
       
   179 VARDEF uint8 _network_reconnect;
       
   180 
   127 
   181 VARDEF bool _network_udp_server;
   128 // Here we keep track of the clients
   182 VARDEF uint16 _network_udp_broadcast;
   129 //  (and the client uses [0] for his own communication)
   183 
   130 extern NetworkTCPSocketHandler _clients[MAX_CLIENTS];
   184 VARDEF byte _network_lan_internet;
       
   185 
       
   186 VARDEF bool _network_need_advertise;
       
   187 VARDEF uint32 _network_last_advertise_frame;
       
   188 VARDEF uint8 _network_advertise_retries;
       
   189 
       
   190 VARDEF bool _network_autoclean_companies;
       
   191 VARDEF uint8 _network_autoclean_unprotected; // Remove a company after X months
       
   192 VARDEF uint8 _network_autoclean_protected;   // Unprotect a company after X months
       
   193 
       
   194 VARDEF Year _network_restart_game_year;      // If this year is reached, the server automaticly restarts
       
   195 VARDEF uint8 _network_min_players;           // Minimum number of players for game to unpause
       
   196 
   131 
   197 void NetworkTCPQueryServer(const char* host, unsigned short port);
   132 void NetworkTCPQueryServer(const char* host, unsigned short port);
   198 
   133 
   199 byte NetworkSpectatorCount();
       
   200 
       
   201 VARDEF char *_network_host_list[10];
       
   202 VARDEF char *_network_ban_list[25];
       
   203 
       
   204 void ParseConnectionString(const char **player, const char **port, char *connection_string);
       
   205 void NetworkUpdateClientInfo(uint16 client_index);
       
   206 void NetworkAddServer(const char *b);
   134 void NetworkAddServer(const char *b);
   207 void NetworkRebuildHostList();
   135 void NetworkRebuildHostList();
   208 bool NetworkChangeCompanyPassword(byte argc, char *argv[]);
       
   209 void NetworkPopulateCompanyInfo();
       
   210 void UpdateNetworkGameWindow(bool unselect);
   136 void UpdateNetworkGameWindow(bool unselect);
   211 void CheckMinPlayers();
       
   212 void NetworkStartDebugLog(const char *hostname, uint16 port);
       
   213 
       
   214 void NetworkUDPCloseAll();
       
   215 void NetworkGameLoop();
       
   216 void NetworkUDPGameLoop();
       
   217 bool NetworkServerStart();
       
   218 bool NetworkClientConnectGame(const char *host, uint16 port);
       
   219 void NetworkReboot();
       
   220 void NetworkDisconnect();
       
   221 
   137 
   222 bool IsNetworkCompatibleVersion(const char *version);
   138 bool IsNetworkCompatibleVersion(const char *version);
   223 
   139 
       
   140 void NetworkExecuteCommand(CommandPacket *cp);
       
   141 void NetworkAddCommandQueue(NetworkTCPSocketHandler *cs, CommandPacket *cp);
       
   142 
       
   143 // from network.c
       
   144 void NetworkCloseClient(NetworkTCPSocketHandler *cs);
       
   145 void CDECL NetworkTextMessage(NetworkAction action, ConsoleColour color, bool self_send, const char *name, const char *str, ...);
       
   146 void NetworkGetClientName(char *clientname, size_t size, const NetworkTCPSocketHandler *cs);
       
   147 uint NetworkCalculateLag(const NetworkTCPSocketHandler *cs);
       
   148 byte NetworkGetCurrentLanguageIndex();
       
   149 NetworkTCPSocketHandler *NetworkFindClientStateFromIndex(uint16 client_index);
       
   150 unsigned long NetworkResolveHost(const char *hostname);
       
   151 char* GetNetworkErrorMsg(char* buf, NetworkErrorCode err, const char* last);
       
   152 bool NetworkFindName(char new_name[NETWORK_CLIENT_NAME_LENGTH]);
       
   153 
       
   154 #define DEREF_CLIENT(i) (&_clients[i])
       
   155 // This returns the NetworkClientInfo from a NetworkClientState
       
   156 #define DEREF_CLIENT_INFO(cs) (&_network_client_info[cs - _clients])
       
   157 
       
   158 // Macros to make life a bit more easier
       
   159 #define DEF_CLIENT_RECEIVE_COMMAND(type) NetworkRecvStatus NetworkPacketReceive_ ## type ## _command(Packet *p)
       
   160 #define DEF_CLIENT_SEND_COMMAND(type) void NetworkPacketSend_ ## type ## _command()
       
   161 #define DEF_CLIENT_SEND_COMMAND_PARAM(type) void NetworkPacketSend_ ## type ## _command
       
   162 #define DEF_SERVER_RECEIVE_COMMAND(type) void NetworkPacketReceive_ ## type ## _command(NetworkTCPSocketHandler *cs, Packet *p)
       
   163 #define DEF_SERVER_SEND_COMMAND(type) void NetworkPacketSend_ ## type ## _command(NetworkTCPSocketHandler *cs)
       
   164 #define DEF_SERVER_SEND_COMMAND_PARAM(type) void NetworkPacketSend_ ## type ## _command
       
   165 
       
   166 #define SEND_COMMAND(type) NetworkPacketSend_ ## type ## _command
       
   167 #define RECEIVE_COMMAND(type) NetworkPacketReceive_ ## type ## _command
       
   168 
       
   169 #define FOR_ALL_CLIENTS(cs) for (cs = _clients; cs != endof(_clients) && cs->IsConnected(); cs++)
       
   170 
   224 #endif /* ENABLE_NETWORK */
   171 #endif /* ENABLE_NETWORK */
   225 #endif /* NETWORK_INTERNAL_H */
   172 #endif /* NETWORK_INTERNAL_H */