Bytemaster's Boost Libraries
/Users/dlarimer/dev/libs/cmt/include/boost/cmt/asio/streambuf.hpp
00001 #ifndef _BOOST_CMT_BASIC_STREAM_BUF_HPP_
00002 #define _BOOST_CMT_BASIC_STREAM_BUF_HPP_
00003 #include <boost/asio/streambuf.hpp>
00004 #include <boost/cmt/asio.hpp>
00005 
00006 namespace boost { namespace cmt { namespace asio {
00007 
00008 template <typename SocketType, typename Allocator = std::allocator<char> >
00009 class socket_streambuf : public boost::asio::basic_streambuf<Allocator> {
00010     public:
00015       explicit socket_streambuf( SocketType& sock,
00016           std::size_t max_size = (std::numeric_limits<std::size_t>::max)(),
00017           const Allocator& allocator = Allocator())
00018         : boost::asio::basic_streambuf<Allocator>(max_size, allocator),m_sock(sock)
00019       {
00020       }
00021     protected:
00025       int underflow()
00026       {
00027         if (gptr() < pptr())
00028         {
00029           setg(&buffer_[0], gptr(), pptr());
00030           return traits_type::to_int_type(*gptr());
00031         }
00032         else
00033         {
00034            uint32_t av = m_sock.bytes_available();
00035            char tmp[1024];
00036            size_t s = boost::cmt::read_some( m_sock, boost::asio::buffer(tmp,sizeof(tmp)) );
00037            prepare(s) 
00038            memcpy(boost::asio::buffer_cast<char*>(prepare(tmp)), tmp, s);
00039            commit(s);
00040            
00041            if (gptr() < pptr()) {
00042               setg(&buffer_[0], gptr(), pptr());
00043               return traits_type::to_int_type(*gptr());
00044            }
00045             
00046            return traits_type::eof();
00047         }
00048       }
00049 
00050        SocketType& m_sock;
00051 };
00052 
00053 
00054 } } } // namespace boost::cmt::asio
00055 
00056 #endif // _BOOST_CMT_BASIC_STREAM_BUF_HPP_
 All Classes Namespaces Files Functions Variables Typedefs Defines