I Started to work around InterprocessConnection trying with a project from scratch just to test how to use interprocessConnection:
MainComponent.h:
#pragma once
#include <JuceHeader.h>
//==============================================================================
/*
This component lives inside our window, and this is where you should put all
your controls and content.
*/
class MainComponent : public juce::Component
{
public:
//==============================================================================
MainComponent();
~MainComponent() override;
//==============================================================================
void paint (juce::Graphics&) override;
void resized() override;
bool connect();
private:
TextButton send {"send"}, connectBtn{"connect"}, disconnect{"disconnect"};
class InterprocessMixerConnection : public InterprocessConnection
{
public:
InterprocessMixerConnection (MainComponent& mainComponent);
void connectionMade() override;
void connectionLost() override;
void messageReceived (const MemoryBlock& message) override;
private:
MainComponent& owner;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (InterprocessMixerConnection)
};
InterprocessMixerConnection mixerConnection {*this};
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
};
MainComponent.cpp:
#include "MainComponent.h"
//==============================================================================
MainComponent::MainComponent()
{
setSize (600, 400);
addAndMakeVisible(connectBtn);
addAndMakeVisible(send);
addAndMakeVisible(disconnect);
connectBtn.onClick = [this]() {
if (mixerConnection.isConnected())
{
DBG("yet connected");
} else {
if (connect())
{
DBG("succesfully connected");
} else {
DBG("not connected");
}
}
};
send.onClick = [this]() {
const String text ("sender message");
MemoryBlock messageData (text.toUTF8(), (size_t) text.getNumBytesAsUTF8());
mixerConnection.sendMessage(messageData);
};
disconnect.onClick = [this]() {
mixerConnection.disconnect();
};
}
MainComponent::~MainComponent()
{
mixerConnection.disconnect();
}
bool MainComponent::connect()
{
bool connected = false;
mixerConnection.createPipe("Ayra Node Graph", 5000, true);
if (!mixerConnection.isConnected())
if (mixerConnection.connectToPipe("Ayra Node Graph", 5000))
connected = true;
return connected || mixerConnection.isConnected();
}
//==============================================================================
void MainComponent::paint (juce::Graphics& g)
{
g.fillAll (getLookAndFeel().findColour (juce::ResizableWindow::backgroundColourId));
}
void MainComponent::resized()
{
auto area = getLocalBounds();
connectBtn.setBounds(area.removeFromLeft(area.getWidth()/3));
send.setBounds(area.removeFromLeft(area.getWidth()/2));
disconnect.setBounds(area);
}
//MARK: InterprocessMixerConnection ==============================================================================
MainComponent::InterprocessMixerConnection::InterprocessMixerConnection (MainComponent& mainComponent) : InterprocessConnection (true), owner (mainComponent)
{
}
void MainComponent::InterprocessMixerConnection::connectionMade()
{
DBG("connectionMade()");
}
void MainComponent::InterprocessMixerConnection::connectionLost()
{
DBG("connectionLost()");
}
void MainComponent::InterprocessMixerConnection::messageReceived (const MemoryBlock& message)
{
DBG("messageReceived(\n" + message.toString() + "\n)");
}
I suppose to open two instances of this app ā click connect on both ā click send to one of the two and get in the other printed the message āsender messageā.
What really happens is that open just only one instance i click on connect and it connects, after a while I get in console connectionLost() and if I click on Send button my app starts work for a while (I can see working mouse icon on my mac) and after again is displayed connectionLost().
My final purpose is to get more apps connected to the same pipe and get them send messageData at their sliders value-changes so all sliders of those app are sync together (immagine widgets slider demo on juce DemoRunner where slider are synched together but with those slider in different apps )