core_snippets.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. // prevent ssl.hpp from actually being included,
  10. // otherwise we would need OpenSSL on AppVeyor
  11. #ifndef BOOST_ASIO_SSL_HPP
  12. #define BOOST_ASIO_SSL_HPP
  13. namespace boost { namespace asio { namespace ssl { } } }
  14. #endif
  15. //[snippet_core_1a
  16. #include <boost/beast/core.hpp>
  17. #include <boost/asio.hpp>
  18. #include <boost/asio/ssl.hpp>
  19. #include <iostream>
  20. #include <thread>
  21. //]
  22. using namespace boost::beast;
  23. namespace doc_core_snippets {
  24. void fxx()
  25. {
  26. //[snippet_core_1b
  27. //
  28. using namespace boost::beast;
  29. namespace net = boost::asio;
  30. namespace ssl = boost::asio::ssl;
  31. using tcp = net::ip::tcp;
  32. net::io_context ioc;
  33. auto work = net::make_work_guard(ioc);
  34. std::thread t{[&](){ ioc.run(); }};
  35. error_code ec;
  36. tcp::socket sock{ioc};
  37. //]
  38. boost::ignore_unused(ec);
  39. {
  40. //[snippet_core_2
  41. // The resolver is used to look up IP addresses and port numbers from a domain and service name pair
  42. tcp::resolver r{ioc};
  43. // A socket represents the local end of a connection between two peers
  44. tcp::socket stream{ioc};
  45. // Establish a connection before sending and receiving data
  46. net::connect(stream, r.resolve("www.example.com", "http"));
  47. // At this point `stream` is a connected to a remote
  48. // host and may be used to perform stream operations.
  49. //]
  50. }
  51. } // fxx()
  52. //------------------------------------------------------------------------------
  53. //[snippet_core_3
  54. template<class SyncWriteStream>
  55. void write_string(SyncWriteStream& stream, string_view s)
  56. {
  57. static_assert(is_sync_write_stream<SyncWriteStream>::value,
  58. "SyncWriteStream type requirements not met");
  59. net::write(stream, net::const_buffer(s.data(), s.size()));
  60. }
  61. //]
  62. } // doc_core_snippets