mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-19 18:45:52 +00:00
Beast.WebSocket provides developers with a robust WebSocket implementation built on Boost.Asio with a consistent asynchronous model using a modern C++ approach.
306 lines
7.9 KiB
C++
306 lines
7.9 KiB
C++
//------------------------------------------------------------------------------
|
|
/*
|
|
This file is part of Beast: https://github.com/vinniefalco/Beast
|
|
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
*/
|
|
//==============================================================================
|
|
|
|
#ifndef BEAST_WSPROTO_OPTION_H_INCLUDED
|
|
#define BEAST_WSPROTO_OPTION_H_INCLUDED
|
|
|
|
#include <beast/wsproto/detail/socket_base.h>
|
|
#include <algorithm>
|
|
#include <cstdint>
|
|
#include <stdexcept>
|
|
#include <type_traits>
|
|
|
|
namespace beast {
|
|
namespace wsproto {
|
|
|
|
/** Automatic fragmentation size option.
|
|
|
|
Sets the maximum size of fragments generated when sending
|
|
messages on a WebSocket socket.
|
|
|
|
When the automatic fragmentation size is non-zero, messages
|
|
exceeding the size will be split into multiple frames no
|
|
larger than the size. This setting does not affect frames
|
|
send explicitly using `write_frame` or `async_write_frame`.
|
|
|
|
The default setting is to fragment messages into 16KB frames.
|
|
|
|
@note Objects of this type are passed to socket::set_option.
|
|
|
|
@par Example
|
|
Setting the automatic fragmentation size option:
|
|
@code
|
|
...
|
|
wsproto::socket<ip::tcp::socket> stream(ios);
|
|
stream.set_option(auto_fragment_size{8192});
|
|
@endcode
|
|
*/
|
|
#if GENERATING_DOCS
|
|
using auto_fragment_size = implementation_defined;
|
|
#else
|
|
struct auto_fragment_size
|
|
{
|
|
std::size_t value;
|
|
|
|
auto_fragment_size(std::size_t n)
|
|
: value(n)
|
|
{
|
|
}
|
|
};
|
|
#endif
|
|
|
|
/** HTTP decorator option.
|
|
|
|
The decorator transforms the HTTP requests and responses used
|
|
when requesting or responding to the WebSocket Upgrade. This may
|
|
be used to set or change header fields. For example to set the
|
|
Server or User-Agent fields. The default setting applies no
|
|
transformation to the HTTP message.
|
|
|
|
For synchronous operations, the implementation will call the
|
|
decorator before the function call to perform the operation
|
|
returns.
|
|
|
|
For asynchronous operations, the implementation guarantees that
|
|
calls to the decorator will be made from the same implicit or
|
|
explicit strand used to call the asynchronous initiation
|
|
function.
|
|
|
|
The default setting is no decorator.
|
|
|
|
@note Objects of this type are passed to socket::set_option.
|
|
|
|
@par Example
|
|
Setting the decorator.
|
|
@code
|
|
struct identity
|
|
{
|
|
template<bool isRequest, class Body, class Headers>
|
|
void
|
|
operator()(http::message<isRequest, Body, Headers>& m)
|
|
{
|
|
if(isRequest)
|
|
m.headers.replace("User-Agent", "MyClient");
|
|
else
|
|
m.headers.replace("Server", "MyServer");
|
|
}
|
|
};
|
|
...
|
|
websocket::stream<ip::tcp::socket> ws(ios);
|
|
ws.set_option(decorate(identity{}));
|
|
@endcode
|
|
*/
|
|
#if GENERATING_DOCS
|
|
using decorate = implementation_defined;
|
|
#else
|
|
template<class Decorator>
|
|
inline
|
|
auto
|
|
decorate(Decorator&& d)
|
|
{
|
|
return std::make_unique<detail::decorator<
|
|
std::decay_t<Decorator>>>(
|
|
std::forward<Decorator>(d));
|
|
}
|
|
#endif
|
|
|
|
/** Keep-alive option.
|
|
|
|
Determines if the connection is closed after a failed upgrade
|
|
request.
|
|
|
|
This setting only affects the behavior of HTTP requests that
|
|
implicitly or explicitly ask for a keepalive. For HTTP requests
|
|
that indicate the connection should be closed, the connection is
|
|
closed as per rfc2616.
|
|
|
|
The default setting is to close connections after a failed
|
|
upgrade request.
|
|
|
|
@note Objects of this type are passed to socket::set_option.
|
|
|
|
@par Example
|
|
Setting the keep alive option.
|
|
@code
|
|
...
|
|
websocket::stream<ip::tcp::socket> ws(ios);
|
|
ws.set_option(keep_alive{8192});
|
|
@endcode
|
|
*/
|
|
#if GENERATING_DOCS
|
|
using keep_alive = implementation_defined;
|
|
#else
|
|
struct keep_alive
|
|
{
|
|
bool value;
|
|
|
|
keep_alive(bool v)
|
|
: value(v)
|
|
{
|
|
}
|
|
};
|
|
#endif
|
|
|
|
/** Message type option.
|
|
|
|
This controls the opcode set for outgoing messages. Valid
|
|
choices are opcode::binary or opcode::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 opcode::text.
|
|
|
|
@note Objects of this type are passed to socket::set_option.
|
|
|
|
@par Example
|
|
Setting the message type to binary.
|
|
@code
|
|
...
|
|
websocket::stream<ip::tcp::socket> ws(ios);
|
|
ws.set_option(message_type{opcode::binary});
|
|
@endcode
|
|
*/
|
|
#if GENERATING_DOCS
|
|
using message_type = implementation_defined;
|
|
#else
|
|
struct message_type
|
|
{
|
|
opcode value;
|
|
|
|
explicit
|
|
message_type(opcode op)
|
|
{
|
|
if(op != opcode::binary && op != opcode::text)
|
|
throw std::domain_error("bad opcode");
|
|
value = op;
|
|
}
|
|
};
|
|
#endif
|
|
|
|
/** Read buffer size option.
|
|
|
|
Sets the number of bytes allocated to the socket's read buffer.
|
|
If this is zero, then reads are not buffered. Setting this
|
|
higher can improve performance when expecting to receive
|
|
many small frames.
|
|
|
|
The default is no buffering.
|
|
|
|
@note Objects of this type are passed to socket::set_option.
|
|
|
|
@par Example
|
|
Setting the read buffer size.
|
|
@code
|
|
...
|
|
websocket::stream<ip::tcp::socket> ws(ios);
|
|
ws.set_option(read_buffer_size{16 * 1024});
|
|
@endcode
|
|
*/
|
|
#if GENERATING_DOCS
|
|
using read_buffer_size = implementation_defined;
|
|
#else
|
|
struct read_buffer_size
|
|
{
|
|
std::size_t value;
|
|
|
|
explicit
|
|
read_buffer_size(std::size_t n)
|
|
: value(n)
|
|
{
|
|
}
|
|
};
|
|
#endif
|
|
|
|
/** Maximum incoming message size option.
|
|
|
|
Sets the largest permissible incoming message size. Message
|
|
frame headers indicating a size that would bring the total
|
|
message size over this limit will cause a protocol failure.
|
|
|
|
The default setting is 16 megabytes.
|
|
|
|
@note Objects of this type are passed to socket::set_option.
|
|
|
|
@par Example
|
|
Setting the maximum read message size.
|
|
@code
|
|
...
|
|
websocket::stream<ip::tcp::socket> ws(ios);
|
|
ws.set_option(read_message_max{65536});
|
|
@endcode
|
|
*/
|
|
#if GENERATING_DOCS
|
|
using read_message_max = implementation_defined;
|
|
#else
|
|
struct read_message_max
|
|
{
|
|
std::size_t value;
|
|
|
|
explicit
|
|
read_message_max(std::size_t n)
|
|
: value(n)
|
|
{
|
|
}
|
|
};
|
|
#endif
|
|
|
|
/** Write buffer size option.
|
|
|
|
Sets the number of bytes allocated to the socket's write buffer.
|
|
This buffer is used to hold masked frame payload data. Lowering
|
|
the size of the buffer can decrease the memory requirements for
|
|
each connection, at the cost of an increased number of calls to
|
|
perform socket writes.
|
|
|
|
This setting does not affect connections operating in the server
|
|
role, since servers do not apply a masking key to frame payloads.
|
|
|
|
The default setting is 4096. The minimum value is 1024.
|
|
|
|
@note Objects of this type are passed to socket::set_option.
|
|
|
|
@par Example
|
|
Setting the write buffer size.
|
|
@code
|
|
...
|
|
websocket::stream<ip::tcp::socket> ws(ios);
|
|
ws.set_option(write_buffer_size{8192});
|
|
@endcode
|
|
*/
|
|
#if GENERATING_DOCS
|
|
using write_buffer_size = implementation_defined;
|
|
#else
|
|
struct write_buffer_size
|
|
{
|
|
std::size_t value;
|
|
|
|
explicit
|
|
write_buffer_size(std::size_t n)
|
|
: value(n)
|
|
{
|
|
}
|
|
};
|
|
#endif
|
|
|
|
} // wsproto
|
|
} // beast
|
|
|
|
#endif
|