OSC bind address
This commit is contained in:
@@ -55,7 +55,7 @@ OscServer::~OscServer()
|
||||
Stop();
|
||||
}
|
||||
|
||||
bool OscServer::Start(unsigned short port, const Callbacks& callbacks, std::string& error)
|
||||
bool OscServer::Start(const std::string& bindAddress, unsigned short port, const Callbacks& callbacks, std::string& error)
|
||||
{
|
||||
if (port == 0)
|
||||
return true;
|
||||
@@ -78,11 +78,15 @@ bool OscServer::Start(unsigned short port, const Callbacks& callbacks, std::stri
|
||||
|
||||
sockaddr_in address = {};
|
||||
address.sin_family = AF_INET;
|
||||
address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||
if (!TryParseBindAddress(bindAddress, address.sin_addr, error))
|
||||
{
|
||||
mSocket.reset();
|
||||
return false;
|
||||
}
|
||||
address.sin_port = htons(static_cast<u_short>(port));
|
||||
if (bind(mSocket.get(), reinterpret_cast<sockaddr*>(&address), sizeof(address)) != 0)
|
||||
{
|
||||
error = "Could not bind OSC listener to UDP port " + std::to_string(port) + ".";
|
||||
error = "Could not bind OSC listener to " + bindAddress + ":" + std::to_string(port) + ".";
|
||||
mSocket.reset();
|
||||
return false;
|
||||
}
|
||||
@@ -92,6 +96,24 @@ bool OscServer::Start(unsigned short port, const Callbacks& callbacks, std::stri
|
||||
return true;
|
||||
}
|
||||
|
||||
bool OscServer::TryParseBindAddress(const std::string& bindAddress, in_addr& address, std::string& error)
|
||||
{
|
||||
if (bindAddress.empty())
|
||||
{
|
||||
error = "OSC bind address must not be empty.";
|
||||
return false;
|
||||
}
|
||||
|
||||
address = {};
|
||||
if (InetPtonA(AF_INET, bindAddress.c_str(), &address) != 1)
|
||||
{
|
||||
error = "Invalid OSC bind address '" + bindAddress + "'. Use an IPv4 address such as 127.0.0.1 or 0.0.0.0.";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void OscServer::Stop()
|
||||
{
|
||||
mRunning = false;
|
||||
|
||||
@@ -20,7 +20,7 @@ public:
|
||||
OscServer();
|
||||
~OscServer();
|
||||
|
||||
bool Start(unsigned short port, const Callbacks& callbacks, std::string& error);
|
||||
bool Start(const std::string& bindAddress, unsigned short port, const Callbacks& callbacks, std::string& error);
|
||||
void Stop();
|
||||
|
||||
unsigned short GetPort() const { return mPort; }
|
||||
@@ -37,6 +37,7 @@ private:
|
||||
void ServerLoop();
|
||||
bool DecodeMessage(const char* data, int byteCount, OscMessage& message, std::string& error) const;
|
||||
bool DispatchMessage(const OscMessage& message, std::string& error) const;
|
||||
static bool TryParseBindAddress(const std::string& bindAddress, in_addr& address, std::string& error);
|
||||
static bool DecodeArgument(const char* data, int byteCount, int& offset, char valueType, std::string& valueJson);
|
||||
static bool ReadPaddedString(const char* data, int byteCount, int& offset, std::string& value);
|
||||
static bool ReadInt32(const char* data, int byteCount, int& offset, int& value);
|
||||
|
||||
@@ -44,7 +44,7 @@ bool StartRuntimeControlServices(
|
||||
oscCallbacks.updateParameter = [&composite](const std::string& layerKey, const std::string& parameterKey, const std::string& valueJson, std::string& actionError) {
|
||||
return composite.UpdateLayerParameterByControlKeyJson(layerKey, parameterKey, valueJson, actionError);
|
||||
};
|
||||
if (runtimeHost.GetOscPort() > 0 && !oscServer.Start(runtimeHost.GetOscPort(), oscCallbacks, error))
|
||||
if (runtimeHost.GetOscPort() > 0 && !oscServer.Start(runtimeHost.GetOscBindAddress(), runtimeHost.GetOscPort(), oscCallbacks, error))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user