mirror of
https://github.com/storytold/UE4-OSC.git
synced 2026-10-09 00:09:43 +00:00
validate the "SendTargets" setting
This commit is contained in:
@@ -103,7 +103,7 @@ public:
|
||||
{
|
||||
FIPv4Address receiveAddress(0);
|
||||
uint32_t receivePort;
|
||||
if(UOscSettings::Parse(settings->ReceiveFrom, &receiveAddress, &receivePort))
|
||||
if(UOscSettings::Parse(settings->ReceiveFrom, &receiveAddress, &receivePort, UOscSettings::ParseOption::OptionalAddress))
|
||||
{
|
||||
_dispatcher->Listen(receiveAddress, receivePort, settings->MulticastLoopback);
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ int32 UOscSettings::AddSendTarget(const FString & ip_port)
|
||||
|
||||
FIPv4Address address(0);
|
||||
uint32_t port;
|
||||
if(Parse(ip_port, &address, &port) && address != FIPv4Address::Any)
|
||||
if(Parse(ip_port, &address, &port, ParseOption::Strict))
|
||||
{
|
||||
target->SetIp(address.Value);
|
||||
target->SetPort(port);
|
||||
@@ -142,30 +142,54 @@ void UOscSettings::Send(const uint8 *buffer, int32 length, int32 targetIndex)
|
||||
}
|
||||
}
|
||||
|
||||
bool UOscSettings::Parse(const FString & ip_port, FIPv4Address * address, uint32_t * port)
|
||||
bool UOscSettings::Parse(const FString & ip_port, FIPv4Address * address, uint32_t * port, ParseOption option)
|
||||
{
|
||||
if(ip_port.IsEmpty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
FIPv4Address addressResult(0);
|
||||
uint32_t portResult;
|
||||
FIPv4Address addressResult = FIPv4Address::Any;
|
||||
uint32_t portResult = 0;
|
||||
|
||||
int32 sep = -1;
|
||||
if(ip_port.FindChar(TEXT(':'), sep))
|
||||
const bool hasSep = ip_port.FindChar(TEXT(':'), sep);
|
||||
|
||||
if(hasSep)
|
||||
{
|
||||
portResult = FCString::Atoi(&ip_port.GetCharArray()[sep+1]);
|
||||
if(portResult == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto ip = ip_port.Left(sep).Trim();
|
||||
if(!FIPv4Address::Parse(ip, addressResult))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
portResult = FCString::Atoi(&ip_port.GetCharArray()[sep+1]);
|
||||
if(portResult == 0)
|
||||
else
|
||||
{
|
||||
return false;
|
||||
if(option == ParseOption::Strict)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if(option == ParseOption::OptionalAddress)
|
||||
{
|
||||
portResult = FCString::Atoi(ip_port.GetCharArray().GetData());
|
||||
if(portResult == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if(option == ParseOption::OptionalPort)
|
||||
{
|
||||
if(!FIPv4Address::Parse(ip_port, addressResult))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*address = addressResult;
|
||||
@@ -194,3 +218,27 @@ void UOscSettings::UpdateKeyInputs(UOscDispatcher & dispatcher)
|
||||
_keyReceivers.Add(std::move(receiver));
|
||||
}
|
||||
}
|
||||
|
||||
void UOscSettings::PostEditChangeProperty(FPropertyChangedEvent & PropertyChangedEvent)
|
||||
{
|
||||
static const FName SendTargetsName("SendTargets");
|
||||
|
||||
if( PropertyChangedEvent.GetPropertyName() == SendTargetsName )
|
||||
{
|
||||
for(auto & target : SendTargets)
|
||||
{
|
||||
FIPv4Address address;
|
||||
uint32_t port;
|
||||
if( !Parse(target, &address, &port, ParseOption::OptionalPort) || address == FIPv4Address::Any )
|
||||
{
|
||||
target = "127.0.0.1:8000";
|
||||
}
|
||||
else if( port == 0 )
|
||||
{
|
||||
target = address.ToString() + ":8000";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Super::PostEditChangeProperty(PropertyChangedEvent);
|
||||
}
|
||||
|
||||
@@ -32,6 +32,11 @@ public:
|
||||
UPROPERTY(Config, EditAnywhere, Category=Receive)
|
||||
FString ReceiveFrom;
|
||||
|
||||
/**
|
||||
* Specify the addresses (ip:port) to send messages to.
|
||||
*
|
||||
* Addresses can also be added at runtime with the AddSendOscTarget function.
|
||||
*/
|
||||
UPROPERTY(Config, EditAnywhere, Category=Send)
|
||||
TArray<FString> SendTargets;
|
||||
|
||||
@@ -58,13 +63,22 @@ public:
|
||||
|
||||
void UpdateKeyInputs(UOscDispatcher & dispatcher);
|
||||
|
||||
void PostEditChangeProperty( struct FPropertyChangedEvent& PropertyChangedEvent) override;
|
||||
|
||||
public:
|
||||
|
||||
enum class ParseOption
|
||||
{
|
||||
Strict, /// "192.168.0.12:8000"
|
||||
OptionalPort, /// "192.168.0.12" or "192.168.0.12:8000"
|
||||
OptionalAddress, /// "8000" or "192.168.0.12:8000"
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Parse "8000" and "192.168.0.12" to IP and port values.
|
||||
* @return true if succeed
|
||||
*/
|
||||
static bool Parse(const FString & ip_port, FIPv4Address * address, uint32_t * port);
|
||||
static bool Parse(const FString & ip_port, FIPv4Address * address, uint32_t * port, ParseOption option);
|
||||
|
||||
private:
|
||||
int32 AddSendTarget(const FString & ip_port);
|
||||
|
||||
@@ -124,5 +124,5 @@ public:
|
||||
* @see SendOsc
|
||||
*/
|
||||
UFUNCTION(BlueprintCallable, Category=OSC)
|
||||
static int32 AddSendOscTarget(FString IpPort);
|
||||
static int32 AddSendOscTarget(FString IpPort = "127.0.0.1:8000");
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user