Commit 35ba8ac5 authored by louiz’'s avatar louiz’

Use an exceptions to handle invalid received protobuf messages

parent b4acfda1
......@@ -18,11 +18,6 @@ void Game::new_entity_callback(Message* message)
{
log_debug("Game::new_entity_callback");
auto srl = message->parse_body_to_protobuf_object<ser::order::NewEntity>();
if (!srl.IsInitialized())
{
log_error("Invalid data received for new entity: " << srl.InitializationErrorString());
return ;
}
Position pos;
pos.x.raw() = srl.pos().x();
......@@ -35,11 +30,6 @@ void Game::move_callback(Message* message)
{
log_debug("Game::move_callback");
auto srl = message->parse_body_to_protobuf_object<ser::order::Move>();
if (!srl.IsInitialized())
{
log_error("Invalid data received for move: " << srl.InitializationErrorString());
return ;
}
std::vector<EntityId> ids;
for (const auto& id: srl.entity_id())
......@@ -67,11 +57,6 @@ void Game::cast_callback(Message* message)
{
log_debug("Game::cast_callback");
auto srl = message->parse_body_to_protobuf_object<ser::order::Cast>();
if (!srl.IsInitialized())
{
log_error("Invalid data received for cast: " << srl.InitializationErrorString());
return ;
}
std::vector<EntityId> ids;
for (const auto& id: srl.entity_id())
......
......@@ -149,22 +149,6 @@ Screen& GameClient::get_screen()
return *this->screen;
}
void GameClient::send_message(const char* name, const google::protobuf::Message& msg)
{
Message* message = new Message;
message->set_name(name);
message->set_body(msg);
this->send(message);
}
void GameClient::send_message(const char* name, const std::string& archive)
{
Message* message = new Message;
message->set_name(name);
message->set_body(archive.data(), archive.length());
this->send(message);
}
void GameClient::on_next_turn(TurnNb turn)
{
}
......@@ -174,11 +158,6 @@ void GameClient::new_occupant_callback(Message* message)
log_debug("new_occupant_callback");
auto srl = message->parse_body_to_protobuf_object<ser::game::Occupant>();
if (!srl.IsInitialized())
{
log_error("Invalid data received for the occupant: " << srl.InitializationErrorString());
return ;
}
auto occupant = std::make_unique<Occupant>(srl);
......@@ -194,11 +173,6 @@ void GameClient::new_occupant_callback(Message* message)
void GameClient::occupant_left_callback(Message* message)
{
auto srl = message->parse_body_to_protobuf_object<ser::game::Occupant>();
if (!srl.IsInitialized())
{
log_error("Invalid data received for the leaving occupant." << srl.InitializationErrorString());
return ;
}
Occupant occupant(srl);
log_debug("Occupant to remove: " << occupant.id);
......@@ -208,11 +182,6 @@ void GameClient::occupant_left_callback(Message* message)
void GameClient::handle_seed_message(Message* message)
{
auto srl = message->parse_body_to_protobuf_object<ser::order::Seed>();
if (!srl.IsInitialized())
{
log_warning("Invalid data for SEED message " << srl.InitializationErrorString());
return ;
}
log_debug("Seed value received: " << srl.value());
this->world.seed(srl.value());
}
......@@ -220,11 +189,6 @@ void GameClient::handle_seed_message(Message* message)
void GameClient::handle_start_message(Message* message)
{
auto srl = message->parse_body_to_protobuf_object<ser::order::Start>();
if (!srl.IsInitialized())
{
log_warning("Invalid data for START message " << srl.InitializationErrorString());
return ;
}
log_debug("The first turn to start is " << srl.turn());
if (srl.turn() != 0)
this->turn_handler.mark_as_ready_until(srl.turn());
......
......@@ -31,9 +31,6 @@ public:
void add_new_occupant(std::unique_ptr<Occupant>&& occupant);
void send_message(const char* name, const google::protobuf::Message& msg);
void send_message(const char* name, const std::string& archive);
/**
* Callbacks set in the WorldCallbacks objects. See over there for
* comments.
......
......@@ -239,11 +239,6 @@ void GameServer::init()
void GameServer::on_move_request(Message* message)
{
auto srl = message->parse_body_to_protobuf_object<ser::request::Move>();
if (!srl.IsInitialized())
{
log_error("Invalid data received for Move request : " << srl.InitializationErrorString());
return ;
}
std::vector<EntityId> ids;
for (const auto& id: srl.entity_id())
ids.push_back(id);
......@@ -261,11 +256,6 @@ void GameServer::on_move_request(Message* message)
void GameServer::on_cast_request(Message* message)
{
auto srl = message->parse_body_to_protobuf_object<ser::request::Cast>();
if (!srl.IsInitialized())
{
log_error("Invalid data received for Cast request: " << srl.InitializationErrorString());
return ;
}
std::vector<EntityId> ids;
for (const auto& id: srl.entity_id())
ids.push_back(id);
......
......@@ -26,6 +26,7 @@
# define MESSAGE_HPP
#include <google/protobuf/message.h>
#include <serialization/exception.hpp>
#include "logging/logging.hpp"
......@@ -89,6 +90,8 @@ public:
ProtobufClass res;
res.ParseFromArray(this->body, this->body_size);
log_debug("parse_body_to_protobuf_object: " << res.ShortDebugString());
if (!res.IsInitialized())
throw SerializationException{res.InitializationErrorString()};
return res;
}
......
......@@ -127,8 +127,16 @@ void MessageHandler::binary_read_handler(const boost::system::error_code& error,
else
{
for (const auto& cb: callbacks)
{
try {
cb(message);
}
catch (const SerializationException& error)
{
log_error("Invalid message received.");
}
}
}
delete message;
this->install_read_handler();
}
......@@ -160,6 +168,22 @@ void MessageHandler::send(Message* message, std::function< void(void) > on_sent)
this->check_messages_to_send();
}
void MessageHandler::send_message(const char* name, const google::protobuf::Message& msg)
{
Message* message = new Message;
message->set_name(name);
message->set_body(msg);
this->send(message);
}
void MessageHandler::send_message(const char* name, const std::string& archive)
{
Message* message = new Message;
message->set_name(name);
message->set_body(archive.data(), archive.length());
this->send(message);
}
bool MessageHandler::check_messages_to_send()
{
log_debug("Length of the queue: " << this->messages_to_send.size());
......
......@@ -70,6 +70,8 @@ public:
* It does not necessarily actually send the message on the socket.
*/
void send(Message* message, std::function< void(void) > on_sent = 0);
void send_message(const char* name, const google::protobuf::Message& msg);
void send_message(const char* name, const std::string& archive);
protected:
/**
......
#ifndef SERIALIZATION_EXCEPTION_HPP_INCLUDED
#define SERIALIZATION_EXCEPTION_HPP_INCLUDED
#include <stdexcept>
class SerializationException: public std::runtime_error
{
public:
SerializationException(const std::string& str):
std::runtime_error(str) {}
};
#endif /* SERIALIZATION_EXCEPTION_HPP_INCLUDED */
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