Add beast_asio module

This commit is contained in:
Vinnie Falco
2013-08-06 18:55:51 -07:00
parent 05e93b0786
commit 6b2c6a5d39
11 changed files with 1346 additions and 1 deletions

View File

@@ -69,6 +69,14 @@
<None Include="..\..\README.md" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\modules\beast_asio\beast_asio.h" />
<ClInclude Include="..\..\modules\beast_asio\boost\beast_BoostIncludes.h" />
<ClInclude Include="..\..\modules\beast_asio\sockets\beast_Socket.h" />
<ClInclude Include="..\..\modules\beast_asio\sockets\beast_SharedSocket.h" />
<ClInclude Include="..\..\modules\beast_asio\sockets\beast_SocketBase.h" />
<ClInclude Include="..\..\modules\beast_asio\sockets\beast_SocketInterface.h" />
<ClInclude Include="..\..\modules\beast_asio\sockets\beast_SocketWrapper.h" />
<ClInclude Include="..\..\modules\beast_asio\sockets\beast_SocketWrapperBase.h" />
<ClInclude Include="..\..\modules\beast_basics\beast_basics.h" />
<ClInclude Include="..\..\modules\beast_basics\events\beast_DeadlineTimer.h" />
<ClInclude Include="..\..\modules\beast_basics\events\beast_OncePerSecond.h" />
@@ -270,6 +278,7 @@
<ClInclude Include="BeastConfig.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\modules\beast_asio\beast_asio.cpp" />
<ClCompile Include="..\..\modules\beast_basics\beast_basics.cpp" />
<ClCompile Include="..\..\modules\beast_basics\events\beast_DeadlineTimer.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
@@ -1129,7 +1138,7 @@
</PrecompiledHeader>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(ProjectDir)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(ProjectDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>

View File

@@ -140,6 +140,15 @@
<Filter Include="beast_core\logging">
<UniqueIdentifier>{0fa6b76b-305f-473c-9b94-c1028a3af3fc}</UniqueIdentifier>
</Filter>
<Filter Include="beast_asio">
<UniqueIdentifier>{54bbe439-76c3-4781-becc-9c70a2be6d82}</UniqueIdentifier>
</Filter>
<Filter Include="beast_asio\sockets">
<UniqueIdentifier>{af535ad5-a06c-462f-8ac0-8207a708e032}</UniqueIdentifier>
</Filter>
<Filter Include="beast_asio\boost">
<UniqueIdentifier>{4a91e76d-d6ab-4ce9-a451-0c2685cd3064}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\modules\beast_core\beast_core.h">
@@ -737,6 +746,30 @@
<ClInclude Include="..\..\modules\beast_core\diagnostic\beast_SemanticVersion.h">
<Filter>beast_core\diagnostic</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_asio\beast_asio.h">
<Filter>beast_asio</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_asio\sockets\beast_SocketBase.h">
<Filter>beast_asio\sockets</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_asio\boost\beast_BoostIncludes.h">
<Filter>beast_asio\boost</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_asio\sockets\beast_SocketWrapperBase.h">
<Filter>beast_asio\sockets</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_asio\sockets\beast_SocketWrapper.h">
<Filter>beast_asio\sockets</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_asio\sockets\beast_SharedSocket.h">
<Filter>beast_asio\sockets</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_asio\sockets\beast_Socket.h">
<Filter>beast_asio\sockets</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_asio\sockets\beast_SocketInterface.h">
<Filter>beast_asio\sockets</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\modules\beast_core\beast_core.cpp">
@@ -1153,6 +1186,9 @@
<ClCompile Include="..\..\modules\beast_core\diagnostic\beast_SemanticVersion.cpp">
<Filter>beast_core\diagnostic</Filter>
</ClCompile>
<ClCompile Include="..\..\modules\beast_asio\beast_asio.cpp">
<Filter>beast_asio</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text Include="..\..\TODO.txt" />

View File

@@ -0,0 +1,27 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
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
{
}

View File

@@ -0,0 +1,61 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
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

View File

@@ -0,0 +1,55 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
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 <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/foreach.hpp>
#include <boost/type_traits.hpp>
//#include <boost/mpl/at.hpp>
//#include <boost/asio/io_service.hpp>
//#include <boost/array.hpp>
//#include <boost/bind.hpp>
//#include <boost/unordered_map.hpp>
//#include <boost/mpl/vector.hpp>
// VFALCO TODO check for version availability
#ifndef BOOST_ASIO_INITFN_RESULT_TYPE
#define BOOST_ASIO_INITFN_RESULT_TYPE(expr,val) void
#endif
#endif

View File

@@ -0,0 +1,54 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
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 <SharedSocket> Ptr;
};
/** A RAII container for wrapping an object as a Socket. */
template <class Object>
class SharedSocketType
: public SharedSocket
, public SocketWrapper <Object>
{
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> (object)
, m_object (object)
{
}
private:
ScopedPointer <Object> m_object;
};
#endif

