Skip to content

Commit

Permalink
add setup argument to listen on specific ip (#7574)
Browse files Browse the repository at this point in the history
  • Loading branch information
2bbb authored Jul 21, 2023
1 parent da1dc6b commit 4b5632a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
5 changes: 3 additions & 2 deletions addons/ofxOsc/src/ofxOscReceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ ofxOscReceiver& ofxOscReceiver::copy(const ofxOscReceiver &other){
}

//--------------------------------------------------------------
bool ofxOscReceiver::setup(int port){
bool ofxOscReceiver::setup(int port, std::string host) {
if(listenSocket){ // already running
stop();
}
settings.port = port;
settings.host = host;
return start();
}

Expand Down Expand Up @@ -62,7 +63,7 @@ bool ofxOscReceiver::start() {
// create socket
osc::UdpListeningReceiveSocket *socket = nullptr;
try{
osc::IpEndpointName name(osc::IpEndpointName::ANY_ADDRESS, settings.port);
osc::IpEndpointName name(settings.host.c_str(), settings.port);
socket = new osc::UdpListeningReceiveSocket(name, this, settings.reuse);
auto deleter = [](osc::UdpListeningReceiveSocket*socket){
// tell the socket to shutdown
Expand Down
11 changes: 6 additions & 5 deletions addons/ofxOsc/src/ofxOscReceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
/// \struct ofxOscSenderSettings
/// \brief OSC message sender settings
struct ofxOscReceiverSettings {
int port = 0; ///< port to listen on
bool reuse = true; ///< should the port be reused by other receivers?
bool start = true; ///< start listening after setup?
int port = 0; ///< port to listen on
std::string host = "0.0.0.0"; ///< host to listen on
bool reuse = true; ///< should the port be reused by other receivers?
bool start = true; ///< start listening after setup?
};

/// \class ofxOscReceiver
Expand All @@ -30,14 +31,14 @@ class ofxOscReceiver : public osc::OscPacketListener {
/// for operator= and copy constructor
ofxOscReceiver& copy(const ofxOscReceiver &other);

/// set up the receiver with the port to listen for messages on
/// set up the receiver with the port (and specific host/ip) to listen for messages on
/// and start listening
///
/// multiple receivers can share the same port if port reuse is
/// enabled (true by default)
///
/// \return true if listening started
bool setup(int port);
bool setup(int port, std::string host = "0.0.0.0");

/// set up the receiver with the given settings
///
Expand Down

1 comment on commit 4b5632a

@artificiel
Copy link
Contributor

@artificiel artificiel commented on 4b5632a Jul 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the option to specify the address, but I don't agree with the parameter order where it becomes setup(port, host) -- it is much more common to have host followed by port, and ofxOscSender uses that order; this creates an unusual usage exception.

since the current behaviour is a single int arg for the port, a new setup method :

bool setup(std::string host, int port);

could be introduced without generating conflicts (and the default ="0.0.0.0" is then not needed because if you use this version of setup, it is explicitly because you want to set a host. and the old setup(int port) then simply calls this setup with 0.0.0.0 as the addr)

Please sign in to comment.