Tidy up namespaces in examples

This commit is contained in:
Vinnie Falco
2017-06-19 14:41:28 -07:00
parent c3e2de29ef
commit fc09a4cad1
6 changed files with 69 additions and 51 deletions

View File

@@ -4,6 +4,7 @@ Version 62:
* Increase detail::static_ostream coverage
* Add server-framework tests
* Doc fixes and tidy
* Tidy up namespaces in examples
--------------------------------------------------------------------------------

View File

@@ -13,6 +13,10 @@
#include <iostream>
#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()
{
// A helper for reporting errors
@@ -28,8 +32,8 @@ int main()
// Normal boost::asio setup
boost::asio::io_service ios;
boost::asio::ip::tcp::resolver r{ios};
boost::asio::ip::tcp::socket sock{ios};
tcp::resolver r{ios};
tcp::socket sock{ios};
// Look up the domain name
std::string const host = "www.example.com";
@@ -43,27 +47,27 @@ int main()
return fail("connect", ec);
// Wrap the now-connected socket in an SSL stream
boost::asio::ssl::context ctx{boost::asio::ssl::context::sslv23};
boost::asio::ssl::stream<boost::asio::ip::tcp::socket&> stream{sock, ctx};
stream.set_verify_mode(boost::asio::ssl::verify_none);
ssl::context ctx{ssl::context::sslv23};
ssl::stream<tcp::socket&> stream{sock, ctx};
stream.set_verify_mode(ssl::verify_none);
// Perform SSL handshaking
stream.handshake(boost::asio::ssl::stream_base::client, ec);
stream.handshake(ssl::stream_base::client, ec);
if(ec)
return fail("handshake", ec);
// Set up an HTTP GET request message
beast::http::request<beast::http::string_body> req;
req.method(beast::http::verb::get);
http::request<http::string_body> req;
req.method(http::verb::get);
req.target("/");
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()));
req.set(beast::http::field::user_agent, "Beast");
req.set(http::field::user_agent, "Beast");
req.prepare();
// Write the HTTP request to the remote host
beast::http::write(stream, req, ec);
http::write(stream, req, ec);
if(ec)
return fail("write", ec);
@@ -71,10 +75,10 @@ int main()
beast::flat_buffer b;
// Declare a container to hold the response
beast::http::response<beast::http::dynamic_body> res;
http::response<http::dynamic_body> res;
// Read the response
beast::http::read(stream, b, res, ec);
http::read(stream, b, res, ec);
if(ec)
return fail("read", ec);

View File

@@ -15,6 +15,9 @@
#include <iostream>
#include <string>
using tcp = boost::asio::ip::tcp; // from <boost/asio.hpp>
namespace http = beast::http; // from <beast/http.hpp>
int main()
{
// A helper for reporting errors
@@ -30,8 +33,8 @@ int main()
// Set up an asio socket
boost::asio::io_service ios;
boost::asio::ip::tcp::resolver r{ios};
boost::asio::ip::tcp::socket sock{ios};
tcp::resolver r{ios};
tcp::socket sock{ios};
// Look up the domain name
std::string const host = "www.example.com";
@@ -45,17 +48,17 @@ int main()
return fail("connect", ec);
// Set up an HTTP GET request message
beast::http::request<beast::http::string_body> req;
req.method(beast::http::verb::get);
http::request<http::string_body> req;
req.method(http::verb::get);
req.target("/");
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()));
req.set(beast::http::field::user_agent, "Beast");
req.set(http::field::user_agent, "Beast");
req.prepare();
// Write the HTTP request to the remote host
beast::http::write(sock, req, ec);
http::write(sock, req, ec);
if(ec)
return fail("write", ec);
@@ -63,10 +66,10 @@ int main()
beast::flat_buffer b;
// Declare a container to hold the response
beast::http::response<beast::http::dynamic_body> res;
http::response<http::dynamic_body> res;
// Read the response
beast::http::read(sock, b, res, ec);
http::read(sock, b, res, ec);
if(ec)
return fail("read", ec);
@@ -74,7 +77,7 @@ int main()
std::cout << res << std::endl;
// Gracefully close the socket
sock.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
sock.shutdown(tcp::socket::shutdown_both, ec);
if(ec)
return fail("shutdown", ec);

View File

