繁体   English   中英

如何阅读Boost ASIO streambuf?

[英]How to read from Boost ASIO streambuf?

有没有办法从streambuf读取而不删除字节?

我正在从缓冲区中读取“消息大小”字段以检查是否收到了整个消息。

如果没有,我发布另一个异步读取来获取它,但处理程序然后无法知道消息应该有多长 - 因为大小字段已被删除。

任何帮助赞赏!

例如

boost::asio::streambuf _buffer;

void onReceive(const boost::system::error_code& e, std::size_t bytesTransferred)
{
  if(e) return;

  if(_buffer.size() > 0)
  {
    // Partial message was previously received, but I don't know how long.
  }
  else
  {
    _buffer.commit(bytesTransferred);

    /* Read the size (and remove it from the stream) */
    unsigned short size = 0;
    std::istream in(&_buffer);
    in.read((char*)&size, sizeof(unsigned short);

    /* Got the whole message? */
    if(_buffer.size() > size)
    {
      /* Yes. */
    }
    else
    {
      /* No - read the rest. */
      boost::asio::async_read(/*...*/);
    }
  }
}

您可以使用read_async使用消息头的大小启动读取,然后在“完成条件”回调中调整它,如下所示:

typedef boost::system::error_code error_code;

template <typename Stream, typename Message>
void MessageReader<Stream, Message>::startRead()
{
  readBuffer = allocateMsg();
  async_read(stream, 
             boost::asio::buffer(readBuffer.get(), sizeof(*readBuffer)),
             boost::bind(&MessageReader<Stream, Message>::bytesToRead, this,
                         boost::asio::placeholders::error, 
                         boost::asio::placeholders::bytes_transferred),
             boost::bind(&MessageReader<Stream, Message>::readDone, this, 
                         boost::asio::placeholders::error, 
                         boost::asio::placeholders::bytes_transferred));
}

template <typename Stream, typename Message>
size_t MessageReader<Stream, Message>::bytesToRead(const error_code& error, 
                                                   size_t bytes_read)
{
  size_t result;

  if (error)
    result = 0;                                         // error - stop reading

  else if (bytes_read < sizeof(CmnMessageHeader))
    result = sizeof(CmnMessageHeader) - bytes_read;     // read rest of header

  else if (readBuffer->header.byteCount > sizeof(*readBuffer))
    result = 0;                                         // bad byte count

  else
    result = readBuffer->header.byteCount - bytes_read; // read message body

  return result;
}

template <typename Stream, typename Message>
void MessageReader<Stream, Message>::readDone(const error_code& error, 
                                              size_t bytes_read)
{
  if (error)
  {
    if (error.value() == boost::system::errc::no_such_file_or_directory)
    {
      notifyStop();
    }

    else if (error.value() != boost::system::errc::operation_canceled)
    {
      notifyStop();
    }

    // else the operation was cancelled, thus no stop notification is needed and
    // we can merely return
  }

  else if (bytes_read != readBuffer->header.byteCount)
  {
    LOG4CXX_ERROR(logger, "Message byte count mismatch");
    notifyStop();
  }

  else
  {
    handleMsg(readBuffer);
    startRead();
  }
}

编辑:为error_code添加了typedef。

我昨天这样做了。 所以我想我会提供我的解决方案......

#include <iostream>
#include <sstream>
#include <algorithm>
#include <iterator>

#include <boost/asio.hpp>
#include <boost/asio/streambuf.hpp>

void ReadFromStreambuf()
{
    boost::asio::streambuf mybuffer;

    // write some data to the buffer
    std::ostream o2buffer (&mybuffer);
    o2buffer << "hello stackoverflow";

    // get buffer size
    size_t nBufferSize = boost::asio::buffer_size(mybuffer.data());

    // get const buffer
    std::stringstream ssOut;
    boost::asio::streambuf::const_buffers_type constBuffer = mybuffer.data();

    // copy const buffer to stringstream, then output
    std::copy(
        boost::asio::buffers_begin(constBuffer),
        boost::asio::buffers_begin(constBuffer) + nBufferSize,
        std::ostream_iterator<char>(ssOut)
    );

    std::cout << ssOut.str() << "\n";
}


int main(int argc, char const *argv[])
{
    ReadFromStreambuf();
    return 0;
}

您可以采用两种方法:

  1. 发出单个读取以读取大小的字节数(例如4),问题是读取所需大小。

  2. 使用read some调用,缓冲代码中的字节,比如在向量中并以这种方式进行分析。

我会去选项2,它确实意味着复制缓冲区,但是我会冒险它比多次读取一些调用便宜。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM