Files
boost_beast/test/doc/core_snippets.cpp

87 lines
2.0 KiB
C++
Raw Permalink Normal View History

2017-06-07 18:18:50 -07:00
//
2019-02-21 07:00:31 -08:00
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
2017-06-07 18:18:50 -07:00
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
2017-07-20 13:40:34 -07:00
// Official repository: https://github.com/boostorg/beast
//
2017-06-07 18:18:50 -07:00
// prevent ssl.hpp from actually being included,
// otherwise we would need OpenSSL on AppVeyor
#ifndef BOOST_ASIO_SSL_HPP
#define BOOST_ASIO_SSL_HPP
namespace boost { namespace asio { namespace ssl { } } }
#endif
2017-06-07 18:18:50 -07:00
//[snippet_core_1a
2017-07-20 13:40:34 -07:00
#include <boost/beast/core.hpp>
2017-06-07 18:18:50 -07:00
#include <boost/asio.hpp>
2018-12-01 13:22:21 -08:00
#include <boost/asio/ssl.hpp>
2017-06-07 18:18:50 -07:00
#include <iostream>
#include <thread>
//]
2017-07-20 13:40:34 -07:00
using namespace boost::beast;
2017-06-07 18:18:50 -07:00
namespace doc_core_snippets {
void fxx()
{
//[snippet_core_1b
//
2017-07-20 13:40:34 -07:00
using namespace boost::beast;
2018-12-01 13:22:21 -08:00
namespace net = boost::asio;
namespace ssl = boost::asio::ssl;
using tcp = net::ip::tcp;
2017-06-07 18:18:50 -07:00
net::io_context ioc;
2020-07-04 07:33:22 +02:00
net::any_io_executor work =
2020-07-04 08:22:39 +02:00
net::require(ioc.get_executor(),
2020-07-04 07:33:22 +02:00
net::execution::outstanding_work.tracked);
std::thread t{[&](){ ioc.run(); }};
2017-06-07 18:18:50 -07:00
2017-06-07 16:30:49 -07:00
error_code ec;
2018-12-01 13:22:21 -08:00
tcp::socket sock{ioc};
2017-06-07 18:18:50 -07:00
//]
boost::ignore_unused(ec);
2017-06-07 18:18:50 -07:00
{
2017-06-07 18:18:50 -07:00
//[snippet_core_2
2018-12-01 13:22:21 -08:00
// The resolver is used to look up IP addresses and port numbers from a domain and service name pair
tcp::resolver r{ioc};
// A socket represents the local end of a connection between two peers
tcp::socket stream{ioc};
// Establish a connection before sending and receiving data
net::connect(stream, r.resolve("www.example.com", "http"));
2017-06-07 18:18:50 -07:00
// At this point `stream` is a connected to a remote
// host and may be used to perform stream operations.
//]
}
2017-06-07 18:18:50 -07:00
} // fxx()
//------------------------------------------------------------------------------
2017-06-07 18:18:50 -07:00
//[snippet_core_3
template<class SyncWriteStream>
void write_string(SyncWriteStream& stream, string_view s)
{
static_assert(is_sync_write_stream<SyncWriteStream>::value,
2019-02-20 19:19:59 -08:00
"SyncWriteStream type requirements not met");
net::write(stream, net::const_buffer(s.data(), s.size()));
2017-06-07 18:18:50 -07:00
}
//]
} // doc_core_snippets