Guest User

Untitled

a guest
Oct 16th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. /** Read a line and echo it back.
  2.  
  3. This function is used to read a line ending in a carriage-return
  4. ("CR") from the stream, and then write it back. The function call
  5. blocks until one of the following conditions is true:
  6.  
  7. @li A line was read in and sent back on the stream
  8.  
  9. @li An error occurs.
  10.  
  11. The implementation may read additional octets that lie past
  12. the end of the line being read. These octets are silently
  13. discarded.
  14.  
  15. @param The stream to operate on. The type must meet the
  16. requirements of @b AsyncReadStream and @AsyncWriteStream
  17.  
  18. @param ec Set to the error, if any occurred.
  19. */
  20. template<class SyncStream>
  21. void
  22. echo(SyncStream& stream, boost::beast::error_code& ec)
  23. {
  24. // This dynamic buffer will be used to hold the line of text
  25. boost::asio::streambuf buffer;
  26.  
  27. // Read a line of text from the stream into the buffer
  28. auto const bytes_matched = boost::asio::read_until(stream, buffer, "\r", ec);
  29. if(ec)
  30. return;
  31.  
  32. // Now echo the text. Since we could have read past the carriage
  33. // return, we use buffer_prefix to send only the text up to and
  34. // including the carriage return.
  35. boost::asio::write(stream, boost::beast::buffers_prefix(bytes_matched, buffer.data()), ec);
  36. if(ec)
  37. return;
  38.  
  39. // If we get here then there was no error
  40. }
Add Comment
Please, Sign In to add comment