Files
xahaud/include/beast/websocket/stream.hpp
Miguel Portilla f0f96bd1da Squashed 'src/beast/' changes from 6d5547a..3bcd986
3bcd986 Set version to 79
4f42f8c Remove spurious fallthrough guidance
fa1ac16 Set version to 78
0cb9b63 Fix warning in root ca declaration
f78c73a Tidy up file_posix unused variable
72ce9ef Tidy up dstream for existing Boost versions
efe8e58 Add Boost.Locale utf8 benchmark comparison
f7c745e Remove string_view_body
7a79efa Tidy up FieldsReader doc
e51aefd Header file tidying
69898f4 Fix warning in zlib
53723c0 Add message keep_alive, chunked, content_length members
d7af73b Fix spurious uninitialized warning
ca42cc0 Tidy up invalid characters in test vector
48d3e60 Use make_unique_noinit
a1ff804 span, string, vector bodies are public
feab6a0 Documentation work
116c0b0 Add span_body
7fb901d Tidy up includes and javadocs
f0f58be Add span
7a8982b Add vector_body
524f73a Tidy up basic_string_body
a8ad67b Set version to 77
d555859 file_posix works without large file support
1bc30cb Set version to 76
9a1e7a8 Disable SSE4.2 optimizations
09af312 Fix parse illegal characters in obs-fold
7dd684c Add file_body_win32:
1bbc71c serializing file_body is not const
9a4b55e BodyReader, BodyWriter use two-phase init (API Change):
dc400ce Serializer members are not const
1a33c37 Rename to serializer::keep_alive (API Change):
55935c5 Add serializer::chunked
63ace37 Add serializer::get
2c4047b BodyReader may construct from a non-const message
0a0a225 Use Boost.Config
6f83d07 Always go through write_some:
0e23066 Set version to 75
28f3ece Doc tidying
3495331 Using SSE4.2 intrinsics in basic_parser if available
bc1f0ac file_body tests
4e03d7e Add serializer::limit
85e3ee8 Shrink serializer buffers using buffers_ref
78bcdb1 Tidy up BEAST_NO_BIG_VARIANTS
3ea6cf2 Construct buffer_prefix_view in-place
69f9f7a Use file_body for valid requests, string_body otherwise.
6f88f01 Set version to 74
93fed8e remove redundant flush() from example:
e0f56da Fix Beast include directories for cmake targets
5ff9e0a Add file_posix
1bb5705 Remove common/file_body.hpp
5c89d87 Add file_body
67a55c8 Add file_win32
647d3b0 Add file_stdio and File concept
89c416c Set version to 73
0efc32f Fixes for gcc-4.8
c8910ab Initialize local variable in basic_parser
8a28193 Adjust benchmarks
81e51d8 Verify certificates in SSL clients
a43f6d4 Jamroot tweak
8c85ee8 Put more... links on overload reference pages
ff1104e Documentation tidy
826ff0e serializer::next replaces serializer::get (API Change):
8d67775 Refactor header and message constructors:
6c79f19 Add basic_parser tests
25127d9 basic_parser optimizations:
9d082fd Set version to 72
c88e2b9 Various improvements to http_server_fast.cpp:
20b0fdb Documentation tidying
afd1fa7 Add websocket-server-async example
954b597 Add http-server-threaded example
df8f253 Refactor file_body for best practices
11c1037 Newly constructed responses have a 200 OK result
a648817 Refine Body::size specification
40aad37 Tidy up set payload in http-server-fast
52cefbc Set version to 71
8c51c77 Tidy up Jamroot /permissive-
5efecea Update README.md
5a47acd Tidy up http_sync_port error check
a2af2b5 Concept check in basic_dynamic_body
8b80a6f Fix buffer overflow handling for string_body and mutable_body
ec3b4fd Return std::size_t from Body::writer::put (API Change)
effbb37 Check trailers in test
f5368cf Call prepare_payload in HTTP example
a3e5e01 Fix spurious on_chunk invocation
96d94eb Add options for building examples and tests. Move zlib test sources to test/zlib
e0efdc7 Allow close, ping, and write to happen concurrently
9c1a419 Refactor websocket composed ops
d5659a4 Fine tune websocket asserts
b8e8943 std::pair "last" -> "first" in http_message.qbk
c691bf4 Fix can/cannot thinko in FAQ.
6dd006b Documentation revision
6d2e315 Fix extra ; warning
78a065b Set version to 70
00c7e9d Fix HEAD response in file_service
67d70d2 Fix BEAST_FALLTHROUGH in config
4f33655 Add parser::on_header to set a callback
9c16b21 Add basic_parser header and body limits:
b64d1f7 Rename to message::base (API Change):
436c66a Serialize in one step when possible
3e1061b Set version to 69
f709273 Add /permissive- to msvc toolchain
0dae464 Use BEAST_FALLTHROUGH to silence warnings
a70d386 basic_parser optimizations
4269f35 Set version to 68
544327f Link statically on cmake MSVC
e213ffe Add const_body, mutable_body to examples
0568763 Optimize field lookups
8fc3001 Use string_ref in older Boost versions
8af77da bad_target replaces bad_path (API Change):
325dd62 Adjust buffer size in fast server
be59785 Doc erratum
d9b44f3 Small speed up in fields comparisons
3e6ce38 Use Boost master on Appveyor
09f3d64 Split common tests to a new project
adfd22a Remove BodyReader::is_deferred (API Change):
582d28d Change BodyReader, BodyWriter requirements (API Change):
8982e14 Set version to 67
daa58a2 Group common example headers
afd8f1a Rename to http-server-fast
07cb9f7 control_callback replaces ping_callback (API Change):
91e83ed Use boost::string_view
067db87 Merge stream_base to stream and tidy
d61241a Add http-server-small example
eb08e92 Fix doc example link
7fb75d0 Set version to 66
df86723 Respect debug flag for marked output
c08565a Squelch spurious warning on gcc
188ef7c Documentation work
1c62d3a Add http-server example
3f54582 basic_fields optimizations
a8b05b8 Add header aliases
b94eac3 Tidy up message piecewise ctors
9c48b52 Handle bad_alloc in parser
1b57c54 Fix costly potential value-init in parser
1edc41e Make consuming_buffers smaller
72ac918 Add serializer request/response aliases
18f7606 string_param optimizations
c675252 Set version to 65
c398cdd Enable msvc warnings in Jamfile
380cceb Fix unused variable warnings
4172e7e Enable unused variable warning on msvc cmake
f04d227 Fix integer warnings
ca975b3 Fix narrowing in deflate_stream
2fab796 Fix narrowing in inflate_stream
cff87f6 Fix narrowing in ostream
1956886 Fix narrowing in static_ostream
69cdc4b Fix integer types in deflate_stream::bi_reverse
3e3dd21 Enable narrowing warning on msvc cmake
e11a294 Set version to 64
a00e070 Remove make_serializer (API Change):
8449697 Add link_directories to cmake
7b2b066 Doc tidying
158d3e8 async_write requires a non-const message:
d13328b Better User-Agent in examples
ebcb2c0 Exemplars are compiled code
b9054d3 Simplify websocket write_op
c2571fe Simplify ssl teardown composed op
d8ad3d1 Simplify buffered_read_stream composed op
f68dc34 Set version to 63
a99f7ef Control running with valgrind explicitly
4eb7af4 Tidy up Jamfiles
49b42a5 Tidy up CMakeLists.txt
dadb54f Only run the tests under ubasan
d1c7696 Move benchmarks to a separate project
a4aada8 Only build and run tests in variant=coverage
f835b9a Don't use cached Boost
a0edd82 Put num_jobs back up on Travis
a8d5823 Use std::to_string instead of lexical_cast
45d8b81 Set version to 62
09e07ce Put slow tests back for coverage builds
295b1d7 Doc tidy
f58425c Squelch harmless not_connected errors
9b537f7 Add http::is_fields trait
d43701b message::prepare_payload replaces message::prepare (API Change):
42ba289 Refine FieldsReader concept (API Change)
bde90a1 Narrow the use of Fields parameters:
5f47526 parser requires basic_fields (API Change):
60f58e4 Avoid explicit operator bool for error
352f8de Clear the error faster
34befd8 Tidy up namespaces in examples
9e0b4b5 Doc fixes and tidy
c003a2a Tidy up test build scripts and projects
b929130 Add server-framework tests
03d4301 Increase detail::static_ostream coverage
80d7cbc Remove libssl-dev from a Travis matrix item
4c15db4 Set version to 61
1dfbd0b Don't run slow tests on certain targets
6bb1109 Use one job less on CI
6f58342 Tidy up resolver calls
530b044 Add multi_port to server-framework
bfef5d1 Tidy up http-crawl example
e2f2f33 Reorganize SSL examples
adc301b Fix shadowing warnings
c60185e Add server-framework SSL HTTP and WebSocket ports
7912fb8 Refactor WebSocket, HTTP examples:
cd4b9e0 Flush the output stream in the example
d046b20 Tidy up names in error categories
9d4a422 status-codes is unsigned (API Change)
e3599b0 header::version is unsigned (API Change)
a26b043 Add message::header_part()
fc8d2e9 Tidy up some integer conversion warnings
c91732e Reorganize SSL examples
9907b31 Documentation work
4b2e78e Use generic_cateogry for errno
38c46cd Remove Spirit dependency
c111d6f Set version to 60
d78dc12 Documentation work
141a524 New server-framework, full featured server example:
3f7ffd9 Fix response message type in async websocket accept
13f3750 String comparisons are public interfaces
4e4bcf8 Set version to 59
5015cdb Remove obsolete doc/README.md
71c3f0a Fix base64 alphabet
aa2b843 Change Body::size signature (API Change):
80a599a Documentation work
9c19449 Integrated Beast interface.
3f8097b Set version to 58
4581777 Better message formal parameter names
5879cd8 Fix parsing chunk size with leading zeroes
56bd228 Remove redundant code
534ca63 Use static string in basic_fields::reader
a7b3810 basic_parser::put doc
1e4413f basic_fields::set optimization:
9b244c1 Fix basic_fields insert ordering
4f854d0 Avoid std::string in websocket
dc8f146 Renamed to basic_fields::set (API Change):
660c465 Specification for http read
981285b Documentation tidy
983d676 Reorganize examples:
a2a5c57 Qualify size_t in message template
d86769c Fix unaligned reads in utf8-checker
8ba182c Set version to 57
42e2791 Update doc/ for docca
1ee0afd Merge commit '101d7dbfb9725674cb9ce5a4196f19aa1d4bb801' as 'doc/docca'
101d7db Squashed 'doc/docca/' content from commit c50b3ba5
900c04e Documentation work
8eee932 Fix warning in basic_parser.cpp
437a616 Fix message.hpp javadocs
18c68ce Set version to 56
b058e90 Convert buffer in range loops
cbc9212 Add Beast INTERFACE CMake target
2914b59 More basic_parser tests
ed5c317 Reset error codes
ba14251 Test error code handling
e45e50b Tidy up README.md build instructions
16efb9b Try harder to find Boost (cmake)
e281d91 HTTP/1 is the default version
916fe4a Call on_chunk when the extension is empty
9855598 Add string_view_body
19d4520 Tidy up
6e59f9b Add provisional IANA header field names
84722f2 Revert "Add a Beast CMake interface target:"
fde6929 Set version to 55
01f6cc4 Documentation work
a7a388c read_size replaces read_size_helper:
ed8f0bb Tidy up type_traits
c2f6268 Avoid a parser allocation using non-flat buffer:
906db45 Add a Beast CMake interface target:
47f2541 Don't allocate memory to handle obs-fold
6a8912a Set version to 54
296ef3b Documentation work
e3e9b61 Fix incorrect use of [[fallthrough]]
3c44398 Retain ownership when reading using a message
a71bb2b basic_fields refactor (API Change):
d8d3562 Add string_param
83b2558 basic_fields members and coverage
c4f5fa5 consuming_buffers members and coverage
e10507c multi_buffer members and coverage
0e6bd3f flat_buffer coverage
7351d6e static_buffer coverage
18a8ef5 Set version to 53
452df59 Remove extraneous doc file
3ef0359 Fix read_size_helper usage:
b64e6d3 Fix basic_parser::maybe_flatten (#462)
76402f7 Set version to 52
b0ceb2a Add drain_buffer class
4c6735a flat_buffer is an AllocatorAwareContainer
9d5d4d5 Documentation work
d4ec693 finish(error_code&) is a BodyReader requirement (API Change)
7b24cad opcode is private (API Change):
068c2ac Documentation work
a1ff89b Disable std::future snippet for libstdc++ bug
b5ef664 read_frame returns `bool` fin (API Change):
7911847 Remove opcode from read, async_read (API Change):
c72d70f ping_callback is a member of stream (API Change):
720a309 write_buffer_size is a member of stream (API Change):
7ff0178 read_message_max is a member of stream (API Change):
cd40964 read_buffer_size is a member of stream (API Change):
a58e5e1 binary, text are members of stream (API Change):
ad35846 auto_fragment is a member of stream (API Change):
ccee139 Documentation work
13c64e3 Set version to 51
cafc8e2 Fix infinite loop in basic_parser
dc4b69d Add construct, destroy to handler_alloc
58c2739 multi_buffer implementation change (API Change):
dd7f5c0 DynamicBuffer benchmarks
1c4811b Use BOOST_STRINGIZE
31051ac Use BOOST_FALLTHROUGH
8f2430b Documentation work
eb35b92 Fix file_body::get() not setting the more flag correctly
566244a Tidy up file_body
53cbeea Tune up static_buffer (API Change):
20c59b7 Fix operator<< for header
a2c1117 Set version to 50
6045b74 http read_some, async_read_some don't return bytes (API Change):
4df6885 Fix chunk header parsing
36bf32b Fix test::pipe read_size
bf69ce1 Fix chunk delimiter parsing
0c6b6b1 Add missing handler_alloc nested types
a06b8f9 Tidy up message javadoc
3bd8260 Remove obsolete serializer allocator (API Change)
001c979 Remove message free functions (API Change)
745876b Remove message connection settings (API Change)
bcf2c33 Body documentation work
1e303b0 Fields concept work
9d0464a Tidy up basic_fields, header, and concepts
3ba81b5 Use field in basic_parser
b5f6cc1 Use field in basic_fields and call sites
cfd6d14 Documentation reference tidy
2adc80a Protect basic_fields special members (API Change)
d55b079 Fix basic_fields allocator awareness
d8febda Documentation work
485a6e5 Refactor prepare (API Change)
9fd3071 Derive header from Fields (API Change)
8ad26b8 Use allocator more in basic_fields
0071039 Add verb to on_request for parsers (API Change)
74f6cbb Add field enumeration
be0d74f Documentation fixes
054ac40 Remove header_parser (API Change)
a007eba parser is constructible from other body types
d89809f Documentation work (buffer_body)
ac5bc4f Set version to 49
af47128 Documentation work
a1848f0 Add HEAD request example
ddfbfbf Use <iosfwd> instead of <ostream>
e67c0ab Refactor header status, reason, and target (API Change):
60f044a Tidy up empty_body writer error
7d267f4 Canonicalize string_view parameter types
ac175cb Refactor method and verb (API Change):
e18efed Documentation work
d77e423 Set version to 48
d3a5a05 Documentation work
acf18fb Tidy up traits
6cb188e Remove detail::sync_ostream
d6092bc Documentation work
4707b21 Rename to parser (API Change):
3cb385d Consolidate parsers to parser.hpp
290bdf1 Documentation work
7cb442c Make buffer_prefix_view public
ef0b121 Rename to buffer_cat_view (API Change)
b9df187 Tidy up chunk decorator (API Change):
458fa7e Set version to 47
fc83a03 Documentation work
1ee3013 Fix leak in basic_flat_buffer
55fbf76 Fix undefined behavior in pausation
fe75a7c Refactor HTTP serialization and parsing (API Change):
50cba32 buffer_size overload for basic_multi_buffer::const_buffers_type
d977bf2 Disable operator<< for buffer_body
5db707a Refactor treatment of status code and obsolete reason (API Change):
9a585a8 Refactor treatment of request-method (API Change):
3ae76d0 Set version to 46
6004712 Documentation work
34ea0b3 Refactor serialization algorithms:
407b046 Rename to make_serializer
c29451a Refactor type_traits (API Change):
8578419 Refactor HTTP serialization (API Change):
f8612aa Remove HTTP header aliases (API Change):
b0054e3 Add test::pipe
dfba72b Set version to 45
6ba3697 Disable reverse_iterator buffer_view test
266ebac buffer_view skips empty buffer sequences
96b9892 Documentation work
c23f1e2 Fix header::reason
9796106 Better test::enable_yield_to
9a8bcb7 Fix message doc image
7a5e87e Workaround for boost::asio::basic_streambuf type check
663c275 Set version to 44
f205976 Make buffers_adapter meet requirements
8e39c60 Tidy up is_dynamic_buffer traits test
0088f7c Add buffers_adapter regression test
8a23de1 Fix README websocket example
949504a Fix async return values in docs
cd9f41b Use BOOST_STATIC_ASSERT
1b616fa Tidy up and make get_lowest_layer public
612e616 Require Boost 1.58 or later
1b1daa7 Tidy up read_size_helper and dynamic buffers
bf0145d Use BOOST_THROW_EXCEPTION
e762818 Add GitHub issue template
dab679c Set version to 43
386b817 Reformat README.md QR code
50e5123 Additional constructors for consuming_buffers
f7289b9 Add write limit to test::string_ostream
3aa87e0 Tidy up buffer_prefix overloads and test
bee583c Fix strict aliasing warnings in buffers_view
6b54d3a Require Boost 1.64.0
76f1084 Set version to 42
0bdb148 Make buffers_view a public interface
338fc81 Add formal review notes
784f965 Fix javadoc typo
823aee2 Set version to v41
88adbdd Remove handler helpers, tidy up hook invocations (API Change)
4974af2 Rename prepare_buffer(s) to buffer_prefix (API Change)
ebd459b Tidy up websocket::close_code enum and constructors
c3fd6f9 Tidy up formal parameter names
210cd70 Remove coveralls integration
d811962 Concept revision and documentation (API Change):
bdae92a Replace asynchronous helper macros with template aliases (API Change)
df95a09 Move prepare_buffers to prepare_buffer.hpp (API Change)
787de21 Remove placeholders (API Change)
c59b544 Trim Appveyor matrix rows
b7184f3 Return http::error::end_of_stream on HTTP read eof (API Change)
f2d8255 Set version to 40
40b9194 Tidy up .travis.yml:
9b240c7 Fix basic_streambuf movable trait
76a2617 Consolidate get_lowest_layer in type_traits.hpp
6d00321 Add to_static_string:
f888136 Set version to 39
47c82b5 Better travis deps
4ed7865 Squelch openssl spurious leak and memory errors
b6bc26f Fixed braced-init error with older gcc
59b2f8f ostream workaround for gcc 4.8.4
8363d86 Increase ostream test coverage
5631936 Tidy up HTTP reason_string (API Change):
2bf5150 Harmonize concepts and identifiers with net-ts (API Change):
728e9d8 Tidy up basic_parser javadocs
1c9067b Use beast::string_view alias
771c5ca Doc fixes and tidying
e2b5c31 Rename to buffered_read_stream (API Change):
a753f1c Rename to static_buffer, static_buffer_n (API Change):
24b6686 Rename to flat_buffer, basic_flat_buffer (API Change):
69259ef Rename to multi_buffer, basic_multi_buffer (API Change):
bef9ae1 New buffers() replaces to_string() (API Change):
a7ef4f5 New ostream() returns dynamic buffer output stream (API Change):
87fd60c Fix eof error on ssl::stream shutdown
606fc9d Add websocket async echo ssl server test:
ff5e659 Refactor http::header contents (API Change):
dd02097 Set version to 1.0.0-b38
5596e97 Prevent basic_fields operator[] assignment
c2b32dc Remove websocket::keep_alive option (API Change):
32dbfb2 Refactor WebSocket error codes (API Change):
dd6b500 WebSocket doc work
0b4d87c More flat_streambuf tests
aacefb4 Add test_allocator to extras/test
931a5fb Simplify get_lowest_layer test
ba4228a Use static_string for WebSocket handshakes:
6df3ff3 Refactor base64:
19b124d Refactor static_string:
30e8d16 Set version to 1.0.0-b37
b141020 Fix narrowing warning in table constants
d554b81 Add -funsigned-char to asan build target
bcc6ad8 Add ub sanitizer blacklist
e1f08e9 Fix flat_streambuf:
7d08f59 Fix typo in documentation example
21ef97d Rename to http::dynamic_body, consolidate header:
45a2d73 Rename project to http-bench
c86fee9 Move everything in basic_fields.hpp to fields.hpp (API Change)
a14a5d6 Rename to detail::is_invocable
540d037 Rename to websocket::detail::pausation
84e1739 Document websocket::stream thread safety
dc274af Add is_upgrade() free function:
2c17d04 Refactor websocket decorators (API Change):
235fe68 Provide websocket::stream accept() overloads (API Change):
a715825 CMake hide command lines in .vcxproj Output windows
32024d8 Set version to 1.0.0-b36
f48b95f Update README.md
d8db5f1 Set version to 1.0.0-b35
dd2a514 Tidy up doc declarations
2c50aba Fix README.md CMake instructions
4ffdce2 Update .gitignore for VS2017
403011f Remove http::empty_body (API Change)
f47b661 New HTTP interfaces (API Change):
f6835b8 Rename to BEAST_DOXYGEN
7e37723 Add flat_streambuf:
5b68faa Doc XSL support for list and table markdown
3de46de Make websocket::close_code a proper enum:
0128743 Tidy up MSVC CMake configuration
ccd188e Add appveyor build script

git-subtree-dir: src/beast
git-subtree-split: 3bcd9865f80f12ba5faad35c564918f85b02e271
2017-07-11 12:17:02 -04:00

3356 lines
123 KiB
C++

//
// Copyright (c) 2013-2017 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_STREAM_HPP
#define BEAST_WEBSOCKET_STREAM_HPP
#include <beast/config.hpp>
#include <beast/websocket/error.hpp>
#include <beast/websocket/option.hpp>
#include <beast/websocket/rfc6455.hpp>
#include <beast/websocket/detail/frame.hpp>
#include <beast/websocket/detail/hybi13.hpp>
#include <beast/websocket/detail/mask.hpp>
#include <beast/websocket/detail/pausation.hpp>
#include <beast/websocket/detail/pmd_extension.hpp>
#include <beast/websocket/detail/utf8_checker.hpp>
#include <beast/http/empty_body.hpp>
#include <beast/http/message.hpp>
#include <beast/http/string_body.hpp>
#include <beast/http/detail/type_traits.hpp>
#include <beast/core/async_result.hpp>
#include <beast/core/buffered_read_stream.hpp>
#include <beast/core/string.hpp>
#include <beast/core/detail/type_traits.hpp>
#include <beast/zlib/deflate_stream.hpp>
#include <beast/zlib/inflate_stream.hpp>
#include <boost/asio.hpp>
#include <algorithm>
#include <cstdint>
#include <functional>
#include <limits>
#include <type_traits>
namespace beast {
namespace websocket {
namespace detail {
class frame_test;
}
/// The type of object holding HTTP Upgrade requests
using request_type = http::request<http::empty_body>;
/// The type of object holding HTTP Upgrade responses
using response_type = http::response<http::string_body>;
/** The type of received control frame.
Values of this type are passed to the control frame
callback set using @ref stream::control_callback.
*/
enum class frame_type
{
/// A close frame was received
close,
/// A ping frame was received
ping,
/// A pong frame was received
pong
};
//--------------------------------------------------------------------
/** Provides message-oriented functionality using WebSocket.
The @ref stream class template provides asynchronous and blocking
message-oriented functionality necessary for clients and servers
to utilize the WebSocket protocol.
For asynchronous operations, the application must ensure
that they are are all performed within the same implicit
or explicit strand.
@par Thread Safety
@e Distinct @e objects: Safe.@n
@e Shared @e objects: Unsafe.
@par Example
To use the @ref stream template with an `ip::tcp::socket`,
you would write:
@code
websocket::stream<ip::tcp::socket> ws{io_service};
@endcode
Alternatively, you can write:
@code
ip::tcp::socket sock{io_service};
websocket::stream<ip::tcp::socket&> ws{sock};
@endcode
@tparam NextLayer The type representing the next layer, to which
data will be read and written during operations. For synchronous
operations, the type must support the @b SyncStream concept.
For asynchronous operations, the type must support the
@b AsyncStream concept.
@note A stream object must not be moved or destroyed while there
are pending asynchronous operations associated with it.
@par Concepts
@b AsyncStream,
@b DynamicBuffer,
@b SyncStream
*/
template<class NextLayer>
class stream
{
friend class detail::frame_test;
friend class stream_test;
buffered_read_stream<NextLayer, multi_buffer> stream_;
/// Identifies the role of a WebSockets stream.
enum class role_type
{
/// Stream is operating as a client.
client,
/// Stream is operating as a server.
server
};
friend class frame_test;
using control_cb_type =
std::function<void(frame_type, string_view)>;
struct op {};
detail::maskgen maskgen_; // source of mask keys
std::size_t rd_msg_max_ =
16 * 1024 * 1024; // max message size
bool wr_autofrag_ = true; // auto fragment
std::size_t wr_buf_size_ = 4096; // write buffer size
std::size_t rd_buf_size_ = 4096; // read buffer size
detail::opcode wr_opcode_ =
detail::opcode::text; // outgoing message type
control_cb_type ctrl_cb_; // control callback
role_type role_; // server or client
bool failed_; // the connection failed
bool wr_close_; // sent close frame
op* wr_block_; // op currenly writing
ping_data* ping_data_; // where to put the payload
detail::pausation rd_op_; // paused read op
detail::pausation wr_op_; // paused write op
detail::pausation ping_op_; // paused ping op
detail::pausation close_op_; // paused close op
close_reason cr_; // set from received close frame
// State information for the message being received
//
struct rd_t
{
// opcode of current message being read
detail::opcode op;
// `true` if the next frame is a continuation.
bool cont;
// Checks that test messages are valid utf8
detail::utf8_checker utf8;
// Size of the current message so far.
std::uint64_t size;
// Size of the read buffer.
// This gets set to the read buffer size option at the
// beginning of sending a message, so that the option can be
// changed mid-send without affecting the current message.
std::size_t buf_size;
// The read buffer. Used for compression and masking.
std::unique_ptr<std::uint8_t[]> buf;
};
rd_t rd_;
// State information for the message being sent
//
struct wr_t
{
// `true` if next frame is a continuation,
// `false` if next frame starts a new message
bool cont;
// `true` if this message should be auto-fragmented
// This gets set to the auto-fragment option at the beginning
// of sending a message, so that the option can be changed
// mid-send without affecting the current message.
bool autofrag;
// `true` if this message should be compressed.
// This gets set to the compress option at the beginning of
// of sending a message, so that the option can be changed
// mid-send without affecting the current message.
bool compress;
// Size of the write buffer.
// This gets set to the write buffer size option at the
// beginning of sending a message, so that the option can be
// changed mid-send without affecting the current message.
std::size_t buf_size;
// The write buffer. Used for compression and masking.
// The buffer is allocated or reallocated at the beginning of
// sending a message.
std::unique_ptr<std::uint8_t[]> buf;
};
wr_t wr_;
// State information for the permessage-deflate extension
struct pmd_t
{
// `true` if current read message is compressed
bool rd_set;
zlib::deflate_stream zo;
zlib::inflate_stream zi;
};
// If not engaged, then permessage-deflate is not
// enabled for the currently active session.
std::unique_ptr<pmd_t> pmd_;
// Local options for permessage-deflate
permessage_deflate pmd_opts_;
// Offer for clients, negotiated result for servers
detail::pmd_offer pmd_config_;
void
open(role_type role);
void
close();
template<class DynamicBuffer>
std::size_t
read_fh1(detail::frame_header& fh,
DynamicBuffer& db, close_code& code);
template<class DynamicBuffer>
void
read_fh2(detail::frame_header& fh,
DynamicBuffer& db, close_code& code);
// Called before receiving the first frame of each message
void
rd_begin();
// Called before sending the first frame of each message
//
void
wr_begin();
template<class DynamicBuffer>
void
write_close(DynamicBuffer& db, close_reason const& rc);
template<class DynamicBuffer>
void
write_ping(DynamicBuffer& db,
detail::opcode op, ping_data const& data);
public:
/// The type of the next layer.
using next_layer_type =
typename std::remove_reference<NextLayer>::type;
/// The type of the lowest layer.
using lowest_layer_type =
typename get_lowest_layer<next_layer_type>::type;
/** Move constructor
If @c NextLayer is move constructible, this function
will move-construct a new stream from the existing stream.
@note The behavior of move assignment on or from streams
with active or pending operations is undefined.
*/
stream(stream&&) = default;
/** Move assignment
If `NextLayer` is move assignable, this function
will move-assign a new stream from the existing stream.
@note The behavior of move assignment on or from streams
with active or pending operations is undefined.
*/
stream& operator=(stream&&) = default;
/** Constructor
This constructor creates a websocket stream and initializes
the next layer object.
@throws Any exceptions thrown by the NextLayer constructor.
@param args The arguments to be passed to initialize the
next layer object. The arguments are forwarded to the next
layer's constructor.
*/
template<class... Args>
explicit
stream(Args&&... args);
/** Destructor
@note A stream object must not be destroyed while there
are pending asynchronous operations associated with it.
*/
~stream() = default;
/** Return the `io_service` associated with the stream
This function may be used to obtain the `io_service` object
that the stream uses to dispatch handlers for asynchronous
operations.
@return A reference to the io_service object that the stream
will use to dispatch handlers.
*/
boost::asio::io_service&
get_io_service()
{
return stream_.get_io_service();
}
/** Get a reference to the next layer
This function returns a reference to the next layer
in a stack of stream layers.
@return A reference to the next layer in the stack of
stream layers.
*/
next_layer_type&
next_layer()
{
return stream_.next_layer();
}
/** Get a reference to the next layer
This function returns a reference to the next layer in a
stack of stream layers.
@return A reference to the next layer in the stack of
stream layers.
*/
next_layer_type const&
next_layer() const
{
return stream_.next_layer();
}
/** Get a reference to the lowest layer
This function returns a reference to the lowest layer
in a stack of stream layers.
@return A reference to the lowest layer in the stack of
stream layers.
*/
lowest_layer_type&
lowest_layer()
{
return stream_.lowest_layer();
}
/** Get a reference to the lowest layer
This function returns a reference to the lowest layer
in a stack of stream layers.
@return A reference to the lowest layer in the stack of
stream layers. Ownership is not transferred to the caller.
*/
lowest_layer_type const&
lowest_layer() const
{
return stream_.lowest_layer();
}
/// Set the permessage-deflate extension options
void
set_option(permessage_deflate const& o);
/// Get the permessage-deflate extension options
void
get_option(permessage_deflate& o)
{
o = pmd_opts_;
}
/** Set the automatic fragmentation option.
Determines if outgoing message payloads are broken up into
multiple pieces.
When the automatic fragmentation size is turned on, outgoing
message payloads are broken up into multiple frames no larger
than the write buffer size.
The default setting is to fragment messages.
@param v A `bool` indicating if auto fragmentation should be on.
@par Example
Setting the automatic fragmentation option:
@code
ws.auto_fragment(true);
@endcode
*/
void
auto_fragment(bool v)
{
wr_autofrag_ = v;
}
/// Returns `true` if the automatic fragmentation option is set.
bool
auto_fragment() const
{
return wr_autofrag_;
}
/** Set the binary message option.
This controls whether or not outgoing message opcodes
are set to binary or text. The setting is only applied
at the start when a caller begins a new message. Changing
the opcode after a message is started will only take effect
after the current message being sent is complete.
The default setting is to send text messages.
@param v `true` if outgoing messages should indicate
binary, or `false` if they should indicate text.
@par Example
Setting the message type to binary.
@code
ws.binary(true);
@endcode
*/
void
binary(bool v)
{
wr_opcode_ = v ?
detail::opcode::binary :
detail::opcode::text;
}
/// Returns `true` if the binary message option is set.
bool
binary() const
{
return wr_opcode_ == detail::opcode::binary;
}
/** Set the control frame callback.
Sets the callback to be invoked whenever a ping, pong,
or close control frame is received during a call to one
of the following functions:
@li @ref beast::websocket::stream::read
@li @ref beast::websocket::stream::read_frame
@li @ref beast::websocket::stream::async_read
@li @ref beast::websocket::stream::async_read_frame
Unlike completion handlers, the callback will be invoked
for each control frame during a call to any synchronous
or asynchronous read function. The operation is passive,
with no associated error code, and triggered by reads.
The signature of the callback must be:
@code
void
callback(
frame_type kind, // The type of frame
string_view payload // The payload in the frame
);
@endcode
For close frames, the close reason code may be obtained by
calling the function @ref reason.
If the read operation which receives the control frame is
an asynchronous operation, the callback will be invoked using
the same method as that used to invoke the final handler.
@note It is not necessary to send a close frame upon receipt
of a close frame. The implementation does this automatically.
Attempting to send a close frame after a close frame is
received will result in undefined behavior.
@param cb The callback to set.
*/
void
control_callback(
std::function<void(frame_type, string_view)> cb)
{
ctrl_cb_ = std::move(cb);
}
/** Set the read buffer size option.
Sets the size of the read buffer used by the implementation to
receive frames. The read buffer is needed when permessage-deflate
is used.
Lowering the size of the buffer can decrease the memory requirements
for each connection, while increasing the size of the buffer can reduce
the number of calls made to the next layer to read data.
The default setting is 4096. The minimum value is 8.
@param n The size of the read buffer.
@throws std::invalid_argument If the buffer size is less than 8.
@par Example
Setting the read buffer size.
@code
ws.read_buffer_size(16 * 1024);
@endcode
*/
void
read_buffer_size(std::size_t n)
{
if(n < 8)
BOOST_THROW_EXCEPTION(std::invalid_argument{
"read buffer size underflow"});
rd_buf_size_ = n;
}
/// Returns the read buffer size setting.
std::size_t
read_buffer_size() const
{
return rd_buf_size_;
}
/** Set the maximum incoming message size option.
Sets the largest permissible incoming message size. Message
frame fields indicating a size that would bring the total
message size over this limit will cause a protocol failure.
The default setting is 16 megabytes. A value of zero indicates
a limit of the maximum value of a `std::uint64_t`.
@par Example
Setting the maximum read message size.
@code
ws.read_message_max(65536);
@endcode
@param n The limit on the size of incoming messages.
*/
void
read_message_max(std::size_t n)
{
rd_msg_max_ = n;
}
/// Returns the maximum incoming message size setting.
std::size_t
read_message_max() const
{
return rd_msg_max_;
}
/** Set the write buffer size option.
Sets the size of the write buffer used by the implementation to
send frames. The write buffer is needed when masking payload data
in the client role, compressing frames, or auto-fragmenting message
data.
Lowering the size of the buffer can decrease the memory requirements
for each connection, while increasing the size of the buffer can reduce
the number of calls made to the next layer to write data.
The default setting is 4096. The minimum value is 8.
The write buffer size can only be changed when the stream is not
open. Undefined behavior results if the option is modified after a
successful WebSocket handshake.
@par Example
Setting the write buffer size.
@code
ws.write_buffer_size(8192);
@endcode
@param n The size of the write buffer in bytes.
*/
void
write_buffer_size(std::size_t n)
{
if(n < 8)
BOOST_THROW_EXCEPTION(std::invalid_argument{
"write buffer size underflow"});
wr_buf_size_ = n;
};
/// Returns the size of the write buffer.
std::size_t
write_buffer_size() const
{
return wr_buf_size_;
}
/** Set the text message option.
This controls whether or not outgoing message opcodes
are set to binary or text. The setting is only applied
at the start when a caller begins a new message. Changing
the opcode after a message is started will only take effect
after the current message being sent is complete.
The default setting is to send text messages.
@param v `true` if outgoing messages should indicate
text, or `false` if they should indicate binary.
@par Example
Setting the message type to text.
@code
ws.text(true);
@endcode
*/
void
text(bool v)
{
wr_opcode_ = v ?
detail::opcode::text :
detail::opcode::binary;
}
/// Returns `true` if the text message option is set.
bool
text() const
{
return wr_opcode_ == detail::opcode::text;
}
/** Returns the close reason received from the peer.
This is only valid after a read completes with error::closed.
*/
close_reason const&
reason() const
{
return cr_;
}
/** Returns `true` if the latest message data indicates binary.
This function informs the caller of whether the last
received message frame represents a message with the
binary opcode.
If there is no last message frame, the return value is
undefined.
*/
bool
got_binary()
{
return rd_.op == detail::opcode::binary;
}
/** Returns `true` if the latest message data indicates text.
This function informs the caller of whether the last
received message frame represents a message with the
text opcode.
If there is no last message frame, the return value is
undefined.
*/
bool
got_text()
{
return ! got_binary();
}
/** Read and respond to a WebSocket HTTP Upgrade request.
This function is used to synchronously read an HTTP WebSocket
Upgrade request and send the HTTP response. The call blocks
until one of the following conditions is true:
@li The request is received and the response finishes sending.
@li An error occurs on the stream.
This function is implemented in terms of one or more calls to
the next layer's `read_some` and `write_some` functions.
If the stream receives a valid HTTP WebSocket Upgrade request,
an HTTP response is sent back indicating a successful upgrade.
When this call returns, the stream is then ready to send and
receive WebSocket protocol frames and messages.
If the HTTP Upgrade request is invalid or cannot be satisfied,
an HTTP response is sent indicating the reason and status code
(typically 400, "Bad Request"). This counts as a failure.
@throws system_error Thrown on failure.
*/
void
accept();
/** Read and respond to a WebSocket HTTP Upgrade request.
This function is used to synchronously read an HTTP WebSocket
Upgrade request and send the HTTP response. The call blocks
until one of the following conditions is true:
@li The request is received and the response finishes sending.
@li An error occurs on the stream.
This function is implemented in terms of one or more calls to
the next layer's `read_some` and `write_some` functions.
If the stream receives a valid HTTP WebSocket Upgrade request,
an HTTP response is sent back indicating a successful upgrade.
When this call returns, the stream is then ready to send and
receive WebSocket protocol frames and messages.
If the HTTP Upgrade request is invalid or cannot be satisfied,
an HTTP response is sent indicating the reason and status code
(typically 400, "Bad Request"). This counts as a failure.
@param decorator A function object which will be called to modify
the HTTP response object delivered by the implementation. This
could be used to set the Server field, subprotocols, or other
application or HTTP specific fields. The object will be called
with this equivalent signature:
@code void decorator(
response_type& res
); @endcode
@throws system_error Thrown on failure.
*/
template<class ResponseDecorator>
void
accept_ex(ResponseDecorator const& decorator);
/** Read and respond to a WebSocket HTTP Upgrade request.
This function is used to synchronously read an HTTP WebSocket
Upgrade request and send the HTTP response. The call blocks
until one of the following conditions is true:
@li The request is received and the response finishes sending.
@li An error occurs on the stream.
This function is implemented in terms of one or more calls to
the next layer's `read_some` and `write_some` functions.
If the stream receives a valid HTTP WebSocket Upgrade request,
an HTTP response is sent back indicating a successful upgrade.
When this call returns, the stream is then ready to send and
receive WebSocket protocol frames and messages.
If the HTTP Upgrade request is invalid or cannot be satisfied,
an HTTP response is sent indicating the reason and status code
(typically 400, "Bad Request"). This counts as a failure.
@param ec Set to indicate what error occurred, if any.
*/
void
accept(error_code& ec);
/** Read and respond to a WebSocket HTTP Upgrade request.
This function is used to synchronously read an HTTP WebSocket
Upgrade request and send the HTTP response. The call blocks
until one of the following conditions is true:
@li The request is received and the response finishes sending.
@li An error occurs on the stream.
This function is implemented in terms of one or more calls to
the next layer's `read_some` and `write_some` functions.
If the stream receives a valid HTTP WebSocket Upgrade request,
an HTTP response is sent back indicating a successful upgrade.
When this call returns, the stream is then ready to send and
receive WebSocket protocol frames and messages.
If the HTTP Upgrade request is invalid or cannot be satisfied,
an HTTP response is sent indicating the reason and status code
(typically 400, "Bad Request"). This counts as a failure.
@param decorator A function object which will be called to modify
the HTTP response object delivered by the implementation. This
could be used to set the Server field, subprotocols, or other
application or HTTP specific fields. The object will be called
with this equivalent signature:
@code void decorator(
response_type& res
); @endcode
@param ec Set to indicate what error occurred, if any.
*/
template<class ResponseDecorator>
void
accept_ex(ResponseDecorator const& decorator,
error_code& ec);
/** Read and respond to a WebSocket HTTP Upgrade request.
This function is used to synchronously read an HTTP WebSocket
Upgrade request and send the HTTP response. The call blocks
until one of the following conditions is true:
@li The request is received and the response finishes sending.
@li An error occurs on the stream.
This function is implemented in terms of one or more calls to
the next layer's `read_some` and `write_some` functions.
If the stream receives a valid HTTP WebSocket Upgrade request,
an HTTP response is sent back indicating a successful upgrade.
When this call returns, the stream is then ready to send and
receive WebSocket protocol frames and messages.
If the HTTP Upgrade request is invalid or cannot be satisfied,
an HTTP response is sent indicating the reason and status code
(typically 400, "Bad Request"). This counts as a failure.
@param buffers Caller provided data that has already been
received on the stream. The implementation will copy the
caller provided data before the function returns.
@throws system_error Thrown on failure.
*/
template<class ConstBufferSequence>
#if BEAST_DOXYGEN
void
#else
typename std::enable_if<! http::detail::is_header<
ConstBufferSequence>::value>::type
#endif
accept(ConstBufferSequence const& buffers);
/** Read and respond to a WebSocket HTTP Upgrade request.
This function is used to synchronously read an HTTP WebSocket
Upgrade request and send the HTTP response. The call blocks
until one of the following conditions is true:
@li The request is received and the response finishes sending.
@li An error occurs on the stream.
This function is implemented in terms of one or more calls to
the next layer's `read_some` and `write_some` functions.
If the stream receives a valid HTTP WebSocket Upgrade request,
an HTTP response is sent back indicating a successful upgrade.
When this call returns, the stream is then ready to send and
receive WebSocket protocol frames and messages.
If the HTTP Upgrade request is invalid or cannot be satisfied,
an HTTP response is sent indicating the reason and status code
(typically 400, "Bad Request"). This counts as a failure.
@param buffers Caller provided data that has already been
received on the stream. The implementation will copy the
caller provided data before the function returns.
@param decorator A function object which will be called to modify
the HTTP response object delivered by the implementation. This
could be used to set the Server field, subprotocols, or other
application or HTTP specific fields. The object will be called
with this equivalent signature:
@code void decorator(
response_type& res
); @endcode
@throws system_error Thrown on failure.
*/
template<class ConstBufferSequence,
class ResponseDecorator>
#if BEAST_DOXYGEN
void
#else
typename std::enable_if<! http::detail::is_header<
ConstBufferSequence>::value>::type
#endif
accept_ex(ConstBufferSequence const& buffers,
ResponseDecorator const& decorator);
/** Read and respond to a WebSocket HTTP Upgrade request.
This function is used to synchronously read an HTTP WebSocket
Upgrade request and send the HTTP response. The call blocks
until one of the following conditions is true:
@li The request is received and the response finishes sending.
@li An error occurs on the stream.
This function is implemented in terms of one or more calls to
the next layer's `read_some` and `write_some` functions.
If the stream receives a valid HTTP WebSocket Upgrade request,
an HTTP response is sent back indicating a successful upgrade.
When this call returns, the stream is then ready to send and
receive WebSocket protocol frames and messages.
If the HTTP Upgrade request is invalid or cannot be satisfied,
an HTTP response is sent indicating the reason and status code
(typically 400, "Bad Request"). This counts as a failure.
@param buffers Caller provided data that has already been
received on the stream. The implementation will copy the
caller provided data before the function returns.
@param ec Set to indicate what error occurred, if any.
*/
template<class ConstBufferSequence>
#if BEAST_DOXYGEN
void
#else
typename std::enable_if<! http::detail::is_header<
ConstBufferSequence>::value>::type
#endif
accept(ConstBufferSequence const& buffers, error_code& ec);
/** Read and respond to a WebSocket HTTP Upgrade request.
This function is used to synchronously read an HTTP WebSocket
Upgrade request and send the HTTP response. The call blocks
until one of the following conditions is true:
@li The request is received and the response finishes sending.
@li An error occurs on the stream.
This function is implemented in terms of one or more calls to
the next layer's `read_some` and `write_some` functions.
If the stream receives a valid HTTP WebSocket Upgrade request,
an HTTP response is sent back indicating a successful upgrade.
When this call returns, the stream is then ready to send and
receive WebSocket protocol frames and messages.
If the HTTP Upgrade request is invalid or cannot be satisfied,
an HTTP response is sent indicating the reason and status code
(typically 400, "Bad Request"). This counts as a failure.
@param buffers Caller provided data that has already been
received on the stream. The implementation will copy the
caller provided data before the function returns.
@param decorator A function object which will be called to modify
the HTTP response object delivered by the implementation. This
could be used to set the Server field, subprotocols, or other
application or HTTP specific fields. The object will be called
with this equivalent signature:
@code void decorator(
response_type& res
); @endcode
@param ec Set to indicate what error occurred, if any.
*/
template<class ConstBufferSequence,
class ResponseDecorator>
#if BEAST_DOXYGEN
void
#else
typename std::enable_if<! http::detail::is_header<
ConstBufferSequence>::value>::type
#endif
accept_ex(ConstBufferSequence const& buffers,
ResponseDecorator const& decorator,
error_code& ec);
/** Respond to a WebSocket HTTP Upgrade request
This function is used to synchronously send the HTTP response
to an HTTP request possibly containing a WebSocket Upgrade.
The call blocks until one of the following conditions is true:
@li The response finishes sending.
@li An error occurs on the stream.
This function is implemented in terms of one or more calls to
the next layer's `read_some` and `write_some` functions.
If the stream receives a valid HTTP WebSocket Upgrade request,
an HTTP response is sent back indicating a successful upgrade.
When this call returns, the stream is then ready to send and
receive WebSocket protocol frames and messages.
If the HTTP Upgrade request is invalid or cannot be satisfied,
an HTTP response is sent indicating the reason and status code
(typically 400, "Bad Request"). This counts as a failure.
@param req An object containing the HTTP Upgrade request.
Ownership is not transferred, the implementation will not
access this object from other threads.
@throws system_error Thrown on failure.
*/
template<class Allocator>
void
accept(http::header<true, http::basic_fields<Allocator>> const& req);
/** Respond to a WebSocket HTTP Upgrade request
This function is used to synchronously send the HTTP response
to an HTTP request possibly containing a WebSocket Upgrade.
The call blocks until one of the following conditions is true:
@li The response finishes sending.
@li An error occurs on the stream.
This function is implemented in terms of one or more calls to
the next layer's `read_some` and `write_some` functions.
If the stream receives a valid HTTP WebSocket Upgrade request,
an HTTP response is sent back indicating a successful upgrade.
When this call returns, the stream is then ready to send and
receive WebSocket protocol frames and messages.
If the HTTP Upgrade request is invalid or cannot be satisfied,
an HTTP response is sent indicating the reason and status code
(typically 400, "Bad Request"). This counts as a failure.
@param req An object containing the HTTP Upgrade request.
Ownership is not transferred, the implementation will not
access this object from other threads.
@param decorator A function object which will be called to modify
the HTTP response object delivered by the implementation. This
could be used to set the Server field, subprotocols, or other
application or HTTP specific fields. The object will be called
with this equivalent signature:
@code void decorator(
response_type& res
); @endcode
@throws system_error Thrown on failure.
*/
template<class Allocator, class ResponseDecorator>
void
accept_ex(http::header<true,
http::basic_fields<Allocator>> const& req,
ResponseDecorator const& decorator);
/** Respond to a WebSocket HTTP Upgrade request
This function is used to synchronously send the HTTP response
to an HTTP request possibly containing a WebSocket Upgrade.
The call blocks until one of the following conditions is true:
@li The response finishes sending.
@li An error occurs on the stream.
This function is implemented in terms of one or more calls to
the next layer's `read_some` and `write_some` functions.
If the stream receives a valid HTTP WebSocket Upgrade request,
an HTTP response is sent back indicating a successful upgrade.
When this call returns, the stream is then ready to send and
receive WebSocket protocol frames and messages.
If the HTTP Upgrade request is invalid or cannot be satisfied,
an HTTP response is sent indicating the reason and status code
(typically 400, "Bad Request"). This counts as a failure.
@param req An object containing the HTTP Upgrade request.
Ownership is not transferred, the implementation will not
access this object from other threads.
@param ec Set to indicate what error occurred, if any.
*/
template<class Allocator>
void
accept(http::header<true, http::basic_fields<Allocator>> const& req,
error_code& ec);
/** Respond to a WebSocket HTTP Upgrade request
This function is used to synchronously send the HTTP response
to an HTTP request possibly containing a WebSocket Upgrade.
The call blocks until one of the following conditions is true:
@li The response finishes sending.
@li An error occurs on the stream.
This function is implemented in terms of one or more calls to
the next layer's `read_some` and `write_some` functions.
If the stream receives a valid HTTP WebSocket Upgrade request,
an HTTP response is sent back indicating a successful upgrade.
When this call returns, the stream is then ready to send and
receive WebSocket protocol frames and messages.
If the HTTP Upgrade request is invalid or cannot be satisfied,
an HTTP response is sent indicating the reason and status code
(typically 400, "Bad Request"). This counts as a failure.
@param req An object containing the HTTP Upgrade request.
Ownership is not transferred, the implementation will not
access this object from other threads.
@param decorator A function object which will be called to modify
the HTTP response object delivered by the implementation. This
could be used to set the Server field, subprotocols, or other
application or HTTP specific fields. The object will be called
with this equivalent signature:
@code void decorator(
response_type& res
); @endcode
@param ec Set to indicate what error occurred, if any.
*/
template<class Allocator, class ResponseDecorator>
void
accept_ex(http::header<true,
http::basic_fields<Allocator>> const& req,
ResponseDecorator const& decorator,
error_code& ec);
/** Respond to a WebSocket HTTP Upgrade request
This function is used to synchronously send the HTTP response
to an HTTP request possibly containing a WebSocket Upgrade.
The call blocks until one of the following conditions is true:
@li The response finishes sending.
@li An error occurs on the stream.
This function is implemented in terms of one or more calls to
the next layer's `read_some` and `write_some` functions.
If the stream receives a valid HTTP WebSocket Upgrade request,
an HTTP response is sent back indicating a successful upgrade.
When this call returns, the stream is then ready to send and
receive WebSocket protocol frames and messages.
If the HTTP Upgrade request is invalid or cannot be satisfied,
an HTTP response is sent indicating the reason and status code
(typically 400, "Bad Request"). This counts as a failure.
@param req An object containing the HTTP Upgrade request.
Ownership is not transferred, the implementation will not
access this object from other threads.
@param buffers Caller provided data that has already been
received on the stream. This must not include the octets
corresponding to the HTTP Upgrade request. The implementation
will copy the caller provided data before the function returns.
@throws system_error Thrown on failure.
*/
template<class Allocator, class ConstBufferSequence>
void
accept(http::header<true, http::basic_fields<Allocator>> const& req,
ConstBufferSequence const& buffers);
/** Respond to a WebSocket HTTP Upgrade request
This function is used to synchronously send the HTTP response
to an HTTP request possibly containing a WebSocket Upgrade.
The call blocks until one of the following conditions is true:
@li The response finishes sending.
@li An error occurs on the stream.
This function is implemented in terms of one or more calls to
the next layer's `read_some` and `write_some` functions.
If the stream receives a valid HTTP WebSocket Upgrade request,
an HTTP response is sent back indicating a successful upgrade.
When this call returns, the stream is then ready to send and
receive WebSocket protocol frames and messages.
If the HTTP Upgrade request is invalid or cannot be satisfied,
an HTTP response is sent indicating the reason and status code
(typically 400, "Bad Request"). This counts as a failure.
@param req An object containing the HTTP Upgrade request.
Ownership is not transferred, the implementation will not
access this object from other threads.
@param buffers Caller provided data that has already been
received on the stream. This must not include the octets
corresponding to the HTTP Upgrade request. The implementation
will copy the caller provided data before the function returns.
@param decorator A function object which will be called to modify
the HTTP response object delivered by the implementation. This
could be used to set the Server field, subprotocols, or other
application or HTTP specific fields. The object will be called
with this equivalent signature:
@code void decorator(
response_type& res
); @endcode
@throws system_error Thrown on failure.
*/
template<class Allocator, class ConstBufferSequence,
class ResponseDecorator>
void
accept_ex(http::header<true,
http::basic_fields<Allocator>> const& req,
ConstBufferSequence const& buffers,
ResponseDecorator const& decorator);
/** Respond to a WebSocket HTTP Upgrade request
This function is used to synchronously send the HTTP response
to an HTTP request possibly containing a WebSocket Upgrade.
The call blocks until one of the following conditions is true:
@li The response finishes sending.
@li An error occurs on the stream.
This function is implemented in terms of one or more calls to
the next layer's `read_some` and `write_some` functions.
If the stream receives a valid HTTP WebSocket Upgrade request,
an HTTP response is sent back indicating a successful upgrade.
When this call returns, the stream is then ready to send and
receive WebSocket protocol frames and messages.
If the HTTP Upgrade request is invalid or cannot be satisfied,
an HTTP response is sent indicating the reason and status code
(typically 400, "Bad Request"). This counts as a failure.
@param req An object containing the HTTP Upgrade request.
Ownership is not transferred, the implementation will not
access this object from other threads.
@param buffers Caller provided data that has already been
received on the stream. This must not include the octets
corresponding to the HTTP Upgrade request. The implementation
will copy the caller provided data before the function returns.
@param ec Set to indicate what error occurred, if any.
*/
template<class Allocator, class ConstBufferSequence>
void
accept(http::header<true, Allocator> const& req,
ConstBufferSequence const& buffers, error_code& ec);
/** Respond to a WebSocket HTTP Upgrade request
This function is used to synchronously send the HTTP response
to an HTTP request possibly containing a WebSocket Upgrade.
The call blocks until one of the following conditions is true:
@li The response finishes sending.
@li An error occurs on the stream.
This function is implemented in terms of one or more calls to
the next layer's `read_some` and `write_some` functions.
If the stream receives a valid HTTP WebSocket Upgrade request,
an HTTP response is sent back indicating a successful upgrade.
When this call returns, the stream is then ready to send and
receive WebSocket protocol frames and messages.
If the HTTP Upgrade request is invalid or cannot be satisfied,
an HTTP response is sent indicating the reason and status code
(typically 400, "Bad Request"). This counts as a failure.
@param req An object containing the HTTP Upgrade request.
Ownership is not transferred, the implementation will not
access this object from other threads.
@param buffers Caller provided data that has already been
received on the stream. This must not include the octets
corresponding to the HTTP Upgrade request. The implementation
will copy the caller provided data before the function returns.
@param decorator A function object which will be called to modify
the HTTP response object delivered by the implementation. This
could be used to set the Server field, subprotocols, or other
application or HTTP specific fields. The object will be called
with this equivalent signature:
@code void decorator(
response_type& res
); @endcode
@param ec Set to indicate what error occurred, if any.
*/
template<class Allocator, class ConstBufferSequence,
class ResponseDecorator>
void
accept_ex(http::header<true,
http::basic_fields<Allocator>> const& req,
ConstBufferSequence const& buffers,
ResponseDecorator const& decorator,
error_code& ec);
/** Start reading and responding to a WebSocket HTTP Upgrade request.
This function is used to asynchronously read an HTTP WebSocket
Upgrade request and send the HTTP response. The function call
always returns immediately. The asynchronous operation will
continue until one of the following conditions is true:
@li The request is received and the response finishes sending.
@li An error occurs on the stream.
This operation is implemented in terms of one or more calls to
the next layer's `async_read_some` and `async_write_some`
functions, and is known as a <em>composed operation</em>. The
program must ensure that the stream performs no other
asynchronous operations until this operation completes.
If the stream receives a valid HTTP WebSocket Upgrade request,
an HTTP response is sent back indicating a successful upgrade.
When the completion handler is invoked, the stream is then
ready to send and receive WebSocket protocol frames and
messages.
If the HTTP Upgrade request is invalid or cannot be satisfied,
an HTTP response is sent indicating the reason and status code
(typically 400, "Bad Request"). This counts as a failure, and
the completion handler will be invoked with a suitable error
code set.
@param handler The handler to be called when the request
completes. Copies will be made of the handler as required. The
equivalent function signature of the handler must be:
@code void handler(
error_code const& ec // Result of operation
); @endcode
Regardless of whether the asynchronous operation completes
immediately or not, the handler will not be invoked from within
this function. Invocation of the handler will be performed in a
manner equivalent to using `boost::asio::io_service::post`.
*/
template<class AcceptHandler>
#if BEAST_DOXYGEN
void_or_deduced
#else
async_return_type<
AcceptHandler, void(error_code)>
#endif
async_accept(AcceptHandler&& handler);
/** Start reading and responding to a WebSocket HTTP Upgrade request.
This function is used to asynchronously read an HTTP WebSocket
Upgrade request and send the HTTP response. The function call
always returns immediately. The asynchronous operation will
continue until one of the following conditions is true:
@li The request is received and the response finishes sending.
@li An error occurs on the stream.
This operation is implemented in terms of one or more calls to
the next layer's `async_read_some` and `async_write_some`
functions, and is known as a <em>composed operation</em>. The
program must ensure that the stream performs no other
asynchronous operations until this operation completes.
If the stream receives a valid HTTP WebSocket Upgrade request,
an HTTP response is sent back indicating a successful upgrade.
When the completion handler is invoked, the stream is then
ready to send and receive WebSocket protocol frames and
messages.
If the HTTP Upgrade request is invalid or cannot be satisfied,
an HTTP response is sent indicating the reason and status code
(typically 400, "Bad Request"). This counts as a failure, and
the completion handler will be invoked with a suitable error
code set.
@param decorator A function object which will be called to modify
the HTTP response object delivered by the implementation. This
could be used to set the Server field, subprotocols, or other
application or HTTP specific fields. The object will be called
with this equivalent signature:
@code void decorator(
response_type& res
); @endcode
@param handler The handler to be called when the request
completes. Copies will be made of the handler as required. The
equivalent function signature of the handler must be:
@code void handler(
error_code const& ec // Result of operation
); @endcode
Regardless of whether the asynchronous operation completes
immediately or not, the handler will not be invoked from within
this function. Invocation of the handler will be performed in a
manner equivalent to using `boost::asio::io_service::post`.
*/
template<class ResponseDecorator, class AcceptHandler>
#if BEAST_DOXYGEN
void_or_deduced
#else
async_return_type<
AcceptHandler, void(error_code)>
#endif
async_accept_ex(ResponseDecorator const& decorator,
AcceptHandler&& handler);
/** Start reading and responding to a WebSocket HTTP Upgrade request.
This function is used to asynchronously read an HTTP WebSocket
Upgrade request and send the HTTP response. The function call
always returns immediately. The asynchronous operation will
continue until one of the following conditions is true:
@li The request is received and the response finishes sending.
@li An error occurs on the stream.
This operation is implemented in terms of one or more calls to
the next layer's `async_read_some` and `async_write_some`
functions, and is known as a <em>composed operation</em>. The
program must ensure that the stream performs no other
asynchronous operations until this operation completes.
If the stream receives a valid HTTP WebSocket Upgrade request,
an HTTP response is sent back indicating a successful upgrade.
When the completion handler is invoked, the stream is then
ready to send and receive WebSocket protocol frames and
messages.
If the HTTP Upgrade request is invalid or cannot be satisfied,
an HTTP response is sent indicating the reason and status code
(typically 400, "Bad Request"). This counts as a failure, and
the completion handler will be invoked with a suitable error
code set.
@param buffers Caller provided data that has already been
received on the stream. This may be used for implementations
allowing multiple protocols on the same stream. The
buffered data will first be applied to the handshake, and
then to received WebSocket frames. The implementation will
copy the caller provided data before the function returns.
@param handler The handler to be called when the request
completes. Copies will be made of the handler as required. The
equivalent function signature of the handler must be:
@code void handler(
error_code const& ec // Result of operation
); @endcode
Regardless of whether the asynchronous operation completes
immediately or not, the handler will not be invoked from within
this function. Invocation of the handler will be performed in a
manner equivalent to using `boost::asio::io_service::post`.
*/
template<class ConstBufferSequence, class AcceptHandler>
#if BEAST_DOXYGEN
void_or_deduced
#else
typename std::enable_if<
! http::detail::is_header<ConstBufferSequence>::value,
async_return_type<AcceptHandler, void(error_code)>>::type
#endif
async_accept(ConstBufferSequence const& buffers,
AcceptHandler&& handler);
/** Start reading and responding to a WebSocket HTTP Upgrade request.
This function is used to asynchronously read an HTTP WebSocket
Upgrade request and send the HTTP response. The function call
always returns immediately. The asynchronous operation will
continue until one of the following conditions is true:
@li The request is received and the response finishes sending.
@li An error occurs on the stream.
This operation is implemented in terms of one or more calls to
the next layer's `async_read_some` and `async_write_some`
functions, and is known as a <em>composed operation</em>. The
program must ensure that the stream performs no other
asynchronous operations until this operation completes.
If the stream receives a valid HTTP WebSocket Upgrade request,
an HTTP response is sent back indicating a successful upgrade.
When the completion handler is invoked, the stream is then
ready to send and receive WebSocket protocol frames and
messages.
If the HTTP Upgrade request is invalid or cannot be satisfied,
an HTTP response is sent indicating the reason and status code
(typically 400, "Bad Request"). This counts as a failure, and
the completion handler will be invoked with a suitable error
code set.
@param buffers Caller provided data that has already been
received on the stream. This may be used for implementations
allowing multiple protocols on the same stream. The
buffered data will first be applied to the handshake, and
then to received WebSocket frames. The implementation will
copy the caller provided data before the function returns.
@param decorator A function object which will be called to modify
the HTTP response object delivered by the implementation. This
could be used to set the Server field, subprotocols, or other
application or HTTP specific fields. The object will be called
with this equivalent signature:
@code void decorator(
response_type& res
); @endcode
@param handler The handler to be called when the request
completes. Copies will be made of the handler as required. The
equivalent function signature of the handler must be:
@code void handler(
error_code const& ec // Result of operation
); @endcode
Regardless of whether the asynchronous operation completes
immediately or not, the handler will not be invoked from within
this function. Invocation of the handler will be performed in a
manner equivalent to using `boost::asio::io_service::post`.
*/
template<class ConstBufferSequence,
class ResponseDecorator, class AcceptHandler>
#if BEAST_DOXYGEN
void_or_deduced
#else
typename std::enable_if<
! http::detail::is_header<ConstBufferSequence>::value,
async_return_type<AcceptHandler, void(error_code)>>::type
#endif
async_accept_ex(ConstBufferSequence const& buffers,
ResponseDecorator const& decorator,
AcceptHandler&& handler);
/** Start responding to a WebSocket HTTP Upgrade request.
This function is used to asynchronously send the HTTP response
to an HTTP request possibly containing a WebSocket Upgrade
request. The function call always returns immediately. The
asynchronous operation will continue until one of the following
conditions is true:
@li The response finishes sending.
@li An error occurs on the stream.
This operation is implemented in terms of one or more calls to
the next layer's `async_write_some` functions, and is known as
a <em>composed operation</em>. The program must ensure that the
stream performs no other operations until this operation
completes.
If the stream receives a valid HTTP WebSocket Upgrade request,
an HTTP response is sent back indicating a successful upgrade.
When the completion handler is invoked, the stream is then
ready to send and receive WebSocket protocol frames and
messages.
If the HTTP Upgrade request is invalid or cannot be satisfied,
an HTTP response is sent indicating the reason and status code
(typically 400, "Bad Request"). This counts as a failure, and
the completion handler will be invoked with a suitable error
code set.
@param req An object containing the HTTP Upgrade request.
Ownership is not transferred, the implementation will not access
this object from other threads.
@param handler The handler to be called when the request
completes. Copies will be made of the handler as required. The
equivalent function signature of the handler must be:
@code void handler(
error_code const& ec // Result of operation
); @endcode
Regardless of whether the asynchronous operation completes
immediately or not, the handler will not be invoked from within
this function. Invocation of the handler will be performed in a
manner equivalent to using `boost::asio::io_service::post`.
*/
template<class Allocator, class AcceptHandler>
#if BEAST_DOXYGEN
void_or_deduced
#else
async_return_type<
AcceptHandler, void(error_code)>
#endif
async_accept(http::header<true,
http::basic_fields<Allocator>> const& req,
AcceptHandler&& handler);
/** Start responding to a WebSocket HTTP Upgrade request.
This function is used to asynchronously send the HTTP response
to an HTTP request possibly containing a WebSocket Upgrade
request. The function call always returns immediately. The
asynchronous operation will continue until one of the following
conditions is true:
@li The response finishes sending.
@li An error occurs on the stream.
This operation is implemented in terms of one or more calls to
the next layer's `async_write_some` functions, and is known as
a <em>composed operation</em>. The program must ensure that the
stream performs no other operations until this operation
completes.
If the stream receives a valid HTTP WebSocket Upgrade request,
an HTTP response is sent back indicating a successful upgrade.
When the completion handler is invoked, the stream is then
ready to send and receive WebSocket protocol frames and
messages.
If the HTTP Upgrade request is invalid or cannot be satisfied,
an HTTP response is sent indicating the reason and status code
(typically 400, "Bad Request"). This counts as a failure, and
the completion handler will be invoked with a suitable error
code set.
@param req An object containing the HTTP Upgrade request.
Ownership is not transferred, the implementation will not access
this object from other threads.
@param decorator A function object which will be called to modify
the HTTP response object delivered by the implementation. This
could be used to set the Server field, subprotocols, or other
application or HTTP specific fields. The object will be called
with this equivalent signature:
@code void decorator(
response_type& res
); @endcode
@param handler The handler to be called when the request
completes. Copies will be made of the handler as required. The
equivalent function signature of the handler must be:
@code void handler(
error_code const& ec // Result of operation
); @endcode
Regardless of whether the asynchronous operation completes
immediately or not, the handler will not be invoked from within
this function. Invocation of the handler will be performed in a
manner equivalent to using `boost::asio::io_service::post`.
*/
template<class Allocator,
class ResponseDecorator, class AcceptHandler>
#if BEAST_DOXYGEN
void_or_deduced
#else
async_return_type<
AcceptHandler, void(error_code)>
#endif
async_accept_ex(http::header<true,
http::basic_fields<Allocator>> const& req,
ResponseDecorator const& decorator,
AcceptHandler&& handler);
/** Start responding to a WebSocket HTTP Upgrade request.
This function is used to asynchronously send the HTTP response
to an HTTP request possibly containing a WebSocket Upgrade
request. The function call always returns immediately. The
asynchronous operation will continue until one of the following
conditions is true:
@li The response finishes sending.
@li An error occurs on the stream.
This operation is implemented in terms of one or more calls to
the next layer's `async_write_some` functions, and is known as
a <em>composed operation</em>. The program must ensure that the
stream performs no other operations until this operation
completes.
If the stream receives a valid HTTP WebSocket Upgrade request,
an HTTP response is sent back indicating a successful upgrade.
When the completion handler is invoked, the stream is then
ready to send and receive WebSocket protocol frames and
messages.
If the HTTP Upgrade request is invalid or cannot be satisfied,
an HTTP response is sent indicating the reason and status code
(typically 400, "Bad Request"). This counts as a failure, and
the completion handler will be invoked with a suitable error
code set.
@param req An object containing the HTTP Upgrade request.
Ownership is not transferred, the implementation will not access
this object from other threads.
@param buffers Caller provided data that has already been
received on the stream. This may be used for implementations
allowing multiple protocols on the same stream. The
buffered data will first be applied to the handshake, and
then to received WebSocket frames. The implementation will
copy the caller provided data before the function returns.
@param handler The handler to be called when the request
completes. Copies will be made of the handler as required. The
equivalent function signature of the handler must be:
@code void handler(
error_code const& ec // Result of operation
); @endcode
Regardless of whether the asynchronous operation completes
immediately or not, the handler will not be invoked from within
this function. Invocation of the handler will be performed in a
manner equivalent to using `boost::asio::io_service::post`.
*/
template<class Allocator,
class ConstBufferSequence, class AcceptHandler>
#if BEAST_DOXYGEN
void_or_deduced
#else
async_return_type<
AcceptHandler, void(error_code)>
#endif
async_accept(http::header<true,
http::basic_fields<Allocator>> const& req,
ConstBufferSequence const& buffers,
AcceptHandler&& handler);
/** Start responding to a WebSocket HTTP Upgrade request.
This function is used to asynchronously send the HTTP response
to an HTTP request possibly containing a WebSocket Upgrade
request. The function call always returns immediately. The
asynchronous operation will continue until one of the following
conditions is true:
@li The response finishes sending.
@li An error occurs on the stream.
This operation is implemented in terms of one or more calls to
the next layer's `async_write_some` functions, and is known as
a <em>composed operation</em>. The program must ensure that the
stream performs no other operations until this operation
completes.
If the stream receives a valid HTTP WebSocket Upgrade request,
an HTTP response is sent back indicating a successful upgrade.
When the completion handler is invoked, the stream is then
ready to send and receive WebSocket protocol frames and
messages.
If the HTTP Upgrade request is invalid or cannot be satisfied,
an HTTP response is sent indicating the reason and status code
(typically 400, "Bad Request"). This counts as a failure, and
the completion handler will be invoked with a suitable error
code set.
@param req An object containing the HTTP Upgrade request.
Ownership is not transferred, the implementation will not access
this object from other threads.
@param buffers Caller provided data that has already been
received on the stream. This may be used for implementations
allowing multiple protocols on the same stream. The
buffered data will first be applied to the handshake, and
then to received WebSocket frames. The implementation will
copy the caller provided data before the function returns.
@param decorator A function object which will be called to modify
the HTTP response object delivered by the implementation. This
could be used to set the Server field, subprotocols, or other
application or HTTP specific fields. The object will be called
with this equivalent signature:
@code void decorator(
response_type& res
); @endcode
@param handler The handler to be called when the request
completes. Copies will be made of the handler as required. The
equivalent function signature of the handler must be:
@code void handler(
error_code const& ec // Result of operation
); @endcode
Regardless of whether the asynchronous operation completes
immediately or not, the handler will not be invoked from within
this function. Invocation of the handler will be performed in a
manner equivalent to using `boost::asio::io_service::post`.
*/
template<class Allocator, class ConstBufferSequence,
class ResponseDecorator, class AcceptHandler>
#if BEAST_DOXYGEN
void_or_deduced
#else
async_return_type<
AcceptHandler, void(error_code)>
#endif
async_accept_ex(http::header<true,
http::basic_fields<Allocator>> const& req,
ConstBufferSequence const& buffers,
ResponseDecorator const& decorator,
AcceptHandler&& handler);
/** Send an HTTP WebSocket Upgrade request and receive the response.
This function is used to synchronously send the WebSocket
upgrade HTTP request. The call blocks until one of the
following conditions is true:
@li The request is sent and the response is received.
@li An error occurs on the stream
This function is implemented in terms of one or more calls to the
next layer's `read_some` and `write_some` functions.
The operation is successful if the received HTTP response indicates
a successful HTTP Upgrade (represented by a Status-Code of 101,
"switching protocols").
@param host The name of the remote host,
required by the HTTP protocol.
@param target The Request Target, which may not be empty,
required by the HTTP protocol.
@throws system_error Thrown on failure.
@par Example
@code
websocket::stream<ip::tcp::socket> ws{io_service};
...
try
{
ws.handshake("localhost", "/");
}
catch(...)
{
// An error occurred.
}
@endcode
*/
void
handshake(string_view host, string_view target);
/** Send an HTTP WebSocket Upgrade request and receive the response.
This function is used to synchronously send the WebSocket
upgrade HTTP request. The call blocks until one of the
following conditions is true:
@li The request is sent and the response is received.
@li An error occurs on the stream
This function is implemented in terms of one or more calls to the
next layer's `read_some` and `write_some` functions.
The operation is successful if the received HTTP response indicates
a successful HTTP Upgrade (represented by a Status-Code of 101,
"switching protocols").
@param res The HTTP Upgrade response returned by the remote
endpoint.
@param host The name of the remote host,
required by the HTTP protocol.
@param target The Request Target, which may not be empty,
required by the HTTP protocol.
@throws system_error Thrown on failure.
@par Example
@code
websocket::stream<ip::tcp::socket> ws{io_service};
...
try
{
response_type res;
ws.handshake(res, "localhost", "/");
}
catch(...)
{
// An error occurred.
}
@endcode
*/
void
handshake(response_type& res,
string_view host, string_view target);
/** Send an HTTP WebSocket Upgrade request and receive the response.
This function is used to synchronously send the WebSocket
upgrade HTTP request. The call blocks until one of the
following conditions is true:
@li The request is sent and the response is received.
@li An error occurs on the stream
This function is implemented in terms of one or more calls to the
next layer's `read_some` and `write_some` functions.
The operation is successful if the received HTTP response indicates
a successful HTTP Upgrade (represented by a Status-Code of 101,
"switching protocols").
@param host The name of the remote host,
required by the HTTP protocol.
@param target The Request Target, which may not be empty,
required by the HTTP protocol.
@param decorator A function object which will be called to modify
the HTTP request object generated by the implementation. This
could be used to set the User-Agent field, subprotocols, or other
application or HTTP specific fields. The object will be called
with this equivalent signature:
@code void decorator(
request_type& req
); @endcode
@throws system_error Thrown on failure.
@par Example
@code
websocket::stream<ip::tcp::socket> ws{io_service};
...
try
{
ws.handshake("localhost", "/",
[](request_type& req)
{
req.set(field::user_agent, "Beast");
});
}
catch(...)
{
// An error occurred.
}
@endcode
*/
template<class RequestDecorator>
void
handshake_ex(string_view host, string_view target,
RequestDecorator const& decorator);
/** Send an HTTP WebSocket Upgrade request and receive the response.
This function is used to synchronously send the WebSocket
upgrade HTTP request. The call blocks until one of the
following conditions is true:
@li The request is sent and the response is received.
@li An error occurs on the stream
This function is implemented in terms of one or more calls to the
next layer's `read_some` and `write_some` functions.
The operation is successful if the received HTTP response indicates
a successful HTTP Upgrade (represented by a Status-Code of 101,
"switching protocols").
@param res The HTTP Upgrade response returned by the remote
endpoint.
@param host The name of the remote host,
required by the HTTP protocol.
@param target The Request Target, which may not be empty,
required by the HTTP protocol.
@param decorator A function object which will be called to modify
the HTTP request object generated by the implementation. This
could be used to set the User-Agent field, subprotocols, or other
application or HTTP specific fields. The object will be called
with this equivalent signature:
@code void decorator(
request_type& req
); @endcode
@throws system_error Thrown on failure.
@par Example
@code
websocket::stream<ip::tcp::socket> ws{io_service};
...
try
{
response_type res;
ws.handshake(res, "localhost", "/",
[](request_type& req)
{
req.set(field::user_agent, "Beast");
});
}
catch(...)
{
// An error occurred.
}
@endcode
*/
template<class RequestDecorator>
void
handshake_ex(response_type& res,
string_view host, string_view target,
RequestDecorator const& decorator);
/** Send an HTTP WebSocket Upgrade request and receive the response.
This function is used to synchronously send the WebSocket
upgrade HTTP request. The call blocks until one of the
following conditions is true:
@li The request is sent and the response is received.
@li An error occurs on the stream
This function is implemented in terms of one or more calls to the
next layer's `read_some` and `write_some` functions.
The operation is successful if the received HTTP response indicates
a successful HTTP Upgrade (represented by a Status-Code of 101,
"switching protocols").
@param host The name of the remote host,
required by the HTTP protocol.
@param target The Request Target, which may not be empty,
required by the HTTP protocol.
@param ec Set to indicate what error occurred, if any.
@par Example
@code
websocket::stream<ip::tcp::socket> ws{io_service};
...
error_code ec;
ws.handshake(host, target, ec);
if(ec)
{
// An error occurred.
}
@endcode
*/
void
handshake(string_view host,
string_view target, error_code& ec);
/** Send an HTTP WebSocket Upgrade request and receive the response.
This function is used to synchronously send the WebSocket
upgrade HTTP request. The call blocks until one of the
following conditions is true:
@li The request is sent and the response is received.
@li An error occurs on the stream
This function is implemented in terms of one or more calls to the
next layer's `read_some` and `write_some` functions.
The operation is successful if the received HTTP response indicates
a successful HTTP Upgrade (represented by a Status-Code of 101,
"switching protocols").
@param host The name of the remote host,
required by the HTTP protocol.
@param target The Request Target, which may not be empty,
required by the HTTP protocol.
@param ec Set to indicate what error occurred, if any.
@param res The HTTP Upgrade response returned by the remote
endpoint. If `ec is set, the return value is undefined.
@par Example
@code
websocket::stream<ip::tcp::socket> ws{io_service};
...
error_code ec;
response_type res;
ws.handshake(res, host, target, ec);
if(ec)
{
// An error occurred.
}
@endcode
*/
void
handshake(response_type& res,
string_view host, string_view target, error_code& ec);
/** Send an HTTP WebSocket Upgrade request and receive the response.
This function is used to synchronously send the WebSocket
upgrade HTTP request. The call blocks until one of the
following conditions is true:
@li The request is sent and the response is received.
@li An error occurs on the stream
This function is implemented in terms of one or more calls to the
next layer's `read_some` and `write_some` functions.
The operation is successful if the received HTTP response indicates
a successful HTTP Upgrade (represented by a Status-Code of 101,
"switching protocols").
@param host The name of the remote host,
required by the HTTP protocol.
@param target The Request Target, which may not be empty,
required by the HTTP protocol.
@param decorator A function object which will be called to modify
the HTTP request object generated by the implementation. This
could be used to set the User-Agent field, subprotocols, or other
application or HTTP specific fields. The object will be called
with this equivalent signature:
@code void decorator(
request_type& req
); @endcode
@param ec Set to indicate what error occurred, if any.
@par Example
@code
websocket::stream<ip::tcp::socket> ws{io_service};
...
error_code ec;
ws.handshake("localhost", "/",
[](request_type& req)
{
req.set(field::user_agent, "Beast");
},
ec);
if(ec)
{
// An error occurred.
}
@endcode
*/
template<class RequestDecorator>
void
handshake_ex(string_view host,
string_view target, RequestDecorator const& decorator,
error_code& ec);
/** Send an HTTP WebSocket Upgrade request and receive the response.
This function is used to synchronously send the WebSocket
upgrade HTTP request. The call blocks until one of the
following conditions is true:
@li The request is sent and the response is received.
@li An error occurs on the stream
This function is implemented in terms of one or more calls to the
next layer's `read_some` and `write_some` functions.
The operation is successful if the received HTTP response indicates
a successful HTTP Upgrade (represented by a Status-Code of 101,
"switching protocols").
@param res The HTTP Upgrade response returned by the remote
endpoint.
@param host The name of the remote host,
required by the HTTP protocol.
@param target The Request Target, which may not be empty,
required by the HTTP protocol.
@param decorator A function object which will be called to modify
the HTTP request object generated by the implementation. This
could be used to set the User-Agent field, subprotocols, or other
application or HTTP specific fields. The object will be called
with this equivalent signature:
@code void decorator(
request_type& req
); @endcode
@param ec Set to indicate what error occurred, if any.
@par Example
@code
websocket::stream<ip::tcp::socket> ws{io_service};
...
error_code ec;
response_type res;
ws.handshake(res, "localhost", "/",
[](request_type& req)
{
req.set(field::user_agent, "Beast");
},
ec);
if(ec)
{
// An error occurred.
}
@endcode
*/
template<class RequestDecorator>
void
handshake_ex(response_type& res,
string_view host, string_view target,
RequestDecorator const& decorator, error_code& ec);
/** Start an asynchronous operation to send an upgrade request and receive the response.
This function is used to asynchronously send the HTTP WebSocket
upgrade request and receive the HTTP WebSocket Upgrade response.
This function call always returns immediately. The asynchronous
operation will continue until one of the following conditions is
true:
@li The request is sent and the response is received.
@li An error occurs on the stream
This operation is implemented in terms of one or more calls to the
next layer's `async_read_some` and `async_write_some` functions, and
is known as a <em>composed operation</em>. The program must ensure
that the stream performs no other operations until this operation
completes.
The operation is successful if the received HTTP response indicates
a successful HTTP Upgrade (represented by a Status-Code of 101,
"switching protocols").
@param host The name of the remote host, required by
the HTTP protocol. Copies may be made as needed.
@param target The Request Target, which may not be empty,
required by the HTTP protocol. Copies of this parameter may
be made as needed.
@param handler The handler to be called when the request completes.
Copies will be made of the handler as required. The equivalent
function signature of the handler must be:
@code void handler(
error_code const& ec // Result of operation
); @endcode
Regardless of whether the asynchronous operation completes
immediately or not, the handler will not be invoked from within
this function. Invocation of the handler will be performed in a
manner equivalent to using `boost::asio::io_service::post`.
*/
template<class HandshakeHandler>
#if BEAST_DOXYGEN
void_or_deduced
#else
async_return_type<
HandshakeHandler, void(error_code)>
#endif
async_handshake(string_view host,
string_view target, HandshakeHandler&& handler);
/** Start an asynchronous operation to send an upgrade request and receive the response.
This function is used to asynchronously send the HTTP WebSocket
upgrade request and receive the HTTP WebSocket Upgrade response.
This function call always returns immediately. The asynchronous
operation will continue until one of the following conditions is
true:
@li The request is sent and the response is received.
@li An error occurs on the stream
This operation is implemented in terms of one or more calls to the
next layer's `async_read_some` and `async_write_some` functions, and
is known as a <em>composed operation</em>. The program must ensure
that the stream performs no other operations until this operation
completes.
The operation is successful if the received HTTP response indicates
a successful HTTP Upgrade (represented by a Status-Code of 101,
"switching protocols").
@param res The HTTP Upgrade response returned by the remote
endpoint. The caller must ensure this object is valid for at
least until the completion handler is invoked.
@param host The name of the remote host, required by
the HTTP protocol. Copies may be made as needed.
@param target The Request Target, which may not be empty,
required by the HTTP protocol. Copies of this parameter may
be made as needed.
@param handler The handler to be called when the request completes.
Copies will be made of the handler as required. The equivalent
function signature of the handler must be:
@code void handler(
error_code const& ec // Result of operation
); @endcode
Regardless of whether the asynchronous operation completes
immediately or not, the handler will not be invoked from within
this function. Invocation of the handler will be performed in a
manner equivalent to using `boost::asio::io_service::post`.
*/
template<class HandshakeHandler>
#if BEAST_DOXYGEN
void_or_deduced
#else
async_return_type<
HandshakeHandler, void(error_code)>
#endif
async_handshake(response_type& res,
string_view host, string_view target,
HandshakeHandler&& handler);
/** Start an asynchronous operation to send an upgrade request and receive the response.
This function is used to asynchronously send the HTTP WebSocket
upgrade request and receive the HTTP WebSocket Upgrade response.
This function call always returns immediately. The asynchronous
operation will continue until one of the following conditions is
true:
@li The request is sent and the response is received.
@li An error occurs on the stream
This operation is implemented in terms of one or more calls to the
next layer's `async_read_some` and `async_write_some` functions, and
is known as a <em>composed operation</em>. The program must ensure
that the stream performs no other operations until this operation
completes.
The operation is successful if the received HTTP response indicates
a successful HTTP Upgrade (represented by a Status-Code of 101,
"switching protocols").
@param host The name of the remote host, required by
the HTTP protocol. Copies may be made as needed.
@param target The Request Target, which may not be empty,
required by the HTTP protocol. Copies of this parameter may
be made as needed.
@param decorator A function object which will be called to modify
the HTTP request object generated by the implementation. This
could be used to set the User-Agent field, subprotocols, or other
application or HTTP specific fields. The object will be called
with this equivalent signature:
@code void decorator(
request_type& req
); @endcode
@param handler The handler to be called when the request completes.
Copies will be made of the handler as required. The equivalent
function signature of the handler must be:
@code void handler(
error_code const& ec // Result of operation
); @endcode
Regardless of whether the asynchronous operation completes
immediately or not, the handler will not be invoked from within
this function. Invocation of the handler will be performed in a
manner equivalent to using `boost::asio::io_service::post`.
*/
template<class RequestDecorator, class HandshakeHandler>
#if BEAST_DOXYGEN
void_or_deduced
#else
async_return_type<
HandshakeHandler, void(error_code)>
#endif
async_handshake_ex(string_view host,
string_view target, RequestDecorator const& decorator,
HandshakeHandler&& handler);
/** Start an asynchronous operation to send an upgrade request and receive the response.
This function is used to asynchronously send the HTTP WebSocket
upgrade request and receive the HTTP WebSocket Upgrade response.
This function call always returns immediately. The asynchronous
operation will continue until one of the following conditions is
true:
@li The request is sent and the response is received.
@li An error occurs on the stream
This operation is implemented in terms of one or more calls to the
next layer's `async_read_some` and `async_write_some` functions, and
is known as a <em>composed operation</em>. The program must ensure
that the stream performs no other operations until this operation
completes.
The operation is successful if the received HTTP response indicates
a successful HTTP Upgrade (represented by a Status-Code of 101,
"switching protocols").
@param res The HTTP Upgrade response returned by the remote
endpoint. The caller must ensure this object is valid for at
least until the completion handler is invoked.
@param host The name of the remote host, required by
the HTTP protocol. Copies may be made as needed.
@param target The Request Target, which may not be empty,
required by the HTTP protocol. Copies of this parameter may
be made as needed.
@param decorator A function object which will be called to modify
the HTTP request object generated by the implementation. This
could be used to set the User-Agent field, subprotocols, or other
application or HTTP specific fields. The object will be called
with this equivalent signature:
@code void decorator(
request_type& req
); @endcode
@param handler The handler to be called when the request completes.
Copies will be made of the handler as required. The equivalent
function signature of the handler must be:
@code void handler(
error_code const& ec // Result of operation
); @endcode
Regardless of whether the asynchronous operation completes
immediately or not, the handler will not be invoked from within
this function. Invocation of the handler will be performed in a
manner equivalent to using `boost::asio::io_service::post`.
*/
template<class RequestDecorator, class HandshakeHandler>
#if BEAST_DOXYGEN
void_or_deduced
#else
async_return_type<
HandshakeHandler, void(error_code)>
#endif
async_handshake_ex(response_type& res,
string_view host, string_view target,
RequestDecorator const& decorator,
HandshakeHandler&& handler);
/** Send a WebSocket close frame.
This function is used to synchronously send a close frame on
the stream. The call blocks until one of the following is true:
@li The close frame finishes sending.
@li An error occurs on the stream.
This function is implemented in terms of one or more calls
to the next layer's `write_some` functions.
If the close reason specifies a close code other than
@ref beast::websocket::close_code::none, the close frame is
sent with the close code and optional reason string. Otherwise,
the close frame is sent with no payload.
Callers should not attempt to write WebSocket data after
initiating the close. Instead, callers should continue
reading until an error occurs. A read returning @ref error::closed
indicates a successful connection closure.
@param cr The reason for the close.
@throws system_error Thrown on failure.
*/
void
close(close_reason const& cr);
/** Send a WebSocket close frame.
This function is used to synchronously send a close frame on
the stream. The call blocks until one of the following is true:
@li The close frame finishes sending.
@li An error occurs on the stream.
This function is implemented in terms of one or more calls
to the next layer's `write_some` functions.
If the close reason specifies a close code other than
@ref beast::websocket::close_code::none, the close frame is
sent with the close code and optional reason string. Otherwise,
the close frame is sent with no payload.
Callers should not attempt to write WebSocket data after
initiating the close. Instead, callers should continue
reading until an error occurs. A read returning @ref error::closed
indicates a successful connection closure.
@param cr The reason for the close.
@param ec Set to indicate what error occurred, if any.
*/
void
close(close_reason const& cr, error_code& ec);
/** Start an asynchronous operation to send a WebSocket close frame.
This function is used to asynchronously send a close frame on
the stream. This function call always returns immediately. The
asynchronous operation will continue until one of the following
conditions is true:
@li The close frame finishes sending.
@li An error occurs on the stream.
This operation is implemented in terms of one or more calls to the
next layer's `async_write_some` functions, and is known as a
<em>composed operation</em>. The program must ensure that the
stream performs no other write operations (such as @ref async_ping,
@ref stream::async_write, @ref stream::async_write_frame, or
@ref stream::async_close) until this operation completes.
If the close reason specifies a close code other than
@ref beast::websocket::close_code::none, the close frame is
sent with the close code and optional reason string. Otherwise,
the close frame is sent with no payload.
Callers should not attempt to write WebSocket data after
initiating the close. Instead, callers should continue
reading until an error occurs. A read returning @ref error::closed
indicates a successful connection closure.
@param cr The reason for the close.
@param handler The handler to be called when the close operation
completes. Copies will be made of the handler as required. The
function signature of the handler must be:
@code
void handler(
error_code const& ec // Result of operation
);
@endcode
Regardless of whether the asynchronous operation completes
immediately or not, the handler will not be invoked from within
this function. Invocation of the handler will be performed in a
manner equivalent to using `boost::asio::io_service::post`.
*/
template<class CloseHandler>
#if BEAST_DOXYGEN
void_or_deduced
#else
async_return_type<
CloseHandler, void(error_code)>
#endif
async_close(close_reason const& cr, CloseHandler&& handler);
/** Send a WebSocket ping frame.
This function is used to synchronously send a ping frame on
the stream. The call blocks until one of the following is true:
@li The ping frame finishes sending.
@li An error occurs on the stream.
This function is implemented in terms of one or more calls to the
next layer's `write_some` functions.
@param payload The payload of the ping message, which may be empty.
@throws system_error Thrown on failure.
*/
void
ping(ping_data const& payload);
/** Send a WebSocket ping frame.
This function is used to synchronously send a ping frame on
the stream. The call blocks until one of the following is true:
@li The ping frame finishes sending.
@li An error occurs on the stream.
This function is implemented in terms of one or more calls to the
next layer's `write_some` functions.
@param payload The payload of the ping message, which may be empty.
@param ec Set to indicate what error occurred, if any.
*/
void
ping(ping_data const& payload, error_code& ec);
/** Start an asynchronous operation to send a WebSocket ping frame.
This function is used to asynchronously send a ping frame to
the stream. The function call always returns immediately. The
asynchronous operation will continue until one of the following
is true:
@li The entire ping frame is sent.
@li An error occurs on the stream.
This operation is implemented in terms of one or more calls to the
next layer's `async_write_some` functions, and is known as a
<em>composed operation</em>. The program must ensure that the
stream performs no other writes until this operation completes.
If a close frame is sent or received before the ping frame is
sent, the completion handler will be called with the error
set to `boost::asio::error::operation_aborted`.
@param payload The payload of the ping message, which may be empty.
@param handler The handler to be called when the read operation
completes. Copies will be made of the handler as required. The
function signature of the handler must be:
@code
void handler(
error_code const& ec // Result of operation
);
@endcode
Regardless of whether the asynchronous operation completes
immediately or not, the handler will not be invoked from within
this function. Invocation of the handler will be performed in a
manner equivalent to using `boost::asio::io_service::post`.
*/
template<class WriteHandler>
#if BEAST_DOXYGEN
void_or_deduced
#else
async_return_type<
WriteHandler, void(error_code)>
#endif
async_ping(ping_data const& payload, WriteHandler&& handler);
/** Send a WebSocket pong frame.
This function is used to synchronously send a pong frame on
the stream. The call blocks until one of the following is true:
@li The pong frame finishes sending.
@li An error occurs on the stream.
This function is implemented in terms of one or more calls to the
next layer's `write_some` functions.
The WebSocket protocol allows pong frames to be sent from either
end at any time. It is not necessary to first receive a ping in
order to send a pong. The remote peer may use the receipt of a
pong frame as an indication that the connection is not dead.
@param payload The payload of the pong message, which may be empty.
@throws system_error Thrown on failure.
*/
void
pong(ping_data const& payload);
/** Send a WebSocket pong frame.
This function is used to synchronously send a pong frame on
the stream. The call blocks until one of the following is true:
@li The pong frame finishes sending.
@li An error occurs on the stream.
This function is implemented in terms of one or more calls to the
next layer's `write_some` functions.
The WebSocket protocol allows pong frames to be sent from either
end at any time. It is not necessary to first receive a ping in
order to send a pong. The remote peer may use the receipt of a
pong frame as an indication that the connection is not dead.
@param payload The payload of the pong message, which may be empty.
@param ec Set to indicate what error occurred, if any.
*/
void
pong(ping_data const& payload, error_code& ec);
/** Start an asynchronous operation to send a WebSocket pong frame.
This function is used to asynchronously send a pong frame to
the stream. The function call always returns immediately. The
asynchronous operation will continue until one of the following
is true:
@li The entire pong frame is sent.
@li An error occurs on the stream.
This operation is implemented in terms of one or more calls to the
next layer's `async_write_some` functions, and is known as a
<em>composed operation</em>. The program must ensure that the
stream performs no other writes until this operation completes.
The WebSocket protocol allows pong frames to be sent from either
end at any time. It is not necessary to first receive a ping in
order to send a pong. The remote peer may use the receipt of a
pong frame as an indication that the connection is not dead.
If a close frame is sent or received before the pong frame is
sent, the completion handler will be called with the error
set to `boost::asio::error::operation_aborted`.
@param payload The payload of the pong message, which may be empty.
@param handler The handler to be called when the read operation
completes. Copies will be made of the handler as required. The
function signature of the handler must be:
@code
void handler(
error_code const& ec // Result of operation
);
@endcode
Regardless of whether the asynchronous operation completes
immediately or not, the handler will not be invoked from within
this function. Invocation of the handler will be performed in a
manner equivalent to using `boost::asio::io_service::post`.
*/
template<class WriteHandler>
#if BEAST_DOXYGEN
void_or_deduced
#else
async_return_type<
WriteHandler, void(error_code)>
#endif
async_pong(ping_data const& payload, WriteHandler&& handler);
/** Read a message from the stream.
This function is used to synchronously read a message from
the stream. The call blocks until one of the following is true:
@li A complete message is received.
@li An error occurs on the stream.
This call is implemented in terms of one or more calls to the
stream's `read_some` and `write_some` operations.
Upon a success, the input area of the stream buffer will
hold the received message payload bytes (which may be zero
in length). The functions @ref got_binary and @ref got_text
may be used to query the stream and determine the type
of the last received message.
During reads, the implementation handles control frames as
follows:
@li A pong frame is sent when a ping frame is received.
@li The @ref control_callback is invoked when a ping frame
or pong frame is received.
@li The WebSocket close procedure is started if a close frame
is received. In this case, the operation will eventually
complete with the error set to @ref error::closed.
@param buffer A dynamic buffer to hold the message data after
any masking or decompression has been applied.
@throws system_error Thrown on failure.
*/
template<class DynamicBuffer>
void
read(DynamicBuffer& buffer);
/** Read a message from the stream.
This function is used to synchronously read a message from
the stream. The call blocks until one of the following is true:
@li A complete message is received.
@li An error occurs on the stream.
This call is implemented in terms of one or more calls to the
stream's `read_some` and `write_some` operations.
Upon a success, the input area of the stream buffer will
hold the received message payload bytes (which may be zero
in length). The functions @ref got_binary and @ref got_text
may be used to query the stream and determine the type
of the last received message.
During reads, the implementation handles control frames as
follows:
@li The @ref control_callback is invoked when a ping frame
or pong frame is received.
@li A pong frame is sent when a ping frame is received.
@li The WebSocket close procedure is started if a close frame
is received. In this case, the operation will eventually
complete with the error set to @ref error::closed.
@param buffer A dynamic buffer to hold the message data after
any masking or decompression has been applied.
@param ec Set to indicate what error occurred, if any.
*/
template<class DynamicBuffer>
void
read(DynamicBuffer& buffer, error_code& ec);
/** Start an asynchronous operation to read a message from the stream.
This function is used to asynchronously read a message from
the stream. The function call always returns immediately. The
asynchronous operation will continue until one of the following
is true:
@li A complete message is received.
@li An error occurs on the stream.
This operation is implemented in terms of one or more calls to the
next layer's `async_read_some` and `async_write_some` functions,
and is known as a <em>composed operation</em>. The program must
ensure that the stream performs no other reads until this operation
completes.
Upon a success, the input area of the stream buffer will
hold the received message payload bytes (which may be zero
in length). The functions @ref got_binary and @ref got_text
may be used to query the stream and determine the type
of the last received message.
During reads, the implementation handles control frames as
follows:
@li The @ref control_callback is invoked when a ping frame
or pong frame is received.
@li A pong frame is sent when a ping frame is received.
@li The WebSocket close procedure is started if a close frame
is received. In this case, the operation will eventually
complete with the error set to @ref error::closed.
Because of the need to handle control frames, read operations
can cause writes to take place. These writes are managed
transparently; callers can still have one active asynchronous
read and asynchronous write operation pending simultaneously
(a user initiated call to @ref async_close counts as a write).
@param buffer A dynamic buffer to hold the message data after
any masking or decompression has been applied. This object must
remain valid until the handler is called.
@param handler The handler to be called when the read operation
completes. Copies will be made of the handler as required. The
function signature of the handler must be:
@code
void handler(
error_code const& ec // Result of operation
);
@endcode
Regardless of whether the asynchronous operation completes
immediately or not, the handler will not be invoked from within
this function. Invocation of the handler will be performed in a
manner equivalent to using `boost::asio::io_service::post`.
*/
template<class DynamicBuffer, class ReadHandler>
#if BEAST_DOXYGEN
void_or_deduced
#else
async_return_type<
ReadHandler, void(error_code)>
#endif
async_read(DynamicBuffer& buffer, ReadHandler&& handler);
/** Read a message frame from the stream.
This function is used to synchronously read a single message
frame from the stream. The call blocks until one of the following
is true:
@li A complete frame is received.
@li An error occurs on the stream.
This call is implemented in terms of one or more calls to the
stream's `read_some` and `write_some` operations.
During reads, the implementation handles control frames as
follows:
@li The @ref control_callback is invoked when a ping frame
or pong frame is received.
@li A pong frame is sent when a ping frame is received.
@li The WebSocket close procedure is started if a close frame
is received. In this case, the operation will eventually
complete with the error set to @ref error::closed.
@param buffer A dynamic buffer to hold the message data after
any masking or decompression has been applied.
@return `true` if this is the last frame of the message.
@throws system_error Thrown on failure.
*/
template<class DynamicBuffer>
bool
read_frame(DynamicBuffer& buffer);
/** Read a message frame from the stream.
This function is used to synchronously read a single message
frame from the stream. The call blocks until one of the following
is true:
@li A complete frame is received.
@li An error occurs on the stream.
This call is implemented in terms of one or more calls to the
stream's `read_some` and `write_some` operations.
During reads, the implementation handles control frames as
follows:
@li The @ref control_callback is invoked when a ping frame
or pong frame is received.
@li A pong frame is sent when a ping frame is received.
@li The WebSocket close procedure is started if a close frame
is received. In this case, the operation will eventually
complete with the error set to @ref error::closed.
@param buffer A dynamic buffer to hold the message data after
any masking or decompression has been applied.
@param ec Set to indicate what error occurred, if any.
@return `true` if this is the last frame of the message.
*/
template<class DynamicBuffer>
bool
read_frame(DynamicBuffer& buffer, error_code& ec);
/** Start an asynchronous operation to read a message frame from the stream.
This function is used to asynchronously read a single message
frame from the websocket. The function call always returns
immediately. The asynchronous operation will continue until
one of the following conditions is true:
@li A complete frame is received.
@li An error occurs on the stream.
This operation is implemented in terms of one or more calls to the
next layer's `async_read_some` and `async_write_some` functions,
and is known as a <em>composed operation</em>. The program must
ensure that the stream performs no other reads until this operation
completes.
During reads, the implementation handles control frames as
follows:
@li The @ref control_callback is invoked when a ping frame
or pong frame is received.
@li A pong frame is sent when a ping frame is received.
@li The WebSocket close procedure is started if a close frame
is received. In this case, the operation will eventually
complete with the error set to @ref error::closed.
Because of the need to handle control frames, read operations
can cause writes to take place. These writes are managed
transparently; callers can still have one active asynchronous
read and asynchronous write operation pending simultaneously
(a user initiated call to @ref async_close counts as a write).
@param buffer A dynamic buffer to hold the message data after
any masking or decompression has been applied. This object must
remain valid until the handler is called.
@param handler The handler to be called when the read operation
completes. Copies will be made of the handler as required. The
function signature of the handler must be:
@code
void handler(
error_code const& ec, // Result of operation
bool fin // `true` if this is the last frame
);
@endcode
Regardless of whether the asynchronous operation completes
immediately or not, the handler will not be invoked from within
this function. Invocation of the handler will be performed in a
manner equivalent to using boost::asio::io_service::post().
*/
template<class DynamicBuffer, class ReadHandler>
#if BEAST_DOXYGEN
void_or_deduced
#else
async_return_type<ReadHandler, void(error_code, bool)>
#endif
async_read_frame(DynamicBuffer& buffer, ReadHandler&& handler);
/** Write a message to the stream.
This function is used to synchronously write a message to
the stream. The call blocks until one of the following conditions
is met:
@li The entire message is sent.
@li An error occurs.
This operation is implemented in terms of one or more calls to the
next layer's `write_some` function.
The current setting of the @ref binary option controls
whether the message opcode is set to text or binary. If the
@ref auto_fragment option is set, the message will be split
into one or more frames as necessary. The actual payload contents
sent may be transformed as per the WebSocket protocol settings.
@param buffers The buffers containing the entire message
payload. The implementation will make copies of this object
as needed, but ownership of the underlying memory is not
transferred. The caller is responsible for ensuring that
the memory locations pointed to by buffers remains valid
until the completion handler is called.
@throws system_error Thrown on failure.
@note This function always sends an entire message. To
send a message in fragments, use @ref write_frame.
*/
template<class ConstBufferSequence>
void
write(ConstBufferSequence const& buffers);
/** Write a message to the stream.
This function is used to synchronously write a message to
the stream. The call blocks until one of the following conditions
is met:
@li The entire message is sent.
@li An error occurs.
This operation is implemented in terms of one or more calls to the
next layer's `write_some` function.
The current setting of the @ref binary option controls
whether the message opcode is set to text or binary. If the
@ref auto_fragment option is set, the message will be split
into one or more frames as necessary. The actual payload contents
sent may be transformed as per the WebSocket protocol settings.
@param buffers The buffers containing the entire message
payload. The implementation will make copies of this object
as needed, but ownership of the underlying memory is not
transferred. The caller is responsible for ensuring that
the memory locations pointed to by buffers remains valid
until the completion handler is called.
@param ec Set to indicate what error occurred, if any.
@throws system_error Thrown on failure.
@note This function always sends an entire message. To
send a message in fragments, use @ref write_frame.
*/
template<class ConstBufferSequence>
void
write(ConstBufferSequence const& buffers, error_code& ec);
/** Start an asynchronous operation to write a message to the stream.
This function is used to asynchronously write a message to
the stream. The function call always returns immediately.
The asynchronous operation will continue until one of the
following conditions is true:
@li The entire message is sent.
@li An error occurs.
This operation is implemented in terms of one or more calls
to the next layer's `async_write_some` functions, and is known
as a <em>composed operation</em>. The program must ensure that
the stream performs no other write operations (such as
stream::async_write, stream::async_write_frame, or
stream::async_close).
The current setting of the @ref binary option controls
whether the message opcode is set to text or binary. If the
@ref auto_fragment option is set, the message will be split
into one or more frames as necessary. The actual payload contents
sent may be transformed as per the WebSocket protocol settings.
@param buffers The buffers containing the entire message
payload. The implementation will make copies of this object
as needed, but ownership of the underlying memory is not
transferred. The caller is responsible for ensuring that
the memory locations pointed to by buffers remains valid
until the completion handler is called.
@param handler The handler to be called when the write operation
completes. Copies will be made of the handler as required. The
function signature of the handler must be:
@code
void handler(
error_code const& ec // Result of operation
);
@endcode
Regardless of whether the asynchronous operation completes
immediately or not, the handler will not be invoked from within
this function. Invocation of the handler will be performed in a
manner equivalent to using `boost::asio::io_service::post`.
*/
template<class ConstBufferSequence, class WriteHandler>
#if BEAST_DOXYGEN
void_or_deduced
#else
async_return_type<
WriteHandler, void(error_code)>
#endif
async_write(ConstBufferSequence const& buffers,
WriteHandler&& handler);
/** Write partial message data on the stream.
This function is used to write some or all of a message's
payload to the stream. The call will block until one of the
following conditions is true:
@li A frame is sent.
@li Message data is transferred to the write buffer.
@li An error occurs.
This operation is implemented in terms of one or more calls
to the stream's `write_some` function.
If this is the beginning of a new message, the message opcode
will be set to text or binary as per the current setting of
the @ref binary option. The actual payload sent may be
transformed as per the WebSocket protocol settings.
@param fin `true` if this is the last frame in the message.
@param buffers The input buffer sequence holding the data to write.
@return The number of bytes consumed in the input buffers.
@throws system_error Thrown on failure.
*/
template<class ConstBufferSequence>
void
write_frame(bool fin, ConstBufferSequence const& buffers);
/** Write partial message data on the stream.
This function is used to write some or all of a message's
payload to the stream. The call will block until one of the
following conditions is true:
@li A frame is sent.
@li Message data is transferred to the write buffer.
@li An error occurs.
This operation is implemented in terms of one or more calls
to the stream's `write_some` function.
If this is the beginning of a new message, the message opcode
will be set to text or binary as per the current setting of
the @ref binary option. The actual payload sent may be
transformed as per the WebSocket protocol settings.
@param fin `true` if this is the last frame in the message.
@param buffers The input buffer sequence holding the data to write.
@param ec Set to indicate what error occurred, if any.
@return The number of bytes consumed in the input buffers.
*/
template<class ConstBufferSequence>
void
write_frame(bool fin,
ConstBufferSequence const& buffers, error_code& ec);
/** Start an asynchronous operation to send a message frame on the stream.
This function is used to asynchronously write a message frame
on the stream. This function call always returns immediately.
The asynchronous operation will continue until one of the following
conditions is true:
@li The entire frame is sent.
@li An error occurs.
This operation is implemented in terms of one or more calls
to the next layer's `async_write_some` functions, and is known
as a <em>composed operation</em>. The actual payload sent
may be transformed as per the WebSocket protocol settings. The
program must ensure that the stream performs no other write
operations (such as stream::async_write, stream::async_write_frame,
or stream::async_close).
If this is the beginning of a new message, the message opcode
will be set to text or binary as per the current setting of
the @ref binary option. The actual payload sent may be
transformed as per the WebSocket protocol settings.
@param fin A bool indicating whether or not the frame is the
last frame in the corresponding WebSockets message.
@param buffers A object meeting the requirements of
ConstBufferSequence which holds the payload data before any
masking or compression. Although the buffers object may be copied
as necessary, ownership of the underlying buffers is retained by
the caller, which must guarantee that they remain valid until
the handler is called.
@param handler The handler to be called when the write completes.
Copies will be made of the handler as required. The equivalent
function signature of the handler must be:
@code void handler(
error_code const& ec // Result of operation
); @endcode
*/
template<class ConstBufferSequence, class WriteHandler>
#if BEAST_DOXYGEN
void_or_deduced
#else
async_return_type<
WriteHandler, void(error_code)>
#endif
async_write_frame(bool fin,
ConstBufferSequence const& buffers, WriteHandler&& handler);
private:
template<class Decorator, class Handler> class accept_op;
template<class Handler> class close_op;
template<class Handler> class handshake_op;
template<class Handler> class ping_op;
template<class Handler> class response_op;
template<class Buffers, class Handler> class write_op;
template<class Buffers, class Handler> class write_frame_op;
template<class DynamicBuffer, class Handler> class read_op;
template<class DynamicBuffer, class Handler> class read_frame_op;
static
void
default_decorate_req(request_type&)
{
}
static
void
default_decorate_res(response_type&)
{
}
void
reset();
template<class Decorator>
void
do_accept(Decorator const& decorator,
error_code& ec);
template<class Allocator, class Decorator>
void
do_accept(http::header<true,
http::basic_fields<Allocator>> const& req,
Decorator const& decorator, error_code& ec);
template<class RequestDecorator>
void
do_handshake(response_type* res_p,
string_view host,
string_view target,
RequestDecorator const& decorator,
error_code& ec);
template<class Decorator>
request_type
build_request(detail::sec_ws_key_type& key,
string_view host,
string_view target,
Decorator const& decorator);
template<class Allocator, class Decorator>
response_type
build_response(http::header<true,
http::basic_fields<Allocator>> const& req,
Decorator const& decorator);
void
do_response(http::header<false> const& resp,
detail::sec_ws_key_type const& key, error_code& ec);
};
} // websocket
} // beast
#include <beast/websocket/impl/accept.ipp>
#include <beast/websocket/impl/close.ipp>
#include <beast/websocket/impl/handshake.ipp>
#include <beast/websocket/impl/ping.ipp>
#include <beast/websocket/impl/read.ipp>
#include <beast/websocket/impl/stream.ipp>
#include <beast/websocket/impl/write.ipp>
#endif