void ServerSession::handle_read_body(const boost::system::error_code& error) { if (!error) { try { _on_envelope(_read_envelope); } catch (const std::exception& e) { std::ostringstream oss; oss << "ServerSession: Exception during processing of envelope: " << e.what() << " Sending error snip back to client." << std::endl; std::cerr << oss.str() << std::endl; fullcircle::Snip error; error.set_type(fullcircle::Snip::ERROR); fullcircle::Snip_ErrorSnip* esnip=error.mutable_error_snip(); esnip->set_errorcode(fullcircle::Snip::DECODING_ERROR); esnip->set_description(oss.str()); std::ostringstream oss2; if (!error.SerializeToOstream(&oss2)) { std::cout << "error snip serialization did not work." << std::endl; } oss2.flush(); fullcircle::Envelope::Ptr renv(new fullcircle::Envelope()); renv->set_body(oss2.str()); do_write(renv); } // react to the next message of this client. boost::asio::async_read(_socket, boost::asio::buffer(_read_envelope->get_raw_ptr(), Envelope::header_length), boost::bind(&ServerSession::handle_read_header, shared_from_this(), boost::asio::placeholders::error)); } else { handle_connection_error(error, "can't read body"); } }