Skip to content

Instantly share code, notes, and snippets.

@mht
Created July 4, 2016 13:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mht/e07604190015d0f909aa17cea5db864b to your computer and use it in GitHub Desktop.
Save mht/e07604190015d0f909aa17cea5db864b to your computer and use it in GitHub Desktop.
JUCE InterprocessConnection Demo : Server
const int kPortNumber = 52713;
class Connection : public InterprocessConnection
{
public:
Connection(WaitableEvent& stop_signal)
: InterprocessConnection(false, 15),
stop_signal_(stop_signal)
{
}
void connectionMade() override
{
printf("Connection made\n");
String msg("Stop talking!");
MemoryBlock mb(msg.toRawUTF8(), msg.length());
sendMessage(mb);
}
void connectionLost() override
{
printf("Connection lost\n");
}
void messageReceived(const MemoryBlock& msg) override
{
const auto str = msg.toString();
printf("From client: %s\n", str.toRawUTF8());
if (str.contains("713")) {
stop_signal_.signal();
}
}
private:
WaitableEvent& stop_signal_;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Connection);
};
class IPCServer : public InterprocessConnectionServer
{
public:
IPCServer(WaitableEvent& stop_signal)
: stop_signal_(stop_signal), connection_(nullptr)
{
}
~IPCServer()
{
delete connection_;
}
protected:
InterprocessConnection* createConnectionObject() override
{
connection_ = new Connection(stop_signal_);
return connection_;
}
WaitableEvent& stop_signal_;
Connection* connection_;
};
int main (int argc, char* argv[])
{
printf("\n");
WaitableEvent stop_signal;
IPCServer server(stop_signal);
if (server.beginWaitingForSocket(kPortNumber)) {
printf("Waiting for client...\n");
while (!stop_signal.wait(500)) {
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment