From fc09a4cad13db4ccdd8a8899417f1db94119f07a Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Mon, 19 Jun 2017 14:41:28 -0700 Subject: [PATCH] Tidy up namespaces in examples --- CHANGELOG.md | 1 + example/http-client-ssl/http_client_ssl.cpp | 30 ++++++++++-------- example/http-client/http_client.cpp | 23 ++++++++------ example/http-crawl/http_crawl.cpp | 31 ++++++++++--------- .../websocket_client_ssl.cpp | 22 +++++++------ example/websocket-client/websocket_client.cpp | 13 +++++--- 6 files changed, 69 insertions(+), 51 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 55866315..e1a6a150 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ Version 62: * Increase detail::static_ostream coverage * Add server-framework tests * Doc fixes and tidy +* Tidy up namespaces in examples -------------------------------------------------------------------------------- diff --git a/example/http-client-ssl/http_client_ssl.cpp b/example/http-client-ssl/http_client_ssl.cpp index 88e12433..8c5cab72 100644 --- a/example/http-client-ssl/http_client_ssl.cpp +++ b/example/http-client-ssl/http_client_ssl.cpp @@ -13,6 +13,10 @@ #include #include +using tcp = boost::asio::ip::tcp; // from +namespace ssl = boost::asio::ssl; // from +namespace http = beast::http; // from + 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 stream{sock, ctx}; - stream.set_verify_mode(boost::asio::ssl::verify_none); + ssl::context ctx{ssl::context::sslv23}; + ssl::stream 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 req; - req.method(beast::http::verb::get); + http::request 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(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 res; + http::response res; // Read the response - beast::http::read(stream, b, res, ec); + http::read(stream, b, res, ec); if(ec) return fail("read", ec); diff --git a/example/http-client/http_client.cpp b/example/http-client/http_client.cpp index e1e496e3..a17ddb9d 100644 --- a/example/http-client/http_client.cpp +++ b/example/http-client/http_client.cpp @@ -15,6 +15,9 @@ #include #include +using tcp = boost::asio::ip::tcp; // from +namespace http = beast::http; // from + 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 req; - req.method(beast::http::verb::get); + http::request 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(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 res; + http::response 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); diff --git a/example/http-crawl/http_crawl.cpp b/example/http-crawl/http_crawl.cpp index 24c4e47b..af74cde4 100644 --- a/example/http-crawl/http_crawl.cpp +++ b/example/http-crawl/http_crawl.cpp @@ -15,6 +15,9 @@ #include #include +using tcp = boost::asio::ip::tcp; // from +namespace http = beast::http; // from + template 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 req; + http::request 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(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 res; + http::response 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); diff --git a/example/websocket-client-ssl/websocket_client_ssl.cpp b/example/websocket-client-ssl/websocket_client_ssl.cpp index cae706ad..35ad063d 100644 --- a/example/websocket-client-ssl/websocket_client_ssl.cpp +++ b/example/websocket-client-ssl/websocket_client_ssl.cpp @@ -14,6 +14,10 @@ #include #include +using tcp = boost::asio::ip::tcp; // from +namespace ssl = boost::asio::ssl; // from +namespace websocket = beast::websocket; // from + 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::ssl::context ctx{boost::asio::ssl::context::sslv23}; + using stream_type = ssl::stream; + 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 ws{stream}; + websocket::stream 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. diff --git a/example/websocket-client/websocket_client.cpp b/example/websocket-client/websocket_client.cpp index 495b4ece..d1ee985a 100644 --- a/example/websocket-client/websocket_client.cpp +++ b/example/websocket-client/websocket_client.cpp @@ -14,6 +14,9 @@ #include #include +using tcp = boost::asio::ip::tcp; // from +namespace websocket = beast::websocket; // from + 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 ws{sock}; + websocket::stream 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.