(svn r1194) Feature: You can now add and remove servers from the server list. Those will be remembered until you delete them by pressing the Delete key.

This commit is contained in:
dominik 2004-12-20 22:14:39 +00:00
parent 3bbea05a93
commit 7cfaa8a418
9 changed files with 107 additions and 24 deletions

View File

@ -160,7 +160,6 @@ void NetworkGameLoop(void);
void NetworkUDPGameLoop(void); void NetworkUDPGameLoop(void);
bool NetworkServerStart(void); bool NetworkServerStart(void);
bool NetworkClientConnectGame(const byte* host, unsigned short port); bool NetworkClientConnectGame(const byte* host, unsigned short port);
void NetworkQueryServer(const byte* host, unsigned short port, bool game_info);
void NetworkReboot(); void NetworkReboot();
void NetworkDisconnect(); void NetworkDisconnect();
void NetworkSend_Command(uint32 tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback); void NetworkSend_Command(uint32 tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback);

View File

@ -774,24 +774,23 @@ static void NetworkInitialize(void)
NetworkUDPInitialize(); NetworkUDPInitialize();
// add all servers from the config file to our list // add all servers from the config file to our list
for (i=0; i != lengthof(_network_server_list); i++) { for (i=0; i != lengthof(_network_host_list); i++) {
if (_network_server_list[i] == NULL) break; if (_network_host_list[i] == NULL) break;
NetworkAddServer(_network_server_list[i]); NetworkAddServer(_network_host_list[i]);
} }
} }
// Query a server to fetch his game-info // Query a server to fetch his game-info
// If game_info is true, only the gameinfo is fetched, // If game_info is true, only the gameinfo is fetched,
// else only the client_info is fetched // else only the client_info is fetched
void NetworkQueryServer(const byte* host, unsigned short port, bool game_info) NetworkGameList *NetworkQueryServer(const byte* host, unsigned short port, bool game_info)
{ {
if (!_network_available) return; if (!_network_available) return NULL;
NetworkDisconnect(); NetworkDisconnect();
if (game_info) { if (game_info) {
NetworkUDPQueryServer(host, port); return NetworkUDPQueryServer(host, port);
return;
} }
NetworkInitialize(); NetworkInitialize();
@ -807,18 +806,22 @@ void NetworkQueryServer(const byte* host, unsigned short port, bool game_info)
// We are connected // We are connected
if (_networking) { if (_networking) {
SEND_COMMAND(PACKET_CLIENT_COMPANY_INFO)(); SEND_COMMAND(PACKET_CLIENT_COMPANY_INFO)();
return; return NULL;
} }
// No networking, close everything down again // No networking, close everything down again
NetworkDisconnect(); NetworkDisconnect();
return NULL;
} }
// validates an address entered as a string and adds the server to /* Validates an address entered as a string and adds the server to
// the list * the list. If you use this functions, the games will be marked
* as manually added. */
void NetworkAddServer(const byte *b) void NetworkAddServer(const byte *b)
{ {
if (*b != '\0') { if (*b != '\0') {
NetworkGameList *item;
uint i;
const byte *port = NULL; const byte *port = NULL;
const byte *player = NULL; const byte *player = NULL;
byte host[NETWORK_HOSTNAME_LENGTH]; byte host[NETWORK_HOSTNAME_LENGTH];
@ -834,7 +837,26 @@ void NetworkAddServer(const byte *b)
if (player != NULL) _network_playas = atoi(player); if (player != NULL) _network_playas = atoi(player);
if (port != NULL) rport = atoi(port); if (port != NULL) rport = atoi(port);
NetworkQueryServer(host, rport, true); item = NetworkQueryServer(host, rport, true);
item->manually = true;
}
}
/* Generates the list of manually added hosts from NetworkGameList and
* dumps them into the array _network_host_list. This array is needed
* by the function that generates the config file. */
void NetworkRebuildHostList()
{
int i=0;
NetworkGameList *item = _network_game_list;
while (item != NULL && i!=lengthof(_network_host_list)) {
if (item->manually)
_network_host_list[i++] = strdup(item->info.hostname);
item = item->next;
}
for (; i<lengthof(_network_host_list); i++) {
_network_host_list[i] = strdup("");
} }
} }

View File

