Skip to content

Commit b8484ef

Browse files
ShadowNinjaSmallJoker
authored andcommittedMar 18, 2017
Server list cleanup
This removes the hacky server_dedicated pseudo-setting.
1 parent 2cc518d commit b8484ef

File tree

6 files changed

+56
-50
lines changed

6 files changed

+56
-50
lines changed
 

‎src/game.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1899,7 +1899,7 @@ bool Game::createSingleplayerServer(const std::string map_dir,
18991899
}
19001900

19011901
server = new Server(map_dir, gamespec, simple_singleplayer_mode,
1902-
bind_addr.isIPv6());
1902+
bind_addr.isIPv6(), false);
19031903

19041904
server->start(bind_addr);
19051905

‎src/main.cpp

+3-7
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,6 @@ int main(int argc, char *argv[])
213213
infostream << "Using commanded world path ["
214214
<< game_params.world_path << "]" << std::endl;
215215

216-
//Run dedicated server if asked to or no other option
217-
g_settings->set("server_dedicated",
218-
game_params.is_dedicated_server ? "true" : "false");
219-
220216
if (game_params.is_dedicated_server)
221217
return run_dedicated_server(game_params, cmd_args) ? 0 : 1;
222218

@@ -852,8 +848,8 @@ static bool run_dedicated_server(const GameParams &game_params, const Settings &
852848

853849
try {
854850
// Create server
855-
Server server(game_params.world_path,
856-
game_params.game_spec, false, bind_addr.isIPv6(), &iface);
851+
Server server(game_params.world_path, game_params.game_spec,
852+
false, bind_addr.isIPv6(), true, &iface);
857853

858854
g_term_console.setup(&iface, &kill, admin_nick);
859855

@@ -887,7 +883,7 @@ static bool run_dedicated_server(const GameParams &game_params, const Settings &
887883
try {
888884
// Create server
889885
Server server(game_params.world_path, game_params.game_spec, false,
890-
bind_addr.isIPv6());
886+
bind_addr.isIPv6(), true);
891887
server.start(bind_addr);
892888

893889
// Run server

‎src/server.cpp

+15-15
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,13 @@ Server::Server(
148148
const SubgameSpec &gamespec,
149149
bool simple_singleplayer_mode,
150150
bool ipv6,
151+
bool dedicated,
151152
ChatInterface *iface
152153
):
153154
m_path_world(path_world),
154155
m_gamespec(gamespec),
155156
m_simple_singleplayer_mode(simple_singleplayer_mode),
157+
m_dedicated(dedicated),
156158
m_async_fatal_error(""),
157159
m_env(NULL),
158160
m_con(PROTOCOL_ID,
@@ -629,18 +631,19 @@ void Server::AsyncRunStep(bool initial_step)
629631
// send masterserver announce
630632
{
631633
float &counter = m_masterserver_timer;
632-
if(!isSingleplayer() && (!counter || counter >= 300.0) &&
633-
g_settings->getBool("server_announce"))
634-
{
635-
ServerList::sendAnnounce(counter ? "update" : "start",
634+
if (!isSingleplayer() && (!counter || counter >= 300.0) &&
635+
g_settings->getBool("server_announce")) {
636+
ServerList::sendAnnounce(counter ? ServerList::AA_UPDATE :
637+
ServerList::AA_START,
636638
m_bind_addr.getPort(),
637639
m_clients.getPlayerNames(),
638640
m_uptime.get(),
639641
m_env->getGameTime(),
640642
m_lag,
641643
m_gamespec.id,
642644
Mapgen::getMapgenName(m_emerge->mgparams->mgtype),
643-
m_mods);
645+
m_mods,
646+
m_dedicated);
644647
counter = 0.01;
645648
}
646649
counter += dtime;
@@ -3574,16 +3577,6 @@ void dedicated_server_loop(Server &server, bool &kill)
35743577
}
35753578
server.step(steplen);
35763579

3577-
if(server.getShutdownRequested() || kill)
3578-
{
3579-
infostream<<"Dedicated server quitting"<<std::endl;
3580-
#if USE_CURL
3581-
if(g_settings->getBool("server_announce"))
3582-
ServerList::sendAnnounce("delete", server.m_bind_addr.getPort());
3583-
#endif
3584-
break;
3585-
}
3586-
35873580
/*
35883581
Profiler
35893582
*/
@@ -3596,4 +3589,11 @@ void dedicated_server_loop(Server &server, bool &kill)
35963589
}
35973590
}
35983591
}
3592+
3593+
infostream << "Dedicated server quitting" << std::endl;
3594+
#if USE_CURL
3595+
if (g_settings->getBool("server_announce"))
3596+
ServerList::sendAnnounce(ServerList::AA_DELETE,
3597+
server.m_bind_addr.getPort());
3598+
#endif
35993599
}

‎src/server.h

+3
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ class Server : public con::PeerHandler, public MapEventReceiver,
148148
const SubgameSpec &gamespec,
149149
bool simple_singleplayer_mode,
150150
bool ipv6,
151+
bool dedicated,
151152
ChatInterface *iface = NULL
152153
);
153154
~Server();
@@ -510,6 +511,8 @@ class Server : public con::PeerHandler, public MapEventReceiver,
510511
// functionality
511512
bool m_simple_singleplayer_mode;
512513
u16 m_max_chatmessage_length;
514+
// For "dedicated" server list flag
515+
bool m_dedicated;
513516

