2005-07-24 16:12:37 +02:00
|
|
|
/* $Id$ */
|
|
|
|
|
2007-01-02 18:34:03 +01:00
|
|
|
#include "../stdafx.h"
|
2004-12-04 18:54:56 +01:00
|
|
|
#include "network_data.h"
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-15 17:51:55 +01:00
|
|
|
#if defined(WITH_REV)
|
|
|
|
extern const char _openttd_revision[];
|
|
|
|
#elif defined(WITH_REV_HACK)
|
|
|
|
#define WITH_REV
|
2007-01-10 19:56:51 +01:00
|
|
|
extern const char _openttd_revision[] = WITH_REV_HACK;
|
2004-12-15 17:51:55 +01:00
|
|
|
#else
|
2007-01-10 19:56:51 +01:00
|
|
|
extern const char _openttd_revision[] = NOREV_STRING;
|
2004-12-15 17:51:55 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
#ifdef ENABLE_NETWORK
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2007-01-02 18:34:03 +01:00
|
|
|
#include "../openttd.h"
|
|
|
|
#include "../debug.h"
|
|
|
|
#include "../functions.h"
|
|
|
|
#include "../string.h"
|
|
|
|
#include "../strings.h"
|
|
|
|
#include "../map.h"
|
|
|
|
#include "../command.h"
|
|
|
|
#include "../variables.h"
|
|
|
|
#include "../date.h"
|
|
|
|
#include "../newgrf_config.h"
|
2007-01-02 20:19:48 +01:00
|
|
|
#include "table/strings.h"
|
2004-12-04 18:54:56 +01:00
|
|
|
#include "network_client.h"
|
|
|
|
#include "network_server.h"
|
|
|
|
#include "network_udp.h"
|
|
|
|
#include "network_gamelist.h"
|
2007-01-02 18:34:03 +01:00
|
|
|
#include "core/udp.h"
|
|
|
|
#include "core/tcp.h"
|
2007-01-04 19:50:40 +01:00
|
|
|
#include "core/core.h"
|
2006-12-30 02:52:09 +01:00
|
|
|
#include "network_gui.h"
|
2007-01-02 18:34:03 +01:00
|
|
|
#include "../console.h" /* IConsoleCmdExec */
|
2004-12-04 18:54:56 +01:00
|
|
|
#include <stdarg.h> /* va_list */
|
2007-01-02 18:34:03 +01:00
|
|
|
#include "../md5.h"
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2007-01-23 15:47:38 +01:00
|
|
|
/* Check whether NETWORK_NUM_LANDSCAPES is still in sync with NUM_LANDSCAPE */
|
|
|
|
assert_compile((int)NETWORK_NUM_LANDSCAPES == (int)NUM_LANDSCAPE);
|
|
|
|
|
2007-01-10 19:56:51 +01:00
|
|
|
// global variables (declared in network_data.h)
|
|
|
|
CommandPacket *_local_command_queue;
|
|
|
|
|
2007-01-12 15:30:01 +01:00
|
|
|
extern NetworkUDPSocketHandler *_udp_client_socket; ///< udp client socket
|
|
|
|
extern NetworkUDPSocketHandler *_udp_server_socket; ///< udp server socket
|
|
|
|
extern NetworkUDPSocketHandler *_udp_master_socket; ///< udp master socket
|
2007-01-10 19:56:51 +01:00
|
|
|
|
|
|
|
// Here we keep track of the clients
|
|
|
|
// (and the client uses [0] for his own communication)
|
2007-01-12 21:19:49 +01:00
|
|
|
NetworkTCPSocketHandler _clients[MAX_CLIENTS];
|
2007-01-10 19:56:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// The listen socket for the server
|
|
|
|
static SOCKET _listensocket;
|
2004-08-27 13:01:26 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// The amount of clients connected
|
|
|
|
static byte _network_clients_connected = 0;
|
|
|
|
// The index counter for new clients (is never decreased)
|
|
|
|
static uint16 _network_client_index = NETWORK_SERVER_INDEX + 1;
|
2004-08-27 13:01:26 +02:00
|
|
|
|
2004-12-19 11:17:26 +01:00
|
|
|
/* Some externs / forwards */
|
2005-01-22 21:23:18 +01:00
|
|
|
extern void StateGameLoop(void);
|
2004-12-19 11:17:26 +01:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// Function that looks up the CI for a given client-index
|
|
|
|
NetworkClientInfo *NetworkFindClientInfoFromIndex(uint16 client_index)
|
2004-09-10 21:02:27 +02:00
|
|
|
{
|
2004-12-04 18:54:56 +01:00
|
|
|
NetworkClientInfo *ci;
|
2004-08-27 13:01:26 +02:00
|
|
|
|
2006-10-18 15:17:46 +02:00
|
|
|
for (ci = _network_client_info; ci != endof(_network_client_info); ci++) {
|
2006-06-27 23:25:53 +02:00
|
|
|
if (ci->client_index == client_index) return ci;
|
|
|
|
}
|
2004-08-27 13:01:26 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
return NULL;
|
2004-08-22 12:23:37 +02:00
|
|
|
}
|
|
|
|
|
2006-01-19 16:58:57 +01:00
|
|
|
/** Return the CI for a given IP
|
|
|
|
* @param ip IP of the client we are looking for. This must be in string-format
|
|
|
|
* @return return a pointer to the corresponding NetworkClientInfo struct or NULL on failure */
|
|
|
|
NetworkClientInfo *NetworkFindClientInfoFromIP(const char *ip)
|
|
|
|
{
|
|
|
|
NetworkClientInfo *ci;
|
|
|
|
uint32 ip_number = inet_addr(ip);
|
|
|
|
|
2006-10-18 15:17:46 +02:00
|
|
|
for (ci = _network_client_info; ci != endof(_network_client_info); ci++) {
|
2006-01-19 16:58:57 +01:00
|
|
|
if (ci->client_ip == ip_number) return ci;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// Function that looks up the CS for a given client-index
|
2007-01-12 21:19:49 +01:00
|
|
|
NetworkTCPSocketHandler *NetworkFindClientStateFromIndex(uint16 client_index)
|
2004-12-04 18:54:56 +01:00
|
|
|
{
|
2007-01-12 21:19:49 +01:00
|
|
|
NetworkTCPSocketHandler *cs;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2007-01-08 20:18:53 +01:00
|
|
|
for (cs = _clients; cs != endof(_clients); cs++) {
|
2006-06-27 23:25:53 +02:00
|
|
|
if (cs->index == client_index) return cs;
|
|
|
|
}
|
2004-09-07 00:46:02 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
return NULL;
|
2004-09-07 00:46:02 +02:00
|
|
|
}
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// NetworkGetClientName is a server-safe function to get the name of the client
|
|
|
|
// if the user did not send it yet, Client #<no> is used.
|
2007-01-12 21:19:49 +01:00
|
|
|
void NetworkGetClientName(char *client_name, size_t size, const NetworkTCPSocketHandler *cs)
|
2004-09-08 21:20:46 +02:00
|
|
|
{
|
2006-04-22 11:46:31 +02:00
|
|
|
const NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs);
|
2006-06-27 23:25:53 +02:00
|
|
|
|
|
|
|
if (ci->client_name[0] == '\0') {
|
2006-04-22 11:46:31 +02:00
|
|
|
snprintf(client_name, size, "Client #%4d", cs->index);
|
2006-06-27 23:25:53 +02:00
|
|
|
} else {
|
2006-04-22 11:46:31 +02:00
|
|
|
ttd_strlcpy(client_name, ci->client_name, size);
|
2006-06-27 23:25:53 +02:00
|
|
|
}
|
2004-09-07 00:46:02 +02:00
|
|
|
}
|
2004-09-12 16:12:33 +02:00
|
|
|
|
2006-01-31 23:16:15 +01:00
|
|
|
byte NetworkSpectatorCount(void)
|
|
|
|
{
|
2007-01-12 21:19:49 +01:00
|
|
|
NetworkTCPSocketHandler *cs;
|
2006-01-31 23:16:15 +01:00
|
|
|
byte count = 0;
|
|
|
|
|
|
|
|
FOR_ALL_CLIENTS(cs) {
|
2006-10-14 17:49:43 +02:00
|
|
|
if (DEREF_CLIENT_INFO(cs)->client_playas == PLAYER_SPECTATOR) count++;
|
2006-01-31 23:16:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// This puts a text-message to the console, or in the future, the chat-box,
|
|
|
|
// (to keep it all a bit more general)
|
2004-12-19 16:14:55 +01:00
|
|
|
// If 'self_send' is true, this is the client who is sending the message
|
|
|
|
void CDECL NetworkTextMessage(NetworkAction action, uint16 color, bool self_send, const char *name, const char *str, ...)
|
2004-09-08 21:20:46 +02:00
|
|
|
{
|
2004-12-04 18:54:56 +01:00
|
|
|
char buf[1024];
|
|
|
|
va_list va;
|
|
|
|
const int duration = 10; // Game days the messages stay visible
|
2004-12-19 16:14:55 +01:00
|
|
|
char message[1024];
|
|
|
|
char temp[1024];
|
2004-09-07 00:46:02 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
va_start(va, str);
|
2005-08-28 14:24:57 +02:00
|
|
|
vsnprintf(buf, lengthof(buf), str, va);
|
2004-12-04 18:54:56 +01:00
|
|
|
va_end(va);
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
switch (action) {
|
2006-10-25 00:23:08 +02:00
|
|
|
case NETWORK_ACTION_SERVER_MESSAGE:
|
|
|
|
color = 1;
|
|
|
|
snprintf(message, sizeof(message), "*** %s", buf);
|
|
|
|
break;
|
2004-12-19 16:14:55 +01:00
|
|
|
case NETWORK_ACTION_JOIN:
|
2006-10-25 00:23:08 +02:00
|
|
|
color = 1;
|
2006-10-22 01:31:34 +02:00
|
|
|
GetString(temp, STR_NETWORK_CLIENT_JOINED, lastof(temp));
|
2004-12-19 16:14:55 +01:00
|
|
|
snprintf(message, sizeof(message), "*** %s %s", name, temp);
|
|
|
|
break;
|
|
|
|
case NETWORK_ACTION_LEAVE:
|
2006-10-25 00:23:08 +02:00
|
|
|
color = 1;
|
2006-10-22 01:31:34 +02:00
|
|
|
GetString(temp, STR_NETWORK_ERR_LEFT, lastof(temp));
|
2004-12-19 16:14:55 +01:00
|
|
|
snprintf(message, sizeof(message), "*** %s %s (%s)", name, temp, buf);
|
2004-12-04 18:54:56 +01:00
|
|
|
break;
|
|
|
|
case NETWORK_ACTION_GIVE_MONEY:
|
2004-12-19 16:14:55 +01:00
|
|
|
if (self_send) {
|
2005-07-14 11:43:59 +02:00
|
|
|
SetDParamStr(0, name);
|
2004-12-19 16:14:55 +01:00
|
|
|
SetDParam(1, atoi(buf));
|
2006-10-22 01:31:34 +02:00
|
|
|
GetString(temp, STR_NETWORK_GAVE_MONEY_AWAY, lastof(temp));
|
2004-12-19 16:14:55 +01:00
|
|
|
snprintf(message, sizeof(message), "*** %s", temp);
|
|
|
|
} else {
|
|
|
|
SetDParam(0, atoi(buf));
|
2006-10-22 01:31:34 +02:00
|
|
|
GetString(temp, STR_NETWORK_GIVE_MONEY, lastof(temp));
|
2004-12-19 16:14:55 +01:00
|
|
|
snprintf(message, sizeof(message), "*** %s %s", name, temp);
|
|
|
|
}
|
2004-12-04 18:54:56 +01:00
|
|
|
break;
|
|
|
|
case NETWORK_ACTION_NAME_CHANGE:
|
2006-10-22 01:31:34 +02:00
|
|
|
GetString(temp, STR_NETWORK_NAME_CHANGE, lastof(temp));
|
2004-12-19 16:14:55 +01:00
|
|
|
snprintf(message, sizeof(message), "*** %s %s %s", name, temp, buf);
|
2004-12-04 18:54:56 +01:00
|
|
|
break;
|
2006-10-25 00:19:12 +02:00
|
|
|
case NETWORK_ACTION_CHAT_COMPANY:
|
|
|
|
SetDParamStr(0, name);
|
|
|
|
SetDParamStr(1, buf);
|
|
|
|
GetString(temp, self_send ? STR_NETWORK_CHAT_TO_COMPANY : STR_NETWORK_CHAT_COMPANY, lastof(temp));
|
|
|
|
ttd_strlcpy(message, temp, sizeof(message));
|
|
|
|
break;
|
|
|
|
case NETWORK_ACTION_CHAT_CLIENT:
|
|
|
|
SetDParamStr(0, name);
|
|
|
|
SetDParamStr(1, buf);
|
|
|
|
GetString(temp, self_send ? STR_NETWORK_CHAT_TO_CLIENT : STR_NETWORK_CHAT_CLIENT, lastof(temp));
|
|
|
|
ttd_strlcpy(message, temp, sizeof(message));
|
|
|
|
break;
|
2004-12-04 18:54:56 +01:00
|
|
|
default:
|
2005-07-14 11:43:59 +02:00
|
|
|
SetDParamStr(0, name);
|
2006-10-25 00:19:12 +02:00
|
|
|
SetDParamStr(1, buf);
|
2006-10-22 01:31:34 +02:00
|
|
|
GetString(temp, STR_NETWORK_CHAT_ALL, lastof(temp));
|
2006-10-25 00:19:12 +02:00
|
|
|
ttd_strlcpy(message, temp, sizeof(message));
|
2004-12-04 18:54:56 +01:00
|
|
|
break;
|
|
|
|
}
|
2004-12-19 16:14:55 +01:00
|
|
|
|
2004-12-20 16:25:39 +01:00
|
|
|
IConsolePrintF(color, "%s", message);
|
|
|
|
AddTextMessage(color, duration, "%s", message);
|
2004-09-11 21:34:11 +02:00
|
|
|
}
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// Calculate the frame-lag of a client
|
2007-01-12 21:19:49 +01:00
|
|
|
uint NetworkCalculateLag(const NetworkTCPSocketHandler *cs)
|
2004-09-12 00:10:31 +02:00
|
|
|
{
|
2004-12-04 18:54:56 +01:00
|
|
|
int lag = cs->last_frame_server - cs->last_frame;
|
|
|
|
// This client has missed his ACK packet after 1 DAY_TICKS..
|
|
|
|
// so we increase his lag for every frame that passes!
|
|
|
|
// The packet can be out by a max of _net_frame_freq
|
|
|
|
if (cs->last_frame_server + DAY_TICKS + _network_frame_freq < _frame_counter)
|
|
|
|
lag += _frame_counter - (cs->last_frame_server + DAY_TICKS + _network_frame_freq);
|
2004-09-12 00:10:31 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
return lag;
|
2004-09-12 00:10:31 +02:00
|
|
|
}
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
|
|
|
|
// There was a non-recoverable error, drop back to the main menu with a nice
|
|
|
|
// error
|
2005-01-22 23:47:58 +01:00
|
|
|
static void NetworkError(StringID error_string)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2004-12-04 18:54:56 +01:00
|
|
|
_switch_mode = SM_MENU;
|
|
|
|
_switch_mode_errorstr = error_string;
|
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-01-14 20:44:42 +01:00
|
|
|
static void ClientStartError(const char *error)
|
|
|
|
{
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(net, 0, "[client] could not start network: %s",error);
|
2004-12-04 18:54:56 +01:00
|
|
|
NetworkError(STR_NETWORK_ERR_CLIENT_START);
|
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-01-14 20:44:42 +01:00
|
|
|
static void ServerStartError(const char *error)
|
|
|
|
{
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(net, 0, "[server] could not start network: %s",error);
|
2004-12-04 18:54:56 +01:00
|
|
|
NetworkError(STR_NETWORK_ERR_SERVER_START);
|
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2007-01-12 21:19:49 +01:00
|
|
|
static void NetworkClientError(NetworkRecvStatus res, NetworkTCPSocketHandler* cs)
|
2006-06-27 23:25:53 +02:00
|
|
|
{
|
2004-12-04 18:54:56 +01:00
|
|
|
// First, send a CLIENT_ERROR to the server, so he knows we are
|
|
|
|
// disconnection (and why!)
|
|
|
|
NetworkErrorCode errorno;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// We just want to close the connection..
|
|
|
|
if (res == NETWORK_RECV_STATUS_CLOSE_QUERY) {
|
2006-10-18 01:34:12 +02:00
|
|
|
cs->has_quit = true;
|
2004-12-19 11:17:26 +01:00
|
|
|
NetworkCloseClient(cs);
|
2004-12-04 18:54:56 +01:00
|
|
|
_networking = false;
|
2004-09-08 21:20:46 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
|
|
|
|
return;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2006-02-01 08:36:15 +01:00
|
|
|
switch (res) {
|
2007-01-30 18:22:56 +01:00
|
|
|
case NETWORK_RECV_STATUS_DESYNC: errorno = NETWORK_ERROR_DESYNC; break;
|
|
|
|
case NETWORK_RECV_STATUS_SAVEGAME: errorno = NETWORK_ERROR_SAVEGAME_FAILED; break;
|
|
|
|
case NETWORK_RECV_STATUS_NEWGRF_MISMATCH: errorno = NETWORK_ERROR_NEWGRF_MISMATCH; break;
|
|
|
|
default: errorno = NETWORK_ERROR_GENERAL; break;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2004-12-04 18:54:56 +01:00
|
|
|
// This means we fucked up and the server closed the connection
|
2005-01-02 13:03:43 +01:00
|
|
|
if (res != NETWORK_RECV_STATUS_SERVER_ERROR && res != NETWORK_RECV_STATUS_SERVER_FULL &&
|
|
|
|
res != NETWORK_RECV_STATUS_SERVER_BANNED) {
|
2004-12-04 18:54:56 +01:00
|
|
|
SEND_COMMAND(PACKET_CLIENT_ERROR)(errorno);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// Dequeue all commands before closing the socket
|
2007-02-02 00:50:15 +01:00
|
|
|
DEREF_CLIENT(0)->Send_Packets();
|
2004-12-04 18:54:56 +01:00
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
_switch_mode = SM_MENU;
|
2004-12-19 11:17:26 +01:00
|
|
|
NetworkCloseClient(cs);
|
2004-12-04 18:54:56 +01:00
|
|
|
_networking = false;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2006-04-03 20:31:01 +02:00
|
|
|
/** Retrieve a string representation of an internal error number
|
|
|
|
* @param buf buffer where the error message will be stored
|
2006-06-27 23:25:53 +02:00
|
|
|
* @param err NetworkErrorCode
|
2006-04-03 20:31:01 +02:00
|
|
|
* @return returns a pointer to the error message (buf) */
|
2006-10-22 01:31:34 +02:00
|
|
|
char* GetNetworkErrorMsg(char* buf, NetworkErrorCode err, const char* last)
|
2006-04-03 20:31:01 +02:00
|
|
|
{
|
|
|
|
/* List of possible network errors, used by
|
|
|
|
* PACKET_SERVER_ERROR and PACKET_CLIENT_ERROR */
|
|
|
|
static const StringID network_error_strings[] = {
|
|
|
|
STR_NETWORK_ERR_CLIENT_GENERAL,
|
|
|
|
STR_NETWORK_ERR_CLIENT_DESYNC,
|
|
|
|
STR_NETWORK_ERR_CLIENT_SAVEGAME,
|
|
|
|
STR_NETWORK_ERR_CLIENT_CONNECTION_LOST,
|
|
|
|
STR_NETWORK_ERR_CLIENT_PROTOCOL_ERROR,
|
2007-01-30 18:22:56 +01:00
|
|
|
STR_NETWORK_ERR_CLIENT_NEWGRF_MISMATCH,
|
2006-04-03 20:31:01 +02:00
|
|
|
STR_NETWORK_ERR_CLIENT_NOT_AUTHORIZED,
|
|
|
|
STR_NETWORK_ERR_CLIENT_NOT_EXPECTED,
|
|
|
|
STR_NETWORK_ERR_CLIENT_WRONG_REVISION,
|
|
|
|
STR_NETWORK_ERR_CLIENT_NAME_IN_USE,
|
|
|
|
STR_NETWORK_ERR_CLIENT_WRONG_PASSWORD,
|
|
|
|
STR_NETWORK_ERR_CLIENT_PLAYER_MISMATCH,
|
|
|
|
STR_NETWORK_ERR_CLIENT_KICKED,
|
|
|
|
STR_NETWORK_ERR_CLIENT_CHEATER,
|
2006-06-27 23:25:53 +02:00
|
|
|
STR_NETWORK_ERR_CLIENT_SERVER_FULL
|
2006-04-03 20:31:01 +02:00
|
|
|
};
|
|
|
|
|
2007-01-10 19:56:51 +01:00
|
|
|
if (err >= (ptrdiff_t)lengthof(network_error_strings)) err = NETWORK_ERROR_GENERAL;
|
2006-04-03 20:31:01 +02:00
|
|
|
|
2006-10-22 01:31:34 +02:00
|
|
|
return GetString(buf, network_error_strings[err], last);
|
2006-04-03 20:31:01 +02:00
|
|
|
}
|
|
|
|
|
2006-10-03 18:15:34 +02:00
|
|
|
/* Count the number of active clients connected */
|
|
|
|
static uint NetworkCountPlayers(void)
|
|
|
|
{
|
2007-01-12 21:19:49 +01:00
|
|
|
NetworkTCPSocketHandler *cs;
|
2006-10-03 18:15:34 +02:00
|
|
|
uint count = 0;
|
|
|
|
|
|
|
|
FOR_ALL_CLIENTS(cs) {
|
2006-10-18 01:34:12 +02:00
|
|
|
const NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs);
|
2006-10-18 00:16:46 +02:00
|
|
|
if (IsValidPlayer(ci->client_playas)) count++;
|
2006-10-03 18:15:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool _min_players_paused = false;
|
|
|
|
|
|
|
|
/* Check if the minimum number of players has been reached and pause or unpause the game as appropriate */
|
|
|
|
void CheckMinPlayers(void)
|
|
|
|
{
|
|
|
|
if (!_network_dedicated) return;
|
|
|
|
|
|
|
|
if (NetworkCountPlayers() < _network_min_players) {
|
|
|
|
if (_min_players_paused) return;
|
|
|
|
|
|
|
|
_min_players_paused = true;
|
|
|
|
DoCommandP(0, 1, 0, NULL, CMD_PAUSE);
|
2006-10-25 00:23:08 +02:00
|
|
|
NetworkServer_HandleChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "Game paused (not enough players)", NETWORK_SERVER_INDEX);
|
2006-10-03 18:15:34 +02:00
|
|
|
} else {
|
|
|
|
if (!_min_players_paused) return;
|
|
|
|
|
|
|
|
_min_players_paused = false;
|
|
|
|
DoCommandP(0, 0, 0, NULL, CMD_PAUSE);
|
2006-10-25 00:23:08 +02:00
|
|
|
NetworkServer_HandleChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "Game unpaused (enough players)", NETWORK_SERVER_INDEX);
|
2006-10-03 18:15:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// Find all IP-aliases for this host
|
2004-12-19 11:17:26 +01:00
|
|
|
static void NetworkFindIPs(void)
|
2004-09-11 21:34:11 +02:00
|
|
|
{
|
2006-06-17 12:24:20 +02:00
|
|
|
int i;
|
2004-09-11 21:34:11 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
#if defined(BEOS_NET_SERVER) /* doesn't have neither getifaddrs or net/if.h */
|
|
|
|
/* Based on Andrew Bachmann's netstat+.c. Big thanks to him! */
|
|
|
|
int _netstat(int fd, char **output, int verbose);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
int seek_past_header(char **pos, const char *header) {
|
|
|
|
char *new_pos = strstr(*pos, header);
|
|
|
|
if (new_pos == 0) {
|
|
|
|
return B_ERROR;
|
2004-09-12 17:29:37 +02:00
|
|
|
}
|
2004-12-04 18:54:56 +01:00
|
|
|
*pos += strlen(header) + new_pos - *pos + 1;
|
|
|
|
return B_OK;
|
2004-09-12 17:29:37 +02:00
|
|
|
}
|
2004-09-15 20:36:33 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
int output_length;
|
|
|
|
char *output_pointer = NULL;
|
|
|
|
char **output;
|
|
|
|
int sock = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
i = 0;
|
2004-09-14 00:49:11 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// If something fails, make sure the list is empty
|
2006-06-17 12:24:20 +02:00
|
|
|
_broadcast_list[0] = 0;
|
2004-09-14 00:49:11 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
if (sock < 0) {
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(net, 0, "[core] error creating socket");
|
2004-12-04 18:54:56 +01:00
|
|
|
return;
|
2004-09-12 17:29:37 +02:00
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
output_length = _netstat(sock, &output_pointer, 1);
|
|
|
|
if (output_length < 0) {
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(net, 0, "[core] error running _netstat");
|
2004-12-04 18:54:56 +01:00
|
|
|
return;
|
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
output = &output_pointer;
|
|
|
|
if (seek_past_header(output, "IP Interfaces:") == B_OK) {
|
|
|
|
for (;;) {
|
|
|
|
uint32 n, fields, read;
|
|
|
|
uint8 i1, i2, i3, i4, j1, j2, j3, j4;
|
|
|
|
struct in_addr inaddr;
|
2006-06-17 12:24:20 +02:00
|
|
|
uint32 ip;
|
|
|
|
uint32 netmask;
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
fields = sscanf(*output, "%u: %hhu.%hhu.%hhu.%hhu, netmask %hhu.%hhu.%hhu.%hhu%n",
|
|
|
|
&n, &i1,&i2,&i3,&i4, &j1,&j2,&j3,&j4, &read);
|
|
|
|
read += 1;
|
|
|
|
if (fields != 9) {
|
|
|
|
break;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2006-06-17 12:24:20 +02:00
|
|
|
|
|
|
|
ip = (uint32)i1 << 24 | (uint32)i2 << 16 | (uint32)i3 << 8 | (uint32)i4;
|
|
|
|
netmask = (uint32)j1 << 24 | (uint32)j2 << 16 | (uint32)j3 << 8 | (uint32)j4;
|
|
|
|
|
|
|
|
if (ip != INADDR_LOOPBACK && ip != INADDR_ANY) {
|
|
|
|
inaddr.s_addr = htonl(ip | ~netmask);
|
|
|
|
_broadcast_list[i] = inaddr.s_addr;
|
2004-12-04 18:54:56 +01:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (read < 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*output += read;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2004-12-04 18:54:56 +01:00
|
|
|
/* XXX - Using either one of these crashes openttd heavily? - wber */
|
|
|
|
/*free(output_pointer);*/
|
|
|
|
/*free(output);*/
|
|
|
|
closesocket(sock);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2004-12-04 18:54:56 +01:00
|
|
|
#elif defined(HAVE_GETIFADDRS)
|
|
|
|
struct ifaddrs *ifap, *ifa;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// If something fails, make sure the list is empty
|
2006-06-17 12:24:20 +02:00
|
|
|
_broadcast_list[0] = 0;
|
2004-09-12 17:29:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
if (getifaddrs(&ifap) != 0)
|
|
|
|
return;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
i = 0;
|
|
|
|
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
|
2006-06-17 12:24:20 +02:00
|
|
|
if (!(ifa->ifa_flags & IFF_BROADCAST)) continue;
|
|
|
|
if (ifa->ifa_broadaddr == NULL) continue;
|
|
|
|
if (ifa->ifa_broadaddr->sa_family != AF_INET) continue;
|
|
|
|
_broadcast_list[i] = ((struct sockaddr_in*)ifa->ifa_broadaddr)->sin_addr.s_addr;
|
2004-12-04 18:54:56 +01:00
|
|
|
i++;
|
2004-09-14 00:49:11 +02:00
|
|
|
}
|
2004-12-04 18:54:56 +01:00
|
|
|
freeifaddrs(ifap);
|
2004-09-14 00:49:11 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
#else /* not HAVE_GETIFADDRS */
|
|
|
|
SOCKET sock;
|
2006-06-17 10:16:58 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
DWORD len = 0;
|
|
|
|
INTERFACE_INFO ifo[MAX_INTERFACES];
|
2006-06-17 12:24:20 +02:00
|
|
|
uint j;
|
2006-06-17 10:16:58 +02:00
|
|
|
#else
|
|
|
|
char buf[4 * 1024]; // Arbitrary buffer size
|
|
|
|
struct ifconf ifconf;
|
|
|
|
const char* buf_end;
|
|
|
|
const char* p;
|
2004-12-04 18:54:56 +01:00
|
|
|
#endif
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// If something fails, make sure the list is empty
|
2006-06-17 12:24:20 +02:00
|
|
|
_broadcast_list[0] = 0;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-03-09 12:49:34 +01:00
|
|
|
sock = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (sock == INVALID_SOCKET) return;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
#ifdef WIN32
|
|
|
|
memset(&ifo[0], 0, sizeof(ifo));
|
|
|
|
if ((WSAIoctl(sock, SIO_GET_INTERFACE_LIST, NULL, 0, &ifo[0], sizeof(ifo), &len, NULL, NULL)) != 0) {
|
|
|
|
closesocket(sock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-06-17 14:11:17 +02:00
|
|
|
i = 0;
|
2006-06-17 12:24:20 +02:00
|
|
|
for (j = 0; j < len / sizeof(*ifo); j++) {
|
2006-06-17 14:11:17 +02:00
|
|
|
if (ifo[j].iiFlags & IFF_LOOPBACK) continue;
|
2006-06-17 12:24:20 +02:00
|
|
|
if (!(ifo[j].iiFlags & IFF_BROADCAST)) continue;
|
2006-06-17 14:11:17 +02:00
|
|
|
/* iiBroadcast is unusable, because it always seems to be set to
|
|
|
|
* 255.255.255.255.
|
|
|
|
*/
|
|
|
|
_broadcast_list[i++] =
|
|
|
|
ifo[j].iiAddress.AddressIn.sin_addr.s_addr |
|
|
|
|
~ifo[j].iiNetmask.AddressIn.sin_addr.s_addr;
|
2006-06-17 10:16:58 +02:00
|
|
|
}
|
2004-12-04 18:54:56 +01:00
|
|
|
#else
|
2006-06-17 10:16:58 +02:00
|
|
|
ifconf.ifc_len = sizeof(buf);
|
|
|
|
ifconf.ifc_buf = buf;
|
|
|
|
if (ioctl(sock, SIOCGIFCONF, &ifconf) == -1) {
|
|
|
|
closesocket(sock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
buf_end = buf + ifconf.ifc_len;
|
|
|
|
for (p = buf; p < buf_end;) {
|
|
|
|
const struct ifreq* req = (const struct ifreq*)p;
|
|
|
|
|
|
|
|
if (req->ifr_addr.sa_family == AF_INET) {
|
|
|
|
struct ifreq r;
|
|
|
|
|
|
|
|
strncpy(r.ifr_name, req->ifr_name, lengthof(r.ifr_name));
|
2006-06-17 15:25:22 +02:00
|
|
|
if (ioctl(sock, SIOCGIFFLAGS, &r) != -1 &&
|
|
|
|
r.ifr_flags & IFF_BROADCAST &&
|
|
|
|
ioctl(sock, SIOCGIFBRDADDR, &r) != -1) {
|
2006-06-17 12:24:20 +02:00
|
|
|
_broadcast_list[i++] =
|
|
|
|
((struct sockaddr_in*)&r.ifr_broadaddr)->sin_addr.s_addr;
|
2006-06-17 10:16:58 +02:00
|
|
|
}
|
2004-12-04 18:54:56 +01:00
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-06-17 10:16:58 +02:00
|
|
|
p += sizeof(struct ifreq);
|
|
|
|
#ifdef AF_LINK
|
|
|
|
p += req->ifr_addr.sa_len - sizeof(struct sockaddr);
|
2004-12-04 18:54:56 +01:00
|
|
|
#endif
|
|
|
|
}
|
2006-06-17 10:16:58 +02:00
|
|
|
#endif
|
2004-09-08 21:20:46 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
closesocket(sock);
|
|
|
|
#endif /* not HAVE_GETIFADDRS */
|
|
|
|
|
2006-06-17 12:24:20 +02:00
|
|
|
_broadcast_list[i] = 0;
|
2004-12-04 18:54:56 +01:00
|
|
|
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(net, 3, "Detected broadcast addresses:");
|
2004-12-04 18:54:56 +01:00
|
|
|
// Now display to the debug all the detected ips
|
2006-06-17 12:24:20 +02:00
|
|
|
for (i = 0; _broadcast_list[i] != 0; i++) {
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(net, 3, "%d) %s", i, inet_ntoa(*(struct in_addr *)&_broadcast_list[i]));//inet_ntoa(inaddr));
|
2004-12-04 18:54:56 +01:00
|
|
|
}
|
2004-09-12 00:10:31 +02:00
|
|
|
}
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// Resolve a hostname to a inet_addr
|
|
|
|
unsigned long NetworkResolveHost(const char *hostname)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2004-12-04 18:54:56 +01:00
|
|
|
in_addr_t ip;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// First try: is it an ip address?
|
|
|
|
ip = inet_addr(hostname);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// If not try to resolve the name
|
|
|
|
if (ip == INADDR_NONE) {
|
|
|
|
struct hostent *he = gethostbyname(hostname);
|
|
|
|
if (he == NULL) {
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(net, 0, "Cannot resolve '%s'", hostname);
|
2004-08-09 19:04:08 +02:00
|
|
|
} else {
|
2004-12-04 18:54:56 +01:00
|
|
|
struct in_addr addr = *(struct in_addr *)he->h_addr_list[0];
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(net, 1, "Resolved '%s' to %s", hostname, inet_ntoa(addr));
|
2004-12-04 18:54:56 +01:00
|
|
|
ip = addr.s_addr;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
}
|
2004-12-04 18:54:56 +01:00
|
|
|
return ip;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2007-01-17 00:01:06 +01:00
|
|
|
/** Converts a string to ip/port/player
|
|
|
|
* Format: IP#player:port
|
|
|
|
*
|
|
|
|
* connection_string will be re-terminated to seperate out the hostname, and player and port will
|
|
|
|
* be set to the player and port strings given by the user, inside the memory area originally
|
|
|
|
* occupied by connection_string. */
|
2005-02-06 23:25:27 +01:00
|
|
|
void ParseConnectionString(const char **player, const char **port, char *connection_string)
|
2004-09-12 16:12:33 +02:00
|
|
|
{
|
2005-02-06 23:25:27 +01:00
|
|
|
char *p;
|
2004-12-04 18:54:56 +01:00
|
|
|
for (p = connection_string; *p != '\0'; p++) {
|
2007-01-17 00:01:06 +01:00
|
|
|
switch (*p) {
|
|
|
|
case '#':
|
|
|
|
*player = p + 1;
|
|
|
|
*p = '\0';
|
|
|
|
break;
|
|
|
|
case ':':
|
|
|
|
*port = p + 1;
|
|
|
|
*p = '\0';
|
|
|
|
break;
|
2004-09-11 21:34:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// Creates a new client from a socket
|
|
|
|
// Used both by the server and the client
|
2007-01-12 21:19:49 +01:00
|
|
|
static NetworkTCPSocketHandler *NetworkAllocClient(SOCKET s)
|
2004-09-11 21:34:11 +02:00
|
|
|
{
|
2007-01-12 21:19:49 +01:00
|
|
|
NetworkTCPSocketHandler *cs;
|
2006-10-18 01:34:12 +02:00
|
|
|
byte client_no = 0;
|
2004-09-11 21:34:11 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
if (_network_server) {
|
|
|
|
// Can we handle a new client?
|
2006-10-18 01:34:12 +02:00
|
|
|
if (_network_clients_connected >= MAX_CLIENTS) return NULL;
|
|
|
|
if (_network_game_info.clients_on >= _network_game_info.clients_max) return NULL;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// Register the login
|
|
|
|
client_no = _network_clients_connected++;
|
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-10-18 01:34:12 +02:00
|
|
|
cs = DEREF_CLIENT(client_no);
|
2007-01-12 21:19:49 +01:00
|
|
|
cs->Initialize();
|
|
|
|
cs->sock = s;
|
2005-03-29 21:10:13 +02:00
|
|
|
cs->last_frame = _frame_counter;
|
|
|
|
cs->last_frame_server = _frame_counter;
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
if (_network_server) {
|
2006-10-18 01:34:12 +02:00
|
|
|
NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs);
|
2004-12-04 18:54:56 +01:00
|
|
|
memset(ci, 0, sizeof(*ci));
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
cs->index = _network_client_index++;
|
|
|
|
ci->client_index = cs->index;
|
2006-10-18 00:16:46 +02:00
|
|
|
ci->client_playas = PLAYER_INACTIVE_CLIENT;
|
2004-12-04 18:54:56 +01:00
|
|
|
ci->join_date = _date;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
InvalidateWindow(WC_CLIENT_LIST, 0);
|
|
|
|
}
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
return cs;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// Close a connection
|
2007-01-12 21:19:49 +01:00
|
|
|
void NetworkCloseClient(NetworkTCPSocketHandler *cs)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2004-12-04 18:54:56 +01:00
|
|
|
NetworkClientInfo *ci;
|
|
|
|
// Socket is already dead
|
2007-01-12 21:19:49 +01:00
|
|
|
if (cs->sock == INVALID_SOCKET) {
|
2006-10-18 01:34:12 +02:00
|
|
|
cs->has_quit = true;
|
2005-01-05 15:39:48 +01:00
|
|
|
return;
|
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(net, 1, "Closed client connection %d", cs->index);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-10-18 01:34:12 +02:00
|
|
|
if (!cs->has_quit && _network_server && cs->status > STATUS_INACTIVE) {
|
2004-12-04 18:54:56 +01:00
|
|
|
// We did not receive a leave message from this client...
|
|
|
|
NetworkErrorCode errorno = NETWORK_ERROR_CONNECTION_LOST;
|
2004-12-19 16:14:55 +01:00
|
|
|
char str[100];
|
2006-04-22 11:46:31 +02:00
|
|
|
char client_name[NETWORK_CLIENT_NAME_LENGTH];
|
2007-01-12 21:19:49 +01:00
|
|
|
NetworkTCPSocketHandler *new_cs;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
NetworkGetClientName(client_name, sizeof(client_name), cs);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-10-22 01:31:34 +02:00
|
|
|
GetNetworkErrorMsg(str, errorno, lastof(str));
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-08-28 14:24:57 +02:00
|
|
|
NetworkTextMessage(NETWORK_ACTION_LEAVE, 1, false, client_name, "%s", str);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// Inform other clients of this... strange leaving ;)
|
|
|
|
FOR_ALL_CLIENTS(new_cs) {
|
|
|
|
if (new_cs->status > STATUS_AUTH && cs != new_cs) {
|
|
|
|
SEND_COMMAND(PACKET_SERVER_ERROR_QUIT)(new_cs, cs->index, errorno);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
}
|
2005-03-30 10:50:34 +02:00
|
|
|
}
|
2005-03-29 21:10:13 +02:00
|
|
|
|
2005-03-30 10:50:34 +02:00
|
|
|
/* When the client was PRE_ACTIVE, the server was in pause mode, so unpause */
|
|
|
|
if (cs->status == STATUS_PRE_ACTIVE && _network_pause_on_join) {
|
|
|
|
DoCommandP(0, 0, 0, NULL, CMD_PAUSE);
|
2006-10-25 00:23:08 +02:00
|
|
|
NetworkServer_HandleChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "Game unpaused", NETWORK_SERVER_INDEX);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2007-02-02 00:50:15 +01:00
|
|
|
cs->Destroy();
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// Close the gap in the client-list
|
|
|
|
ci = DEREF_CLIENT_INFO(cs);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
if (_network_server) {
|
|
|
|
// We just lost one client :(
|
2006-01-31 23:16:15 +01:00
|
|
|
if (cs->status > STATUS_INACTIVE) _network_game_info.clients_on--;
|
2004-12-04 18:54:56 +01:00
|
|
|
_network_clients_connected--;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2007-01-12 21:19:49 +01:00
|
|
|
while ((cs + 1) != DEREF_CLIENT(MAX_CLIENTS) && (cs + 1)->sock != INVALID_SOCKET) {
|
2004-12-04 18:54:56 +01:00
|
|
|
*cs = *(cs + 1);
|
|
|
|
*ci = *(ci + 1);
|
|
|
|
cs++;
|
|
|
|
ci++;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
InvalidateWindow(WC_CLIENT_LIST, 0);
|
2004-09-11 21:34:11 +02:00
|
|
|
}
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// Reset the status of the last socket
|
2007-01-12 21:19:49 +01:00
|
|
|
cs->sock = INVALID_SOCKET;
|
2004-12-04 18:54:56 +01:00
|
|
|
cs->status = STATUS_INACTIVE;
|
|
|
|
cs->index = NETWORK_EMPTY_INDEX;
|
|
|
|
ci->client_index = NETWORK_EMPTY_INDEX;
|
2006-10-03 18:15:34 +02:00
|
|
|
|
|
|
|
CheckMinPlayers();
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// A client wants to connect to a server
|
2004-12-19 11:17:26 +01:00
|
|
|
static bool NetworkConnect(const char *hostname, int port)
|
2004-09-08 21:20:46 +02:00
|
|
|
{
|
2004-12-04 18:54:56 +01:00
|
|
|
SOCKET s;
|
|
|
|
struct sockaddr_in sin;
|
2004-09-08 21:20:46 +02:00
|
|
|
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(net, 1, "Connecting to %s %d", hostname, port);
|
2004-09-16 19:02:19 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
s = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (s == INVALID_SOCKET) {
|
|
|
|
ClientStartError("socket() failed");
|
|
|
|
return false;
|
2004-09-08 21:20:46 +02:00
|
|
|
}
|
2004-09-11 21:34:11 +02:00
|
|
|
|
2006-12-26 18:36:18 +01:00
|
|
|
if (!SetNoDelay(s)) DEBUG(net, 1, "Setting TCP_NODELAY failed");
|
2004-09-11 21:34:11 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_addr.s_addr = NetworkResolveHost(hostname);
|
|
|
|
sin.sin_port = htons(port);
|
|
|
|
_network_last_host_ip = sin.sin_addr.s_addr;
|
2004-09-11 21:34:11 +02:00
|
|
|
|
2006-10-18 01:34:12 +02:00
|
|
|
/* We failed to connect for which reason what so ever */
|
|
|
|
if (connect(s, (struct sockaddr*) &sin, sizeof(sin)) != 0) return false;
|
2004-09-11 21:34:11 +02:00
|
|
|
|
2006-12-26 18:36:18 +01:00
|
|
|
if (!SetNonBlocking(s)) DEBUG(net, 0, "Setting non-blocking mode failed"); // XXX should this be an error?
|
2004-09-12 00:10:31 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// in client mode, only the first client field is used. it's pointing to the server.
|
2004-12-19 11:17:26 +01:00
|
|
|
NetworkAllocClient(s);
|
2004-09-12 00:10:31 +02:00
|
|
|
|
2006-12-30 02:52:09 +01:00
|
|
|
_network_join_status = NETWORK_JOIN_STATUS_CONNECTING;
|
2004-12-04 18:54:56 +01:00
|
|
|
ShowJoinStatusWindow();
|
2004-09-11 21:34:11 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
return true;
|
2004-09-11 21:34:11 +02:00
|
|
|
}
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// For the server, to accept new clients
|
|
|
|
static void NetworkAcceptClients(void)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
|
|
|
struct sockaddr_in sin;
|
2007-01-12 21:19:49 +01:00
|
|
|
NetworkTCPSocketHandler *cs;
|
2005-01-09 22:25:44 +01:00
|
|
|
uint i;
|
2005-01-02 13:03:43 +01:00
|
|
|
bool banned;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// Should never ever happen.. is it possible??
|
2004-08-09 19:04:08 +02:00
|
|
|
assert(_listensocket != INVALID_SOCKET);
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
for (;;) {
|
2006-10-18 01:34:12 +02:00
|
|
|
socklen_t sin_len = sizeof(sin);
|
|
|
|
SOCKET s = accept(_listensocket, (struct sockaddr*)&sin, &sin_len);
|
2004-08-09 19:04:08 +02:00
|
|
|
if (s == INVALID_SOCKET) return;
|
|
|
|
|
2005-02-07 10:56:16 +01:00
|
|
|
SetNonBlocking(s); // XXX error handling?
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(net, 1, "Client connected from %s on frame %d", inet_ntoa(sin.sin_addr), _frame_counter);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-02-07 10:56:16 +01:00
|
|
|
SetNoDelay(s); // XXX error handling?
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-01-02 13:03:43 +01:00
|
|
|
/* Check if the client is banned */
|
|
|
|
banned = false;
|
|
|
|
for (i = 0; i < lengthof(_network_ban_list); i++) {
|
2006-10-18 01:34:12 +02:00
|
|
|
if (_network_ban_list[i] == NULL) continue;
|
2005-01-02 13:03:43 +01:00
|
|
|
|
|
|
|
if (sin.sin_addr.s_addr == inet_addr(_network_ban_list[i])) {
|
2007-02-01 23:30:35 +01:00
|
|
|
Packet p(PACKET_SERVER_BANNED);
|
|
|
|
p.PrepareToSend();
|
2005-01-02 13:03:43 +01:00
|
|
|
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(net, 1, "Banned ip tried to join (%s), refused", _network_ban_list[i]);
|
2005-01-02 13:03:43 +01:00
|
|
|
|
2007-02-01 23:30:35 +01:00
|
|
|
send(s, (const char*)p.buffer, p.size, 0);
|
2005-01-02 13:03:43 +01:00
|
|
|
closesocket(s);
|
|
|
|
|
|
|
|
banned = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* If this client is banned, continue with next client */
|
2006-10-18 01:34:12 +02:00
|
|
|
if (banned) continue;
|
2005-01-02 13:03:43 +01:00
|
|
|
|
2004-12-19 11:17:26 +01:00
|
|
|
cs = NetworkAllocClient(s);
|
2004-08-09 19:04:08 +02:00
|
|
|
if (cs == NULL) {
|
|
|
|
// no more clients allowed?
|
2004-12-04 18:54:56 +01:00
|
|
|
// Send to the client that we are full!
|
2007-02-01 23:30:35 +01:00
|
|
|
Packet p(PACKET_SERVER_FULL);
|
|
|
|
p.PrepareToSend();
|
2004-12-04 18:54:56 +01:00
|
|
|
|
2007-02-01 23:30:35 +01:00
|
|
|
send(s, (const char*)p.buffer, p.size, 0);
|
2004-08-09 19:04:08 +02:00
|
|
|
closesocket(s);
|
2004-12-04 18:54:56 +01:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// a new client has connected. We set him at inactive for now
|
|
|
|
// maybe he is only requesting server-info. Till he has sent a PACKET_CLIENT_MAP_OK
|
|
|
|
// the client stays inactive
|
|
|
|
cs->status = STATUS_INACTIVE;
|
|
|
|
|
2006-10-18 01:34:12 +02:00
|
|
|
DEREF_CLIENT_INFO(cs)->client_ip = sin.sin_addr.s_addr; // Save the IP of the client
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// Set up the listen socket for the server
|
2004-12-19 11:17:26 +01:00
|
|
|
static bool NetworkListen(void)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2004-12-04 18:54:56 +01:00
|
|
|
SOCKET ls;
|
|
|
|
struct sockaddr_in sin;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(net, 1, "Listening on %s:%d", _network_server_bind_ip_host, _network_server_port);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
ls = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (ls == INVALID_SOCKET) {
|
|
|
|
ServerStartError("socket() on listen socket failed");
|
|
|
|
return false;
|
|
|
|
}
|
2004-09-11 21:34:11 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
{ // reuse the socket
|
|
|
|
int reuse = 1;
|
|
|
|
// The (const char*) cast is needed for windows!!
|
|
|
|
if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) == -1) {
|
|
|
|
ServerStartError("setsockopt() on listen socket failed");
|
|
|
|
return false;
|
2004-09-11 21:34:11 +02:00
|
|
|
}
|
2004-12-04 18:54:56 +01:00
|
|
|
}
|
2004-09-11 21:34:11 +02:00
|
|
|
|
2006-12-26 18:36:18 +01:00
|
|
|
if (!SetNonBlocking(ls)) DEBUG(net, 0, "Setting non-blocking mode failed"); // XXX should this be an error?
|
2004-08-22 12:23:37 +02:00
|
|
|
|
|
|
|
sin.sin_family = AF_INET;
|
2004-12-13 18:47:21 +01:00
|
|
|
sin.sin_addr.s_addr = _network_server_bind_ip;
|
2006-10-18 01:34:12 +02:00
|
|
|
sin.sin_port = htons(_network_server_port);
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
if (bind(ls, (struct sockaddr*)&sin, sizeof(sin)) != 0) {
|
|
|
|
ServerStartError("bind() failed");
|
|
|
|
return false;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
if (listen(ls, 1) != 0) {
|
|
|
|
ServerStartError("listen() failed");
|
|
|
|
return false;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
_listensocket = ls;
|
2004-09-11 21:34:11 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
return true;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// Close all current connections
|
2004-12-19 11:17:26 +01:00
|
|
|
static void NetworkClose(void)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2007-01-12 21:19:49 +01:00
|
|
|
NetworkTCPSocketHandler *cs;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
FOR_ALL_CLIENTS(cs) {
|
|
|
|
if (!_network_server) {
|
|
|
|
SEND_COMMAND(PACKET_CLIENT_QUIT)("leaving");
|
2007-02-02 00:50:15 +01:00
|
|
|
cs->Send_Packets();
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2004-12-19 11:17:26 +01:00
|
|
|
NetworkCloseClient(cs);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
if (_network_server) {
|
|
|
|
// We are a server, also close the listensocket
|
|
|
|
closesocket(_listensocket);
|
|
|
|
_listensocket = INVALID_SOCKET;
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(net, 1, "Closed listener");
|
2007-01-12 15:30:01 +01:00
|
|
|
NetworkUDPCloseAll();
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// Inits the network (cleans sockets and stuff)
|
2004-12-19 11:17:26 +01:00
|
|
|
static void NetworkInitialize(void)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2007-01-12 21:19:49 +01:00
|
|
|
NetworkTCPSocketHandler *cs;
|
2004-08-10 16:14:00 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
_local_command_queue = NULL;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// Clean all client-sockets
|
|
|
|
for (cs = _clients; cs != &_clients[MAX_CLIENTS]; cs++) {
|
2007-01-12 21:19:49 +01:00
|
|
|
cs->Initialize();
|
2004-12-04 18:54:56 +01:00
|
|
|
}
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// Clean the client_info memory
|
2006-01-25 19:11:06 +01:00
|
|
|
memset(&_network_client_info, 0, sizeof(_network_client_info));
|
|
|
|
memset(&_network_player_info, 0, sizeof(_network_player_info));
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
_sync_frame = 0;
|
|
|
|
_network_first_time = true;
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
_network_reconnect = 0;
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
NetworkUDPInitialize();
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// Query a server to fetch his game-info
|
|
|
|
// If game_info is true, only the gameinfo is fetched,
|
|
|
|
// else only the client_info is fetched
|
2005-02-06 23:25:27 +01:00
|
|
|
NetworkGameList *NetworkQueryServer(const char* host, unsigned short port, bool game_info)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2004-12-20 23:14:39 +01:00
|
|
|
if (!_network_available) return NULL;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
NetworkDisconnect();
|
2004-09-12 16:12:33 +02:00
|
|
|
|
2006-10-18 01:34:12 +02:00
|
|
|
if (game_info) return NetworkUDPQueryServer(host, port);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
NetworkInitialize();
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
_network_server = false;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// Try to connect
|
|
|
|
_networking = NetworkConnect(host, port);
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// We are connected
|
|
|
|
if (_networking) {
|
|
|
|
SEND_COMMAND(PACKET_CLIENT_COMPANY_INFO)();
|
2006-10-18 01:34:12 +02:00
|
|
|
} else { // No networking, close everything down again
|
|
|
|
NetworkDisconnect();
|
2004-12-04 18:54:56 +01:00
|
|
|
}
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-12-20 23:14:39 +01:00
|
|
|
return NULL;
|
2004-12-04 18:54:56 +01:00
|
|
|
}
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-12-20 23:14:39 +01:00
|
|
|
/* Validates an address entered as a string and adds the server to
|
2006-01-25 20:03:50 +01:00
|
|
|
* the list. If you use this function, the games will be marked
|
2004-12-20 23:14:39 +01:00
|
|
|
* as manually added. */
|
2005-02-06 23:25:27 +01:00
|
|
|
void NetworkAddServer(const char *b)
|
2004-12-18 19:58:03 +01:00
|
|
|
{
|
|
|
|
if (*b != '\0') {
|
2004-12-20 23:14:39 +01:00
|
|
|
NetworkGameList *item;
|
2005-02-06 23:25:27 +01:00
|
|
|
const char *port = NULL;
|
|
|
|
const char *player = NULL;
|
|
|
|
char host[NETWORK_HOSTNAME_LENGTH];
|
2004-12-18 19:58:03 +01:00
|
|
|
uint16 rport;
|
|
|
|
|
2004-12-19 11:17:26 +01:00
|
|
|
ttd_strlcpy(host, b, lengthof(host));
|
|
|
|
|
2004-12-18 19:58:03 +01:00
|
|
|
ttd_strlcpy(_network_default_ip, b, lengthof(_network_default_ip));
|
|
|
|
rport = NETWORK_DEFAULT_PORT;
|
|
|
|
|
2004-12-19 11:17:26 +01:00
|
|
|
ParseConnectionString(&player, &port, host);
|
2004-12-18 19:58:03 +01:00
|
|
|
if (port != NULL) rport = atoi(port);
|
|
|
|
|
2004-12-20 23:14:39 +01:00
|
|
|
item = NetworkQueryServer(host, rport, true);
|
|
|
|
item->manually = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-21 17:56:43 +01:00
|
|
|
/* Generates the list of manually added hosts from NetworkGameList and
|
2004-12-20 23:14:39 +01:00
|
|
|
* dumps them into the array _network_host_list. This array is needed
|
|
|
|
* by the function that generates the config file. */
|
2005-01-22 21:23:18 +01:00
|
|
|
void NetworkRebuildHostList(void)
|
2004-12-20 23:14:39 +01:00
|
|
|
{
|
2005-01-09 22:25:44 +01:00
|
|
|
uint i = 0;
|
2006-10-18 01:34:12 +02:00
|
|
|
const NetworkGameList *item = _network_game_list;
|
2005-01-02 13:03:43 +01:00
|
|
|
while (item != NULL && i != lengthof(_network_host_list)) {
|
2006-01-29 19:34:43 +01:00
|
|
|
if (item->manually) {
|
|
|
|
free(_network_host_list[i]);
|
2005-01-05 10:59:45 +01:00
|
|
|
_network_host_list[i++] = str_fmt("%s:%i", item->info.hostname, item->port);
|
2006-01-29 19:34:43 +01:00
|
|
|
}
|
2004-12-20 23:14:39 +01:00
|
|
|
item = item->next;
|
|
|
|
}
|
|
|
|
|
2005-01-02 13:03:43 +01:00
|
|
|
for (; i < lengthof(_network_host_list); i++) {
|
2006-01-29 19:34:43 +01:00
|
|
|
free(_network_host_list[i]);
|
|
|
|
_network_host_list[i] = NULL;
|
2004-12-18 19:58:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// Used by clients, to connect to a server
|
2006-10-18 01:34:12 +02:00
|
|
|
bool NetworkClientConnectGame(const char *host, uint16 port)
|
2004-12-04 18:54:56 +01:00
|
|
|
{
|
|
|
|
if (!_network_available) return false;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
if (port == 0) return false;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
ttd_strlcpy(_network_last_host, host, sizeof(_network_last_host));
|
|
|
|
_network_last_port = port;
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
NetworkDisconnect();
|
2007-01-12 15:30:01 +01:00
|
|
|
NetworkUDPCloseAll();
|
2004-12-04 18:54:56 +01:00
|
|
|
NetworkInitialize();
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// Try to connect
|
|
|
|
_networking = NetworkConnect(host, port);
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// We are connected
|
|
|
|
if (_networking) {
|
|
|
|
IConsoleCmdExec("exec scripts/on_client.scr 0");
|
|
|
|
NetworkClient_Connected();
|
|
|
|
} else {
|
|
|
|
// Connecting failed
|
|
|
|
NetworkError(STR_NETWORK_ERR_NOCONNECTION);
|
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
return _networking;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2004-12-19 11:17:26 +01:00
|
|
|
static void NetworkInitGameInfo(void)
|
2004-09-12 16:12:33 +02:00
|
|
|
{
|
2004-12-04 18:54:56 +01:00
|
|
|
NetworkClientInfo *ci;
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
ttd_strlcpy(_network_game_info.server_name, _network_server_name, sizeof(_network_game_info.server_name));
|
2005-01-15 21:09:16 +01:00
|
|
|
ttd_strlcpy(_network_game_info.server_password, _network_server_password, sizeof(_network_server_password));
|
|
|
|
ttd_strlcpy(_network_game_info.rcon_password, _network_rcon_password, sizeof(_network_rcon_password));
|
2004-12-04 18:54:56 +01:00
|
|
|
if (_network_game_info.server_name[0] == '\0')
|
|
|
|
snprintf(_network_game_info.server_name, sizeof(_network_game_info.server_name), "Unnamed Server");
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2006-01-25 19:11:06 +01:00
|
|
|
ttd_strlcpy(_network_game_info.server_revision, _openttd_revision, sizeof(_network_game_info.server_revision));
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// The server is a client too ;)
|
|
|
|
if (_network_dedicated) {
|
|
|
|
_network_game_info.clients_on = 0;
|
2006-01-25 19:11:06 +01:00
|
|
|
_network_game_info.companies_on = 0;
|
2004-12-04 18:54:56 +01:00
|
|
|
_network_game_info.dedicated = true;
|
|
|
|
} else {
|
|
|
|
_network_game_info.clients_on = 1;
|
2006-01-25 19:11:06 +01:00
|
|
|
_network_game_info.companies_on = 1;
|
2004-12-04 18:54:56 +01:00
|
|
|
_network_game_info.dedicated = false;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2006-01-25 19:11:06 +01:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
_network_game_info.spectators_on = 0;
|
2006-01-25 19:11:06 +01:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
_network_game_info.game_date = _date;
|
2006-08-16 14:07:24 +02:00
|
|
|
_network_game_info.start_date = ConvertYMDToDate(_patches.starting_year, 0, 1);
|
2005-01-03 19:59:58 +01:00
|
|
|
_network_game_info.map_width = MapSizeX();
|
|
|
|
_network_game_info.map_height = MapSizeY();
|
2004-12-04 18:54:56 +01:00
|
|
|
_network_game_info.map_set = _opt.landscape;
|
|
|
|
|
2005-06-04 09:35:12 +02:00
|
|
|
_network_game_info.use_password = (_network_server_password[0] != '\0');
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// We use _network_client_info[MAX_CLIENT_INFO - 1] to store the server-data in it
|
|
|
|
// The index is NETWORK_SERVER_INDEX ( = 1)
|
|
|
|
ci = &_network_client_info[MAX_CLIENT_INFO - 1];
|
|
|
|
memset(ci, 0, sizeof(*ci));
|
|
|
|
|
|
|
|
ci->client_index = NETWORK_SERVER_INDEX;
|
2006-10-18 00:16:46 +02:00
|
|
|
ci->client_playas = _network_dedicated ? PLAYER_SPECTATOR : _local_player;
|
|
|
|
|
2004-12-16 16:51:18 +01:00
|
|
|
ttd_strlcpy(ci->client_name, _network_player_name, sizeof(ci->client_name));
|
|
|
|
ttd_strlcpy(ci->unique_id, _network_unique_id, sizeof(ci->unique_id));
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
bool NetworkServerStart(void)
|
2004-09-12 16:12:33 +02:00
|
|
|
{
|
2004-12-04 18:54:56 +01:00
|
|
|
if (!_network_available) return false;
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-12-13 18:47:21 +01:00
|
|
|
/* Call the pre-scripts */
|
|
|
|
IConsoleCmdExec("exec scripts/pre_server.scr 0");
|
|
|
|
if (_network_dedicated) IConsoleCmdExec("exec scripts/pre_dedicated.scr 0");
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
NetworkInitialize();
|
2006-10-18 01:34:12 +02:00
|
|
|
if (!NetworkListen()) return false;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// Try to start UDP-server
|
|
|
|
_network_udp_server = true;
|
2007-01-12 15:30:01 +01:00
|
|
|
_network_udp_server = _udp_server_socket->Listen(_network_server_bind_ip, _network_server_port, false);
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
_network_server = true;
|
|
|
|
_networking = true;
|
|
|
|
_frame_counter = 0;
|
|
|
|
_frame_counter_server = 0;
|
|
|
|
_frame_counter_max = 0;
|
2005-07-16 14:59:23 +02:00
|
|
|
_last_sync_frame = 0;
|
2004-12-04 18:54:56 +01:00
|
|
|
_network_own_client_index = NETWORK_SERVER_INDEX;
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2006-10-18 00:16:46 +02:00
|
|
|
/* Non-dedicated server will always be player #1 */
|
2007-01-10 19:56:51 +01:00
|
|
|
if (!_network_dedicated) _network_playas = PLAYER_FIRST;
|
2004-12-16 12:04:43 +01:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
_network_clients_connected = 0;
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
NetworkInitGameInfo();
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// execute server initialization script
|
|
|
|
IConsoleCmdExec("exec scripts/on_server.scr 0");
|
|
|
|
// if the server is dedicated ... add some other script
|
|
|
|
if (_network_dedicated) IConsoleCmdExec("exec scripts/on_dedicated.scr 0");
|
2004-12-15 21:10:34 +01:00
|
|
|
|
2006-10-03 18:15:34 +02:00
|
|
|
_min_players_paused = false;
|
|
|
|
CheckMinPlayers();
|
|
|
|
|
2004-12-15 21:10:34 +01:00
|
|
|
/* Try to register us to the master server */
|
2006-01-19 18:50:40 +01:00
|
|
|
_network_last_advertise_frame = 0;
|
|
|
|
_network_need_advertise = true;
|
2004-12-15 21:10:34 +01:00
|
|
|
NetworkUDPAdvertise();
|
2004-12-04 18:54:56 +01:00
|
|
|
return true;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// The server is rebooting...
|
|
|
|
// The only difference with NetworkDisconnect, is the packets that is sent
|
|
|
|
void NetworkReboot(void)
|
2004-09-12 16:12:33 +02:00
|
|
|
{
|
2004-12-04 18:54:56 +01:00
|
|
|
if (_network_server) {
|
2007-01-12 21:19:49 +01:00
|
|
|
NetworkTCPSocketHandler *cs;
|
2004-12-04 18:54:56 +01:00
|
|
|
FOR_ALL_CLIENTS(cs) {
|
|
|
|
SEND_COMMAND(PACKET_SERVER_NEWGAME)(cs);
|
2007-02-02 00:50:15 +01:00
|
|
|
cs->Send_Packets();
|
2004-08-22 12:23:37 +02:00
|
|
|
}
|
2004-12-04 18:54:56 +01:00
|
|
|
}
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
NetworkClose();
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// Free all queued commands
|
|
|
|
while (_local_command_queue != NULL) {
|
|
|
|
CommandPacket *p = _local_command_queue;
|
|
|
|
_local_command_queue = _local_command_queue->next;
|
|
|
|
free(p);
|
2004-08-22 12:23:37 +02:00
|
|
|
}
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
_networking = false;
|
|
|
|
_network_server = false;
|
2004-08-22 12:23:37 +02:00
|
|
|
}
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// We want to disconnect from the host/clients
|
|
|
|
void NetworkDisconnect(void)
|
2004-09-12 16:12:33 +02:00
|
|
|
{
|
2004-12-04 18:54:56 +01:00
|
|
|
if (_network_server) {
|
2007-01-12 21:19:49 +01:00
|
|
|
NetworkTCPSocketHandler *cs;
|
2004-12-04 18:54:56 +01:00
|
|
|
FOR_ALL_CLIENTS(cs) {
|
|
|
|
SEND_COMMAND(PACKET_SERVER_SHUTDOWN)(cs);
|
2007-02-02 00:50:15 +01:00
|
|
|
cs->Send_Packets();
|
2004-12-04 18:54:56 +01:00
|
|
|
}
|
2004-09-12 16:12:33 +02:00
|
|
|
}
|
2004-08-27 13:01:26 +02:00
|
|
|
|
2006-10-18 01:34:12 +02:00
|
|
|
if (_network_advertise) NetworkUDPRemoveAdvertise();
|
2004-12-22 19:56:52 +01:00
|
|
|
|
2004-12-12 14:46:10 +01:00
|
|
|
DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
NetworkClose();
|
2004-09-07 00:53:59 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// Free all queued commands
|
|
|
|
while (_local_command_queue != NULL) {
|
|
|
|
CommandPacket *p = _local_command_queue;
|
|
|
|
_local_command_queue = _local_command_queue->next;
|
|
|
|
free(p);
|
2004-09-12 16:12:33 +02:00
|
|
|
}
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
_networking = false;
|
|
|
|
_network_server = false;
|
|
|
|
}
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// Receives something from the network
|
2004-12-19 11:17:26 +01:00
|
|
|
static bool NetworkReceive(void)
|
2004-12-04 18:54:56 +01:00
|
|
|
{
|
2007-01-12 21:19:49 +01:00
|
|
|
NetworkTCPSocketHandler *cs;
|
2004-12-04 18:54:56 +01:00
|
|
|
int n;
|
|
|
|
fd_set read_fd, write_fd;
|
|
|
|
struct timeval tv;
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
FD_ZERO(&read_fd);
|
|
|
|
FD_ZERO(&write_fd);
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
FOR_ALL_CLIENTS(cs) {
|
2007-01-12 21:19:49 +01:00
|
|
|
FD_SET(cs->sock, &read_fd);
|
|
|
|
FD_SET(cs->sock, &write_fd);
|
2004-12-04 18:54:56 +01:00
|
|
|
}
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// take care of listener port
|
2006-10-18 01:34:12 +02:00
|
|
|
if (_network_server) FD_SET(_listensocket, &read_fd);
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
tv.tv_sec = tv.tv_usec = 0; // don't block at all.
|
|
|
|
#if !defined(__MORPHOS__) && !defined(__AMIGA__)
|
|
|
|
n = select(FD_SETSIZE, &read_fd, &write_fd, NULL, &tv);
|
|
|
|
#else
|
|
|
|
n = WaitSelect(FD_SETSIZE, &read_fd, &write_fd, NULL, &tv, NULL);
|
|
|
|
#endif
|
|
|
|
if (n == -1 && !_network_server) NetworkError(STR_NETWORK_ERR_LOSTCONNECTION);
|
2004-08-27 13:01:26 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// accept clients..
|
2006-10-18 01:34:12 +02:00
|
|
|
if (_network_server && FD_ISSET(_listensocket, &read_fd)) NetworkAcceptClients();
|
2004-09-12 16:12:33 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// read stuff from clients
|
|
|
|
FOR_ALL_CLIENTS(cs) {
|
2007-01-12 21:19:49 +01:00
|
|
|
cs->writable = !!FD_ISSET(cs->sock, &write_fd);
|
|
|
|
if (FD_ISSET(cs->sock, &read_fd)) {
|
2006-06-27 23:25:53 +02:00
|
|
|
if (_network_server) {
|
2004-12-04 18:54:56 +01:00
|
|
|
NetworkServer_ReadPackets(cs);
|
2006-06-27 23:25:53 +02:00
|
|
|
} else {
|
2006-10-18 01:34:12 +02:00
|
|
|
NetworkRecvStatus res;
|
2006-06-27 23:25:53 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// The client already was quiting!
|
2006-10-18 01:34:12 +02:00
|
|
|
if (cs->has_quit) return false;
|
2006-06-27 23:25:53 +02:00
|
|
|
|
|
|
|
res = NetworkClient_ReadPackets(cs);
|
|
|
|
if (res != NETWORK_RECV_STATUS_OKAY) {
|
2004-12-04 18:54:56 +01:00
|
|
|
// The client made an error of which we can not recover
|
|
|
|
// close the client and drop back to main menu
|
|
|
|
NetworkClientError(res, cs);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2004-09-12 16:12:33 +02:00
|
|
|
}
|
2004-08-22 12:23:37 +02:00
|
|
|
}
|
2004-12-04 18:54:56 +01:00
|
|
|
return true;
|
2004-08-22 12:23:37 +02:00
|
|
|
}
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// This sends all buffered commands (if possible)
|
|
|
|
static void NetworkSend(void)
|
2004-09-13 01:35:01 +02:00
|
|
|
{
|
2007-01-12 21:19:49 +01:00
|
|
|
NetworkTCPSocketHandler *cs;
|
2004-12-04 18:54:56 +01:00
|
|
|
FOR_ALL_CLIENTS(cs) {
|
|
|
|
if (cs->writable) {
|
2007-02-02 00:50:15 +01:00
|
|
|
cs->Send_Packets();
|
2004-12-04 18:54:56 +01:00
|
|
|
|
|
|
|
if (cs->status == STATUS_MAP) {
|
|
|
|
// This client is in the middle of a map-send, call the function for that
|
|
|
|
SEND_COMMAND(PACKET_SERVER_MAP)(cs);
|
|
|
|
}
|
2004-09-13 01:35:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// Handle the local-command-queue
|
2004-12-19 11:17:26 +01:00
|
|
|
static void NetworkHandleLocalQueue(void)
|
2004-08-22 12:23:37 +02:00
|
|
|
{
|
2005-07-16 16:29:36 +02:00
|
|
|
CommandPacket *cp, **cp_prev;
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2005-07-16 16:29:36 +02:00
|
|
|
cp_prev = &_local_command_queue;
|
|
|
|
|
|
|
|
while ( (cp = *cp_prev) != NULL) {
|
|
|
|
|
|
|
|
// The queue is always in order, which means
|
|
|
|
// that the first element will be executed first.
|
2006-10-18 01:34:12 +02:00
|
|
|
if (_frame_counter < cp->frame) break;
|
2005-07-16 16:29:36 +02:00
|
|
|
|
|
|
|
if (_frame_counter > cp->frame) {
|
|
|
|
// If we reach here, it means for whatever reason, we've already executed
|
|
|
|
// past the command we need to execute.
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(net, 0, "Trying to execute a packet in the past!");
|
2005-07-16 16:29:36 +02:00
|
|
|
assert(0);
|
2004-12-04 18:54:56 +01:00
|
|
|
}
|
2005-07-16 16:29:36 +02:00
|
|
|
|
|
|
|
// We can execute this command
|
|
|
|
NetworkExecuteCommand(cp);
|
|
|
|
|
|
|
|
*cp_prev = cp->next;
|
|
|
|
free(cp);
|
2004-09-07 00:46:02 +02:00
|
|
|
}
|
2005-07-16 16:29:36 +02:00
|
|
|
|
|
|
|
// Just a safety check, to be removed in the future.
|
|
|
|
// Make sure that no older command appears towards the end of the queue
|
|
|
|
// In that case we missed executing it. This will never happen.
|
2006-02-01 08:36:15 +01:00
|
|
|
for (cp = _local_command_queue; cp; cp = cp->next) {
|
2005-07-16 16:29:36 +02:00
|
|
|
assert(_frame_counter < cp->frame);
|
|
|
|
}
|
|
|
|
|
2004-08-22 12:23:37 +02:00
|
|
|
}
|
|
|
|
|
2004-12-19 11:17:26 +01:00
|
|
|
static bool NetworkDoClientLoop(void)
|
2004-09-12 16:12:33 +02:00
|
|
|
{
|
2004-12-04 18:54:56 +01:00
|
|
|
_frame_counter++;
|
2004-09-12 16:12:33 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
NetworkHandleLocalQueue();
|
2004-09-12 16:12:33 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
StateGameLoop();
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// Check if we are in sync!
|
|
|
|
if (_sync_frame != 0) {
|
|
|
|
if (_sync_frame == _frame_counter) {
|
|
|
|
#ifdef NETWORK_SEND_DOUBLE_SEED
|
|
|
|
if (_sync_seed_1 != _random_seeds[0][0] || _sync_seed_2 != _random_seeds[0][1]) {
|
|
|
|
#else
|
|
|
|
if (_sync_seed_1 != _random_seeds[0][0]) {
|
|
|
|
#endif
|
|
|
|
NetworkError(STR_NETWORK_ERR_DESYNC);
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(net, 0, "Sync error detected!");
|
2004-12-04 18:54:56 +01:00
|
|
|
NetworkClientError(NETWORK_RECV_STATUS_DESYNC, DEREF_CLIENT(0));
|
|
|
|
return false;
|
|
|
|
}
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// If this is the first time we have a sync-frame, we
|
|
|
|
// need to let the server know that we are ready and at the same
|
|
|
|
// frame as he is.. so we can start playing!
|
|
|
|
if (_network_first_time) {
|
|
|
|
_network_first_time = false;
|
|
|
|
SEND_COMMAND(PACKET_CLIENT_ACK)();
|
|
|
|
}
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
_sync_frame = 0;
|
|
|
|
} else if (_sync_frame < _frame_counter) {
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(net, 1, "Missed frame for sync-test (%d / %d)", _sync_frame, _frame_counter);
|
2004-12-04 18:54:56 +01:00
|
|
|
_sync_frame = 0;
|
2004-08-22 12:23:37 +02:00
|
|
|
}
|
2004-12-04 18:54:56 +01:00
|
|
|
}
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
return true;
|
2004-08-22 12:23:37 +02:00
|
|
|
}
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// We have to do some UDP checking
|
|
|
|
void NetworkUDPGameLoop(void)
|
2004-09-12 16:12:33 +02:00
|
|
|
{
|
2004-12-22 19:42:56 +01:00
|
|
|
if (_network_udp_server) {
|
2007-01-12 15:30:01 +01:00
|
|
|
_udp_server_socket->ReceivePackets();
|
|
|
|
_udp_master_socket->ReceivePackets();
|
|
|
|
} else {
|
|
|
|
_udp_client_socket->ReceivePackets();
|
2006-06-10 10:37:41 +02:00
|
|
|
if (_network_udp_broadcast > 0) _network_udp_broadcast--;
|
2007-02-01 22:04:40 +01:00
|
|
|
NetworkGameListRequery();
|
2004-08-22 12:23:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// The main loop called from ttd.c
|
|
|
|
// Here we also have to do StateGameLoop if needed!
|
|
|
|
void NetworkGameLoop(void)
|
2004-09-12 16:12:33 +02:00
|
|
|
{
|
2004-12-04 18:54:56 +01:00
|
|
|
if (!_networking) return;
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
if (!NetworkReceive()) return;
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
if (_network_server) {
|
2005-07-29 23:55:49 +02:00
|
|
|
bool send_frame = false;
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// We first increase the _frame_counter
|
|
|
|
_frame_counter++;
|
2005-07-29 23:55:49 +02:00
|
|
|
// Update max-frame-counter
|
|
|
|
if (_frame_counter > _frame_counter_max) {
|
|
|
|
_frame_counter_max = _frame_counter + _network_frame_freq;
|
|
|
|
send_frame = true;
|
|
|
|
}
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
NetworkHandleLocalQueue();
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// Then we make the frame
|
|
|
|
StateGameLoop();
|
2004-09-12 16:12:33 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
_sync_seed_1 = _random_seeds[0][0];
|
|
|
|
#ifdef NETWORK_SEND_DOUBLE_SEED
|
|
|
|
_sync_seed_2 = _random_seeds[0][1];
|
|
|
|
#endif
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2005-07-29 23:55:49 +02:00
|
|
|
NetworkServer_Tick(send_frame);
|
2004-12-04 18:54:56 +01:00
|
|
|
} else {
|
|
|
|
// Client
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
// Make sure we are at the frame were the server is (quick-frames)
|
|
|
|
if (_frame_counter_server > _frame_counter) {
|
|
|
|
while (_frame_counter_server > _frame_counter) {
|
|
|
|
if (!NetworkDoClientLoop()) break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Else, keep on going till _frame_counter_max
|
2006-10-18 01:34:12 +02:00
|
|
|
if (_frame_counter_max > _frame_counter) NetworkDoClientLoop();
|
2004-12-04 18:54:56 +01:00
|
|
|
}
|
2004-08-22 12:23:37 +02:00
|
|
|
}
|
2004-12-04 18:54:56 +01:00
|
|
|
|
|
|
|
NetworkSend();
|
2004-08-22 12:23:37 +02:00
|
|
|
}
|
|
|
|
|
2004-12-19 11:17:26 +01:00
|
|
|
static void NetworkGenerateUniqueId(void)
|
2004-12-12 17:04:32 +01:00
|
|
|
{
|
|
|
|
md5_state_t state;
|
|
|
|
md5_byte_t digest[16];
|
|
|
|
char hex_output[16*2 + 1];
|
|
|
|
char coding_string[NETWORK_NAME_LENGTH];
|
|
|
|
int di;
|
|
|
|
|
2004-12-12 18:47:50 +01:00
|
|
|
snprintf(coding_string, sizeof(coding_string), "%d%s", (uint)Random(), "OpenTTD Unique ID");
|
2004-12-12 17:04:32 +01:00
|
|
|
|
|
|
|
/* Generate the MD5 hash */
|
|
|
|
md5_init(&state);
|
2005-11-14 20:48:04 +01:00
|
|
|
md5_append(&state, (const md5_byte_t*)coding_string, strlen(coding_string));
|
2004-12-12 17:04:32 +01:00
|
|
|
md5_finish(&state, digest);
|
|
|
|
|
|
|
|
for (di = 0; di < 16; ++di)
|
|
|
|
sprintf(hex_output + di * 2, "%02x", digest[di]);
|
|
|
|
|
|
|
|
/* _network_unique_id is our id */
|
|
|
|
snprintf(_network_unique_id, sizeof(_network_unique_id), "%s", hex_output);
|
|
|
|
}
|
|
|
|
|
2007-01-04 20:12:45 +01:00
|
|
|
/** This tries to launch the network for a given OS */
|
2004-12-04 18:54:56 +01:00
|
|
|
void NetworkStartUp(void)
|
2004-09-12 16:12:33 +02:00
|
|
|
{
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(net, 3, "[core] starting network...");
|
2004-12-22 22:12:36 +01:00
|
|
|
|
2007-01-04 20:12:45 +01:00
|
|
|
/* Network is available */
|
|
|
|
_network_available = NetworkCoreInitialize();;
|
2004-12-04 18:54:56 +01:00
|
|
|
_network_dedicated = false;
|
2006-01-19 18:50:40 +01:00
|
|
|
_network_last_advertise_frame = 0;
|
|
|
|
_network_need_advertise = true;
|
2004-12-22 19:42:56 +01:00
|
|
|
_network_advertise_retries = 0;
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-13 18:47:21 +01:00
|
|
|
/* Load the ip from the openttd.cfg */
|
|
|
|
_network_server_bind_ip = inet_addr(_network_server_bind_ip_host);
|
|
|
|
/* And put the data back in it in case it was an invalid ip */
|
|
|
|
snprintf(_network_server_bind_ip_host, sizeof(_network_server_bind_ip_host), "%s", inet_ntoa(*(struct in_addr *)&_network_server_bind_ip));
|
|
|
|
|
2004-12-12 17:04:32 +01:00
|
|
|
/* Generate an unique id when there is none yet */
|
2006-01-25 19:11:06 +01:00
|
|
|
if (_network_unique_id[0] == '\0') NetworkGenerateUniqueId();
|
2004-12-12 17:04:32 +01:00
|
|
|
|
2006-08-27 12:49:43 +02:00
|
|
|
{
|
|
|
|
byte cl_max = _network_game_info.clients_max;
|
|
|
|
byte cp_max = _network_game_info.companies_max;
|
|
|
|
byte sp_max = _network_game_info.spectators_max;
|
|
|
|
|
|
|
|
memset(&_network_game_info, 0, sizeof(_network_game_info));
|
|
|
|
_network_game_info.clients_max = cl_max;
|
|
|
|
_network_game_info.companies_max = cp_max;
|
|
|
|
_network_game_info.spectators_max = sp_max;
|
|
|
|
}
|
2004-09-12 16:12:33 +02:00
|
|
|
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
NetworkInitialize();
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(net, 3, "[core] network online, multiplayer available");
|
2004-12-04 18:54:56 +01:00
|
|
|
NetworkFindIPs();
|
2004-08-22 12:23:37 +02:00
|
|
|
}
|
|
|
|
|
2007-01-04 20:12:45 +01:00
|
|
|
/** This shuts the network down */
|
2004-12-04 18:54:56 +01:00
|
|
|
void NetworkShutDown(void)
|
2004-09-12 16:12:33 +02:00
|
|
|
{
|
2006-10-12 16:48:36 +02:00
|
|
|
NetworkDisconnect();
|
2007-01-12 15:30:01 +01:00
|
|
|
NetworkUDPShutdown();
|
2006-10-12 16:48:36 +02:00
|
|
|
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(net, 3, "[core] shutting down network");
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
_network_available = false;
|
2004-08-22 12:23:37 +02:00
|
|
|
|
2007-01-04 19:50:40 +01:00
|
|
|
NetworkCoreShutdown();
|
2004-08-22 12:23:37 +02:00
|
|
|
}
|
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
#endif /* ENABLE_NETWORK */
|