Skip to content

Commit

Permalink
zlib_service uses buffer types
Browse files Browse the repository at this point in the history
  • Loading branch information
ashtum committed Sep 18, 2024
1 parent 3992dbe commit 59b62c3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 90 deletions.
21 changes: 12 additions & 9 deletions include/boost/http_proto/service/zlib_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
#include <boost/http_proto/detail/workspace.hpp>
#include <boost/http_proto/service/service.hpp>

#include <boost/buffers/const_buffer.hpp>
#include <boost/buffers/mutable_buffer.hpp>

namespace boost {
namespace http_proto {
namespace zlib {
Expand Down Expand Up @@ -68,19 +71,19 @@ struct BOOST_HTTP_PROTO_ZLIB_DECL
trees
};

struct params
struct results
{
void const* next_in;
std::size_t avail_in;
void* next_out;
std::size_t avail_out;
std::size_t out_bytes;
std::size_t in_bytes;
system::error_code ec;
bool finished;
};

virtual bool
virtual results
write(
params&,
flush,
system::error_code& ec) noexcept = 0;
buffers::mutable_buffer out,
buffers::const_buffer in,
flush) noexcept = 0;
};

virtual
Expand Down
23 changes: 10 additions & 13 deletions src/serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,24 @@ class deflator_filter

for(;;)
{
auto params = stream_t::params{ in.data(), in.size(),
out.data(), out.size() };
auto r = deflator_.write(out, in, flush);

results.finished =
deflator_.write(params, flush, results.ec);
results.out_bytes += r.out_bytes;
results.in_bytes += r.in_bytes;
results.ec = r.ec;
results.finished = r.finished;

results.in_bytes += (in.size() - params.avail_in);
results.out_bytes += (out.size() - params.avail_out);

if(results.ec || results.finished)
if(r.ec || r.finished)
return results;

auto prev_out_size = out.size();
in = buffers::suffix(in, params.avail_in);
out = buffers::suffix(out, params.avail_out);
out = buffers::sans_prefix(out, r.out_bytes);
in = buffers::sans_prefix(in, r.in_bytes);

if(in.size() == 0)
{
// TODO: is this necessary?
if(prev_out_size == params.avail_out)
if(r.out_bytes == 0)
{
// TODO: is this necessary?
flush = stream_t::flush::sync;
continue;
}
Expand Down
114 changes: 46 additions & 68 deletions src_zlib/service/zlib_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,55 +254,41 @@ struct service_impl
{
using key_type = service;

class BOOST_ATTRIBUTE_NODISCARD
adapter
static ::uInt
clamp(std::size_t x) noexcept
{
static constexpr auto uint_max =
(std::numeric_limits<::uInt>::max)();
z_stream* zs_;
stream::params* params_;

public:
adapter(
z_stream* zs,
stream::params* params) noexcept
: zs_{ zs }
, params_{ params }
{
zs->next_in = reinterpret_cast<unsigned char*>(
const_cast<void*>(params->next_in));
zs->avail_in = clamp(params->avail_in);
zs->next_out = reinterpret_cast<unsigned char*>(
params->next_out);
zs->avail_out = clamp(params->avail_out);
}

static ::uInt
clamp(std::size_t x) noexcept
{
if(x >= uint_max)
return uint_max;
return static_cast<::uInt>(x);
}
if(x >= (std::numeric_limits<::uInt>::max)())
return (std::numeric_limits<::uInt>::max)();
return static_cast<::uInt>(x);
}

static std::size_t
unclamp(std::size_t org, ::uInt x) noexcept
{
if(org >= uint_max)
return org - uint_max + x;
return x;
}
static void
sync(
z_stream* zs,
buffers::mutable_buffer const& out,
buffers::const_buffer const& in) noexcept
{
zs->next_in = reinterpret_cast<
unsigned char*>(const_cast<void*>(in.data()));
zs->avail_in = clamp(in.size());
zs->next_out = reinterpret_cast<
unsigned char*>(out.data());
zs->avail_out = clamp(out.size());
}

~adapter()
{
*params_ = {
zs_->next_in,
unclamp(params_->avail_in, zs_->avail_in),
zs_->next_out,
unclamp(params_->avail_out, zs_->avail_out)
};
}
};
static stream::results
make_results(
z_stream const& zs,
buffers::mutable_buffer const& out,
buffers::const_buffer const& in,
int ret) noexcept
{
return {
clamp(out.size()) - zs.avail_out,
clamp(in.size()) - zs.avail_in,
ret < 0 ? static_cast<error>(ret) : system::error_code{},
ret == Z_STREAM_END };
}

class deflator
: public stream
Expand All @@ -326,19 +312,15 @@ struct service_impl
throw_zlib_error(ret);
}

virtual bool
virtual results
write(
params& params,
flush flush,
system::error_code& ec) noexcept override
buffers::mutable_buffer out,
buffers::const_buffer in,
flush flush) noexcept override
{
auto adp = adapter{ &zs_, &params };
auto ret = deflate(&zs_, static_cast<int>(flush));
if(ret == Z_STREAM_END)
return true;
if(ret != Z_OK)
ec = static_cast<error>(ret);
return false;
sync(&zs_, out, in);
return make_results(zs_, out, in,
deflate(&zs_, static_cast<int>(flush)));
}
};

Expand All @@ -361,19 +343,15 @@ struct service_impl
throw_zlib_error(ret);
}

virtual bool
virtual results
write(
params& params,
flush flush,
system::error_code& ec) noexcept override
buffers::mutable_buffer out,
buffers::const_buffer in,
flush flush) noexcept override
{
auto adp = adapter{ &zs_, &params };
auto ret = inflate(&zs_, static_cast<int>(flush));
if(ret == Z_STREAM_END)
return true;
if(ret != Z_OK)
ec = static_cast<error>(ret);
return false;
sync(&zs_, out, in);
return make_results(zs_, out, in,
inflate(&zs_, static_cast<int>(flush)));
}
};

Expand Down

0 comments on commit 59b62c3

Please sign in to comment.