Refactor net buffers classes

This commit is contained in:
Vinnie Falco
2013-10-03 19:44:26 -07:00
parent ac37c38133
commit 44445ff1b8
17 changed files with 259 additions and 37 deletions

View File

@@ -131,6 +131,8 @@
<ClInclude Include="..\..\beast\mpl\RemoveReference.h" />
<ClInclude Include="..\..\beast\mpl\RemoveVolatile.h" />
<ClInclude Include="..\..\beast\Net.h" />
<ClInclude Include="..\..\beast\net\BufferType.h" />
<ClInclude Include="..\..\beast\net\DynamicBuffer.h" />
<ClInclude Include="..\..\beast\net\IPEndpoint.h" />
<ClInclude Include="..\..\beast\SafeBool.h" />
<ClInclude Include="..\..\beast\SmartPtr.h" />
@@ -174,8 +176,6 @@
<ClInclude Include="..\..\modules\beast_asio\async\SharedHandlerAllocator.h" />
<ClInclude Include="..\..\modules\beast_asio\async\SharedHandlerPtr.h" />
<ClInclude Include="..\..\modules\beast_asio\async\SharedHandlerType.h" />
<ClInclude Include="..\..\modules\beast_asio\basics\BufferType.h" />
<ClInclude Include="..\..\modules\beast_asio\basics\ContentBodyBuffer.h" />
<ClInclude Include="..\..\modules\beast_asio\basics\BuffersType.h" />
<ClInclude Include="..\..\modules\beast_asio\basics\FixedInputBuffer.h" />
<ClInclude Include="..\..\modules\beast_asio\basics\PeerRole.h" />
@@ -466,6 +466,12 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\beast\net\impl\DynamicBuffer.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\beast\net\impl\IPEndpoint.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
@@ -511,12 +517,6 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\modules\beast_asio\basics\ContentBodyBuffer.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\modules\beast_asio\basics\PeerRole.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>

View File

@@ -902,9 +902,6 @@
<ClInclude Include="..\..\modules\beast_asio\protocol\PrefilledReadStream.h">
<Filter>beast_asio\protocol</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_asio\basics\ContentBodyBuffer.h">
<Filter>beast_asio\basics</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_asio\http\HTTPResponse.h">
<Filter>beast_asio\http</Filter>
</ClInclude>
@@ -935,9 +932,6 @@
<ClInclude Include="..\..\modules\beast_asio\basics\BuffersType.h">
<Filter>beast_asio\basics</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_asio\basics\BufferType.h">
<Filter>beast_asio\basics</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_core\memory\SharedSingleton.h">
<Filter>beast_core\memory</Filter>
</ClInclude>
@@ -1215,6 +1209,12 @@
<ClInclude Include="..\..\beast\SmartPtr.h">
<Filter>beast</Filter>
</ClInclude>
<ClInclude Include="..\..\beast\net\DynamicBuffer.h">
<Filter>beast\net</Filter>
</ClInclude>
<ClInclude Include="..\..\beast\net\BufferType.h">
<Filter>beast\net</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\modules\beast_core\containers\AbstractFifo.cpp">
@@ -1610,9 +1610,6 @@
<ClCompile Include="..\..\modules\beast_asio\protocol\HandshakeDetectLogicPROXY.cpp">
<Filter>beast_asio\protocol</Filter>
</ClCompile>
<ClCompile Include="..\..\modules\beast_asio\basics\ContentBodyBuffer.cpp">
<Filter>beast_asio\basics</Filter>
</ClCompile>
<ClCompile Include="..\..\modules\beast_asio\http\HTTPResponse.cpp">
<Filter>beast_asio\http</Filter>
</ClCompile>
@@ -1748,6 +1745,9 @@
<ClCompile Include="..\..\beast\utility\impl\Error.cpp">
<Filter>beast\utility\impl</Filter>
</ClCompile>
<ClCompile Include="..\..\beast\net\impl\DynamicBuffer.cpp">
<Filter>beast\net\impl</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text Include="..\..\TODO.txt">

View File

@@ -20,6 +20,9 @@
#ifndef BEAST_NET_H_INCLUDED
#define BEAST_NET_H_INCLUDED
#include "net/BufferType.h"
#include "net/DynamicBuffer.h"
#include "net/IPEndpoint.h"
#endif

View File

@@ -17,8 +17,12 @@
*/
//==============================================================================
#ifndef BEAST_ASIO_BASICS_BUFFERTYPE_H_INCLUDED
#define BEAST_ASIO_BASICS_BUFFERTYPE_H_INCLUDED
#ifndef BEAST_NET_BASICS_BUFFERTYPE_H_INCLUDED
#define BEAST_NET_BASICS_BUFFERTYPE_H_INCLUDED
#include "../mpl/IfCond.h"
namespace beast {
/** General linear memory buffer.
This wraps the underlying buffer type and provides additional methods
@@ -99,4 +103,6 @@ private:
};
/** @} */
}
#endif