View File

@@ -0,0 +1,279 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
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 <bost::boost::asio::ip::tcp::socket> ();
if (sock != nullptr)
sock->set_option (
boost::boost::asio::ip::tcp::no_delay (true));
}
@endcode
*/
template <class Object>
Object* native_object ()
{
void* const object = native_object_raw ();
if (object != nullptr)
return dynamic_cast <Object> (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 <class MutableBufferSequence>
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 <class ConstBufferSequence>
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 <class MutableBufferSequence, class ReadHandler>
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 <class ConstBufferSequence, class WriteHandler>
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 <class ConstBufferSequence>
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 <class ConstBufferSequence>
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 <class HandshakeHandler>
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 <class ConstBufferSequence, class BufferedHandshakeHandler>
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 <class ShutdownHandler>
void async_shutdown (BOOST_ASIO_MOVE_ARG(ShutdownHandler) handler)
{
async_shutdown_impl (handler);
}
virtual void async_shutdown_impl (ErrorCall const& call) = 0;
};
#endif

View File

@@ -0,0 +1,298 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
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 Buffer>
class Buffers
{
public:
typedef Buffer value_type;
typedef typename std::vector <Buffer>::const_iterator const_iterator;
Buffers ()
: m_size (0)
{
}
template <class OtherBuffers>
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 <Buffer> m_buffers;
};
/** Meets the requirements of ConstBufferSequence */
typedef Buffers <boost::asio::const_buffer> ConstBuffers;
/** Meets the requirements of MutableBufferSequence */
typedef Buffers <boost::asio::mutable_buffer> 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 <class Handler>
CompletionCall (BOOST_ASIO_MOVE_ARG (Handler) handler)
: m_call (new CallType <Handler> (handler))
{
}
CompletionCall (CompletionCall const& other)
: m_call (other.m_call)
{
}
void operator() ()
{
(*m_call) ();
}
private:
struct Call : SharedObject, LeakChecked <Call>
{
virtual void operator() () = 0;
};
template <class Handler>
struct CallType : Call
{
CallType (BOOST_ASIO_MOVE_ARG (Handler) handler)
: m_handler (handler)
{
}
void operator() ()
{
m_handler ();
}
Handler m_handler;
};
private:
SharedObjectPtr <Call> 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 <class Handler>
ErrorCall (BOOST_ASIO_MOVE_ARG (Handler) handler)
: m_call (new CallType <Handler> (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 <Call>
{
virtual void operator() (boost::system::error_code const&) = 0;
};
template <class Handler>
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 <Call> 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 <class Handler>
TransferCall (BOOST_ASIO_MOVE_ARG (Handler) handler)
: m_call (new CallType <Handler> (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 <Call>
{
virtual void operator() (boost::system::error_code const&, std::size_t) = 0;
};
template <class Handler>
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 <Call> 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

View File

@@ -0,0 +1,42 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
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

View File

@@ -0,0 +1,396 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
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 Object>
class SocketWrapper
: public SocketWrapperBase
, public virtual Socket
{
public:
typedef Object ObjectType;
typedef typename boost::remove_reference <Object>::type ObjectT;
typedef typename boost::remove_reference <Object>::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 <ObjectT, SocketInterface::Handshake>::value;
}
void* native_object_raw ()
{
return m_impl;
}
// Socket
boost::system::error_code cancel (boost::system::error_code& ec)
{
return cancel (ec, HasInterface <ObjectT, SocketInterface::Socket> ());
}
boost::system::error_code shutdown (shutdown_type what, boost::system::error_code& ec)
{
return shutdown (what, ec, HasInterface <ObjectT, SocketInterface::Socket> ());
}
boost::system::error_code close (boost::system::error_code& ec)
{
return close (ec, HasInterface <ObjectT, SocketInterface::Socket> ());
}
// Stream
std::size_t read_some_impl (Socket::MutableBuffers const& buffers,
boost::system::error_code& ec)
{
return read_some (buffers, ec, HasInterface <ObjectT, SocketInterface::Stream> ());
}
std::size_t write_some_impl (Socket::ConstBuffers const& buffers,
boost::system::error_code& ec)
{
return write_some (buffers, ec, HasInterface <ObjectT, SocketInterface::Stream> ());
}
void async_read_some_impl (Socket::MutableBuffers const& buffers,
TransferCall const& call)
{
async_read_some (buffers, call, HasInterface <ObjectT, SocketInterface::Stream> ());
}
void async_write_some_impl (Socket::ConstBuffers const& buffers,
TransferCall const& call)
{
async_write_some (buffers, call, HasInterface <ObjectT, SocketInterface::Stream> ());
}
// Handshake
boost::system::error_code handshake (handshake_type type, boost::system::error_code& ec)
{
return handshake (type, ec, HasInterface <ObjectT, SocketInterface::Handshake> ());
}
#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 <ObjectT, SocketInterface::Handshake> ());
}
#endif
void async_handshake_impl (handshake_type role, ErrorCall const& call)
{
async_handshake (role, call, HasInterface <ObjectT, SocketInterface::Handshake> ());
}
#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 <ObjectT, SocketInterface::Handshake> ());
}
#endif
boost::system::error_code shutdown (boost::system::error_code& ec)
{
return shutdown (ec, HasInterface <ObjectT, SocketInterface::Handshake> ());
}
void async_shutdown_impl (ErrorCall const& call)
{
async_shutdown (call, HasInterface <ObjectT, SocketInterface::Handshake> ());
}
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 <typename MutableBufferSequence>
std::size_t read_some (MutableBufferSequence const& buffers, boost::system::error_code& ec, boost::true_type)
{
return get_object ().read_some (buffers, ec);
}
template <typename MutableBufferSequence>
std::size_t read_some (MutableBufferSequence const&, boost::system::error_code&, boost::false_type)
{
fail ();
return 0;
}
template <typename ConstBufferSequence>
std::size_t write_some (ConstBufferSequence const& buffers, boost::system::error_code& ec, boost::true_type)
{
return get_object ().write_some (buffers, ec);
}
template <typename ConstBufferSequence>
std::size_t write_some (ConstBufferSequence const&, boost::system::error_code&, boost::false_type)
{
fail ();
return 0;
}
template <typename MutableBufferSequence, typename ReadHandler>
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 <typename MutableBufferSequence, typename ReadHandler>
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 <typename ConstBufferSequence, typename WriteHandler>
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 <typename ConstBufferSequence, typename WriteHandler>
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 <typename ConstBufferSequence>
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 <typename ConstBufferSequence>
boost::system::error_code handshake (handshake_type, ConstBufferSequence const&,
boost::system::error_code&, boost::false_type)
{
fail ();
return boost::system::error_code ();
}
#endif
template <typename HandshakeHandler>
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 <typename HandshakeHandler>
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 <typename ConstBufferSequence, typename BufferedHandshakeHandler>
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 <typename ConstBufferSequence, typename BufferedHandshakeHandler>
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 <typename ShutdownHandler>
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 <typename ShutdownHandler>
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

View File

@@ -0,0 +1,88 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
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 <typename Object>
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 <typename Protocol, typename SocketService>
struct InterfacesOf <boost::asio::basic_socket <Protocol, SocketService> >
{
struct value : SocketInterface::Socket { };
typedef value type;
};
// Specialization for boost::asio::basic_stream_socket
template <typename Protocol, typename SocketService>
struct InterfacesOf <boost::asio::basic_stream_socket <Protocol, SocketService> >
{
struct value : SocketInterface::Socket, SocketInterface::Stream { };
typedef value type;
};
// Specialization for boost::asio::ssl::stream
template <typename Stream>
struct InterfacesOf <boost::asio::ssl::stream <Stream> >
{
struct value : SocketInterface::Stream , SocketInterface::Handshake { };
typedef value type;
};
// Determines if Object supports the specified Interface
template <typename Object, typename Interface, class Enable = void>
struct HasInterface : boost::false_type { };
template <typename Object, typename Interface>
struct HasInterface <Object, Interface,
typename boost::enable_if <boost::is_base_of <
Interface, typename InterfacesOf <Object>::type> >::type >
: boost::true_type { };
};
#endif