new cyclic writer

This commit is contained in:
awe
2026-04-09 18:27:59 +03:00
parent 9b521641c9
commit af462ab46a
3 changed files with 402 additions and 219 deletions

View File

@ -1,34 +1,41 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
class TtyProtocolWriter {
public:
explicit TtyProtocolWriter(std::string path);
struct StatsSnapshot {
std::uint64_t frames_written = 0;
std::uint64_t frames_dropped = 0;
std::uint64_t ring_overflows = 0;
};
TtyProtocolWriter(std::string path, std::size_t ring_capacity_bytes);
~TtyProtocolWriter();
TtyProtocolWriter(const TtyProtocolWriter&) = delete;
TtyProtocolWriter& operator=(const TtyProtocolWriter&) = delete;
TtyProtocolWriter(TtyProtocolWriter&& other) noexcept;
TtyProtocolWriter& operator=(TtyProtocolWriter&& other) noexcept;
TtyProtocolWriter(TtyProtocolWriter&& other) noexcept = delete;
TtyProtocolWriter& operator=(TtyProtocolWriter&& other) noexcept = delete;
void emit_packet_start() const;
void emit_step(uint16_t index, int16_t ch1_avg, int16_t ch2_avg) const;
void emit_packet_start();
void emit_step(uint16_t index, int16_t ch1_avg, int16_t ch2_avg);
void enqueue_encoded_frames(const uint16_t* words, std::size_t frame_count);
StatsSnapshot stats() const;
const std::string& path() const;
void throw_if_failed() const;
void shutdown();
private:
void write_frame(uint16_t word0, uint16_t word1, uint16_t word2, uint16_t word3) const;
void close_fd() noexcept;
void close_slave_fd() noexcept;
void remove_owned_link() noexcept;
void enqueue_frame(uint16_t word0, uint16_t word1, uint16_t word2, uint16_t word3);
void worker_loop();
std::string path_;
#ifndef _WIN32
int fd_ = -1;
int slave_fd_ = -1;
std::string slave_path_;
bool owns_link_ = false;
#endif
struct Impl;
std::unique_ptr<Impl> impl_;
};