mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-01-10 23:44:24 +00:00
Removed some default parameters in NamedPipe methods and fixed time-outs when using posix NamedPipe::read().
This commit is contained in:
parent
601f729bf7
commit
9b8a39e27c
7 changed files with 129 additions and 118 deletions
|
|
@ -64,8 +64,7 @@ bool InterprocessConnection::connectToSocket (const String& hostName,
|
|||
}
|
||||
}
|
||||
|
||||
bool InterprocessConnection::connectToPipe (const String& pipeName,
|
||||
const int pipeReceiveMessageTimeoutMs)
|
||||
bool InterprocessConnection::connectToPipe (const String& pipeName, const int timeoutMs)
|
||||
{
|
||||
disconnect();
|
||||
|
||||
|
|
@ -74,7 +73,7 @@ bool InterprocessConnection::connectToPipe (const String& pipeName,
|
|||
if (newPipe->openExisting (pipeName))
|
||||
{
|
||||
const ScopedLock sl (pipeAndSocketLock);
|
||||
pipeReceiveMessageTimeout = pipeReceiveMessageTimeoutMs;
|
||||
pipeReceiveMessageTimeout = timeoutMs;
|
||||
initialiseWithPipe (newPipe.release());
|
||||
return true;
|
||||
}
|
||||
|
|
@ -82,8 +81,7 @@ bool InterprocessConnection::connectToPipe (const String& pipeName,
|
|||
return false;
|
||||
}
|
||||
|
||||
bool InterprocessConnection::createPipe (const String& pipeName,
|
||||
const int pipeReceiveMessageTimeoutMs)
|
||||
bool InterprocessConnection::createPipe (const String& pipeName, const int timeoutMs)
|
||||
{
|
||||
disconnect();
|
||||
|
||||
|
|
@ -92,7 +90,7 @@ bool InterprocessConnection::createPipe (const String& pipeName,
|
|||
if (newPipe->createNewPipe (pipeName))
|
||||
{
|
||||
const ScopedLock sl (pipeAndSocketLock);
|
||||
pipeReceiveMessageTimeout = pipeReceiveMessageTimeoutMs;
|
||||
pipeReceiveMessageTimeout = timeoutMs;
|
||||
initialiseWithPipe (newPipe.release());
|
||||
return true;
|
||||
}
|
||||
|
|
@ -163,7 +161,7 @@ bool InterprocessConnection::sendMessage (const MemoryBlock& message)
|
|||
if (socket != nullptr)
|
||||
bytesWritten = socket->write (messageData.getData(), (int) messageData.getSize());
|
||||
else if (pipe != nullptr)
|
||||
bytesWritten = pipe->write (messageData.getData(), (int) messageData.getSize());
|
||||
bytesWritten = pipe->write (messageData.getData(), (int) messageData.getSize(), pipeReceiveMessageTimeout);
|
||||
|
||||
return bytesWritten == (int) messageData.getSize();
|
||||
}
|
||||
|
|
@ -171,7 +169,7 @@ bool InterprocessConnection::sendMessage (const MemoryBlock& message)
|
|||
//==============================================================================
|
||||
void InterprocessConnection::initialiseWithSocket (StreamingSocket* const socket_)
|
||||
{
|
||||
jassert (socket == 0);
|
||||
jassert (socket == nullptr);
|
||||
socket = socket_;
|
||||
connectionMadeInt();
|
||||
startThread();
|
||||
|
|
@ -179,7 +177,7 @@ void InterprocessConnection::initialiseWithSocket (StreamingSocket* const socket
|
|||
|
||||
void InterprocessConnection::initialiseWithPipe (NamedPipe* const pipe_)
|
||||
{
|
||||
jassert (pipe == 0);
|
||||
jassert (pipe == nullptr);
|
||||
pipe = pipe_;
|
||||
connectionMadeInt();
|
||||
startThread();
|
||||
|
|
@ -206,6 +204,8 @@ struct ConnectionStateMessage : public MessageManager::MessageBase
|
|||
|
||||
WeakReference<InterprocessConnection> owner;
|
||||
bool connectionMade;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ConnectionStateMessage);
|
||||
};
|
||||
|
||||
void InterprocessConnection::connectionMadeInt()
|
||||
|
|
@ -264,18 +264,16 @@ void InterprocessConnection::deliverDataInt (const MemoryBlock& data)
|
|||
//==============================================================================
|
||||
bool InterprocessConnection::readNextMessageInt()
|
||||
{
|
||||
const int maximumMessageSize = 1024 * 1024 * 10; // sanity check
|
||||
|
||||
uint32 messageHeader[2];
|
||||
const int bytes = socket != nullptr ? socket->read (messageHeader, sizeof (messageHeader), true)
|
||||
: pipe ->read (messageHeader, sizeof (messageHeader), pipeReceiveMessageTimeout);
|
||||
: pipe ->read (messageHeader, sizeof (messageHeader), -1);
|
||||
|
||||
if (bytes == sizeof (messageHeader)
|
||||
&& ByteOrder::swapIfBigEndian (messageHeader[0]) == magicMessageHeader)
|
||||
{
|
||||
int bytesInMessage = (int) ByteOrder::swapIfBigEndian (messageHeader[1]);
|
||||
|
||||
if (bytesInMessage > 0 && bytesInMessage < maximumMessageSize)
|
||||
if (bytesInMessage > 0)
|
||||
{
|
||||
MemoryBlock messageData ((size_t) bytesInMessage, true);
|
||||
int bytesRead = 0;
|
||||
|
|
@ -286,8 +284,10 @@ bool InterprocessConnection::readNextMessageInt()
|
|||
return false;
|
||||
|
||||
const int numThisTime = jmin (bytesInMessage, 65536);
|
||||
const int bytesIn = socket != nullptr ? socket->read (static_cast <char*> (messageData.getData()) + bytesRead, numThisTime, true)
|
||||
: pipe ->read (static_cast <char*> (messageData.getData()) + bytesRead, numThisTime, pipeReceiveMessageTimeout);
|
||||
void* const data = addBytesToPointer (messageData.getData(), bytesRead);
|
||||
|
||||
const int bytesIn = socket != nullptr ? socket->read (data, numThisTime, true)
|
||||
: pipe ->read (data, numThisTime, -1);
|
||||
|
||||
if (bytesIn <= 0)
|
||||
break;
|
||||
|
|
@ -339,7 +339,7 @@ void InterprocessConnection::run()
|
|||
}
|
||||
else
|
||||
{
|
||||
Thread::sleep (2);
|
||||
Thread::sleep (1);
|
||||
}
|
||||
}
|
||||
else if (pipe != nullptr)
|
||||
|
|
|
|||
|
|
@ -96,25 +96,26 @@ public:
|
|||
an InterprocessConnection object and used createPipe() to create a pipe for this
|
||||
to connect to.
|
||||
|
||||
You can optionally specify a timeout length to be passed to the NamedPipe::read() method.
|
||||
|
||||
@param pipeName the name to use for the pipe - this should be unique to your app
|
||||
@param pipeReceiveMessageTimeoutMs a timeout length to be used when reading or writing
|
||||
to the pipe, or -1 for an infinite timeout.
|
||||
@returns true if it connects successfully.
|
||||
@see createPipe, NamedPipe
|
||||
*/
|
||||
bool connectToPipe (const String& pipeName,
|
||||
int pipeReceiveMessageTimeoutMs = -1);
|
||||
bool connectToPipe (const String& pipeName, int pipeReceiveMessageTimeoutMs);
|
||||
|
||||
/** Tries to create a new pipe for other processes to connect to.
|
||||
|
||||
This creates a pipe with the given name, so that other processes can use
|
||||
connectToPipe() to connect to the other end.
|
||||
|
||||
You can optionally specify a timeout length to be passed to the NamedPipe::read() method.
|
||||
|
||||
If another process is already using this pipe, this will fail and return false.
|
||||
@param pipeName the name to use for the pipe - this should be unique to your app
|
||||
@param pipeReceiveMessageTimeoutMs a timeout length to be used when reading or writing
|
||||
to the pipe, or -1 for an infinite timeout.
|
||||
@returns true if the pipe was created, or false if it fails (e.g. if another process is
|
||||
already using using the pipe).
|
||||
*/
|
||||
bool createPipe (const String& pipeName,
|
||||
int pipeReceiveMessageTimeoutMs = -1);
|
||||
bool createPipe (const String& pipeName, int pipeReceiveMessageTimeoutMs);
|
||||
|
||||
/** Disconnects and closes any currently-open sockets or pipes. */
|
||||
void disconnect();
|
||||
|
|
@ -122,16 +123,14 @@ public:
|
|||
/** True if a socket or pipe is currently active. */
|
||||
bool isConnected() const;
|
||||
|
||||
/** Returns the socket that this connection is using (or null if it uses a pipe). */
|
||||
/** Returns the socket that this connection is using (or nullptr if it uses a pipe). */
|
||||
StreamingSocket* getSocket() const noexcept { return socket; }
|
||||
|
||||
/** Returns the pipe that this connection is using (or null if it uses a socket). */
|
||||
/** Returns the pipe that this connection is using (or nullptr if it uses a socket). */
|
||||
NamedPipe* getPipe() const noexcept { return pipe; }
|
||||
|
||||
/** Returns the name of the machine at the other end of this connection.
|
||||
|
||||
This will return an empty string if the other machine isn't known for
|
||||
some reason.
|
||||
This may return an empty string if the name is unknown.
|
||||
*/
|
||||
String getConnectedHostName() const;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue