Created
July 4, 2016 13:15
-
-
Save mht/e07604190015d0f909aa17cea5db864b to your computer and use it in GitHub Desktop.
JUCE InterprocessConnection Demo : Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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