Skip to content

Instantly share code, notes, and snippets.

@mht mht/server.cpp
Created Jul 4, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.