in demo_example/asio/asio/impl/read_until.hpp [1143:1235]
void operator()(asio::error_code ec,
std::size_t bytes_transferred, int start = 0)
{
const std::size_t not_found = (std::numeric_limits<std::size_t>::max)();
std::size_t bytes_to_read;
switch (start_ = start)
{
case 1:
for (;;)
{
{
// Determine the range of the data to be searched.
typedef typename DynamicBuffer_v1::const_buffers_type
buffers_type;
typedef buffers_iterator<buffers_type> iterator;
buffers_type data_buffers = buffers_.data();
iterator begin = iterator::begin(data_buffers);
iterator start_pos = begin + search_position_;
iterator end = iterator::end(data_buffers);
// Look for a match.
std::pair<iterator, bool> result = detail::partial_search(
start_pos, end, delim_.begin(), delim_.end());
if (result.first != end && result.second)
{
// Full match. We're done.
search_position_ = result.first - begin + delim_.length();
bytes_to_read = 0;
}
// No match yet. Check if buffer is full.
else if (buffers_.size() == buffers_.max_size())
{
search_position_ = not_found;
bytes_to_read = 0;
}
// Need to read some more data.
else
{
if (result.first != end)
{
// Partial match. Next search needs to start from beginning of
// match.
search_position_ = result.first - begin;
}
else
{
// Next search can start with the new data.
search_position_ = end - begin;
}
bytes_to_read = std::min<std::size_t>(
std::max<std::size_t>(512,
buffers_.capacity() - buffers_.size()),
std::min<std::size_t>(65536,
buffers_.max_size() - buffers_.size()));
}
}
// Check if we're done.
if (!start && bytes_to_read == 0)
break;
// Start a new asynchronous read operation to obtain more data.
{
ASIO_HANDLER_LOCATION((
__FILE__, __LINE__, "async_read_until"));
stream_.async_read_some(buffers_.prepare(bytes_to_read),
ASIO_MOVE_CAST(read_until_delim_string_op_v1)(*this));
}
return; default:
buffers_.commit(bytes_transferred);
if (ec || bytes_transferred == 0)
break;
if (this->cancelled() != cancellation_type::none)
{
ec = error::operation_aborted;
break;
}
}
const asio::error_code result_ec =
(search_position_ == not_found)
? error::not_found : ec;
const std::size_t result_n =
(ec || search_position_ == not_found)
? 0 : search_position_;
ASIO_MOVE_OR_LVALUE(ReadHandler)(handler_)(result_ec, result_n);
}
}