Skip to content

Commit

Permalink
Merge pull request #24 from gstorer/master
Browse files Browse the repository at this point in the history
Use bound IP address when using bound port
  • Loading branch information
getnamo authored Mar 2, 2022
2 parents d2109f7 + 1686aa8 commit fe2e601
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
14 changes: 10 additions & 4 deletions Source/UDPWrapper/Private/UDPComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ UUDPComponent::UUDPComponent(const FObjectInitializer &init) : UActorComponent(i

void UUDPComponent::LinkupCallbacks()
{
Native->OnSendOpened = [this](int32 SpecifiedPort, int32 BoundPort)
Native->OnSendOpened = [this](int32 SpecifiedPort, int32 BoundPort, FString BoundIP)
{
Settings.bIsSendOpen = true;
Settings.SendBoundPort = BoundPort; //ensure sync on opened bound port
Settings.SendBoundIP = BoundIP;

Settings.SendIP = Native->Settings.SendIP;
Settings.SendPort = Native->Settings.SendPort;

OnSendSocketOpened.Broadcast(Settings.SendPort, Settings.SendBoundPort);
OnSendSocketOpened.Broadcast(Settings.SendPort, Settings.SendBoundPort, Settings.SendBoundIP);
};
Native->OnSendClosed = [this](int32 Port)
{
Expand Down Expand Up @@ -67,6 +68,7 @@ int32 UUDPComponent::OpenSendSocket(const FString& InIP /*= TEXT("127.0.0.1")*/,
bool UUDPComponent::CloseSendSocket()
{
Settings.SendBoundPort = 0;
Settings.SendBoundIP = FString(TEXT("0.0.0.0"));
return Native->CloseSendSocket();
}

Expand Down Expand Up @@ -173,10 +175,13 @@ int32 FUDPNative::OpenSendSocket(const FString& InIP /*= TEXT("127.0.0.1")*/, co
bool bDidConnect = SenderSocket->Connect(*RemoteAdress);
Settings.bIsSendOpen = true;
Settings.SendBoundPort = SenderSocket->GetPortNo();
TSharedRef<FInternetAddr> SendBoundAddress = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
SenderSocket->GetAddress(*SendBoundAddress);
Settings.SendBoundIP = SendBoundAddress->ToString(false);

if (OnSendOpened)
{
OnSendOpened(Settings.SendPort, Settings.SendBoundPort);
OnSendOpened(Settings.SendPort, Settings.SendBoundPort, Settings.SendBoundIP);
}

return Settings.SendBoundPort;
Expand Down Expand Up @@ -230,7 +235,7 @@ bool FUDPNative::OpenReceiveSocket(const FString& InListenIP /*= TEXT("0.0.0.0")
UE_LOG(LogTemp, Error, TEXT("FUDPNative::OpenReceiveSocket Can't bind to SendBoundPort if send socket hasn't been opened before this call."));
return false;
}
Settings.ReceiveIP = Settings.SendIP;
Settings.ReceiveIP = Settings.SendBoundIP;
Settings.ReceivePort = Settings.SendBoundPort;
}
else
Expand Down Expand Up @@ -351,6 +356,7 @@ FUDPSettings::FUDPSettings()
SendIP = FString(TEXT("127.0.0.1"));
SendPort = 3001;
SendBoundPort = 0; //invalid if 0
SendBoundIP = FString(TEXT("0.0.0.0"));
ReceiveIP = FString(TEXT("0.0.0.0"));
ReceivePort = 3002;
SendSocketName = FString(TEXT("ue4-dgram-send"));
Expand Down
10 changes: 7 additions & 3 deletions Source/UDPWrapper/Public/UDPComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ struct UDPWRAPPER_API FUDPSettings
int32 SendPort;

/** Port to which send is bound to on this client (this should change on each open) */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "UDP Connection Properties")
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "UDP Connection Properties")
int32 SendBoundPort;

/** IP to which send is bound to on this client (this could change on open) */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "UDP Connection Properties")
FString SendBoundIP;

/** Default receiving socket IP string in form e.g. 0.0.0.0 for all connections, may need 127.0.0.1 for some cases. */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "UDP Connection Properties")
FString ReceiveIP;
Expand Down Expand Up @@ -75,7 +79,7 @@ class UDPWRAPPER_API FUDPNative
TFunction<void(const TArray<uint8>&, const FString&)> OnReceivedBytes;
TFunction<void(int32 Port)> OnReceiveOpened;
TFunction<void(int32 Port)> OnReceiveClosed;
TFunction<void(int32 SpecifiedPort, int32 BoundPort)> OnSendOpened;
TFunction<void(int32 SpecifiedPort, int32 BoundPort, FString BoundIP)> OnSendOpened;
TFunction<void(int32 Port)> OnSendClosed;

//Default settings, on open send/receive they will sync with what was last passed into them
Expand Down Expand Up @@ -125,7 +129,7 @@ class UDPWRAPPER_API FUDPNative
};

DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FUDPSocketStateSignature, int32, Port);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FUDPSocketSendStateSignature, int32, SpecifiedPort, int32, BoundPort);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_ThreeParams(FUDPSocketSendStateSignature, int32, SpecifiedPort, int32, BoundPort, const FString&, BoundIP);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FUDPMessageSignature, const TArray<uint8>&, Bytes, const FString&, IPAddress);

UCLASS(ClassGroup = "Networking", meta = (BlueprintSpawnableComponent))
Expand Down

0 comments on commit fe2e601

Please sign in to comment.