@ -101,6 +101,7 @@ typedef struct NetworkGameList {
uint32 ip; uint32 ip;
uint16 port; uint16 port;
bool online; // False if the server did not respond (default status) bool online; // False if the server did not respond (default status)
bool manually; // True if the server was added manually
struct NetworkGameList *next; struct NetworkGameList *next;
} NetworkGameList; } NetworkGameList;
@ -132,7 +133,7 @@ VARDEF NetworkClientInfo _network_client_info[MAX_CLIENT_INFO];
VARDEF char _network_player_name[NETWORK_NAME_LENGTH]; VARDEF char _network_player_name[NETWORK_NAME_LENGTH];
VARDEF char _network_default_ip[NETWORK_HOSTNAME_LENGTH]; VARDEF char _network_default_ip[NETWORK_HOSTNAME_LENGTH];
#define MAX_SAVED_SERVERS 10 #define MAX_SAVED_SERVERS 10
VARDEF char *_network_server_list[MAX_SAVED_SERVERS]; VARDEF char *_network_host_list[MAX_SAVED_SERVERS];
VARDEF uint16 _network_own_client_index; VARDEF uint16 _network_own_client_index;
VARDEF char _network_unique_id[NETWORK_NAME_LENGTH]; // Our own unique ID VARDEF char _network_unique_id[NETWORK_NAME_LENGTH]; // Our own unique ID
@ -194,5 +195,7 @@ VARDEF byte _network_playas; // an id to play as..
void ParseConnectionString(const byte **player, const byte **port, byte *connection_string); void ParseConnectionString(const byte **player, const byte **port, byte *connection_string);
void NetworkUpdateClientInfo(uint16 client_index); void NetworkUpdateClientInfo(uint16 client_index);
void NetworkAddServer(const byte *b); void NetworkAddServer(const byte *b);
void NetworkRebuildHostList();
NetworkGameList *NetworkQueryServer(const byte* host, unsigned short port, bool game_info);
#endif /* NETWORK_H */ #endif /* NETWORK_H */

View File

