From 7735187d36e74cc7e26765c819bd5075042fb95b Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sat, 10 Aug 2013 12:16:46 -0700 Subject: [PATCH] Move PeerRole to its own file --- Builds/VisualStudio2012/beast.vcxproj | 7 +++ Builds/VisualStudio2012/beast.vcxproj.filters | 6 +++ modules/beast_asio/basics/beast_PeerRole.cpp | 44 +++++++++++++++++++ modules/beast_asio/basics/beast_PeerRole.h | 40 +++++++++++++++++ modules/beast_asio/beast_asio.cpp | 2 + modules/beast_asio/beast_asio.h | 1 + .../beast_asio/system/beast_BoostIncludes.h | 1 + .../beast_asio/tests/beast_TestPeerBasics.cpp | 34 ++++---------- .../beast_asio/tests/beast_TestPeerBasics.h | 25 +++-------- .../tests/beast_TestPeerDetailsTcp.h | 6 +-- .../beast_asio/tests/beast_TestPeerLogic.h | 2 +- .../tests/beast_TestPeerLogicAsyncClient.cpp | 4 +- .../tests/beast_TestPeerLogicAsyncClient.h | 2 +- .../tests/beast_TestPeerLogicAsyncServer.cpp | 4 +- .../tests/beast_TestPeerLogicAsyncServer.h | 2 +- .../tests/beast_TestPeerLogicSyncClient.cpp | 6 +-- .../tests/beast_TestPeerLogicSyncClient.h | 2 +- .../tests/beast_TestPeerLogicSyncServer.cpp | 6 +-- .../tests/beast_TestPeerLogicSyncServer.h | 2 +- modules/beast_asio/tests/beast_TestPeerType.h | 8 ++-- 20 files changed, 136 insertions(+), 68 deletions(-) create mode 100644 modules/beast_asio/basics/beast_PeerRole.cpp create mode 100644 modules/beast_asio/basics/beast_PeerRole.h diff --git a/Builds/VisualStudio2012/beast.vcxproj b/Builds/VisualStudio2012/beast.vcxproj index 78d0e2141f..86f07818e1 100644 --- a/Builds/VisualStudio2012/beast.vcxproj +++ b/Builds/VisualStudio2012/beast.vcxproj @@ -72,6 +72,7 @@ + @@ -296,6 +297,12 @@ + + true + true + true + true + true diff --git a/Builds/VisualStudio2012/beast.vcxproj.filters b/Builds/VisualStudio2012/beast.vcxproj.filters index 2846fd9748..9d67bed8c4 100644 --- a/Builds/VisualStudio2012/beast.vcxproj.filters +++ b/Builds/VisualStudio2012/beast.vcxproj.filters @@ -833,6 +833,9 @@ beast_asio\basics + + beast_asio\basics + @@ -1291,6 +1294,9 @@ beast_asio\tests + + beast_asio\basics + diff --git a/modules/beast_asio/basics/beast_PeerRole.cpp b/modules/beast_asio/basics/beast_PeerRole.cpp new file mode 100644 index 0000000000..f10a8baf03 --- /dev/null +++ b/modules/beast_asio/basics/beast_PeerRole.cpp @@ -0,0 +1,44 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +PeerRole::PeerRole (role_t role) + : m_role (role) +{ +} + +String PeerRole::name () const noexcept +{ + if (m_role == server) + return "server"; + return "client"; +} + +bool PeerRole::operator== (role_t role) const noexcept +{ + return m_role == role; +} + +#if 0 +PeerRole::operator Socket::handshake_type () const noexcept +{ + if (m_role == server) + return Socket::server; + return Socket::client; +} +#endif diff --git a/modules/beast_asio/basics/beast_PeerRole.h b/modules/beast_asio/basics/beast_PeerRole.h new file mode 100644 index 0000000000..9e865b2d50 --- /dev/null +++ b/modules/beast_asio/basics/beast_PeerRole.h @@ -0,0 +1,40 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#ifndef BEAST_PEERROLE_H_INCLUDED +#define BEAST_PEERROLE_H_INCLUDED + +/** Identifies if the peer is a client or a server. */ +struct PeerRole +{ + enum role_t + { + client, + server + }; + + PeerRole (role_t role); + String name () const noexcept; + bool operator== (role_t role) const noexcept; + +private: + role_t m_role; +}; + +#endif diff --git a/modules/beast_asio/beast_asio.cpp b/modules/beast_asio/beast_asio.cpp index 39fcf7d105..f8b0817cd5 100644 --- a/modules/beast_asio/beast_asio.cpp +++ b/modules/beast_asio/beast_asio.cpp @@ -24,6 +24,8 @@ namespace beast { +#include "basics/beast_PeerRole.cpp" + #include "sockets/beast_SocketBase.cpp" #include "sockets/beast_Socket.cpp" #include "sockets/beast_SslContext.cpp" diff --git a/modules/beast_asio/beast_asio.h b/modules/beast_asio/beast_asio.h index 159190c76e..be9b236de9 100644 --- a/modules/beast_asio/beast_asio.h +++ b/modules/beast_asio/beast_asio.h @@ -48,6 +48,7 @@ namespace beast // Order matters +#include "basics/beast_PeerRole.h" #include "basics/beast_BufferType.h" #include "basics/beast_CompletionCall.h" #include "basics/beast_ErrorCall.h" diff --git a/modules/beast_asio/system/beast_BoostIncludes.h b/modules/beast_asio/system/beast_BoostIncludes.h index e1c3c6c751..588fd45c26 100644 --- a/modules/beast_asio/system/beast_BoostIncludes.h +++ b/modules/beast_asio/system/beast_BoostIncludes.h @@ -38,6 +38,7 @@ # endif #endif +#include #include #include #include diff --git a/modules/beast_asio/tests/beast_TestPeerBasics.cpp b/modules/beast_asio/tests/beast_TestPeerBasics.cpp index 847030e9ba..080e502f42 100644 --- a/modules/beast_asio/tests/beast_TestPeerBasics.cpp +++ b/modules/beast_asio/tests/beast_TestPeerBasics.cpp @@ -17,32 +17,6 @@ */ //============================================================================== -TestPeerBasics::Role::Role (role_t role) - : m_role (role) -{ -} - -String TestPeerBasics::Role::name () const noexcept -{ - if (m_role == server) - return "server"; - return "client"; -} - -bool TestPeerBasics::Role::operator== (role_t role) const noexcept -{ - return m_role == role; -} - -TestPeerBasics::Role::operator Socket::handshake_type () const noexcept -{ - if (m_role == server) - return Socket::server; - return Socket::client; -} - -//------------------------------------------------------------------------------ - TestPeerBasics::Model::Model (model_t model) : m_model (model) { @@ -60,6 +34,14 @@ bool TestPeerBasics::Model::operator== (model_t model) const noexcept return m_model == model; } +boost::asio::ssl::stream_base::handshake_type + TestPeerBasics::to_handshake_type (PeerRole const& role) +{ + if (role == PeerRole::client) + return boost::asio::ssl::stream_base::client; + return boost::asio::ssl::stream_base::server; +} + //------------------------------------------------------------------------------ boost::system::error_category const& TestPeerBasics::test_category () noexcept diff --git a/modules/beast_asio/tests/beast_TestPeerBasics.h b/modules/beast_asio/tests/beast_TestPeerBasics.h index ecb0202cea..e3dd643408 100644 --- a/modules/beast_asio/tests/beast_TestPeerBasics.h +++ b/modules/beast_asio/tests/beast_TestPeerBasics.h @@ -27,26 +27,6 @@ class TestPeerBasics { public: - /** Identifies if the peer is a client or a server. */ - struct Role - { - enum role_t - { - client, - server - }; - - Role (role_t role); - String name () const noexcept; - bool operator== (role_t role) const noexcept; - operator Socket::handshake_type () const noexcept; - - private: - role_t m_role; - }; - - //-------------------------------------------------------------------------- - /** Selects between synchronous or asynchronous networking i/o usage. */ struct Model { @@ -66,6 +46,11 @@ public: //-------------------------------------------------------------------------- + /** Convert a PeerRole to boost::asio::ssl::stream_base_handshake_type */ + static boost::asio::ssl::stream_base::handshake_type to_handshake_type (PeerRole const& role); + + //-------------------------------------------------------------------------- + // Custom error codes for distinguishing test conditions struct errc { diff --git a/modules/beast_asio/tests/beast_TestPeerDetailsTcp.h b/modules/beast_asio/tests/beast_TestPeerDetailsTcp.h index a9a8de49fa..72ac36fa79 100644 --- a/modules/beast_asio/tests/beast_TestPeerDetailsTcp.h +++ b/modules/beast_asio/tests/beast_TestPeerDetailsTcp.h @@ -79,18 +79,18 @@ public: return m_acceptor; } - endpoint_type get_endpoint (TestPeer::Role role) + endpoint_type get_endpoint (PeerRole role) { if (m_protocol == protocol_type::v4 ()) { - if (role == TestPeer::Role::server) + if (role == PeerRole::server) return endpoint_type (m_protocol, 1053); else return endpoint_type (boost::asio::ip::address_v4::loopback (), 1053); } else { - if (role == TestPeer::Role::server) + if (role == PeerRole::server) return endpoint_type (m_protocol, 1052); else return endpoint_type (boost::asio::ip::address_v6 ().from_string ("::1"), 1052); diff --git a/modules/beast_asio/tests/beast_TestPeerLogic.h b/modules/beast_asio/tests/beast_TestPeerLogic.h index 43a359bbbd..f281d09e23 100644 --- a/modules/beast_asio/tests/beast_TestPeerLogic.h +++ b/modules/beast_asio/tests/beast_TestPeerLogic.h @@ -35,7 +35,7 @@ public: Socket& socket () noexcept; - virtual Role get_role () const noexcept = 0; + virtual PeerRole get_role () const noexcept = 0; virtual Model get_model () const noexcept = 0; diff --git a/modules/beast_asio/tests/beast_TestPeerLogicAsyncClient.cpp b/modules/beast_asio/tests/beast_TestPeerLogicAsyncClient.cpp index 03a90112c1..c431beae38 100644 --- a/modules/beast_asio/tests/beast_TestPeerLogicAsyncClient.cpp +++ b/modules/beast_asio/tests/beast_TestPeerLogicAsyncClient.cpp @@ -22,9 +22,9 @@ TestPeerLogicAsyncClient::TestPeerLogicAsyncClient (Socket& socket) { } -TestPeerBasics::Role TestPeerLogicAsyncClient::get_role () const noexcept +PeerRole TestPeerLogicAsyncClient::get_role () const noexcept { - return Role::client; + return PeerRole::client; } TestPeerBasics::Model TestPeerLogicAsyncClient::get_model () const noexcept diff --git a/modules/beast_asio/tests/beast_TestPeerLogicAsyncClient.h b/modules/beast_asio/tests/beast_TestPeerLogicAsyncClient.h index c97a8fffe5..070e034abe 100644 --- a/modules/beast_asio/tests/beast_TestPeerLogicAsyncClient.h +++ b/modules/beast_asio/tests/beast_TestPeerLogicAsyncClient.h @@ -24,7 +24,7 @@ class TestPeerLogicAsyncClient : public TestPeerLogic { public: explicit TestPeerLogicAsyncClient (Socket& socket); - Role get_role () const noexcept; + PeerRole get_role () const noexcept; Model get_model () const noexcept; void on_connect_async (error_code const& ec); void on_handshake (error_code const& ec); diff --git a/modules/beast_asio/tests/beast_TestPeerLogicAsyncServer.cpp b/modules/beast_asio/tests/beast_TestPeerLogicAsyncServer.cpp index 112e51a8c4..46fa6cfb1e 100644 --- a/modules/beast_asio/tests/beast_TestPeerLogicAsyncServer.cpp +++ b/modules/beast_asio/tests/beast_TestPeerLogicAsyncServer.cpp @@ -22,9 +22,9 @@ TestPeerLogicAsyncServer::TestPeerLogicAsyncServer (Socket& socket) { } -TestPeerBasics::Role TestPeerLogicAsyncServer::get_role () const noexcept +PeerRole TestPeerLogicAsyncServer::get_role () const noexcept { - return Role::server; + return PeerRole::server; } TestPeerBasics::Model TestPeerLogicAsyncServer::get_model () const noexcept diff --git a/modules/beast_asio/tests/beast_TestPeerLogicAsyncServer.h b/modules/beast_asio/tests/beast_TestPeerLogicAsyncServer.h index 0b0d8e7f35..43d453e80e 100644 --- a/modules/beast_asio/tests/beast_TestPeerLogicAsyncServer.h +++ b/modules/beast_asio/tests/beast_TestPeerLogicAsyncServer.h @@ -24,7 +24,7 @@ class TestPeerLogicAsyncServer : public TestPeerLogic { public: explicit TestPeerLogicAsyncServer (Socket& socket); - Role get_role () const noexcept; + PeerRole get_role () const noexcept; Model get_model () const noexcept; void on_connect_async (error_code const& ec); void on_handshake (error_code const& ec); diff --git a/modules/beast_asio/tests/beast_TestPeerLogicSyncClient.cpp b/modules/beast_asio/tests/beast_TestPeerLogicSyncClient.cpp index 87775fd6bd..9b579090b5 100644 --- a/modules/beast_asio/tests/beast_TestPeerLogicSyncClient.cpp +++ b/modules/beast_asio/tests/beast_TestPeerLogicSyncClient.cpp @@ -22,9 +22,9 @@ TestPeerLogicSyncClient::TestPeerLogicSyncClient (Socket& socket) { } -TestPeerBasics::Role TestPeerLogicSyncClient::get_role () const noexcept +PeerRole TestPeerLogicSyncClient::get_role () const noexcept { - return Role::client; + return PeerRole::client; } TestPeerBasics::Model TestPeerLogicSyncClient::get_model () const noexcept @@ -43,7 +43,7 @@ void TestPeerLogicSyncClient::on_connect () if (socket ().requires_handshake ()) { - if (failure (socket ().handshake (get_role (), error ()))) + if (failure (socket ().handshake (to_handshake_type (get_role ()), error ()))) return; } diff --git a/modules/beast_asio/tests/beast_TestPeerLogicSyncClient.h b/modules/beast_asio/tests/beast_TestPeerLogicSyncClient.h index 2e1a555856..61cd29b393 100644 --- a/modules/beast_asio/tests/beast_TestPeerLogicSyncClient.h +++ b/modules/beast_asio/tests/beast_TestPeerLogicSyncClient.h @@ -24,7 +24,7 @@ class TestPeerLogicSyncClient : public TestPeerLogic { public: explicit TestPeerLogicSyncClient (Socket& socket); - Role get_role () const noexcept; + PeerRole get_role () const noexcept; Model get_model () const noexcept; void on_connect (); virtual void on_pre_handshake (); diff --git a/modules/beast_asio/tests/beast_TestPeerLogicSyncServer.cpp b/modules/beast_asio/tests/beast_TestPeerLogicSyncServer.cpp index 7be6335d68..b19a1f9e8e 100644 --- a/modules/beast_asio/tests/beast_TestPeerLogicSyncServer.cpp +++ b/modules/beast_asio/tests/beast_TestPeerLogicSyncServer.cpp @@ -22,9 +22,9 @@ TestPeerLogicSyncServer::TestPeerLogicSyncServer (Socket& socket) { } -TestPeerBasics::Role TestPeerLogicSyncServer::get_role () const noexcept +PeerRole TestPeerLogicSyncServer::get_role () const noexcept { - return Role::server; + return PeerRole::server; } TestPeerBasics::Model TestPeerLogicSyncServer::get_model () const noexcept @@ -36,7 +36,7 @@ void TestPeerLogicSyncServer::on_connect () { if (socket ().requires_handshake ()) { - if (failure (socket ().handshake (get_role (), error ()))) + if (failure (socket ().handshake (to_handshake_type (get_role ()), error ()))) return; } diff --git a/modules/beast_asio/tests/beast_TestPeerLogicSyncServer.h b/modules/beast_asio/tests/beast_TestPeerLogicSyncServer.h index c198ea6f62..b6f8bccb52 100644 --- a/modules/beast_asio/tests/beast_TestPeerLogicSyncServer.h +++ b/modules/beast_asio/tests/beast_TestPeerLogicSyncServer.h @@ -24,7 +24,7 @@ class TestPeerLogicSyncServer : public TestPeerLogic { public: explicit TestPeerLogicSyncServer (Socket& socket); - Role get_role () const noexcept; + PeerRole get_role () const noexcept; Model get_model () const noexcept; void on_connect (); }; diff --git a/modules/beast_asio/tests/beast_TestPeerType.h b/modules/beast_asio/tests/beast_TestPeerType.h index c76b154e32..485c4ad4bb 100644 --- a/modules/beast_asio/tests/beast_TestPeerType.h +++ b/modules/beast_asio/tests/beast_TestPeerType.h @@ -175,11 +175,11 @@ public: { if (is_async ()) { - if (get_role () == Role::server) + if (get_role () == PeerRole::server) { run_async_server (); } - else if (get_role () == Role::client) + else if (get_role () == PeerRole::client) { run_async_client (); } @@ -190,11 +190,11 @@ public: } else if (get_model () == Model::sync) { - if (get_role () == Role::server) + if (get_role () == PeerRole::server) { run_sync_server (); } - else if (get_role () == Role::client) + else if (get_role () == PeerRole::client) { run_sync_client (); }