write.hpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* Copyright (c) 2018-2023 Marcelo Zimbres Silva ([email protected])
  2. *
  3. * Distributed under the Boost Software License, Version 1.0. (See
  4. * accompanying file LICENSE.txt)
  5. */
  6. #ifndef BOOST_REDIS_WRITE_HPP
  7. #define BOOST_REDIS_WRITE_HPP
  8. #include <boost/asio/write.hpp>
  9. #include <boost/redis/request.hpp>
  10. namespace boost::redis::detail {
  11. /** \brief Writes a request synchronously.
  12. * \ingroup low-level-api
  13. *
  14. * \param stream Stream to write the request to.
  15. * \param req Request to write.
  16. */
  17. template<class SyncWriteStream>
  18. auto write(SyncWriteStream& stream, request const& req)
  19. {
  20. return asio::write(stream, asio::buffer(req.payload()));
  21. }
  22. template<class SyncWriteStream>
  23. auto write(SyncWriteStream& stream, request const& req, system::error_code& ec)
  24. {
  25. return asio::write(stream, asio::buffer(req.payload()), ec);
  26. }
  27. /** \brief Writes a request asynchronously.
  28. * \ingroup low-level-api
  29. *
  30. * \param stream Stream to write the request to.
  31. * \param req Request to write.
  32. * \param token Asio completion token.
  33. */
  34. template<
  35. class AsyncWriteStream,
  36. class CompletionToken = asio::default_completion_token_t<typename AsyncWriteStream::executor_type>
  37. >
  38. auto async_write(
  39. AsyncWriteStream& stream,
  40. request const& req,
  41. CompletionToken&& token =
  42. asio::default_completion_token_t<typename AsyncWriteStream::executor_type>{})
  43. {
  44. return asio::async_write(stream, asio::buffer(req.payload()), token);
  45. }
  46. } // boost::redis::detail
  47. #endif // BOOST_REDIS_WRITE_HPP