| 213 | // asynchronous operation completes, this function will be invoked. |
| 214 | |
| 215 | void |
| 216 | operator()( |
| 217 | beast::error_code ec, |
| 218 | std::size_t bytes_transferred = 0, |
| 219 | bool cont = true) /*< Second and subsequent invocations will seee `cont=true`. */ |
| 220 | { |
| 221 | // The `reenter` keyword transfers control to the last |
| 222 | // yield point, or to the beginning of the scope if |
| 223 | // this is the first time. |
| 224 | |
| 225 | reenter(*this) |
| 226 | { |
| 227 | for(;;) |
| 228 | { |
| 229 | std::size_t pos; |
| 230 | |
| 231 | // Search for a newline in the readable bytes of the buffer |
| 232 | pos = find_newline(buffer_.data()); |
| 233 | |
| 234 | // If we don't have the newline, then read more |
| 235 | if(pos == 0) |
| 236 | { |
| 237 | std::size_t bytes_to_read; |
| 238 | |
| 239 | // Determine the number of bytes to read, |
| 240 | // using available capacity in the buffer first. |
| 241 | |
| 242 | bytes_to_read = std::min<std::size_t>( |
| 243 | std::max<std::size_t>(512, // under 512 is too little, |
| 244 | buffer_.capacity() - buffer_.size()), |
| 245 | std::min<std::size_t>(65536, // and over 65536 is too much. |
| 246 | buffer_.max_size() - buffer_.size())); |
| 247 | |
| 248 | // Read some data into our dynamic buffer_. We transfer |
| 249 | // ownership of the composed operation by using the |
| 250 | // `std::move(*this)` idiom. The `yield` keyword causes |
| 251 | // the function to return immediately after the initiating |
| 252 | // function returns. |
| 253 | |
| 254 | yield stream_.async_read_some( |
| 255 | buffer_.prepare(bytes_to_read), std::move(*this)); |
| 256 | |
| 257 | // After the `async_read_some` completes, control is |
| 258 | // transferred to this line by the `reenter` keyword. |
| 259 | |
| 260 | // Move the bytes read from the writable area to the |
| 261 | // readable area. |
| 262 | |
| 263 | buffer_.commit(bytes_transferred); |
| 264 | |
| 265 | // If an error occurs, deliver it to the caller's completion handler. |
| 266 | if(ec) |
| 267 | break; |
| 268 | |
| 269 | // Keep looping until we get the newline |
| 270 | continue; |
| 271 | } |
| 272 | |