9#include <userver/engine/io/socket.hpp>
10#include <userver/server/http/http_request.hpp>
11#include <userver/tracing/span.hpp>
12#include <userver/utils/span.hpp>
13#include <userver/yaml_config/fwd.hpp>
15USERVER_NAMESPACE_BEGIN
17namespace server::websocket {
19using CloseStatusInt = int16_t;
27 kProtocolError = 1002,
28 kUnsupportedData = 1003,
29 kFrameTooLarge = 1004,
31 kAbnormalClosure = 1006,
32 kBadMessageData = 1007,
33 kPolicyViolation = 1008,
35 kExtensionMismatch = 1010,
46class WebSocketConnectionImpl;
49 unsigned max_remote_payload = 65536;
50 unsigned fragment_size = 65536;
55struct Statistics
final {
56 std::atomic<int64_t> msg_sent{0};
57 std::atomic<int64_t> msg_recv{0};
58 std::atomic<int64_t> bytes_sent{0};
59 std::atomic<int64_t> bytes_recv{0};
65 WebSocketConnection();
73 virtual ~WebSocketConnection();
98 virtual void SendText(std::string_view message) = 0;
100 template <
typename ContiguousContainer>
101 void SendBinary(
const ContiguousContainer& message) {
103 sizeof(
typename ContiguousContainer::value_type) == 1,
"SendBinary() should send either std::bytes or chars"
105 DoSendBinary(utils::span(
106 reinterpret_cast<
const std::byte*>(message.data()),
107 reinterpret_cast<
const std::byte*>(message.data() + message.size())
113 virtual const engine::
io::Sockaddr& RemoteAddr()
const = 0;
115 virtual void AddFinalTags(
tracing::Span& span)
const = 0;
116 virtual void AddStatistics(Statistics& stats)
const = 0;
119 virtual void DoSendBinary(
utils::span<
const std::byte> message) = 0;
123MakeWebSocket(std::unique_ptr<engine::io::RwBase>&& socket, engine::
io::Sockaddr&& peer_name,
const Config& config);