125
beast/net/DynamicBuffer.h Normal file
View File

@@ -0,0 +1,125 @@
//------------------------------------------------------------------------------
/*
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_NET_DYNAMICBUFFER_H_INCLUDED
#define BEAST_NET_DYNAMICBUFFER_H_INCLUDED
#include <vector>
namespace beast {
/** Disjoint, but efficient buffer storage for network operations.
This is designed to not require asio in order to compile.
*/
class DynamicBuffer
{
public:
enum
{
defaultBlocksize = 32 * 1024
};
typedef std::size_t size_type;
/** Create the dynamic buffer with the specified block size. */
explicit DynamicBuffer (size_type blocksize = defaultBlocksize);
DynamicBuffer (DynamicBuffer const& other);
~DynamicBuffer ();
DynamicBuffer& operator= (DynamicBuffer const& other);
/** Swap the contents of this buffer with another.
This is the preferred way to transfer ownership.
*/
void swapWith (DynamicBuffer& other);
/** Returns the size of the input sequence. */
size_type size () const;
/** Returns a buffer to the input sequence.
ConstBufferType must be constructible with this signature:
ConstBufferType (void const* buffer, size_type bytes);
*/
template <typename ConstBufferType>
std::vector <ConstBufferType> data () const
{
std::vector <ConstBufferType> buffers;
buffers.reserve (m_buffers.size());
size_type amount (m_size);
for (typename Buffers::const_iterator iter (m_buffers.begin());
amount > 0 && iter != m_buffers.end(); ++iter)
{
size_type const n (std::min (amount, m_blocksize));
buffers.push_back (ConstBufferType (*iter, n));
amount -= n;
}
return buffers;
}
/** Reserve space in the output sequence.
This also returns a buffer suitable for writing.
MutableBufferType must be constructible with this signature:
MutableBufferType (void* buffer, size_type bytes);
*/
template <typename MutableBufferType>
std::vector <MutableBufferType> prepare (size_type amount)
{
std::vector <MutableBufferType> buffers;
buffers.reserve (m_buffers.size());
reserve (amount);
size_type offset (m_size % m_blocksize);
for (Buffers::iterator iter = m_buffers.begin () + (m_size / m_blocksize);
amount > 0 && iter != m_buffers.end (); ++iter)
{
size_type const n (std::min (amount, m_blocksize - offset));
buffers.push_back (MutableBufferType (
((static_cast <char*> (*iter)) + offset), n));
amount -= n;
offset = 0;
}
return buffers;
}
/** Reserve space in the output sequence. */
void reserve (size_type n);
/** Move bytes from the output to the input sequence. */
void commit (size_type n);
/** Release memory while preserving the input sequence. */
void shrink_to_fit ();
/** Convert the entire buffer into a single string.
This is mostly for diagnostics, it defeats the purpose of the class!
*/
std::string to_string () const;
private:
typedef std::vector <void*> Buffers;
size_type m_blocksize;
size_type m_size;
Buffers m_buffers;
};
}
#endif

View File

@@ -23,4 +23,5 @@
#include "../../modules/beast_core/beast_core.h" // for UnitTest
#include "impl/DynamicBuffer.cpp"
#include "impl/IPEndpoint.cpp"

View File

@@ -0,0 +1,91 @@
//------------------------------------------------------------------------------
/*
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 <memory>
#include "../DynamicBuffer.h"
namespace beast {
DynamicBuffer::DynamicBuffer (size_type blocksize)
: m_blocksize (blocksize)
, m_size (0)
{
}
DynamicBuffer::~DynamicBuffer ()
{
for (Buffers::iterator iter (m_buffers.begin());
iter != m_buffers.end(); ++iter)
free (*iter);
}
void DynamicBuffer::swapWith (DynamicBuffer& other)
{
std::swap (m_blocksize, other.m_blocksize);
std::swap (m_size, other.m_size);
m_buffers.swap (other.m_buffers);
}
void DynamicBuffer::commit (size_type n)
{
m_size += n;
bassert (m_size <= m_buffers.size () * m_blocksize);
}
DynamicBuffer::size_type DynamicBuffer::size () const
{
return m_size;
}
void DynamicBuffer::reserve (size_type n)
{
size_type count ((m_size + n + m_blocksize - 1) / m_blocksize);
if (count > m_buffers.size ())
for (count -= m_buffers.size (); count-- > 0;)
m_buffers.push_back (malloc (m_blocksize));
}
void DynamicBuffer::shrink_to_fit ()
{
size_type const count ((m_size + m_blocksize - 1) / m_blocksize);
while (m_buffers.size () > count)
{
free (m_buffers.back ());
m_buffers.erase (m_buffers.end () - 1);
}
}
std::string DynamicBuffer::to_string () const
{
std::string (s);
s.reserve (m_size);
std::size_t amount (m_size);
for (Buffers::const_iterator iter (m_buffers.begin());
amount > 0 && iter != m_buffers.end(); ++iter)
{
char const* p (static_cast <char const*> (*iter));
size_type const n (std::min (amount, m_blocksize));
s.append (p, p + n);
amount -= n;
}
return s;
}
}

View File

@@ -121,11 +121,9 @@ private:
//------------------------------------------------------------------------------
/** A single linear read-only buffer. */
//typedef BufferType <true> ConstBuffer;
typedef boost::asio::const_buffer ConstBuffer;
/** A single linear writable buffer. */
//typedef BufferType <false> MutableBuffer;
typedef boost::asio::mutable_buffer MutableBuffer;
/** Meets the requirements of ConstBufferSequence */

