Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Protocol::AsyncHelper {
- typedef boost::shared_ptr<Protocol::AsyncHelper> AsyncHelperP;
- public:
- AsyncHelper(Protocol &protocol) :
- buf(),
- protocol_(protocol) {}
- template <class AsyncReadStream>
- void AsyncReadMessage(AsyncReadStream &stream,
- AsyncReadHandler handler) {
- AsyncHelperP ptr(this);
- buf.reset(new uint8_t[sizeof(uint32_t) * 2]);
- boost::asio::async_read(stream, boost::asio::buffer(&buf[0],
- sizeof(uint32_t) * 2),
- boost::bind(&AsyncHelper
- ::AsyncReadHeader<AsyncReadStream>,
- this,
- boost::asio::placeholders::error,
- boost::ref(stream),
- handler,
- ptr));
- }
- private:
- template <class AsyncReadStream>
- void AsyncReadHeader(const boost::system::error_code &ec,
- AsyncReadStream &stream,
- AsyncReadHandler handler,
- AsyncHelperP ptr) {
- if (ec) {
- handler(MessageP(), ProtocolExceptionP(
- new ProtocolException("IO error in Protocol::"
- "AsyncHelper::AsyncReadHeader.")));
- } else {
- uint32_t id = reinterpret_cast<uint32_t*>(&buf[0])[0];
- uint32_t size = reinterpret_cast<uint32_t*>(&buf[0])[1];
- buf.reset(new uint8_t[size]);
- boost::asio::async_read(stream, boost::asio::buffer(&buf[0], size),
- boost::bind(&AsyncHelper
- ::AsyncReadBody<AsyncReadStream>,
- this,
- boost::asio::placeholders::error,
- id,
- size,
- handler,
- ptr));
- }
- }
- template <class AsyncReadStream>
- void AsyncReadBody(const boost::system::error_code &ec,
- uint32_t id, uint32_t size,
- AsyncReadHandler handler,
- AsyncHelperP ptr) {
- if (ec) {
- handler(MessageP(), ProtocolExceptionP(
- new ProtocolException("IO error in Protocol::"
- "AsyncHelper::AsyncReadBody.")));
- } else {
- ProtocolExceptionP exception;
- MessageP message;
- try {
- message = protocol_.helpers_[id]->ReadMessage(&buf[0], size);
- } catch (ProtocolException &e) {
- exception.reset(new ProtocolException(e));
- }
- handler(message, exception);
- }
- }
- private:
- boost::scoped_array<uint8_t> buf;
- Protocol &protocol_;
- };
- template <class AsyncReadStream>
- void Protocol::AsyncReadMessage(AsyncReadStream &stream,
- AsyncReadHandler handler) {
- (new AsyncHelper(*this))->AsyncReadMessage(stream, handler);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement