From a70101e3ecb99c0ff81094477aa345d23562e44b Mon Sep 17 00:00:00 2001 From: reuk Date: Mon, 2 Nov 2020 12:51:17 +0000 Subject: [PATCH] IPC: Allow setting custom timeouts in disconnect Also allows optionally ignoring callbacks during disconnect, so that the call to `connectionLost` can be bypassed when disconnect is called from the derived class destructor. --- .../interprocess/juce_InterprocessConnection.cpp | 11 +++++++---- .../interprocess/juce_InterprocessConnection.h | 8 +++++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/modules/juce_events/interprocess/juce_InterprocessConnection.cpp b/modules/juce_events/interprocess/juce_InterprocessConnection.cpp index 72f414a579..0d89775a01 100644 --- a/modules/juce_events/interprocess/juce_InterprocessConnection.cpp +++ b/modules/juce_events/interprocess/juce_InterprocessConnection.cpp @@ -89,7 +89,7 @@ InterprocessConnection::~InterprocessConnection() jassert (! safeAction->isSafe()); callbackConnectionState = false; - disconnect(); + disconnect (4000, Notify::no); thread.reset(); } @@ -145,7 +145,7 @@ bool InterprocessConnection::createPipe (const String& pipeName, int timeoutMs, return false; } -void InterprocessConnection::disconnect() +void InterprocessConnection::disconnect (int timeoutMs, Notify notify) { thread->signalThreadShouldExit(); @@ -155,10 +155,13 @@ void InterprocessConnection::disconnect() if (pipe != nullptr) pipe->close(); } - thread->stopThread (4000); + thread->stopThread (timeoutMs); deletePipeAndSocket(); - connectionLostInt(); + if (notify == Notify::yes) + connectionLostInt(); + + callbackConnectionState = false; safeAction->setSafe (false); } diff --git a/modules/juce_events/interprocess/juce_InterprocessConnection.h b/modules/juce_events/interprocess/juce_InterprocessConnection.h index d9fd82b37b..2025a69fb2 100644 --- a/modules/juce_events/interprocess/juce_InterprocessConnection.h +++ b/modules/juce_events/interprocess/juce_InterprocessConnection.h @@ -120,12 +120,18 @@ public: */ bool createPipe (const String& pipeName, int pipeReceiveMessageTimeoutMs, bool mustNotExist = false); + /** Whether the disconnect call should trigger callbacks. */ + enum class Notify { no, yes }; + /** Disconnects and closes any currently-open sockets or pipes. Derived classes *must* call this in their destructors in order to avoid undefined behaviour. + + @param timeoutMs the time in ms to wait before killing the thread by force + @param notify whether or not to call `connectionLost` */ - void disconnect(); + void disconnect (int timeoutMs = -1, Notify notify = Notify::yes); /** True if a socket or pipe is currently active. */ bool isConnected() const;