NeoGraph 0.10.0
A C++17 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 <cstdint>
42#include <memory>
43#include <optional>
44#include <string>
45#include <string_view>
46#include <utility>
47#include <vector>
48
49namespace neograph::async {
50
51enum class WsOpcode : std::uint8_t {
52 Continuation = 0x0,
53 Text = 0x1,
54 Binary = 0x2,
55 Close = 0x8,
56 Ping = 0x9,
57 Pong = 0xA,
58};
59
60struct WsMessage {
67 WsOpcode op;
68 std::string payload;
69};
70
75class NEOGRAPH_API WsClient {
76 public:
77 ~WsClient();
78 WsClient(const WsClient&) = delete;
79 WsClient& operator=(const WsClient&) = delete;
80 WsClient(WsClient&&) noexcept;
81 WsClient& operator=(WsClient&&) noexcept;
82
86 asio::awaitable<void> send_text(std::string_view payload);
87
89 asio::awaitable<void> send_binary(std::string_view payload);
90
96 asio::awaitable<void> send_close(
97 std::uint16_t code = 1000, std::string_view reason = "");
98
107 asio::awaitable<WsMessage> recv();
108
109 private:
110 // Friend forward-declaration; the trailing standalone declaration
111 // (further down) also carries NEOGRAPH_API. MSVC C2375 fires if
112 // the two declarations disagree on linkage, so the macro must
113 // appear here too even though it's redundant on POSIX.
114 friend NEOGRAPH_API asio::awaitable<std::unique_ptr<WsClient>>
116 asio::any_io_executor, std::string_view, std::string_view,
117 std::string_view, std::vector<std::pair<std::string, std::string>>,
118 bool);
119
120 struct Impl;
121 std::unique_ptr<Impl> impl_;
122
123 explicit WsClient(std::unique_ptr<Impl>);
124};
125
141NEOGRAPH_API asio::awaitable<std::unique_ptr<WsClient>> ws_connect(
142 asio::any_io_executor ex,
143 std::string_view host,
144 std::string_view port,
145 std::string_view path,
146 std::vector<std::pair<std::string, std::string>> headers = {},
147 bool tls = true);
148
149namespace detail {
150
152struct WsFrameHeader {
153 WsOpcode opcode;
154 bool fin;
155 bool masked;
156 std::uint64_t payload_len;
157 std::uint8_t mask_key[4];
159 std::size_t header_size;
160};
161
166NEOGRAPH_API std::optional<WsFrameHeader> parse_frame_header(std::string_view buf);
167
170NEOGRAPH_API void encode_frame_header(
171 std::string& out,
172 WsOpcode opcode,
173 bool fin,
174 bool masked,
175 std::uint64_t payload_len,
176 const std::uint8_t mask_key[4] = nullptr);
177
179NEOGRAPH_API void apply_mask(char* data, std::size_t len, const std::uint8_t mask_key[4]) noexcept;
180
183NEOGRAPH_API std::string generate_sec_websocket_key();
184
187NEOGRAPH_API std::string compute_sec_websocket_accept(std::string_view client_key);
188
189} // namespace detail
190
191} // namespace neograph::async
NEOGRAPH_API export/import macro for shared-library builds.
Async WebSocket client.
Definition ws_client.h:75
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). Masked per §5.3.
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.
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.