Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
named_pipe.hpp
Go to the documentation of this file.
1#pragma once
2
3// Named pipe for IPC — POSIX mkfifo / Windows CreateNamedPipe.
4
5#include <atomic>
6#include <mutex>
7#include <string>
8#include <string_view>
9#include <optional>
10#include <vector>
11#include <cstdint>
12
13namespace pulp::runtime {
14
15class NamedPipe {
16public:
17 NamedPipe() = default;
19
25 bool create_server(std::string_view name);
26
28 bool connect_client(std::string_view name);
29
31 int write(const uint8_t* data, size_t length);
32 int write(std::string_view data);
33
35 int read(uint8_t* buffer, size_t buffer_size);
36
38 std::optional<std::string> read_string(size_t max_size = 65536);
39
41 void close();
42
44 bool is_open() const;
45
46 // No copy, move OK
47 NamedPipe(const NamedPipe&) = delete;
48 NamedPipe& operator=(const NamedPipe&) = delete;
51
52private:
53 std::string name_;
54 std::atomic<bool> is_server_{false};
55#ifdef _WIN32
56 void* handle_ = nullptr;
57 std::atomic<bool> closing_{false};
58 std::atomic<bool> connecting_{false};
59#else
60 std::string write_name_;
61 std::atomic<int> read_fd_{-1};
62 std::atomic<int> write_fd_{-1};
63 mutable std::mutex read_fd_mutex_;
64 mutable std::mutex write_fd_mutex_;
65 std::atomic<bool> closing_{false};
66 std::atomic<bool> read_peer_confirmed_{false};
67#endif
68};
69
70} // namespace pulp::runtime
NamedPipe(const NamedPipe &)=delete
int write(std::string_view data)
NamedPipe(NamedPipe &&other) noexcept
int read(uint8_t *buffer, size_t buffer_size)
Read data from the pipe. Returns bytes read, or -1 on error. 0 = EOF.
NamedPipe & operator=(const NamedPipe &)=delete
int write(const uint8_t *data, size_t length)
Write data to the pipe. Returns bytes written, or -1 on error.
bool create_server(std::string_view name)
Create and open a pipe as server (creates the pipe).
bool connect_client(std::string_view name)
Connect to an existing pipe as client.
void close()
Close the pipe.
std::optional< std::string > read_string(size_t max_size=65536)
Read all available data as a string.
NamedPipe & operator=(NamedPipe &&other) noexcept
bool is_open() const
Whether the pipe is open.
Definition generated_event_source.hpp:33
@ other
Definition udev_monitor.hpp:26