Commit e4adf991 authored by Dominik Charousset's avatar Dominik Charousset

handle and log exceptions correctly in default_peer

parent 69913604
......@@ -33,13 +33,15 @@
#include <cstring>
#include <sstream>
#include <iterator>
#include <iostream>
#include <exception>
#include <stdexcept>
#include <type_traits>
#include "cppa/detail/logging.hpp"
#include "cppa/binary_deserializer.hpp"
using std::enable_if;
using namespace std;
namespace cppa {
......@@ -49,32 +51,33 @@ typedef const char* iterator;
inline void range_check(iterator begin, iterator end, size_t read_size) {
if ((begin + read_size) > end) {
throw std::out_of_range("binary_deserializer::read()");
CPPA_LOGF_ERROR("range_check failed");
throw out_of_range("binary_deserializer::read()");
}
}
template<typename T>
iterator read(iterator begin, iterator end, T& storage,
typename enable_if<std::is_integral<T>::value>::type* = 0) {
typename enable_if<is_integral<T>::value>::type* = 0) {
range_check(begin, end, sizeof(T));
memcpy(&storage, begin, sizeof(T));
return begin + sizeof(T);
}
iterator read(iterator begin, iterator end, std::string& storage) {
std::uint32_t str_size;
iterator read(iterator begin, iterator end, string& storage) {
uint32_t str_size;
begin = read(begin, end, str_size);
range_check(begin, end, str_size);
storage.clear();
storage.reserve(str_size);
iterator cpy_end = begin + str_size;
std::copy(begin, cpy_end, std::back_inserter(storage));
copy(begin, cpy_end, back_inserter(storage));
return begin + str_size;
}
template<typename CharType, typename StringType>
iterator read_unicode_string(iterator begin, iterator end, StringType& str) {
std::uint32_t str_size;
uint32_t str_size;
begin = read(begin, end, str_size);
str.reserve(str_size);
for (size_t i = 0; i < str_size; ++i) {
......@@ -88,25 +91,25 @@ iterator read_unicode_string(iterator begin, iterator end, StringType& str) {
// @returns the next iterator position
template<typename T>
iterator read(iterator begin, iterator end, T& value,
typename enable_if<std::is_floating_point<T>::value>::type* = 0) {
typename enable_if<is_floating_point<T>::value>::type* = 0) {
// floating points are written as strings
std::string str;
string str;
auto result = read_unicode_string<char>(begin, end, str);
std::istringstream iss(str);
istringstream iss(str);
iss >> value;
return result;
}
iterator read(iterator begin, iterator end, std::u16string& storage) {
iterator read(iterator begin, iterator end, u16string& storage) {
// char16_t is guaranteed to has *at least* 16 bytes,
// but not to have *exactly* 16 bytes; thus use uint16_t
return read_unicode_string<std::uint16_t>(begin, end, storage);
return read_unicode_string<uint16_t>(begin, end, storage);
}
iterator read(iterator begin, iterator end, std::u32string& storage) {
iterator read(iterator begin, iterator end, u32string& storage) {
// char32_t is guaranteed to has *at least* 32 bytes,
// but not to have *exactly* 32 bytes; thus use uint32_t
return read_unicode_string<std::uint32_t>(begin, end, storage);
return read_unicode_string<uint32_t>(begin, end, storage);
}
struct pt_reader {
......@@ -133,34 +136,32 @@ binary_deserializer::binary_deserializer(const char* bbegin, const char* bend,
actor_addressing* addressing)
: super(addressing), pos(bbegin), end(bend) { }
std::string binary_deserializer::seek_object() {
std::string result;
string binary_deserializer::seek_object() {
string result;
pos = read(pos, end, result);
return result;
}
std::string binary_deserializer::peek_object() {
std::string result;
string binary_deserializer::peek_object() {
string result;
read(pos, end, result);
return result;
}
void binary_deserializer::begin_object(const std::string&) {
}
void binary_deserializer::begin_object(const string&) { }
void binary_deserializer::end_object() {
}
void binary_deserializer::end_object() { }
size_t binary_deserializer::begin_sequence() {
static_assert(sizeof(size_t) >= sizeof(std::uint32_t),
CPPA_LOG_TRACE("");
static_assert(sizeof(size_t) >= sizeof(uint32_t),
"sizeof(size_t) < sizeof(uint32_t)");
std::uint32_t result;
uint32_t result;
pos = read(pos, end, result);
return static_cast<size_t>(result);
}
void binary_deserializer::end_sequence() {
}
void binary_deserializer::end_sequence() { }
primitive_variant binary_deserializer::read_value(primitive_type ptype) {
primitive_variant val(ptype);
......@@ -174,7 +175,7 @@ void binary_deserializer::read_tuple(size_t size,
const primitive_type* ptypes,
primitive_variant* storage) {
for (auto end = ptypes + size; ptypes != end; ++ptypes) {
*storage = std::move(read_value(*ptypes));
*storage = move(read_value(*ptypes));
++storage;
}
}
......
......@@ -135,7 +135,13 @@ continue_reading_result default_peer::continue_reading() {
addressed_message msg;
binary_deserializer bd(m_rd_buf.data(), m_rd_buf.size(),
m_parent->addressing());
m_meta_msg->deserialize(&msg, &bd);
try { m_meta_msg->deserialize(&msg, &bd); }
catch (exception& e) {
CPPA_LOG_ERROR("exception during read_message: "
<< detail::demangle(typeid(e))
<< ", what(): " << e.what());
return read_failure;
}
auto& content = msg.content();
CPPA_LOG_DEBUG("deserialized: " << to_string(msg));
//DEBUG("<-- " << to_string(msg));
......
......@@ -155,10 +155,10 @@ actor_ptr default_protocol::remote_actor(io_stream_ptr_pair io,
// throws on error
io.second->write(&process_id, sizeof(std::uint32_t));
io.second->write(pinf->node_id().data(), pinf->node_id().size());
std::uint32_t remote_aid;
actor_id remote_aid;
std::uint32_t peer_pid;
process_information::node_id_type peer_node_id;
io.first->read(&remote_aid, sizeof(remote_aid));
io.first->read(&remote_aid, sizeof(actor_id));
io.first->read(&peer_pid, sizeof(std::uint32_t));
io.first->read(peer_node_id.data(), peer_node_id.size());
process_information_ptr pinfptr(new process_information(peer_pid, peer_node_id));
......
......@@ -121,7 +121,8 @@ network::io_stream_ptr ipv4_io_stream::from_native_socket(native_socket_type fd)
}
network::io_stream_ptr ipv4_io_stream::connect_to(const char* host,
std::uint16_t port) {
std::uint16_t port) {
CPPA_LOG_INFO("try to connect to " << host << " on port " << port);
struct sockaddr_in serv_addr;
struct hostent* server;
native_socket_type fd = socket(AF_INET, SOCK_STREAM, 0);
......
......@@ -218,7 +218,7 @@ int client_part(const vector<string_pair>& args) {
} // namespace <anonymous>
void verbose_terminate() {
try { if (std::uncaught_exception()); throw; }
try { if (std::uncaught_exception()) throw; }
catch (std::exception& e) {
cerr << "terminate called after throwing "
<< to_verbose_string(e) << endl;
......
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