514517
// Thread can set; step() will throw as ServerError
515518
MutexedVariable<std::string> m_async_fatal_error;

‎src/serverlist.cpp

+12-10
Original file line numberDiff line numberDiff line change
@@ -196,23 +196,25 @@ const std::string serializeJson(const std::vector<ServerListSpec> &serverlist)
196196

197197

198198
#if USE_CURL
199-
void sendAnnounce(const std::string &action,
199+
void sendAnnounce(AnnounceAction action,
200200
const u16 port,
201201
const std::vector<std::string> &clients_names,
202202
const double uptime,
203203
const u32 game_time,
204204
const float lag,
205205
const std::string &gameid,
206206
const std::string &mg_name,
207-
const std::vector<ModSpec> &mods)
207+
const std::vector<ModSpec> &mods,
208+
bool dedicated)
208209
{
210+
static const char *aa_names[] = {"start", "update", "delete"};
209211
Json::Value server;
210-
server["action"] = action;
212+
server["action"] = aa_names[action];
211213
server["port"] = port;
212214
if (g_settings->exists("server_address")) {
213215
server["address"] = g_settings->get("server_address");
214216
}
215-
if (action != "delete") {
217+
if (action != AA_DELETE) {
216218
bool strict_checking = g_settings->getBool("strict_protocol_version_checking");
217219
server["name"] = g_settings->get("server_name");
218220
server["description"] = g_settings->get("server_description");
@@ -237,20 +239,19 @@ void sendAnnounce(const std::string &action,
237239
if (gameid != "") server["gameid"] = gameid;
238240
}
239241

240-
if (action == "start") {
241-
server["dedicated"] = g_settings->getBool("server_dedicated");
242+
if (action == AA_START) {
243+
server["dedicated"] = dedicated;
242244
server["rollback"] = g_settings->getBool("enable_rollback_recording");
243245
server["mapgen"] = mg_name;
244246
server["privs"] = g_settings->get("default_privs");
245247
server["can_see_far_names"] = g_settings->getS16("player_transfer_distance") <= 0;
246248
server["mods"] = Json::Value(Json::arrayValue);
247249
for (std::vector<ModSpec>::const_iterator it = mods.begin();
248-
it != mods.end();
249-
++it) {
250+
it != mods.end(); ++it) {
250251
server["mods"].append(it->name);
251252
}
252253
actionstream << "Announcing to " << g_settings->get("serverlist_url") << std::endl;
253-
} else {
254+
} else if (action == AA_UPDATE) {
254255
if (lag)
255256
server["lag"] = lag;
256257
}
@@ -264,4 +265,5 @@ void sendAnnounce(const std::string &action,
264265
}
265266
#endif
266267

267-
} //namespace ServerList
268+
} // namespace ServerList
269+

‎src/serverlist.h

+22-17
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,27 @@ typedef Json::Value ServerListSpec;
2929

3030
namespace ServerList
3131
{
32-
std::vector<ServerListSpec> getLocal();
33-
std::vector<ServerListSpec> getOnline();
34-
bool deleteEntry(const ServerListSpec &server);
35-
bool insert(const ServerListSpec &server);
36-
std::vector<ServerListSpec> deSerialize(const std::string &liststring);
37-
const std::string serialize(const std::vector<ServerListSpec> &serverlist);
38-
std::vector<ServerListSpec> deSerializeJson(const std::string &liststring);
39-
const std::string serializeJson(const std::vector<ServerListSpec> &serverlist);
40-
#if USE_CURL
41-
void sendAnnounce(const std::string &action, const u16 port,
42-
const std::vector<std::string> &clients_names = std::vector<std::string>(),
43-
const double uptime = 0, const u32 game_time = 0,
44-
const float lag = 0, const std::string &gameid = "",
45-
const std::string &mg_name = "",
46-
const std::vector<ModSpec> &mods = std::vector<ModSpec>());
47-
#endif
48-
} // ServerList namespace
32+
std::vector<ServerListSpec> getLocal();
33+
std::vector<ServerListSpec> getOnline();
34+
35+
bool deleteEntry(const ServerListSpec &server);
36+
bool insert(const ServerListSpec &server);
37+
38+
std::vector<ServerListSpec> deSerialize(const std::string &liststring);
39+
const std::string serialize(const std::vector<ServerListSpec> &serverlist);
40+
std::vector<ServerListSpec> deSerializeJson(const std::string &liststring);
41+
const std::string serializeJson(const std::vector<ServerListSpec> &serverlist);
42+
43+
#if USE_CURL
44+
enum AnnounceAction {AA_START, AA_UPDATE, AA_DELETE};
45+
void sendAnnounce(AnnounceAction, u16 port,
46+
const std::vector<std::string> &clients_names = std::vector<std::string>(),
47+
double uptime = 0, u32 game_time = 0, float lag = 0,
48+
const std::string &gameid = "", const std::string &mg_name = "",
49+
const std::vector<ModSpec> &mods = std::vector<ModSpec>(),
50+
bool dedicated = false);
51+
#endif
52+
53+
} // namespace ServerList
4954

5055
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.