View difference between Paste ID: 2RXqfDp0 and p8nQ5NFi
SHOW: | | - or go back to the newest paste.
1-
class Protocol::AsyncHelper {
1+
template <class AsyncReadStream >
2-
  typedef boost::shared_ptr<Protocol::AsyncHelper> AsyncHelperP;
2+
class Protocol::AsyncHelper:
3
  public boost::enable_shared_from_this< Protocol::
4-
  AsyncHelper(Protocol &protocol) :
4+
                                         AsyncHelper< AsyncReadStream > > {
5
6-
      protocol_(protocol) {}
6+
7-
  template <class AsyncReadStream>
7+
  AsyncHelper(Protocol &protocol,
8-
  void AsyncReadMessage(AsyncReadStream &stream, 
8+
              AsyncReadStream &stream,
9-
                        AsyncReadHandler handler) {
9+
              AsyncReadHandler handler) :
10-
    AsyncHelperP ptr(this);
10+
11
      protocol_(protocol),
12-
    boost::asio::async_read(stream, boost::asio::buffer(&buf[0], 
12+
      stream_(stream),
13
      handler_(handler) {}
14-
                            boost::bind(&AsyncHelper
14+
  
15-
                                        ::AsyncReadHeader<AsyncReadStream>, 
15+
  void AsyncReadMessage() {
16-
                                        this, 
16+
17-
                                        boost::asio::placeholders::error, 
17+
    boost::asio::async_read(stream_, boost::asio::buffer(&buf[0], 
18-
                                        boost::ref(stream),
18+
19-
                                        handler,
19+
                            boost::bind(&AsyncHelper::AsyncReadHeader, 
20-
                                        ptr));
20+
                                        shared_from_this(), 
21
                                        boost::asio::placeholders::error));
22
  }
23-
  template <class AsyncReadStream>
23+
24-
  void AsyncReadHeader(const boost::system::error_code &ec,
24+
  void AsyncReadHeader(const boost::system::error_code &ec) {
25-
                       AsyncReadStream &stream,
25+
26-
                       AsyncReadHandler handler,
26+
      handler_(MessageP(), ProtocolExceptionP(
27-
                       AsyncHelperP ptr) {
27+
28
                                "AsyncHelper::AsyncReadHeader.")));
29-
      handler(MessageP(), ProtocolExceptionP(
29+
      return;
30
    } 
31
    uint32_t id = reinterpret_cast<uint32_t*>(&buf[0])[0];
32-
    } else {
32+
    uint32_t size = reinterpret_cast<uint32_t*>(&buf[0])[1];
33-
      uint32_t id = reinterpret_cast<uint32_t*>(&buf[0])[0];
33+
    buf.reset(new uint8_t[size]);
34-
      uint32_t size = reinterpret_cast<uint32_t*>(&buf[0])[1];
34+
    boost::asio::async_read(stream_, boost::asio::buffer(&buf[0], size),
35-
      buf.reset(new uint8_t[size]);
35+
                            boost::bind(&AsyncHelper::AsyncReadBody, 
36-
      boost::asio::async_read(stream, boost::asio::buffer(&buf[0], size),
36+
                                        shared_from_this(),
37-
                              boost::bind(&AsyncHelper
37+
                                        boost::asio::placeholders::error,
38-
                                          ::AsyncReadBody<AsyncReadStream>, 
38+
                                        id,
39-
                                          this,
39+
                                        size);
40-
                                          boost::asio::placeholders::error, 
40+
41-
                                          id,
41+
42-
                                          size,
42+
43-
                                          handler,
43+
                     uint32_t id, uint32_t size) {
44-
                                          ptr));
44+
45
      handler_(MessageP(), ProtocolExceptionP(
46
          new ProtocolException("IO error in Protocol::"
47
                                "AsyncHelper::AsyncReadBody.")));
48-
  template <class AsyncReadStream>
48+
      return;
49
    }
50-
                     uint32_t id, uint32_t size,
50+
    
51-
                     AsyncReadHandler handler,
51+
    ProtocolExceptionP exception;
52-
                     AsyncHelperP ptr) {
52+
    MessageP message;
53
    try {
54-
      handler(MessageP(), ProtocolExceptionP(
54+
      message = protocol_.helpers_[id]->ReadMessage(&buf[0], size);
55
    } catch (ProtocolException &e) {
56
      exception.reset(new ProtocolException(e));
57-
    } else {
57+
58-
      ProtocolExceptionP exception;
58+
    handler_(message, exception);
59-
      MessageP message;
59+
60-
      try {
60+
61-
        message = protocol_.helpers_[id]->ReadMessage(&buf[0], size);
61+
62-
      } catch (ProtocolException &e) {
62+
63-
        exception.reset(new ProtocolException(e));
63+
  AsyncReadStream &stream_;
64-
      }
64+
  AsyncReadHandler handler_;
65-
      handler(message, exception);
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
}