mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-20 19:15:54 +00:00
Add SharedArg and AsyncObject
This commit is contained in:
@@ -99,6 +99,7 @@
|
|||||||
<ClInclude Include="..\..\beast\Utility.h" />
|
<ClInclude Include="..\..\beast\Utility.h" />
|
||||||
<ClInclude Include="..\..\beast\utility\EnableIf.h" />
|
<ClInclude Include="..\..\beast\utility\EnableIf.h" />
|
||||||
<ClInclude Include="..\..\beast\utility\Journal.h" />
|
<ClInclude Include="..\..\beast\utility\Journal.h" />
|
||||||
|
<ClInclude Include="..\..\modules\beast_asio\async\AsyncObject.h" />
|
||||||
<ClInclude Include="..\..\modules\beast_asio\async\ComposedAsyncOperation.h" />
|
<ClInclude Include="..\..\modules\beast_asio\async\ComposedAsyncOperation.h" />
|
||||||
<ClInclude Include="..\..\modules\beast_asio\async\SharedHandler.h" />
|
<ClInclude Include="..\..\modules\beast_asio\async\SharedHandler.h" />
|
||||||
<ClInclude Include="..\..\modules\beast_asio\async\SharedHandlerAllocator.h" />
|
<ClInclude Include="..\..\modules\beast_asio\async\SharedHandlerAllocator.h" />
|
||||||
@@ -109,6 +110,7 @@
|
|||||||
<ClInclude Include="..\..\modules\beast_asio\basics\BuffersType.h" />
|
<ClInclude Include="..\..\modules\beast_asio\basics\BuffersType.h" />
|
||||||
<ClInclude Include="..\..\modules\beast_asio\basics\FixedInputBuffer.h" />
|
<ClInclude Include="..\..\modules\beast_asio\basics\FixedInputBuffer.h" />
|
||||||
<ClInclude Include="..\..\modules\beast_asio\basics\PeerRole.h" />
|
<ClInclude Include="..\..\modules\beast_asio\basics\PeerRole.h" />
|
||||||
|
<ClInclude Include="..\..\modules\beast_asio\basics\SharedArg.h" />
|
||||||
<ClInclude Include="..\..\modules\beast_asio\basics\SSLContext.h" />
|
<ClInclude Include="..\..\modules\beast_asio\basics\SSLContext.h" />
|
||||||
<ClInclude Include="..\..\modules\beast_asio\beast_asio.h" />
|
<ClInclude Include="..\..\modules\beast_asio\beast_asio.h" />
|
||||||
<ClInclude Include="..\..\modules\beast_asio\http\HTTPClientType.h" />
|
<ClInclude Include="..\..\modules\beast_asio\http\HTTPClientType.h" />
|
||||||
|
|||||||
@@ -1098,6 +1098,12 @@
|
|||||||
<ClInclude Include="..\..\modules\beast_asio\http\HTTPRequest.h">
|
<ClInclude Include="..\..\modules\beast_asio\http\HTTPRequest.h">
|
||||||
<Filter>beast_asio\http</Filter>
|
<Filter>beast_asio\http</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\..\modules\beast_asio\async\AsyncObject.h">
|
||||||
|
<Filter>beast_asio\async</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\..\modules\beast_asio\basics\SharedArg.h">
|
||||||
|
<Filter>beast_asio\basics</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="..\..\modules\beast_core\containers\AbstractFifo.cpp">
|
<ClCompile Include="..\..\modules\beast_core\containers\AbstractFifo.cpp">
|
||||||
|
|||||||
72
modules/beast_asio/async/AsyncObject.h
Normal file
72
modules/beast_asio/async/AsyncObject.h
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
//------------------------------------------------------------------------------
|
||||||
|
/*
|
||||||
|
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_ASYNCOBJECT_H_INCLUDED
|
||||||
|
#define BEAST_ASIO_ASYNCOBJECT_H_INCLUDED
|
||||||
|
|
||||||
|
/** Mix-in to track when all pending I/O is complete.
|
||||||
|
Derived classes must be callable with this signature:
|
||||||
|
void asyncHandlersComplete()
|
||||||
|
*/
|
||||||
|
template <class Derived>
|
||||||
|
class AsyncObject
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
~AsyncObject ()
|
||||||
|
{
|
||||||
|
// Destroying the object with I/O pending? Not a clean exit!
|
||||||
|
bassert (m_pending.get() == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** RAII container that maintains the count of pending I/O.
|
||||||
|
Bind this into the argument list of every handler passed
|
||||||
|
to an initiating function.
|
||||||
|
*/
|
||||||
|
class CompletionCounter
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit CompletionCounter (Derived* owner)
|
||||||
|
: m_owner (owner)
|
||||||
|
{
|
||||||
|
++m_owner->m_pending;
|
||||||
|
}
|
||||||
|
|
||||||
|
CompletionCounter (CompletionCounter const& other)
|
||||||
|
: m_owner (other.m_owner)
|
||||||
|
{
|
||||||
|
++m_owner->m_pending;
|
||||||
|
}
|
||||||
|
|
||||||
|
~CompletionCounter ()
|
||||||
|
{
|
||||||
|
if (--m_owner->m_pending == 0)
|
||||||
|
m_owner->asyncHandlersComplete ();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
CompletionCounter& operator= (CompletionCounter const&);
|
||||||
|
Derived* m_owner;
|
||||||
|
};
|
||||||
|
|
||||||
|
private:
|
||||||
|
// The number of handlers pending.
|
||||||
|
Atomic <int> m_pending;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
161
modules/beast_asio/basics/SharedArg.h
Normal file
161
modules/beast_asio/basics/SharedArg.h
Normal file
@@ -0,0 +1,161 @@
|
|||||||
|
//------------------------------------------------------------------------------
|
||||||
|
/*
|
||||||
|
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_SHAREDARG_H_INCLUDED
|
||||||
|
#define BEAST_ASIO_SHAREDARG_H_INCLUDED
|
||||||
|
|
||||||
|
/** A container that turns T into a SharedObject.
|
||||||
|
We use this to manage the lifetime of objects passed to handlers.
|
||||||
|
*/
|
||||||
|
template <typename T>
|
||||||
|
struct SharedArg
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
struct Arg : SharedObject
|
||||||
|
{
|
||||||
|
Arg ()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
explicit Arg (BEAST_MOVE_ARG(T) t)
|
||||||
|
: value (BEAST_MOVE_CAST(T)(t))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class P1>
|
||||||
|
explicit Arg (P1 p1)
|
||||||
|
: value (p1)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class P1, class P2>
|
||||||
|
Arg (P1 p1, P2 p2)
|
||||||
|
: value (p1, p2)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class P1, class P2, class P3>
|
||||||
|
Arg (P1 p1, P2 p2, P3 p3)
|
||||||
|
: value (p1, p2, p3)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class P1, class P2, class P3, class P4>
|
||||||
|
Arg (P1 p1, P2 p2, P3 p3, P4 p4)
|
||||||
|
: value (p1, p2, p3, p4)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
~Arg ()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
T value;
|
||||||
|
};
|
||||||
|
|
||||||
|
public:
|
||||||
|
SharedArg ()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
explicit SharedArg (BEAST_MOVE_ARG(T) t)
|
||||||
|
: m_arg (new Arg (BEAST_MOVE_CAST(T)(t)))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class P1>
|
||||||
|
explicit SharedArg (P1 p1)
|
||||||
|
: m_arg (new Arg (p1))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class P1, class P2>
|
||||||
|
SharedArg (P1 p1, P2 p2)
|
||||||
|
: m_arg (new Arg (p1, p2))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class P1, class P2, class P3>
|
||||||
|
SharedArg (P1 p1, P2 p2, P3 p3)
|
||||||
|
: m_arg (new Arg (p1, p2, p3))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class P1, class P2, class P3, class P4>
|
||||||
|
SharedArg (P1 p1, P2 p2, P3 p3, P4 p4)
|
||||||
|
: m_arg (new Arg (p1, p2, p3, p4))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
SharedArg (SharedArg const& other)
|
||||||
|
: m_arg (other.m_arg)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
SharedArg& operator= (SharedArg const& other)
|
||||||
|
{
|
||||||
|
m_arg = other.m_arg;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
T& get ()
|
||||||
|
{
|
||||||
|
return m_arg->value;
|
||||||
|
}
|
||||||
|
|
||||||
|
T const& get () const
|
||||||
|
{
|
||||||
|
return m_arg->value;
|
||||||
|
}
|
||||||
|
|
||||||
|
T& operator* ()
|
||||||
|
{
|
||||||
|
return get();
|
||||||
|
}
|
||||||
|
|
||||||
|
T const& operator* () const
|
||||||
|
{
|
||||||
|
return get();
|
||||||
|
}
|
||||||
|
|
||||||
|
T* operator-> ()
|
||||||
|
{
|
||||||
|
return &get();
|
||||||
|
}
|
||||||
|
|
||||||
|
T const* operator-> () const
|
||||||
|
{
|
||||||
|
return &get();
|
||||||
|
}
|
||||||
|
|
||||||
|
operator T& ()
|
||||||
|
{
|
||||||
|
return m_arg->value;
|
||||||
|
}
|
||||||
|
|
||||||
|
operator T const& () const
|
||||||
|
{
|
||||||
|
return m_arg->value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
SharedPtr <Arg> m_arg;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -65,6 +65,7 @@ namespace beast
|
|||||||
# include "async/SharedHandlerPtr.h"
|
# include "async/SharedHandlerPtr.h"
|
||||||
# include "async/ComposedAsyncOperation.h"
|
# include "async/ComposedAsyncOperation.h"
|
||||||
#include "async/SharedHandlerAllocator.h"
|
#include "async/SharedHandlerAllocator.h"
|
||||||
|
#include "async/AsyncObject.h"
|
||||||
|
|
||||||
# include "basics/BufferType.h"
|
# include "basics/BufferType.h"
|
||||||
# include "basics/BuffersType.h"
|
# include "basics/BuffersType.h"
|
||||||
@@ -72,6 +73,7 @@ namespace beast
|
|||||||
#include "basics/FixedInputBuffer.h"
|
#include "basics/FixedInputBuffer.h"
|
||||||
#include "basics/PeerRole.h"
|
#include "basics/PeerRole.h"
|
||||||
#include "basics/SSLContext.h"
|
#include "basics/SSLContext.h"
|
||||||
|
#include "basics/SharedArg.h"
|
||||||
|
|
||||||
# include "sockets/SocketBase.h"
|
# include "sockets/SocketBase.h"
|
||||||
# include "sockets/Socket.h"
|
# include "sockets/Socket.h"
|
||||||
|
|||||||
@@ -20,10 +20,10 @@
|
|||||||
namespace detail
|
namespace detail
|
||||||
{
|
{
|
||||||
|
|
||||||
class LeakCheckedBase::CounterBase::Singleton
|
class LeakCheckedBase::LeakCounterBase::Singleton
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void push_back (CounterBase* counter)
|
void push_back (LeakCounterBase* counter)
|
||||||
{
|
{
|
||||||
m_list.push_front (counter);
|
m_list.push_front (counter);
|
||||||
}
|
}
|
||||||
@@ -32,7 +32,7 @@ public:
|
|||||||
{
|
{
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
CounterBase* const counter = m_list.pop_front ();
|
LeakCounterBase* const counter = m_list.pop_front ();
|
||||||
|
|
||||||
if (!counter)
|
if (!counter)
|
||||||
break;
|
break;
|
||||||
@@ -51,17 +51,17 @@ public:
|
|||||||
private:
|
private:
|
||||||
friend class LeakCheckedBase;
|
friend class LeakCheckedBase;
|
||||||
|
|
||||||
LockFreeStack <CounterBase> m_list;
|
LockFreeStack <LeakCounterBase> m_list;
|
||||||
};
|
};
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
LeakCheckedBase::CounterBase::CounterBase ()
|
LeakCheckedBase::LeakCounterBase::LeakCounterBase ()
|
||||||
{
|
{
|
||||||
Singleton::getInstance ().push_back (this);
|
Singleton::getInstance ().push_back (this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LeakCheckedBase::CounterBase::checkForLeaks ()
|
void LeakCheckedBase::LeakCounterBase::checkForLeaks ()
|
||||||
{
|
{
|
||||||
// If there's a runtime error from this line, it means there's
|
// If there's a runtime error from this line, it means there's
|
||||||
// an order of destruction problem between different translation units!
|
// an order of destruction problem between different translation units!
|
||||||
@@ -120,7 +120,7 @@ void LeakCheckedBase::reportDanglingPointer (char const*)
|
|||||||
|
|
||||||
void LeakCheckedBase::checkForLeaks ()
|
void LeakCheckedBase::checkForLeaks ()
|
||||||
{
|
{
|
||||||
CounterBase::Singleton::getInstance ().checkForLeaks ();
|
LeakCounterBase::Singleton::getInstance ().checkForLeaks ();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,12 +29,12 @@ public:
|
|||||||
static void checkForLeaks ();
|
static void checkForLeaks ();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
class CounterBase : public LockFreeStack <CounterBase>::Node
|
class LeakCounterBase : public LockFreeStack <LeakCounterBase>::Node
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CounterBase ();
|
LeakCounterBase ();
|
||||||
|
|
||||||
virtual ~CounterBase ()
|
virtual ~LeakCounterBase ()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,10 +95,10 @@ protected:
|
|||||||
private:
|
private:
|
||||||
// Singleton that maintains the count of this object
|
// Singleton that maintains the count of this object
|
||||||
//
|
//
|
||||||
class Counter : public CounterBase
|
class LeakCounter : public LeakCounterBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Counter () noexcept
|
LeakCounter () noexcept
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,9 +123,9 @@ private:
|
|||||||
|
|
||||||
// Retrieve the singleton for this object
|
// Retrieve the singleton for this object
|
||||||
//
|
//
|
||||||
static Counter& getCounter () noexcept
|
static LeakCounter& getCounter () noexcept
|
||||||
{
|
{
|
||||||
return StaticObject <Counter>::get();
|
return StaticObject <LeakCounter>::get();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user