Commit 2dcb37ba authored by louiz’'s avatar louiz’

Check every deserialization, return and warn if it’s invalid. #61

parent aba363c5
......@@ -2,38 +2,52 @@
#include <string>
Event::Event(const unsigned long int id):
id(id)
id(id),
valid(true)
{
}
Event::Event(const Command* command)
{
this->from_string(std::string(command->body,
command->body_size).c_str());
if (this->from_string(std::string(command->body,
command->body_size).c_str()) == false)
this->valid = false;
else
this->valid = true;
}
Event::Event():
id(Event::current_id++)
id(Event::current_id++),
valid(true)
{
}
OkEvent::OkEvent(const Command* command)
{
this->from_string(std::string(command->body,
command->body_size).c_str());
if (this->from_string(std::string(command->body,
command->body_size).c_str()) == false)
this->valid = false;
else
this->valid = true;
}
MoveEvent::MoveEvent(const Command* command)
{
this->from_string(std::string(command->body,
command->body_size).c_str());
if (this->from_string(std::string(command->body,
command->body_size).c_str()) == false)
this->valid = false;
else
this->valid = true;
}
PathEvent::PathEvent(const Command* command)
{
this->from_string(std::string(command->body,
command->body_size).c_str());
if (this->from_string(std::string(command->body,
command->body_size).c_str()) == false)
this->valid = false;
else
this->valid = true;
}
unsigned long int Event::current_id = 0;
......@@ -36,11 +36,16 @@ public:
{
return this->id;
}
bool is_valid() const
{
return this->valid;
}
private:
Event(const Event&);
Event& operator=(const Event&);
bool valid;
unsigned long int id;
};
......
......@@ -35,7 +35,13 @@ boost::asio::io_service& RemoteGameClient::get_io_service()
void RemoteGameClient::ok_callback(Command* command)
{
// todo replace that Event struct by just an int.
Event ok_event(command);
if (ok_event.is_valid() == false)
{
log_warning("Invalid data for OK command");
return ;
}
World* world = static_cast<GameServer*>(this->server)->get_world();
this->send_ok(ok_event.get_id(), this->get_number());
......
......@@ -91,8 +91,12 @@ void World::new_occupant_callback(Command* command)
Occupant* occupant = new Occupant;
std::string data(command->body, command->body_size);
occupant->from_string(data);
log_debug("occupant: " << occupant->name << " " << occupant->number);
if (occupant->from_string(data) == false)
{
log_error("Invalid data received for the occupant.");
return ;
}
log_debug("Occupant: " << occupant->name << " " << occupant->number);
this->add_new_occupant(occupant);
}
......@@ -101,13 +105,22 @@ void World::handle_start_command(Command* command)
// Action* action = new Action(0, event, this->occupants.size());
// this->turn_handler->insert_action(action, turn);
Event* start_event = new Event(command);
if (start_event->is_valid() == false)
{
log_warning("Invalid data for START command");
return ;
}
this->install_start_action(start_event, 1);
}
void World::occupant_left_callback(Command* command)
{
Occupant occupant;
occupant.from_string(std::string(command->body, command->body_size));
if (occupant.from_string(std::string(command->body, command->body_size)) == false)
{
log_error("Invalid data received for the leaving occupant.");
return ;
}
log_debug("Occupant to remove: " << occupant.number);
this->remove_occupant(&occupant);
}
......@@ -141,7 +154,11 @@ void World::new_entity_callback(Command* command)
Entity* new_entity = new Entity;
std::string data(command->body, command->body_size);
log_debug("New_Entity: " << data);
new_entity->from_string(data);
if (new_entity->from_string(data) == false)
{
log_debug("Invalid data for the new entity.");
return ;
}
this->insert_entity(new_entity);
}
......@@ -220,6 +237,11 @@ void World::confirm_initial_turn()
void World::ok_callback(Command* command)
{
OkEvent ok_event(command);
if (ok_event.is_valid() == false)
{
log_warning("Invalid data for OK command");
return ;
}
this->validate_action(ok_event.get_id(), ok_event.client_id);
}
......@@ -228,6 +250,11 @@ void World::move_callback(Command* command)
// TODO, do an actuall path finding, and other stuff, and
// generate a PathEvent, instead of a MoveEvent.
MoveEvent event(command);
if (event.is_valid() == false)
{
log_warning("Invalid data for MOVE command");
return ;
}
PathEvent* path_event = new PathEvent(event);
unsigned long current_turn = this->turn_handler->get_current_turn();
log_debug("Currently at: " << current_turn);
......@@ -241,7 +268,12 @@ void World::move_callback(Command* command)
void World::path_callback(Command* command)
{
PathEvent* e = new PathEvent(command);
log_debug("Must move unit " << e->actors_ids[0] << "to " << e->x << ":" << e->y << " on turn " << e->turn);
if (e->is_valid() == false)
{
log_warning("Invalid data for PATH command");
return ;
}
log_debug("Must move unit " << e->actors_ids[0] << " to " << e->x << ":" << e->y << " on turn " << e->turn);
Action* action = new Action(boost::bind(&World::do_path, this, _1), e, this->occupants.size());
this->turn_handler->insert_action(action, e->turn);
this->confirm_action(e->get_id());
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment