Bytemaster's Boost Libraries
/Users/dlarimer/dev/libs/cmt/include/boost/cmt/asio.hpp
Go to the documentation of this file.
00001 
00005 #ifndef _BOOST_CMT_ASIO_HPP_
00006 #define _BOOST_CMT_ASIO_HPP_
00007 #include <boost/asio.hpp>
00008 #include <boost/thread.hpp>
00009 #include <boost/cmt/future.hpp>
00010 
00011 namespace boost { namespace cmt { namespace asio {
00012     namespace detail {
00013         using namespace boost::cmt;
00014 
00015         void read_write_handler( const promise<size_t>::ptr& p, 
00016                                  const boost::system::error_code& ec, 
00017                                 size_t bytes_transferred );
00018         void read_write_handler_ec( promise<size_t>* p, 
00019                                     boost::system::error_code* oec, 
00020                                     const boost::system::error_code& ec, 
00021                                     size_t bytes_transferred );
00022 
00023         void error_handler( const promise<boost::system::error_code>::ptr& p, 
00024                               const boost::system::error_code& ec );
00025         void error_handler_ec( promise<boost::system::error_code>* p, 
00026                               const boost::system::error_code& ec ); 
00027 
00028         template<typename EndpointType, typename IteratorType>
00029         void resolve_handler( 
00030                              const typename promise<std::vector<EndpointType> >::ptr& p,
00031                              const boost::system::error_code& ec, 
00032                              IteratorType itr) {
00033             if( !ec ) {
00034                 std::vector<EndpointType> eps;
00035                 while( itr != IteratorType() ) {
00036                     eps.push_back(*itr);
00037                     ++itr;
00038                 }
00039                 p->set_value( eps );
00040             } else {
00041                 p->set_exception( boost::copy_exception( boost::system::system_error(ec) ) );
00042             }
00043         }
00044     }
00051     boost::asio::io_service& default_io_service();
00052 
00056     template<typename AsyncReadStream, typename MutableBufferSequence>
00057     size_t read( AsyncReadStream& s, const MutableBufferSequence& buf, const microseconds& timeout_us = microseconds::max() ) {
00058         promise<size_t>::ptr p(new promise<size_t>());
00059         boost::asio::async_read( s, buf, boost::bind( detail::read_write_handler, p, _1, _2 ) );
00060         return p->wait(timeout_us);
00061     }
00065     template<typename AsyncReadStream, typename MutableBufferSequence>
00066     size_t read_some( AsyncReadStream& s, const MutableBufferSequence& buf, const microseconds& timeout_us = microseconds::max() ) {
00067         promise<size_t>::ptr p(new promise<size_t>());
00068         s.async_read_some( buf, boost::bind( detail::read_write_handler, p, _1, _2 ) );
00069         return p->wait(timeout_us);
00070     }
00071 
00075     template<typename AsyncReadStream, typename MutableBufferSequence>
00076     size_t write( AsyncReadStream& s, const MutableBufferSequence& buf, const microseconds& timeout_us = microseconds::max() ) {
00077         promise<size_t>::ptr p(new promise<size_t>());
00078         boost::asio::async_write( s, buf, boost::bind( detail::read_write_handler, p, _1, _2 ) );
00079         return p->wait(timeout_us);
00080     }
00081 
00085     template<typename AsyncReadStream, typename MutableBufferSequence>
00086     size_t write_some( AsyncReadStream& s, const MutableBufferSequence& buf, const microseconds& timeout_us = microseconds::max() ) {
00087         promise<size_t>::ptr p(new promise<size_t>());
00088         s.async_write_some(  buf, boost::bind( detail::read_write_handler, p, _1, _2 ) );
00089         return p->wait(timeout_us);
00090     }
00091 
00092     namespace tcp {
00093         typedef boost::asio::ip::tcp::endpoint endpoint;
00094         typedef boost::asio::ip::tcp::resolver::iterator resolver_iterator;
00095         typedef boost::asio::ip::tcp::resolver resolver;
00097         std::vector<endpoint> resolve( const std::string& hostname, const std::string& port, const microseconds& timeout_us = microseconds::max() );
00098 
00100         template<typename SocketType, typename AcceptorType>
00101         boost::system::error_code accept( AcceptorType& acc, SocketType& sock, const microseconds& timeout_us = microseconds::max() ) {
00102             promise<boost::system::error_code>::ptr p( new promise<boost::system::error_code>() );
00103             acc.async_accept( sock, boost::bind( boost::cmt::asio::detail::error_handler, p, _1 ) );
00104             return p->wait( timeout_us );
00105         }
00106 
00108         template<typename AsyncSocket, typename EndpointType>
00109         boost::system::error_code connect( AsyncSocket& sock, const EndpointType& ep, const microseconds& timeout_us = microseconds::max() ) {
00110             promise<boost::system::error_code>::ptr p(new promise<boost::system::error_code>());
00111             sock.async_connect( ep, boost::bind( boost::cmt::asio::detail::error_handler, p, _1 ) );
00112             return p->wait(timeout_us);
00113         }
00114 
00115 
00116     }
00117     namespace udp {
00118         typedef boost::asio::ip::udp::endpoint endpoint;
00119         typedef boost::asio::ip::udp::resolver::iterator resolver_iterator;
00120         typedef boost::asio::ip::udp::resolver resolver;
00122         std::vector<endpoint> resolve( resolver& r, const std::string& hostname, const std::string& port, const microseconds& timeout_us = microseconds::max() );
00123     }
00124 
00125 
00126 } } } // namespace boost::cmt::asio
00127 
00128 #endif // _BOOST_CMT_ASIO_HPP_
 All Classes Namespaces Files Functions Variables Typedefs Defines