Client & ClientEnvirnment: don't create fake events (#5676)

Instead of create fake events on the stack on each loop call (Game::run), verify is queue is empty or not and handle event directly if there is.

This prevents fake ClientEvent creation & memory allocations

Same fix is also applied on ClientEnvironment, & rename getClientEvent to getClientEnvEvent to match ClientEnvEvent object
This commit is contained in:
Loïc Blot 2017-04-29 17:25:25 +02:00 committed by GitHub
parent f727f54192
commit 3db66b4531
5 changed files with 27 additions and 33 deletions

@ -411,16 +411,14 @@ void Client::step(float dtime)
/* /*
Get events Get events
*/ */
for(;;) { while (m_env.hasClientEnvEvents()) {
ClientEnvEvent event = m_env.getClientEvent(); ClientEnvEvent envEvent = m_env.getClientEnvEvent();
if(event.type == CEE_NONE) {
break;
}
else if(event.type == CEE_PLAYER_DAMAGE) {
if(m_ignore_damage_timer <= 0) {
u8 damage = event.player_damage.amount;
if(event.player_damage.send_to_server) if (envEvent.type == CEE_PLAYER_DAMAGE) {
if (m_ignore_damage_timer <= 0) {
u8 damage = envEvent.player_damage.amount;
if (envEvent.player_damage.send_to_server)
sendDamage(damage); sendDamage(damage);
// Add to ClientEvent queue // Add to ClientEvent queue
@ -431,8 +429,8 @@ void Client::step(float dtime)
} }
} }
// Protocol v29 or greater obsoleted this event // Protocol v29 or greater obsoleted this event
else if (event.type == CEE_PLAYER_BREATH && m_proto_ver < 29) { else if (envEvent.type == CEE_PLAYER_BREATH && m_proto_ver < 29) {
u16 breath = event.player_breath.amount; u16 breath = envEvent.player_breath.amount;
sendBreath(breath); sendBreath(breath);
} }
} }
@ -1596,14 +1594,11 @@ void Client::addUpdateMeshTaskForNode(v3s16 nodepos, bool ack_to_server, bool ur
ClientEvent Client::getClientEvent() ClientEvent Client::getClientEvent()
{ {
ClientEvent event; FATAL_ERROR_IF(m_client_event_queue.empty(),
if (m_client_event_queue.empty()) { "Cannot getClientEvent, queue is empty.");
event.type = CE_NONE;
} ClientEvent event = m_client_event_queue.front();
else { m_client_event_queue.pop();
event = m_client_event_queue.front();
m_client_event_queue.pop();
}
return event; return event;
} }

@ -414,7 +414,8 @@ public:
void updateCameraOffset(v3s16 camera_offset) void updateCameraOffset(v3s16 camera_offset)
{ m_mesh_update_thread.m_camera_offset = camera_offset; } { m_mesh_update_thread.m_camera_offset = camera_offset; }
// Get event from queue. CE_NONE is returned if queue is empty. bool hasClientEvents() const { return !m_client_event_queue.empty(); }
// Get event from queue. If queue is empty, it triggers an assertion failure.
ClientEvent getClientEvent(); ClientEvent getClientEvent();
bool accessDenied() const { return m_access_denied; } bool accessDenied() const { return m_access_denied; }

@ -598,15 +598,13 @@ void ClientEnvironment::getActiveObjects(v3f origin, f32 max_d,
} }
} }
ClientEnvEvent ClientEnvironment::getClientEvent() ClientEnvEvent ClientEnvironment::getClientEnvEvent()
{ {
ClientEnvEvent event; FATAL_ERROR_IF(m_client_event_queue.empty(),
if(m_client_event_queue.empty()) "ClientEnvironment::getClientEnvEvent(): queue is empty");
event.type = CEE_NONE;
else { ClientEnvEvent event = m_client_event_queue.front();
event = m_client_event_queue.front(); m_client_event_queue.pop();
m_client_event_queue.pop();
}
return event; return event;
} }

@ -126,8 +126,9 @@ public:
void getActiveObjects(v3f origin, f32 max_d, void getActiveObjects(v3f origin, f32 max_d,
std::vector<DistanceSortedActiveObject> &dest); std::vector<DistanceSortedActiveObject> &dest);
// Get event from queue. CEE_NONE is returned if queue is empty. bool hasClientEnvEvents() const { return !m_client_event_queue.empty(); }
ClientEnvEvent getClientEvent(); // Get event from queue. If queue is empty, it triggers an assertion failure.
ClientEnvEvent getClientEnvEvent();
/*! /*!
* Gets closest object pointed by the shootline. * Gets closest object pointed by the shootline.

@ -3055,11 +3055,10 @@ inline void Game::step(f32 *dtime)
void Game::processClientEvents(CameraOrientation *cam) void Game::processClientEvents(CameraOrientation *cam)
{ {
ClientEvent event = client->getClientEvent();
LocalPlayer *player = client->getEnv().getLocalPlayer(); LocalPlayer *player = client->getEnv().getLocalPlayer();
for ( ; event.type != CE_NONE; event = client->getClientEvent()) { while (client->hasClientEvents()) {
ClientEvent event = client->getClientEvent();
switch (event.type) { switch (event.type) {
case CE_PLAYER_DAMAGE: case CE_PLAYER_DAMAGE: