Mercurial > hg > openttd
changeset 11565:cf37aec7c469 draft
(svn r15931) -Codechange: let the host and ban lists use of SmallVector.
author | rubidium <rubidium@openttd.org> |
---|---|
date | Fri, 03 Apr 2009 12:49:58 +0000 |
parents | 1fa775b6b68a |
children | b4b8ed3247b4 |
files | src/console_cmds.cpp src/core/smallvec_type.hpp src/network/network.cpp src/network/network_func.h src/network/network_gamelist.cpp src/network/network_gui.cpp src/network/network_server.cpp src/settings.cpp |
diffstat | 8 files changed, 41 insertions(+), 78 deletions(-) [+] |
line wrap: on
line diff
--- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -422,7 +422,6 @@ DEF_CONSOLE_CMD(ConUnBan) { - uint i, index; if (argc == 0) { IConsoleHelp("Unban a client from a network game. Usage: 'unban <ip | client-id>'"); @@ -432,15 +431,14 @@ if (argc != 2) return false; - index = (strchr(argv[1], '.') == NULL) ? atoi(argv[1]) : 0; + uint index = (strchr(argv[1], '.') == NULL) ? atoi(argv[1]) : 0; index--; + uint i = 0; - for (i = 0; i < lengthof(_network_ban_list); i++) { - if (_network_ban_list[i] == NULL) continue; - + for (char **iter = _network_ban_list.Begin(); iter != _network_ban_list.End(); iter++, i++) { if (strcmp(_network_ban_list[i], argv[1]) == 0 || index == i) { free(_network_ban_list[i]); - _network_ban_list[i] = NULL; + _network_ban_list.Erase(iter); IConsolePrint(CC_DEFAULT, "IP unbanned."); return true; } @@ -452,8 +450,6 @@ DEF_CONSOLE_CMD(ConBanList) { - uint i; - if (argc == 0) { IConsoleHelp("List the IP's of banned clients: Usage 'banlist'"); return true; @@ -461,9 +457,9 @@ IConsolePrint(CC_DEFAULT, "Banlist: "); - for (i = 0; i < lengthof(_network_ban_list); i++) { - if (_network_ban_list[i] != NULL) - IConsolePrintF(CC_DEFAULT, " %d) %s", i + 1, _network_ban_list[i]); + uint i = 1; + for (char **iter = _network_ban_list.Begin(); iter != _network_ban_list.End(); iter++, i++) { + IConsolePrintF(CC_DEFAULT, " %d) %s", i, *iter); } return true;
--- a/src/core/smallvec_type.hpp +++ b/src/core/smallvec_type.hpp @@ -267,4 +267,6 @@ } }; +typedef AutoFreeSmallVector<char*, 4> StringList; + #endif /* SMALLVEC_TYPE_HPP */
--- a/src/network/network.cpp +++ b/src/network/network.cpp @@ -55,8 +55,8 @@ bool _network_need_advertise; uint32 _network_last_advertise_frame; uint8 _network_reconnect; -char *_network_host_list[10]; -char *_network_ban_list[25]; +StringList _network_host_list; +StringList _network_ban_list; uint32 _frame_counter_server; // The frame_counter of the server, if in network-mode uint32 _frame_counter_max; // To where we may go with our clients uint32 _frame_counter; @@ -492,11 +492,9 @@ /* Check if the client is banned */ banned = false; - for (i = 0; i < lengthof(_network_ban_list); i++) { - if (_network_ban_list[i] == NULL) continue; - + for (char **iter = _network_ban_list.Begin(); iter != _network_ban_list.End(); iter++) { /* Check for CIDR separator */ - char *chr_cidr = strchr(_network_ban_list[i], '/'); + char *chr_cidr = strchr(*iter, '/'); if (chr_cidr != NULL) { int cidr = atoi(chr_cidr + 1); @@ -505,7 +503,7 @@ /* Remove and then replace the / so that inet_addr() works on the IP portion */ *chr_cidr = '\0'; - uint32 ban_ip = inet_addr(_network_ban_list[i]); + uint32 ban_ip = inet_addr(*iter); *chr_cidr = '/'; /* Convert CIDR to mask in network format */ @@ -513,14 +511,14 @@ if ((sin.sin_addr.s_addr & mask) == (ban_ip & mask)) banned = true; } else { /* No CIDR used, so just perform a simple IP test */ - if (sin.sin_addr.s_addr == inet_addr(_network_ban_list[i])) banned = true; + if (sin.sin_addr.s_addr == inet_addr(*iter)) banned = true; } if (banned) { Packet p(PACKET_SERVER_BANNED); p.PrepareToSend(); - DEBUG(net, 1, "Banned ip tried to join (%s), refused", _network_ban_list[i]); + DEBUG(net, 1, "Banned ip tried to join (%s), refused", *iter); send(s, (const char*)p.buffer, p.size, 0); closesocket(s); @@ -690,19 +688,10 @@ * by the function that generates the config file. */ void NetworkRebuildHostList() { - uint i = 0; - const NetworkGameList *item = _network_game_list; - while (item != NULL && i != lengthof(_network_host_list)) { - if (item->manually) { - free(_network_host_list[i]); - _network_host_list[i++] = str_fmt("%s:%i", item->info.hostname, item->address.GetPort()); - } - item = item->next; - } + _network_host_list.Clear(); - for (; i < lengthof(_network_host_list); i++) { - free(_network_host_list[i]); - _network_host_list[i] = NULL; + for (NetworkGameList *item = _network_game_list; item != NULL; item = item->next) { + if (item->manually) *_network_host_list.Append() = strdup(item->address.GetAddressAsString()); } }
--- a/src/network/network_func.h +++ b/src/network/network_func.h @@ -11,6 +11,7 @@ #include "network_type.h" #include "../console_type.h" #include "../gfx_type.h" +#include "../core/smallvec_type.hpp" extern NetworkServerGameInfo _network_game_info; extern NetworkCompanyState *_network_company_states; @@ -20,8 +21,8 @@ extern bool _network_need_advertise; extern uint32 _network_last_advertise_frame; extern uint8 _network_reconnect; -extern char *_network_host_list[10]; -extern char *_network_ban_list[25]; +extern StringList _network_host_list; +extern StringList _network_ban_list; byte NetworkSpectatorCount(); void NetworkUpdateClientName();
--- a/src/network/network_gamelist.cpp +++ b/src/network/network_gamelist.cpp @@ -50,7 +50,7 @@ strecpy(item->info.hostname, ins_item->info.hostname, lastof(item->info.hostname)); item->online = false; } - item->manually = ins_item->manually; + item->manually |= ins_item->manually; UpdateNetworkGameWindow(false); } free(ins_item);
--- a/src/network/network_gui.cpp +++ b/src/network/network_gui.cpp @@ -951,13 +951,11 @@ /* Only show once */ if (first) { - char * const *srv; - first = false; /* add all servers from the config file to our list */ - for (srv = &_network_host_list[0]; srv != endof(_network_host_list) && *srv != NULL; srv++) { - NetworkAddServer(*srv); - } + for (char **iter = _network_host_list.Begin(); iter != _network_host_list.End(); iter++) { + NetworkAddServer(*iter); + } } new NetworkGameWindow(&_network_game_window_desc);
--- a/src/network/network_server.cpp +++ b/src/network/network_server.cpp @@ -1798,12 +1798,7 @@ } /* Add user to ban-list */ - for (uint index = 0; index < lengthof(_network_ban_list); index++) { - if (_network_ban_list[index] == NULL) { - _network_ban_list[index] = strdup(banip); - break; - } - } + *_network_ban_list.Append() = strdup(banip); } bool NetworkCompanyHasClients(CompanyID company)
--- a/src/settings.cpp +++ b/src/settings.cpp @@ -68,9 +68,8 @@ GameSettings _settings_game; GameSettings _settings_newgame; -typedef const char *SettingListCallbackProc(const IniItem *item, uint index); typedef void SettingDescProc(IniFile *ini, const SettingDesc *desc, const char *grpname, void *object); -typedef void SettingDescProcList(IniFile *ini, const char *grpname, char **list, uint len, SettingListCallbackProc proc); +typedef void SettingDescProcList(IniFile *ini, const char *grpname, StringList *list); static bool IsSignedVarMemType(VarType vt); @@ -598,27 +597,18 @@ * @param ini IniFile handle to the ini file with the source data * @param grpname character string identifying the section-header of the ini * file that will be parsed - * @param list pointer to an string(pointer) array that will store the parsed * entries of the given section - * @param len the maximum number of items available for the above list - * @param proc callback function that can override how the values are stored - * inside the list */ -static void ini_load_setting_list(IniFile *ini, const char *grpname, char **list, uint len, SettingListCallbackProc proc) + */ +static void ini_load_setting_list(IniFile *ini, const char *grpname, StringList *list) { IniGroup *group = ini->GetGroup(grpname); - IniItem *item; - const char *entry; - uint i, j; - if (group == NULL) return; + if (group == NULL || list == NULL) return; - for (i = j = 0, item = group->item; item != NULL; item = item->next) { - entry = (proc != NULL) ? proc(item, i++) : item->name; + list->Clear(); - if (entry == NULL || list == NULL) continue; - - if (j == len) break; - list[j++] = strdup(entry); + for (const IniItem *item = group->item; item != NULL; item = item->next) { + if (item->name != NULL) *list->Append() = strdup(item->name); } } @@ -629,24 +619,16 @@ * @param grpname character string identifying the section-header of the ini file * @param list pointer to an string(pointer) array that will be used as the * source to be saved into the relevant ini section - * @param len the maximum number of items available for the above list - * @param proc callback function that can will provide the source data if defined */ -static void ini_save_setting_list(IniFile *ini, const char *grpname, char **list, uint len, SettingListCallbackProc proc) + */ +static void ini_save_setting_list(IniFile *ini, const char *grpname, StringList *list) { IniGroup *group = ini->GetGroup(grpname); - const char *entry; - uint i; - if (proc == NULL && list == NULL) return; - if (group == NULL) return; + if (group == NULL || list == NULL) return; group->Clear(); - for (i = 0; i != len; i++) { - entry = (proc != NULL) ? proc(NULL, i) : list[i]; - - if (entry == NULL || *entry == '\0') continue; - - group->GetItem(entry, true)->SetValue(""); + for (char **iter = list->Begin(); iter != list->End(); iter++) { + group->GetItem(*iter, true)->SetValue(""); } } @@ -1295,8 +1277,8 @@ proc(ini, _currency_settings,"currency", &_custom_currency); #ifdef ENABLE_NETWORK - proc_list(ini, "servers", _network_host_list, lengthof(_network_host_list), NULL); - proc_list(ini, "bans", _network_ban_list, lengthof(_network_ban_list), NULL); + proc_list(ini, "servers", &_network_host_list); + proc_list(ini, "bans", &_network_ban_list); #endif /* ENABLE_NETWORK */ }