Skip to content

Commit

Permalink
Version for UE5.1 added
Browse files Browse the repository at this point in the history
  • Loading branch information
extall committed Jan 22, 2023
1 parent 4106b11 commit 8e902b9
Show file tree
Hide file tree
Showing 28 changed files with 868 additions and 0 deletions.
Binary file not shown.
Binary file not shown.
7 changes: 7 additions & 0 deletions 5.0/UDPCommunication/Binaries/Win64/UnrealEditor.modules
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"BuildId": "16433597",
"Modules":
{
"UDPCommunication": "UnrealEditor-UDPCommunication.dll"
}
}
Binary file added 5.0/UDPCommunication/Resources/Icon128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 5.0/UDPCommunication/Resources/udp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
85 changes: 85 additions & 0 deletions 5.0/UDPCommunication/Source/UDPCommunication/Classes/UDPData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#pragma once

#include "Serialization/Archive.h"
#include "UDPData.generated.h"

USTRUCT(BlueprintType)
struct FUDPData {
GENERATED_USTRUCT_BODY()
UPROPERTY(EditAnyWhere, BlueprintReadWrite, Category = "UDPCommunication")
float float1 = 0.0f;
UPROPERTY(EditAnyWhere, BlueprintReadWrite, Category = "UDPCommunication")
float float2 = 0.0f;
UPROPERTY(EditAnyWhere, BlueprintReadWrite, Category = "UDPCommunication")
float float3 = 0.0f;
UPROPERTY(EditAnyWhere, BlueprintReadWrite, Category = "UDPCommunication")
float float4 = 0.0f;
UPROPERTY(EditAnyWhere, BlueprintReadWrite, Category = "UDPCommunication")
float float5 = 0.0f;
UPROPERTY(EditAnyWhere, BlueprintReadWrite, Category = "UDPCommunication")
float float6 = 0.0f;
UPROPERTY(EditAnyWhere, BlueprintReadWrite, Category = "UDPCommunication")
float float7 = 0.0f;
UPROPERTY(EditAnyWhere, BlueprintReadWrite, Category = "UDPCommunication")
float float8 = 0.0f;
UPROPERTY(EditAnyWhere, BlueprintReadWrite, Category = "UDPCommunication")
float float9 = 0.0f;
UPROPERTY(EditAnyWhere, BlueprintReadWrite, Category = "UDPCommunication")
float float10 = 0.0f;
UPROPERTY(EditAnyWhere, BlueprintReadWrite, Category = "UDPCommunication")
float float11 = 0.0f;
UPROPERTY(EditAnyWhere, BlueprintReadWrite, Category = "UDPCommunication")
float float12 = 0.0f;
UPROPERTY(EditAnyWhere, BlueprintReadWrite, Category = "UDPCommunication")
float float13 = 0.0f;
UPROPERTY(EditAnyWhere, BlueprintReadWrite, Category = "UDPCommunication")
float float14 = 0.0f;
UPROPERTY(EditAnyWhere, BlueprintReadWrite, Category = "UDPCommunication")
float float15 = 0.0f;
UPROPERTY(EditAnyWhere, BlueprintReadWrite, Category = "UDPCommunication")
float float16 = 0.0f;
UPROPERTY(EditAnyWhere, BlueprintReadWrite, Category = "UDPCommunication")
float float17 = 0.0f;
UPROPERTY(EditAnyWhere, BlueprintReadWrite, Category = "UDPCommunication")
float float18 = 0.0f;
UPROPERTY(EditAnyWhere, BlueprintReadWrite, Category = "UDPCommunication")
float float19 = 0.0f;
UPROPERTY(EditAnyWhere, BlueprintReadWrite, Category = "UDPCommunication")
float float20 = 0.0f;
UPROPERTY(EditAnyWhere, BlueprintReadWrite, Category = "UDPCommunication")
uint8 uint1 = 0;
UPROPERTY(EditAnyWhere, BlueprintReadWrite, Category = "UDPCommunication")
uint8 uint2 = 0;
UPROPERTY(EditAnyWhere, BlueprintReadWrite, Category = "UDPCommunication")
uint8 uint3 = 0;
FUDPData() {
}
};

