2012-09-09 04:25:43 +00:00
|
|
|
#include <fc/asio.hpp>
|
2013-06-05 19:19:00 +00:00
|
|
|
#include <fc/thread/thread.hpp>
|
2012-09-09 04:25:43 +00:00
|
|
|
#include <boost/thread.hpp>
|
2013-06-05 19:19:00 +00:00
|
|
|
#include <fc/log/logger.hpp>
|
2012-09-09 04:25:43 +00:00
|
|
|
|
|
|
|
|
namespace fc {
|
|
|
|
|
namespace asio {
|
|
|
|
|
namespace detail {
|
2014-09-09 15:10:37 +00:00
|
|
|
|
|
|
|
|
read_write_handler::read_write_handler(const promise<size_t>::ptr& completion_promise) :
|
|
|
|
|
_completion_promise(completion_promise)
|
2014-09-11 20:30:03 +00:00
|
|
|
{
|
|
|
|
|
// assert(false); // to detect anywhere we're not passing in a shared buffer
|
|
|
|
|
}
|
2014-09-09 15:10:37 +00:00
|
|
|
void read_write_handler::operator()(const boost::system::error_code& ec, size_t bytes_transferred)
|
|
|
|
|
{
|
2014-09-11 20:30:03 +00:00
|
|
|
// assert(false); // to detect anywhere we're not passing in a shared buffer
|
2014-09-09 15:10:37 +00:00
|
|
|
if( !ec )
|
|
|
|
|
_completion_promise->set_value(bytes_transferred);
|
|
|
|
|
else if( ec == boost::asio::error::eof )
|
|
|
|
|
_completion_promise->set_exception( fc::exception_ptr( new fc::eof_exception( FC_LOG_MESSAGE( error, "${message} ", ("message", boost::system::system_error(ec).what())) ) ) );
|
|
|
|
|
else
|
|
|
|
|
_completion_promise->set_exception( fc::exception_ptr( new fc::exception( FC_LOG_MESSAGE( error, "${message} ", ("message", boost::system::system_error(ec).what())) ) ) );
|
|
|
|
|
}
|
|
|
|
|
read_write_handler_with_buffer::read_write_handler_with_buffer(const promise<size_t>::ptr& completion_promise,
|
|
|
|
|
const std::shared_ptr<const char>& buffer) :
|
2014-09-11 20:30:03 +00:00
|
|
|
_completion_promise(completion_promise),
|
2014-09-09 15:10:37 +00:00
|
|
|
_buffer(buffer)
|
|
|
|
|
{}
|
2014-09-11 20:30:03 +00:00
|
|
|
void read_write_handler_with_buffer::operator()(const boost::system::error_code& ec, size_t bytes_transferred)
|
|
|
|
|
{
|
|
|
|
|
if( !ec )
|
|
|
|
|
_completion_promise->set_value(bytes_transferred);
|
|
|
|
|
else if( ec == boost::asio::error::eof )
|
|
|
|
|
_completion_promise->set_exception( fc::exception_ptr( new fc::eof_exception( FC_LOG_MESSAGE( error, "${message} ", ("message", boost::system::system_error(ec).what())) ) ) );
|
|
|
|
|
else
|
|
|
|
|
_completion_promise->set_exception( fc::exception_ptr( new fc::exception( FC_LOG_MESSAGE( error, "${message} ", ("message", boost::system::system_error(ec).what())) ) ) );
|
|
|
|
|
}
|
2014-09-09 15:10:37 +00:00
|
|
|
|
2012-09-09 04:25:43 +00:00
|
|
|
void read_write_handler_ec( promise<size_t>* p, boost::system::error_code* oec, const boost::system::error_code& ec, size_t bytes_transferred ) {
|
|
|
|
|
p->set_value(bytes_transferred);
|
|
|
|
|
*oec = ec;
|
|
|
|
|
}
|
2013-07-14 21:58:27 +00:00
|
|
|
void error_handler( const promise<void>::ptr& p,
|
2012-09-09 04:25:43 +00:00
|
|
|
const boost::system::error_code& ec ) {
|
2014-03-27 18:12:27 +00:00
|
|
|
if( !ec )
|
|
|
|
|
p->set_value();
|
2013-07-14 21:58:27 +00:00
|
|
|
else
|
|
|
|
|
{
|
2014-08-28 19:34:04 +00:00
|
|
|
if( ec == boost::asio::error::eof )
|
2013-07-14 21:58:27 +00:00
|
|
|
{
|
|
|
|
|
p->set_exception( fc::exception_ptr( new fc::eof_exception(
|
|
|
|
|
FC_LOG_MESSAGE( error, "${message} ", ("message", boost::system::system_error(ec).what())) ) ) );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-06-17 19:43:07 +00:00
|
|
|
//elog( "${message} ", ("message", boost::system::system_error(ec).what()));
|
2013-07-14 21:58:27 +00:00
|
|
|
p->set_exception( fc::exception_ptr( new fc::exception(
|
|
|
|
|
FC_LOG_MESSAGE( error, "${message} ", ("message", boost::system::system_error(ec).what())) ) ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-09-09 04:25:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void error_handler_ec( promise<boost::system::error_code>* p,
|
|
|
|
|
const boost::system::error_code& ec ) {
|
|
|
|
|
p->set_value(ec);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename EndpointType, typename IteratorType>
|
|
|
|
|
void resolve_handler(
|
|
|
|
|
const typename promise<std::vector<EndpointType> >::ptr& p,
|
|
|
|
|
const boost::system::error_code& ec,
|
|
|
|
|
IteratorType itr) {
|
|
|
|
|
if( !ec ) {
|
|
|
|
|
std::vector<EndpointType> eps;
|
|
|
|
|
while( itr != IteratorType() ) {
|
|
|
|
|
eps.push_back(*itr);
|
|
|
|
|
++itr;
|
|
|
|
|
}
|
|
|
|
|
p->set_value( eps );
|
|
|
|
|
} else {
|
2013-06-05 19:19:00 +00:00
|
|
|
//elog( "%s", boost::system::system_error(ec).what() );
|
|
|
|
|
//p->set_exception( fc::copy_exception( boost::system::system_error(ec) ) );
|
|
|
|
|
p->set_exception(
|
|
|
|
|
fc::exception_ptr( new fc::exception(
|
|
|
|
|
FC_LOG_MESSAGE( error, "process exited with: ${message} ",
|
|
|
|
|
("message", boost::system::system_error(ec).what())) ) ) );
|
2012-09-09 04:25:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-05 19:19:00 +00:00
|
|
|
|
2013-08-06 16:46:04 +00:00
|
|
|
struct default_io_service_scope
|
|
|
|
|
{
|
|
|
|
|
boost::asio::io_service* io;
|
|
|
|
|
boost::thread* asio_thread;
|
|
|
|
|
boost::asio::io_service::work* the_work;
|
|
|
|
|
|
|
|
|
|
default_io_service_scope()
|
|
|
|
|
{
|
|
|
|
|
io = new boost::asio::io_service();
|
|
|
|
|
the_work = new boost::asio::io_service::work(*io);
|
|
|
|
|
asio_thread = new boost::thread( [=]()
|
|
|
|
|
{
|
|
|
|
|
fc::thread::current().set_name("asio");
|
|
|
|
|
io->run();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~default_io_service_scope()
|
|
|
|
|
{
|
|
|
|
|
delete the_work;
|
|
|
|
|
io->stop();
|
2013-11-25 03:23:29 +00:00
|
|
|
asio_thread->join();
|
2013-08-06 16:46:04 +00:00
|
|
|
delete io;
|
|
|
|
|
delete asio_thread;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
boost::asio::io_service& default_io_service(bool cleanup) {
|
|
|
|
|
static default_io_service_scope fc_asio_service;
|
|
|
|
|
return *fc_asio_service.io;
|
2012-09-09 04:25:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace tcp {
|
2014-08-01 15:31:36 +00:00
|
|
|
std::vector<boost::asio::ip::tcp::endpoint> resolve( const std::string& hostname, const std::string& port)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
resolver res( fc::asio::default_io_service() );
|
|
|
|
|
promise<std::vector<boost::asio::ip::tcp::endpoint> >::ptr p( new promise<std::vector<boost::asio::ip::tcp::endpoint> >("tcp::resolve completion") );
|
|
|
|
|
res.async_resolve( boost::asio::ip::tcp::resolver::query(hostname,port),
|
|
|
|
|
boost::bind( detail::resolve_handler<boost::asio::ip::tcp::endpoint,resolver_iterator>, p, _1, _2 ) );
|
|
|
|
|
return p->wait();;
|
2012-09-09 04:25:43 +00:00
|
|
|
}
|
2014-08-01 15:31:36 +00:00
|
|
|
FC_RETHROW_EXCEPTIONS(warn, "")
|
|
|
|
|
}
|
2012-09-09 04:25:43 +00:00
|
|
|
}
|
|
|
|
|
namespace udp {
|
2014-08-01 15:31:36 +00:00
|
|
|
std::vector<udp::endpoint> resolve( resolver& r, const std::string& hostname, const std::string& port)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
resolver res( fc::asio::default_io_service() );
|
|
|
|
|
promise<std::vector<endpoint> >::ptr p( new promise<std::vector<endpoint> >("udp::resolve completion") );
|
|
|
|
|
res.async_resolve( resolver::query(hostname,port),
|
|
|
|
|
boost::bind( detail::resolve_handler<endpoint,resolver_iterator>, p, _1, _2 ) );
|
|
|
|
|
return p->wait();
|
2012-09-09 04:25:43 +00:00
|
|
|
}
|
2014-08-01 15:31:36 +00:00
|
|
|
FC_RETHROW_EXCEPTIONS(warn, "")
|
|
|
|
|
}
|
2012-09-09 04:25:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} } // namespace fc::asio
|