View File

@@ -29,7 +29,6 @@ namespace beast {
#include "async/SharedHandler.cpp"
#include "basics/ContentBodyBuffer.cpp"
#include "basics/PeerRole.cpp"
#include "basics/SSLContext.cpp"

View File

@@ -70,9 +70,7 @@ namespace beast {
#include "async/SharedHandlerAllocator.h"
#include "async/AsyncObject.h"
# include "basics/BufferType.h"
# include "basics/BuffersType.h"
#include "basics/ContentBodyBuffer.h"
#include "basics/FixedInputBuffer.h"
#include "basics/PeerRole.h"
#include "basics/SSLContext.h"

View File

@@ -19,7 +19,7 @@
HTTPMessage::HTTPMessage (HTTPVersion const& version_,
StringPairArray& fields,
ContentBodyBuffer& body)
DynamicBuffer& body)
: m_version (version_)
, m_headers (fields)
{
@@ -36,7 +36,7 @@ HTTPHeaders const& HTTPMessage::headers () const
return m_headers;
}
ContentBodyBuffer const& HTTPMessage::body () const
DynamicBuffer const& HTTPMessage::body () const
{
return m_body;
}

View File

@@ -40,7 +40,7 @@ public:
*/
HTTPMessage (HTTPVersion const& version_,
StringPairArray& fields,
ContentBodyBuffer& body);
DynamicBuffer& body);
/** Returns the HTTP version of this message. */
HTTPVersion const& version () const;
@@ -49,7 +49,7 @@ public:
HTTPHeaders const& headers () const;
/** Returns the content-body. */
ContentBodyBuffer const& body () const;
DynamicBuffer const& body () const;
/** Outputs all the HTTPMessage data excluding the body into a string. */
String toString () const;
@@ -57,7 +57,7 @@ public:
private:
HTTPVersion m_version;
HTTPHeaders m_headers;
ContentBodyBuffer m_body;
DynamicBuffer m_body;
};
#endif

View File

@@ -125,7 +125,7 @@ public:
return m_headersComplete;
}
ContentBodyBuffer& body ()
DynamicBuffer& body ()
{
return m_body;
}
@@ -188,7 +188,8 @@ private:
int onBody (char const* at, std::size_t length)
{
m_body.commit (boost::asio::buffer_copy (m_body.prepare (length),
m_body.commit (boost::asio::buffer_copy (
m_body.prepare <boost::asio::mutable_buffer> (length),
boost::asio::buffer (at, length)));
return 0;
}
@@ -258,7 +259,7 @@ private:
std::string m_field;
std::string m_value;
bool m_headersComplete;
ContentBodyBuffer m_body;
DynamicBuffer m_body;
};
#endif

View File

@@ -20,7 +20,7 @@
HTTPRequest::HTTPRequest (
HTTPVersion const& version_,
StringPairArray& fields,
ContentBodyBuffer& body,
DynamicBuffer& body,
unsigned short method_)
: HTTPMessage (version_, fields, body)
, m_method (method_)

View File

@@ -30,7 +30,7 @@ public:
HTTPRequest (
HTTPVersion const& version_,
StringPairArray& fields,
ContentBodyBuffer& body,
DynamicBuffer& body,
unsigned short method_);
unsigned short method () const;

View File

@@ -20,7 +20,7 @@
HTTPResponse::HTTPResponse (
HTTPVersion const& version_,
StringPairArray& fields,
ContentBodyBuffer& body,
DynamicBuffer& body,
unsigned short status_)
: HTTPMessage (version_, fields, body)
, m_status (status_)

View File

@@ -30,7 +30,7 @@ public:
HTTPResponse (
HTTPVersion const& version_,
StringPairArray& fields,
ContentBodyBuffer& body,
DynamicBuffer& body,
unsigned short status_);
unsigned short status () const;