FORCEINLINE FArchive& operator<<(FArchive &Ar, FUDPData &Structure)
{
Ar << Structure.float1;
Ar << Structure.float2;
Ar << Structure.float3;
Ar << Structure.float4;
Ar << Structure.float5;
Ar << Structure.float6;
Ar << Structure.float7;
Ar << Structure.float8;
Ar << Structure.float9;
Ar << Structure.float10;
Ar << Structure.float11;
Ar << Structure.float12;
Ar << Structure.float13;
Ar << Structure.float14;
Ar << Structure.float15;
Ar << Structure.float16;
Ar << Structure.float17;
Ar << Structure.float18;
Ar << Structure.float19;
Ar << Structure.float20;
Ar << Structure.uint1;
Ar << Structure.uint2;
Ar << Structure.uint3;
return Ar;
}
43 changes: 43 additions & 0 deletions 5.0/UDPCommunication/Source/UDPCommunication/Classes/UDPReceiver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

#include "GameFramework/Actor.h"
#include "Networking.h"
#include "UDPData.h"
#include "UDPReceiver.generated.h"

UCLASS()
class UDPCOMMUNICATION_API AUDPReceiver : public AActor
{
GENERATED_UCLASS_BODY()
public:
FUDPData MyData;
bool UpdateInterest = true;
bool got_new_data = false;

public:
UFUNCTION(BlueprintImplementableEvent, Category = "UDPCommunication")
void BPEvent_DataReceived(const FUDPData& data);
public:

FSocket* ListenSocket;
FUdpSocketReceiver* Receiver = nullptr;
void Recv(const FArrayReaderPtr& ArrayReaderPtr, const FIPv4Endpoint& EndPt);
virtual void Archive(const FArrayReaderPtr & ArrayReaderPtr);
virtual void UpdateReceiverData(FUDPData data);

UFUNCTION(BlueprintCallable, Category = "UDPCommunication")
FUDPData GetData();

UFUNCTION(BlueprintCallable, Category = "UDPCommunication")
bool IsNewDataReady();

UFUNCTION(BlueprintCallable, Category = "UDPCommunication")
bool StartUDPReceiver(const FString& SocketName,
const int32 Port);

public:

/** Called whenever this actor is being removed from a level */
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;

};
36 changes: 36 additions & 0 deletions 5.0/UDPCommunication/Source/UDPCommunication/Classes/UDPSender.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include "Serialization/Archive.h"
#include "UDPData.h"
#include "Networking.h"
#include "GameFramework/Actor.h"
#include "UDPSender.generated.h"

UCLASS()
class UDPCOMMUNICATION_API AUDPSender : public AActor
{
GENERATED_UCLASS_BODY()

public:
TSharedPtr<FInternetAddr> RemoteAddr;
FSocket* SenderSocket;

//Tekitab saatja jaoks sokli pordi ja ip-ga
UFUNCTION(BlueprintCallable, Category = "UDPCommunication")
bool StartUDPSender(
const FString& SocketName,
const FString& TheIP,
const int32 ThePort
);

//Saadab numbrite jada läbi saatja ning serialiseerimise
UFUNCTION(BlueprintCallable, Category = "UDPCommunication")
bool UDPSendArray(FUDPData data);

public:

/** Called whenever this actor is being removed from a level */
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;

}
;
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright Epic Games, Inc. All Rights Reserved.

#include "UDPCommunication.h"

#define LOCTEXT_NAMESPACE "FUDPCommunicationModule"

void FUDPCommunicationModule::StartupModule()
{
// This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module
}

void FUDPCommunicationModule::ShutdownModule()
{
// This function may be called during shutdown to clean up your module. For modules that support dynamic reloading,
// we call this function before unloading the module.
}

#undef LOCTEXT_NAMESPACE

IMPLEMENT_MODULE(FUDPCommunicationModule, UDPCommunication)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include "CoreMinimal.h"
#include "Modules/ModuleManager.h"

