├── .gitignore ├── CHANGELOG.md ├── CMakeLists.txt ├── LICENSE_1_0.txt ├── README.md ├── cmake ├── CppWAMPDependencies.cmake ├── FindCatch2.cmake └── FindJsoncons.cmake ├── cppwamp-coro └── CMakeLists.txt ├── cppwamp ├── CMakeLists.txt ├── include │ └── cppwamp │ │ ├── anyhandler.hpp │ │ ├── api.hpp │ │ ├── asiodefs.hpp │ │ ├── asyncresult.hpp │ │ ├── blob.hpp │ │ ├── bundled │ │ ├── boost_asio_any_completion_executor.hpp │ │ ├── boost_asio_any_completion_handler.hpp │ │ └── boost_asio_impl_any_completion_executor.ipp │ │ ├── cbor.hpp │ │ ├── chits.hpp │ │ ├── codec.hpp │ │ ├── config.hpp │ │ ├── connector.hpp │ │ ├── consolelogger.hpp │ │ ├── conversion.hpp │ │ ├── conversionaccess.hpp │ │ ├── coro │ │ ├── corosession.hpp │ │ └── corounpacker.hpp │ │ ├── corosession.hpp │ │ ├── corounpacker.hpp │ │ ├── error.hpp │ │ ├── erroror.hpp │ │ ├── internal │ │ ├── base64.hpp │ │ ├── blob.ipp │ │ ├── callee.hpp │ │ ├── caller.hpp │ │ ├── callertimeout.hpp │ │ ├── cbor.ipp │ │ ├── challengee.hpp │ │ ├── chits.ipp │ │ ├── client.hpp │ │ ├── connector.ipp │ │ ├── consolelogger.ipp │ │ ├── endian.hpp │ │ ├── error.ipp │ │ ├── integersequence.hpp │ │ ├── json.ipp │ │ ├── jsonencoding.hpp │ │ ├── logging.ipp │ │ ├── messagetraits.hpp │ │ ├── messagetraits.ipp │ │ ├── msgpack.ipp │ │ ├── passkey.hpp │ │ ├── peer.hpp │ │ ├── peerdata.ipp │ │ ├── rawsockconnector.hpp │ │ ├── rawsockhandshake.hpp │ │ ├── rawsockheader.hpp │ │ ├── rawsocklistener.hpp │ │ ├── rawsocktransport.hpp │ │ ├── registration.ipp │ │ ├── session.ipp │ │ ├── socketoptions.hpp │ │ ├── subscriber.hpp │ │ ├── subscription.ipp │ │ ├── tcp.ipp │ │ ├── tcpacceptor.hpp │ │ ├── tcpendpoint.ipp │ │ ├── tcphost.ipp │ │ ├── tcpopener.hpp │ │ ├── tcpprotocol.ipp │ │ ├── uds.ipp │ │ ├── udsacceptor.hpp │ │ ├── udsopener.hpp │ │ ├── udspath.ipp │ │ ├── udsprotocol.ipp │ │ ├── variant.ipp │ │ ├── variantdecoding.hpp │ │ ├── variantencoding.hpp │ │ ├── varianttraits.hpp │ │ ├── variantvisitors.hpp │ │ ├── version.ipp │ │ └── wampmessage.hpp │ │ ├── json.hpp │ │ ├── logging.hpp │ │ ├── messagebuffer.hpp │ │ ├── msgpack.hpp │ │ ├── null.hpp │ │ ├── options.hpp │ │ ├── payload.hpp │ │ ├── peerdata.hpp │ │ ├── rawsockoptions.hpp │ │ ├── registration.hpp │ │ ├── session.hpp │ │ ├── sessiondata.hpp │ │ ├── spawn.hpp │ │ ├── subscription.hpp │ │ ├── tagtypes.hpp │ │ ├── tcp.hpp │ │ ├── tcpendpoint.hpp │ │ ├── tcphost.hpp │ │ ├── tcpprotocol.hpp │ │ ├── traits.hpp │ │ ├── transport.hpp │ │ ├── types │ │ ├── array.hpp │ │ ├── boostoptional.hpp │ │ ├── optional.hpp │ │ ├── set.hpp │ │ ├── tuple.hpp │ │ ├── unorderedmap.hpp │ │ └── unorderedset.hpp │ │ ├── uds.hpp │ │ ├── udspath.hpp │ │ ├── udsprotocol.hpp │ │ ├── unpacker.hpp │ │ ├── variant.hpp │ │ ├── variantdefs.hpp │ │ ├── version.hpp │ │ ├── visitor.hpp │ │ └── wampdefs.hpp └── src │ └── cppwamp.cpp ├── doc ├── CMakeLists.txt ├── Doxyfile.in ├── architecture.vpp ├── cppwamp.dox ├── images │ ├── client_api.svg │ ├── layers.svg │ ├── messaging.svg │ ├── registrations.svg │ ├── subscriptions.svg │ └── transport.svg ├── index.html ├── registrations.dox ├── subscriptions.dox ├── tutorial-callbacks.dox ├── tutorial-connections.dox ├── tutorial-conversions.dox ├── tutorial-errors.dox ├── tutorial-pubsub.dox ├── tutorial-rpc.dox ├── tutorial-sessions.dox ├── tutorial-variants.dox └── typerequirements.dox ├── examples ├── .crossbar │ └── config.json ├── CMakeLists.txt ├── asynctimeclient │ ├── CMakeLists.txt │ └── main.cpp ├── asynctimeservice │ ├── CMakeLists.txt │ └── main.cpp ├── chat │ ├── CMakeLists.txt │ └── main.cpp ├── coro20timeclient │ ├── CMakeLists.txt │ └── main.cpp ├── coro20timeservice │ ├── CMakeLists.txt │ └── main.cpp ├── futuretimeclient │ ├── CMakeLists.txt │ └── main.cpp ├── futuretimeservice │ ├── CMakeLists.txt │ └── main.cpp ├── stacklesstimeclient │ ├── CMakeLists.txt │ └── main.cpp ├── stacklesstimeservice │ ├── CMakeLists.txt │ └── main.cpp ├── timeclient │ ├── CMakeLists.txt │ └── main.cpp └── timeservice │ ├── CMakeLists.txt │ └── main.cpp ├── packaging ├── CMakeLists.txt └── CppWAMPConfig.cmake └── test ├── .crossbar ├── config.json └── oldconfig.json ├── CMakeLists.txt ├── codectestcbor.cpp ├── codectestjson.cpp ├── codectestmsgpack.cpp ├── main.cpp ├── payloadtest.cpp ├── precompiled.hpp ├── transporttest.cpp ├── varianttestassign.cpp ├── varianttestbadaccess.cpp ├── varianttestcomparison.cpp ├── varianttestconvert.cpp ├── varianttestconvertboost.cpp ├── varianttestconvertcontainers.cpp ├── varianttestconverter.cpp ├── varianttestconverttuple.cpp ├── varianttestinfo.cpp ├── varianttestinit.cpp ├── varianttestmap.cpp ├── varianttestoutput.cpp ├── varianttestvector.cpp ├── varianttestvisitation.cpp ├── wampoldtest.cpp ├── wampoldtestadvanced.cpp ├── wamptest.cpp └── wamptestadvanced.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | build*/ 2 | _*/ 3 | CMakeLists.txt.user 4 | CMakeUserPresets.json 5 | key.priv 6 | key.pub 7 | *.autosave 8 | *.vpp~* 9 | *.vpp.bak 10 | -------------------------------------------------------------------------------- /LICENSE_1_0.txt: -------------------------------------------------------------------------------- 1 | Boost Software License - Version 1.0 - August 17th, 2003 2 | 3 | Permission is hereby granted, free of charge, to any person or organization 4 | obtaining a copy of the software and accompanying documentation covered by 5 | this license (the "Software") to use, reproduce, display, distribute, 6 | execute, and transmit the Software, and to prepare derivative works of the 7 | Software, and to permit third-parties to whom the Software is furnished to 8 | do so, all subject to the following: 9 | 10 | The copyright notices in the Software and this entire statement, including 11 | the above license grant, this restriction and the following disclaimer, 12 | must be included in all copies of the Software, in whole or in part, and 13 | all derivative works of the Software, unless such copies or derivative 14 | works are solely in the form of machine-executable object code generated by 15 | a source language processor. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 20 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 21 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 22 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 | DEALINGS IN THE SOFTWARE. 24 | 25 | -------------------------------------------------------------------------------- /cmake/FindCatch2.cmake: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # Copyright Butterfly Energy Systems 2022. 3 | # Distributed under the Boost Software License, Version 1.0. 4 | # https://www.boost.org/LICENSE_1_0.txt 5 | #------------------------------------------------------------------------------- 6 | 7 | #[=======================================================================[.rst: 8 | FindCatch2 9 | ------------- 10 | 11 | Finds the Catch2 v2 header-only library. 12 | 13 | Imported Targets 14 | ^^^^^^^^^^^^^^^^ 15 | 16 | This module provides the following imported targets, if found: 17 | 18 | ``Catch2::Catch2`` 19 | The Catch2 v2 header-only interface library 20 | 21 | Result Variables 22 | ^^^^^^^^^^^^^^^^ 23 | 24 | This will define the following variables: 25 | 26 | ``Catch2_FOUND`` 27 | True if the system has the Catch2 headers. 28 | ``Catch2_VERSION`` 29 | The version of the Catch2 library which was found. 30 | ``Catch2_INCLUDE_DIRS`` 31 | Include directories needed to use Catch2. 32 | 33 | Cache Variables 34 | ^^^^^^^^^^^^^^^ 35 | 36 | The following cache variables may also be set: 37 | 38 | ``Catch2_INCLUDE_DIR`` 39 | The ``single_include`` directory containing ``catch2/catch.hpp``. 40 | 41 | Hints 42 | ^^^^^ 43 | 44 | This module reads hints about search locations from variables: 45 | 46 | ``Catch2_ROOT`` 47 | Preferred installation prefix. 48 | 49 | #]=======================================================================] 50 | 51 | find_package(PkgConfig) 52 | if(PKG_CONFIG_FOUND) 53 | pkg_check_modules(PC_Catch2 QUIET catch2) 54 | endif() 55 | 56 | find_path(Catch2_INCLUDE_DIR "catch2/catch.hpp" 57 | HINTS ${PC_Catch2_INCLUDE_DIRS} ${PC_Catch2_INCLUDEDIR} 58 | ) 59 | set(Catch2_VERSION ${PC_Catch2_VERSION}) 60 | 61 | set(Catch2_VERSION_FILE 62 | "${Catch2_INCLUDE_DIR}/catch2/catch.hpp") 63 | if(NOT "${Catch2_INCLUDE_DIR}" STREQUAL "Catch2_INCLUDE_DIR-NOTFOUND" 64 | AND "${Catch2_VERSION}" STREQUAL "" 65 | AND EXISTS ${Catch2_VERSION_FILE}) 66 | # Expected format: 67 | #define CATCH_VERSION_MAJOR N 68 | #define CATCH_VERSION_MINOR N 69 | #define CATCH_VERSION_PATCH N 70 | file(STRINGS ${Catch2_VERSION_FILE} Catch2_VERSION_LINES 71 | REGEX "#define CATCH_VERSION_" 72 | LIMIT_COUNT 3) 73 | list(LENGTH Catch2_VERSION_LINES Catch2_VERSION_LINE_COUNT) 74 | if(${Catch2_VERSION_LINE_COUNT} GREATER 2) 75 | list(GET Catch2_VERSION_LINES 0 Catch2_VMAJ_LN) 76 | list(GET Catch2_VERSION_LINES 1 Catch2_VMIN_LN) 77 | list(GET Catch2_VERSION_LINES 2 Catch2_VREV_LN) 78 | string(REGEX MATCH "[0-9]" Catch2_VMAJ ${Catch2_VMAJ_LN}) 79 | string(REGEX MATCH "[0-9]" Catch2_VMIN ${Catch2_VMIN_LN}) 80 | string(REGEX MATCH "[0-9]" Catch2_VREV ${Catch2_VREV_LN}) 81 | set(Catch2_VERSION 82 | "${Catch2_VMAJ}.${Catch2_VMIN}.${Catch2_VREV}") 83 | endif() 84 | endif() 85 | 86 | include(FindPackageHandleStandardArgs) 87 | find_package_handle_standard_args(Catch2 88 | FOUND_VAR Catch2_FOUND 89 | REQUIRED_VARS Catch2_INCLUDE_DIR 90 | VERSION_VAR Catch2_VERSION 91 | ) 92 | 93 | if(Catch2_FOUND) 94 | set(Catch2_INCLUDE_DIRS ${Catch2_INCLUDE_DIR}) 95 | 96 | if(NOT TARGET Catch2::Catch2) 97 | if(NOT TARGET Catch2) 98 | add_library(Catch2::Catch2 INTERFACE IMPORTED) 99 | else() 100 | add_library(Catch2::Catch2 ALIAS Catch2) 101 | endif() 102 | target_include_directories(Catch2::Catch2 SYSTEM 103 | INTERFACE "${Catch2_INCLUDE_DIR}") 104 | endif() 105 | endif() 106 | 107 | mark_as_advanced(Catch2_INCLUDE_DIR) 108 | -------------------------------------------------------------------------------- /cmake/FindJsoncons.cmake: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # Copyright Butterfly Energy Systems 2022. 3 | # Distributed under the Boost Software License, Version 1.0. 4 | # https://www.boost.org/LICENSE_1_0.txt 5 | #------------------------------------------------------------------------------- 6 | 7 | #[=======================================================================[.rst: 8 | FindJsoncons 9 | ----------- 10 | 11 | Finds the jsoncons C++ library. 12 | 13 | Imported Targets 14 | ^^^^^^^^^^^^^^^^ 15 | 16 | This module provides the following imported targets, if found: 17 | 18 | ``jsonconsc-cxx`` 19 | The jsoncons C++ header-only interface library 20 | 21 | Result Variables 22 | ^^^^^^^^^^^^^^^^ 23 | 24 | This will define the following variables: 25 | 26 | ``jsoncons_FOUND`` 27 | True if the system has the jsoncons C++ headers. 28 | ``jsoncons_VERSION`` 29 | The version of the jsoncons C++ library which was found. 30 | ``jsoncons_INCLUDE_DIRS`` 31 | Include directories needed to use jsoncons C++. 32 | 33 | Cache Variables 34 | ^^^^^^^^^^^^^^^ 35 | 36 | The following cache variables may also be set: 37 | 38 | ``jsoncons_INCLUDE_DIR`` 39 | The directory containing ``jsoncons.hpp``. 40 | 41 | Hints 42 | ^^^^^ 43 | 44 | This module reads hints about search locations from variables: 45 | 46 | ``jsoncons_ROOT`` 47 | Preferred installation prefix. 48 | 49 | #]=======================================================================] 50 | 51 | find_package(PkgConfig) 52 | if(PKG_CONFIG_FOUND) 53 | pkg_check_modules(PC_jsoncons QUIET jsoncons) 54 | endif() 55 | 56 | find_path(jsoncons_INCLUDE_DIR "jsoncons/jsoncons.hpp" 57 | HINTS ${PC_jsoncons_INCLUDE_DIRS} ${PC_jsoncons_INCLUDEDIR} 58 | ) 59 | set(jsoncons_VERSION ${PC_jsoncons_VERSION}) 60 | 61 | set(jsoncons_VERSION_FILE 62 | "${jsoncons_INCLUDE_DIR}/jsoncons/config/version.hpp") 63 | if(NOT "${jsoncons_INCLUDE_DIR}" STREQUAL "jsoncons_INCLUDE_DIR-NOTFOUND" 64 | AND "${jsoncons_VERSION}" STREQUAL "" 65 | AND EXISTS ${jsoncons_VERSION_FILE}) 66 | # Expected version_master.hpp file format: 67 | #define JSONCONS_VERSION_MAJOR N 68 | #define JSONCONS_VERSION_MINOR N 69 | #define JSONCONS_VERSION_PATCH N 70 | file(STRINGS ${jsoncons_VERSION_FILE} jsoncons_VERSION_LINES 71 | REGEX "#define JSONCONS_VERSION" 72 | LIMIT_COUNT 3) 73 | list(LENGTH jsoncons_VERSION_LINES jsoncons_VERSION_LINE_COUNT) 74 | if(${jsoncons_VERSION_LINE_COUNT} GREATER 2) 75 | list(GET jsoncons_VERSION_LINES 0 jsoncons_VMAJ_LN) 76 | list(GET jsoncons_VERSION_LINES 1 jsoncons_VMIN_LN) 77 | list(GET jsoncons_VERSION_LINES 2 jsoncons_VREV_LN) 78 | string(REGEX MATCH "[0-9]" jsoncons_VMAJ ${jsoncons_VMAJ_LN}) 79 | string(REGEX MATCH "[0-9]" jsoncons_VMIN ${jsoncons_VMIN_LN}) 80 | string(REGEX MATCH "[0-9]" jsoncons_VREV ${jsoncons_VREV_LN}) 81 | set(jsoncons_VERSION 82 | "${jsoncons_VMAJ}.${jsoncons_VMIN}.${jsoncons_VREV}") 83 | endif() 84 | endif() 85 | 86 | include(FindPackageHandleStandardArgs) 87 | find_package_handle_standard_args(jsoncons 88 | FOUND_VAR jsoncons_FOUND 89 | REQUIRED_VARS jsoncons_INCLUDE_DIR 90 | VERSION_VAR jsoncons_VERSION 91 | ) 92 | 93 | if(jsoncons_FOUND) 94 | set(jsoncons_INCLUDE_DIRS ${jsoncons_INCLUDE_DIR}) 95 | if(NOT TARGET jsoncons) 96 | add_library(jsoncons INTERFACE IMPORTED) 97 | target_include_directories(jsoncons SYSTEM 98 | INTERFACE "${jsoncons_INCLUDE_DIR}") 99 | endif() 100 | endif() 101 | 102 | mark_as_advanced(jsoncons_INCLUDE_DIR) 103 | -------------------------------------------------------------------------------- /cppwamp-coro/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # Copyright Butterfly Energy Systems 2022. 3 | # Distributed under the Boost Software License, Version 1.0. 4 | # https://www.boost.org/LICENSE_1_0.txt 5 | #------------------------------------------------------------------------------- 6 | 7 | # Empty interface library for conveniently adding usage requirements to 8 | # projects wanting to use stackful coroutines. 9 | add_library(cppwamp-coro-usage INTERFACE) 10 | target_compile_features(cppwamp-coro-usage INTERFACE cxx_std_11) 11 | target_link_libraries(cppwamp-coro-usage 12 | INTERFACE 13 | CppWAMP::coro-usage 14 | "$") 15 | set_target_properties(cppwamp-coro-usage PROPERTIES EXPORT_NAME coro-usage) 16 | add_library(CppWAMP::coro-usage ALIAS cppwamp-coro-usage) 17 | 18 | # Deprecated aliases left for backward compatibility 19 | add_library(cppwamp-coro-headers ALIAS cppwamp-coro-usage) 20 | add_library(CppWAMP::coro-headers ALIAS cppwamp-coro-usage) 21 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/api.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_API_HPP 8 | #define CPPWAMP_API_HPP 9 | 10 | //------------------------------------------------------------------------------ 11 | /** @file 12 | @brief Defines macros related to exporting/importing APIs. */ 13 | //------------------------------------------------------------------------------ 14 | 15 | #ifdef CPPWAMP_COMPILED_LIB 16 | # define CPPWAMP_INLINE 17 | # if defined _WIN32 || defined __CYGWIN__ 18 | # define CPPWAMP_API_IMPORT __declspec(dllimport) 19 | # define CPPWAMP_API_EXPORT __declspec(dllexport) 20 | # define CPPWAMP_API_HIDDEN 21 | # else 22 | # define CPPWAMP_API_IMPORT __attribute__((visibility("default"))) 23 | # define CPPWAMP_API_EXPORT __attribute__((visibility("default"))) 24 | # define CPPWAMP_API_HIDDEN __attribute__((visibility("hidden"))) 25 | # endif 26 | # ifdef CPPWAMP_IS_STATIC 27 | # define CPPWAMP_API 28 | # define CPPWAMP_HIDDEN 29 | # else 30 | # ifdef cppwamp_core_EXPORTS // We are building this library 31 | # define CPPWAMP_API CPPWAMP_API_EXPORT 32 | # else // We are using this library 33 | # define CPPWAMP_API CPPWAMP_API_IMPORT 34 | # endif 35 | # define CPPWAMP_HIDDEN CPPWAMP_API_HIDDEN 36 | # endif 37 | #else 38 | # define CPPWAMP_INLINE inline 39 | # define CPPWAMP_API 40 | # define CPPWAMP_HIDDEN 41 | #endif 42 | 43 | #endif // CPPWAMP_JSON_API_HPP 44 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/asiodefs.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_ASIODEFS_HPP 8 | #define CPPWAMP_ASIODEFS_HPP 9 | 10 | //------------------------------------------------------------------------------ 11 | /** @file 12 | @brief Commonly used Boost.Asio type aliases. 13 | @see */ 14 | //------------------------------------------------------------------------------ 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include "config.hpp" 22 | 23 | namespace wamp 24 | { 25 | 26 | /** Polymorphic executor for I/O objects. */ 27 | using AnyIoExecutor = boost::asio::any_io_executor; 28 | 29 | /** Alias of AnyIoExecutor kept for backward compatibility. 30 | @deprecated Use wamp::AnyIoExecutor instead. */ 31 | using AnyExecutor CPPWAMP_DEPRECATED = AnyIoExecutor; 32 | 33 | /** Queues and runs I/O completion handlers. */ 34 | using IoContext = boost::asio::io_context; 35 | 36 | /** Alias of IoContext kept for backward compatibility. */ 37 | using AsioContext CPPWAMP_DEPRECATED = boost::asio::io_context; 38 | 39 | /** Alias of AsioContext kept for backward compatibility. 40 | @deprecated Use wamp::AsioContext instead. */ 41 | using AsioService CPPWAMP_DEPRECATED = IoContext; 42 | 43 | /** Serializes I/O operations. */ 44 | using IoStrand = boost::asio::strand; 45 | 46 | /** Type used by Boost.Asio for reporting errors. 47 | @deprecated Will be removed */ 48 | using AsioErrorCode = boost::system::error_code; 49 | 50 | /** Metafunction that determines if T meets the requirements of 51 | Boost.Asio's ExecutionContext. */ 52 | template 53 | static constexpr bool isExecutionContext() 54 | { 55 | return std::is_base_of::value; 56 | } 57 | 58 | /** Completion token used to indicate that there is no completion handler 59 | waiting for the operation's result. */ 60 | using Detached = boost::asio::detached_t; 61 | 62 | #if defined(BOOST_ASIO_HAS_CONSTEXPR) || defined(CPPWAMP_FOR_DOXYGEN) 63 | constexpr Detached detached; 64 | #endif 65 | 66 | // boost::asio::detached 67 | 68 | } // namespace wamp 69 | 70 | #endif // CPPWAMP_ASIODEFS_HPP 71 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/asyncresult.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_ASYNCRESULT_HPP 8 | #define CPPWAMP_ASYNCRESULT_HPP 9 | 10 | //------------------------------------------------------------------------------ 11 | /** @file 12 | @brief Backward compatibility header: use instead. */ 13 | //------------------------------------------------------------------------------ 14 | 15 | #include "config.hpp" 16 | #include "erroror.hpp" 17 | 18 | namespace wamp 19 | { 20 | 21 | /** @deprecated Backward compatiblity type alias. */ 22 | template 23 | using AsyncResult CPPWAMP_DEPRECATED = ErrorOr; 24 | 25 | } // namespace wamp 26 | 27 | #endif // CPPWAMP_ASYNCRESULT_HPP 28 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/blob.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_BLOB_HPP 8 | #define CPPWAMP_BLOB_HPP 9 | 10 | //------------------------------------------------------------------------------ 11 | /** @file 12 | @brief Contains the declaration of Variant and other closely related 13 | types/functions. */ 14 | //------------------------------------------------------------------------------ 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include "api.hpp" 21 | 22 | namespace wamp 23 | { 24 | 25 | //------------------------------------------------------------------------------ 26 | /** Contains binary data as an array of bytes. */ 27 | //------------------------------------------------------------------------------ 28 | class CPPWAMP_API Blob 29 | { 30 | public: 31 | /// Array of bytes used to contain the binary data. 32 | using Data = std::vector; 33 | 34 | /** Default constructor. */ 35 | Blob(); 36 | 37 | /** Constructor taking a vector of bytes. */ 38 | explicit Blob(Data data); 39 | 40 | /** Constructor taking an initializer list. */ 41 | explicit Blob(std::initializer_list list); 42 | 43 | /** Obtains the blob's array of bytes. */ 44 | Data& data(); 45 | 46 | /** Obtains the blob's constant array of bytes. */ 47 | const Data& data() const; 48 | 49 | /** Equality comparison. */ 50 | bool operator==(const Blob& other) const; 51 | 52 | /** Inequality comparison. */ 53 | bool operator!=(const Blob& other) const; 54 | 55 | /** Less-than comparison. */ 56 | bool operator<(const Blob& other) const; 57 | 58 | private: 59 | std::vector data_; 60 | }; 61 | 62 | //------------------------------------------------------------------------------ 63 | /** Outputs a Blob to the given output stream. 64 | @relates Blob */ 65 | //------------------------------------------------------------------------------ 66 | CPPWAMP_API std::ostream& operator<<(std::ostream& out, const Blob& blob); 67 | 68 | } // namespace wamp 69 | 70 | #ifndef CPPWAMP_COMPILED_LIB 71 | #include "internal/blob.ipp" 72 | #endif 73 | 74 | #endif // CPPWAMP_BLOB_HPP 75 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/bundled/boost_asio_impl_any_completion_executor.ipp: -------------------------------------------------------------------------------- 1 | // 2 | // impl/any_completion_executor.ipp 3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 4 | // 5 | // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com) 6 | // 7 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 8 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 9 | // 10 | 11 | // Original retrieved from 12 | // https://github.com/chriskohlhoff/asio/blob/any-completion-handler/asio/include/asio/impl/any_completion_executor.ipp 13 | // Adapted for use by CppWAMP 14 | 15 | #ifndef BOOST_ASIO_IMPL_ANY_COMPLETION_EXECUTOR_IPP 16 | #define BOOST_ASIO_IMPL_ANY_COMPLETION_EXECUTOR_IPP 17 | 18 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) 19 | # pragma once 20 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) 21 | 22 | #include "boost/asio/detail/config.hpp" 23 | 24 | #if !defined(BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT) 25 | 26 | #include "boost_asio_any_completion_executor.hpp" 27 | 28 | #include "boost/asio/detail/push_options.hpp" 29 | 30 | namespace boost { 31 | namespace asio { 32 | 33 | any_completion_executor::any_completion_executor() BOOST_ASIO_NOEXCEPT 34 | : base_type() 35 | { 36 | } 37 | 38 | any_completion_executor::any_completion_executor(nullptr_t) BOOST_ASIO_NOEXCEPT 39 | : base_type(nullptr_t()) 40 | { 41 | } 42 | 43 | any_completion_executor::any_completion_executor(const any_completion_executor& e) BOOST_ASIO_NOEXCEPT 44 | : base_type(static_cast(e)) 45 | { 46 | } 47 | 48 | #if defined(BOOST_ASIO_HAS_MOVE) 49 | any_completion_executor::any_completion_executor(any_completion_executor&& e) BOOST_ASIO_NOEXCEPT 50 | : base_type(static_cast(e)) 51 | { 52 | } 53 | #endif // defined(BOOST_ASIO_HAS_MOVE) 54 | 55 | any_completion_executor& any_completion_executor::operator=( 56 | const any_completion_executor& e) BOOST_ASIO_NOEXCEPT 57 | { 58 | base_type::operator=(static_cast(e)); 59 | return *this; 60 | } 61 | 62 | #if defined(BOOST_ASIO_HAS_MOVE) 63 | any_completion_executor& any_completion_executor::operator=( 64 | any_completion_executor&& e) BOOST_ASIO_NOEXCEPT 65 | { 66 | base_type::operator=(static_cast(e)); 67 | return *this; 68 | } 69 | #endif // defined(BOOST_ASIO_HAS_MOVE) 70 | 71 | any_completion_executor& any_completion_executor::operator=(nullptr_t) 72 | { 73 | base_type::operator=(nullptr_t()); 74 | return *this; 75 | } 76 | 77 | any_completion_executor::~any_completion_executor() 78 | { 79 | } 80 | 81 | void any_completion_executor::swap(any_completion_executor& other) BOOST_ASIO_NOEXCEPT 82 | { 83 | static_cast(*this).swap(static_cast(other)); 84 | } 85 | 86 | template <> 87 | any_completion_executor any_completion_executor::require( 88 | const execution::blocking_t::never_t& p, int) const 89 | { 90 | return static_cast(*this).require(p); 91 | } 92 | 93 | template <> 94 | any_completion_executor any_completion_executor::prefer( 95 | const execution::blocking_t::possibly_t& p, int) const 96 | { 97 | return static_cast(*this).prefer(p); 98 | } 99 | 100 | template <> 101 | any_completion_executor any_completion_executor::prefer( 102 | const execution::outstanding_work_t::tracked_t& p, int) const 103 | { 104 | return static_cast(*this).prefer(p); 105 | } 106 | 107 | template <> 108 | any_completion_executor any_completion_executor::prefer( 109 | const execution::outstanding_work_t::untracked_t& p, int) const 110 | { 111 | return static_cast(*this).prefer(p); 112 | } 113 | 114 | template <> 115 | any_completion_executor any_completion_executor::prefer( 116 | const execution::relationship_t::fork_t& p, int) const 117 | { 118 | return static_cast(*this).prefer(p); 119 | } 120 | 121 | template <> 122 | any_completion_executor any_completion_executor::prefer( 123 | const execution::relationship_t::continuation_t& p, int) const 124 | { 125 | return static_cast(*this).prefer(p); 126 | } 127 | 128 | } // namespace asio 129 | } // namespace boost 130 | 131 | #include "boost/asio/detail/pop_options.hpp" 132 | 133 | #endif // !defined(BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT) 134 | 135 | #endif // BOOST_ASIO_IMPL_ANY_COMPLETION_EXECUTOR_IPP 136 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/chits.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_CHITS_HPP 8 | #define CPPWAMP_CHITS_HPP 9 | 10 | //------------------------------------------------------------------------------ 11 | /** @file 12 | @brief Contains lightweight tokens representing pending requests. */ 13 | //------------------------------------------------------------------------------ 14 | 15 | #include 16 | #include "api.hpp" 17 | #include "wampdefs.hpp" 18 | #include "./internal/passkey.hpp" 19 | 20 | namespace wamp 21 | { 22 | 23 | // Forward declaration 24 | namespace internal { class Caller; } 25 | 26 | //------------------------------------------------------------------------------ 27 | /** Lightweight token representing a call request. */ 28 | //------------------------------------------------------------------------------ 29 | class CPPWAMP_API CallChit 30 | { 31 | public: 32 | /** Constructs an empty subscription */ 33 | CallChit(); 34 | 35 | /** Returns false if the chit is empty. */ 36 | explicit operator bool() const; 37 | 38 | /** Obtains the request ID associated with the call. */ 39 | RequestId requestId() const; 40 | 41 | /** Obtains the default cancel mode associated with the call. */ 42 | CallCancelMode cancelMode() const; 43 | 44 | /** Requests cancellation of the call using the cancel mode that 45 | was specified in the @ref wamp::Rpc "Rpc". */ 46 | void cancel() const; 47 | 48 | /** Requests cancellation of the call using the given mode. */ 49 | void cancel(CallCancelMode mode) const; 50 | 51 | private: 52 | using CallerPtr = std::weak_ptr; 53 | 54 | static constexpr RequestId invalidId_ = -1; 55 | CallerPtr caller_; 56 | RequestId reqId_ = invalidId_; 57 | CallCancelMode cancelMode_ = {}; 58 | 59 | public: 60 | // Internal use only 61 | CallChit(CallerPtr caller, RequestId reqId, CallCancelMode mode, 62 | internal::PassKey); 63 | 64 | }; 65 | 66 | } // namespace wamp 67 | 68 | #ifndef CPPWAMP_COMPILED_LIB 69 | #include "internal/chits.ipp" 70 | #endif 71 | 72 | #endif // CPPWAMP_CHITS_HPP 73 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/config.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_CONFIG_HPP 8 | #define CPPWAMP_CONFIG_HPP 9 | 10 | #ifdef _WIN32 11 | #define CPPWAMP_HAS_UNIX_DOMAIN_SOCKETS 0 12 | #else 13 | #define CPPWAMP_HAS_UNIX_DOMAIN_SOCKETS 1 14 | #endif 15 | 16 | #if defined(__cpp_inline_variables) || defined(CPPWAMP_FOR_DOXYGEN) 17 | #define CPPWAMP_INLINE_VARIABLE inline 18 | #else 19 | #define CPPWAMP_INLINE_VARIABLE 20 | #endif 21 | 22 | #if (defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard)) \ 23 | || defined(CPPWAMP_FOR_DOXYGEN) 24 | #define CPPWAMP_NODISCARD [[nodiscard]] 25 | #else 26 | #define CPPWAMP_NODISCARD 27 | #endif 28 | 29 | #if (defined(__has_cpp_attribute) && __has_cpp_attribute(deprecated)) \ 30 | || defined(CPPWAMP_FOR_DOXYGEN) 31 | #define CPPWAMP_DEPRECATED [[deprecated]] 32 | #else 33 | #define CPPWAMP_DEPRECATED 34 | #endif 35 | 36 | #endif // CPPWAMP_CONFIG_HPP 37 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/consolelogger.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_CONSOLELOGGER_HPP 8 | #define CPPWAMP_CONSOLELOGGER_HPP 9 | 10 | //------------------------------------------------------------------------------ 11 | /** @file 12 | @brief Contains the ConsoleLogger class. */ 13 | //------------------------------------------------------------------------------ 14 | 15 | #include 16 | #include "api.hpp" 17 | #include "logging.hpp" 18 | 19 | namespace wamp 20 | { 21 | 22 | //------------------------------------------------------------------------------ 23 | /** Outputs log entries to the console. 24 | The format is per wamp::toString(const LogEntry&). 25 | Entries below LogLevel::warning are output to std::clog, and all others 26 | are output to std::cerr. Concurrent output operations are not serialized. */ 27 | //------------------------------------------------------------------------------ 28 | class CPPWAMP_API ConsoleLogger 29 | { 30 | public: 31 | /** Default constructor. */ 32 | ConsoleLogger(); 33 | 34 | /** Constructor taking a custom origin label. */ 35 | explicit ConsoleLogger(std::string originLabel); 36 | 37 | /** Outputs the given log entry to the console. */ 38 | void operator()(LogEntry entry) const; 39 | 40 | private: 41 | std::string origin_; 42 | }; 43 | 44 | //------------------------------------------------------------------------------ 45 | /** Outputs log entries to the console using ANSI color escape codes that 46 | depend on severity. 47 | The format is per wamp::toString(const LogEntry&). 48 | Entries below LogLevel::warning are output to std::clog, and all others 49 | are output to std::cerr. Concurrent output operations are not serialized. */ 50 | //------------------------------------------------------------------------------ 51 | class CPPWAMP_API ColorConsoleLogger 52 | { 53 | public: 54 | /** Default constructor. */ 55 | ColorConsoleLogger(); 56 | 57 | /** Constructor taking a custom origin label. */ 58 | explicit ColorConsoleLogger(std::string originLabel); 59 | 60 | /** Outputs the given log entry to the console. */ 61 | void operator()(LogEntry entry) const; 62 | 63 | private: 64 | std::string origin_; 65 | }; 66 | 67 | } // namespace wamp 68 | 69 | #ifndef CPPWAMP_COMPILED_LIB 70 | #include "internal/consolelogger.ipp" 71 | #endif 72 | 73 | #endif // CPPWAMP_CONSOLELOGGER_HPP 74 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/conversion.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | (See accompanying file LICENSE_1_0.txt or copy at 5 | http://www.boost.org/LICENSE_1_0.txt) 6 | ------------------------------------------------------------------------------*/ 7 | 8 | #ifndef CPPWAMP_CONVERSION_HPP 9 | #define CPPWAMP_CONVERSION_HPP 10 | 11 | //------------------------------------------------------------------------------ 12 | /** @file 13 | @brief (Deprecated) Backward compatibility header. 14 | This header file is deprecated but retained for backward compatibility. 15 | Variant conversion facilities are now in variant.hpp */ 16 | //------------------------------------------------------------------------------ 17 | 18 | #include "variant.hpp" 19 | 20 | #endif // CPPWAMP_CONVERSION_HPP 21 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/coro/corounpacker.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | (See accompanying file LICENSE_1_0.txt or copy at 5 | http://www.boost.org/LICENSE_1_0.txt) 6 | ------------------------------------------------------------------------------*/ 7 | 8 | #ifndef CPPWAMP_CORO_COROUNPACKER_HPP 9 | #define CPPWAMP_CORO_COROUNPACKER_HPP 10 | 11 | //------------------------------------------------------------------------------ 12 | /** @file 13 | @brief Backward compatilibity header: use 14 | instead. */ 15 | //------------------------------------------------------------------------------ 16 | 17 | #include 18 | 19 | #endif // CPPWAMP_CORO_COROUNPACKER_HPP 20 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/corosession.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | (See accompanying file LICENSE_1_0.txt or copy at 5 | http://www.boost.org/LICENSE_1_0.txt) 6 | ------------------------------------------------------------------------------*/ 7 | 8 | #ifndef CPPWAMP_COROSESSION_HPP 9 | #define CPPWAMP_COROSESSION_HPP 10 | 11 | //------------------------------------------------------------------------------ 12 | /** @file 13 | @brief Convenience header which includes */ 14 | //------------------------------------------------------------------------------ 15 | 16 | #include 17 | 18 | #endif // CPPWAMP_COROSESSION_HPP 19 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/internal/blob.ipp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #include "../api.hpp" 8 | #include "../blob.hpp" 9 | #include "base64.hpp" 10 | 11 | namespace wamp 12 | { 13 | 14 | //------------------------------------------------------------------------------ 15 | CPPWAMP_INLINE Blob::Blob() {} 16 | 17 | //------------------------------------------------------------------------------ 18 | CPPWAMP_INLINE Blob::Blob(Data data) : data_(std::move(data)) {} 19 | 20 | //------------------------------------------------------------------------------ 21 | CPPWAMP_INLINE Blob::Blob(std::initializer_list list) : data_(list) {} 22 | 23 | //------------------------------------------------------------------------------ 24 | CPPWAMP_INLINE Blob::Data& Blob::data() {return data_;} 25 | 26 | //------------------------------------------------------------------------------ 27 | CPPWAMP_INLINE const Blob::Data& Blob::data() const {return data_;} 28 | 29 | //------------------------------------------------------------------------------ 30 | CPPWAMP_INLINE bool Blob::operator==(const Blob& other) const 31 | {return data_ == other.data_;} 32 | 33 | //------------------------------------------------------------------------------ 34 | CPPWAMP_INLINE bool Blob::operator!=(const Blob& other) const 35 | {return data_ != other.data_;} 36 | 37 | //------------------------------------------------------------------------------ 38 | CPPWAMP_INLINE bool Blob::operator<(const Blob& other) const 39 | {return data_ < other.data_;} 40 | 41 | //------------------------------------------------------------------------------ 42 | CPPWAMP_INLINE std::ostream& operator<<(std::ostream& out, const Blob& blob) 43 | { 44 | struct Sink 45 | { 46 | using value_type = char; 47 | std::ostream& os; 48 | void append(const value_type* data, std::size_t n) {os.write(data, n);} 49 | }; 50 | 51 | Sink sink{out}; 52 | internal::Base64::encode(blob.data().data(), blob.data().size(), sink); 53 | return out; 54 | } 55 | 56 | } // namespace wamp 57 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/internal/callee.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_INTERNAL_CALLEE_HPP 8 | #define CPPWAMP_INTERNAL_CALLEE_HPP 9 | 10 | #include 11 | #include 12 | #include "../anyhandler.hpp" 13 | #include "../erroror.hpp" 14 | #include "../peerdata.hpp" 15 | #include "../wampdefs.hpp" 16 | 17 | namespace wamp 18 | { 19 | 20 | class Registration; 21 | 22 | namespace internal 23 | { 24 | 25 | //------------------------------------------------------------------------------ 26 | class Callee 27 | { 28 | public: 29 | using WeakPtr = std::weak_ptr; 30 | 31 | virtual ~Callee() {} 32 | 33 | virtual void safeUnregister(const Registration&) = 0; 34 | 35 | virtual void safeUnregister( 36 | const Registration&, 37 | AnyCompletionHandler)>&& handler) = 0; 38 | 39 | virtual ErrorOrDone yield(RequestId, wamp::Result&&) = 0; 40 | 41 | virtual std::future safeYield(RequestId, wamp::Result&&) = 0; 42 | 43 | virtual ErrorOrDone yield(RequestId, wamp::Error&&) = 0; 44 | 45 | virtual std::future safeYield(RequestId, wamp::Error&&) = 0; 46 | }; 47 | 48 | } // namespace internal 49 | 50 | } // namespace wamp 51 | 52 | #endif // CPPWAMP_INTERNAL_CALLEE_HPP 53 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/internal/caller.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_INTERNAL_CALLER_HPP 8 | #define CPPWAMP_INTERNAL_CALLER_HPP 9 | 10 | #include 11 | #include 12 | #include "../erroror.hpp" 13 | #include "../wampdefs.hpp" 14 | 15 | namespace wamp 16 | { 17 | 18 | namespace internal 19 | { 20 | 21 | //------------------------------------------------------------------------------ 22 | class Caller 23 | { 24 | public: 25 | using WeakPtr = std::weak_ptr; 26 | 27 | virtual ~Caller() {} 28 | 29 | virtual std::future safeCancelCall(RequestId, 30 | CallCancelMode) = 0; 31 | }; 32 | 33 | } // namespace internal 34 | 35 | } // namespace wamp 36 | 37 | #endif // CPPWAMP_INTERNAL_CALLER_HPP 38 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/internal/cbor.ipp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #include "../cbor.hpp" 8 | #include 9 | #include 10 | #include "../api.hpp" 11 | #include "../traits.hpp" 12 | #include "variantdecoding.hpp" 13 | #include "variantencoding.hpp" 14 | 15 | namespace wamp 16 | { 17 | 18 | //------------------------------------------------------------------------------ 19 | template 20 | class SinkEncoder::Impl 21 | { 22 | public: 23 | void encode(const Variant& variant, TSink sink) 24 | { 25 | encoder_.encode(variant, sink); 26 | } 27 | 28 | private: 29 | struct Config 30 | { 31 | using Sink = TSink; 32 | 33 | template 34 | using EncoderType = 35 | jsoncons::cbor::basic_cbor_encoder; 36 | }; 37 | 38 | internal::GenericEncoder encoder_; 39 | }; 40 | 41 | //------------------------------------------------------------------------------ 42 | template 43 | SinkEncoder::SinkEncoder() : impl_(new Impl) {} 44 | 45 | //------------------------------------------------------------------------------ 46 | // Avoids incomplete type errors. 47 | //------------------------------------------------------------------------------ 48 | template 49 | SinkEncoder::~SinkEncoder() {} 50 | 51 | //------------------------------------------------------------------------------ 52 | template 53 | void SinkEncoder::encode(const Variant& variant, Sink sink) 54 | { 55 | impl_->encode(variant, sink); 56 | } 57 | 58 | 59 | //------------------------------------------------------------------------------ 60 | // Explicit template instantiations 61 | //------------------------------------------------------------------------------ 62 | #ifdef CPPWAMP_COMPILED_LIB 63 | template class SinkEncoder; 64 | template class SinkEncoder; 65 | template class SinkEncoder; 66 | #endif 67 | 68 | //------------------------------------------------------------------------------ 69 | template 70 | class SourceDecoder::Impl 71 | { 72 | public: 73 | Impl() : decoder_("Cbor") {} 74 | 75 | std::error_code decode(Source source, Variant& variant) 76 | { 77 | return decoder_.decode(source.input(), variant); 78 | } 79 | 80 | private: 81 | struct Config 82 | { 83 | using Source = TSource; 84 | 85 | template 86 | using Parser = jsoncons::cbor::basic_cbor_parser; 87 | }; 88 | 89 | internal::GenericDecoder decoder_; 90 | }; 91 | 92 | //------------------------------------------------------------------------------ 93 | template 94 | SourceDecoder::SourceDecoder() : impl_(new Impl) {} 95 | 96 | //------------------------------------------------------------------------------ 97 | // Avoids incomplete type errors. 98 | //------------------------------------------------------------------------------ 99 | template 100 | SourceDecoder::~SourceDecoder() {} 101 | 102 | //------------------------------------------------------------------------------ 103 | template 104 | std::error_code SourceDecoder::decode(Source source, 105 | Variant& variant) 106 | { 107 | return impl_->decode(source, variant); 108 | } 109 | 110 | //------------------------------------------------------------------------------ 111 | // Explicit template instantiations 112 | //------------------------------------------------------------------------------ 113 | #ifdef CPPWAMP_COMPILED_LIB 114 | template class SourceDecoder; 115 | template class SourceDecoder; 116 | template class SourceDecoder; 117 | #endif 118 | 119 | } // namespace wamp 120 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/internal/challengee.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_INTERNAL_CHALLENGEE_HPP 8 | #define CPPWAMP_INTERNAL_CHALLENGEE_HPP 9 | 10 | #include 11 | #include 12 | #include "../erroror.hpp" 13 | 14 | namespace wamp 15 | { 16 | 17 | class Authentication; 18 | 19 | namespace internal 20 | { 21 | 22 | //------------------------------------------------------------------------------ 23 | class Challengee 24 | { 25 | public: 26 | using WeakPtr = std::weak_ptr; 27 | 28 | virtual ~Challengee() {} 29 | 30 | virtual ErrorOrDone authenticate(Authentication&&) = 0; 31 | 32 | virtual std::future safeAuthenticate(Authentication&&) = 0; 33 | }; 34 | 35 | } // namespace internal 36 | 37 | } // namespace wamp 38 | 39 | #endif // CPPWAMP_INTERNAL_CHALLENGEE_HPP 40 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/internal/chits.ipp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #include "../chits.hpp" 8 | #include "caller.hpp" 9 | 10 | namespace wamp 11 | { 12 | 13 | //------------------------------------------------------------------------------ 14 | CPPWAMP_INLINE CallChit::CallChit() {} 15 | 16 | //------------------------------------------------------------------------------ 17 | CPPWAMP_INLINE CallChit::operator bool() const {return reqId_ != invalidId_;} 18 | 19 | //------------------------------------------------------------------------------ 20 | CPPWAMP_INLINE RequestId CallChit::requestId() const {return reqId_;} 21 | 22 | //------------------------------------------------------------------------------ 23 | CPPWAMP_INLINE CallCancelMode CallChit::cancelMode() const {return cancelMode_;} 24 | 25 | //------------------------------------------------------------------------------ 26 | CPPWAMP_INLINE void CallChit::cancel() const 27 | { 28 | cancel(cancelMode_); 29 | } 30 | 31 | //------------------------------------------------------------------------------ 32 | CPPWAMP_INLINE void CallChit::cancel(CallCancelMode mode) const 33 | { 34 | auto caller = caller_.lock(); 35 | if (caller) 36 | caller->safeCancelCall(reqId_, mode); 37 | } 38 | 39 | //------------------------------------------------------------------------------ 40 | CPPWAMP_INLINE CallChit::CallChit(CallerPtr caller, RequestId reqId, 41 | CallCancelMode mode, internal::PassKey) 42 | : caller_(caller), 43 | reqId_(reqId), 44 | cancelMode_(mode) 45 | {} 46 | 47 | } // namespace wamp 48 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/internal/connector.ipp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #include "../connector.hpp" 8 | 9 | namespace wamp 10 | { 11 | 12 | //------------------------------------------------------------------------------ 13 | CPPWAMP_INLINE Connecting::Ptr ConnectorBuilder::operator()(IoStrand s, 14 | int codecId) const 15 | { 16 | return builder_(std::move(s), codecId); 17 | } 18 | 19 | //------------------------------------------------------------------------------ 20 | CPPWAMP_INLINE const AnyIoExecutor& LegacyConnector::executor() const 21 | { 22 | return exec_; 23 | } 24 | 25 | CPPWAMP_INLINE const ConnectorBuilder& LegacyConnector::connectorBuilder() const 26 | { 27 | return connectorBuilder_; 28 | } 29 | 30 | CPPWAMP_INLINE const BufferCodecBuilder& LegacyConnector::codecBuilder() const 31 | { 32 | return codecBuilder_; 33 | } 34 | 35 | //------------------------------------------------------------------------------ 36 | CPPWAMP_INLINE ConnectionWish::ConnectionWish(const LegacyConnector& c) 37 | : connectorBuilder_(c.connectorBuilder()), 38 | codecBuilder_(c.codecBuilder()) 39 | {} 40 | 41 | CPPWAMP_INLINE int ConnectionWish::codecId() const {return codecBuilder_.id();} 42 | 43 | CPPWAMP_INLINE Connecting::Ptr ConnectionWish::makeConnector(IoStrand s) const 44 | { 45 | return connectorBuilder_(std::move(s), codecId()); 46 | } 47 | 48 | CPPWAMP_INLINE AnyBufferCodec ConnectionWish::makeCodec() const 49 | { 50 | return codecBuilder_(); 51 | } 52 | 53 | } // namespace wamp 54 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/internal/consolelogger.ipp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #include "../consolelogger.hpp" 8 | #include "../api.hpp" 9 | 10 | namespace wamp 11 | { 12 | 13 | //------------------------------------------------------------------------------ 14 | CPPWAMP_INLINE ConsoleLogger::ConsoleLogger() 15 | : origin_("cppwamp") 16 | {} 17 | 18 | CPPWAMP_INLINE ConsoleLogger::ConsoleLogger(std::string originLabel) 19 | : origin_(std::move(originLabel)) 20 | {} 21 | 22 | CPPWAMP_INLINE void ConsoleLogger::operator()(LogEntry entry) const 23 | { 24 | if (entry.severity() < LogLevel::warning) 25 | toStream(std::clog, entry, origin_) << "\n"; 26 | else 27 | toStream(std::cerr, entry, origin_) << std::endl; 28 | } 29 | 30 | //------------------------------------------------------------------------------ 31 | CPPWAMP_INLINE ColorConsoleLogger::ColorConsoleLogger() 32 | : origin_("cppwamp") 33 | {} 34 | 35 | CPPWAMP_INLINE ColorConsoleLogger::ColorConsoleLogger(std::string originLabel) 36 | : origin_(std::move(originLabel)) 37 | {} 38 | 39 | CPPWAMP_INLINE void ColorConsoleLogger::operator()(LogEntry entry) const 40 | { 41 | if (entry.severity() < LogLevel::warning) 42 | toColorStream(std::clog, entry, origin_) << "\n"; 43 | else 44 | toColorStream(std::cerr, entry, origin_) << std::endl; 45 | } 46 | 47 | } // namespace wamp 48 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/internal/endian.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_ENDIAN_HPP 8 | #define CPPWAMP_ENDIAN_HPP 9 | 10 | #include 11 | 12 | #if defined(__has_include) && __has_include() 13 | #include 14 | #ifdef __cpp_lib_endian 15 | #define CPPWAMP_HAS_STD_ENDIAN 16 | #endif 17 | #endif 18 | 19 | namespace wamp 20 | { 21 | 22 | namespace internal 23 | { 24 | 25 | namespace endian 26 | { 27 | 28 | inline uint32_t flip(uint32_t n) 29 | { 30 | // This usually optimizes to a single byte swap instruction. 31 | return ((n & 0xFF000000u) >> 24u) | ((n & 0x00FF0000u) >> 8u) | 32 | ((n & 0x0000FF00u) << 8u) | ((n & 0x0000000FF) << 24u); 33 | } 34 | 35 | constexpr bool nativeIsLittle() 36 | { 37 | #ifdef CPPWAMP_HAS_STD_ENDIAN 38 | return (std::endian::native == std::endian::little); 39 | #elif defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) 40 | return __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__; 41 | #elif defined(_WIN32) 42 | return true; 43 | #elif defined(CPPWAMP_ASSUME_LITTLE_ENDIAN) 44 | return true; 45 | #elif defined(CPPWAMP_ASSUME_BIG_ENDIAN) 46 | return false; 47 | #else 48 | #warning Cannot detect endianness; assuming little endian 49 | #warning Please define either CPPWAMP_ASSUME_LITTLE_ENDIAN or CPPWAMP_ASSUME_BIG_ENDIAN 50 | return little_endian; 51 | #endif 52 | } 53 | 54 | inline uint32_t nativeToBig32(uint32_t native) 55 | { 56 | return nativeIsLittle() ? flip(native) : native; 57 | } 58 | 59 | inline uint32_t bigToNative32(uint32_t big) 60 | { 61 | return nativeIsLittle() ? flip(big) : big; 62 | } 63 | 64 | 65 | } // namespace endian 66 | 67 | } // namespace internal 68 | 69 | } // namespace wamp 70 | 71 | 72 | #endif // CPPWAMP_ENDIAN_HPP 73 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/internal/integersequence.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_INTEGERSEQUENCE_HPP 8 | #define CPPWAMP_INTEGERSEQUENCE_HPP 9 | 10 | #ifndef CPPWAMP_FOR_DOXYGEN // GenIntegerSequence confuses the Doxygen parser 11 | 12 | namespace wamp 13 | { 14 | 15 | namespace internal 16 | { 17 | 18 | // Represents a compile-time sequence of integers. 19 | template struct IntegerSequence { }; 20 | 21 | 22 | // Generates a compile-time sequence of integers. 23 | template 24 | struct GenIntegerSequence : GenIntegerSequence { }; 25 | 26 | template 27 | struct GenIntegerSequence<0, S...> 28 | { 29 | using type = IntegerSequence; 30 | }; 31 | 32 | } // namespace internal 33 | 34 | } // namespace wamp 35 | 36 | #endif // CPPWAMP_FOR_DOXYGEN 37 | 38 | #endif // CPPWAMP_INTEGERSEQUENCE_HPP 39 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/internal/messagetraits.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_MESSSAGE_TRAITS_HPP 8 | #define CPPWAMP_MESSSAGE_TRAITS_HPP 9 | 10 | #include 11 | #include "../api.hpp" 12 | #include "../wampdefs.hpp" 13 | #include "../variantdefs.hpp" 14 | 15 | namespace wamp 16 | { 17 | 18 | namespace internal 19 | { 20 | 21 | //------------------------------------------------------------------------------ 22 | enum class WampMsgType : uint8_t 23 | { 24 | none = 0, 25 | hello = 1, 26 | welcome = 2, 27 | abort = 3, 28 | challenge = 4, 29 | authenticate = 5, 30 | goodbye = 6, 31 | error = 8, 32 | publish = 16, 33 | published = 17, 34 | subscribe = 32, 35 | subscribed = 33, 36 | unsubscribe = 34, 37 | unsubscribed = 35, 38 | event = 36, 39 | call = 48, 40 | cancel = 49, 41 | result = 50, 42 | enroll = 64, 43 | registered = 65, 44 | unregister = 66, 45 | unregistered = 67, 46 | invocation = 68, 47 | interrupt = 69, 48 | yield = 70 49 | }; 50 | 51 | //------------------------------------------------------------------------------ 52 | struct CPPWAMP_API MessageTraits 53 | { 54 | // CPPWAMP_API visibility required by codec component libraries 55 | 56 | static const MessageTraits& lookup(WampMsgType type); 57 | 58 | bool isValidType() const; 59 | 60 | bool isValidRx(SessionState s, bool isRouter) const; 61 | 62 | const char* nameOr(const char* fallback) const; 63 | 64 | const char* name; 65 | WampMsgType repliesTo : 8; 66 | size_t idPosition : 8; 67 | size_t minSize : 8; 68 | size_t maxSize : 8; 69 | bool isClientRx : 1; 70 | bool isRouterRx : 1; 71 | bool forEstablishing : 1; 72 | bool forChallenging : 1; 73 | bool forEstablished : 1; 74 | TypeId fieldTypes[7]; 75 | }; 76 | 77 | } // namespace internal 78 | 79 | } // namespace wamp 80 | 81 | #ifndef CPPWAMP_COMPILED_LIB 82 | #include "messagetraits.ipp" 83 | #endif 84 | 85 | #endif // CPPWAMP_MESSSAGE_TRAITS_HPP 86 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/internal/passkey.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_PASSKEY_HPP 8 | #define CPPWAMP_PASSKEY_HPP 9 | 10 | namespace wamp 11 | { 12 | 13 | class Session; 14 | 15 | namespace internal 16 | { 17 | class PassKey 18 | { 19 | PassKey() {} 20 | 21 | friend class Peer; 22 | friend class Client; 23 | friend class wamp::Session; 24 | }; 25 | } 26 | 27 | } 28 | 29 | #endif // CPPWAMP_PASSKEY_HPP 30 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/internal/rawsockhandshake.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_INTERNAL_RAWSOCKHANDSHAKE_HPP 8 | #define CPPWAMP_INTERNAL_RAWSOCKHANDSHAKE_HPP 9 | 10 | #include 11 | #include 12 | #include 13 | #include "../error.hpp" 14 | #include "../rawsockoptions.hpp" 15 | #include "endian.hpp" 16 | 17 | namespace wamp 18 | { 19 | 20 | namespace internal 21 | { 22 | 23 | //------------------------------------------------------------------------------ 24 | class RawsockHandshake 25 | { 26 | public: 27 | static size_t byteLengthOf(RawsockMaxLength len) 28 | {return 1u << ((int)len + 9);} 29 | 30 | static RawsockHandshake fromBigEndian(uint32_t big) 31 | {return RawsockHandshake(endian::bigToNative32(big));} 32 | 33 | static RawsockHandshake eUnsupportedFormat() 34 | {return RawsockHandshake(0x7f100000);} 35 | 36 | static RawsockHandshake eUnacceptableLength() 37 | {return RawsockHandshake(0x7f200000);} 38 | 39 | static RawsockHandshake eReservedBitsUsed() 40 | {return RawsockHandshake(0x7f300000);} 41 | 42 | static RawsockHandshake eMaxConnections() 43 | {return RawsockHandshake(0x7f400000);} 44 | 45 | RawsockHandshake() : hs_(magicOctet_) {} 46 | 47 | explicit RawsockHandshake(uint32_t hostOrder) : hs_(hostOrder) {} 48 | 49 | uint16_t reserved() const {return get(reservedMask_);} 50 | 51 | int codecId() const 52 | {return get(codecMask_, codecPos_);} 53 | 54 | RawsockMaxLength maxLength() const 55 | {return get(lengthMask_, lengthPos_);} 56 | 57 | size_t maxLengthInBytes() const {return byteLengthOf(maxLength());} 58 | 59 | bool hasError() const {return get<>(codecMask_) == 0;} 60 | 61 | RawsockErrc errorCode() const 62 | { 63 | return get(errorMask_, errorPos_); 64 | } 65 | 66 | bool hasMagicOctet() const {return get<>(magicMask_) == magicOctet_;} 67 | 68 | uint32_t toBigEndian() const {return endian::nativeToBig32(hs_);} 69 | 70 | uint32_t toHostOrder() const {return hs_;} 71 | 72 | RawsockHandshake& setCodecId(int codecId) 73 | {return put(codecId, codecPos_);} 74 | 75 | RawsockHandshake& setMaxLength(RawsockMaxLength length) 76 | {return put(length, lengthPos_);} 77 | 78 | private: 79 | static constexpr uint32_t reservedMask_ = 0x0000ffff; 80 | static constexpr uint32_t codecMask_ = 0x000f0000; 81 | static constexpr uint32_t lengthMask_ = 0x00f00000; 82 | static constexpr uint32_t errorMask_ = 0x00f00000; 83 | static constexpr uint32_t magicMask_ = 0xff000000; 84 | static constexpr uint32_t magicOctet_ = 0x7f000000; 85 | static constexpr int byteLengthPos_ = 9; 86 | static constexpr int codecPos_ = 16; 87 | static constexpr int lengthPos_ = 20; 88 | static constexpr int errorPos_ = 20; 89 | 90 | template 91 | T get(uint32_t mask, int pos = 0) const 92 | {return static_cast((hs_ & mask) >> pos);} 93 | 94 | template 95 | RawsockHandshake& put(T value, int pos = 0) 96 | {hs_ |= (static_cast(value) << pos); return *this;} 97 | 98 | uint32_t hs_; 99 | }; 100 | 101 | } // namespace internal 102 | 103 | } // namespace wamp 104 | 105 | #endif // CPPWAMP_INTERNAL_RAWSOCKHANDSHAKE_HPP 106 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/internal/rawsockheader.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_INTERNAL_RAWSOCKHEADER_HPP 8 | #define CPPWAMP_INTERNAL_RAWSOCKHEADER_HPP 9 | 10 | #include 11 | #include "endian.hpp" 12 | 13 | namespace wamp 14 | { 15 | 16 | namespace internal 17 | { 18 | 19 | //------------------------------------------------------------------------------ 20 | enum class RawsockMsgType 21 | { 22 | wamp, 23 | ping, 24 | pong 25 | }; 26 | 27 | //------------------------------------------------------------------------------ 28 | class RawsockHeader 29 | { 30 | public: 31 | static RawsockHeader fromBigEndian(uint32_t big) 32 | {return RawsockHeader(endian::bigToNative32(big));} 33 | 34 | RawsockHeader() : hdr_(0) {} 35 | 36 | explicit RawsockHeader(uint32_t hostOrder) : hdr_(hostOrder) {} 37 | 38 | bool msgTypeIsValid() const 39 | { 40 | auto type = msgType(); 41 | return type == RawsockMsgType::wamp || 42 | type == RawsockMsgType::ping || 43 | type == RawsockMsgType::pong; 44 | } 45 | 46 | RawsockMsgType msgType() const 47 | { 48 | return get(msgTypeMask_, msgTypePos_); 49 | } 50 | 51 | size_t length() const {return get(lengthMask_);} 52 | 53 | uint32_t toBigEndian() const {return endian::nativeToBig32(hdr_);} 54 | 55 | uint32_t toHostOrder() const {return hdr_;} 56 | 57 | RawsockHeader& setMsgType(RawsockMsgType msgType) 58 | {return put(msgType, msgTypePos_);} 59 | 60 | RawsockHeader& setLength(size_t length) 61 | {return put(length, lengthPos_);} 62 | 63 | private: 64 | static constexpr uint32_t msgTypeMask_ = 0xff000000; 65 | static constexpr uint32_t lengthMask_ = 0x00ffffff; 66 | static constexpr int msgTypePos_ = 24; 67 | static constexpr int lengthPos_ = 0; 68 | 69 | template 70 | T get(uint32_t mask, int pos = 0) const 71 | {return static_cast((hdr_ & mask) >> pos);} 72 | 73 | template 74 | RawsockHeader& put(T value, int pos = 0) 75 | {hdr_ |= (static_cast(value) << pos); return *this;} 76 | 77 | uint32_t hdr_; 78 | }; 79 | 80 | } // namespace internal 81 | 82 | } // namespace wamp 83 | 84 | #endif // CPPWAMP_INTERNAL_RAWSOCKHEADER_HPP 85 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/internal/registration.ipp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #include "../registration.hpp" 8 | #include 9 | #include "callee.hpp" 10 | #include "../api.hpp" 11 | 12 | namespace wamp 13 | { 14 | 15 | /******************************************************************************* 16 | * Registration 17 | *******************************************************************************/ 18 | 19 | /** @post `!!(*this) == false` */ 20 | CPPWAMP_INLINE Registration::Registration() {} 21 | 22 | CPPWAMP_INLINE Registration::Registration(const Registration& other) 23 | : callee_(other.callee_), 24 | id_(other.id_) 25 | {} 26 | 27 | /** @post `!other == true` */ 28 | CPPWAMP_INLINE Registration::Registration(Registration&& other) noexcept 29 | : callee_(other.callee_), 30 | id_(other.id_) 31 | { 32 | other.callee_.reset(); 33 | other.id_ = invalidId_; 34 | } 35 | 36 | CPPWAMP_INLINE Registration::operator bool() const {return id_ != invalidId_;} 37 | 38 | CPPWAMP_INLINE RegistrationId Registration::id() const {return id_;} 39 | 40 | CPPWAMP_INLINE Registration& Registration::operator=(const Registration& other) 41 | { 42 | callee_ = other.callee_; 43 | id_ = other.id_; 44 | return *this; 45 | } 46 | 47 | /** @post `!other == true` */ 48 | CPPWAMP_INLINE Registration& 49 | Registration::operator=(Registration&& other) noexcept 50 | { 51 | callee_ = other.callee_; 52 | id_ = other.id_; 53 | other.callee_.reset(); 54 | other.id_ = invalidId_; 55 | return *this; 56 | } 57 | 58 | CPPWAMP_INLINE void Registration::unregister() const 59 | { 60 | auto callee = callee_.lock(); 61 | if (callee) 62 | callee->safeUnregister(*this); 63 | } 64 | 65 | CPPWAMP_INLINE Registration::Registration(CalleePtr callee, RegistrationId id, 66 | internal::PassKey) 67 | : callee_(callee), 68 | id_(id) 69 | {} 70 | 71 | 72 | /******************************************************************************* 73 | ScopedRegistration 74 | *******************************************************************************/ 75 | 76 | //------------------------------------------------------------------------------ 77 | CPPWAMP_INLINE ScopedRegistration::ScopedRegistration() {} 78 | 79 | //------------------------------------------------------------------------------ 80 | CPPWAMP_INLINE 81 | ScopedRegistration::ScopedRegistration(ScopedRegistration&& other) noexcept 82 | : Base(std::move(other)) 83 | {} 84 | 85 | //------------------------------------------------------------------------------ 86 | CPPWAMP_INLINE 87 | ScopedRegistration::ScopedRegistration(Registration registration) 88 | : Base(std::move(registration)) 89 | {} 90 | 91 | //------------------------------------------------------------------------------ 92 | CPPWAMP_INLINE ScopedRegistration::~ScopedRegistration() 93 | { 94 | unregister(); 95 | } 96 | 97 | //------------------------------------------------------------------------------ 98 | CPPWAMP_INLINE ScopedRegistration& 99 | ScopedRegistration::operator=(ScopedRegistration&& other) noexcept 100 | { 101 | unregister(); 102 | Base::operator=(std::move(other)); 103 | return *this; 104 | } 105 | 106 | //------------------------------------------------------------------------------ 107 | CPPWAMP_INLINE ScopedRegistration& 108 | ScopedRegistration::operator=(Registration subscription) 109 | { 110 | unregister(); 111 | Base::operator=(std::move(subscription)); 112 | return *this; 113 | } 114 | 115 | //------------------------------------------------------------------------------ 116 | CPPWAMP_INLINE void ScopedRegistration::release() 117 | { 118 | Base::operator=(Registration()); 119 | } 120 | } // namespace wamp 121 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/internal/subscriber.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_INTERNAL_SUBSCRIBER_HPP 8 | #define CPPWAMP_INTERNAL_SUBSCRIBER_HPP 9 | 10 | #include 11 | #include "../anyhandler.hpp" 12 | #include "../erroror.hpp" 13 | #include "../subscription.hpp" 14 | 15 | namespace wamp 16 | { 17 | 18 | class Subscription; 19 | 20 | namespace internal 21 | { 22 | 23 | //------------------------------------------------------------------------------ 24 | class Subscriber 25 | { 26 | public: 27 | using WeakPtr = std::weak_ptr; 28 | 29 | virtual ~Subscriber() {} 30 | 31 | virtual void safeUnsubscribe(const Subscription&) = 0; 32 | 33 | virtual void safeUnsubscribe( 34 | const Subscription&, AnyCompletionHandler)>&&) = 0; 35 | }; 36 | 37 | } // namespace internal 38 | 39 | } // namespace wamp 40 | 41 | #endif // CPPWAMP_INTERNAL_SUBSCRIBER_HPP 42 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/internal/subscription.ipp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #include "../subscription.hpp" 8 | #include 9 | #include "../api.hpp" 10 | #include "subscriber.hpp" 11 | 12 | namespace wamp 13 | { 14 | 15 | /******************************************************************************* 16 | * Subscription 17 | *******************************************************************************/ 18 | 19 | /** @post `!!(*this) == false` */ 20 | CPPWAMP_INLINE Subscription::Subscription() {} 21 | 22 | CPPWAMP_INLINE Subscription::Subscription(const Subscription& other) 23 | : subscriber_(other.subscriber_), 24 | subId_(other.subId_), 25 | slotId_(other.slotId_) 26 | {} 27 | 28 | /** @post `!other == true` */ 29 | CPPWAMP_INLINE Subscription::Subscription(Subscription&& other) noexcept 30 | : subscriber_(other.subscriber_), 31 | subId_(other.subId_), 32 | slotId_(other.slotId_) 33 | { 34 | other.subscriber_.reset(); 35 | other.subId_ = invalidId_; 36 | other.slotId_ = invalidId_; 37 | } 38 | 39 | CPPWAMP_INLINE Subscription::operator bool() const 40 | { 41 | return subId_ != invalidId_; 42 | } 43 | 44 | CPPWAMP_INLINE SubscriptionId Subscription::id() const {return subId_;} 45 | 46 | CPPWAMP_INLINE Subscription& Subscription::operator=(const Subscription& other) 47 | { 48 | subscriber_ = other.subscriber_; 49 | subId_ = other.subId_; 50 | slotId_ = other.slotId_; 51 | return *this; 52 | } 53 | 54 | /** @post `!other == true` */ 55 | CPPWAMP_INLINE Subscription& 56 | Subscription::operator=(Subscription&& other) noexcept 57 | { 58 | subscriber_ = other.subscriber_; 59 | subId_ = other.subId_; 60 | slotId_ = other.slotId_; 61 | other.subscriber_.reset(); 62 | other.subId_ = invalidId_; 63 | other.slotId_ = invalidId_; 64 | return *this; 65 | } 66 | 67 | CPPWAMP_INLINE void Subscription::unsubscribe() const 68 | { 69 | auto subscriber = subscriber_.lock(); 70 | if (subscriber) 71 | subscriber->safeUnsubscribe(*this); 72 | } 73 | 74 | CPPWAMP_INLINE Subscription::Subscription(SubscriberPtr subscriber, 75 | SubscriptionId subId, SlotId slotId, internal::PassKey) 76 | : subscriber_(subscriber), 77 | subId_(subId), 78 | slotId_(slotId) 79 | {} 80 | 81 | CPPWAMP_INLINE Subscription::SlotId 82 | Subscription::slotId(internal::PassKey) const {return slotId_;} 83 | 84 | 85 | /******************************************************************************* 86 | * ScopedSubscription 87 | *******************************************************************************/ 88 | 89 | CPPWAMP_INLINE ScopedSubscription::ScopedSubscription() {} 90 | 91 | CPPWAMP_INLINE 92 | ScopedSubscription::ScopedSubscription(ScopedSubscription&& other) noexcept 93 | : Base(std::move(other)) 94 | {} 95 | 96 | CPPWAMP_INLINE 97 | ScopedSubscription::ScopedSubscription(Subscription subscription) 98 | : Base(std::move(subscription)) 99 | {} 100 | 101 | CPPWAMP_INLINE ScopedSubscription::~ScopedSubscription() 102 | { 103 | unsubscribe(); 104 | } 105 | 106 | CPPWAMP_INLINE ScopedSubscription& 107 | ScopedSubscription::operator=(ScopedSubscription&& other) noexcept 108 | { 109 | unsubscribe(); 110 | Base::operator=(std::move(other)); 111 | return *this; 112 | } 113 | 114 | CPPWAMP_INLINE ScopedSubscription& 115 | ScopedSubscription::operator=(Subscription subscription) 116 | { 117 | unsubscribe(); 118 | Base::operator=(std::move(subscription)); 119 | return *this; 120 | } 121 | 122 | CPPWAMP_INLINE void ScopedSubscription::release() 123 | { 124 | Base::operator=(Subscription()); 125 | } 126 | 127 | } // namespace wamp 128 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/internal/tcp.ipp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #include "../tcp.hpp" 8 | #include "rawsockconnector.hpp" 9 | #include "tcpopener.hpp" 10 | 11 | namespace wamp 12 | { 13 | 14 | //------------------------------------------------------------------------------ 15 | struct Connector::Impl 16 | { 17 | using RawsockOpener = internal::RawsockConnector; 18 | 19 | Impl(IoStrand i, Settings s, int codecId) 20 | : cnct(RawsockOpener::create(std::move(i), std::move(s), codecId)) 21 | {} 22 | 23 | RawsockOpener::Ptr cnct; 24 | }; 25 | 26 | //------------------------------------------------------------------------------ 27 | CPPWAMP_INLINE Connector::Connector(IoStrand i, Settings s, int codecId) 28 | : impl_(new Impl(std::move(i), std::move(s), codecId)) 29 | {} 30 | 31 | //------------------------------------------------------------------------------ 32 | // Needed to avoid incomplete type errors. 33 | //------------------------------------------------------------------------------ 34 | CPPWAMP_INLINE Connector::~Connector() {} 35 | 36 | //------------------------------------------------------------------------------ 37 | CPPWAMP_INLINE void Connector::establish(Handler&& handler) 38 | { 39 | impl_->cnct->establish(std::move(handler)); 40 | } 41 | 42 | //------------------------------------------------------------------------------ 43 | CPPWAMP_INLINE void Connector::cancel() {impl_->cnct->cancel();} 44 | 45 | } // namespace wamp 46 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/internal/tcpacceptor.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_INTERNAL_TCPACCEPTOR_HPP 8 | #define CPPWAMP_INTERNAL_TCPACCEPTOR_HPP 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include "../asiodefs.hpp" 16 | #include "../erroror.hpp" 17 | #include "../tcpendpoint.hpp" 18 | 19 | namespace wamp 20 | { 21 | 22 | namespace internal 23 | { 24 | 25 | //------------------------------------------------------------------------------ 26 | class TcpAcceptor 27 | { 28 | public: 29 | using Settings = TcpEndpoint; 30 | using Socket = boost::asio::ip::tcp::socket; 31 | using SocketPtr = std::unique_ptr; 32 | 33 | template 34 | TcpAcceptor(TExecutorOrStrand&& exec, const Settings& s) 35 | : strand_(std::forward(exec)), 36 | acceptor_(strand_, makeEndpoint(s)) 37 | {} 38 | 39 | template 40 | void establish(F&& callback) 41 | { 42 | struct Accepted 43 | { 44 | TcpAcceptor* self; 45 | typename std::decay::type callback; 46 | 47 | void operator()(boost::system::error_code asioEc) 48 | { 49 | SocketPtr socket{std::move(self->socket_)}; 50 | self->socket_.reset(); 51 | if (self->checkError(asioEc, callback)) 52 | callback(std::move(socket)); 53 | } 54 | }; 55 | 56 | assert(!socket_ && "Accept already in progress"); 57 | 58 | socket_.reset(new Socket(strand_)); 59 | 60 | // RawsockListener will keep this object alive until completion. 61 | acceptor_.async_accept(*socket_, 62 | Accepted{this, std::forward(callback)}); 63 | } 64 | 65 | void cancel() 66 | { 67 | acceptor_.cancel(); 68 | } 69 | 70 | private: 71 | static boost::asio::ip::tcp::endpoint makeEndpoint(const Settings& s) 72 | { 73 | if (!s.address().empty()) 74 | return {boost::asio::ip::make_address(s.address()), s.port()}; 75 | else 76 | return {boost::asio::ip::tcp::v4(), s.port()}; 77 | } 78 | 79 | template 80 | bool checkError(boost::system::error_code asioEc, F& callback) 81 | { 82 | if (asioEc) 83 | { 84 | auto ec = make_error_code(static_cast(asioEc.value())); 85 | callback(UnexpectedError(ec)); 86 | } 87 | return !asioEc; 88 | } 89 | 90 | IoStrand strand_; 91 | boost::asio::ip::tcp::acceptor acceptor_; 92 | SocketPtr socket_; 93 | }; 94 | 95 | } // namespace internal 96 | 97 | } // namespace wamp 98 | 99 | 100 | #endif // CPPWAMP_INTERNAL_TCPACCEPTOR_HPP 101 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/internal/tcpendpoint.ipp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #include "../tcpendpoint.hpp" 8 | #include 9 | #include "../api.hpp" 10 | 11 | namespace wamp 12 | { 13 | 14 | CPPWAMP_INLINE TcpEndpoint::TcpEndpoint(unsigned short port, TcpOptions options, 15 | RawsockMaxLength maxRxLength) 16 | : options_(std::move(options)), 17 | maxRxLength_(maxRxLength), 18 | port_(port) 19 | {} 20 | 21 | CPPWAMP_INLINE TcpEndpoint::TcpEndpoint(std::string address, 22 | unsigned short port, TcpOptions options, 23 | RawsockMaxLength maxRxLength) 24 | : address_(std::move(address)), 25 | options_(std::move(options)), 26 | maxRxLength_(maxRxLength), 27 | port_(port) 28 | {} 29 | 30 | CPPWAMP_INLINE TcpEndpoint& TcpEndpoint::withOptions(TcpOptions options) 31 | { 32 | options_ = std::move(options); 33 | return *this; 34 | } 35 | 36 | CPPWAMP_INLINE TcpEndpoint& TcpEndpoint::withMaxRxLength(RawsockMaxLength length) 37 | { 38 | maxRxLength_ = length; 39 | return *this; 40 | } 41 | 42 | CPPWAMP_INLINE const std::string& TcpEndpoint::address() const 43 | { 44 | return address_; 45 | } 46 | 47 | CPPWAMP_INLINE unsigned short TcpEndpoint::port() const 48 | { 49 | return port_; 50 | } 51 | 52 | CPPWAMP_INLINE const TcpOptions& TcpEndpoint::options() const {return options_;} 53 | 54 | CPPWAMP_INLINE RawsockMaxLength TcpEndpoint::maxRxLength() const 55 | { 56 | return maxRxLength_; 57 | } 58 | 59 | } // namespace wamp 60 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/internal/tcphost.ipp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #include "../tcphost.hpp" 8 | #include 9 | #include "../api.hpp" 10 | 11 | namespace wamp 12 | { 13 | 14 | CPPWAMP_INLINE TcpHost::TcpHost(std::string hostName, std::string serviceName, 15 | TcpOptions options, 16 | RawsockMaxLength maxRxLength) 17 | : hostName_(std::move(hostName)), 18 | serviceName_(std::move(serviceName)), 19 | options_(std::move(options)), 20 | maxRxLength_(maxRxLength) 21 | {} 22 | 23 | CPPWAMP_INLINE TcpHost::TcpHost(std::string hostName, unsigned short port, 24 | TcpOptions options, 25 | RawsockMaxLength maxRxLength) 26 | : hostName_(std::move(hostName)), 27 | serviceName_(std::to_string(port)), 28 | options_(std::move(options)), 29 | maxRxLength_(maxRxLength) 30 | {} 31 | 32 | CPPWAMP_INLINE TcpHost& TcpHost::withOptions(TcpOptions options) 33 | { 34 | options_ = std::move(options); 35 | return *this; 36 | } 37 | 38 | CPPWAMP_INLINE TcpHost& TcpHost::withMaxRxLength(RawsockMaxLength length) 39 | { 40 | maxRxLength_ = length; 41 | return *this; 42 | } 43 | 44 | CPPWAMP_INLINE const std::string& TcpHost::hostName() const 45 | { 46 | return hostName_; 47 | } 48 | 49 | CPPWAMP_INLINE const std::string& TcpHost::serviceName() const 50 | { 51 | return serviceName_; 52 | } 53 | 54 | CPPWAMP_INLINE const TcpOptions& TcpHost::options() const {return options_;} 55 | 56 | CPPWAMP_INLINE RawsockMaxLength TcpHost::maxRxLength() const 57 | { 58 | return maxRxLength_; 59 | } 60 | 61 | } // namespace wamp 62 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/internal/tcpopener.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_INTERNAL_TCPOPENER_HPP 8 | #define CPPWAMP_INTERNAL_TCPOPENER_HPP 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include "../asiodefs.hpp" 17 | #include "../erroror.hpp" 18 | #include "../tcphost.hpp" 19 | 20 | namespace wamp 21 | { 22 | 23 | namespace internal 24 | { 25 | 26 | //------------------------------------------------------------------------------ 27 | class TcpOpener 28 | { 29 | public: 30 | using Settings = TcpHost; 31 | using Socket = boost::asio::ip::tcp::socket; 32 | using SocketPtr = std::unique_ptr; 33 | 34 | template 35 | TcpOpener(TExecutorOrStrand&& exec, Settings s) 36 | : strand_(std::forward(exec)), 37 | settings_(std::move(s)), 38 | resolver_(strand_) 39 | {} 40 | 41 | template 42 | void establish(F&& callback) 43 | { 44 | struct Resolved 45 | { 46 | TcpOpener* self; 47 | typename std::decay::type callback; 48 | 49 | void operator()(boost::system::error_code asioEc, 50 | tcp::resolver::iterator iterator) 51 | { 52 | if (self->checkError(asioEc, callback)) 53 | self->connect(iterator, std::move(callback)); 54 | } 55 | }; 56 | 57 | // RawsockConnector will keep this object alive until completion. 58 | boost::asio::ip::tcp::resolver::query query{settings_.hostName(), 59 | settings_.serviceName()}; 60 | resolver_.async_resolve( 61 | query, Resolved{this, std::forward(callback)}); 62 | } 63 | 64 | void cancel() 65 | { 66 | resolver_.cancel(); 67 | if (socket_) 68 | socket_->close(); 69 | } 70 | 71 | private: 72 | using tcp = boost::asio::ip::tcp; 73 | 74 | template 75 | bool checkError(boost::system::error_code asioEc, F& callback) 76 | { 77 | if (asioEc) 78 | { 79 | auto ec = make_error_code(static_cast(asioEc.value())); 80 | callback(UnexpectedError(ec)); 81 | } 82 | return !asioEc; 83 | } 84 | 85 | template 86 | void connect(tcp::resolver::iterator iterator, F&& callback) 87 | { 88 | struct Connected 89 | { 90 | TcpOpener* self; 91 | typename std::decay::type callback; 92 | 93 | void operator()(boost::system::error_code asioEc, 94 | tcp::resolver::iterator) 95 | { 96 | SocketPtr socket{std::move(self->socket_)}; 97 | self->socket_.reset(); 98 | if (self->checkError(asioEc, callback)) 99 | callback(std::move(socket)); 100 | } 101 | }; 102 | 103 | assert(!socket_); 104 | socket_.reset(new Socket(strand_)); 105 | socket_->open(boost::asio::ip::tcp::v4()); 106 | settings_.options().applyTo(*socket_); 107 | 108 | // RawsockConnector will keep this object alive until completion. 109 | boost::asio::async_connect(*socket_, iterator, 110 | Connected{this, std::forward(callback)}); 111 | } 112 | 113 | IoStrand strand_; 114 | Settings settings_; 115 | boost::asio::ip::tcp::resolver resolver_; 116 | SocketPtr socket_; 117 | }; 118 | 119 | } // namespace internal 120 | 121 | } // namespace wamp 122 | 123 | #endif // CPPWAMP_INTERNAL_TCPOPENER_HPP 124 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/internal/tcpprotocol.ipp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #include "../tcpprotocol.hpp" 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "../api.hpp" 13 | 14 | namespace wamp 15 | { 16 | 17 | CPPWAMP_INLINE TcpOptions& TcpOptions::withBroadcast(bool b) {return set(b);} 18 | CPPWAMP_INLINE TcpOptions& TcpOptions::withDebug(bool b) {return set(b);} 19 | CPPWAMP_INLINE TcpOptions& TcpOptions::withDoNotRoute(bool b) {return set(b);} 20 | CPPWAMP_INLINE TcpOptions& TcpOptions::withKeepAlive(bool b) {return set(b);} 21 | CPPWAMP_INLINE TcpOptions& TcpOptions::withLinger(bool b, int n) {return set(b, n);} 22 | CPPWAMP_INLINE TcpOptions& TcpOptions::withOutOfBandInline(bool b) {return set(b);} 23 | CPPWAMP_INLINE TcpOptions& TcpOptions::withReceiveBufferSize(int n) {return set(n);} 24 | CPPWAMP_INLINE TcpOptions& TcpOptions::withReceiveLowWatermark(int n) {return set(n);} 25 | CPPWAMP_INLINE TcpOptions& TcpOptions::withReuseAddress(bool b) {return set(b);} 26 | CPPWAMP_INLINE TcpOptions& TcpOptions::withSendBufferSize(int n) {return set(n);} 27 | CPPWAMP_INLINE TcpOptions& TcpOptions::withSendLowWatermark(int n) {return set(n);} 28 | CPPWAMP_INLINE TcpOptions& TcpOptions::withUnicastHops(int n) {return set(n);} 29 | CPPWAMP_INLINE TcpOptions& TcpOptions::withIpV6Only(bool b) {return set(b);} 30 | CPPWAMP_INLINE TcpOptions& TcpOptions::withNoDelay(bool b) {return set(b);} 31 | 32 | template 33 | TcpOptions& TcpOptions::set(TArgs... args) 34 | { 35 | options_.add(TOption(args...)); 36 | return *this; 37 | } 38 | 39 | template 40 | void TcpOptions::applyTo(TSocket& socket) const {options_.applyTo(socket);} 41 | 42 | // Explicit template instantiation 43 | template void TcpOptions::applyTo(boost::asio::ip::tcp::socket&) const; 44 | 45 | } // namespace wamp 46 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/internal/uds.ipp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #include "../uds.hpp" 8 | #include "rawsockconnector.hpp" 9 | #include "udsopener.hpp" 10 | 11 | namespace wamp 12 | { 13 | 14 | //------------------------------------------------------------------------------ 15 | struct Connector::Impl 16 | { 17 | using RawsockOpener = internal::RawsockConnector; 18 | 19 | Impl(IoStrand i, Settings s, int codecId) 20 | : cnct(RawsockOpener::create(std::move(i), std::move(s), codecId)) 21 | {} 22 | 23 | RawsockOpener::Ptr cnct; 24 | }; 25 | 26 | //------------------------------------------------------------------------------ 27 | CPPWAMP_INLINE Connector::Connector(IoStrand i, Settings s, int codecId) 28 | : impl_(new Impl(std::move(i), std::move(s), codecId)) 29 | {} 30 | 31 | //------------------------------------------------------------------------------ 32 | // Needed to avoid incomplete type errors. 33 | //------------------------------------------------------------------------------ 34 | CPPWAMP_INLINE Connector::~Connector() {} 35 | 36 | //------------------------------------------------------------------------------ 37 | CPPWAMP_INLINE void Connector::establish(Handler&& handler) 38 | { 39 | impl_->cnct->establish(std::move(handler)); 40 | } 41 | 42 | //------------------------------------------------------------------------------ 43 | CPPWAMP_INLINE void Connector::cancel() {impl_->cnct->cancel();} 44 | 45 | } // namespace wamp 46 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/internal/udsacceptor.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_INTERNAL_UDSACCEPTOR_HPP 8 | #define CPPWAMP_INTERNAL_UDSACCEPTOR_HPP 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include "../asiodefs.hpp" 16 | #include "../erroror.hpp" 17 | #include "../udspath.hpp" 18 | 19 | namespace wamp 20 | { 21 | 22 | namespace internal 23 | { 24 | 25 | //------------------------------------------------------------------------------ 26 | class UdsAcceptor 27 | { 28 | public: 29 | using Settings = UdsPath; 30 | using Socket = boost::asio::local::stream_protocol::socket; 31 | using SocketPtr = std::unique_ptr; 32 | 33 | template 34 | UdsAcceptor(TExecutorOrStrand&& exec, Settings s) 35 | : strand_(std::forward(exec)), 36 | settings_(std::move(s)) 37 | {} 38 | 39 | template 40 | void establish(F&& callback) 41 | { 42 | struct Accepted 43 | { 44 | UdsAcceptor* self; 45 | typename std::decay::type callback; 46 | 47 | void operator()(boost::system::error_code asioEc) 48 | { 49 | SocketPtr socket{std::move(self->socket_)}; 50 | self->socket_.reset(); 51 | if (self->checkError(asioEc, callback)) 52 | callback(std::move(socket)); 53 | } 54 | }; 55 | 56 | assert(!socket_ && "Accept already in progress"); 57 | 58 | // Acceptor must be constructed lazily to give a chance to delete 59 | // remnant file. 60 | if (!acceptor_) 61 | { 62 | if (settings_.deletePathEnabled()) 63 | std::remove(settings_.pathName().c_str()); 64 | acceptor_.reset(new Acceptor(strand_, settings_.pathName())); 65 | } 66 | socket_.reset(new Socket(strand_)); 67 | 68 | // RawsockListener will keep this object alive until completion. 69 | acceptor_->async_accept(*socket_, 70 | Accepted{this, std::forward(callback)}); 71 | } 72 | 73 | void cancel() 74 | { 75 | if (acceptor_) 76 | acceptor_->cancel(); 77 | } 78 | 79 | private: 80 | using Acceptor = boost::asio::local::stream_protocol::acceptor; 81 | 82 | template 83 | bool checkError(boost::system::error_code asioEc, F& callback) 84 | { 85 | if (asioEc) 86 | { 87 | auto ec = make_error_code(static_cast(asioEc.value())); 88 | callback(UnexpectedError(ec)); 89 | } 90 | return !asioEc; 91 | } 92 | 93 | IoStrand strand_; 94 | Settings settings_; 95 | std::unique_ptr acceptor_; 96 | SocketPtr socket_; 97 | }; 98 | 99 | } // namespace internal 100 | 101 | } // namespace wamp 102 | 103 | #endif // CPPWAMP_INTERNAL_UDSACCEPTOR_HPP 104 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/internal/udsopener.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_INTERNAL_UDSOPENER_HPP 8 | #define CPPWAMP_INTERNAL_UDSOPENER_HPP 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include "../asiodefs.hpp" 16 | #include "../erroror.hpp" 17 | #include "../udspath.hpp" 18 | 19 | namespace wamp 20 | { 21 | 22 | namespace internal 23 | { 24 | 25 | //------------------------------------------------------------------------------ 26 | class UdsOpener 27 | { 28 | public: 29 | using Settings = UdsPath; 30 | using Socket = boost::asio::local::stream_protocol::socket; 31 | using SocketPtr = std::unique_ptr; 32 | 33 | template 34 | UdsOpener(TExecutorOrStrand&& exec, Settings s) 35 | : strand_(std::forward(exec)), 36 | settings_(std::move(s)) 37 | {} 38 | 39 | template 40 | void establish(F&& callback) 41 | { 42 | struct Connected 43 | { 44 | UdsOpener* self; 45 | typename std::decay::type callback; 46 | 47 | void operator()(boost::system::error_code asioEc) 48 | { 49 | SocketPtr socket{std::move(self->socket_)}; 50 | self->socket_.reset(); 51 | if (self->checkError(asioEc, callback)) 52 | callback(std::move(socket)); 53 | } 54 | }; 55 | 56 | assert(!socket_ && "Connect already in progress"); 57 | 58 | socket_.reset(new Socket(strand_)); 59 | socket_->open(); 60 | settings_.options().applyTo(*socket_); 61 | 62 | // RawsockConnector will keep this object alive until completion. 63 | socket_->async_connect(settings_.pathName(), 64 | Connected{this, std::forward(callback)}); 65 | } 66 | 67 | void cancel() 68 | { 69 | if (socket_) 70 | socket_->close(); 71 | } 72 | 73 | private: 74 | template 75 | bool checkError(boost::system::error_code asioEc, F& callback) 76 | { 77 | if (asioEc) 78 | { 79 | auto ec = make_error_code(static_cast(asioEc.value())); 80 | callback(UnexpectedError(ec)); 81 | } 82 | return !asioEc; 83 | } 84 | 85 | IoStrand strand_; 86 | Settings settings_; 87 | SocketPtr socket_; 88 | }; 89 | 90 | } // namespace internal 91 | 92 | } // namespace wamp 93 | 94 | #endif // CPPWAMP_INTERNAL_UDSOPENER_HPP 95 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/internal/udspath.ipp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #include "../udspath.hpp" 8 | #include 9 | #include "../api.hpp" 10 | 11 | namespace wamp 12 | { 13 | 14 | CPPWAMP_INLINE UdsPath::UdsPath(std::string pathName, UdsOptions options, 15 | RawsockMaxLength maxRxLength, 16 | bool deletePath) 17 | : pathName_(pathName), 18 | options_(options), 19 | maxRxLength_(maxRxLength), 20 | deletePathEnabled_(deletePath) 21 | {} 22 | 23 | CPPWAMP_INLINE UdsPath& UdsPath::withOptions(UdsOptions options) 24 | { 25 | options_ = std::move(options); 26 | return *this; 27 | } 28 | 29 | CPPWAMP_INLINE UdsPath& UdsPath::withMaxRxLength(RawsockMaxLength length) 30 | { 31 | maxRxLength_ = length; 32 | return *this; 33 | } 34 | 35 | CPPWAMP_INLINE const std::string& UdsPath::pathName() const {return pathName_;} 36 | 37 | CPPWAMP_INLINE const UdsOptions& UdsPath::options() const {return options_;} 38 | 39 | CPPWAMP_INLINE RawsockMaxLength UdsPath::maxRxLength() const 40 | { 41 | return maxRxLength_; 42 | } 43 | 44 | CPPWAMP_INLINE bool UdsPath::deletePathEnabled() const 45 | { 46 | return deletePathEnabled_; 47 | } 48 | 49 | } // namespace wamp 50 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/internal/udsprotocol.ipp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #include "../udsprotocol.hpp" 8 | #include 9 | #include "../api.hpp" 10 | 11 | namespace wamp 12 | { 13 | 14 | CPPWAMP_INLINE UdsOptions& UdsOptions::withBroadcast(bool b) {return set(b);} 15 | CPPWAMP_INLINE UdsOptions& UdsOptions::withDebug(bool b) {return set(b);} 16 | CPPWAMP_INLINE UdsOptions& UdsOptions::withDoNotRoute(bool b) {return set(b);} 17 | CPPWAMP_INLINE UdsOptions& UdsOptions::withKeepAlive(bool b) {return set(b);} 18 | CPPWAMP_INLINE UdsOptions& UdsOptions::withLinger(bool b, int n) {return set(b, n);} 19 | CPPWAMP_INLINE UdsOptions& UdsOptions::withOutOfBandInline(bool b) {return set(b);} 20 | CPPWAMP_INLINE UdsOptions& UdsOptions::withReceiveBufferSize(int n) {return set(n);} 21 | CPPWAMP_INLINE UdsOptions& UdsOptions::withReceiveLowWatermark(int n) {return set(n);} 22 | CPPWAMP_INLINE UdsOptions& UdsOptions::withReuseAddress(bool b) {return set(b);} 23 | CPPWAMP_INLINE UdsOptions& UdsOptions::withSendBufferSize(int n) {return set(n);} 24 | CPPWAMP_INLINE UdsOptions& UdsOptions::withSendLowWatermark(int n) {return set(n);} 25 | 26 | template 27 | UdsOptions& UdsOptions::set(TArgs... args) 28 | { 29 | options_.add(TOption(args...)); 30 | return *this; 31 | } 32 | 33 | template 34 | void UdsOptions::applyTo(TSocket& socket) const {options_.applyTo(socket);} 35 | 36 | // Explicit template instantiation 37 | template void 38 | UdsOptions::applyTo(boost::asio::local::stream_protocol::socket&) const; 39 | 40 | } // namespace wamp 41 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/internal/variantencoding.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_INTERNAL_VARIANTENCODING_HPP 8 | #define CPPWAMP_INTERNAL_VARIANTENCODING_HPP 9 | 10 | #include 11 | #include 12 | #include "../codec.hpp" 13 | #include "../variant.hpp" 14 | 15 | namespace wamp 16 | { 17 | 18 | namespace internal 19 | { 20 | 21 | //------------------------------------------------------------------------------ 22 | template 23 | class VariantEncodingVisitor : public Visitor<> 24 | { 25 | public: 26 | explicit VariantEncodingVisitor(TEncoder& encoder) 27 | : encoder_(encoder) 28 | {} 29 | 30 | void operator()(Null) 31 | { 32 | encoder_.null_value(); 33 | } 34 | 35 | void operator()(Bool b) 36 | { 37 | encoder_.bool_value(b); 38 | } 39 | 40 | void operator()(Int n) 41 | { 42 | encoder_.int64_value(n); 43 | } 44 | 45 | void operator()(UInt n) 46 | { 47 | encoder_.uint64_value(n); 48 | } 49 | 50 | void operator()(Real x) 51 | { 52 | encoder_.double_value(x); 53 | } 54 | 55 | void operator()(const String& s) 56 | { 57 | encoder_.string_value({s.data(), s.size()}); 58 | } 59 | 60 | void operator()(const Blob& b) 61 | { 62 | jsoncons::byte_string_view bsv(b.data().data(), b.data().size()); 63 | encoder_.byte_string_value(bsv); 64 | } 65 | 66 | void operator()(const Array& a) 67 | { 68 | encoder_.begin_array(a.size()); 69 | for (const auto& v: a) 70 | wamp::apply(*this, v); 71 | encoder_.end_array(); 72 | } 73 | 74 | void operator()(const Object& o) 75 | { 76 | encoder_.begin_object(o.size()); 77 | for (const auto& kv: o) 78 | { 79 | const auto& key = kv.first; 80 | encoder_.key({key.data(), key.size()}); 81 | wamp::apply(*this, kv.second); 82 | } 83 | encoder_.end_object(); 84 | } 85 | 86 | private: 87 | TEncoder& encoder_; 88 | }; 89 | 90 | //------------------------------------------------------------------------------ 91 | template 92 | struct GenericEncoderSinkTraits {}; 93 | 94 | template <> 95 | struct GenericEncoderSinkTraits 96 | { 97 | using Sink = jsoncons::string_sink; 98 | using StubArg = std::string; 99 | }; 100 | 101 | template <> 102 | struct GenericEncoderSinkTraits 103 | { 104 | using Sink = jsoncons::string_sink; 105 | using StubArg = MessageBuffer; 106 | }; 107 | 108 | template <> 109 | struct GenericEncoderSinkTraits 110 | { 111 | using Sink = jsoncons::stream_sink; 112 | using StubArg = std::nullptr_t; 113 | }; 114 | 115 | //------------------------------------------------------------------------------ 116 | template 117 | class GenericEncoder 118 | { 119 | private: 120 | using SinkTraits = internal::GenericEncoderSinkTraits; 121 | 122 | public: 123 | using Sink = typename TConfig::Sink; 124 | 125 | GenericEncoder() : 126 | stub_(typename SinkTraits::StubArg{}), 127 | encoder_(stub_) 128 | {} 129 | 130 | void encode(const Variant& variant, Sink sink) 131 | { 132 | encoder_.reset(sink.output()); 133 | wamp::apply(VariantEncodingVisitor(encoder_), 134 | variant); 135 | } 136 | 137 | private: 138 | using Output = typename Sink::Output; 139 | using EncoderSink = typename SinkTraits::Sink; 140 | using Encoder = typename TConfig::template EncoderType; 141 | 142 | Output stub_; 143 | Encoder encoder_; 144 | }; 145 | 146 | } // namespace internal 147 | 148 | } // namespace wamp 149 | 150 | #endif // CPPWAMP_INTERNAL_VARIANTENCODING_HPP 151 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/internal/version.ipp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #include "../version.hpp" 8 | 9 | #include 10 | #include "../api.hpp" 11 | 12 | namespace wamp 13 | { 14 | 15 | //------------------------------------------------------------------------------ 16 | CPPWAMP_INLINE Version Version::parts() 17 | { 18 | return {CPPWAMP_MAJOR_VERSION, 19 | CPPWAMP_MINOR_VERSION, 20 | CPPWAMP_PATCH_VERSION}; 21 | } 22 | 23 | //------------------------------------------------------------------------------ 24 | /** @details 25 | The integer version number is computed as: 26 | ``` 27 | (MAJOR*10000) + (MINOR*100) + PATCH 28 | ``` 29 | */ 30 | //------------------------------------------------------------------------------ 31 | CPPWAMP_INLINE int Version::integer() 32 | { 33 | return CPPWAMP_VERSION; 34 | } 35 | 36 | //------------------------------------------------------------------------------ 37 | /** @details 38 | The string representation is formatted as: 39 | ``` 40 | MAJOR.MINOR.PATCH 41 | ``` 42 | without any zero padding. */ 43 | //------------------------------------------------------------------------------ 44 | CPPWAMP_INLINE std::string Version::toString() 45 | { 46 | std::ostringstream oss; 47 | oss << CPPWAMP_MAJOR_VERSION << '.' 48 | << CPPWAMP_MINOR_VERSION << '.' 49 | << CPPWAMP_PATCH_VERSION; 50 | return oss.str(); 51 | } 52 | 53 | //------------------------------------------------------------------------------ 54 | /** @details 55 | The agent string is formatted as: 56 | ``` 57 | cppwamp-MAJOR.MINOR.PATCH 58 | ``` 59 | without any zero padding. */ 60 | //------------------------------------------------------------------------------ 61 | CPPWAMP_INLINE std::string Version::agentString() 62 | { 63 | return "cppwamp-" + Version::toString(); 64 | } 65 | 66 | } // namespace wamp 67 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/messagebuffer.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_MESSAGEBUFFER_HPP 8 | #define CPPWAMP_MESSAGEBUFFER_HPP 9 | 10 | //------------------------------------------------------------------------------ 11 | /** @file 12 | @brief Contains the MessageBuffer definition. */ 13 | //------------------------------------------------------------------------------ 14 | 15 | #include 16 | #include 17 | 18 | namespace wamp 19 | { 20 | 21 | //------------------------------------------------------------------------------ 22 | /** Container type used for encoded WAMP messages that are sent/received 23 | over a transport. */ 24 | //------------------------------------------------------------------------------ 25 | using MessageBuffer = std::vector; 26 | 27 | 28 | } // namespace wamp 29 | 30 | #endif // CPPWAMP_MESSAGEBUFFER_HPP 31 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/null.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_NULL_HPP 8 | #define CPPWAMP_NULL_HPP 9 | 10 | //------------------------------------------------------------------------------ 11 | /** @file 12 | @brief Contains the declaration of Null, one of the bound types 13 | used by Variant. */ 14 | //------------------------------------------------------------------------------ 15 | 16 | #include 17 | #include "api.hpp" 18 | #include "config.hpp" 19 | 20 | namespace wamp 21 | { 22 | 23 | //------------------------------------------------------------------------------ 24 | /** Type used to represent a null or empty Variant value. 25 | 26 | The `wamp::null` constant should be used to nullify a `Variant`, and 27 | for checking if a `Variant` is empty: 28 | ``` 29 | using namespace wamp; 30 | Variant v(42); // v contains an integer 31 | v = null; // v is now empty 32 | 33 | // Check if v is null 34 | if (v == null) 35 | std::cout << "v is empty\n"; 36 | ``` 37 | @see wamp::null */ 38 | //------------------------------------------------------------------------------ 39 | struct CPPWAMP_API Null 40 | { 41 | /// Default constructor for instantiating constexpr object. 42 | constexpr Null() = default; 43 | 44 | /// Compares two `Null` objects for equality. @return always `true`. 45 | bool operator==(Null) const {return true;} 46 | 47 | /// Compares two `Null` objects for inequality. @return always `false`. 48 | bool operator!=(Null) const {return false;} 49 | 50 | /// Performs less-than comparison on two `Null` objects. 51 | /// @return always `false`. 52 | bool operator<(Null) const {return false;} 53 | }; 54 | 55 | //------------------------------------------------------------------------------ 56 | /** @relates Null 57 | Outputs a `Null` object to the given output stream. 58 | The string `"null"` (without the quotes) will be outputted. */ 59 | //------------------------------------------------------------------------------ 60 | CPPWAMP_API inline std::ostream& operator<<(std::ostream& out, Null) 61 | { 62 | return out << "null"; 63 | } 64 | 65 | //------------------------------------------------------------------------------ 66 | /** Constant Null object that can be assigned to, or compared with a Variant. */ 67 | //------------------------------------------------------------------------------ 68 | CPPWAMP_INLINE_VARIABLE constexpr Null null; 69 | 70 | 71 | } // namespace wamp 72 | 73 | #endif // CPPWAMP_NULL_HPP 74 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/options.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_OPTIONS_HPP 8 | #define CPPWAMP_OPTIONS_HPP 9 | 10 | #include 11 | #include "api.hpp" 12 | #include "traits.hpp" 13 | #include "variant.hpp" 14 | #include "./internal/passkey.hpp" 15 | #include "./internal/wampmessage.hpp" 16 | 17 | //------------------------------------------------------------------------------ 18 | /** @file 19 | @brief Contains the declaration of the Options class. */ 20 | //------------------------------------------------------------------------------ 21 | 22 | namespace wamp 23 | { 24 | 25 | //------------------------------------------------------------------------------ 26 | /** Wrapper around a WAMP message containing an options dictionary. */ 27 | //------------------------------------------------------------------------------ 28 | template 29 | class CPPWAMP_API Options 30 | { 31 | public: 32 | /** Adds an option. */ 33 | TDerived& withOption(String key, Variant value) 34 | { 35 | message_.options().emplace(std::move(key), std::move(value)); 36 | return static_cast(*this); 37 | } 38 | 39 | /** Sets all options at once. */ 40 | TDerived& withOptions(Object opts) 41 | { 42 | message_.options() = std::move(opts); 43 | return static_cast(*this); 44 | } 45 | 46 | /** Accesses the entire dictionary of options. */ 47 | const Object& options() const {return message_.options();} 48 | 49 | /** Obtains an option by key. */ 50 | Variant optionByKey(const String& key) const 51 | { 52 | Variant result; 53 | auto iter = options().find(key); 54 | if (iter != options().end()) 55 | result = iter->second; 56 | return result; 57 | } 58 | 59 | /** Obtains an option by key or a fallback value. */ 60 | template 61 | ValueTypeOf optionOr( 62 | const String& key, /**< The key to search under. */ 63 | T&& fallback /**< The fallback value to return if the key was 64 | not found. */ 65 | ) const 66 | { 67 | auto iter = options().find(key); 68 | if (iter != options().end()) 69 | return iter->second.template to>(); 70 | else 71 | return std::forward(fallback); 72 | } 73 | 74 | protected: 75 | using MessageType = TMessage; 76 | 77 | /** Constructor taking message construction aruments. */ 78 | template 79 | explicit Options(TArgs&&... args) 80 | : message_(std::forward(args)...) 81 | {} 82 | 83 | MessageType& message() {return message_;} 84 | 85 | const MessageType& message() const {return message_;} 86 | 87 | private: 88 | MessageType message_; 89 | 90 | public: 91 | // Internal use only 92 | MessageType& message(internal::PassKey) {return message_;} 93 | }; 94 | 95 | } // namespace wamp 96 | 97 | #endif // CPPWAMP_OPTIONS_HPP 98 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/rawsockoptions.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_RAWSOCKOPTIONS_HPP 8 | #define CPPWAMP_RAWSOCKOPTIONS_HPP 9 | 10 | //------------------------------------------------------------------------------ 11 | /** @file 12 | @brief Contains common option definitions for raw socket transports. */ 13 | //------------------------------------------------------------------------------ 14 | 15 | namespace wamp 16 | { 17 | 18 | //------------------------------------------------------------------------------ 19 | /** Enumerators used to specify the maximum length of messages that a raw 20 | socket transport can receive. */ 21 | //------------------------------------------------------------------------------ 22 | enum class RawsockMaxLength 23 | { 24 | B_512, ///< 512 bytes 25 | kB_1, ///< 1 kilobyte 26 | kB_2, ///< 2 kilobytes 27 | kB_4, ///< 4 kilobytes 28 | kB_8, ///< 8 kilobytes 29 | kB_16, ///< 16 kilobytes 30 | kB_32, ///< 32 kilobytes 31 | kB_64, ///< 64 kilobytes 32 | kB_128, ///< 128 kilobytes 33 | kB_256, ///< 256 kilobytes 34 | kB_512, ///< 512 kilobytes 35 | MB_1, ///< 1 megabyte 36 | MB_2, ///< 2 megabytes 37 | MB_4, ///< 4 megabytes 38 | MB_8, ///< 8 megabytes 39 | MB_16 ///< 16 megabytes 40 | }; 41 | 42 | } // namespace wamp 43 | 44 | #endif // CPPWAMP_RAWSOCKOPTIONS_HPP 45 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/sessiondata.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_SESSIONDATA_HPP 8 | #define CPPWAMP_SESSIONDATA_HPP 9 | 10 | // This header is deprecated but kept for backward compatiblity. 11 | // Please include instead. 12 | 13 | #include "peerdata.hpp" 14 | 15 | #endif // CPPWAMP_SESSIONDATA_HPP 16 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/tagtypes.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_TAGTYPES_HPP 8 | #define CPPWAMP_TAGTYPES_HPP 9 | 10 | //------------------------------------------------------------------------------ 11 | /** @file 12 | @brief Contain miscellaneous overload disambiguation tag types. */ 13 | //------------------------------------------------------------------------------ 14 | 15 | #include "api.hpp" 16 | #include "config.hpp" 17 | 18 | namespace wamp 19 | { 20 | 21 | //------------------------------------------------------------------------------ 22 | /** Tag type used to specify than an operation is to be dispatched via the 23 | called objects's execution strand. 24 | Use the wamp::threadSafe constant to conveniently pass this tag 25 | to functions. */ 26 | //------------------------------------------------------------------------------ 27 | struct CPPWAMP_API ThreadSafe 28 | { 29 | constexpr ThreadSafe() = default; 30 | }; 31 | 32 | //------------------------------------------------------------------------------ 33 | /** Constant ThreadSafe object instance that can be passed to functions. */ 34 | //------------------------------------------------------------------------------ 35 | CPPWAMP_INLINE_VARIABLE constexpr ThreadSafe threadSafe; 36 | 37 | 38 | } // namespace wamp 39 | 40 | #endif // CPPWAMP_TAGTYPES_HPP 41 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/tcp.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_TCP_HPP 8 | #define CPPWAMP_TCP_HPP 9 | 10 | //------------------------------------------------------------------------------ 11 | /** @file 12 | @brief Contains facilities for creating TCP transport connectors. */ 13 | //------------------------------------------------------------------------------ 14 | 15 | #include 16 | #include 17 | #include "api.hpp" 18 | #include "asiodefs.hpp" 19 | #include "connector.hpp" 20 | #include "tcphost.hpp" 21 | #include "traits.hpp" 22 | 23 | namespace wamp 24 | { 25 | 26 | //------------------------------------------------------------------------------ 27 | /** Connector specialization that establishes a TCP transport. 28 | Users do not need to use this class directly and should use 29 | ConnectionWish instead. */ 30 | //------------------------------------------------------------------------------ 31 | template <> 32 | class CPPWAMP_API Connector : public Connecting 33 | { 34 | public: 35 | /** Type containing the transport settings. */ 36 | using Settings = TcpHost; 37 | 38 | /** Constructor. */ 39 | Connector(IoStrand i, Settings s, int codecId); 40 | 41 | /** Destructor. */ 42 | ~Connector(); 43 | 44 | /** Starts establishing the transport connection, emitting a 45 | Transportable::Ptr via the given handler if successful. */ 46 | void establish(Handler&& handler) override; 47 | 48 | /** Cancels transport connection in progress, emitting an error code 49 | via the handler passed to the establish method. */ 50 | void cancel() override; 51 | 52 | private: 53 | struct Impl; 54 | std::unique_ptr impl_; 55 | }; 56 | 57 | //------------------------------------------------------------------------------ 58 | /** Creates a LegacyConnector that can establish a TCP raw socket transport. 59 | 60 | This overload takes an executor that is convertible to 61 | the boost::asio::any_io_executor polymorphic wrapper. 62 | 63 | @deprecated Use wamp::ConnectionWish instead 64 | @relates TcpHost 65 | @returns a `std::shared_ptr` to a Connecting 66 | @tparam TFormat The serialization format to use over this transport. 67 | @see Connecting, Json, Msgpack */ 68 | //------------------------------------------------------------------------------ 69 | template 70 | CPPWAMP_API LegacyConnector connector( 71 | AnyIoExecutor e, ///< The executor to be used by the transport. 72 | TcpHost h ///< TCP host address and other socket options. 73 | ) 74 | { 75 | return LegacyConnector{std::move(e), std::move(h), TFormat{}}; 76 | } 77 | 78 | //------------------------------------------------------------------------------ 79 | /** Creates a LegacyConnector that can establish a TCP raw socket transport. 80 | 81 | Only participates in overload resolution when 82 | `isExecutionContext() == true` 83 | 84 | @deprecated Use wamp::ConnectionWish instead 85 | @relates TcpHost 86 | @returns a `std::shared_ptr` to a Connecting 87 | @tparam TFormat The serialization format to use over this transport. 88 | @tparam TExecutionContext The given execution context type (deduced). 89 | @see Connecting, Json, Msgpack */ 90 | //------------------------------------------------------------------------------ 91 | template 92 | CPPWAMP_ENABLED_TYPE(LegacyConnector, isExecutionContext()) 93 | connector( 94 | TExecutionContext& context, /**< The I/O context containing the executor 95 | to be used by the transport. */ 96 | TcpHost host ///< TCP host address and other socket options. 97 | ) 98 | { 99 | return connector(context.get_executor(), std::move(host)); 100 | } 101 | 102 | } // namespace wamp 103 | 104 | #ifndef CPPWAMP_COMPILED_LIB 105 | #include "internal/tcp.ipp" 106 | #endif 107 | 108 | #endif // CPPWAMP_TCP_HPP 109 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/tcpendpoint.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_TCPENDPOINT_HPP 8 | #define CPPWAMP_TCPENDPOINT_HPP 9 | 10 | //------------------------------------------------------------------------------ 11 | /** @file 12 | @brief Contains facilities for specifying TCP server parameters and 13 | options. */ 14 | //------------------------------------------------------------------------------ 15 | 16 | #include 17 | #include "api.hpp" 18 | #include "rawsockoptions.hpp" 19 | #include "tcpprotocol.hpp" 20 | 21 | namespace wamp 22 | { 23 | 24 | //------------------------------------------------------------------------------ 25 | /** Contains TCP host address information, as well as other socket options. */ 26 | //------------------------------------------------------------------------------ 27 | class CPPWAMP_API TcpEndpoint 28 | { 29 | public: 30 | /// Transport protocol tag associated with these settings. 31 | using Protocol = Tcp; 32 | 33 | /// The default maximum length permitted for incoming messages. 34 | static constexpr RawsockMaxLength defaultMaxRxLength = 35 | RawsockMaxLength::MB_16; 36 | 37 | /** Constructor taking a port number. */ 38 | TcpEndpoint( 39 | unsigned short port, ///< Port number. 40 | TcpOptions options = {}, ///< TCP socket options. 41 | RawsockMaxLength maxRxLength 42 | = defaultMaxRxLength ///< Maximum inbound message length 43 | ); 44 | 45 | /** Constructor taking an address string and a port number. */ 46 | TcpEndpoint( 47 | std::string address, ///< Address string. 48 | unsigned short port, ///< Port number. 49 | TcpOptions options = {}, ///< TCP socket options. 50 | RawsockMaxLength maxRxLength 51 | = defaultMaxRxLength ///< Maximum inbound message length 52 | ); 53 | 54 | /** Specifies the socket options to use. */ 55 | TcpEndpoint& withOptions(TcpOptions options); 56 | 57 | /** Specifies the maximum length permitted for incoming messages. */ 58 | TcpEndpoint& withMaxRxLength(RawsockMaxLength length); 59 | 60 | /** Obtains the endpoint address. */ 61 | const std::string& address() const; 62 | 63 | /** Obtains the the port number. */ 64 | unsigned short port() const; 65 | 66 | /** Obtains the transport options. */ 67 | const TcpOptions& options() const; 68 | 69 | /** Obtains the specified maximum incoming message length. */ 70 | RawsockMaxLength maxRxLength() const; 71 | 72 | private: 73 | std::string address_; 74 | TcpOptions options_; 75 | RawsockMaxLength maxRxLength_; 76 | unsigned short port_; 77 | }; 78 | 79 | } // namespace wamp 80 | 81 | #ifndef CPPWAMP_COMPILED_LIB 82 | #include "./internal/tcpendpoint.ipp" 83 | #endif 84 | 85 | #endif // CPPWAMP_TCPENDPOINT_HPP 86 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/tcpprotocol.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_TCPPROTOCOL_HPP 8 | #define CPPWAMP_TCPPROTOCOL_HPP 9 | 10 | //------------------------------------------------------------------------------ 11 | /** @file 12 | @brief Contains basic TCP protocol facilities. */ 13 | //------------------------------------------------------------------------------ 14 | 15 | #include "api.hpp" 16 | #include "config.hpp" 17 | #include "internal/socketoptions.hpp" 18 | 19 | // Forward declaration 20 | namespace boost { namespace asio { namespace ip { class tcp; }}} 21 | 22 | namespace wamp 23 | { 24 | 25 | //------------------------------------------------------------------------------ 26 | /** Tag type associated with the TCP transport. */ 27 | //------------------------------------------------------------------------------ 28 | struct CPPWAMP_API Tcp 29 | { 30 | constexpr Tcp() = default; 31 | }; 32 | 33 | 34 | namespace internal { class TcpOpener; } // Forward declaration 35 | 36 | //------------------------------------------------------------------------------ 37 | /** Contains options for the TCP transport. 38 | @note Support for these options depends on the the operating system. 39 | @see https://man7.org/linux/man-pages/man7/socket.7.html 40 | @see https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-setsockopt 41 | @see https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/setsockopt.2.html */ 42 | //------------------------------------------------------------------------------ 43 | class CPPWAMP_API TcpOptions 44 | { 45 | public: 46 | /** Adds the SO_BROADCAST socket option. */ 47 | TcpOptions& withBroadcast(bool enabled = true); 48 | 49 | /** Adds the SO_DEBUG socket option. */ 50 | TcpOptions& withDebug(bool enabled = true); 51 | 52 | /** Adds the SO_DONTROUTE socket option. */ 53 | TcpOptions& withDoNotRoute(bool enabled = true); 54 | 55 | /** Adds the SO_KEEPALIVE socket option. */ 56 | TcpOptions& withKeepAlive(bool enabled = true); 57 | 58 | /** Adds the SO_LINGER socket option. */ 59 | TcpOptions& withLinger(bool enabled, int timeout); 60 | 61 | /** Adds the SO_OOBINLINE socket option. */ 62 | TcpOptions& withOutOfBandInline(bool enabled); 63 | 64 | /** Adds the SO_RCVBUF socket option. */ 65 | TcpOptions& withReceiveBufferSize(int size); 66 | 67 | /** Adds the SO_RCVLOWAT socket option. */ 68 | TcpOptions& withReceiveLowWatermark(int size); 69 | 70 | /** Adds the SO_REUSEADDR socket option. */ 71 | TcpOptions& withReuseAddress(bool enabled = true); 72 | 73 | /** Adds the SO_SNDBUF socket option. */ 74 | TcpOptions& withSendBufferSize(int size); 75 | 76 | /** Adds the SO_SNDLOWAT socket option. */ 77 | TcpOptions& withSendLowWatermark(int size); 78 | 79 | /** Adds the IP_UNICAST_TTL socket option. */ 80 | TcpOptions& withUnicastHops(int hops); 81 | 82 | /** Adds the IP_V6ONLY socket option. */ 83 | TcpOptions& withIpV6Only(bool enabled = true); 84 | 85 | /** Adds the TCP_NODELAY socket option. 86 | This option is for disabling the Nagle algorithm. */ 87 | TcpOptions& withNoDelay(bool enabled = true); 88 | 89 | private: 90 | template 91 | TcpOptions& set(TArgs... args); 92 | 93 | template void applyTo(TSocket& socket) const; 94 | 95 | internal::SocketOptionList options_; 96 | 97 | friend class internal::TcpOpener; 98 | 99 | /* Implementation note: Explicit template instantiation does not seem 100 | to play nice with CRTP, so it was not feasible to factor out the 101 | commonality with UdsOptions as a mixin (not without giving up the 102 | fluent API). */ 103 | }; 104 | 105 | } // namespace wamp 106 | 107 | #ifndef CPPWAMP_COMPILED_LIB 108 | #include "./internal/tcpprotocol.ipp" 109 | #endif 110 | 111 | #endif // CPPWAMP_TCPPROTOCOL_HPP 112 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/transport.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_TRANSPORT_HPP 8 | #define CPPWAMP_TRANSPORT_HPP 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "asiodefs.hpp" 15 | #include "erroror.hpp" 16 | #include "messagebuffer.hpp" 17 | 18 | namespace wamp 19 | { 20 | 21 | //------------------------------------------------------------------------------ 22 | struct TransportInfo 23 | { 24 | int codecId; 25 | std::size_t maxTxLength; 26 | std::size_t maxRxLength; 27 | }; 28 | 29 | //------------------------------------------------------------------------------ 30 | // Interface class for transports. 31 | //------------------------------------------------------------------------------ 32 | class Transporting : public std::enable_shared_from_this 33 | { 34 | public: 35 | /// Shared pointer to a Transporting object. 36 | using Ptr = std::shared_ptr; 37 | 38 | /// Handler type used for message received events. 39 | using RxHandler = std::function)>; 40 | 41 | /// Handler type used for transmission error events. 42 | using TxErrorHandler = std::function; 43 | 44 | /// Handler type used for ping response events. 45 | using PingHandler = std::function; 46 | 47 | // Noncopyable 48 | Transporting(const Transporting&) = delete; 49 | Transporting& operator=(const Transporting&) = delete; 50 | 51 | /** Destructor. */ 52 | virtual ~Transporting() {} 53 | 54 | /** Obtains information pertaining to this transport. */ 55 | virtual TransportInfo info() const = 0; 56 | 57 | /** Returns true if the transport has been started. */ 58 | virtual bool isStarted() const = 0; 59 | 60 | /** Starts the transport's I/O operations. */ 61 | virtual void start(RxHandler rxHandler, 62 | TxErrorHandler txHandler = nullptr) = 0; 63 | 64 | /** Sends the given serialized message via the transport. */ 65 | virtual void send(MessageBuffer message) = 0; 66 | 67 | /** Stops I/O operations and closes the underlying socket. */ 68 | virtual void close() = 0; 69 | 70 | /** Sends a transport-level ping message. */ 71 | virtual void ping(MessageBuffer message, PingHandler handler) = 0; 72 | 73 | protected: 74 | Transporting() = default; 75 | }; 76 | 77 | } // namespace wamp 78 | 79 | #endif // CPPWAMP_TRANSPORT_HPP 80 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/types/array.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2017, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_TYPES_ARRAY_HPP 8 | #define CPPWAMP_TYPES_ARRAY_HPP 9 | 10 | //------------------------------------------------------------------------------ 11 | /** @file 12 | @brief Provides facilities allowing Variant to interoperate 13 | with std::array. */ 14 | //------------------------------------------------------------------------------ 15 | 16 | #include 17 | #include "../api.hpp" 18 | #include "../error.hpp" 19 | #include "../variant.hpp" 20 | 21 | namespace wamp 22 | { 23 | 24 | //------------------------------------------------------------------------------ 25 | /** Performs the conversion from an array variant to a `std::array`. 26 | Users should not use this function directly. Use Variant::to instead. */ 27 | //------------------------------------------------------------------------------ 28 | template 29 | CPPWAMP_API void convert(FromVariantConverter& conv, std::array& array) 30 | { 31 | using namespace wamp; 32 | const auto& variant = conv.variant(); 33 | if (variant.is() == false) 34 | { 35 | throw error::Conversion("Attempting to convert non-array variant " 36 | "to std::array"); 37 | } 38 | 39 | std::array newArray; 40 | const auto& variantArray = variant.as(); 41 | if (variantArray.size() != Size) 42 | { 43 | throw error::Conversion("Variant array size does not match that " 44 | "of std::array"); 45 | } 46 | 47 | for (Array::size_type i=0; i(); 52 | } 53 | catch (const error::Conversion& e) 54 | { 55 | std::string msg = e.what(); 56 | msg += " (for element #" + std::to_string(i) + ")"; 57 | throw error::Conversion(msg); 58 | } 59 | } 60 | array = std::move(newArray); 61 | } 62 | 63 | //------------------------------------------------------------------------------ 64 | /** Performs the conversion from a `std::array` to an array variant. 65 | Users should not use this function directly. Use Variant::from instead. */ 66 | //------------------------------------------------------------------------------ 67 | template 68 | CPPWAMP_API void convert(ToVariantConverter& conv, std::array& array) 69 | { 70 | using namespace wamp; 71 | Array variantArray; 72 | for (const auto& elem: array) 73 | { 74 | variantArray.emplace_back(Variant::from(elem)); 75 | } 76 | conv.variant() = std::move(variantArray); 77 | } 78 | 79 | } // namespace wamp 80 | 81 | #endif // CPPWAMP_TYPES_ARRAY_HPP 82 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/types/boostoptional.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_TYPES_BOOSTOPTIONAL_HPP 8 | #define CPPWAMP_TYPES_BOOSTOPTIONAL_HPP 9 | 10 | //------------------------------------------------------------------------------ 11 | /** @file 12 | @brief Provides facilities allowing Variant to interoperate with 13 | boost::optional. */ 14 | //------------------------------------------------------------------------------ 15 | 16 | #include 17 | #include "../api.hpp" 18 | #include "../variant.hpp" 19 | 20 | namespace wamp 21 | { 22 | 23 | //------------------------------------------------------------------------------ 24 | /** Performs the conversion from a variant to a boost::optional. 25 | Users should not use this function directly. Use Variant::to instead. */ 26 | //------------------------------------------------------------------------------ 27 | template 28 | CPPWAMP_API void convert(FromVariantConverter& conv, boost::optional& opt) 29 | { 30 | const auto& variant = conv.variant(); 31 | if (!variant) 32 | opt.reset(); 33 | else 34 | opt = variant.to(); 35 | } 36 | 37 | //------------------------------------------------------------------------------ 38 | /** Performs the conversion from a boost::optional to a variant. 39 | Users should not use this function directly. Use Variant::from instead. */ 40 | //------------------------------------------------------------------------------ 41 | template 42 | CPPWAMP_API void convert(ToVariantConverter& conv, boost::optional& opt) 43 | { 44 | auto& variant = conv.variant(); 45 | if (!opt) 46 | variant = null; 47 | else 48 | variant = Variant::from(*opt); 49 | } 50 | 51 | 52 | //------------------------------------------------------------------------------ 53 | // Comparison Operators 54 | //------------------------------------------------------------------------------ 55 | 56 | /// Compares a variant and a `boost::optional` for equality. 57 | template 58 | CPPWAMP_API bool operator==(const Variant& v, const boost::optional o) 59 | { 60 | return !o ? !v : (v == *o); 61 | } 62 | 63 | /// Compares a variant and a `boost::optional` for equality. 64 | template 65 | CPPWAMP_API bool operator==(const boost::optional o, const Variant& v) 66 | { 67 | return v == o; 68 | } 69 | 70 | /// Compares a variant and a `boost::optional` for inequality. 71 | template 72 | CPPWAMP_API bool operator!=(const Variant& v, const boost::optional o) 73 | { 74 | return !o ? !!v : (v != *o); 75 | } 76 | 77 | /// Compares a variant and a `boost::optional` for inequality. 78 | template 79 | CPPWAMP_API bool operator!=(const boost::optional o, const Variant& v) 80 | { 81 | return v != o; 82 | } 83 | 84 | } // namespace wamp 85 | 86 | #endif // CPPWAMP_TYPES_BOOSTOPTIONAL_HPP 87 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/types/optional.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_TYPES_OPTIONAL_HPP 8 | #define CPPWAMP_TYPES_OPTIONAL_HPP 9 | 10 | //------------------------------------------------------------------------------ 11 | /** @file 12 | @brief Provides facilities allowing Variant to interoperate with 13 | std::optional. */ 14 | //------------------------------------------------------------------------------ 15 | 16 | #include 17 | #include "../api.hpp" 18 | #include "../variant.hpp" 19 | 20 | namespace wamp 21 | { 22 | 23 | //------------------------------------------------------------------------------ 24 | /** Performs the conversion from a variant to a std::optional. 25 | Users should not use this function directly. Use Variant::to instead. */ 26 | //------------------------------------------------------------------------------ 27 | template 28 | CPPWAMP_API void convert(FromVariantConverter& conv, std::optional& opt) 29 | { 30 | const auto& variant = conv.variant(); 31 | if (!variant) 32 | opt.reset(); 33 | else 34 | opt = variant.to(); 35 | } 36 | 37 | //------------------------------------------------------------------------------ 38 | /** Performs the conversion from a std::optional to a variant. 39 | Users should not use this function directly. Use Variant::from instead. */ 40 | //------------------------------------------------------------------------------ 41 | template 42 | CPPWAMP_API void convert(ToVariantConverter& conv, std::optional& opt) 43 | { 44 | auto& variant = conv.variant(); 45 | if (!opt) 46 | variant = null; 47 | else 48 | variant = Variant::from(*opt); 49 | } 50 | 51 | 52 | //------------------------------------------------------------------------------ 53 | // Comparison Operators 54 | //------------------------------------------------------------------------------ 55 | 56 | /// Compares a variant and a `std::optional` for equality. 57 | template 58 | CPPWAMP_API bool operator==(const Variant& v, const std::optional o) 59 | { 60 | return !o ? !v : (v == *o); 61 | } 62 | 63 | /// Compares a variant and a `std::optional` for equality. 64 | template 65 | CPPWAMP_API bool operator==(const std::optional o, const Variant& v) 66 | { 67 | return v == o; 68 | } 69 | 70 | /// Compares a variant and a `std::optional` for inequality. 71 | template 72 | CPPWAMP_API bool operator!=(const Variant& v, const std::optional o) 73 | { 74 | return !o ? !!v : (v != *o); 75 | } 76 | 77 | /// Compares a variant and a `std::optional` for inequality. 78 | template 79 | CPPWAMP_API bool operator!=(const std::optional o, const Variant& v) 80 | { 81 | return v != o; 82 | } 83 | 84 | } // namespace wamp 85 | 86 | #endif // CPPWAMP_TYPES_OPTIONAL_HPP 87 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/types/set.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_TYPES_SET_HPP 8 | #define CPPWAMP_TYPES_SET_HPP 9 | 10 | //------------------------------------------------------------------------------ 11 | /** @file 12 | @brief Provides facilities allowing Variant to interoperate with std::set. */ 13 | //------------------------------------------------------------------------------ 14 | 15 | #include 16 | #include "../api.hpp" 17 | #include "../error.hpp" 18 | #include "../variant.hpp" 19 | 20 | namespace wamp 21 | { 22 | 23 | //------------------------------------------------------------------------------ 24 | /** Performs the conversion from an array variant to a `std::set`. 25 | Users should not use this function directly. Use Variant::to instead. */ 26 | //------------------------------------------------------------------------------ 27 | template 28 | void CPPWAMP_API convert(FromVariantConverter& conv, std::set& set) 29 | { 30 | const auto& variant = conv.variant(); 31 | if (!variant.is()) 32 | { 33 | throw error::Conversion("Attempting to convert non-array variant " 34 | "to std::set"); 35 | } 36 | 37 | std::set newSet; 38 | const auto& array = variant.as(); 39 | for (Array::size_type i=0; i()); 44 | } 45 | catch (const error::Conversion& e) 46 | { 47 | std::string msg = e.what(); 48 | msg += " (for element #" + std::to_string(i) + ")"; 49 | throw error::Conversion(msg); 50 | } 51 | } 52 | set = std::move(newSet); 53 | } 54 | 55 | //------------------------------------------------------------------------------ 56 | /** Performs the conversion from a `std::set` to an array variant. 57 | Users should not use this function directly. Use Variant::from instead. */ 58 | //------------------------------------------------------------------------------ 59 | template 60 | void CPPWAMP_API convert(ToVariantConverter& conv, std::set& set) 61 | { 62 | Array array; 63 | for (const auto& elem: set) 64 | { 65 | array.emplace_back(Variant::from(elem)); 66 | } 67 | conv.variant() = std::move(array); 68 | } 69 | 70 | } // namespace wamp 71 | 72 | #endif // CPPWAMP_TYPES_SET_HPP 73 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/types/unorderedmap.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_TYPES_UNORDEREDMAP_HPP 8 | #define CPPWAMP_TYPES_UNORDEREDMAP_HPP 9 | 10 | //------------------------------------------------------------------------------ 11 | /** @file 12 | @brief Provides facilities allowing Variant to interoperate with 13 | std::unordered_map. */ 14 | //------------------------------------------------------------------------------ 15 | 16 | #include 17 | #include 18 | #include "../api.hpp" 19 | #include "../error.hpp" 20 | #include "../variant.hpp" 21 | 22 | namespace wamp 23 | { 24 | 25 | //------------------------------------------------------------------------------ 26 | /** Performs the conversion from an object variant to a `std::unordered_map`. 27 | Users should not use this function directly. Use Variant::to instead. */ 28 | //------------------------------------------------------------------------------ 29 | template 30 | void CPPWAMP_API convert(FromVariantConverter& conv, 31 | std::unordered_map& map) 32 | { 33 | const auto& variant = conv.variant(); 34 | if (!variant.is()) 35 | { 36 | throw error::Conversion("Attempting to convert non-object variant " 37 | "to std::unordered_map"); 38 | } 39 | 40 | std::unordered_map newMap; 41 | for (const auto& kv: variant.as()) 42 | { 43 | try 44 | { 45 | newMap.emplace(kv.first, kv.second.to()); 46 | } 47 | catch (const error::Conversion& e) 48 | { 49 | std::string msg = e.what(); 50 | msg += " (for variant member \"" + kv.first + "\")"; 51 | throw error::Conversion(msg); 52 | } 53 | } 54 | map = std::move(newMap); 55 | } 56 | 57 | //------------------------------------------------------------------------------ 58 | /** Performs the conversion from a `std::unordered_map` to an object variant. 59 | Users should not use this function directly. Use Variant::from instead. */ 60 | //------------------------------------------------------------------------------ 61 | template 62 | void CPPWAMP_API convert(ToVariantConverter& conv, 63 | std::unordered_map& map) 64 | { 65 | Object obj; 66 | for (const auto& kv: map) 67 | { 68 | obj.emplace(kv.first, Variant::from(kv.second)); 69 | } 70 | conv.variant() = std::move(obj); 71 | } 72 | 73 | 74 | } 75 | 76 | #endif // CPPWAMP_TYPES_UNORDEREDMAP_HPP 77 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/types/unorderedset.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_TYPES_UNORDEREDSET_HPP 8 | #define CPPWAMP_TYPES_UNORDEREDSET_HPP 9 | 10 | //------------------------------------------------------------------------------ 11 | /** @file 12 | @brief Provides facilities allowing Variant to interoperate 13 | with std::unordered_set. */ 14 | //------------------------------------------------------------------------------ 15 | 16 | #include 17 | #include "../api.hpp" 18 | #include "../error.hpp" 19 | #include "../variant.hpp" 20 | 21 | namespace wamp 22 | { 23 | 24 | //------------------------------------------------------------------------------ 25 | /** Performs the conversion from an array variant to a `std::unordered_set`. 26 | Users should not use this function directly. Use Variant::to instead. */ 27 | //------------------------------------------------------------------------------ 28 | template 29 | CPPWAMP_API void convert(FromVariantConverter& conv, std::unordered_set& set) 30 | { 31 | const auto& variant = conv.variant(); 32 | if (!variant.is()) 33 | { 34 | throw error::Conversion("Attempting to convert non-array variant " 35 | "to std::unordered_set"); 36 | } 37 | 38 | std::unordered_set newSet; 39 | const auto& array = variant.as(); 40 | for (Array::size_type i=0; i()); 45 | } 46 | catch (const error::Conversion& e) 47 | { 48 | std::string msg = e.what(); 49 | msg += " (for element #" + std::to_string(i) + ")"; 50 | throw error::Conversion(msg); 51 | } 52 | } 53 | set = std::move(newSet); 54 | } 55 | 56 | //------------------------------------------------------------------------------ 57 | /** Performs the conversion from a `std::unordered_set` to an array variant. 58 | Users should not use this function directly. Use Variant::from instead. */ 59 | //------------------------------------------------------------------------------ 60 | template 61 | CPPWAMP_API void convert(ToVariantConverter& conv, std::unordered_set& set) 62 | { 63 | Array array; 64 | for (const auto& elem: set) 65 | { 66 | array.emplace_back(Variant::from(elem)); 67 | } 68 | conv.variant() = std::move(array); 69 | } 70 | 71 | } // namespace wamp 72 | 73 | #endif // CPPWAMP_TYPES_UNORDEREDSET_HPP 74 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/uds.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_UDS_HPP 8 | #define CPPWAMP_UDS_HPP 9 | 10 | //------------------------------------------------------------------------------ 11 | /** @file 12 | @brief Contains facilities for creating Unix domain socket 13 | transport connectors. */ 14 | //------------------------------------------------------------------------------ 15 | 16 | #include 17 | #include 18 | #include "api.hpp" 19 | #include "asiodefs.hpp" 20 | #include "connector.hpp" 21 | #include "udspath.hpp" 22 | #include "traits.hpp" 23 | 24 | namespace wamp 25 | { 26 | 27 | //------------------------------------------------------------------------------ 28 | /** Connector specialization that establishes a Unix domain socket transport. 29 | Users do not need to use this class directly and should use 30 | ConnectionWish instead. */ 31 | //------------------------------------------------------------------------------ 32 | template <> 33 | class CPPWAMP_API Connector : public Connecting 34 | { 35 | public: 36 | /** Type containing the transport settings. */ 37 | using Settings = UdsPath; 38 | 39 | /** Constructor. */ 40 | Connector(IoStrand i, Settings s, int codecId); 41 | 42 | /** Destructor. */ 43 | ~Connector(); 44 | 45 | /** Starts establishing the transport connection, emitting a 46 | Transportable::Ptr via the given handler if successful. */ 47 | void establish(Handler&& handler) override; 48 | 49 | /** Cancels transport connection in progress, emitting an error code 50 | via the handler passed to the establish method. */ 51 | void cancel() override; 52 | 53 | private: 54 | struct Impl; 55 | std::unique_ptr impl_; 56 | }; 57 | 58 | //------------------------------------------------------------------------------ 59 | /** Creates a LegacyConnector that can establish a Unix domain socket transport. 60 | 61 | This overload takes an executor that is convertible to 62 | the boost::asio::any_io_executor polymorphic wrapper. 63 | 64 | @deprecated Use wamp::ConnectionWish instead 65 | @relates UdsPath 66 | @returns a `std::shared_ptr` to a Connecting 67 | @tparam TFormat The serialization format to use over this transport. 68 | @see Connecting, Json, Msgpack */ 69 | //------------------------------------------------------------------------------ 70 | template 71 | CPPWAMP_API LegacyConnector connector( 72 | AnyIoExecutor e, ///< The executor to be used by the transport. 73 | UdsPath p ///< Unix domain socket path and other socket options. 74 | ) 75 | { 76 | return LegacyConnector{std::move(e), std::move(p), TFormat{}}; 77 | } 78 | 79 | //------------------------------------------------------------------------------ 80 | /** Creates a Connector that can establish a TCP raw socket transport. 81 | 82 | Only participates in overload resolution when 83 | `isExecutionContext() == true` 84 | 85 | @deprecated Use wamp::ConnectionWish instead 86 | @relates TcpHost 87 | @returns a `std::shared_ptr` to a Connecting 88 | @tparam TFormat The serialization formatto use over this transport. 89 | @tparam TExecutionContext The given execution context type (deduced). 90 | @see Connecting, Json, Msgpack */ 91 | //------------------------------------------------------------------------------ 92 | template 93 | CPPWAMP_ENABLED_TYPE(LegacyConnector, isExecutionContext()) 94 | connector( 95 | TExecutionContext& context, /**< The I/O context containing the executor 96 | to be used by the transport. */ 97 | UdsPath path ///< Unix domain socket path and other socket options. 98 | ) 99 | { 100 | return connector(context.get_executor(), std::move(path)); 101 | } 102 | 103 | } // namespace wamp 104 | 105 | #ifndef CPPWAMP_COMPILED_LIB 106 | #include "internal/uds.ipp" 107 | #endif 108 | 109 | #endif // CPPWAMP_UDS_HPP 110 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/udsprotocol.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_UDSPROTOCOL_HPP 8 | #define CPPWAMP_UDSPROTOCOL_HPP 9 | 10 | //------------------------------------------------------------------------------ 11 | /** @file 12 | @brief Contains basic Unix Domain Socket protocol facilities. */ 13 | //------------------------------------------------------------------------------ 14 | 15 | #include "api.hpp" 16 | #include "config.hpp" 17 | #include "internal/socketoptions.hpp" 18 | 19 | // Forward declaration 20 | namespace boost { namespace asio { namespace local { class stream_protocol; }}} 21 | 22 | namespace wamp 23 | { 24 | 25 | //------------------------------------------------------------------------------ 26 | /** Protocol tag type associated with Unix Domain Sockets transport. */ 27 | //------------------------------------------------------------------------------ 28 | struct CPPWAMP_API Uds 29 | { 30 | constexpr Uds() = default; 31 | }; 32 | 33 | 34 | namespace internal { class UdsOpener; } // Forward declaration 35 | 36 | //------------------------------------------------------------------------------ 37 | /** Contains options for the UNIX domain socket transport. 38 | @note Support for these options depends on the the operating system. 39 | Some may not even make sense for a UNIX domain socket. This library 40 | aims not to be opinionated about which socket options are irrelevant 41 | so they are all made available. 42 | @see https://man7.org/linux/man-pages/man7/socket.7.html 43 | @see https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-setsockopt 44 | @see https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/setsockopt.2.html */ 45 | //------------------------------------------------------------------------------ 46 | class CPPWAMP_API UdsOptions 47 | { 48 | public: 49 | /** Adds the SO_BROADCAST socket option. */ 50 | UdsOptions& withBroadcast(bool enabled = true); 51 | 52 | /** Adds the SO_DEBUG socket option. */ 53 | UdsOptions& withDebug(bool enabled = true); 54 | 55 | /** Adds the SO_DONTROUTE socket option. */ 56 | UdsOptions& withDoNotRoute(bool enabled = true); 57 | 58 | /** Adds the SO_KEEPALIVE socket option. */ 59 | UdsOptions& withKeepAlive(bool enabled = true); 60 | 61 | /** Adds the SO_LINGER socket option. */ 62 | UdsOptions& withLinger(bool enabled, int timeout); 63 | 64 | /** Adds the SO_OOBINLINE socket option. */ 65 | UdsOptions& withOutOfBandInline(bool enabled); 66 | 67 | /** Adds the SO_RCVBUF socket option. */ 68 | UdsOptions& withReceiveBufferSize(int size); 69 | 70 | /** Adds the SO_RCVLOWAT socket option. */ 71 | UdsOptions& withReceiveLowWatermark(int size); 72 | 73 | /** Adds the SO_REUSEADDR socket option. */ 74 | UdsOptions& withReuseAddress(bool enabled = true); 75 | 76 | /** Adds the SO_SNDBUF socket option. */ 77 | UdsOptions& withSendBufferSize(int size); 78 | 79 | /** Adds the SO_SNDLOWAT socket option. */ 80 | UdsOptions& withSendLowWatermark(int size); 81 | 82 | private: 83 | template 84 | UdsOptions& set(TArgs... args); 85 | 86 | template void applyTo(TSocket& socket) const; 87 | 88 | internal::SocketOptionList options_; 89 | 90 | friend class internal::UdsOpener; 91 | 92 | /* Implementation note: Explicit template instantiation does not seem 93 | to play nice with CRTP, so it was not feasible to factor out the 94 | commonality with TcpOptions as a mixin (not without giving up the 95 | fluent API). */ 96 | }; 97 | 98 | } // namespace wamp 99 | 100 | #ifndef CPPWAMP_COMPILED_LIB 101 | #include "./internal/udsprotocol.ipp" 102 | #endif 103 | 104 | #endif // CPPWAMP_UDSPROTOCOL_HPP 105 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/variantdefs.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_VARIANTDEFS_HPP 8 | #define CPPWAMP_VARIANTDEFS_HPP 9 | 10 | //------------------------------------------------------------------------------ 11 | /** @file 12 | @brief Contains fundamental type definitions related to Variant. */ 13 | //------------------------------------------------------------------------------ 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | namespace wamp 21 | { 22 | 23 | //------------------------------------------------------------------------------ 24 | /** Integer ID used to indicate the current dynamic type of a `Variant`. */ 25 | //------------------------------------------------------------------------------ 26 | enum class TypeId : uint8_t 27 | { 28 | null, ///< For Variant::Null 29 | boolean, ///< For Variant::Bool 30 | integer, ///< For Variant::Int 31 | uint, ///< For Variant::UInt 32 | real, ///< For Variant::Real 33 | string, ///< For Variant::String 34 | blob, ///< For Variant::Blob 35 | array, ///< For Variant::Array 36 | object ///< For Variant::Object 37 | }; 38 | 39 | // Forward declaration 40 | class Variant; 41 | 42 | //------------------------------------------------------------------------------ 43 | /** @name Variant bound types */ 44 | //------------------------------------------------------------------------------ 45 | /// @{ 46 | using Bool = bool; ///< Variant bound type for boolean values 47 | using Int = std::int64_t; ///< Variant bound type for signed integers 48 | using UInt = std::uint64_t; ///< Variant bound type for unsigned integers 49 | using Real = double; ///< Variant bound type for floating-point numbers 50 | using String = std::string; ///< Variant bound type for text strings 51 | using Array = std::vector; ///< Variant bound type for arrays of variants 52 | using Object = std::map; ///< Variant bound type for maps of variants 53 | /// @} 54 | 55 | } // namespace wamp 56 | 57 | #endif // CPPWAMP_VARIANTDEFS_HPP 58 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/version.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_VERSION_HPP 8 | #define CPPWAMP_VERSION_HPP 9 | 10 | #include 11 | #include "api.hpp" 12 | 13 | //------------------------------------------------------------------------------ 14 | /** @file 15 | @brief Contains version information on the CppWAMP library. */ 16 | //------------------------------------------------------------------------------ 17 | 18 | /// Major version with incompatible API changes 19 | #define CPPWAMP_MAJOR_VERSION 0 20 | 21 | /// Minor version with functionality added in a backwards-compatible manner. 22 | #define CPPWAMP_MINOR_VERSION 11 23 | 24 | /// Patch version for backwards-compatible bug fixes. 25 | #define CPPWAMP_PATCH_VERSION 1 26 | 27 | /// Integer version number, computed as `(major*10000) + (minor*100) + patch` 28 | #define CPPWAMP_VERSION \ 29 | (CPPWAMP_MAJOR_VERSION * 10000 + \ 30 | CPPWAMP_MINOR_VERSION * 100 + \ 31 | CPPWAMP_PATCH_VERSION) 32 | 33 | namespace wamp 34 | { 35 | 36 | //------------------------------------------------------------------------------ 37 | /** Bundles the major, minor, and patch version numbers. 38 | @see CPPWAMP_MAJOR_VERSION 39 | @see CPPWAMP_MINOR_VERSION 40 | @see CPPWAMP_PATCH_VERSION 41 | @see CPPWAMP_VERSION */ 42 | //------------------------------------------------------------------------------ 43 | struct CPPWAMP_API Version 44 | { 45 | /// Major version with incompatible API changes 46 | int major; 47 | 48 | /// Minor version with functionality added in a backwards-compatible manner. 49 | int minor; 50 | 51 | /// Patch version for backwards-compatible bug fixes. 52 | int patch; 53 | 54 | /** Obtains the current version of the library as 55 | major/minor/patch parts. */ 56 | static Version parts(); 57 | 58 | /** Obtains an integer representation of the library's current version. */ 59 | static int integer(); 60 | 61 | /** Obtains the library's current version as a string. */ 62 | static std::string toString(); 63 | 64 | /** Obtains the agent string sent in `HELLO` messages. */ 65 | static std::string agentString(); 66 | }; 67 | 68 | } // namespace wamp 69 | 70 | #ifndef CPPWAMP_COMPILED_LIB 71 | #include "./internal/version.ipp" 72 | #endif 73 | 74 | #endif // CPPWAMP_VERSION_HPP 75 | -------------------------------------------------------------------------------- /cppwamp/include/cppwamp/wampdefs.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2018, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_WAMPDEFS_HPP 8 | #define CPPWAMP_WAMPDEFS_HPP 9 | 10 | #include "config.hpp" 11 | 12 | //------------------------------------------------------------------------------ 13 | /** @file 14 | @brief Contains type definitions related to WAMP IDs and sessions. */ 15 | //------------------------------------------------------------------------------ 16 | 17 | #include 18 | 19 | namespace wamp 20 | { 21 | 22 | using SessionId = int64_t; ///< Ephemeral ID associated with a WAMP session 23 | using RequestId = int64_t; ///< Ephemeral ID associated with a WAMP request 24 | using SubscriptionId = int64_t; ///< Ephemeral ID associated with an topic subscription 25 | using PublicationId = int64_t; ///< Ephemeral ID associated with an event publication 26 | using RegistrationId = int64_t; ///< Ephemeral ID associated with an RPC registration 27 | 28 | ///< Obtains the value representing a blank RequestId. 29 | constexpr RequestId nullRequestId() {return 0;} 30 | 31 | //------------------------------------------------------------------------------ 32 | /** Enumerates the possible states that a client or router session can be in. */ 33 | //------------------------------------------------------------------------------ 34 | enum class SessionState 35 | { 36 | disconnected, ///< The transport connection is not yet established 37 | connecting, ///< Transport connection is in progress 38 | closed, ///< Transport connected, but WAMP session is closed 39 | establishing, ///< WAMP session establishment is in progress 40 | authenticating, ///< WAMP authentication is in progress 41 | established, ///< WAMP session is established 42 | shuttingDown, ///< WAMP session is closing 43 | failed ///< WAMP session or transport connection has failed 44 | }; 45 | 46 | //------------------------------------------------------------------------------ 47 | /** Enumerates the possible call cancelling modes. */ 48 | //------------------------------------------------------------------------------ 49 | enum class CallCancelMode 50 | { 51 | kill, ///< INTERRUPT sent to callee; RESULT or ERROR returned, depending on callee 52 | killNoWait, ///< INTERRUPT sent to callee; router immediately returns ERROR 53 | skip ///< No INTERRUPT sent to callee; router immediately returns ERROR 54 | }; 55 | 56 | //------------------------------------------------------------------------------ 57 | /** Alias to CallCancelMode kept for backward compatibility. 58 | @deprecated Use CallCancelMode instead. */ 59 | //------------------------------------------------------------------------------ 60 | using CancelMode CPPWAMP_DEPRECATED = CallCancelMode; 61 | 62 | } // namespace wamp 63 | 64 | #endif // CPPWAMP_WAMPDEFS_HPP 65 | -------------------------------------------------------------------------------- /cppwamp/src/cppwamp.cpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #ifndef CPPWAMP_COMPILED_LIB 8 | #error CPPWAMP_COMPILED_LIB must be defined to use this source file 9 | #endif 10 | 11 | #include 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | #if CPPWAMP_HAS_UNIX_DOMAIN_SOCKETS 35 | #include 36 | #include 37 | #include 38 | #endif 39 | -------------------------------------------------------------------------------- /doc/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | # Distributed under the Boost Software License, Version 1.0. 4 | # https://www.boost.org/LICENSE_1_0.txt 5 | #------------------------------------------------------------------------------- 6 | 7 | set(CPPWAMP_DOCUMENTS 8 | Doxyfile.in 9 | cppwamp.dox 10 | registrations.dox 11 | subscriptions.dox 12 | tutorial-variants.dox 13 | tutorial-connections.dox 14 | tutorial-sessions.dox 15 | tutorial-errors.dox 16 | tutorial-rpc.dox 17 | tutorial-pubsub.dox 18 | tutorial-callbacks.dox 19 | tutorial-conversions.dox 20 | typerequirements.dox 21 | ) 22 | 23 | find_package(Doxygen) 24 | if(DOXYGEN_FOUND AND NOT CPPWAMP_OPT_HEADERS_ONLY) 25 | configure_file( 26 | ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in 27 | ${CppWAMP_BINARY_DIR}/Doxyfile 28 | @ONLY) 29 | configure_file( 30 | ${CMAKE_CURRENT_SOURCE_DIR}/index.html 31 | ${CppWAMP_BINARY_DIR}/doc/index.html 32 | COPYONLY) 33 | add_custom_target(cppwamp-doc 34 | COMMAND ${DOXYGEN_EXECUTABLE} ${CppWAMP_BINARY_DIR}/Doxyfile 35 | WORKING_DIRECTORY ${CppWAMP_SOURCE_DIR} 36 | SOURCES ${CPPWAMP_DOCUMENTS} 37 | COMMENT "Generating API documentation with Doxygen" 38 | VERBATIM) 39 | else() 40 | add_custom_target(cppwamp-doc-sources 41 | COMMAND "" 42 | SOURCES ${CPPWAMP_DOCUMENTS}) 43 | endif() 44 | -------------------------------------------------------------------------------- /doc/architecture.vpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecorm/cppwamp/17722971ae6e3f2e6eb60eb627811eafbbe78f17/doc/architecture.vpp -------------------------------------------------------------------------------- /doc/cppwamp.dox: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | /** 8 | @namespace wamp 9 | Namespace for all identifiers defined by the CppWAMP library. 10 | */ 11 | 12 | /** 13 | @namespace wamp::error 14 | Namespace containing exception types thrown by the CppWAMP library. 15 | */ 16 | 17 | 18 | /** 19 | @mainpage 20 | This is the reference documentation for the CppWAMP library. CppWAMP is a C++11 21 | implementation of the [WAMP][wamp] protocol's client roles (caller, callee, 22 | publisher, and subscriber). 23 | 24 | The CppWAMP open-source project is hosted at 25 | [https://github.com/ecorm/cppwamp][cppwamp], and is released under the 26 | [Boost License][license]. 27 | 28 | For an **overview of the library's classes**, structs, typedefs, enums, etc., 29 | consult the @ref wamp Namespace Reference. 30 | 31 | For a **tutorial-style overview**, please consult the @ref Tutorial page. 32 | 33 | ## Examples 34 | 35 | ### Using Stackful Coroutines 36 | 37 | - @ref examples/chat/main.cpp 38 | - @ref examples/timeclient/main.cpp 39 | - @ref examples/timeservice/main.cpp 40 | 41 | ### Using C++20 Coroutines 42 | - @ref examples/coro20timeclient/main.cpp 43 | - @ref examples/coro20timeservice/main.cpp 44 | 45 | ### Using Asio Stackless Coroutines 46 | - @ref examples/stacklesstimeclient/main.cpp 47 | - @ref examples/stacklesstimeservice/main.cpp 48 | 49 | ### Using Callback Handler Functions 50 | - @ref examples/asynctimeclient/main.cpp 51 | - @ref examples/asynctimeservice/main.cpp 52 | 53 | ### Using std::future 54 | - @ref examples/futuretimeclient/main.cpp 55 | - @ref examples/futuretimeservice/main.cpp 56 | 57 | For a brief **architectural overview** of the library, see @ref Architecture. 58 | 59 | - - - 60 | 61 | Copyright © 2014-2022 Butterfly Energy Systems 62 | 63 | @include LICENSE_1_0.txt 64 | 65 | [wamp]: http://wamp.ws/ 66 | [cppwamp]: https://github.com/ecorm/cppwamp 67 | [license]: http://www.boost.org/users/license.html 68 | */ 69 | 70 | /** 71 | @page Tutorial Tutorial 72 | 73 | 1. @subpage VariantsTutorial – A `Variant` mimics a dynamic Javascript variable. 74 | 2. @subpage ConnectionsTutorial – How to specify the desired transport and serialization. 75 | 3. @subpage SessionsTutorial – How to establish a WAMP session using stackful coroutines. 76 | 4. @subpage ErrorsTutorial – How to handle runtime errors emitted by `Session`. 77 | 5. @subpage RpcTutorial – How to register and invoke remote procedure calls. 78 | 6. @subpage PubSubTutorial – How to publish and subscribe to event topics. 79 | 7. @subpage AsyncCallbacksTutorial – How to use asynchronous callbacks with `Session`. 80 | 8. @subpage ConversionsTutorial – How to convert `Variant` to/from custom types. 81 | */ 82 | 83 | /** 84 | @page Architecture Architectural Overview 85 | 86 | @tableofcontents 87 | 88 | @section Layers Layers 89 | ![](layers.svg) 90 | 91 | @section Client_API_UML Client API 92 | ![](client_api.svg) 93 | 94 | @section Subriptions_UML Subscriptions 95 | ![](subscriptions.svg) 96 | 97 | @section Registrations_UML Registrations 98 | ![](registrations.svg) 99 | 100 | @section Messaging_UML WAMP Messaging 101 | ![](messaging.svg) 102 | 103 | @section Transport_Establishment_UML Transport Establishment 104 | ![](transport.svg) 105 | */ 106 | 107 | // Examples 108 | /** 109 | @example examples/chat/main.cpp 110 | @example examples/timeservice/main.cpp 111 | @example examples/timeclient/main.cpp 112 | @example examples/asynctimeservice/main.cpp 113 | @example examples/asynctimeclient/main.cpp 114 | @example examples/futuretimeservice/main.cpp 115 | @example examples/futuretimeclient/main.cpp 116 | @example examples/stacklesstimeservice/main.cpp 117 | @example examples/stacklesstimeclient/main.cpp 118 | @example examples/coro20timeservice/main.cpp 119 | @example examples/coro20timeclient/main.cpp 120 | */ 121 | -------------------------------------------------------------------------------- /doc/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 |

