14 Mayıs 2017 Pazar

asio read_until metodu

Giriş
Açıklaması şöyle
read_until may return a buffer that contains more data (it will return as soon as the delimiter is satisfied, but more data may have been read)
read_until - streambuf
Şöyle yaparız.
boost::asio::streambuf buf;
int read = boost::asio::read_until (socket, buf, "\r\n\r\n");
read_until - streambuf + error_code
Şöyle yaparız.
boost::asio::streambuf buf;
boost::system::error_code ec;
boost::asio::read_until(socket, buffer, "]}", ec);
read_until - streambuf + corotouine
Şöyle yaparız.
try {
  boost::asio::streambuf req_buf(100 * 1024); //Limit memory for headers
  boost::asio::async_read_until(*sock, req_buf, "\r\n\r\n", yield);
}catch(boost::system::system_error &e) {
  if( e.code() == boost::asio::error::make_error_code(
    boost::asio::error::misc_errors::not_found) )
    log("HTTP Connection close: cannot find headers end");
    //..
}
read_until - streambuf + match_condition
İmzası şöyle
template<
  typename SyncReadStream,
  typename Allocator,
  typename MatchCondition>
std::size_t read_until(
  SyncReadStream & s,
  boost::asio::basic_streambuf< Allocator > & b,
  MatchCondition match_condition,
  boost::system::error_code & ec,
  typename enable_if< is_match_condition< MatchCondition >::value >::type *  = 0);
Pair döner. İlk parametreyi anlamadım. İkincisi ise eşleşmenin başarılı olup olmadığıdır. Şöyle yaparız.
using iterator = buffers_iterator<basic_streambuf<Allocator>::const_buffers_type>;
/**
\brief Make read_until stop when either: 
    * the stop character was found
    * more than 100MB have been read
*/
pair<iterator, bool> match_condition(iterator begin, iterator end) {
   // to much data?
   if(end - begin > 100*1024*1024) {
      return std::make_pair(begin, true);
   }
   // try and find stop character
   for(auto i = begin; i < end; i++) {
      auto c = i.rdbuf()->sgetc();
      if(c == STOP_CHARACTER) {
         return std::make_pair(i, true);
      } 
   }

   return std::make_pair(begin, false);
}

Hiç yorum yok:

Yorum Gönder