diff --git a/Builds/VisualStudio2012/beast.vcxproj b/Builds/VisualStudio2012/beast.vcxproj index 3556e971..7f441108 100644 --- a/Builds/VisualStudio2012/beast.vcxproj +++ b/Builds/VisualStudio2012/beast.vcxproj @@ -69,6 +69,14 @@ + + + + + + + + @@ -270,6 +278,7 @@ + true @@ -1129,7 +1138,7 @@ Disabled WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) - $(ProjectDir) + $(ProjectDir);%(AdditionalIncludeDirectories) Windows diff --git a/Builds/VisualStudio2012/beast.vcxproj.filters b/Builds/VisualStudio2012/beast.vcxproj.filters index ac7a89f6..c0c6ef94 100644 --- a/Builds/VisualStudio2012/beast.vcxproj.filters +++ b/Builds/VisualStudio2012/beast.vcxproj.filters @@ -140,6 +140,15 @@ {0fa6b76b-305f-473c-9b94-c1028a3af3fc} + + {54bbe439-76c3-4781-becc-9c70a2be6d82} + + + {af535ad5-a06c-462f-8ac0-8207a708e032} + + + {4a91e76d-d6ab-4ce9-a451-0c2685cd3064} + @@ -737,6 +746,30 @@ beast_core\diagnostic + + beast_asio + + + beast_asio\sockets + + + beast_asio\boost + + + beast_asio\sockets + + + beast_asio\sockets + + + beast_asio\sockets + + + beast_asio\sockets + + + beast_asio\sockets + @@ -1153,6 +1186,9 @@ beast_core\diagnostic + + beast_asio + diff --git a/modules/beast_asio/beast_asio.cpp b/modules/beast_asio/beast_asio.cpp new file mode 100644 index 00000000..d0855e1a --- /dev/null +++ b/modules/beast_asio/beast_asio.cpp @@ -0,0 +1,27 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#include "BeastConfig.h" + +#include "beast_asio.h" + +namespace beast +{ + +} diff --git a/modules/beast_asio/beast_asio.h b/modules/beast_asio/beast_asio.h new file mode 100644 index 00000000..a8241ea6 --- /dev/null +++ b/modules/beast_asio/beast_asio.h @@ -0,0 +1,61 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#ifndef BEAST_ASIO_H_INCLUDED +#define BEAST_ASIO_H_INCLUDED + +//------------------------------------------------------------------------------ + +/* If you fail to make sure that all your compile units are building Beast with + the same set of option flags, then there's a risk that different compile + units will treat the classes as having different memory layouts, leading to + very nasty memory corruption errors when they all get linked together. + That's why it's best to always include the BeastConfig.h file before any + beast headers. +*/ +#ifndef BEAST_BEASTCONFIG_H_INCLUDED +# ifdef _MSC_VER +# pragma message ("Have you included your BeastConfig.h file before including the Beast headers?") +# else +# warning "Have you included your BeastConfig.h file before including the Beast headers?" +# endif +#endif + +// Must come before boost includes to fix the bost placeholders. +#include "../beast_core/beast_core.h" + +/* This module requires boost and possibly OpenSSL */ +#include "boost/beast_BoostIncludes.h" + +namespace beast +{ + +// Order matters + +#include "sockets/beast_SocketBase.h" +#include "sockets/beast_Socket.h" +#include "sockets/beast_SocketInterface.h" +#include "sockets/beast_SocketWrapperBase.h" +#include "sockets/beast_SocketWrapper.h" +#include "sockets/beast_SharedSocket.h" + +} + +#endif + diff --git a/modules/beast_asio/boost/beast_BoostIncludes.h b/modules/beast_asio/boost/beast_BoostIncludes.h new file mode 100644 index 00000000..d4e27905 --- /dev/null +++ b/modules/beast_asio/boost/beast_BoostIncludes.h @@ -0,0 +1,55 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#ifndef BEAST_BOOSTINCLUDES_H_INCLUDED +#define BEAST_BOOSTINCLUDES_H_INCLUDED + +// Make sure we take care of fixing boost::bind oddities first. +#if !defined(BEAST_CORE_H_INCLUDED) +#error beast_core.h must be included before including this file +#endif + +#if BEAST_WIN32 +# ifndef _WIN32_WINNT +# define _WIN32_WINNT 0x0600 +# endif +# ifndef _VARIADIC_MAX +# define _VARIADIC_MAX 10 +# endif +#endif + +#include +#include +#include +#include + +//#include +//#include +//#include +//#include +//#include +//#include + +// VFALCO TODO check for version availability +#ifndef BOOST_ASIO_INITFN_RESULT_TYPE +#define BOOST_ASIO_INITFN_RESULT_TYPE(expr,val) void +#endif + +#endif + diff --git a/modules/beast_asio/sockets/beast_SharedSocket.h b/modules/beast_asio/sockets/beast_SharedSocket.h new file mode 100644 index 00000000..6309b10f --- /dev/null +++ b/modules/beast_asio/sockets/beast_SharedSocket.h @@ -0,0 +1,54 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#ifndef BEAST_SHAREDSOCKET_H_INCLUDED +#define BEAST_SHAREDSOCKET_H_INCLUDED + +/** A Socket interface with reference counting. */ +class SharedSocket + : public SharedObject + , public virtual Socket +{ +public: + /** Store your SharedSocket in one of these! */ + typedef SharedObjectPtr Ptr; +}; + +/** A RAII container for wrapping an object as a Socket. */ +template +class SharedSocketType + : public SharedSocket + , public SocketWrapper +{ +public: + /** Create the shared socket. + This takes posession of the object, which will be deleted + when the last reference goes away. + */ + SharedSocketType (Object* object) + : SocketWrapper (object) + , m_object (object) + { + } + +private: + ScopedPointer m_object; +}; + +#endif diff --git a/modules/beast_asio/sockets/beast_Socket.h b/modules/beast_asio/sockets/beast_Socket.h new file mode 100644 index 00000000..bfa4804d --- /dev/null +++ b/modules/beast_asio/sockets/beast_Socket.h @@ -0,0 +1,279 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#ifndef BEAST_SOCKET_H_INCLUDED +#define BEAST_SOCKET_H_INCLUDED + +/** A high level socket abstraction. + + This combines the capabilities of multiple socket interfaces such + as listening, connecting, streaming, and handshaking. It brings + everything together into a single abstract interface. + + When member functions are called and the underlying implementation does + not support the operation, a fatal error is generated. + + Must satisfy these requirements: + + DefaultConstructible, MoveConstructible, CopyConstructible, + MoveAssignable, CopyAssignable, Destructible + + Meets the requirements of these boost concepts: + + SyncReadStream, SyncWriteStream, AsyncReadStream, AsyncWriteStream, + + @see SharedObjectPtr +*/ +class Socket + : public SocketBase + , public boost::asio::ssl::stream_base + , public boost::asio::socket_base +{ +public: + virtual ~Socket () { } + + //-------------------------------------------------------------------------- + // + // General attributes + // + //-------------------------------------------------------------------------- + + /** Determines if the underlying stream requires a handshake. + + If is_handshaked is true, it will be necessary to call handshake or + async_handshake after the connection is established. Furthermore it + will be necessary to call the shutdown member from the + HandshakeInterface to close the connection. Do not close the underlying + socket or else the closure will not be graceful. Only one side should + initiate the handshaking shutdon. The other side should observe it. + Which side does what is up to the user. + */ + virtual bool is_handshaked () = 0; + + /** Retrieve the underlying object. + Returns nullptr if the implementation doesn't match. Usually + you will use this if you need to get at the underlying boost::asio + object. For example: + + @code + + void set_options (Socket& socket) + { + bost::boost::asio::ip::tcp::socket* sock = + socket.native_object (); + + if (sock != nullptr) + sock->set_option ( + boost::boost::asio::ip::tcp::no_delay (true)); + } + + @endcode + */ + template + Object* native_object () + { + void* const object = native_object_raw (); + if (object != nullptr) + return dynamic_cast (object); + return object; + } + + virtual void* native_object_raw () = 0; + + //-------------------------------------------------------------------------- + // + // SocketInterface + // + //-------------------------------------------------------------------------- + + void cancel () + { + boost::system::error_code ec; + throw_error (cancel (ec)); + } + + virtual boost::system::error_code cancel (boost::system::error_code& ec) = 0; + + void shutdown (shutdown_type what) + { + boost::system::error_code ec; + throw_error (shutdown (what, ec)); + } + + virtual boost::system::error_code shutdown (shutdown_type what, boost::system::error_code& ec) = 0; + + void close () + { + boost::system::error_code ec; + throw_error (close (ec)); + } + + virtual boost::system::error_code close (boost::system::error_code& ec) = 0; + + //-------------------------------------------------------------------------- + // + // StreamInterface + // + //-------------------------------------------------------------------------- + + // SyncReadStream + // + // http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/SyncReadStream.html + // + template + std::size_t read_some (MutableBufferSequence const& buffers, boost::system::error_code& ec) + { + return read_some_impl (MutableBuffers (buffers), ec); + } + + virtual std::size_t read_some_impl (MutableBuffers const& buffers, boost::system::error_code& ec) = 0; + + // SyncWriteStream + // + // http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/SyncWriteStream.html + // + template + std::size_t write_some (ConstBufferSequence const& buffers, boost::system::error_code &ec) + { + return write_some_impl (ConstBuffers (buffers), ec); + } + + virtual std::size_t write_some_impl (ConstBuffers const& buffers, boost::system::error_code& ec) = 0; + + // AsyncReadStream + // + // http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/AsyncReadStream.html + // + template + void async_read_some (MutableBufferSequence const& buffers, + BOOST_ASIO_MOVE_ARG(ReadHandler) handler) + { + async_read_some_impl (MutableBuffers (buffers), handler); + } + + virtual void async_read_some_impl (MutableBuffers const& buffers, TransferCall const& call) = 0; + + // AsyncWriteStream + // + // http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/AsyncWriteStream.html + // + template + void async_write_some (ConstBufferSequence const& buffers, + BOOST_ASIO_MOVE_ARG(WriteHandler) handler) + { + async_write_some_impl (ConstBuffers (buffers), handler); + } + + virtual void async_write_some_impl (ConstBuffers const& buffers, TransferCall const& call) = 0; + + //-------------------------------------------------------------------------- + // + // HandshakeInterface + // + //-------------------------------------------------------------------------- + + // ssl::stream::handshake (1 of 4) + // http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/ssl__stream/handshake/overload1.html + // + void handshake (handshake_type role) + { + boost::system::error_code ec; + throw_error (handshake (role, ec)); + } + + // ssl::stream::handshake (2 of 4) + // http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/ssl__stream/handshake/overload2.html + // + virtual boost::system::error_code handshake (handshake_type role, boost::system::error_code& ec) = 0; + +#if (BOOST_VERSION / 100) >= 1054 + // ssl::stream::handshake (3 of 4) + // http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/ssl__stream/handshake/overload3.html + // + template + void handshake (handshake_type role, ConstBufferSequence const& buffers) + { + boost::system::error_code ec; + throw_error (handshake (role, buffers, ec)); + } + + // ssl::stream::handshake (4 of 4) + // http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/ssl__stream/handshake/overload4.html + // + template + boost::system::error_code handshake (handshake_type role, + ConstBufferSequence const& buffers, boost::system::error_code& ec) + { + return handshake_impl (role, ConstBuffers (buffers), ec); + } + + virtual boost::system::error_code handshake_impl (handshake_type role, + ConstBuffers const& buffers, boost::system::error_code& ec) = 0; +#endif + + // ssl::stream::async_handshake (1 of 2) + // http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/ssl__stream/async_handshake/overload1.html + // + template + void async_handshake (handshake_type role, BOOST_ASIO_MOVE_ARG(HandshakeHandler) handler) + { + async_handshake_impl (role, handler); + } + + virtual void async_handshake_impl (handshake_type role, ErrorCall const& calll) = 0; + +#if (BOOST_VERSION / 100) >= 1054 + // ssl::stream::async_handshake (2 of 2) + // http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/ssl__stream/async_handshake/overload2.html + // + template + void async_handshake (handshake_type role, ConstBufferSequence const& buffers, + BOOST_ASIO_MOVE_ARG(BufferedHandshakeHandler) handler) + { + async_handshake_impl (role, ConstBuffers (buffers), handler); + } + + virtual void async_handshake_impl (handshake_type role, + ConstBuffers const& buffers, TransferCall const& call) = 0; +#endif + + // ssl::stream::shutdown + // http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/ssl__stream/shutdown.html + // + void shutdown () + { + boost::system::error_code ec; + throw_error (shutdown (ec)); + } + + virtual boost::system::error_code shutdown (boost::system::error_code& ec) = 0; + + // ssl::stream::async_shutdown + // http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/ssl__stream/async_shutdown.html + // + template + void async_shutdown (BOOST_ASIO_MOVE_ARG(ShutdownHandler) handler) + { + async_shutdown_impl (handler); + } + + virtual void async_shutdown_impl (ErrorCall const& call) = 0; +}; + +#endif diff --git a/modules/beast_asio/sockets/beast_SocketBase.h b/modules/beast_asio/sockets/beast_SocketBase.h new file mode 100644 index 00000000..8259526d --- /dev/null +++ b/modules/beast_asio/sockets/beast_SocketBase.h @@ -0,0 +1,298 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#ifndef BEAST_SOCKETBASE_H_INCLUDED +#define BEAST_SOCKETBASE_H_INCLUDED + +/** Implementation details for AbstractSocket. + Normally you wont need to use this. +*/ +class SocketBase +{ +protected: + //-------------------------------------------------------------------------- + // + // Buffers + // + //-------------------------------------------------------------------------- + + /** Storage for a BufferSequence. + + Meets these requirements: + BufferSequence + ConstBufferSequence (when Buffer is mutable_buffer) + MutableBufferSequence (when Buffer is const_buffer) + */ + template + class Buffers + { + public: + typedef Buffer value_type; + typedef typename std::vector ::const_iterator const_iterator; + + Buffers () + : m_size (0) + { + } + + template + explicit Buffers (OtherBuffers const& buffers) + : m_size (0) + { + m_buffers.reserve (std::distance (buffers.begin (), buffers.end ())); + BOOST_FOREACH (typename OtherBuffers::value_type buffer, buffers) + { + m_size += boost::asio::buffer_size (buffer); + m_buffers.push_back (buffer); + } + } + + /** Determine the total size of all buffers. + This is faster than calling boost::asio::buffer_size. + */ + std::size_t size () const noexcept + { + return m_size; + } + + const_iterator begin () const noexcept + { + return m_buffers.begin (); + } + + const_iterator end () const noexcept + { + return m_buffers.end (); + } + + /** Retrieve a consumed BufferSequence. */ + Buffers consumed (std::size_t bytes) const + { + Buffers result; + result.m_buffers.reserve (m_buffers.size ()); + BOOST_FOREACH (Buffer buffer, m_buffers) + { + std::size_t const have = boost::asio::buffer_size (buffer); + std::size_t const reduce = std::min (bytes, have); + bytes -= reduce; + + if (have > reduce) + result.m_buffers.push_back (buffer + reduce); + } + return result; + } + + private: + std::size_t m_size; + std::vector m_buffers; + }; + + /** Meets the requirements of ConstBufferSequence */ + typedef Buffers ConstBuffers; + + /** Meets the requirements of MutableBufferSequence */ + typedef Buffers MutableBuffers; + + //-------------------------------------------------------------------------- + // + // Handler abstractions + // + //-------------------------------------------------------------------------- + + // Meets these requirements: + // + // CompletionHandler + // http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/CompletionHandler.html + // + class CompletionCall + { + public: + typedef void result_type; + + template + CompletionCall (BOOST_ASIO_MOVE_ARG (Handler) handler) + : m_call (new CallType (handler)) + { + } + + CompletionCall (CompletionCall const& other) + : m_call (other.m_call) + { + } + + void operator() () + { + (*m_call) (); + } + + private: + struct Call : SharedObject, LeakChecked + { + virtual void operator() () = 0; + }; + + template + struct CallType : Call + { + CallType (BOOST_ASIO_MOVE_ARG (Handler) handler) + : m_handler (handler) + { + } + + void operator() () + { + m_handler (); + } + + Handler m_handler; + }; + + private: + SharedObjectPtr m_call; + }; + + // Meets these requirements: + // + // AcceptHandler + // http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/AcceptHandler.html + // + // ConnectHandler + // http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/ConnectHandler.html + // + // ShutdownHandler + // http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/ShutdownHandler.html + // + // HandshakeHandler + // http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/HandshakeHandler.html + // + class ErrorCall + { + public: + typedef void result_type; + + template + ErrorCall (BOOST_ASIO_MOVE_ARG (Handler) handler) + : m_call (new CallType (handler)) + { + } + + ErrorCall (ErrorCall const& other) + : m_call (other.m_call) + { + } + + void operator() (boost::system::error_code const& ec) + { + (*m_call) (ec); + } + + private: + struct Call : SharedObject, LeakChecked + { + virtual void operator() (boost::system::error_code const&) = 0; + }; + + template + struct CallType : Call + { + CallType (BOOST_ASIO_MOVE_ARG (Handler) handler) + : m_handler (handler) + { + } + + void operator() (boost::system::error_code const& ec) + { + m_handler (ec); + } + + Handler m_handler; + }; + + private: + SharedObjectPtr m_call; + }; + + // Meets these requirements + // + // ReadHandler + // http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/ReadHandler.html + // + // WriteHandler + // http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/WriteHandler.html + // + // BUfferedHandshakeHandler + // http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/BufferedHandshakeHandler.html + // + class TransferCall + { + public: + typedef void result_type; + + template + TransferCall (BOOST_ASIO_MOVE_ARG (Handler) handler) + : m_call (new CallType (handler)) + { + } + + TransferCall (TransferCall const& other) + : m_call (other.m_call) + { + } + + void operator() (boost::system::error_code const& ec, std::size_t bytes_transferred) + { + (*m_call) (ec, bytes_transferred); + } + + private: + struct Call : SharedObject, LeakChecked + { + virtual void operator() (boost::system::error_code const&, std::size_t) = 0; + }; + + template + struct CallType : Call + { + CallType (BOOST_ASIO_MOVE_ARG (Handler) handler) + : m_handler (handler) + { + } + + void operator() (boost::system::error_code const& ec, std::size_t bytes_transferred) + { + m_handler (ec, bytes_transferred); + } + + Handler m_handler; + }; + + private: + SharedObjectPtr m_call; + }; + + /** Called when the underlying object does not support the interface. */ + void throw_error (boost::system::error_code const& ec) + { + if (ec) + Throw (boost::system::system_error (ec), __FILE__, __LINE__); + } +}; + + +#endif diff --git a/modules/beast_asio/sockets/beast_SocketInterface.h b/modules/beast_asio/sockets/beast_SocketInterface.h new file mode 100644 index 00000000..ae9d2c87 --- /dev/null +++ b/modules/beast_asio/sockets/beast_SocketInterface.h @@ -0,0 +1,42 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#ifndef BEAST_SOCKETINTERFACE_H_INCLUDED +#define BEAST_SOCKETINTERFACE_H_INCLUDED + +/** These define the interfaces that SocketWrapper can adapt with SFINAE. */ +struct SocketInterface +{ + /** Tag for some compatibility with asio::basic_socket + http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/basic_socket.html + */ + struct Socket { }; + + /** Tag for some compatibility with asio::basic_stream_socket + http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/basic_stream_socket.html + */ + struct Stream { }; + + /** Tag for some compatibility with asio::ssl::stream + http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/ssl__stream.html + */ + struct Handshake { }; +}; + +#endif diff --git a/modules/beast_asio/sockets/beast_SocketWrapper.h b/modules/beast_asio/sockets/beast_SocketWrapper.h new file mode 100644 index 00000000..2650c490 --- /dev/null +++ b/modules/beast_asio/sockets/beast_SocketWrapper.h @@ -0,0 +1,396 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#ifndef BEAST_SOCKETWRAPPER_H_INCLUDED +#define BEAST_SOCKETWRAPPER_H_INCLUDED + +/** Wraps a reference to any object and exports all availble interfaces. + + If the object does not support an interface, calling those + member functions will behave as if a pure virtual was called. + + Note that only a reference to the underlying is stored. Management + of the lifetime of the object is controlled by the caller. + + Supports these concepts: + + CopyConstructible, CopyAssignable, Destructible +*/ +template +class SocketWrapper + : public SocketWrapperBase + , public virtual Socket +{ +public: + typedef Object ObjectType; + typedef typename boost::remove_reference ::type ObjectT; + typedef typename boost::remove_reference ::type next_layer_type; + typedef typename next_layer_type::lowest_layer_type lowest_layer_type; + typedef boost::asio::ssl::stream_base::handshake_type handshake_type; + + SocketWrapper (Object& object) noexcept + : m_impl (&object) + { + } + + SocketWrapper (SocketWrapper const& other) noexcept + : m_impl (other.m_impl) + { + } + + SocketWrapper& operator= (SocketWrapper const& other) noexcept + { + m_impl = other.m_impl; + } + + // Retrieve the underlying object + Object& get_object () const noexcept + { + fatal_assert (m_impl != nullptr); + return *m_impl; + } + + boost::asio::io_service& get_io_service () noexcept + { + return get_object ().get_io_service (); + } + + next_layer_type& next_layer () noexcept + { + return get_object ().next_layer (); + } + + next_layer_type const& next_layer () const noexcept + { + return get_object ().next_layer (); + } + + lowest_layer_type& lowest_layer () noexcept + { + return get_object ().lowest_layer (); + } + + lowest_layer_type const& lowest_layer () const noexcept + { + return get_object ().lowest_layer (); + } + + // General + + bool is_handshaked () + { + return HasInterface ::value; + } + + void* native_object_raw () + { + return m_impl; + } + + // Socket + + boost::system::error_code cancel (boost::system::error_code& ec) + { + return cancel (ec, HasInterface ()); + } + + boost::system::error_code shutdown (shutdown_type what, boost::system::error_code& ec) + { + return shutdown (what, ec, HasInterface ()); + } + + boost::system::error_code close (boost::system::error_code& ec) + { + return close (ec, HasInterface ()); + } + + // Stream + + std::size_t read_some_impl (Socket::MutableBuffers const& buffers, + boost::system::error_code& ec) + { + return read_some (buffers, ec, HasInterface ()); + } + + std::size_t write_some_impl (Socket::ConstBuffers const& buffers, + boost::system::error_code& ec) + { + return write_some (buffers, ec, HasInterface ()); + } + + void async_read_some_impl (Socket::MutableBuffers const& buffers, + TransferCall const& call) + { + async_read_some (buffers, call, HasInterface ()); + } + + void async_write_some_impl (Socket::ConstBuffers const& buffers, + TransferCall const& call) + { + async_write_some (buffers, call, HasInterface ()); + } + + // Handshake + + boost::system::error_code handshake (handshake_type type, boost::system::error_code& ec) + { + return handshake (type, ec, HasInterface ()); + } + +#if (BOOST_VERSION / 100) >= 1054 + boost::system::error_code handshake_impl (handshake_type role, + Socket::ConstBuffers const& buffers, boost::system::error_code& ec) + { + return handshake (role, buffers, ec, HasInterface ()); + } +#endif + + void async_handshake_impl (handshake_type role, ErrorCall const& call) + { + async_handshake (role, call, HasInterface ()); + } + +#if (BOOST_VERSION / 100) >= 1054 + void async_handshake_impl (handshake_type role, + Socket::ConstBuffers const& buffers, TransferCall const& call) + { + async_handshake (role, buffers, call, HasInterface ()); + } +#endif + + boost::system::error_code shutdown (boost::system::error_code& ec) + { + return shutdown (ec, HasInterface ()); + } + + void async_shutdown_impl (ErrorCall const& call) + { + async_shutdown (call, HasInterface ()); + } + +protected: + explicit SocketWrapper (Object* object = nullptr) noexcept + : m_impl (object) + { + } + + void set (Object* ptr) noexcept + { + m_impl = ptr; + } + +private: + static boost::system::error_code fail (boost::system::error_code const& ec = boost::system::error_code ()) + { + fatal_error ("pure virtual"); + return ec; + } + + //-------------------------------------------------------------------------- + // + // Socket + // + //-------------------------------------------------------------------------- + + boost::system::error_code cancel (boost::system::error_code& ec, boost::true_type) + { + return get_object ().cancel (ec); + } + + boost::system::error_code cancel (boost::system::error_code&, boost::false_type) + { + return fail (); + } + + boost::system::error_code shutdown (Socket::shutdown_type what, boost::system::error_code& ec, boost::true_type) + { + return get_object ().shutdown (what, ec); + } + + boost::system::error_code shutdown (Socket::shutdown_type, boost::system::error_code&, boost::false_type) + { + return fail (); + } + + boost::system::error_code close (boost::system::error_code& ec, boost::true_type) + { + return get_object ().close (ec); + } + + boost::system::error_code close (boost::system::error_code&, boost::false_type) + { + return fail (); + } + + //-------------------------------------------------------------------------- + // + // Stream + // + //-------------------------------------------------------------------------- + + template + std::size_t read_some (MutableBufferSequence const& buffers, boost::system::error_code& ec, boost::true_type) + { + return get_object ().read_some (buffers, ec); + } + + template + std::size_t read_some (MutableBufferSequence const&, boost::system::error_code&, boost::false_type) + { + fail (); + return 0; + } + + template + std::size_t write_some (ConstBufferSequence const& buffers, boost::system::error_code& ec, boost::true_type) + { + return get_object ().write_some (buffers, ec); + } + + template + std::size_t write_some (ConstBufferSequence const&, boost::system::error_code&, boost::false_type) + { + fail (); + return 0; + } + + template + BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler, void (boost::system::error_code, std::size_t)) + async_read_some (MutableBufferSequence const& buffers, BOOST_ASIO_MOVE_ARG(ReadHandler) handler, boost::true_type) + { + get_object ().async_read_some (buffers, handler); + } + + template + BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler, void (boost::system::error_code, std::size_t)) + async_read_some (MutableBufferSequence const&, BOOST_ASIO_MOVE_ARG(ReadHandler), boost::false_type) + { + fail (); + } + + template + BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler, void (boost::system::error_code, std::size_t)) + async_write_some (ConstBufferSequence const& buffers, BOOST_ASIO_MOVE_ARG(WriteHandler) handler, boost::true_type) + { + return get_object ().async_write_some (buffers, handler); + } + + template + BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler, void (boost::system::error_code, std::size_t)) + async_write_some (ConstBufferSequence const&, BOOST_ASIO_MOVE_ARG(WriteHandler), boost::false_type) + { + fail (); + } + + //-------------------------------------------------------------------------- + // + // Handshake + // + //-------------------------------------------------------------------------- + + boost::system::error_code handshake (handshake_type type, boost::system::error_code& ec, boost::true_type) + { + return get_object ().handshake (type, ec); + } + + boost::system::error_code handshake (handshake_type, boost::system::error_code&, boost::false_type) + { + fail (); + return boost::system::error_code (); + } + +#if (BOOST_VERSION / 100) >= 1054 + template + boost::system::error_code handshake (handshake_type type, ConstBufferSequence const& buffers, + boost::system::error_code& ec, boost::true_type) + { + return get_object ().handshake (type, buffers, ec); + } + + template + boost::system::error_code handshake (handshake_type, ConstBufferSequence const&, + boost::system::error_code&, boost::false_type) + { + fail (); + return boost::system::error_code (); + } +#endif + + template + BOOST_ASIO_INITFN_RESULT_TYPE(HandshakeHandler, void (boost::system::error_code)) + async_handshake (handshake_type type, BOOST_ASIO_MOVE_ARG(HandshakeHandler) handler, boost::true_type) + { + return get_object ().async_handshake (type, handler); + } + + template + BOOST_ASIO_INITFN_RESULT_TYPE(HandshakeHandler, void (boost::system::error_code)) + async_handshake (handshake_type, BOOST_ASIO_MOVE_ARG(HandshakeHandler), boost::false_type) + { + fail (); + } + +#if (BOOST_VERSION / 100) >= 1054 + template + BOOST_ASIO_INITFN_RESULT_TYPE(BufferedHandshakeHandler, void (boost::system::error_code, std::size_t)) + async_handshake (handshake_type type, const ConstBufferSequence& buffers, + BOOST_ASIO_MOVE_ARG(BufferedHandshakeHandler) handler, boost::true_type) + { + return get_object ().async_handshake (type, buffers, handler); + } + + template + BOOST_ASIO_INITFN_RESULT_TYPE(BufferedHandshakeHandler, void (boost::system::error_code, std::size_t)) + async_handshake (handshake_type, const ConstBufferSequence&, + BOOST_ASIO_MOVE_ARG(BufferedHandshakeHandler), boost::false_type) + { + fail (); + } +#endif + + boost::system::error_code shutdown (boost::system::error_code& ec, boost::true_type) + { + return get_object ().shutdown (ec); + } + + boost::system::error_code shutdown (boost::system::error_code&, boost::false_type) + { + fail (); + return boost::system::error_code (); + } + + template + BOOST_ASIO_INITFN_RESULT_TYPE(ShutdownHandler, void (boost::system::error_code)) + async_shutdown (BOOST_ASIO_MOVE_ARG(ShutdownHandler) handler, boost::true_type) + { + return get_object ().async_shutdown (handler); + } + + template + BOOST_ASIO_INITFN_RESULT_TYPE(ShutdownHandler, void (boost::system::error_code)) + async_shutdown (BOOST_ASIO_MOVE_ARG(ShutdownHandler), boost::false_type) + { + fail (); + } + +private: + Object* m_impl; +}; + +#endif diff --git a/modules/beast_asio/sockets/beast_SocketWrapperBase.h b/modules/beast_asio/sockets/beast_SocketWrapperBase.h new file mode 100644 index 00000000..bfaddfef --- /dev/null +++ b/modules/beast_asio/sockets/beast_SocketWrapperBase.h @@ -0,0 +1,88 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#ifndef BEAST_SOCKETWRAPPERBASE_H_INCLUDED +#define BEAST_SOCKETWRAPPERBASE_H_INCLUDED + +/** Base class of wrappper provides the interface tags. */ +struct SocketWrapperBase +{ + /** Template specialization to determine available interfaces. */ + template + struct InterfacesOf + { + /** Intrusive tag support. + + To use this, add a struct called SocketInterfaces to your + class and derive it from the interfaces that you support. + For example: + + @code + + struct MyHandshakingStream + { + struct SocketInterfaces + : SocketInterface::Stream + , SocketInterface::Handshake + { + }; + } + + @endcode + */ + typedef typename Object::SocketInterfaces type; + typedef type value; + }; + + // Specialization for boost::asio::basic_socket + template + struct InterfacesOf > + { + struct value : SocketInterface::Socket { }; + typedef value type; + }; + + // Specialization for boost::asio::basic_stream_socket + template + struct InterfacesOf > + { + struct value : SocketInterface::Socket, SocketInterface::Stream { }; + typedef value type; + }; + + // Specialization for boost::asio::ssl::stream + template + struct InterfacesOf > + { + struct value : SocketInterface::Stream , SocketInterface::Handshake { }; + typedef value type; + }; + + // Determines if Object supports the specified Interface + template + struct HasInterface : boost::false_type { }; + + template + struct HasInterface ::type> >::type > + : boost::true_type { }; +}; + +#endif