NeoGraph 0.16.0
A C++20 Graph Agent Engine Library — LangGraph for C++
Loading...
Searching...
No Matches
ws_client.h
Go to the documentation of this file.
1
34#pragma once
35
36#include <neograph/api.h>
37
38#include <asio/any_io_executor.hpp>
39#include <asio/awaitable.hpp>
40
41#include <chrono>
42#include <cstddef>
43#include <cstdint>
44#include <memory>
45#include <optional>
46#include <string>
47#include <string_view>
48#include <utility>
49#include <vector>
50
51namespace neograph::async {
52
53enum class WsOpcode : std::uint8_t {
54 Continuation = 0x0,
55 Text = 0x1,
56 Binary = 0x2,
57 Close = 0x8,
58 Ping = 0x9,
59 Pong = 0xA,
60};
61
67 std::size_t max_handshake_bytes = 64u * 1024u;
68
70 std::size_t max_frame_payload_bytes = 16u * 1024u * 1024u;
71
73 std::size_t max_message_payload_bytes = 16u * 1024u * 1024u;
74
79 std::chrono::milliseconds handshake_timeout{30000};
80
86 std::chrono::milliseconds read_timeout{0};
87};
88
89struct WsMessage {
96 WsOpcode op;
97 std::string payload;
98};
99
104class NEOGRAPH_API WsClient {
105public:
106 ~WsClient();
107 WsClient(const WsClient&) = delete;
108 WsClient& operator=(const WsClient&) = delete;
109 WsClient(WsClient&&) noexcept;
110 WsClient& operator=(WsClient&&) noexcept;
111
116 asio::awaitable<void> send_text(std::string_view payload);
117
120 asio::awaitable<void> send_binary(std::string_view payload);
121
127 asio::awaitable<void> send_close(std::uint16_t code = 1000, std::string_view reason = "");
128
138 asio::awaitable<WsMessage> recv();
139
140private:
141 // Friend forward-declaration; the trailing standalone declaration
142 // (further down) also carries NEOGRAPH_API. MSVC C2375 fires if
143 // the two declarations disagree on linkage, so the macro must
144 // appear here too even though it's redundant on POSIX.
145 friend NEOGRAPH_API asio::awaitable<std::unique_ptr<WsClient>> ws_connect(
146 asio::any_io_executor,
147 std::string_view,
148 std::string_view,
149 std::string_view,
150 std::vector<std::pair<std::string, std::string>>,
151 bool);
152 friend NEOGRAPH_API asio::awaitable<std::unique_ptr<WsClient>> ws_connect(
153 asio::any_io_executor,
154 std::string_view,
155 std::string_view,
156 std::string_view,
157 std::vector<std::pair<std::string, std::string>>,
158 bool,
160
161 struct Impl;
162 std::unique_ptr<Impl> impl_;
163
164 explicit WsClient(std::unique_ptr<Impl>);
165
166 asio::awaitable<WsMessage> recv_message();
167 asio::awaitable<void> send_frame_owned(WsOpcode opcode, std::string payload);
168 asio::awaitable<void> send_close_owned(std::uint16_t code, std::string reason);
169 static asio::awaitable<std::unique_ptr<WsClient>> connect_owned(
170 asio::any_io_executor ex,
171 std::string host,
172 std::string port,
173 std::string path,
174 std::vector<std::pair<std::string, std::string>> headers,
175 bool tls,
176 WsClientOptions options);
177 static asio::awaitable<std::unique_ptr<WsClient>> handshake_owned(
178 asio::any_io_executor ex,
179 std::string host,
180 std::string port,
181 std::string path,
182 std::vector<std::pair<std::string, std::string>> headers,
183 bool tls,
184 WsClientOptions options);
185};
186
205NEOGRAPH_API asio::awaitable<std::unique_ptr<WsClient>> ws_connect(
206 asio::any_io_executor ex,
207 std::string_view host,
208 std::string_view port,
209 std::string_view path,
210 std::vector<std::pair<std::string, std::string>> headers = {},
211 bool tls = true);
212
215NEOGRAPH_API asio::awaitable<std::unique_ptr<WsClient>> ws_connect(
216 asio::any_io_executor ex,
217 std::string_view host,
218 std::string_view port,
219 std::string_view path,
220 std::vector<std::pair<std::string, std::string>> headers,
221 bool tls,
222 WsClientOptions options);
223
224namespace detail {
225
227struct WsFrameHeader {
228 WsOpcode opcode;
229 bool fin;
230 bool masked;
231 std::uint64_t payload_len;
232 std::uint8_t mask_key[4];
234 std::size_t header_size;
235};
236
242NEOGRAPH_API std::optional<WsFrameHeader> parse_frame_header(std::string_view buf);
243
246NEOGRAPH_API void encode_frame_header(std::string& out,
247 WsOpcode opcode,
248 bool fin,
249 bool masked,
250 std::uint64_t payload_len,
251 const std::uint8_t mask_key[4] = nullptr);
252
254NEOGRAPH_API void apply_mask(char* data, std::size_t len, const std::uint8_t mask_key[4]) noexcept;
255
258NEOGRAPH_API std::string generate_sec_websocket_key();
259
262NEOGRAPH_API std::string compute_sec_websocket_accept(std::string_view client_key);
263
264} // namespace detail
265
266} // namespace neograph::async
NEOGRAPH_API export/import macro for shared-library builds.
Async WebSocket client.
Definition ws_client.h:104
asio::awaitable< void > send_close(std::uint16_t code=1000, std::string_view reason="")
Send a close frame with the given status code and optional UTF-8 reason.
asio::awaitable< void > send_binary(std::string_view payload)
Send a binary frame (FIN=1).
asio::awaitable< WsMessage > recv()
Block until the next application message arrives.
asio::awaitable< void > send_text(std::string_view payload)
Send a text frame (FIN=1, no fragmentation).
friend NEOGRAPH_API asio::awaitable< std::unique_ptr< WsClient > > ws_connect(asio::any_io_executor, std::string_view, std::string_view, std::string_view, std::vector< std::pair< std::string, std::string > >, bool)
Establish a WebSocket connection.
friend NEOGRAPH_API asio::awaitable< std::unique_ptr< WsClient > > ws_connect(asio::any_io_executor, std::string_view, std::string_view, std::string_view, std::vector< std::pair< std::string, std::string > >, bool, WsClientOptions)
Establish a WebSocket connection with explicit resource ceilings.
Resource ceilings and deadlines for a WebSocket connection.
Definition ws_client.h:64
std::chrono::milliseconds read_timeout
Longest one recv() call waits for the next application message (pings answered meanwhile do not exten...
Definition ws_client.h:86
std::size_t max_handshake_bytes
Maximum HTTP Upgrade response header size, including the final CRLF CRLF.
Definition ws_client.h:67
std::size_t max_message_payload_bytes
Maximum assembled Text/Binary message size across fragments.
Definition ws_client.h:73
std::size_t max_frame_payload_bytes
Maximum payload size of any individual data or control frame.
Definition ws_client.h:70
std::chrono::milliseconds handshake_timeout
Deadline for the whole opening handshake of ws_connect: name resolution, TCP connect,...
Definition ws_client.h:79
NEOGRAPH_API asio::awaitable< std::unique_ptr< WsClient > > ws_connect(asio::any_io_executor ex, std::string_view host, std::string_view port, std::string_view path, std::vector< std::pair< std::string, std::string > > headers={}, bool tls=true)
Establish a WebSocket connection.