@@ -15,6 +15,9 @@
#include <cstdlib>
#include <iostream>
using tcp = boost::asio::ip::tcp; // from <boost/asio.hpp>
namespace http = beast::http; // from <beast/http.hpp>
template<class String>
void
err(beast::error_code const& ec, String const& what)
@@ -47,7 +50,7 @@ main(int, char const*[])
beast::error_code ec;
// Look up the domain name
boost::asio::ip::tcp::resolver r(ios);
tcp::resolver r(ios);
auto lookup = r.resolve({host, "http"}, ec);
if(ec)
{
@@ -56,7 +59,7 @@ main(int, char const*[])
}
// Now create a socket and connect
boost::asio::ip::tcp::socket sock(ios);
tcp::socket sock(ios);
boost::asio::connect(sock, lookup, ec);
if(ec)
{
@@ -73,30 +76,30 @@ main(int, char const*[])
}
// Set up an HTTP GET request
beast::http::request<beast::http::string_body> req;
http::request<http::string_body> req;
req.version = 11;
req.method(beast::http::verb::get);
req.method(http::verb::get);
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()));
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
// the connection. This consumes less resources (no TIME_WAIT) because
// 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
beast::http::write(sock, req, ec);
if(ec == beast::http::error::end_of_stream)
http::write(sock, req, ec);
if(ec == http::error::end_of_stream)
{
// This special error received on a write indicates that the
// semantics of the sent message are such that the connection
// should be closed after the response is done. We do a TCP/IP
// "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)
return fail("shutdown", ec);
}
@@ -110,11 +113,11 @@ main(int, char const*[])
beast::multi_buffer b;
// The response will go into this object
beast::http::response<beast::http::string_body> res;
http::response<http::string_body> res;
// Read the response
beast::http::read(sock, b, res, ec);
if(ec == beast::http::error::end_of_stream)
http::read(sock, b, res, ec);
if(ec == http::error::end_of_stream)
{
// This special error means that the other end closed the socket,
// 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,
// 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)
return fail("shutdown", ec);

View File

@@ -14,6 +14,10 @@
#include <iostream>
#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()
{
// A helper for reporting errors
@@ -29,8 +33,8 @@ int main()
// Set up an asio socket to connect to a remote host
boost::asio::io_service ios;
boost::asio::ip::tcp::resolver r{ios};
boost::asio::ip::tcp::socket sock{ios};
tcp::resolver r{ios};
tcp::socket sock{ios};
// Look up the domain name
std::string const host = "echo.websocket.org";
@@ -44,18 +48,18 @@ int main()
return fail("connect", ec);
// Wrap the now-connected socket in an SSL stream
using stream_type = boost::asio::ssl::stream<boost::asio::ip::tcp::socket&>;
boost::asio::ssl::context ctx{boost::asio::ssl::context::sslv23};
using stream_type = ssl::stream<tcp::socket&>;
ssl::context ctx{ssl::context::sslv23};
stream_type stream{sock, ctx};
stream.set_verify_mode(boost::asio::ssl::verify_none);
stream.set_verify_mode(ssl::verify_none);
// Perform SSL handshaking
stream.handshake(boost::asio::ssl::stream_base::client, ec);
stream.handshake(ssl::stream_base::client, ec);
if(ec)
return fail("ssl handshake", ec);
// 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
ws.handshake(host, "/", ec);
@@ -76,7 +80,7 @@ int main()
return fail("read", ec);
// 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)
return fail("close", ec);
@@ -94,7 +98,7 @@ int main()
ws.read(drain, ec);
// ...until we get the special error code
if(ec == beast::websocket::error::closed)
if(ec == websocket::error::closed)
break;
// Some other error occurred, report it and exit.

View File

@@ -14,6 +14,9 @@
#include <iostream>
#include <string>
using tcp = boost::asio::ip::tcp; // from <boost/asio.hpp>
namespace websocket = beast::websocket; // from <beast/websocket.hpp>
int main()
{
// A helper for reporting errors
@@ -29,8 +32,8 @@ int main()
// Set up an asio socket
boost::asio::io_service ios;
boost::asio::ip::tcp::resolver r{ios};
boost::asio::ip::tcp::socket sock{ios};
tcp::resolver r{ios};
tcp::socket sock{ios};
// Look up the domain name
std::string const host = "echo.websocket.org";
@@ -44,7 +47,7 @@ int main()
return fail("connect", ec);
// 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
ws.handshake(host, "/", ec);
@@ -65,7 +68,7 @@ int main()
return fail("read", ec);
// 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)
return fail("close", ec);
@@ -83,7 +86,7 @@ int main()
ws.read(drain, ec);
// ...until we get the special error code
if(ec == beast::websocket::error::closed)
if(ec == websocket::error::closed)
break;
// Some other error occurred, report it and exit.