diff --git a/include/beast/core/detail/clamp.hpp b/include/beast/core/detail/clamp.hpp new file mode 100644 index 00000000..3a72f3a0 --- /dev/null +++ b/include/beast/core/detail/clamp.hpp @@ -0,0 +1,40 @@ +// +// Copyright (c) 2013-2016 Vinnie Falco (vinnie dot falco at gmail dot com) +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// + +#ifndef BEAST_CORE_DETAIL_CLAMP_HPP +#define BEAST_CORE_DETAIL_CLAMP_HPP + +#include +#include + +namespace beast { +namespace detail { + +template +static +std::size_t +clamp(UInt x) +{ + if(x >= std::numeric_limits::max()) + return std::numeric_limits::max(); + return static_cast(x); +} + +template +static +std::size_t +clamp(UInt x, std::size_t limit) +{ + if(x >= limit) + return limit; + return static_cast(x); +} + +} // detail +} // beast + +#endif diff --git a/include/beast/websocket/detail/stream_base.hpp b/include/beast/websocket/detail/stream_base.hpp index 08579a0f..ec1c7fba 100644 --- a/include/beast/websocket/detail/stream_base.hpp +++ b/include/beast/websocket/detail/stream_base.hpp @@ -9,6 +9,7 @@ #define BEAST_WEBSOCKET_DETAIL_STREAM_BASE_HPP #include +#include #include #include #include @@ -21,36 +22,12 @@ #include #include #include -#include -#include #include namespace beast { namespace websocket { namespace detail { -template -static -std::size_t -clamp(UInt x) -{ - if(x >= std::numeric_limits::max()) - return std::numeric_limits::max(); - return static_cast(x); -} - -template -static -std::size_t -clamp(UInt x, std::size_t limit) -{ - if(x >= limit) - return limit; - return static_cast(x); -} - -using pong_cb = std::function; - /// Identifies the role of a WebSockets stream. enum class role_type { diff --git a/include/beast/websocket/impl/read.ipp b/include/beast/websocket/impl/read.ipp index d4acfee5..0b2feeb5 100644 --- a/include/beast/websocket/impl/read.ipp +++ b/include/beast/websocket/impl/read.ipp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -148,6 +149,7 @@ void stream::read_frame_op:: operator()(error_code ec,std::size_t bytes_transferred, bool again) { + using beast::detail::clamp; enum { do_start = 0, @@ -191,8 +193,7 @@ operator()(error_code ec,std::size_t bytes_transferred, bool again) case do_read_payload: d.state = do_read_payload + 1; - d.dmb = d.db.prepare( - detail::clamp(d.ws.rd_need_)); + d.dmb = d.db.prepare(clamp(d.ws.rd_need_)); // receive payload data d.ws.stream_.async_read_some( *d.dmb, std::move(*this)); @@ -590,6 +591,7 @@ read_frame(frame_info& fi, DynamicBuffer& dynabuf, error_code& ec) "SyncStream requirements not met"); static_assert(beast::is_DynamicBuffer::value, "DynamicBuffer requirements not met"); + using beast::detail::clamp; close_code::value code{}; for(;;) { @@ -668,8 +670,7 @@ read_frame(frame_info& fi, DynamicBuffer& dynabuf, error_code& ec) } } // read payload - auto smb = dynabuf.prepare( - detail::clamp(rd_need_)); + auto smb = dynabuf.prepare(clamp(rd_need_)); auto const bytes_transferred = stream_.read_some(smb, ec); failed_ = ec != 0; diff --git a/include/beast/websocket/impl/write.ipp b/include/beast/websocket/impl/write.ipp index 28d2aec3..d1cffa05 100644 --- a/include/beast/websocket/impl/write.ipp +++ b/include/beast/websocket/impl/write.ipp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -122,6 +123,7 @@ class stream::write_frame_op , cont(boost_asio_handler_cont_helpers:: is_continuation(h)) { + using beast::detail::clamp; fh.op = ws.wr_.cont ? opcode::cont : ws.wr_opcode_; ws.wr_.cont = ! fin; @@ -135,8 +137,7 @@ class stream::write_frame_op { fh.key = ws.maskgen_(); detail::prepare_key(key, fh.key); - tmp_size = detail::clamp( - fh.len, ws.wr_buf_size_); + tmp_size = clamp(fh.len, ws.wr_buf_size_); tmp = boost_asio_handler_alloc_helpers:: allocate(tmp_size, h); remain = fh.len; @@ -232,6 +233,7 @@ stream:: write_frame_op:: operator()(error_code ec, bool again) { + using beast::detail::clamp; using boost::asio::buffer_copy; using boost::asio::mutable_buffers_1; auto& d = *d_; @@ -275,8 +277,7 @@ operator()(error_code ec, bool again) std::move(*this)); return; } - auto const n = - detail::clamp(d.remain, d.tmp_size); + auto const n = clamp(d.remain, d.tmp_size); mutable_buffers_1 mb{d.tmp, n}; buffer_copy(mb, d.cb); d.cb.consume(n); @@ -295,8 +296,7 @@ operator()(error_code ec, bool again) // sent masked payload case 2: { - auto const n = - detail::clamp(d.remain, d.tmp_size); + auto const n = clamp(d.remain, d.tmp_size); mutable_buffers_1 mb{d.tmp, static_cast(n)}; buffer_copy(mb, d.cb); @@ -396,6 +396,7 @@ write_frame(bool fin, static_assert(beast::is_ConstBufferSequence< ConstBufferSequence>::value, "ConstBufferSequence requirements not met"); + using beast::detail::clamp; using boost::asio::buffer; using boost::asio::buffer_copy; using boost::asio::buffer_size; @@ -434,8 +435,7 @@ write_frame(bool fin, ConstBufferSequence> cb(buffers); for(;;) { - auto const n = - detail::clamp(remain, wr_.size); + auto const n = clamp(remain, wr_.size); fh.len = n; remain -= n; fh.fin = fin ? remain == 0 : false; @@ -466,7 +466,7 @@ write_frame(bool fin, consuming_buffers< ConstBufferSequence> cb(buffers); { - auto const n = detail::clamp(remain, wr_.size); + auto const n = clamp(remain, wr_.size); auto const mb = buffer(wr_.buf.get(), n); buffer_copy(mb, cb); cb.consume(n); @@ -480,7 +480,7 @@ write_frame(bool fin, } while(remain > 0) { - auto const n = detail::clamp(remain, wr_.size); + auto const n = clamp(remain, wr_.size); auto const mb = buffer(wr_.buf.get(), n); buffer_copy(mb, cb); cb.consume(n); @@ -503,8 +503,7 @@ write_frame(bool fin, fh.key = maskgen_(); detail::prepared_key_type key; detail::prepare_key(key, fh.key); - auto const n = - detail::clamp(remain, wr_.size); + auto const n = clamp(remain, wr_.size); auto const mb = buffer(wr_.buf.get(), n); buffer_copy(mb, cb); detail::mask_inplace(mb, key); diff --git a/include/beast/websocket/option.hpp b/include/beast/websocket/option.hpp index 818a74f3..127aa158 100644 --- a/include/beast/websocket/option.hpp +++ b/include/beast/websocket/option.hpp @@ -9,9 +9,10 @@ #define BEAST_WEBSOCKET_OPTION_HPP #include -#include +#include #include #include +#include #include #include #include @@ -191,6 +192,12 @@ struct message_type }; #endif +namespace detail { + +using pong_cb = std::function; + +} // detail + /** Pong callback option. Sets the callback to be invoked whenever a pong is received diff --git a/test/Jamfile b/test/Jamfile index a01bda0f..41ba85e2 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -20,6 +20,7 @@ unit-test core-tests : core/buffer_cat.cpp core/buffer_concepts.cpp core/buffers_adapter.cpp + core/clamp.cpp core/consuming_buffers.cpp core/dynabuf_readstream.cpp core/error.cpp @@ -78,7 +79,6 @@ unit-test websocket-tests : websocket/teardown.cpp websocket/frame.cpp websocket/mask.cpp - websocket/stream_base.cpp websocket/utf8_checker.cpp ; diff --git a/test/core/CMakeLists.txt b/test/core/CMakeLists.txt index e6d291cd..8f452ad2 100644 --- a/test/core/CMakeLists.txt +++ b/test/core/CMakeLists.txt @@ -15,6 +15,7 @@ add_executable (core-tests buffer_cat.cpp buffer_concepts.cpp buffers_adapter.cpp + clamp.cpp consuming_buffers.cpp dynabuf_readstream.cpp error.cpp diff --git a/test/websocket/stream_base.cpp b/test/core/clamp.cpp similarity index 70% rename from test/websocket/stream_base.cpp rename to test/core/clamp.cpp index 4bba083e..a429f7f9 100644 --- a/test/websocket/stream_base.cpp +++ b/test/core/clamp.cpp @@ -6,22 +6,20 @@ // // Test that header file is self-contained. -#include +#include #include -#include #include namespace beast { -namespace websocket { namespace detail { -class stream_base_test : public beast::unit_test::suite +class clamp_test : public beast::unit_test::suite { public: void testClamp() { - BEAST_EXPECT(detail::clamp( + BEAST_EXPECT(clamp( std::numeric_limits::max()) == std::numeric_limits::max()); } @@ -32,9 +30,8 @@ public: } }; -BEAST_DEFINE_TESTSUITE(stream_base,websocket,beast); +BEAST_DEFINE_TESTSUITE(clamp,core,beast); } // detail -} // websocket } // beast diff --git a/test/websocket/CMakeLists.txt b/test/websocket/CMakeLists.txt index 51741891..9de18db9 100644 --- a/test/websocket/CMakeLists.txt +++ b/test/websocket/CMakeLists.txt @@ -17,7 +17,6 @@ add_executable (websocket-tests teardown.cpp frame.cpp mask.cpp - stream_base.cpp utf8_checker.cpp )