Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
network_stream.hpp
Go to the documentation of this file.
1#pragma once
2
3// Network Stream backends: TCP sockets and HTTP response bodies.
4//
5// These wrap the existing `Socket` and `http_get`/`http_post` helpers so
6// that higher-level code can treat them uniformly with `FileStream`,
7// `MemoryStream`, and `PipeStream` — and, layered under `AsyncStream`,
8// gain non-blocking semantics without any network-specific plumbing.
9
12#include <pulp/runtime/http.hpp>
13
14#include <atomic>
15#include <cstddef>
16#include <cstdint>
17#include <map>
18#include <memory>
19#include <string>
20#include <string_view>
21#include <vector>
22
23namespace pulp::runtime {
24
30class TcpStream : public Stream {
31public:
32 TcpStream() = default;
33 explicit TcpStream(Socket&& connected);
34 ~TcpStream() override;
35
36 TcpStream(TcpStream&&) noexcept;
37 TcpStream& operator=(TcpStream&&) noexcept;
38
39 bool connect(std::string_view host, std::uint16_t port);
40
41 StreamResult read(std::uint8_t* buffer, std::size_t size) override;
42 StreamResult write(const std::uint8_t* buffer, std::size_t size) override;
44 void shutdown();
45 void close() override;
46 bool is_open() const override { return open_.load(); }
47
49 Socket& socket() { return socket_; }
50
51private:
52 Socket socket_;
53 std::atomic<bool> open_{false};
54};
55
64class HttpStream : public Stream {
65public:
66 struct Request {
67 std::string url;
68 std::string method = "GET";
69 std::string body;
70 std::string content_type = "application/json";
72 };
73
74 HttpStream() = default;
75 explicit HttpStream(const Request& request);
76
78 bool fetch(const Request& request);
79
81 static std::unique_ptr<HttpStream> get(std::string_view url,
82 int timeout_seconds = 30);
83
85 static std::unique_ptr<HttpStream> post(std::string_view url,
86 std::string_view body,
87 std::string_view content_type = "application/json",
88 int timeout_seconds = 30);
89
90 StreamResult read(std::uint8_t* buffer, std::size_t size) override;
91 StreamResult write(const std::uint8_t* buffer, std::size_t size) override;
92 void close() override;
93 bool is_open() const override;
94
96 int status_code() const { return response_.status_code; }
97
100 const std::map<std::string, std::string>& headers() const { return response_.headers; }
101
103 const std::string& transport_error() const { return response_.error; }
104
106 bool eof() const;
107
108private:
109 HttpResponse response_;
110 std::size_t read_pos_ = 0;
111 bool closed_ = false;
112};
113
114} // namespace pulp::runtime
StreamResult write(const std::uint8_t *buffer, std::size_t size) override
Write up to size bytes from buffer.
const std::map< std::string, std::string > & headers() const
Parsed response headers (lowercased keys are not normalized — they come through verbatim from cpp-htt...
Definition network_stream.hpp:100
void close() override
Close the stream. Idempotent.
static std::unique_ptr< HttpStream > get(std::string_view url, int timeout_seconds=30)
Convenience factory for GET requests.
int status_code() const
HTTP status code of the response, or 0 if the request failed.
Definition network_stream.hpp:96
static std::unique_ptr< HttpStream > post(std::string_view url, std::string_view body, std::string_view content_type="application/json", int timeout_seconds=30)
Convenience factory for POST requests.
bool eof() const
True when the HTTP response body was fully consumed by read().
bool is_open() const override
True while the stream is usable for read/write.
const std::string & transport_error() const
Non-empty when the request failed locally (network error, DNS, etc.).
Definition network_stream.hpp:103
HttpStream(const Request &request)
bool fetch(const Request &request)
Issue a new request, replacing any prior state.
StreamResult read(std::uint8_t *buffer, std::size_t size) override
Read up to size bytes into buffer.
Definition socket.hpp:30
Stream(const Stream &)=delete
StreamResult read(std::uint8_t *buffer, std::size_t size) override
Read up to size bytes into buffer.
StreamResult write(const std::uint8_t *buffer, std::size_t size) override
Write up to size bytes from buffer.
TcpStream(TcpStream &&) noexcept
void close() override
Close the stream. Idempotent.
TcpStream(Socket &&connected)
Socket & socket()
Access the underlying socket (advanced use: set options, inspect fd).
Definition network_stream.hpp:49
void shutdown()
Mark the stream closed and interrupt blocking I/O without releasing the socket handle.
bool is_open() const override
True while the stream is usable for read/write.
Definition network_stream.hpp:46
bool connect(std::string_view host, std::uint16_t port)
Bounded event contracts shared by host ingress and graph-authored nodes.
Definition anticipation_eligibility.hpp:9
Definition generated_event_source.hpp:33
STL namespace.
Definition http.hpp:29
Definition network_stream.hpp:66
std::string url
Definition network_stream.hpp:67
std::string body
Only used for POST.
Definition network_stream.hpp:69
int timeout_seconds
Definition network_stream.hpp:71
std::string method
"GET" or "POST".
Definition network_stream.hpp:68
std::string content_type
Definition network_stream.hpp:70
Result of a Stream read/write.
Definition stream.hpp:45