mirror of
https://github.com/boostorg/beast.git
synced 2025-08-03 14:54:32 +02:00
Tidy up namespaces in examples
This commit is contained in:
@@ -4,6 +4,7 @@ Version 62:
|
|||||||
* Increase detail::static_ostream coverage
|
* Increase detail::static_ostream coverage
|
||||||
* Add server-framework tests
|
* Add server-framework tests
|
||||||
* Doc fixes and tidy
|
* Doc fixes and tidy
|
||||||
|
* Tidy up namespaces in examples
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@@ -13,6 +13,10 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
using tcp = boost::asio::ip::tcp; // from <boost/asio.hpp>
|
||||||
|
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
|
||||||
|
namespace http = beast::http; // from <beast/http.hpp>
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// A helper for reporting errors
|
// A helper for reporting errors
|
||||||
@@ -28,8 +32,8 @@ int main()
|
|||||||
|
|
||||||
// Normal boost::asio setup
|
// Normal boost::asio setup
|
||||||
boost::asio::io_service ios;
|
boost::asio::io_service ios;
|
||||||
boost::asio::ip::tcp::resolver r{ios};
|
tcp::resolver r{ios};
|
||||||
boost::asio::ip::tcp::socket sock{ios};
|
tcp::socket sock{ios};
|
||||||
|
|
||||||
// Look up the domain name
|
// Look up the domain name
|
||||||
std::string const host = "www.example.com";
|
std::string const host = "www.example.com";
|
||||||
@@ -43,27 +47,27 @@ int main()
|
|||||||
return fail("connect", ec);
|
return fail("connect", ec);
|
||||||
|
|
||||||
// Wrap the now-connected socket in an SSL stream
|
// Wrap the now-connected socket in an SSL stream
|
||||||
boost::asio::ssl::context ctx{boost::asio::ssl::context::sslv23};
|
ssl::context ctx{ssl::context::sslv23};
|
||||||
boost::asio::ssl::stream<boost::asio::ip::tcp::socket&> stream{sock, ctx};
|
ssl::stream<tcp::socket&> stream{sock, ctx};
|
||||||
stream.set_verify_mode(boost::asio::ssl::verify_none);
|
stream.set_verify_mode(ssl::verify_none);
|
||||||
|
|
||||||
// Perform SSL handshaking
|
// Perform SSL handshaking
|
||||||
stream.handshake(boost::asio::ssl::stream_base::client, ec);
|
stream.handshake(ssl::stream_base::client, ec);
|
||||||
if(ec)
|
if(ec)
|
||||||
return fail("handshake", ec);
|
return fail("handshake", ec);
|
||||||
|
|
||||||
// Set up an HTTP GET request message
|
// Set up an HTTP GET request message
|
||||||
beast::http::request<beast::http::string_body> req;
|
http::request<http::string_body> req;
|
||||||
req.method(beast::http::verb::get);
|
req.method(http::verb::get);
|
||||||
req.target("/");
|
req.target("/");
|
||||||
req.version = 11;
|
req.version = 11;
|
||||||
req.set(beast::http::field::host, host + ":" +
|
req.set(http::field::host, host + ":" +
|
||||||
boost::lexical_cast<std::string>(sock.remote_endpoint().port()));
|
boost::lexical_cast<std::string>(sock.remote_endpoint().port()));
|
||||||
req.set(beast::http::field::user_agent, "Beast");
|
req.set(http::field::user_agent, "Beast");
|
||||||
req.prepare();
|
req.prepare();
|
||||||
|
|
||||||
// Write the HTTP request to the remote host
|
// Write the HTTP request to the remote host
|
||||||
beast::http::write(stream, req, ec);
|
http::write(stream, req, ec);
|
||||||
if(ec)
|
if(ec)
|
||||||
return fail("write", ec);
|
return fail("write", ec);
|
||||||
|
|
||||||
@@ -71,10 +75,10 @@ int main()
|
|||||||
beast::flat_buffer b;
|
beast::flat_buffer b;
|
||||||
|
|
||||||
// Declare a container to hold the response
|
// Declare a container to hold the response
|
||||||
beast::http::response<beast::http::dynamic_body> res;
|
http::response<http::dynamic_body> res;
|
||||||
|
|
||||||
// Read the response
|
// Read the response
|
||||||
beast::http::read(stream, b, res, ec);
|
http::read(stream, b, res, ec);
|
||||||
if(ec)
|
if(ec)
|
||||||
return fail("read", ec);
|
return fail("read", ec);
|
||||||
|
|
||||||
|
@@ -15,6 +15,9 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
using tcp = boost::asio::ip::tcp; // from <boost/asio.hpp>
|
||||||
|
namespace http = beast::http; // from <beast/http.hpp>
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// A helper for reporting errors
|
// A helper for reporting errors
|
||||||
@@ -30,8 +33,8 @@ int main()
|
|||||||
|
|
||||||
// Set up an asio socket
|
// Set up an asio socket
|
||||||
boost::asio::io_service ios;
|
boost::asio::io_service ios;
|
||||||
boost::asio::ip::tcp::resolver r{ios};
|
tcp::resolver r{ios};
|
||||||
boost::asio::ip::tcp::socket sock{ios};
|
tcp::socket sock{ios};
|
||||||
|
|
||||||
// Look up the domain name
|
// Look up the domain name
|
||||||
std::string const host = "www.example.com";
|
std::string const host = "www.example.com";
|
||||||
@@ -45,17 +48,17 @@ int main()
|
|||||||
return fail("connect", ec);
|
return fail("connect", ec);
|
||||||
|
|
||||||
// Set up an HTTP GET request message
|
// Set up an HTTP GET request message
|
||||||
beast::http::request<beast::http::string_body> req;
|
http::request<http::string_body> req;
|
||||||
req.method(beast::http::verb::get);
|
req.method(http::verb::get);
|
||||||
req.target("/");
|
req.target("/");
|
||||||
req.version = 11;
|
req.version = 11;
|
||||||
req.set(beast::http::field::host, host + ":" +
|
req.set(http::field::host, host + ":" +
|
||||||
boost::lexical_cast<std::string>(sock.remote_endpoint().port()));
|
boost::lexical_cast<std::string>(sock.remote_endpoint().port()));
|
||||||
req.set(beast::http::field::user_agent, "Beast");
|
req.set(http::field::user_agent, "Beast");
|
||||||
req.prepare();
|
req.prepare();
|
||||||
|
|
||||||
// Write the HTTP request to the remote host
|
// Write the HTTP request to the remote host
|
||||||
beast::http::write(sock, req, ec);
|
http::write(sock, req, ec);
|
||||||
if(ec)
|
if(ec)
|
||||||
return fail("write", ec);
|
return fail("write", ec);
|
||||||
|
|
||||||
@@ -63,10 +66,10 @@ int main()
|
|||||||
beast::flat_buffer b;
|
beast::flat_buffer b;
|
||||||
|
|
||||||
// Declare a container to hold the response
|
// Declare a container to hold the response
|
||||||
beast::http::response<beast::http::dynamic_body> res;
|
http::response<http::dynamic_body> res;
|
||||||
|
|
||||||
// Read the response
|
// Read the response
|
||||||
beast::http::read(sock, b, res, ec);
|
http::read(sock, b, res, ec);
|
||||||
if(ec)
|
if(ec)
|
||||||
return fail("read", ec);
|
return fail("read", ec);
|
||||||
|
|
||||||
@@ -74,7 +77,7 @@ int main()
|
|||||||
std::cout << res << std::endl;
|
std::cout << res << std::endl;
|
||||||
|
|
||||||
// Gracefully close the socket
|
// Gracefully close the socket
|
||||||
sock.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
|
sock.shutdown(tcp::socket::shutdown_both, ec);
|
||||||
if(ec)
|
if(ec)
|
||||||
return fail("shutdown", ec);
|
return fail("shutdown", ec);
|
||||||
|
|
||||||
|
@@ -15,6 +15,9 @@
|
|||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
using tcp = boost::asio::ip::tcp; // from <boost/asio.hpp>
|
||||||
|
namespace http = beast::http; // from <beast/http.hpp>
|
||||||
|
|
||||||
template<class String>
|
template<class String>
|
||||||
void
|
void
|
||||||
err(beast::error_code const& ec, String const& what)
|
err(beast::error_code const& ec, String const& what)
|
||||||
@@ -47,7 +50,7 @@ main(int, char const*[])
|
|||||||
beast::error_code ec;
|
beast::error_code ec;
|
||||||
|
|
||||||
// Look up the domain name
|
// Look up the domain name
|
||||||
boost::asio::ip::tcp::resolver r(ios);
|
tcp::resolver r(ios);
|
||||||
auto lookup = r.resolve({host, "http"}, ec);
|
auto lookup = r.resolve({host, "http"}, ec);
|
||||||
if(ec)
|
if(ec)
|
||||||
{
|
{
|
||||||
@@ -56,7 +59,7 @@ main(int, char const*[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Now create a socket and connect
|
// Now create a socket and connect
|
||||||
boost::asio::ip::tcp::socket sock(ios);
|
tcp::socket sock(ios);
|
||||||
boost::asio::connect(sock, lookup, ec);
|
boost::asio::connect(sock, lookup, ec);
|
||||||
if(ec)
|
if(ec)
|
||||||
{
|
{
|
||||||
@@ -73,30 +76,30 @@ main(int, char const*[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set up an HTTP GET request
|
// Set up an HTTP GET request
|
||||||
beast::http::request<beast::http::string_body> req;
|
http::request<http::string_body> req;
|
||||||
req.version = 11;
|
req.version = 11;
|
||||||
req.method(beast::http::verb::get);
|
req.method(http::verb::get);
|
||||||
req.target("/");
|
req.target("/");
|
||||||
req.set(beast::http::field::host, host + std::string(":") +
|
req.set(http::field::host, host + std::string(":") +
|
||||||
boost::lexical_cast<std::string>(ep.port()));
|
boost::lexical_cast<std::string>(ep.port()));
|
||||||
req.set(beast::http::field::user_agent, BEAST_VERSION_STRING);
|
req.set(http::field::user_agent, BEAST_VERSION_STRING);
|
||||||
|
|
||||||
// Set the Connection: close field, this way the server will close
|
// Set the Connection: close field, this way the server will close
|
||||||
// the connection. This consumes less resources (no TIME_WAIT) because
|
// the connection. This consumes less resources (no TIME_WAIT) because
|
||||||
// of the graceful close. It also makes things go a little faster.
|
// of the graceful close. It also makes things go a little faster.
|
||||||
//
|
//
|
||||||
req.set(beast::http::field::connection, "close");
|
req.set(http::field::connection, "close");
|
||||||
|
|
||||||
// Send the GET request
|
// Send the GET request
|
||||||
beast::http::write(sock, req, ec);
|
http::write(sock, req, ec);
|
||||||
if(ec == beast::http::error::end_of_stream)
|
if(ec == http::error::end_of_stream)
|
||||||
{
|
{
|
||||||
// This special error received on a write indicates that the
|
// This special error received on a write indicates that the
|
||||||
// semantics of the sent message are such that the connection
|
// semantics of the sent message are such that the connection
|
||||||
// should be closed after the response is done. We do a TCP/IP
|
// should be closed after the response is done. We do a TCP/IP
|
||||||
// "half-close" here to shut down our end.
|
// "half-close" here to shut down our end.
|
||||||
//
|
//
|
||||||
sock.shutdown(boost::asio::ip::tcp::socket::shutdown_send, ec);
|
sock.shutdown(tcp::socket::shutdown_send, ec);
|
||||||
if(ec)
|
if(ec)
|
||||||
return fail("shutdown", ec);
|
return fail("shutdown", ec);
|
||||||
}
|
}
|
||||||
@@ -110,11 +113,11 @@ main(int, char const*[])
|
|||||||
beast::multi_buffer b;
|
beast::multi_buffer b;
|
||||||
|
|
||||||
// The response will go into this object
|
// The response will go into this object
|
||||||
beast::http::response<beast::http::string_body> res;
|
http::response<http::string_body> res;
|
||||||
|
|
||||||
// Read the response
|
// Read the response
|
||||||
beast::http::read(sock, b, res, ec);
|
http::read(sock, b, res, ec);
|
||||||
if(ec == beast::http::error::end_of_stream)
|
if(ec == http::error::end_of_stream)
|
||||||
{
|
{
|
||||||
// This special error means that the other end closed the socket,
|
// This special error means that the other end closed the socket,
|
||||||
// which is what we want since we asked for Connection: close.
|
// which is what we want since we asked for Connection: close.
|
||||||
@@ -130,7 +133,7 @@ main(int, char const*[])
|
|||||||
|
|
||||||
// Now we do the other half of the close,
|
// Now we do the other half of the close,
|
||||||
// which is to shut down the receiver.
|
// which is to shut down the receiver.
|
||||||
sock.shutdown(boost::asio::ip::tcp::socket::shutdown_receive, ec);
|
sock.shutdown(tcp::socket::shutdown_receive, ec);
|
||||||
if(ec)
|
if(ec)
|
||||||
return fail("shutdown", ec);
|
return fail("shutdown", ec);
|
||||||
|
|
||||||
|
@@ -14,6 +14,10 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
using tcp = boost::asio::ip::tcp; // from <boost/asio.hpp>
|
||||||
|
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
|
||||||
|
namespace websocket = beast::websocket; // from <beast/websocket.hpp>
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// A helper for reporting errors
|
// A helper for reporting errors
|
||||||
@@ -29,8 +33,8 @@ int main()
|
|||||||
|
|
||||||
// Set up an asio socket to connect to a remote host
|
// Set up an asio socket to connect to a remote host
|
||||||
boost::asio::io_service ios;
|
boost::asio::io_service ios;
|
||||||
boost::asio::ip::tcp::resolver r{ios};
|
tcp::resolver r{ios};
|
||||||
boost::asio::ip::tcp::socket sock{ios};
|
tcp::socket sock{ios};
|
||||||
|
|
||||||
// Look up the domain name
|
// Look up the domain name
|
||||||
std::string const host = "echo.websocket.org";
|
std::string const host = "echo.websocket.org";
|
||||||
@@ -44,18 +48,18 @@ int main()
|
|||||||
return fail("connect", ec);
|
return fail("connect", ec);
|
||||||
|
|
||||||
// Wrap the now-connected socket in an SSL stream
|
// Wrap the now-connected socket in an SSL stream
|
||||||
using stream_type = boost::asio::ssl::stream<boost::asio::ip::tcp::socket&>;
|
using stream_type = ssl::stream<tcp::socket&>;
|
||||||
boost::asio::ssl::context ctx{boost::asio::ssl::context::sslv23};
|
ssl::context ctx{ssl::context::sslv23};
|
||||||
stream_type stream{sock, ctx};
|
stream_type stream{sock, ctx};
|
||||||
stream.set_verify_mode(boost::asio::ssl::verify_none);
|
stream.set_verify_mode(ssl::verify_none);
|
||||||
|
|
||||||
// Perform SSL handshaking
|
// Perform SSL handshaking
|
||||||
stream.handshake(boost::asio::ssl::stream_base::client, ec);
|
stream.handshake(ssl::stream_base::client, ec);
|
||||||
if(ec)
|
if(ec)
|
||||||
return fail("ssl handshake", ec);
|
return fail("ssl handshake", ec);
|
||||||
|
|
||||||
// Now wrap the handshaked SSL stream in a websocket stream
|
// Now wrap the handshaked SSL stream in a websocket stream
|
||||||
beast::websocket::stream<stream_type&> ws{stream};
|
websocket::stream<stream_type&> ws{stream};
|
||||||
|
|
||||||
// Perform the websocket handshake
|
// Perform the websocket handshake
|
||||||
ws.handshake(host, "/", ec);
|
ws.handshake(host, "/", ec);
|
||||||
@@ -76,7 +80,7 @@ int main()
|
|||||||
return fail("read", ec);
|
return fail("read", ec);
|
||||||
|
|
||||||
// Send a "close" frame to the other end, this is a websocket thing
|
// Send a "close" frame to the other end, this is a websocket thing
|
||||||
ws.close(beast::websocket::close_code::normal, ec);
|
ws.close(websocket::close_code::normal, ec);
|
||||||
if(ec)
|
if(ec)
|
||||||
return fail("close", ec);
|
return fail("close", ec);
|
||||||
|
|
||||||
@@ -94,7 +98,7 @@ int main()
|
|||||||
ws.read(drain, ec);
|
ws.read(drain, ec);
|
||||||
|
|
||||||
// ...until we get the special error code
|
// ...until we get the special error code
|
||||||
if(ec == beast::websocket::error::closed)
|
if(ec == websocket::error::closed)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Some other error occurred, report it and exit.
|
// Some other error occurred, report it and exit.
|
||||||
|
@@ -14,6 +14,9 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
using tcp = boost::asio::ip::tcp; // from <boost/asio.hpp>
|
||||||
|
namespace websocket = beast::websocket; // from <beast/websocket.hpp>
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// A helper for reporting errors
|
// A helper for reporting errors
|
||||||
@@ -29,8 +32,8 @@ int main()
|
|||||||
|
|
||||||
// Set up an asio socket
|
// Set up an asio socket
|
||||||
boost::asio::io_service ios;
|
boost::asio::io_service ios;
|
||||||
boost::asio::ip::tcp::resolver r{ios};
|
tcp::resolver r{ios};
|
||||||
boost::asio::ip::tcp::socket sock{ios};
|
tcp::socket sock{ios};
|
||||||
|
|
||||||
// Look up the domain name
|
// Look up the domain name
|
||||||
std::string const host = "echo.websocket.org";
|
std::string const host = "echo.websocket.org";
|
||||||
@@ -44,7 +47,7 @@ int main()
|
|||||||
return fail("connect", ec);
|
return fail("connect", ec);
|
||||||
|
|
||||||
// Wrap the now-connected socket in a websocket stream
|
// Wrap the now-connected socket in a websocket stream
|
||||||
beast::websocket::stream<boost::asio::ip::tcp::socket&> ws{sock};
|
websocket::stream<tcp::socket&> ws{sock};
|
||||||
|
|
||||||
// Perform the websocket handhskae
|
// Perform the websocket handhskae
|
||||||
ws.handshake(host, "/", ec);
|
ws.handshake(host, "/", ec);
|
||||||
@@ -65,7 +68,7 @@ int main()
|
|||||||
return fail("read", ec);
|
return fail("read", ec);
|
||||||
|
|
||||||
// Send a "close" frame to the other end, this is a websocket thing
|
// Send a "close" frame to the other end, this is a websocket thing
|
||||||
ws.close(beast::websocket::close_code::normal, ec);
|
ws.close(websocket::close_code::normal, ec);
|
||||||
if(ec)
|
if(ec)
|
||||||
return fail("close", ec);
|
return fail("close", ec);
|
||||||
|
|
||||||
@@ -83,7 +86,7 @@ int main()
|
|||||||
ws.read(drain, ec);
|
ws.read(drain, ec);
|
||||||
|
|
||||||
// ...until we get the special error code
|
// ...until we get the special error code
|
||||||
if(ec == beast::websocket::error::closed)
|
if(ec == websocket::error::closed)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Some other error occurred, report it and exit.
|
// Some other error occurred, report it and exit.
|
||||||
|
Reference in New Issue
Block a user