Browse Source

Added some methods to OSCSender and OSCReceiver to allow them to use existing sockets

tags/2021-05-28
jules 7 years ago
parent
commit
eff880e01f
4 changed files with 67 additions and 16 deletions
  1. +23
    -7
      modules/juce_osc/osc/juce_OSCReceiver.cpp
  2. +8
    -0
      modules/juce_osc/osc/juce_OSCReceiver.h
  3. +20
    -4
      modules/juce_osc/osc/juce_OSCSender.cpp
  4. +16
    -5
      modules/juce_osc/osc/juce_OSCSender.h

+ 23
- 7
modules/juce_osc/osc/juce_OSCReceiver.cpp View File

@@ -331,13 +331,12 @@ struct OSCReceiver::Pimpl : private Thread,
} }
//============================================================================== //==============================================================================
bool connectToPort (int portNum)
bool connectToPort (int portNumber)
{ {
if (! disconnect()) if (! disconnect())
return false; return false;
portNumber = portNum;
socket = new DatagramSocket (false);
socket.setOwned (new DatagramSocket (false));
if (! socket->bindToPort (portNumber)) if (! socket->bindToPort (portNumber))
return false; return false;
@@ -346,14 +345,27 @@ struct OSCReceiver::Pimpl : private Thread,
return true; return true;
} }
bool connectToSocket (DatagramSocket& newSocket)
{
if (! disconnect())
return false;
socket.setNonOwned (&newSocket);
startThread();
return true;
}
bool disconnect() bool disconnect()
{ {
if (socket != nullptr) if (socket != nullptr)
{ {
signalThreadShouldExit(); signalThreadShouldExit();
socket->shutdown();
if (socket.willDeleteObject())
socket->shutdown();
waitForThreadToExit (10000); waitForThreadToExit (10000);
socket = nullptr;
socket.reset();
} }
return true; return true;
} }
@@ -567,8 +579,7 @@ private:
Array<std::pair<OSCAddress, OSCReceiver::ListenerWithOSCAddress<OSCReceiver::MessageLoopCallback>*>> listenersWithAddress; Array<std::pair<OSCAddress, OSCReceiver::ListenerWithOSCAddress<OSCReceiver::MessageLoopCallback>*>> listenersWithAddress;
Array<std::pair<OSCAddress, OSCReceiver::ListenerWithOSCAddress<OSCReceiver::RealtimeCallback>*>> realtimeListenersWithAddress; Array<std::pair<OSCAddress, OSCReceiver::ListenerWithOSCAddress<OSCReceiver::RealtimeCallback>*>> realtimeListenersWithAddress;
ScopedPointer<DatagramSocket> socket;
int portNumber = 0;
OptionalScopedPointer<DatagramSocket> socket;
OSCReceiver::FormatErrorHandler formatErrorHandler { nullptr }; OSCReceiver::FormatErrorHandler formatErrorHandler { nullptr };
enum { oscBufferSize = 4098 }; enum { oscBufferSize = 4098 };
@@ -590,6 +601,11 @@ bool OSCReceiver::connect (int portNumber)
return pimpl->connectToPort (portNumber); return pimpl->connectToPort (portNumber);
} }
bool OSCReceiver::connectToSocket (DatagramSocket& socket)
{
return pimpl->connectToSocket (socket);
}
bool OSCReceiver::disconnect() bool OSCReceiver::disconnect()
{ {
return pimpl->disconnect(); return pimpl->disconnect();


+ 8
- 0
modules/juce_osc/osc/juce_OSCReceiver.h View File

@@ -54,6 +54,14 @@ public:
*/ */
bool connect (int portNumber); bool connect (int portNumber);
/** Connects to a UDP datagram socket that is already set up,
and starts listening to OSC packets arriving on this port.
Make sure that the object you give it doesn't get deleted while this
object is still using it!
@returns true if the connection was successful; false otherwise.
*/
bool connectToSocket (DatagramSocket& socketToUse);
//============================================================================== //==============================================================================
/** Disconnects from the currently used UDP port. /** Disconnects from the currently used UDP port.
@returns true if the disconnection was successful; false otherwise. @returns true if the disconnection was successful; false otherwise.


+ 20
- 4
modules/juce_osc/osc/juce_OSCSender.cpp View File

@@ -215,20 +215,31 @@ struct OSCSender::Pimpl
if (! disconnect()) if (! disconnect())
return false; return false;
socket = new DatagramSocket (true);
socket.setOwned (new DatagramSocket (true));
targetHostName = newTargetHost; targetHostName = newTargetHost;
targetPortNumber = newTargetPort; targetPortNumber = newTargetPort;
if (socket->bindToPort (0)) // 0 = use any local port assigned by the OS. if (socket->bindToPort (0)) // 0 = use any local port assigned by the OS.
return true; return true;
socket = nullptr;
socket.reset();
return false; return false;
} }
bool connectToSocket (DatagramSocket& newSocket, const String& newTargetHost, int newTargetPort)
{
if (! disconnect())
return false;
socket.setNonOwned (&newSocket);
targetHostName = newTargetHost;
targetPortNumber = newTargetPort;
return true;
}
bool disconnect() bool disconnect()
{ {
socket = nullptr;
socket.reset();
return true; return true;
} }
@@ -273,7 +284,7 @@ private:
} }
//============================================================================== //==============================================================================
ScopedPointer<DatagramSocket> socket;
OptionalScopedPointer<DatagramSocket> socket;
String targetHostName; String targetHostName;
int targetPortNumber = 0; int targetPortNumber = 0;
@@ -298,6 +309,11 @@ bool OSCSender::connect (const String& targetHostName, int targetPortNumber)
return pimpl->connect (targetHostName, targetPortNumber); return pimpl->connect (targetHostName, targetPortNumber);
} }
bool OSCSender::connectToSocket (DatagramSocket& socket, const String& targetHostName, int targetPortNumber)
{
return pimpl->connectToSocket (socket, targetHostName, targetPortNumber);
}
bool OSCSender::disconnect() bool OSCSender::disconnect()
{ {
return pimpl->disconnect(); return pimpl->disconnect();


+ 16
- 5
modules/juce_osc/osc/juce_OSCSender.h View File

@@ -48,23 +48,34 @@ public:
/** Connects to a datagram socket and prepares the socket for sending OSC /** Connects to a datagram socket and prepares the socket for sending OSC
packets to the specified target. packets to the specified target.
Note: the operating system will choose which specific network adapter(s)
to bind your socket to, and which local port to use for the sender.
@param targetHostName The remote host to which messages will be send. @param targetHostName The remote host to which messages will be send.
@param targetPortNumber The remote UDP port number on which the host will @param targetPortNumber The remote UDP port number on which the host will
receive the messages. receive the messages.
@returns true if the connection was successful; false otherwise. @returns true if the connection was successful; false otherwise.
Note: the operating system will choose which specific network adapter(s)
to bind your socket to, and which local port to use for the sender.
@see send, disconnect. @see send, disconnect.
*/ */
bool connect (const String& targetHostName, int targetPortNumber); bool connect (const String& targetHostName, int targetPortNumber);
/** Uses an existing datagram socket for sending OSC packets to the specified target.
@param socket An existing datagram socket. Make sure this doesn't
get deleted while this class is still using it!
@param targetHostName The remote host to which messages will be send.
@param targetPortNumber The remote UDP port number on which the host will
receive the messages.
@returns true if the connection was successful; false otherwise.
@see connect, send, disconnect.
*/
bool connectToSocket (DatagramSocket& socket, const String& targetHostName, int targetPortNumber);
//============================================================================== //==============================================================================
/** Disconnects from the currently used UDP port. /** Disconnects from the currently used UDP port.
@returns true if the disconnection was successful; false otherwise. @returns true if the disconnection was successful; false otherwise.
@see connect. @see connect.
*/ */
bool disconnect(); bool disconnect();


Loading…
Cancel
Save