Advertisement
Guest User

Untitled

a guest
Jul 25th, 2012
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. template <class AsyncReadStream >
  2. class Protocol::AsyncHelper:
  3.   public boost::enable_shared_from_this< Protocol::
  4.                                          AsyncHelper< AsyncReadStream > > {
  5.  
  6.  public:
  7.   AsyncHelper(Protocol &protocol,
  8.               AsyncReadStream &stream,
  9.               AsyncReadHandler handler) :
  10.       buf(),
  11.       protocol_(protocol),
  12.       stream_(stream),
  13.       handler_(handler) {}
  14.  
  15.   void AsyncReadMessage() {
  16.     buf.reset(new uint8_t[sizeof(uint32_t) * 2]);
  17.     boost::asio::async_read(stream_, boost::asio::buffer(&buf[0],
  18.                                                         sizeof(uint32_t) * 2),
  19.                             boost::bind(&AsyncHelper::AsyncReadHeader,
  20.                                         shared_from_this(),
  21.                                         boost::asio::placeholders::error));
  22.   }
  23.  private:
  24.   void AsyncReadHeader(const boost::system::error_code &ec) {
  25.     if (ec) {
  26.       handler_(MessageP(), ProtocolExceptionP(
  27.           new ProtocolException("IO error in Protocol::"
  28.                                 "AsyncHelper::AsyncReadHeader.")));
  29.       return;
  30.     }
  31.     uint32_t id = reinterpret_cast<uint32_t*>(&buf[0])[0];
  32.     uint32_t size = reinterpret_cast<uint32_t*>(&buf[0])[1];
  33.     buf.reset(new uint8_t[size]);
  34.     boost::asio::async_read(stream_, boost::asio::buffer(&buf[0], size),
  35.                             boost::bind(&AsyncHelper::AsyncReadBody,
  36.                                         shared_from_this(),
  37.                                         boost::asio::placeholders::error,
  38.                                         id,
  39.                                         size);
  40.   }
  41.  
  42.   void AsyncReadBody(const boost::system::error_code &ec,
  43.                      uint32_t id, uint32_t size) {
  44.     if (ec) {
  45.       handler_(MessageP(), ProtocolExceptionP(
  46.           new ProtocolException("IO error in Protocol::"
  47.                                 "AsyncHelper::AsyncReadBody.")));
  48.       return;
  49.     }
  50.    
  51.     ProtocolExceptionP exception;
  52.     MessageP message;
  53.     try {
  54.       message = protocol_.helpers_[id]->ReadMessage(&buf[0], size);
  55.     } catch (ProtocolException &e) {
  56.       exception.reset(new ProtocolException(e));
  57.     }
  58.     handler_(message, exception);
  59.   }
  60.  private:
  61.   boost::scoped_array<uint8_t> buf;
  62.   Protocol &protocol_;
  63.   AsyncReadStream &stream_;
  64.   AsyncReadHandler handler_;
  65. };
  66.  
  67. template <class AsyncReadStream>
  68. void Protocol::AsyncReadMessage(AsyncReadStream &stream,
  69.                                 AsyncReadHandler handler) {
  70.   typedef AsyncHelper< AsyncReadStream > Helper;
  71.   boost::shared_ptr< Helper > helper(new Helper(*this, stream, handler));
  72.   helper->AsyncReadMessage();
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement