// // 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_WEBSOCKET_DETAIL_STREAM_BASE_HPP #define BEAST_WEBSOCKET_DETAIL_STREAM_BASE_HPP #include #include #include #include #include #include #include #include #include #include #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; //------------------------------------------------------------------------------ struct stream_base { protected: struct op {}; detail::maskgen maskgen_; // source of mask keys decorator_type d_; // adorns http messages bool keep_alive_ = false; // close on failed upgrade std::size_t rd_msg_max_ = 16 * 1024 * 1024; // max message size std::size_t wr_frag_size_ = 16 * 1024; // size of auto-fragments std::size_t mask_buf_size_ = 4096; // mask buffer size opcode wr_opcode_ = opcode::text; // outgoing message type pong_cb pong_cb_; // pong callback role_type role_; // server or client bool failed_; // the connection failed detail::frame_header rd_fh_; // current frame header detail::prepared_key_type rd_key_; // prepared masking key detail::utf8_checker rd_utf8_check_;// for current text msg std::uint64_t rd_size_; // size of the current message so far std::uint64_t rd_need_ = 0; // bytes left in msg frame payload opcode rd_opcode_; // opcode of current msg bool rd_cont_; // expecting a continuation frame bool wr_close_; // sent close frame bool wr_cont_; // next write is continuation frame op* wr_block_; // op currenly writing ping_data* pong_data_; // where to put pong payload invokable rd_op_; // invoked after write completes invokable wr_op_; // invoked after read completes close_reason cr_; // set from received close frame stream_base(stream_base&&) = default; stream_base(stream_base const&) = delete; stream_base& operator=(stream_base&&) = default; stream_base& operator=(stream_base const&) = delete; stream_base() : d_(new decorator{}) { } template void open(role_type role); template void prepare_fh(close_code::value& code); template void write_close(DynamicBuffer& db, close_reason const& rc); template void write_ping(DynamicBuffer& db, opcode op, ping_data const& data); }; } // detail } // websocket } // beast #endif