#pragma once #include #include #include #include #include #include #include #include "SharedAudioBuffer.h" #include "IPCProtocol.h" class HelperConnection : private juce::Timer { public: HelperConnection(); ~HelperConnection(); // Lifecycle bool spawnHelper(); void killHelper(); bool isHelperRunning() const { return helperRunning.load(); } // Audio bridge SharedAudioBuffer& getSharedAudio() { return sharedAudio; } SharedSemaphore& getInputSem() { return inputSem; } SharedSemaphore& getOutputSem() { return outputSem; } // Send commands (blocking — waits for response) std::string sendCommand(const std::string& json); // State enum class State { Disconnected, Starting, Connected, Crashed }; State getState() const { return state.load(); } // Callbacks std::function onHelperReady; std::function onHelperCrashed; std::function onParamChanged; std::function onGuiReady; // portName, width, height std::function onGuiResized; // width, height private: void timerCallback() override; void ipcListenLoop(); void handleNotification(const std::string& json); pid_t helperPid = 0; std::atomic helperRunning{false}; std::atomic state{State::Disconnected}; SharedAudioBuffer sharedAudio; SharedSemaphore inputSem; SharedSemaphore outputSem; int serverSocketFd = -1; int clientSocketFd = -1; std::thread ipcThread; std::atomic ipcRunning{false}; std::mutex commandMutex; std::string lastResponse; std::atomic responseReady{false}; int myPid; };