@ -65,6 +65,33 @@ NetworkGameList *NetworkGameListAddItem(uint32 ip, uint16 port)
return item; return item;
} }
void NetworkGameListRemoveItem(NetworkGameList *remove)
{
NetworkGameList *item;
item = _network_game_list;
// examine head of the list
if ( remove == _network_game_list ) {
_network_game_list = remove->next;
free(remove);
DEBUG(net, 4) ("[NET][GameList] Removed server from list");
return;
}
// examine each item
while ( item->next != NULL ) {
if ( item->next == remove )
{
item->next = remove->next;
free(remove);
DEBUG(net, 4) ("[NET][GameList] Removed server from list");
return;
}
item = item->next;
}
}
void NetworkGameListAddQueriedItem(const NetworkGameInfo *info, bool server_online) void NetworkGameListAddQueriedItem(const NetworkGameInfo *info, bool server_online)
{ {
// We queried a server and now we are going to add it to the list // We queried a server and now we are going to add it to the list

View File

@ -3,6 +3,7 @@
void NetworkGameListClear(void); void NetworkGameListClear(void);
NetworkGameList *NetworkGameListAddItem(uint32 ip, uint16 port); NetworkGameList *NetworkGameListAddItem(uint32 ip, uint16 port);
void NetworkGameListRemoveItem(NetworkGameList *remove);
void NetworkGameListAddQueriedItem(const NetworkGameInfo *info, bool server_online); void NetworkGameListAddQueriedItem(const NetworkGameInfo *info, bool server_online);
#endif /* NETWORK_GAMELIST_H */ #endif /* NETWORK_GAMELIST_H */

View File

@ -9,6 +9,7 @@
#include "table/strings.h" #include "table/strings.h"
#include "network_data.h" #include "network_data.h"
#include "network_gamelist.h"
#include "window.h" #include "window.h"
#include "gui.h" #include "gui.h"
#include "gfx.h" #include "gfx.h"
@ -299,13 +300,15 @@ static void NetworkGameWindowWndProc(Window *w, WindowEvent *e)
break; break;
case WE_KEYPRESS: case WE_KEYPRESS:
if (_selected_field != 3) if (_selected_field != 3) {
break; if ( e->keypress.keycode == WKC_DELETE ) { // press 'delete' to remove servers
if (_selected_item != NULL && _selected_item->manually) {
switch (HandleEditBoxKey(w, 3, e)) { NetworkGameListRemoveItem(_selected_item);
case 1: NetworkRebuildHostList();
HandleButtonClick(w, 8); SetWindowDirty(w);
break; _selected_item = NULL;
}
}
} }
// The name is only allowed when it starts with a letter! // The name is only allowed when it starts with a letter!
@ -318,6 +321,7 @@ static void NetworkGameWindowWndProc(Window *w, WindowEvent *e)
case WE_ON_EDIT_TEXT: { case WE_ON_EDIT_TEXT: {
NetworkAddServer(e->edittext.str); NetworkAddServer(e->edittext.str);
NetworkRebuildHostList();
} break; } break;
case WE_CREATE: { case WE_CREATE: {

View File

@ -434,7 +434,7 @@ void NetworkUDPSearchGame(void)
_network_udp_broadcast = 300; // Stay searching for 300 ticks _network_udp_broadcast = 300; // Stay searching for 300 ticks
} }
void NetworkUDPQueryServer(const byte* host, unsigned short port) NetworkGameList *NetworkUDPQueryServer(const byte* host, unsigned short port)
{ {
struct sockaddr_in out_addr; struct sockaddr_in out_addr;
Packet *p; Packet *p;
@ -444,7 +444,7 @@ void NetworkUDPQueryServer(const byte* host, unsigned short port)
// No UDP-socket yet.. // No UDP-socket yet..
if (_udp_client_socket == INVALID_SOCKET) if (_udp_client_socket == INVALID_SOCKET)
if (!NetworkUDPListen(0, 0)) if (!NetworkUDPListen(0, 0))
return; return NULL;
ttd_strlcpy(hostname, host, sizeof(hostname)); ttd_strlcpy(hostname, host, sizeof(hostname));
@ -467,6 +467,7 @@ void NetworkUDPQueryServer(const byte* host, unsigned short port)
free(p); free(p);
UpdateNetworkGameWindow(false); UpdateNetworkGameWindow(false);
return item;
} }
/* Register us to the master server /* Register us to the master server

View File

@ -5,7 +5,7 @@ void NetworkUDPInitialize(void);
bool NetworkUDPListen(uint32 host, uint16 port); bool NetworkUDPListen(uint32 host, uint16 port);
void NetworkUDPReceive(void); void NetworkUDPReceive(void);
void NetworkUDPSearchGame(void); void NetworkUDPSearchGame(void);
void NetworkUDPQueryServer(const byte* host, unsigned short port); NetworkGameList *NetworkUDPQueryServer(const byte* host, unsigned short port);
void NetworkUDPAdvertise(void); void NetworkUDPAdvertise(void);
#endif /* NETWORK_LAN_H */ #endif /* NETWORK_LAN_H */

View File

@ -923,12 +923,37 @@ static void LoadList(IniFile *ini, const char *grpname, char **list, int len)
} }
} }
static void SaveList(IniFile *ini, const char *grpname, char **list, int len)
{
IniGroup *group = ini_getgroup(ini, grpname, -1);
IniItem *item;
int i;
bool first = true;
if (!group)
return;
for ( i=0; i != len; i++) {
if ( list[i] == '\0' ) continue;
if (first) { // add first item to the head of the group
item = ini_item_alloc(group, list[i], strlen(list[i]));
item->value = item->name;
group->item = item;
first = false;
} else { // all other items are attached to the previous one
item->next = ini_item_alloc(group, list[i], strlen(list[i]));
item = item->next;
item->value = item->name;
}
}
}
void LoadFromConfig() void LoadFromConfig()
{ {
IniFile *ini = ini_load(_config_file); IniFile *ini = ini_load(_config_file);
HandleSettingDescs(ini, load_setting_desc); HandleSettingDescs(ini, load_setting_desc);
LoadList(ini, "newgrf", _newgrf_files, lengthof(_newgrf_files)); LoadList(ini, "newgrf", _newgrf_files, lengthof(_newgrf_files));
LoadList(ini, "servers", _network_server_list, lengthof(_network_server_list)); LoadList(ini, "servers", _network_host_list, lengthof(_network_host_list));
ini_free(ini); ini_free(ini);
} }
@ -936,6 +961,7 @@ void SaveToConfig()
{ {
IniFile *ini = ini_load(_config_file); IniFile *ini = ini_load(_config_file);
HandleSettingDescs(ini, save_setting_desc); HandleSettingDescs(ini, save_setting_desc);
SaveList(ini, "servers", _network_host_list, lengthof(_network_host_list));
ini_save(_config_file, ini); ini_save(_config_file, ini);
ini_free(ini); ini_free(ini);
} }