Guest User

Untitled

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