summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortruelight <truelight@openttd.org>2004-12-22 18:42:56 +0000
committertruelight <truelight@openttd.org>2004-12-22 18:42:56 +0000
commit457720ff9512707ef0570d3980c95f404b8e1b8e (patch)
treec7a63eb27c2ca1faec5c97597ef87fd47ba0382c
parent5b701e585d88a647a9f716c75820d335545252e7 (diff)
downloadopenttd-457720ff9512707ef0570d3980c95f404b8e1b8e.tar.xz
(svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
-Fix: UDPListener was launched wrongly -Fix: Not all sockets have broadcast enabled anymore -Fix: Server-advertise retries 3 times before giving up
-rw-r--r--lang/english.txt2
-rw-r--r--misc.c3
-rw-r--r--network.c20
-rw-r--r--network.h3
-rw-r--r--network_data.h2
-rw-r--r--network_gui.c65
-rw-r--r--network_udp.c162
-rw-r--r--network_udp.h5
-rw-r--r--settings.c1
9 files changed, 187 insertions, 76 deletions
diff --git a/lang/english.txt b/lang/english.txt
index 63e21eb2b..34821e09d 100644
--- a/lang/english.txt
+++ b/lang/english.txt
@@ -1247,6 +1247,8 @@ STR_NETWORK_SELECT_MAP_TIP :{BLACK}Which map do you want to play?
STR_NETWORK_NUMBER_OF_CLIENTS :{BLACK}Maximum allowed clients:
STR_NETWORK_NUMBER_OF_CLIENTS_TIP :{BLACK}Choose the maximum number of clients. Not all slots need to be filled
STR_NETWORK_COMBO1 :{BLACK}{SKIP}{SKIP}{SKIP}{SKIP}{SKIP}{SKIP}{SKIP}{STRING}
+STR_NETWORK_LAN :LAN
+STR_NETWORK_INTERNET :Internet
STR_NETWORK_LAN_INTERNET :LAN / Internet
STR_NETWORK_INTERNET_ADVERTISE :Internet (advertise)
STR_NETWORK_COMBO2 :{BLACK}{SKIP}{SKIP}{SKIP}{SKIP}{SKIP}{SKIP}{SKIP}{SKIP}{SKIP}{STRING}
diff --git a/misc.c b/misc.c
index a47b43a44..8ce6e8ce0 100644
--- a/misc.c
+++ b/misc.c
@@ -99,6 +99,9 @@ void SetDate(uint date)
ConvertDayToYMD(&ymd, _date = date);
_cur_year = ymd.year;
_cur_month = ymd.month;
+#ifdef ENABLE_NETWORK
+ _network_last_advertise_date = 0;
+#endif /* ENABLE_NETWORK */
}
diff --git a/network.c b/network.c
index 0d1daffe9..c5a9b21d4 100644
--- a/network.c
+++ b/network.c
@@ -747,7 +747,6 @@ static void NetworkClose(void)
static void NetworkInitialize(void)
{
NetworkClientState *cs;
- uint i;
_local_command_queue = NULL;
@@ -772,12 +771,6 @@ static void NetworkInitialize(void)
InitPlayerRandoms();
NetworkUDPInitialize();
-
- // add all servers from the config file to our list
- for (i=0; i != lengthof(_network_host_list); i++) {
- if (_network_host_list[i] == NULL) break;
- NetworkAddServer(_network_host_list[i]);
- }
}
// Query a server to fetch his game-info
@@ -946,7 +939,7 @@ bool NetworkServerStart(void)
// Try to start UDP-server
_network_udp_server = true;
- _network_udp_server = NetworkUDPListen(_network_server_bind_ip, _network_server_port);
+ _network_udp_server = NetworkUDPListen(&_udp_server_socket, _network_server_bind_ip, _network_server_port, false);
_network_server = true;
_networking = true;
@@ -1179,10 +1172,14 @@ static bool NetworkDoClientLoop(void)
// We have to do some UDP checking
void NetworkUDPGameLoop(void)
{
- if (_network_udp_server)
- NetworkUDPReceive();
+ if (_network_udp_server) {
+ NetworkUDPReceive(_udp_server_socket);
+ if (_udp_master_socket != INVALID_SOCKET) {
+ NetworkUDPReceive(_udp_master_socket);
+ }
+ }
else if (_udp_client_socket != INVALID_SOCKET) {
- NetworkUDPReceive();
+ NetworkUDPReceive(_udp_client_socket);
if (_network_udp_broadcast > 0)
_network_udp_broadcast--;
}
@@ -1260,6 +1257,7 @@ void NetworkStartUp(void)
_network_available = true;
_network_dedicated = false;
_network_last_advertise_date = 0;
+ _network_advertise_retries = 0;
/* Load the ip from the openttd.cfg */
_network_server_bind_ip = inet_addr(_network_server_bind_ip_host);
diff --git a/network.h b/network.h
index 6494d2254..e66639133 100644
--- a/network.h
+++ b/network.h
@@ -176,8 +176,11 @@ VARDEF uint8 _network_reconnect;
VARDEF bool _network_udp_server;
VARDEF uint16 _network_udp_broadcast;
+VARDEF byte _network_lan_internet;
+
VARDEF bool _network_advertise;
VARDEF uint16 _network_last_advertise_date;
+VARDEF uint8 _network_advertise_retries;
VARDEF bool _network_autoclean_companies;
VARDEF uint8 _network_autoclean_unprotected; // Remove a company after X months
diff --git a/network_data.h b/network_data.h
index e68e61d3a..b74b56d0f 100644
--- a/network_data.h
+++ b/network_data.h
@@ -170,6 +170,8 @@ typedef enum {
CommandPacket *_local_command_queue;
SOCKET _udp_client_socket; // udp client socket
+SOCKET _udp_server_socket; // udp server socket
+SOCKET _udp_master_socket; // udp master socket
// Here we keep track of the clients
// (and the client uses [0] for his own communication)
diff --git a/network_gui.c b/network_gui.c
index a72e90164..144552aaf 100644
--- a/network_gui.c
+++ b/network_gui.c
@@ -27,6 +27,7 @@ static void ShowNetworkStartServerWindow(void);
static void ShowNetworkLobbyWindow(void);
static byte _selected_field;
+static bool _first_time_show_network_game_window = true;
static const StringID _connection_types_dropdown[] = {
STR_NETWORK_LAN_INTERNET,
@@ -34,6 +35,12 @@ static const StringID _connection_types_dropdown[] = {
INVALID_STRING_ID
};
+static const StringID _lan_internet_types_dropdown[] = {
+ STR_NETWORK_LAN,
+ STR_NETWORK_INTERNET,
+ INVALID_STRING_ID
+};
+
static StringID _str_map_name, _str_game_name, _str_server_version, _str_server_address;
enum {
@@ -76,24 +83,26 @@ static void NetworkGameWindowWndProc(Window *w, WindowEvent *e)
w->disabled_state = 0;
if (_selected_item == NULL) {
- SETBIT(w->disabled_state, 15); SETBIT(w->disabled_state, 16);
+ SETBIT(w->disabled_state, 17); SETBIT(w->disabled_state, 18);
} else if (!_selected_item->online) {
- SETBIT(w->disabled_state, 15); // Server offline, join button disabled
+ SETBIT(w->disabled_state, 17); // Server offline, join button disabled
} else if (_selected_item->info.clients_on == _selected_item->info.clients_max) {
- SETBIT(w->disabled_state, 15); // Server full, join button disabled
+ SETBIT(w->disabled_state, 17); // Server full, join button disabled
// revisions don't match, check if server has no revision; then allow connection
} else if (strncmp(_selected_item->info.server_revision, _openttd_revision, NETWORK_REVISION_LENGTH - 1) != 0) {
if (strncmp(_selected_item->info.server_revision, NOREV_STRING, sizeof(_selected_item->info.server_revision)) != 0)
- SETBIT(w->disabled_state, 15); // Revision mismatch, join button disabled
+ SETBIT(w->disabled_state, 17); // Revision mismatch, join button disabled
}
SetDParam(0, 0x00);
+ SetDParam(7, _lan_internet_types_dropdown[_network_lan_internet]);
DrawWindowWidgets(w);
DrawEditBox(w, 3);
DrawString(9, 23, STR_NETWORK_PLAYER_NAME, 2);
+ DrawString(9, 43, STR_NETWORK_CONNECTION, 2);
DrawString(15, 63, STR_NETWORK_GAME_NAME, 2);
DrawString(135, 63, STR_NETWORK_CLIENTS_CAPTION, 2);
@@ -211,10 +220,13 @@ static void NetworkGameWindowWndProc(Window *w, WindowEvent *e)
case WE_CLICK:
_selected_field = e->click.widget;
switch(e->click.widget) {
- case 0: case 12: /* Close 'X' | Cancel button */
+ case 0: case 14: /* Close 'X' | Cancel button */
DeleteWindowById(WC_NETWORK_WINDOW, 0);
break;
- case 8: { /* Matrix to show networkgames */
+ case 4: case 5:
+ ShowDropDownMenu(w, _lan_internet_types_dropdown, _network_lan_internet, 5, 0); // do it for widget 5
+ break;
+ case 10: { /* Matrix to show networkgames */
uint32 id_v = (e->click.pt.y - NET_PRC__OFFSET_TOP_WIDGET) / NET_PRC__SIZE_OF_ROW;
if (id_v >= w->vscroll.cap) { return;} // click out of bounds
@@ -259,10 +271,13 @@ static void NetworkGameWindowWndProc(Window *w, WindowEvent *e)
}
SetWindowDirty(w);
} break;
- case 9: /* Find server automatically */
- NetworkUDPSearchGame();
+ case 11: /* Find server automatically */
+ switch (_network_lan_internet) {
+ case 0: NetworkUDPSearchGame(); break;
+ case 1: NetworkUDPQueryMasterServer(); break;
+ }
break;
- case 10: { // Add a server
+ case 12: { // Add a server
StringID str = AllocateName((byte*)_network_default_ip, 0);
ShowQueryString(
@@ -274,10 +289,10 @@ static void NetworkGameWindowWndProc(Window *w, WindowEvent *e)
w->window_number);
DeleteName(str);
} break;
- case 11: /* Start server */
+ case 13: /* Start server */
ShowNetworkStartServerWindow();
break;
- case 15: /* Join Game */
+ case 17: /* Join Game */
if (_selected_item != NULL) {
memcpy(&_network_game_info, &_selected_item->info, sizeof(NetworkGameInfo));
snprintf(_network_last_host, sizeof(_network_last_host), "%s", inet_ntoa(*(struct in_addr *)&_selected_item->ip));
@@ -285,7 +300,7 @@ static void NetworkGameWindowWndProc(Window *w, WindowEvent *e)
ShowNetworkLobbyWindow();
}
break;
- case 16: // Refresh
+ case 18: // Refresh
if (_selected_item != NULL) {
NetworkQueryServer(_selected_item->info.hostname, _selected_item->port, true);
}
@@ -293,6 +308,16 @@ static void NetworkGameWindowWndProc(Window *w, WindowEvent *e)
} break;
+ case WE_DROPDOWN_SELECT: /* we have selected a dropdown item in the list */
+ switch(e->dropdown.button) {
+ case 5:
+ _network_lan_internet = e->dropdown.index;
+ break;
+ }
+
+ SetWindowDirty(w);
+ break;
+
case WE_MOUSELOOP:
if (_selected_field == 3)
HandleEditBox(w, 3);
@@ -314,7 +339,7 @@ static void NetworkGameWindowWndProc(Window *w, WindowEvent *e)
switch (HandleEditBoxKey(w, 3, e)) {
case 1:
- HandleButtonClick(w, 8);
+ HandleButtonClick(w, 10);
break;
}
@@ -345,6 +370,9 @@ static const Widget _network_game_window_widgets[] = {
/* LEFT SIDE */
{ WWT_IMGBTN, BGC, 90, 230, 22, 33, 0x0, STR_NETWORK_ENTER_NAME_TIP},
+{ WWT_6, BGC, 90, 230, 42, 53, STR_NETWORK_COMBO1, STR_NETWORK_CONNECTION_TIP},
+{ WWT_CLOSEBOX, BGC, 219, 229, 43, 52, STR_0225, STR_NETWORK_CONNECTION_TIP},
+
{ WWT_SCROLLBAR, BGC, 220, 230, 62, 185, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST},
{ WWT_IMGBTN, BTC, 10, 130, 62, 73, 0x0, STR_NETWORK_GAME_NAME_TIP },
@@ -380,9 +408,20 @@ static FiosItem *selected_map = NULL; // to highlight slected map
void ShowNetworkGameWindow()
{
+ uint i;
Window *w;
DeleteWindowById(WC_NETWORK_WINDOW, 0);
+ /* Only show once */
+ if (_first_time_show_network_game_window) {
+ _first_time_show_network_game_window = false;
+ // add all servers from the config file to our list
+ for (i=0; i != lengthof(_network_host_list); i++) {
+ if (_network_host_list[i] == NULL) break;
+ NetworkAddServer(_network_host_list[i]);
+ }
+ }
+
w = AllocateWindowDesc(&_network_game_window_desc);
ttd_strlcpy(_edit_str_buf, _network_player_name, MAX_QUERYSTR_LEN);
w->vscroll.cap = 8;
diff --git a/network_udp.c b/network_udp.c
index 03884591e..31541b632 100644
--- a/network_udp.c
+++ b/network_udp.c
@@ -4,6 +4,7 @@
#ifdef ENABLE_NETWORK
#include "network_gamelist.h"
+#include "network_udp.h"
extern void UpdateNetworkGameWindow(bool unselect);
extern void NetworkPopulateCompanyInfo(void);
@@ -20,13 +21,20 @@ typedef enum {
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_MASTER_ACK_REGISTER, // Packet indicating registration has succedeed
+ PACKET_UDP_CLIENT_GET_LIST, // Request for serverlist from master server
+ PACKET_UDP_MASTER_RESPONSE_LIST, // Response from master server with server ip's + port's
PACKET_UDP_END
} PacketUDPType;
-static SOCKET _udp_server_socket; // udp server socket
+enum {
+ ADVERTISE_NORMAL_INTERVAL = 450, // interval between advertising in days
+ ADVERTISE_RETRY_INTERVAL = 5, // readvertise when no response after this amount of days
+ ADVERTISE_RETRY_TIMES = 3 // give up readvertising after this much failed retries
+};
#define DEF_UDP_RECEIVE_COMMAND(type) void NetworkPacketReceive_ ## type ## _command(Packet *p, struct sockaddr_in *client_addr)
-void NetworkSendUDP_Packet(Packet *p, struct sockaddr_in *recv);
+void NetworkSendUDP_Packet(SOCKET udp, Packet *p, struct sockaddr_in *recv);
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER)
{
@@ -58,7 +66,7 @@ DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER)
NetworkSend_uint8 (packet, _network_game_info.dedicated);
// Let the client know that we are here
- NetworkSendUDP_Packet(packet, client_addr);
+ NetworkSendUDP_Packet(_udp_server_socket, packet, client_addr);
free(packet);
@@ -76,6 +84,9 @@ DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_RESPONSE)
game_info_version = NetworkRecv_uint8(p);
+
+ DEBUG(net, 6)("[NET][UDP] Server response from %s:%d", inet_ntoa(client_addr->sin_addr),ntohs(client_addr->sin_port));
+
// Find next item
item = NetworkGameListAddItem(inet_addr(inet_ntoa(client_addr->sin_addr)), ntohs(client_addr->sin_port));
@@ -205,11 +216,39 @@ DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_DETAIL_INFO)
/* Indicates end of client list */
NetworkSend_uint8(packet, 0);
- NetworkSendUDP_Packet(packet, client_addr);
+ NetworkSendUDP_Packet(_udp_server_socket, packet, client_addr);
free(packet);
}
+DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_RESPONSE_LIST) {
+ int i;
+ struct in_addr ip;
+ uint16 port;
+ uint8 ver;
+
+ /* packet begins with the protocol version (uint8)
+ * then an uint16 which indicates how many
+ * ip:port pairs are in this packet, after that
+ * an uint32 (ip) and an uint16 (port) for each pair
+ */
+
+ ver = NetworkRecv_uint8(p);
+
+ if (ver == 1) {
+ for (i = NetworkRecv_uint16(p); i != 0 ; i--) {
+ ip.s_addr = TO_LE32(NetworkRecv_uint32(p));
+ port = NetworkRecv_uint16(p);
+ NetworkUDPQueryServer(inet_ntoa(ip), port);
+ }
+ }
+}
+
+DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_ACK_REGISTER) {
+ _network_advertise_retries = 0;
+ DEBUG(net, 2)("[NET] We are advertised on the master-server!");
+}
+
// The layout for the receive-functions by UDP
typedef void NetworkUDPPacket(Packet *p, struct sockaddr_in *client_addr);
@@ -220,8 +259,12 @@ static NetworkUDPPacket* const _network_udp_packet[] = {
RECEIVE_COMMAND(PACKET_UDP_CLIENT_DETAIL_INFO),
NULL,
NULL,
+ RECEIVE_COMMAND(PACKET_UDP_MASTER_ACK_REGISTER),
+ NULL,
+ RECEIVE_COMMAND(PACKET_UDP_MASTER_RESPONSE_LIST)
};
+
// If this fails, check the array above with network_data.h
assert_compile(lengthof(_network_udp_packet) == PACKET_UDP_END);
@@ -241,17 +284,10 @@ void NetworkHandleUDPPacket(Packet *p, struct sockaddr_in *client_addr)
// Send a packet over UDP
-void NetworkSendUDP_Packet(Packet *p, struct sockaddr_in *recv)
+void NetworkSendUDP_Packet(SOCKET udp, Packet *p, struct sockaddr_in *recv)
{
- SOCKET udp;
int res;
- // Find the correct socket
- if (_network_udp_server)
- udp = _udp_server_socket;
- else
- udp = _udp_client_socket;
-
// Put the length in the buffer
p->buffer[0] = p->size & 0xFF;
p->buffer[1] = p->size >> 8;
@@ -266,19 +302,15 @@ void NetworkSendUDP_Packet(Packet *p, struct sockaddr_in *recv)
}
// Start UDP listener
-bool NetworkUDPListen(uint32 host, uint16 port)
+bool NetworkUDPListen(SOCKET *udp, uint32 host, uint16 port, bool broadcast)
{
struct sockaddr_in sin;
- SOCKET udp;
- // Make sure sockets are closed
- if (_network_udp_server)
- closesocket(_udp_server_socket);
- else
- closesocket(_udp_client_socket);
+ // Make sure socket is closed
+ closesocket(*udp);
- udp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- if (udp == INVALID_SOCKET) {
+ *udp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+ if (*udp == INVALID_SOCKET) {
DEBUG(net, 1)("[NET][UDP] Failed to start UDP support");
return false;
}
@@ -286,7 +318,7 @@ bool NetworkUDPListen(uint32 host, uint16 port)
// set nonblocking mode for socket
{
unsigned long blocking = 1;
- ioctlsocket(udp, FIONBIO, &blocking);
+ ioctlsocket(*udp, FIONBIO, &blocking);
}
sin.sin_family = AF_INET;
@@ -294,25 +326,17 @@ bool NetworkUDPListen(uint32 host, uint16 port)
sin.sin_addr.s_addr = host;
sin.sin_port = htons(port);
- if (bind(udp, (struct sockaddr*)&sin, sizeof(sin)) != 0) {
+ if (bind(*udp, (struct sockaddr*)&sin, sizeof(sin)) != 0) {
DEBUG(net, 1) ("[NET][UDP] error: bind failed on %s:%i", inet_ntoa(*(struct in_addr *)&host), port);
return false;
}
- // enable broadcasting
- // allow reusing
- {
+ if (broadcast) {
+ /* Enable broadcast */
unsigned long val = 1;
- setsockopt(udp, SOL_SOCKET, SO_BROADCAST, (char *) &val , sizeof(val));
- val = 1;
- setsockopt(udp, SOL_SOCKET, SO_REUSEADDR, (char *) &val , sizeof(val));
+ setsockopt(*udp, SOL_SOCKET, SO_BROADCAST, (char *) &val , sizeof(val));
}
- if (_network_udp_server)
- _udp_server_socket = udp;
- else
- _udp_client_socket = udp;
-
DEBUG(net, 1)("[NET][UDP] Listening on port %s:%d", inet_ntoa(*(struct in_addr *)&host), port);
return true;
@@ -321,11 +345,15 @@ bool NetworkUDPListen(uint32 host, uint16 port)
// Close UDP connection
void NetworkUDPClose(void)
{
- DEBUG(net, 1) ("[NET][UDP] Closed listener");
+ DEBUG(net, 1) ("[NET][UDP] Closed listeners");
if (_network_udp_server) {
closesocket(_udp_server_socket);
_udp_server_socket = INVALID_SOCKET;
+
+ closesocket(_udp_master_socket);
+ _udp_master_socket = INVALID_SOCKET;
+
_network_udp_server = false;
_network_udp_broadcast = 0;
} else {
@@ -336,7 +364,7 @@ void NetworkUDPClose(void)
}
// Receive something on UDP level
-void NetworkUDPReceive(void)
+void NetworkUDPReceive(SOCKET udp)
{
struct sockaddr_in client_addr;
#ifndef __MORPHOS__
@@ -347,12 +375,6 @@ void NetworkUDPReceive(void)
int nbytes;
static Packet *p = NULL;
int packet_len;
- SOCKET udp;
-
- if (_network_udp_server)
- udp = _udp_server_socket;
- else
- udp = _udp_client_socket;
// If p is NULL, malloc him.. this prevents unneeded mallocs
if (p == NULL)
@@ -383,7 +405,7 @@ void NetworkUDPReceive(void)
}
// Broadcast to all ips
-void NetworkUDPBroadCast(void)
+void NetworkUDPBroadCast(SOCKET udp)
{
int i;
struct sockaddr_in out_addr;
@@ -408,10 +430,35 @@ void NetworkUDPBroadCast(void)
out_addr.sin_port = htons(_network_server_port);
out_addr.sin_addr.s_addr = bcaddr;
- NetworkSendUDP_Packet(p, &out_addr);
+ NetworkSendUDP_Packet(udp, p, &out_addr);
i++;
}
+}
+
+
+// Request the the server-list from the master server
+void NetworkUDPQueryMasterServer(void)
+{
+ struct sockaddr_in out_addr;
+ Packet *p;
+
+ if (_udp_client_socket == INVALID_SOCKET)
+ if (!NetworkUDPListen(&_udp_client_socket, 0, 0, true))
+ return;
+
+ p = NetworkSend_Init(PACKET_UDP_CLIENT_GET_LIST);
+
+ 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);
+
+ // packet only contains protocol version
+ NetworkSend_uint8(p, NETWORK_MASTER_SERVER_VERSION);
+
+ NetworkSendUDP_Packet(_udp_client_socket, p, &out_addr);
+
+ DEBUG(net, 2)("[NET][UDP] Queried Master Server at %s:%d", inet_ntoa(out_addr.sin_addr),ntohs(out_addr.sin_port));
free(p);
}
@@ -425,12 +472,12 @@ void NetworkUDPSearchGame(void)
// No UDP-socket yet..
if (_udp_client_socket == INVALID_SOCKET)
- if (!NetworkUDPListen(0, 0))
+ if (!NetworkUDPListen(&_udp_client_socket, 0, 0, true))
return;
DEBUG(net, 0)("[NET][UDP] Searching server");
- NetworkUDPBroadCast();
+ NetworkUDPBroadCast(_udp_client_socket);
_network_udp_broadcast = 300; // Stay searching for 300 ticks
}
@@ -443,7 +490,7 @@ NetworkGameList *NetworkUDPQueryServer(const byte* host, unsigned short port)
// No UDP-socket yet..
if (_udp_client_socket == INVALID_SOCKET)
- if (!NetworkUDPListen(0, 0))
+ if (!NetworkUDPListen(&_udp_client_socket, 0, 0, true))
return NULL;
ttd_strlcpy(hostname, host, sizeof(hostname));
@@ -462,7 +509,7 @@ NetworkGameList *NetworkUDPQueryServer(const byte* host, unsigned short port)
// Init the packet
p = NetworkSend_Init(PACKET_UDP_CLIENT_FIND_SERVER);
- NetworkSendUDP_Packet(p, &out_addr);
+ NetworkSendUDP_Packet(_udp_client_socket, p, &out_addr);
free(p);
@@ -481,9 +528,22 @@ void NetworkUDPAdvertise(void)
if (!_networking || !_network_server || !_network_udp_server || !_network_advertise)
return;
+ /* check for socket */
+ if (_udp_master_socket == INVALID_SOCKET)
+ if (!NetworkUDPListen(&_udp_master_socket, 0, 0, false))
+ return;
+
/* Only send once in the 450 game-days (about 15 minutes) */
- if (_network_last_advertise_date + 450 > _date)
+ if (_network_advertise_retries == 0) {
+ if ( (_network_last_advertise_date + ADVERTISE_NORMAL_INTERVAL) > _date)
+ return;
+ _network_advertise_retries = ADVERTISE_RETRY_TIMES;
+ }
+
+ if ( (_network_last_advertise_date + ADVERTISE_RETRY_INTERVAL) > _date)
return;
+
+ _network_advertise_retries--;
_network_last_advertise_date = _date;
/* Find somewhere to send */
@@ -499,7 +559,8 @@ void NetworkUDPAdvertise(void)
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);
+ NetworkSendUDP_Packet(_udp_master_socket, p, &out_addr);
+
free(p);
}
@@ -507,6 +568,7 @@ void NetworkUDPInitialize(void)
{
_udp_client_socket = INVALID_SOCKET;
_udp_server_socket = INVALID_SOCKET;
+ _udp_master_socket = INVALID_SOCKET;
_network_udp_server = false;
_network_udp_broadcast = 0;
diff --git a/network_udp.h b/network_udp.h
index 03ff36579..43ca8f999 100644
--- a/network_udp.h
+++ b/network_udp.h
@@ -2,9 +2,10 @@
#define NETWORK_LAN_H
void NetworkUDPInitialize(void);
-bool NetworkUDPListen(uint32 host, uint16 port);
-void NetworkUDPReceive(void);
+bool NetworkUDPListen(SOCKET *udp, uint32 host, uint16 port, bool broadcast);
+void NetworkUDPReceive(SOCKET udp);
void NetworkUDPSearchGame(void);
+void NetworkUDPQueryMasterServer(void);
NetworkGameList *NetworkUDPQueryServer(const byte* host, unsigned short port);
void NetworkUDPAdvertise(void);
diff --git a/settings.c b/settings.c
index e6351e509..84572ad00 100644
--- a/settings.c
+++ b/settings.c
@@ -757,6 +757,7 @@ static const SettingDesc network_settings[] = {
{"server_bind_ip", SDT_STRINGBUF | (lengthof(_network_server_bind_ip_host) << 16), "0.0.0.0", &_network_server_bind_ip_host, NULL},
{"server_port", SDT_UINT, (void*)NETWORK_DEFAULT_PORT, &_network_server_port, NULL},
{"server_advertise",SDT_BOOL, (void*)false, &_network_advertise, NULL},
+ {"lan_internet", SDT_UINT8, (void*)0, &_network_lan_internet, NULL},
{"player_name", SDT_STRINGBUF | (lengthof(_network_player_name) << 16), NULL, &_network_player_name, NULL},
{"server_password", SDT_STRINGBUF | (lengthof(_network_game_info.server_password) << 16), NULL, &_network_game_info.server_password, NULL},
{"server_name", SDT_STRINGBUF | (lengthof(_network_server_name) << 16), NULL, &_network_server_name, NULL},