diff options
author | truelight <truelight@openttd.org> | 2004-12-15 20:10:34 +0000 |
---|---|---|
committer | truelight <truelight@openttd.org> | 2004-12-15 20:10:34 +0000 |
commit | 015fb81810ebb980c2b0c726d3480a5afd6c37d3 (patch) | |
tree | 21fbe9db43b731371dcd3bd460287c68cf02686d | |
parent | d867a845fd50dc2cb47223ff8ba16ae0e9db506b (diff) | |
download | openttd-015fb81810ebb980c2b0c726d3480a5afd6c37d3.tar.xz |
(svn r1106) -Add: [Network] Added master-server protocol and advertise to
master-server option. No GUI yet, and disabled by default (it still is WIP)
-rw-r--r-- | network.c | 6 | ||||
-rw-r--r-- | network.h | 8 | ||||
-rw-r--r-- | network_data.h | 2 | ||||
-rw-r--r-- | network_server.c | 6 | ||||
-rw-r--r-- | network_udp.c | 35 | ||||
-rw-r--r-- | network_udp.h | 1 |
6 files changed, 57 insertions, 1 deletions
@@ -871,6 +871,10 @@ bool NetworkServerStart(void) 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"); + + /* Try to register us to the master server */ + _network_last_advertise_date = 0; + NetworkUDPAdvertise(); return true; } @@ -1163,6 +1167,8 @@ void NetworkStartUp(void) // Network is available _network_available = true; _network_dedicated = false; + _network_advertise = false; + _network_last_advertise_date = 0; /* Load the ip from the openttd.cfg */ _network_server_bind_ip = inet_addr(_network_server_bind_ip_host); @@ -28,6 +28,11 @@ // Do not change this next line. It should _ALWAYS_ be MAX_CLIENTS + 1 #define MAX_CLIENT_INFO (MAX_CLIENTS + 1) +/* Stuff for the master-server */ +#define NETWORK_MASTER_SERVER_PORT 3978 +#define NETWORK_MASTER_SERVER_HOST "master.openttd.org" +#define NETWORK_MASTER_SERVER_WELCOME_MESSAGE "OpenTTDRegister" + #define NETWORK_DEFAULT_PORT 3979 #define MAX_INTERFACES 9 @@ -166,6 +171,9 @@ VARDEF uint8 _network_reconnect; VARDEF bool _network_udp_server; VARDEF uint16 _network_udp_broadcast; +VARDEF bool _network_advertise; +VARDEF uint16 _network_last_advertise_date; + #endif /* ENABLE_NETWORK */ // Those variables must always be registered! diff --git a/network_data.h b/network_data.h index bd05673f3..a26db94de 100644 --- a/network_data.h +++ b/network_data.h @@ -18,6 +18,8 @@ #define NETWORK_GAME_INFO_VERSION 1 // What version of company info is this? #define NETWORK_COMPANY_INFO_VERSION 1 +// What version of master-server-protocol do we use? +#define NETWORK_MASTER_SERVER_VERSION 1 typedef uint16 PacketSize; diff --git a/network_server.c b/network_server.c index 32410cafa..29402bf4f 100644 --- a/network_server.c +++ b/network_server.c @@ -5,6 +5,7 @@ #include "table/strings.h" #include "network_server.h" +#include "network_udp.h" #include "console.h" #include "command.h" #include "gfx.h" @@ -1198,7 +1199,7 @@ void NetworkPopulateCompanyInfo(void) if (ci != NULL && ci->client_playas > 0 && ci->client_playas <= MAX_PLAYERS) { if (strlen(_network_player_info[ci->client_playas-1].players) != 0) strncat(_network_player_info[ci->client_playas-1].players, ", ", sizeof(_network_player_info[ci->client_playas-1].players)); - + strncat(_network_player_info[ci->client_playas-1].players, client_name, sizeof(_network_player_info[ci->client_playas-1].players)); } } @@ -1372,6 +1373,9 @@ void NetworkServer_Tick(void) last_sync_frame = _frame_counter; } #endif + + /* See if we need to advertise */ + NetworkUDPAdvertise(); } #endif /* ENABLE_NETWORK */ diff --git a/network_udp.c b/network_udp.c index 32a43a29a..b084186ee 100644 --- a/network_udp.c +++ b/network_udp.c @@ -19,6 +19,7 @@ typedef enum { PACKET_UDP_SERVER_RESPONSE, PACKET_UDP_CLIENT_DETAIL_INFO, PACKET_UDP_SERVER_DETAIL_INFO, // Is not used in OpenTTD itself, only for external querying + PACKET_UDP_SERVER_REGISTER, // Packet to register itself to the master server PACKET_UDP_END } PacketUDPType; @@ -218,6 +219,7 @@ static NetworkUDPPacket* const _network_udp_packet[] = { RECEIVE_COMMAND(PACKET_UDP_SERVER_RESPONSE), RECEIVE_COMMAND(PACKET_UDP_CLIENT_DETAIL_INFO), NULL, + NULL, }; // If this fails, check the array above with network_data.h @@ -467,6 +469,39 @@ void NetworkUDPQueryServer(const byte* host, unsigned short port) UpdateNetworkGameWindow(false); } +/* Register us to the master server + This function checks if it needs to send an advertise */ +void NetworkUDPAdvertise() +{ + struct sockaddr_in out_addr; + Packet *p; + + /* Check if we should send an advertise */ + if (!_networking || !_network_server || !_network_udp_server || !_network_advertise) + return; + + /* Only send once in the 450 game-days (about 15 minutes) */ + if (_network_last_advertise_date + 450 > _date) + return; + _network_last_advertise_date = _date; + + /* Find somewhere to send */ + out_addr.sin_family = AF_INET; + out_addr.sin_port = htons(NETWORK_MASTER_SERVER_PORT); + out_addr.sin_addr.s_addr = NetworkResolveHost(NETWORK_MASTER_SERVER_HOST); + + DEBUG(net, 1)("[NET][UDP] Advertising to master server"); + + /* Send the packet */ + p = NetworkSend_Init(PACKET_UDP_SERVER_REGISTER); + /* Packet is: WELCOME_MESSAGE, Version, server_port */ + NetworkSend_string(p, NETWORK_MASTER_SERVER_WELCOME_MESSAGE); + NetworkSend_uint8(p, NETWORK_MASTER_SERVER_VERSION); + NetworkSend_uint16(p, _network_server_port); + NetworkSendUDP_Packet(p, &out_addr); + free(p); +} + void NetworkUDPInitialize(void) { _udp_client_socket = INVALID_SOCKET; diff --git a/network_udp.h b/network_udp.h index 53f238b31..35524ac1c 100644 --- a/network_udp.h +++ b/network_udp.h @@ -6,5 +6,6 @@ bool NetworkUDPListen(uint32 host, uint16 port); void NetworkUDPReceive(void); void NetworkUDPSearchGame(void); void NetworkUDPQueryServer(const byte* host, unsigned short port); +void NetworkUDPAdvertise(); #endif /* NETWORK_LAN_H */ |