26 Ekim 2017 Perşembe

asio streambuf'tan Direkt Okuma

consume metodu
Açıklaması şöyle
asio::streambuf is based on std::vector that grows as needed, but never shrinks. So, consume() is not supposed to release memory, it just adjusts internal pointers:
Metodun içi şöyledir.
void consume(std::size_t n)
{
  if (egptr() < pptr())
    setg(&buffer_[0], gptr(), pptr());
  if (gptr() + n > pptr())
    n = pptr() - gptr();
  gbump(static_cast<int>(n));
}
Örnek
Şöyle yaparız.
std::string extract_direct(asio::streambuf& sb)
{
  auto buffer = sb.data();
  auto first = asio::buffer_cast<const char*>(buffer);
  auto bufsiz = asio::buffer_size(buffer);
  auto last = first + bufsiz;

  auto nlpos = std::find(first, last, '\n');

  auto result = std::string(first, nlpos);

  auto to_consume = std::min(std::size_t(std::distance(first, nlpos) + 1), bufsiz);
  sb.consume(to_consume);
  return result;
}
consume metodu - istream ile
Okunan verinin işlendiğini varsayar. Şöyle yaparız.
const auto bufferIterator = boost::asio::buffers_begin(buf.data());
const std::string message (bufferIterator, bufferIterator + bytes_transferred);
streambuf.consume (bytes_transferred);
consume metodu - ostream ile
ostream ile streambuf nesnesine yazılan verinin okunduğunu varsayar ve siler. Şöyle yaparız.
// Consume 10 bytes from the input sequence.
buf.consume (10);
Socket'e veri gönderdikten sonra streambuf nesnesini boşaltmak için şöyle yaparız
// The input and output sequence are empty.
boost::asio::streambuf buf;
std::ostream os(&buf);
// prepare() and write to the output sequence, then commit the written
// data to the input sequence.  The output sequence is empty and
// input sequence contains "Hello, World!\n".
os << "Hello, World!\n";

// Read from the input sequence, writing to the socket.  The input and
// output sequences remain unchanged.
size_t n = sock.send(buf.data());
// Remove 'n' bytes from the input sequence. If the send operation sent
// the entire buffer, then the input sequence would be empty.
buf.consume(n);

Hiç yorum yok:

Yorum Gönder