Skip to content

Commit 342e933

Browse files
committedAug 15, 2017
server.cpp: code modernization
* Use more for range based loops * Simplify some tests * Code style fixes * connection.h: better PeerChange constructor instead of creating uninitalized object and then affect variables
1 parent 618e0dd commit 342e933

File tree

2 files changed

+31
-40
lines changed

2 files changed

+31
-40
lines changed
 

Diff for: ‎src/network/connection.h

+4
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,10 @@ enum PeerChangeType
586586
};
587587
struct PeerChange
588588
{
589+
PeerChange(PeerChangeType t, u16 _peer_id, bool _timeout):
590+
type(t), peer_id(_peer_id), timeout(_timeout) {}
591+
PeerChange() = delete;
592+
589593
PeerChangeType type;
590594
u16 peer_id;
591595
bool timeout;

Diff for: ‎src/server.cpp

+27-40
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ void *ServerThread::run()
114114

115115
END_DEBUG_EXCEPTION_HANDLER
116116

117-
return NULL;
117+
return nullptr;
118118
}
119119

120120
v3f ServerSoundParams::getPos(ServerEnvironment *env, bool *pos_exists) const
@@ -172,7 +172,7 @@ Server::Server(
172172
{
173173
m_lag = g_settings->getFloat("dedicated_server_step");
174174

175-
if(path_world == "")
175+
if (path_world.empty())
176176
throw ServerError("Supplied empty world path");
177177

178178
if(!gamespec.isValid())
@@ -251,7 +251,7 @@ Server::Server(
251251

252252
// Apply texture overrides from texturepack/override.txt
253253
std::string texture_path = g_settings->get("texture_path");
254-
if (texture_path != "" && fs::IsDir(texture_path))
254+
if (!texture_path.empty() && fs::IsDir(texture_path))
255255
m_nodedef->applyTextureOverrides(texture_path + DIR_DELIM + "override.txt");
256256

257257
m_nodedef->setNodeRegistrationStatus(true);
@@ -326,7 +326,7 @@ Server::~Server()
326326
reconnect = m_shutdown_ask_reconnect;
327327
kick_msg = m_shutdown_msg;
328328
}
329-
if (kick_msg == "") {
329+
if (kick_msg.empty()) {
330330
kick_msg = g_settings->get("kick_msg_shutdown");
331331
}
332332
m_env->kickAllPlayers(SERVER_ACCESSDENIED_SHUTDOWN,
@@ -1016,7 +1016,7 @@ PlayerSAO* Server::StageTwoClientInit(u16 peer_id)
10161016
m_clients.lock();
10171017
try {
10181018
RemoteClient* client = m_clients.lockedGetClientNoEx(peer_id, CS_InitDone);
1019-
if (client != NULL) {
1019+
if (client) {
10201020
playername = client->getName();
10211021
playersao = emergePlayer(playername.c_str(), peer_id, client->net_proto_version);
10221022
}
@@ -1029,7 +1029,7 @@ PlayerSAO* Server::StageTwoClientInit(u16 peer_id)
10291029
RemotePlayer *player = m_env->getPlayer(playername.c_str());
10301030

10311031
// If failed, cancel
1032-
if ((playersao == NULL) || (player == NULL)) {
1032+
if (!playersao || !player) {
10331033
if (player && player->peer_id != 0) {
10341034
actionstream << "Server: Failed to emerge player \"" << playername
10351035
<< "\" (player allocated to an another client)" << std::endl;
@@ -1301,11 +1301,7 @@ void Server::peerAdded(con::Peer *peer)
13011301
verbosestream<<"Server::peerAdded(): peer->id="
13021302
<<peer->id<<std::endl;
13031303

1304-
con::PeerChange c;
1305-
c.type = con::PEER_ADDED;
1306-
c.peer_id = peer->id;
1307-
c.timeout = false;
1308-
m_peer_change_queue.push(c);
1304+
m_peer_change_queue.push(con::PeerChange(con::PEER_ADDED, peer->id, false));
13091305
}
13101306

13111307
void Server::deletingPeer(con::Peer *peer, bool timeout)
@@ -1315,11 +1311,7 @@ void Server::deletingPeer(con::Peer *peer, bool timeout)
13151311
<<peer->id<<", timeout="<<timeout<<std::endl;
13161312

13171313
m_clients.event(peer->id, CSE_Disconnect);
1318-
con::PeerChange c;
1319-
c.type = con::PEER_REMOVED;
1320-
c.peer_id = peer->id;
1321-
c.timeout = timeout;
1322-
m_peer_change_queue.push(c);
1314+
m_peer_change_queue.push(con::PeerChange(con::PEER_REMOVED, peer->id, timeout));
13231315
}
13241316

13251317
bool Server::getClientConInfo(u16 peer_id, con::rtt_stat_type type, float* retval)
@@ -1344,7 +1336,7 @@ bool Server::getClientInfo(
13441336
m_clients.lock();
13451337
RemoteClient* client = m_clients.lockedGetClientNoEx(peer_id, CS_Invalid);
13461338

1347-
if (client == NULL) {
1339+
if (!client) {
13481340
m_clients.unlock();
13491341
return false;
13501342
}
@@ -2134,25 +2126,25 @@ void Server::sendRemoveNode(v3s16 p, u16 ignore_id,
21342126
pkt << p;
21352127

21362128
std::vector<u16> clients = m_clients.getClientIDs();
2137-
for (std::vector<u16>::iterator i = clients.begin(); i != clients.end(); ++i) {
2129+
for (u16 client_id : clients) {
21382130
if (far_players) {
21392131
// Get player
2140-
if (RemotePlayer *player = m_env->getPlayer(*i)) {
2132+
if (RemotePlayer *player = m_env->getPlayer(client_id)) {
21412133
PlayerSAO *sao = player->getPlayerSAO();
21422134
if (!sao)
21432135
continue;
21442136

21452137
// If player is far away, only set modified blocks not sent
21462138
v3f player_pos = sao->getBasePosition();
21472139
if (player_pos.getDistanceFrom(p_f) > maxd) {
2148-
far_players->push_back(*i);
2140+
far_players->push_back(client_id);
21492141
continue;
21502142
}
21512143
}
21522144
}
21532145

21542146
// Send as reliable
2155-
m_clients.send(*i, 0, &pkt, true);
2147+
m_clients.send(client_id, 0, &pkt, true);
21562148
}
21572149
}
21582150

@@ -2275,19 +2267,15 @@ void Server::SendBlocks(float dtime)
22752267

22762268
PrioritySortedBlockTransfer q = queue[i];
22772269

2278-
MapBlock *block = NULL;
2279-
try
2280-
{
2270+
MapBlock *block = nullptr;
2271+
try {
22812272
block = m_env->getMap().getBlockNoCreate(q.pos);
2282-
}
2283-
catch(InvalidPositionException &e)
2284-
{
2273+
} catch(const InvalidPositionException &e) {
22852274
continue;
22862275
}
22872276

22882277
RemoteClient *client = m_clients.lockedGetClientNoEx(q.peer_id, CS_Active);
2289-
2290-
if(!client)
2278+
if (!client)
22912279
continue;
22922280

22932281
SendBlockNoLock(q.peer_id, block, client->serialization_version, client->net_proto_version);
@@ -2718,7 +2706,7 @@ void Server::DeleteClient(u16 peer_id, ClientDeletionReason reason)
27182706
RemotePlayer *player = m_env->getPlayer(peer_id);
27192707

27202708
/* Run scripts and remove from environment */
2721-
if (player != NULL) {
2709+
if (player) {
27222710
PlayerSAO *playersao = player->getPlayerSAO();
27232711
assert(playersao);
27242712

@@ -2737,7 +2725,7 @@ void Server::DeleteClient(u16 peer_id, ClientDeletionReason reason)
27372725
Print out action
27382726
*/
27392727
{
2740-
if(player != NULL && reason != CDR_DENY) {
2728+
if (player && reason != CDR_DENY) {
27412729
std::ostringstream os(std::ios_base::binary);
27422730
std::vector<u16> clients = m_clients.getClientIDs();
27432731

@@ -2889,8 +2877,7 @@ std::wstring Server::handleChat(const std::string &name, const std::wstring &wna
28892877
if (player && player->protocol_version >= 29)
28902878
peer_id_to_avoid_sending = PEER_ID_INEXISTENT;
28912879

2892-
for (u16 i = 0; i < clients.size(); i++) {
2893-
u16 cid = clients[i];
2880+
for (u16 cid : clients) {
28942881
if (cid != peer_id_to_avoid_sending)
28952882
SendChatMessage(cid, ChatMessage(line));
28962883
}
@@ -2927,15 +2914,15 @@ RemoteClient* Server::getClientNoEx(u16 peer_id, ClientState state_min)
29272914
std::string Server::getPlayerName(u16 peer_id)
29282915
{
29292916
RemotePlayer *player = m_env->getPlayer(peer_id);
2930-
if (player == NULL)
2917+
if (!player)
29312918
return "[id="+itos(peer_id)+"]";
29322919
return player->getName();
29332920
}
29342921

29352922
PlayerSAO* Server::getPlayerSAO(u16 peer_id)
29362923
{
29372924
RemotePlayer *player = m_env->getPlayer(peer_id);
2938-
if (player == NULL)
2925+
if (!player)
29392926
return NULL;
29402927
return player->getPlayerSAO();
29412928
}
@@ -2954,12 +2941,12 @@ std::wstring Server::getStatusString()
29542941
bool first = true;
29552942
os<<L", clients={";
29562943
std::vector<u16> clients = m_clients.getClientIDs();
2957-
for (std::vector<u16>::iterator i = clients.begin(); i != clients.end(); ++i) {
2944+
for (u16 client_id : clients) {
29582945
// Get player
2959-
RemotePlayer *player = m_env->getPlayer(*i);
2946+
RemotePlayer *player = m_env->getPlayer(client_id);
29602947
// Get name of player
29612948
std::wstring name = L"unknown";
2962-
if (player != NULL)
2949+
if (player)
29632950
name = narrow_to_wide(player->getName());
29642951
// Add name to information string
29652952
if(!first)
@@ -3540,15 +3527,15 @@ PlayerSAO* Server::emergePlayer(const char *name, u16 peer_id, u16 proto_version
35403527
RemotePlayer *player = m_env->getPlayer(name);
35413528

35423529
// If player is already connected, cancel
3543-
if (player != NULL && player->peer_id != 0) {
3530+
if (player && player->peer_id != 0) {
35443531
infostream<<"emergePlayer(): Player already connected"<<std::endl;
35453532
return NULL;
35463533
}
35473534

35483535
/*
35493536
If player with the wanted peer_id already exists, cancel.
35503537
*/
3551-
if (m_env->getPlayer(peer_id) != NULL) {
3538+
if (m_env->getPlayer(peer_id)) {
35523539
infostream<<"emergePlayer(): Player with wrong name but same"
35533540
" peer_id already exists"<<std::endl;
35543541
return NULL;

0 commit comments

Comments
 (0)
Please sign in to comment.