• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
10 // prevent ssl.hpp from actually being included,
11 // otherwise we would need OpenSSL on AppVeyor
12 #ifndef BOOST_ASIO_SSL_HPP
13 #define BOOST_ASIO_SSL_HPP
14 namespace boost { namespace asio { namespace ssl { } } }
15 #endif
16 
17 //[snippet_core_1a
18 
19 #include <boost/beast/core.hpp>
20 #include <boost/asio.hpp>
21 #include <boost/asio/ssl.hpp>
22 #include <iostream>
23 #include <thread>
24 
25 //]
26 
27 using namespace boost::beast;
28 
29 namespace doc_core_snippets {
30 
fxx()31 void fxx()
32 {
33 //[snippet_core_1b
34 //
35 using namespace boost::beast;
36 namespace net = boost::asio;
37 namespace ssl = boost::asio::ssl;
38 using tcp = net::ip::tcp;
39 
40 net::io_context ioc;
41 net::any_io_executor work =
42     net::require(ioc.get_executor(),
43         net::execution::outstanding_work.tracked);
44 std::thread t{[&](){ ioc.run(); }};
45 
46 error_code ec;
47 tcp::socket sock{ioc};
48 
49 //]
50     boost::ignore_unused(ec);
51 
52     {
53 //[snippet_core_2
54 
55 // The resolver is used to look up IP addresses and port numbers from a domain and service name pair
56 tcp::resolver r{ioc};
57 
58 // A socket represents the local end of a connection between two peers
59 tcp::socket stream{ioc};
60 
61 // Establish a connection before sending and receiving data
62 net::connect(stream, r.resolve("www.example.com", "http"));
63 
64 // At this point `stream` is a connected to a remote
65 // host and may be used to perform stream operations.
66 
67 //]
68     }
69 
70 } // fxx()
71 
72 //------------------------------------------------------------------------------
73 
74 //[snippet_core_3
75 
76 template<class SyncWriteStream>
write_string(SyncWriteStream & stream,string_view s)77 void write_string(SyncWriteStream& stream, string_view s)
78 {
79     static_assert(is_sync_write_stream<SyncWriteStream>::value,
80         "SyncWriteStream type requirements not met");
81     net::write(stream, net::const_buffer(s.data(), s.size()));
82 }
83 
84 //]
85 
86 } // doc_core_snippets
87