You will be redirected automatically, otherwise please 7 | click here.

8 | 9 | 10 | -------------------------------------------------------------------------------- /examples/.crossbar/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": 2, 3 | "controller": {}, 4 | "workers": [ 5 | { 6 | "type": "router", 7 | "realms": [ 8 | { 9 | "name": "cppwamp.demo.chat", 10 | "roles": [ 11 | { 12 | "name": "anonymous", 13 | "permissions": [ 14 | { 15 | "uri": "", 16 | "match": "prefix", 17 | "allow": { 18 | "call": true, 19 | "register": true, 20 | "publish": true, 21 | "subscribe": true 22 | }, 23 | "disclose": { 24 | "caller": false, 25 | "publisher": false 26 | }, 27 | "cache": true 28 | } 29 | ] 30 | } 31 | ] 32 | }, 33 | { 34 | "name": "cppwamp.demo.time", 35 | "roles": [ 36 | { 37 | "name": "anonymous", 38 | "permissions": [ 39 | { 40 | "uri": "", 41 | "match": "prefix", 42 | "allow": { 43 | "call": true, 44 | "register": true, 45 | "publish": true, 46 | "subscribe": true 47 | }, 48 | "disclose": { 49 | "caller": false, 50 | "publisher": false 51 | }, 52 | "cache": true 53 | } 54 | ] 55 | } 56 | ] 57 | } 58 | ], 59 | "transports": [ 60 | { 61 | "type": "rawsocket", 62 | "endpoint": { 63 | "type": "tcp", 64 | "port": 12345 65 | }, 66 | "serializers": [ 67 | "json" 68 | ], 69 | "debug": true 70 | }, 71 | { 72 | "type": "rawsocket", 73 | "endpoint": { 74 | "type": "tcp", 75 | "port": 54321 76 | }, 77 | "serializers": [ 78 | "json" 79 | ], 80 | "debug": true 81 | } 82 | ] 83 | } 84 | ] 85 | } 86 | -------------------------------------------------------------------------------- /examples/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | # Distributed under the Boost Software License, Version 1.0. 4 | # https://www.boost.org/LICENSE_1_0.txt 5 | #------------------------------------------------------------------------------- 6 | 7 | add_subdirectory(asynctimeclient) 8 | add_subdirectory(asynctimeservice) 9 | add_subdirectory(futuretimeclient) 10 | add_subdirectory(futuretimeservice) 11 | 12 | # Convenience target that groups all examples 13 | add_custom_target(cppwamp_examples) 14 | add_dependencies(cppwamp_examples 15 | cppwamp-example-asynctimeclient 16 | cppwamp-example-asynctimeservice 17 | cppwamp-example-futuretimeclient 18 | cppwamp-example-futuretimeservice) 19 | 20 | if(CPPWAMP_OPT_WITH_CORO) 21 | add_subdirectory(chat) 22 | add_subdirectory(timeclient) 23 | add_subdirectory(timeservice) 24 | add_subdirectory(stacklesstimeclient) 25 | add_subdirectory(stacklesstimeservice) 26 | add_dependencies(cppwamp_examples 27 | cppwamp-example-chat 28 | cppwamp-example-timeclient 29 | cppwamp-example-timeservice 30 | cppwamp-example-stacklesstimeclient 31 | cppwamp-example-stacklesstimeservice) 32 | endif() 33 | 34 | if(CPPWAMP_OPT_WITH_CORO20) 35 | add_subdirectory(coro20timeclient) 36 | add_subdirectory(coro20timeservice) 37 | add_dependencies(cppwamp-example-coro20timeclient 38 | cppwamp-example-coro20timeservice) 39 | endif() 40 | 41 | # Copy Crossbar node configuration to build directory 42 | configure_file( 43 | ${CMAKE_CURRENT_SOURCE_DIR}/.crossbar/config.json 44 | ${CMAKE_CURRENT_BINARY_DIR}/.crossbar/config.json 45 | COPYONLY) 46 | -------------------------------------------------------------------------------- /examples/asynctimeclient/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # Copyright Butterfly Energy Systems 2022. 3 | # Distributed under the Boost Software License, Version 1.0. 4 | # https://www.boost.org/LICENSE_1_0.txt 5 | #------------------------------------------------------------------------------- 6 | 7 | add_executable(cppwamp-example-asynctimeclient main.cpp) 8 | 9 | target_link_libraries(cppwamp-example-asynctimeclient 10 | PRIVATE CppWAMP::core) 11 | -------------------------------------------------------------------------------- /examples/asynctimeclient/main.cpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | //****************************************************************************** 8 | // Example WAMP service consumer app using callback handler functions. 9 | //****************************************************************************** 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | const std::string realm = "cppwamp.demo.time"; 20 | const std::string address = "localhost"; 21 | const short port = 12345u; 22 | 23 | //------------------------------------------------------------------------------ 24 | namespace wamp 25 | { 26 | // Convert a std::tm to/from an object variant. 27 | template 28 | void convert(TConverter& conv, std::tm& t) 29 | { 30 | conv ("sec", t.tm_sec) 31 | ("min", t.tm_min) 32 | ("hour", t.tm_hour) 33 | ("mday", t.tm_mday) 34 | ("mon", t.tm_mon) 35 | ("year", t.tm_year) 36 | ("wday", t.tm_wday) 37 | ("yday", t.tm_yday) 38 | ("isdst", t.tm_isdst); 39 | } 40 | } 41 | 42 | //------------------------------------------------------------------------------ 43 | class TimeClient : public std::enable_shared_from_this 44 | { 45 | public: 46 | static std::shared_ptr create(wamp::AnyIoExecutor exec) 47 | { 48 | return std::shared_ptr(new TimeClient(std::move(exec))); 49 | } 50 | 51 | void start(wamp::ConnectionWish where) 52 | { 53 | auto self = shared_from_this(); 54 | session_.connect( 55 | std::move(where), 56 | [this, self](wamp::ErrorOr index) 57 | { 58 | index.value(); // Throws if connect failed 59 | join(); 60 | }); 61 | } 62 | 63 | private: 64 | TimeClient(wamp::AnyIoExecutor exec) 65 | : session_(std::move(exec)) 66 | {} 67 | 68 | static void onTimeTick(std::tm time) 69 | { 70 | std::cout << "The current time is: " << std::asctime(&time) << "\n"; 71 | } 72 | 73 | void join() 74 | { 75 | auto self = shared_from_this(); 76 | session_.join( 77 | wamp::Realm(realm), 78 | [this, self](wamp::ErrorOr info) 79 | { 80 | info.value(); // Throws if join failed 81 | getTime(); 82 | }); 83 | } 84 | 85 | void getTime() 86 | { 87 | auto self = shared_from_this(); 88 | session_.call( 89 | wamp::Rpc("get_time"), 90 | [this, self](wamp::ErrorOr result) 91 | { 92 | // result.value() throws if the call failed 93 | auto time = result.value()[0].to(); 94 | std::cout << "The current time is: " << std::asctime(&time) << "\n"; 95 | subscribe(); 96 | }); 97 | } 98 | 99 | void subscribe() 100 | { 101 | session_.subscribe( 102 | wamp::Topic("time_tick"), 103 | wamp::simpleEvent(&TimeClient::onTimeTick), 104 | [](wamp::ErrorOr sub) 105 | { 106 | sub.value(); // Throws if subscribe failed 107 | }); 108 | } 109 | 110 | wamp::Session session_; 111 | }; 112 | 113 | 114 | //------------------------------------------------------------------------------ 115 | int main() 116 | { 117 | wamp::IoContext ioctx; 118 | auto client = TimeClient::create(ioctx.get_executor()); 119 | client->start(wamp::TcpHost(address, port).withFormat(wamp::json)); 120 | ioctx.run(); 121 | return 0; 122 | } 123 | -------------------------------------------------------------------------------- /examples/asynctimeservice/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # Copyright Butterfly Energy Systems 2022. 3 | # Distributed under the Boost Software License, Version 1.0. 4 | # https://www.boost.org/LICENSE_1_0.txt 5 | #------------------------------------------------------------------------------- 6 | 7 | add_executable(cppwamp-example-asynctimeservice main.cpp) 8 | 9 | target_link_libraries(cppwamp-example-asynctimeservice 10 | PRIVATE CppWAMP::core) 11 | -------------------------------------------------------------------------------- /examples/chat/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | # Distributed under the Boost Software License, Version 1.0. 4 | # https://www.boost.org/LICENSE_1_0.txt 5 | #------------------------------------------------------------------------------- 6 | 7 | add_executable(cppwamp-example-chat main.cpp ) 8 | 9 | target_link_libraries(cppwamp-example-chat 10 | PRIVATE CppWAMP::core CppWAMP::coro-usage) 11 | -------------------------------------------------------------------------------- /examples/coro20timeclient/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # Copyright Butterfly Energy Systems 2022. 3 | # Distributed under the Boost Software License, Version 1.0. 4 | # https://www.boost.org/LICENSE_1_0.txt 5 | #------------------------------------------------------------------------------- 6 | 7 | add_executable(cppwamp-example-coro20timeclient main.cpp) 8 | 9 | target_compile_features(cppwamp-example-coro20timeclient PRIVATE cxx_std_20) 10 | 11 | if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") 12 | target_compile_options(cppwamp-example-coro20timeclient 13 | PRIVATE "-stdlib=libc++" "-fcoroutines-ts") 14 | target_link_options(cppwamp-example-coro20timeclient 15 | PRIVATE "-stdlib=libc++") 16 | target_link_libraries(cppwamp-example-coro20timeclient 17 | PRIVATE CppWAMP::core-headers 18 | "$") 19 | else () 20 | target_compile_options(cppwamp-example-coro20timeclient 21 | PRIVATE "-fcoroutines") 22 | target_link_libraries(cppwamp-example-coro20timeclient 23 | PRIVATE CppWAMP::core) 24 | endif () 25 | -------------------------------------------------------------------------------- /examples/coro20timeclient/main.cpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | //****************************************************************************** 8 | // Example WAMP service consumer app using stackful coroutines. 9 | //****************************************************************************** 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | const std::string realm = "cppwamp.demo.time"; 24 | const std::string address = "localhost"; 25 | const short port = 12345u; 26 | 27 | //------------------------------------------------------------------------------ 28 | namespace wamp 29 | { 30 | // Convert a std::tm to/from an object variant. 31 | template 32 | void convert(TConverter& conv, std::tm& t) 33 | { 34 | conv("sec", t.tm_sec) 35 | ("min", t.tm_min) 36 | ("hour", t.tm_hour) 37 | ("mday", t.tm_mday) 38 | ("mon", t.tm_mon) 39 | ("year", t.tm_year) 40 | ("wday", t.tm_wday) 41 | ("yday", t.tm_yday) 42 | ("isdst", t.tm_isdst); 43 | } 44 | } 45 | 46 | //------------------------------------------------------------------------------ 47 | void onTimeTick(std::tm time) 48 | { 49 | std::cout << "The current time is: " << std::asctime(&time) << "\n"; 50 | } 51 | 52 | //------------------------------------------------------------------------------ 53 | boost::asio::awaitable client(wamp::Session& session, 54 | wamp::ConnectionWish where) 55 | { 56 | using namespace wamp; 57 | using boost::asio::use_awaitable; 58 | 59 | (co_await session.connect(std::move(where), use_awaitable)).value(); 60 | (co_await session.join(Realm(realm), use_awaitable)).value(); 61 | 62 | auto result = (co_await session.call(Rpc("get_time"), 63 | use_awaitable)).value(); 64 | auto time = result[0].to(); 65 | std::cout << "The current time is: " << std::asctime(&time) << "\n"; 66 | 67 | (co_await session.subscribe(Topic("time_tick"), 68 | wamp::simpleEvent(&onTimeTick), 69 | use_awaitable)).value(); 70 | } 71 | 72 | //------------------------------------------------------------------------------ 73 | int main() 74 | { 75 | wamp::IoContext ioctx; 76 | auto tcp = wamp::TcpHost(address, port).withFormat(wamp::json); 77 | wamp::Session session(ioctx); 78 | boost::asio::co_spawn(ioctx, client(session, tcp), boost::asio::detached); 79 | ioctx.run(); 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /examples/coro20timeservice/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # Copyright Butterfly Energy Systems 2022. 3 | # Distributed under the Boost Software License, Version 1.0. 4 | # https://www.boost.org/LICENSE_1_0.txt 5 | #------------------------------------------------------------------------------- 6 | 7 | add_executable(cppwamp-example-coro20timeservice main.cpp) 8 | 9 | target_compile_features(cppwamp-example-coro20timeservice PRIVATE cxx_std_20) 10 | 11 | if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") 12 | target_compile_options(cppwamp-example-coro20timeservice 13 | PRIVATE "-stdlib=libc++" "-fcoroutines-ts") 14 | target_link_options(cppwamp-example-coro20timeservice 15 | PRIVATE "-stdlib=libc++") 16 | target_link_libraries(cppwamp-example-coro20timeservice 17 | PRIVATE CppWAMP::core-headers 18 | "$") 19 | else () 20 | target_compile_options(cppwamp-example-coro20timeservice 21 | PRIVATE "-fcoroutines") 22 | target_link_libraries(cppwamp-example-coro20timeservice 23 | PRIVATE CppWAMP::core) 24 | endif () 25 | -------------------------------------------------------------------------------- /examples/coro20timeservice/main.cpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | //****************************************************************************** 8 | // Example WAMP service provider app using C++20 coroutines. 9 | //****************************************************************************** 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | const std::string realm = "cppwamp.demo.time"; 25 | const std::string address = "localhost"; 26 | const short port = 54321u; 27 | 28 | //------------------------------------------------------------------------------ 29 | namespace wamp 30 | { 31 | // Convert a std::tm to/from an object variant. 32 | template 33 | void convert(TConverter& conv, std::tm& t) 34 | { 35 | conv ("sec", t.tm_sec) 36 | ("min", t.tm_min) 37 | ("hour", t.tm_hour) 38 | ("mday", t.tm_mday) 39 | ("mon", t.tm_mon) 40 | ("year", t.tm_year) 41 | ("wday", t.tm_wday) 42 | ("yday", t.tm_yday) 43 | ("isdst", t.tm_isdst); 44 | } 45 | } 46 | 47 | //------------------------------------------------------------------------------ 48 | std::tm getTime() 49 | { 50 | auto t = std::time(nullptr); 51 | return *std::localtime(&t); 52 | } 53 | 54 | //------------------------------------------------------------------------------ 55 | boost::asio::awaitable service(wamp::Session& session, 56 | wamp::ConnectionWish where) 57 | { 58 | using namespace wamp; 59 | using boost::asio::use_awaitable; 60 | 61 | (co_await session.connect(std::move(where), use_awaitable)).value(); 62 | (co_await session.join(Realm(realm), use_awaitable)).value(); 63 | (co_await session.enroll(Procedure("get_time"), 64 | simpleRpc(&getTime), 65 | use_awaitable)).value(); 66 | 67 | boost::asio::steady_timer timer(co_await boost::asio::this_coro::executor); 68 | auto deadline = std::chrono::steady_clock::now(); 69 | while (true) 70 | { 71 | deadline += std::chrono::seconds(1); 72 | timer.expires_at(deadline); 73 | co_await timer.async_wait(use_awaitable); 74 | 75 | auto t = std::time(nullptr); 76 | const std::tm* local = std::localtime(&t); 77 | (co_await session.publish(Pub("time_tick").withArgs(*local), 78 | use_awaitable)).value(); 79 | std::cout << "Tick: " << std::asctime(local) << "\n"; 80 | } 81 | } 82 | 83 | //------------------------------------------------------------------------------ 84 | int main() 85 | { 86 | wamp::IoContext ioctx; 87 | auto tcp = wamp::TcpHost(address, port).withFormat(wamp::json); 88 | wamp::Session session(ioctx); 89 | boost::asio::co_spawn(ioctx, service(session, tcp), boost::asio::detached); 90 | ioctx.run(); 91 | return 0; 92 | } 93 | -------------------------------------------------------------------------------- /examples/futuretimeclient/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # Copyright Butterfly Energy Systems 2022. 3 | # Distributed under the Boost Software License, Version 1.0. 4 | # https://www.boost.org/LICENSE_1_0.txt 5 | #------------------------------------------------------------------------------- 6 | 7 | add_executable(cppwamp-example-futuretimeclient main.cpp) 8 | 9 | target_link_libraries(cppwamp-example-futuretimeclient 10 | PRIVATE CppWAMP::core) 11 | -------------------------------------------------------------------------------- /examples/futuretimeclient/main.cpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | //****************************************************************************** 8 | // Example WAMP service consumer app using std::future. 9 | //****************************************************************************** 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | const std::string realm = "cppwamp.demo.time"; 21 | const std::string address = "localhost"; 22 | const short port = 12345u; 23 | 24 | //------------------------------------------------------------------------------ 25 | namespace wamp 26 | { 27 | // Convert a std::tm to/from an object variant. 28 | template 29 | void convert(TConverter& conv, std::tm& t) 30 | { 31 | conv ("sec", t.tm_sec) 32 | ("min", t.tm_min) 33 | ("hour", t.tm_hour) 34 | ("mday", t.tm_mday) 35 | ("mon", t.tm_mon) 36 | ("year", t.tm_year) 37 | ("wday", t.tm_wday) 38 | ("yday", t.tm_yday) 39 | ("isdst", t.tm_isdst); 40 | } 41 | } 42 | 43 | //------------------------------------------------------------------------------ 44 | void onTimeTick(std::tm time) 45 | { 46 | std::cout << "The current time is: " << std::asctime(&time) << "\n"; 47 | } 48 | 49 | //------------------------------------------------------------------------------ 50 | int main() 51 | { 52 | using boost::asio::use_future; 53 | 54 | // Run the io_context off in its own thread so that it operates 55 | // completely asynchronously with respect to the rest of the program. 56 | boost::asio::io_context ioctx; 57 | auto work = boost::asio::make_work_guard(ioctx); 58 | std::thread thread([&ioctx](){ ioctx.run(); }); 59 | 60 | auto tcp = wamp::TcpHost(address, port).withFormat(wamp::json); 61 | wamp::Session session(ioctx); 62 | 63 | // get() blocks the main thread until completion. 64 | // value() throws if there was an error. 65 | auto index = session.connect(std::move(tcp), use_future).get().value(); 66 | std::cout << "Connected via " << index << std::endl; 67 | 68 | auto info = session.join(wamp::Realm(realm), use_future).get().value(); 69 | std::cout << "Joined, SessionId=" << info.id() << std::endl; 70 | 71 | auto result = session.call(wamp::Rpc("get_time"), 72 | use_future).get().value(); 73 | auto time = result[0].to(); 74 | std::cout << "The current time is: " << std::asctime(&time) << "\n"; 75 | 76 | session.subscribe(wamp::Topic("time_tick"), 77 | wamp::simpleEvent(&onTimeTick), 78 | use_future).get().value(); 79 | 80 | thread.join(); 81 | 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /examples/futuretimeservice/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # Copyright Butterfly Energy Systems 2022. 3 | # Distributed under the Boost Software License, Version 1.0. 4 | # https://www.boost.org/LICENSE_1_0.txt 5 | #------------------------------------------------------------------------------- 6 | 7 | add_executable(cppwamp-example-futuretimeservice main.cpp) 8 | 9 | target_link_libraries(cppwamp-example-futuretimeservice 10 | PRIVATE CppWAMP::core) 11 | -------------------------------------------------------------------------------- /examples/futuretimeservice/main.cpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | //****************************************************************************** 8 | // Example WAMP service provider app using std::future. 9 | //****************************************************************************** 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | const std::string realm = "cppwamp.demo.time"; 24 | const std::string address = "localhost"; 25 | const short port = 54321u; 26 | 27 | //------------------------------------------------------------------------------ 28 | namespace wamp 29 | { 30 | // Convert a std::tm to/from an object variant. 31 | template 32 | void convert(TConverter& conv, std::tm& t) 33 | { 34 | conv ("sec", t.tm_sec) 35 | ("min", t.tm_min) 36 | ("hour", t.tm_hour) 37 | ("mday", t.tm_mday) 38 | ("mon", t.tm_mon) 39 | ("year", t.tm_year) 40 | ("wday", t.tm_wday) 41 | ("yday", t.tm_yday) 42 | ("isdst", t.tm_isdst); 43 | } 44 | } 45 | 46 | //------------------------------------------------------------------------------ 47 | std::tm getTime() 48 | { 49 | auto t = std::time(nullptr); 50 | return *std::localtime(&t); 51 | } 52 | 53 | //------------------------------------------------------------------------------ 54 | int main() 55 | { 56 | using boost::asio::use_future; 57 | 58 | // Run the io_context off in its own thread so that it operates 59 | // completely asynchronously with respect to the rest of the program. 60 | boost::asio::io_context ioctx; 61 | auto work = boost::asio::make_work_guard(ioctx); 62 | std::thread thread([&ioctx](){ ioctx.run(); }); 63 | 64 | auto tcp = wamp::TcpHost(address, port).withFormat(wamp::json); 65 | wamp::Session session(ioctx); 66 | boost::asio::steady_timer timer(ioctx); 67 | 68 | // get() blocks the main thread until completion. 69 | // value() throws if there was an error. 70 | auto index = session.connect(std::move(tcp), use_future).get().value(); 71 | std::cout << "Connected via " << index << std::endl; 72 | 73 | auto info = session.join(wamp::Realm(realm), use_future).get().value(); 74 | std::cout << "Joined, SessionId=" << info.id() << std::endl; 75 | 76 | auto reg = session.enroll(wamp::Procedure("get_time"), 77 | wamp::simpleRpc(&getTime), 78 | use_future).get().value(); 79 | std::cout << "Registered 'get_time', RegistrationId=" << reg.id() 80 | << std::endl; 81 | 82 | auto deadline = std::chrono::steady_clock::now(); 83 | while (true) 84 | { 85 | deadline += std::chrono::seconds(1); 86 | timer.expires_at(deadline); 87 | timer.async_wait(use_future).get(); 88 | 89 | auto t = std::time(nullptr); 90 | const std::tm* local = std::localtime(&t); 91 | session.publish(wamp::Pub("time_tick").withArgs(*local), 92 | use_future).get().value(); 93 | std::cout << "Tick: " << std::asctime(local) << "\n"; 94 | } 95 | 96 | return 0; 97 | } 98 | -------------------------------------------------------------------------------- /examples/stacklesstimeclient/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # Copyright Butterfly Energy Systems 2022. 3 | # Distributed under the Boost Software License, Version 1.0. 4 | # https://www.boost.org/LICENSE_1_0.txt 5 | #------------------------------------------------------------------------------- 6 | 7 | add_executable(cppwamp-example-stacklesstimeclient main.cpp) 8 | 9 | target_link_libraries(cppwamp-example-stacklesstimeclient 10 | PRIVATE CppWAMP::core CppWAMP::coro-usage) 11 | -------------------------------------------------------------------------------- /examples/stacklesstimeservice/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # Copyright Butterfly Energy Systems 2022. 3 | # Distributed under the Boost Software License, Version 1.0. 4 | # https://www.boost.org/LICENSE_1_0.txt 5 | #------------------------------------------------------------------------------- 6 | 7 | add_executable(cppwamp-example-stacklesstimeservice main.cpp) 8 | 9 | target_link_libraries(cppwamp-example-stacklesstimeservice 10 | PRIVATE CppWAMP::core CppWAMP::coro-usage) 11 | -------------------------------------------------------------------------------- /examples/timeclient/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | # Distributed under the Boost Software License, Version 1.0. 4 | # https://www.boost.org/LICENSE_1_0.txt 5 | #------------------------------------------------------------------------------- 6 | 7 | add_executable(cppwamp-example-timeclient main.cpp) 8 | 9 | target_link_libraries(cppwamp-example-timeclient 10 | PRIVATE CppWAMP::core CppWAMP::coro-usage) 11 | -------------------------------------------------------------------------------- /examples/timeclient/main.cpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | //****************************************************************************** 8 | // Example WAMP service consumer app using stackful coroutines. 9 | //****************************************************************************** 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | const std::string realm = "cppwamp.demo.time"; 21 | const std::string address = "localhost"; 22 | const short port = 12345u; 23 | 24 | //------------------------------------------------------------------------------ 25 | namespace wamp 26 | { 27 | // Convert a std::tm to/from an object variant. 28 | template 29 | void convert(TConverter& conv, std::tm& t) 30 | { 31 | conv ("sec", t.tm_sec) 32 | ("min", t.tm_min) 33 | ("hour", t.tm_hour) 34 | ("mday", t.tm_mday) 35 | ("mon", t.tm_mon) 36 | ("year", t.tm_year) 37 | ("wday", t.tm_wday) 38 | ("yday", t.tm_yday) 39 | ("isdst", t.tm_isdst); 40 | } 41 | } 42 | 43 | //------------------------------------------------------------------------------ 44 | void onTimeTick(std::tm time) 45 | { 46 | std::cout << "The current time is: " << std::asctime(&time) << "\n"; 47 | } 48 | 49 | //------------------------------------------------------------------------------ 50 | int main() 51 | { 52 | wamp::IoContext ioctx; 53 | auto tcp = wamp::TcpHost(address, port).withFormat(wamp::json); 54 | wamp::Session session(ioctx); 55 | 56 | wamp::spawn(ioctx, [tcp, &session](wamp::YieldContext yield) 57 | { 58 | session.connect(tcp, yield).value(); 59 | session.join(wamp::Realm(realm), yield).value(); 60 | auto result = session.call(wamp::Rpc("get_time"), yield).value(); 61 | auto time = result[0].to(); 62 | std::cout << "The current time is: " << std::asctime(&time) << "\n"; 63 | 64 | session.subscribe(wamp::Topic("time_tick"), 65 | wamp::simpleEvent(&onTimeTick), 66 | yield).value(); 67 | }); 68 | 69 | ioctx.run(); 70 | 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /examples/timeservice/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | # Distributed under the Boost Software License, Version 1.0. 4 | # https://www.boost.org/LICENSE_1_0.txt 5 | #------------------------------------------------------------------------------- 6 | 7 | add_executable(cppwamp-example-timeservice main.cpp) 8 | 9 | target_link_libraries(cppwamp-example-timeservice 10 | PRIVATE CppWAMP::core CppWAMP::coro-usage) 11 | -------------------------------------------------------------------------------- /examples/timeservice/main.cpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | //****************************************************************************** 8 | // Example WAMP service provider app using stackful coroutines. 9 | //****************************************************************************** 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | const std::string realm = "cppwamp.demo.time"; 23 | const std::string address = "localhost"; 24 | const short port = 54321u; 25 | 26 | //------------------------------------------------------------------------------ 27 | namespace wamp 28 | { 29 | // Convert a std::tm to/from an object variant. 30 | template 31 | void convert(TConverter& conv, std::tm& t) 32 | { 33 | conv ("sec", t.tm_sec) 34 | ("min", t.tm_min) 35 | ("hour", t.tm_hour) 36 | ("mday", t.tm_mday) 37 | ("mon", t.tm_mon) 38 | ("year", t.tm_year) 39 | ("wday", t.tm_wday) 40 | ("yday", t.tm_yday) 41 | ("isdst", t.tm_isdst); 42 | } 43 | } 44 | 45 | //------------------------------------------------------------------------------ 46 | std::tm getTime() 47 | { 48 | auto t = std::time(nullptr); 49 | return *std::localtime(&t); 50 | } 51 | 52 | //------------------------------------------------------------------------------ 53 | int main() 54 | { 55 | wamp::IoContext ioctx; 56 | auto tcp = wamp::TcpHost(address, port).withFormat(wamp::json); 57 | wamp::Session session(ioctx.get_executor()); 58 | boost::asio::steady_timer timer(ioctx); 59 | 60 | wamp::spawn(ioctx, 61 | [tcp, &session, &timer](wamp::YieldContext yield) 62 | { 63 | session.connect(tcp, yield).value(); 64 | session.join(wamp::Realm(realm), yield).value(); 65 | session.enroll(wamp::Procedure("get_time"), 66 | wamp::simpleRpc(&getTime), 67 | yield).value(); 68 | 69 | auto deadline = std::chrono::steady_clock::now(); 70 | while (true) 71 | { 72 | deadline += std::chrono::seconds(1); 73 | timer.expires_at(deadline); 74 | timer.async_wait(yield); 75 | 76 | auto t = std::time(nullptr); 77 | const std::tm* local = std::localtime(&t); 78 | session.publish(wamp::Pub("time_tick").withArgs(*local), 79 | yield).value(); 80 | std::cout << "Tick: " << std::asctime(local) << "\n"; 81 | } 82 | }); 83 | 84 | ioctx.run(); 85 | 86 | return 0; 87 | } 88 | -------------------------------------------------------------------------------- /packaging/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # Copyright Butterfly Energy Systems 2022. 3 | # Distributed under the Boost Software License, Version 1.0. 4 | # https://www.boost.org/LICENSE_1_0.txt 5 | #------------------------------------------------------------------------------- 6 | 7 | # Inspired by https://github.com/alexreinking/SharedStaticStarter 8 | 9 | include(GNUInstallDirs) 10 | include(CMakePackageConfigHelpers) 11 | 12 | set(CPPWAMP_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/CppWAMP") 13 | 14 | if(BUILD_SHARED_LIBS) 15 | set(linkage shared) 16 | else() 17 | set(linkage static) 18 | endif() 19 | 20 | install(TARGETS cppwamp-core-headers EXPORT CppWAMP_Headers 21 | INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") 22 | 23 | install(EXPORT CppWAMP_Headers 24 | DESTINATION "${CPPWAMP_INSTALL_CMAKEDIR}" 25 | NAMESPACE CppWAMP:: 26 | FILE cppwamp-core-headers-target.cmake 27 | COMPONENT CppWAMP_Development) 28 | 29 | if(NOT CPPWAMP_OPT_HEADERS_ONLY) 30 | install(TARGETS cppwamp-core EXPORT CppWAMP_Core_Target 31 | RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} 32 | COMPONENT CppWAMP_Runtime 33 | LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} 34 | COMPONENT CppWAMP_Runtime 35 | NAMELINK_COMPONENT CppWAMP_Development 36 | ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} 37 | COMPONENT CppWAMP_Development 38 | INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") 39 | 40 | install(EXPORT CppWAMP_Core_Target 41 | DESTINATION "${CPPWAMP_INSTALL_CMAKEDIR}" 42 | NAMESPACE CppWAMP:: 43 | FILE cppwamp-${linkage}-core-target.cmake 44 | COMPONENT CppWAMP_Development) 45 | endif() 46 | 47 | if(CPPWAMP_OPT_WITH_CORO) 48 | install(TARGETS cppwamp-coro-usage EXPORT CppWAMP_Coro_Usage 49 | INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") 50 | 51 | install(EXPORT CppWAMP_Coro_Usage 52 | DESTINATION "${CPPWAMP_INSTALL_CMAKEDIR}" 53 | NAMESPACE CppWAMP:: 54 | FILE cppwamp-coro-usage-target.cmake 55 | COMPONENT CppWAMP_Development) 56 | endif() 57 | 58 | # Hand-written export headers are already bundled in 59 | # ${CppWAMP_SOURCE_DIR}/cppwamp/include/, so there are no generated 60 | # header files in ${CppWAMP_BINARY_DIR} to install. 61 | install(DIRECTORY "${CppWAMP_SOURCE_DIR}/cppwamp/include/" 62 | TYPE INCLUDE 63 | COMPONENT CppWAMP_Development) 64 | 65 | write_basic_package_version_file( 66 | CppWAMPConfigVersion.cmake 67 | COMPATIBILITY SameMajorVersion) 68 | 69 | install(FILES 70 | "${CMAKE_CURRENT_SOURCE_DIR}/CppWAMPConfig.cmake" 71 | "${CMAKE_CURRENT_BINARY_DIR}/CppWAMPConfigVersion.cmake" 72 | DESTINATION "${CPPWAMP_INSTALL_CMAKEDIR}" 73 | COMPONENT CppWAMP_Development) 74 | -------------------------------------------------------------------------------- /test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | # Distributed under the Boost Software License, Version 1.0. 4 | # https://www.boost.org/LICENSE_1_0.txt 5 | #------------------------------------------------------------------------------- 6 | 7 | set(SOURCES 8 | codectestcbor.cpp 9 | codectestjson.cpp 10 | codectestmsgpack.cpp 11 | payloadtest.cpp 12 | transporttest.cpp 13 | varianttestassign.cpp 14 | varianttestbadaccess.cpp 15 | varianttestcomparison.cpp 16 | varianttestconvert.cpp 17 | varianttestconverter.cpp 18 | varianttestconvertboost.cpp 19 | varianttestconvertcontainers.cpp 20 | varianttestconverttuple.cpp 21 | varianttestinfo.cpp 22 | varianttestinit.cpp 23 | varianttestmap.cpp 24 | varianttestoutput.cpp 25 | varianttestvector.cpp 26 | varianttestvisitation.cpp 27 | wamptest.cpp 28 | wamptestadvanced.cpp 29 | wampoldtest.cpp 30 | wampoldtestadvanced.cpp 31 | main.cpp 32 | ) 33 | 34 | add_executable(cppwamp-test ${SOURCES}) 35 | 36 | # Use header-only version of CppWAMP to facilitate debugging tests. 37 | target_link_libraries(cppwamp-test 38 | PRIVATE 39 | "$" 40 | "$" 41 | CppWAMP::core-headers 42 | CppWAMP::coro-usage) 43 | target_compile_options(cppwamp-test PRIVATE 44 | $<$>:-Wall> 45 | $<$:/W4>) 46 | target_compile_definitions(cppwamp-test PRIVATE 47 | "$<$:CPPWAMP_TEST_HAS_CORO=1>") 48 | 49 | # Copy Crossbar node configuration to build directory 50 | configure_file( 51 | ${CMAKE_CURRENT_SOURCE_DIR}/.crossbar/config.json 52 | ${CMAKE_CURRENT_BINARY_DIR}/.crossbar/config.json 53 | COPYONLY) 54 | configure_file( 55 | ${CMAKE_CURRENT_SOURCE_DIR}/.crossbar/oldconfig.json 56 | ${CMAKE_CURRENT_BINARY_DIR}/.crossbar/oldconfig.json 57 | COPYONLY) 58 | -------------------------------------------------------------------------------- /test/main.cpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #define CATCH_CONFIG_MAIN 8 | #include 9 | -------------------------------------------------------------------------------- /test/precompiled.hpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015. 3 | Distributed under the Boost Software License, Version 1.0. 4 | (See accompanying file LICENSE_1_0.txt or copy at 5 | http://www.boost.org/LICENSE_1_0.txt) 6 | ------------------------------------------------------------------------------*/ 7 | 8 | #if defined __cplusplus 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | #include 45 | 46 | #include 47 | #include 48 | #include 49 | #include 50 | #include 51 | #include 52 | #include 53 | #include 54 | #include 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /test/varianttestconvertboost.cpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------------ 2 | Copyright Butterfly Energy Systems 2014-2015, 2022. 3 | Distributed under the Boost Software License, Version 1.0. 4 | http://www.boost.org/LICENSE_1_0.txt 5 | ------------------------------------------------------------------------------*/ 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace wamp; 12 | 13 | namespace 14 | { 15 | 16 | template 17 | void checkOptional(const T& value) 18 | { 19 | INFO( "For value " << value ); 20 | Variant v(value); 21 | boost::optional o; 22 | CHECK_NOTHROW( v.to(o) ); 23 | CHECK( !!o ); 24 | CHECK( *o == value ); 25 | CHECK( v == value ); 26 | } 27 | 28 | template 29 | void checkSame(const Variant& v, const boost::optional& o) 30 | { 31 | INFO( "For variant = " << v << " and optional = " << o ); 32 | CHECK( v == o ); 33 | CHECK( o == v ); 34 | CHECK( !(v != o) ); 35 | CHECK( !(o != v) ); 36 | } 37 | 38 | template 39 | void checkDifferent(const Variant& v, const boost::optional& o) 40 | { 41 | INFO( "For variant = " << v << " and optional = " << o ); 42 | CHECK( v != o ); 43 | CHECK( o != v ); 44 | CHECK( !(v == o) ); 45 | CHECK( !(o == v) ); 46 | } 47 | 48 | } // anonymous namespace 49 | 50 | 51 | //------------------------------------------------------------------------------ 52 | SCENARIO( "Converting to/from Boost.Optional", "[Variant]" ) 53 | { 54 | GIVEN( "an empty Boost.Optional" ) 55 | { 56 | boost::optional opt; 57 | 58 | WHEN( "converted to a variant" ) 59 | { 60 | auto v = Variant::from(opt); 61 | 62 | THEN( "the variant is null" ) 63 | { 64 | CHECK( !v ); 65 | } 66 | } 67 | } 68 | GIVEN( "a null variant" ) 69 | { 70 | Variant v; 71 | 72 | WHEN( "converted to a boost optional" ) 73 | { 74 | boost::optional opt; 75 | v.to(opt); 76 | 77 | THEN( "the optional is empty" ) 78 | { 79 | CHECK( !opt ); 80 | } 81 | } 82 | } 83 | GIVEN( "an assortment of Boost.Optional types" ) 84 | { 85 | checkOptional(false); 86 | checkOptional(true); 87 | checkOptional(42u); 88 | checkOptional(-123); 89 | checkOptional(3.1415); 90 | checkOptional(String("foo")); 91 | } 92 | GIVEN( "an invalid variant type" ) 93 | { 94 | Variant v("foo"); 95 | WHEN( "converting to an incompatible boost::optional" ) 96 | { 97 | boost::optional opt; 98 | CHECK_THROWS_AS( v.to(opt), error::Conversion ); 99 | } 100 | } 101 | } 102 | 103 | //------------------------------------------------------------------------------ 104 | SCENARIO( "Comparing variants with Boost.Optional", "[Variant]" ) 105 | { 106 | GIVEN( "an empty optional" ) 107 | { 108 | boost::optional opt; 109 | checkSame( Variant(null), opt ); 110 | checkDifferent( Variant(true), opt ); 111 | checkDifferent( Variant(0), opt ); 112 | } 113 | GIVEN( "a null variant" ) 114 | { 115 | Variant v; 116 | using boost::optional; 117 | checkSame( v, optional() ); 118 | checkDifferent( v, optional(false) ); 119 | } 120 | GIVEN( "non empty variants and optionals" ) 121 | { 122 | using boost::optional; 123 | checkSame( Variant(42), optional(42) ); 124 | checkSame( Variant(42), optional(42.0f) ); 125 | checkSame( Variant(42.0), optional(42) ); 126 | checkDifferent( Variant("42"), optional(42) ); 127 | checkDifferent( Variant("42"), optional("") ); 128 | } 129 | } 130 | --------------------------------------------------------------------------------