ollie/kde/gui/nativestreamer.h

62 lines
1.6 KiB
C++

#ifndef NATIVESTREAMER_H
#define NATIVESTREAMER_H
#include <QObject>
#include <QProcess>
#include <QString>
#include <QTimer>
#include <functional>
/**
* NativeStreamer - Streaming file reader using ollie-9p subprocess.
*
* Uses ollie-9p subprocess for streaming reads (blocking wait files like
* chat, statewait, eventwait). The native library is used for request/response
* operations, but subprocess is needed for streaming because:
* - Blocking reads in Go can't be interrupted by closing the fid
* - Subprocess can be killed cleanly
*
* Restart policies:
* Oneshot — runs once, emits finished on EOF/error
* Looping — always restarts after EOF/error
* Guarded — restarts only if guard() returns true
*/
class NativeStreamer : public QObject
{
Q_OBJECT
public:
enum RestartPolicy { Oneshot, Looping, Guarded };
explicit NativeStreamer(RestartPolicy policy, QObject *parent = nullptr);
~NativeStreamer() override;
void start(const QString &path);
void stop();
bool isRunning() const;
void setGuard(std::function<bool()> guard);
signals:
void dataReady(const QByteArray &data);
void finished(); // Only emitted when not restarting
void errorOccurred(const QString &error);
private slots:
void onReadyRead();
void onProcessFinished(int exitCode, QProcess::ExitStatus status);
private:
void scheduleRestart();
static QString ollie9pBin();
static QString serverAddr();
QString m_path;
QProcess *m_process = nullptr;
RestartPolicy m_policy;
std::function<bool()> m_guard;
QTimer *m_restartTimer = nullptr;
bool m_stopping = false;
};
#endif // NATIVESTREAMER_H