class FUDPCommunicationModule : public IModuleInterface
{
public:

/** IModuleInterface implementation */
virtual void StartupModule() override;
virtual void ShutdownModule() override;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "UDPReceiver.h"

AUDPReceiver::AUDPReceiver(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
ListenSocket = NULL;
}

void AUDPReceiver::Recv(const FArrayReaderPtr & ArrayReaderPtr, const FIPv4Endpoint & EndPt)
{
if (!&ArrayReaderPtr) {
UE_LOG(LogTemp, Warning, TEXT("Cannot read array, nullptr returned."));
return;
}

got_new_data = true;

if (UpdateInterest)
Archive(ArrayReaderPtr);
}

bool AUDPReceiver::StartUDPReceiver(const FString & SocketName, const int32 Port)
{
FIPv4Endpoint Endpoint(FIPv4Address::Any, Port);
int32 BufferSize = 2 * 1024 * 1024;
ListenSocket = FUdpSocketBuilder(*SocketName).AsNonBlocking()
.AsReusable()
.BoundToEndpoint(Endpoint)
.WithReceiveBufferSize(BufferSize);

FTimespan ThreadWaitTime = FTimespan::FromMilliseconds(100);
Receiver = new FUdpSocketReceiver(ListenSocket, ThreadWaitTime, TEXT("UDP Receiver"));
Receiver->OnDataReceived().BindUObject(this, &AUDPReceiver::Recv);
Receiver->Start();
return true;
}

void AUDPReceiver::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
Super::EndPlay(EndPlayReason);

delete Receiver;
Receiver = nullptr;

//Clear all sockets
if (ListenSocket)
{
ListenSocket->Close();
ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->DestroySocket(ListenSocket);
}
}

void AUDPReceiver::Archive(const FArrayReaderPtr & ArrayReaderPtr) {

FUDPData data;
*ArrayReaderPtr << data;

//Class Parameter Update function to set object ready for a query from blueprint
UpdateReceiverData(data);
}

void AUDPReceiver::UpdateReceiverData(FUDPData data)
{
MyData = data;
}

FUDPData AUDPReceiver::GetData()
{
UpdateInterest = true; // Ready for new data
got_new_data = false;
return MyData;
}

bool AUDPReceiver::IsNewDataReady() {
return got_new_data;
}
70 changes: 70 additions & 0 deletions 5.0/UDPCommunication/Source/UDPCommunication/Public/UDPSender.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "UDPSender.h"

// Sets default values
AUDPSender::AUDPSender(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)

{
SenderSocket = NULL;
}

bool AUDPSender::StartUDPSender(const FString & SocketName, const FString & TheIP, const int32 ThePort)
{
//Create Remote Address
RemoteAddr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();

bool bIsValid;
RemoteAddr->SetIp(*TheIP, bIsValid);
RemoteAddr->SetPort(ThePort);

if (!bIsValid)
{
UE_LOG(LogTemp, Warning, TEXT("Wrong IP address supplied."));
return false;
}

SenderSocket = FUdpSocketBuilder(*SocketName)
.AsReusable()
.WithBroadcast()
;

//Set Send Buffer Size
int32 SendSize = 2 * 1024 * 1024;
SenderSocket->SetSendBufferSize(SendSize, SendSize);
SenderSocket->SetReceiveBufferSize(SendSize, SendSize);
SenderSocket->SetBroadcast(true);

return true;
}

bool AUDPSender::UDPSendArray(FUDPData data)
{
if (!SenderSocket)
{
UE_LOG(LogTemp, Log, TEXT("There is no socket."));
return false;
}
int32 BytesSent = 0;

FArrayWriter Writer;
Writer << data;
SenderSocket->SendTo(Writer.GetData(), Writer.Num(), BytesSent, *RemoteAddr);

if (BytesSent <= 0) {
UE_LOG(LogTemp, Error, TEXT("Socket exists, but receiver did not accept any packets."));
return false;
}

return true;
}

void AUDPSender::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
Super::EndPlay(EndPlayReason);

if (SenderSocket)
{
SenderSocket->Close();
ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->DestroySocket(SenderSocket);
}
}
Loading

0 comments on commit 8e902b9

Please sign in to comment.