Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make Player::peer_id server-side only and add getters and setters (#6478
)

* Make Player::peer_id server-side only and add getters and setters

Player::peer_id has no sense client side, move it to server, make it private and add setter and getter
Also add some PEER_ID_INEXISTENT instead of harcoded 0
  • Loading branch information
nerzhul committed Sep 30, 2017
1 parent f729b5d commit be10c08
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 73 deletions.
21 changes: 2 additions & 19 deletions src/client.cpp
Expand Up @@ -1190,7 +1190,7 @@ void Client::sendReady()
void Client::sendPlayerPos()
{
LocalPlayer *myplayer = m_env.getLocalPlayer();
if(myplayer == NULL)
if (!myplayer)
return;

ClientMap &map = m_env.getClientMap();
Expand All @@ -1216,16 +1216,6 @@ void Client::sendPlayerPos()
myplayer->last_camera_fov = camera_fov;
myplayer->last_wanted_range = wanted_range;

//infostream << "Sending Player Position information" << std::endl;

session_t our_peer_id = m_con->GetPeerID();

// Set peer id if not set already
if(myplayer->peer_id == PEER_ID_INEXISTENT)
myplayer->peer_id = our_peer_id;

assert(myplayer->peer_id == our_peer_id);

NetworkPacket pkt(TOSERVER_PLAYERPOS, 12 + 12 + 4 + 4 + 4 + 1 + 1);

writePlayerPos(myplayer, &map, &pkt);
Expand All @@ -1236,16 +1226,9 @@ void Client::sendPlayerPos()
void Client::sendPlayerItem(u16 item)
{
LocalPlayer *myplayer = m_env.getLocalPlayer();
if(myplayer == NULL)
if (!myplayer)
return;

session_t our_peer_id = m_con->GetPeerID();

// Set peer id if not set already
if(myplayer->peer_id == PEER_ID_INEXISTENT)
myplayer->peer_id = our_peer_id;
assert(myplayer->peer_id == our_peer_id);

NetworkPacket pkt(TOSERVER_PLAYERITEM, 2);

pkt << item;
Expand Down
4 changes: 2 additions & 2 deletions src/content_sao.cpp
Expand Up @@ -848,7 +848,7 @@ void PlayerSAO::addedToEnvironment(u32 dtime_s)
ServerActiveObject::addedToEnvironment(dtime_s);
ServerActiveObject::setBasePosition(m_base_position);
m_player->setPlayerSAO(this);
m_player->peer_id = m_peer_id;
m_player->setPeerId(m_peer_id);
m_last_good_position = m_base_position;
}

Expand Down Expand Up @@ -1359,7 +1359,7 @@ void PlayerSAO::disconnected()
void PlayerSAO::unlinkPlayerSessionAndSave()
{
assert(m_player->getPlayerSAO() == this);
m_player->peer_id = 0;
m_player->setPeerId(PEER_ID_INEXISTENT);
m_env->savePlayer(m_player);
m_player->setPlayerSAO(NULL);
m_env->removePlayer(m_player);
Expand Down
2 changes: 0 additions & 2 deletions src/player.h
Expand Up @@ -147,8 +147,6 @@ class Player
v2s32 local_animations[4];
float local_animation_speed;

session_t peer_id = PEER_ID_INEXISTENT;

std::string inventory_formspec;

PlayerControl control;
Expand Down
6 changes: 6 additions & 0 deletions src/remoteplayer.h
Expand Up @@ -135,6 +135,10 @@ class RemotePlayer : public Player

u16 protocol_version = 0;

session_t getPeerId() const { return m_peer_id; }

void setPeerId(session_t peer_id) { m_peer_id = peer_id; }

private:
/*
serialize() writes a bunch of text that can contain
Expand Down Expand Up @@ -166,4 +170,6 @@ class RemotePlayer : public Player
bool m_sky_clouds;

CloudParams m_cloud_params;

session_t m_peer_id = PEER_ID_INEXISTENT;
};
2 changes: 1 addition & 1 deletion src/script/lua_api/l_object.cpp
Expand Up @@ -1023,7 +1023,7 @@ int ObjectRef::l_is_player_connected(lua_State *L)
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
RemotePlayer *player = getplayer(ref);
lua_pushboolean(L, (player != NULL && player->peer_id != 0));
lua_pushboolean(L, (player != NULL && player->getPeerId() != PEER_ID_INEXISTENT));
return 1;
}

Expand Down
28 changes: 15 additions & 13 deletions src/script/lua_api/l_server.cpp
Expand Up @@ -124,7 +124,7 @@ int ModApiServer::l_get_player_ip(lua_State *L)
}
try
{
Address addr = getServer(L)->getPeerAddress(player->peer_id);
Address addr = getServer(L)->getPeerAddress(player->getPeerId());
std::string ip_str = addr.serializeString();
lua_pushstring(L, ip_str.c_str());
return 1;
Expand All @@ -150,7 +150,7 @@ int ModApiServer::l_get_player_information(lua_State *L)
Address addr;
try
{
addr = getServer(L)->getPeerAddress(player->peer_id);
addr = getServer(L)->getPeerAddress(player->getPeerId());
} catch(const con::PeerNotFoundException &) {
dstream << FUNCTION_NAME << ": peer was not found" << std::endl;
lua_pushnil(L); // error
Expand All @@ -171,16 +171,18 @@ int ModApiServer::l_get_player_information(lua_State *L)
return 1; \
}

ERET(getServer(L)->getClientConInfo(player->peer_id,con::MIN_RTT,&min_rtt))
ERET(getServer(L)->getClientConInfo(player->peer_id,con::MAX_RTT,&max_rtt))
ERET(getServer(L)->getClientConInfo(player->peer_id,con::AVG_RTT,&avg_rtt))
ERET(getServer(L)->getClientConInfo(player->peer_id,con::MIN_JITTER,&min_jitter))
ERET(getServer(L)->getClientConInfo(player->peer_id,con::MAX_JITTER,&max_jitter))
ERET(getServer(L)->getClientConInfo(player->peer_id,con::AVG_JITTER,&avg_jitter))
ERET(getServer(L)->getClientConInfo(player->getPeerId(), con::MIN_RTT, &min_rtt))
ERET(getServer(L)->getClientConInfo(player->getPeerId(), con::MAX_RTT, &max_rtt))
ERET(getServer(L)->getClientConInfo(player->getPeerId(), con::AVG_RTT, &avg_rtt))
ERET(getServer(L)->getClientConInfo(player->getPeerId(), con::MIN_JITTER,
&min_jitter))
ERET(getServer(L)->getClientConInfo(player->getPeerId(), con::MAX_JITTER,
&max_jitter))
ERET(getServer(L)->getClientConInfo(player->getPeerId(), con::AVG_JITTER,
&avg_jitter))

ERET(getServer(L)->getClientInfo(player->peer_id,
&state, &uptime, &ser_vers, &prot_vers,
&major, &minor, &patch, &vers_string))
ERET(getServer(L)->getClientInfo(player->getPeerId(), &state, &uptime, &ser_vers,
&prot_vers, &major, &minor, &patch, &vers_string))

lua_newtable(L);
int table = lua_gettop(L);
Expand Down Expand Up @@ -291,7 +293,7 @@ int ModApiServer::l_ban_player(lua_State *L)
try
{
Address addr = getServer(L)->getPeerAddress(
dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name)->peer_id);
dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name)->getPeerId());
std::string ip_str = addr.serializeString();
getServer(L)->setIpBanned(ip_str, name);
} catch(const con::PeerNotFoundException &) {
Expand Down Expand Up @@ -323,7 +325,7 @@ int ModApiServer::l_kick_player(lua_State *L)
lua_pushboolean(L, false); // No such player
return 1;
}
getServer(L)->DenyAccess_Legacy(player->peer_id, utf8_to_wide(message));
getServer(L)->DenyAccess_Legacy(player->getPeerId(), utf8_to_wide(message));
lua_pushboolean(L, true);
return 1;
}
Expand Down

0 comments on commit be10c08

Please sign in to comment.