最近跟进的一个汽车相关的项目。UE4开发的HMI原型软件,需UDP通信接口打通,之前没碰过UE4,特记录一下实施过程。
1、下载安装ue4。下载前注册Epic Games账号(此账号可以用来购买虚幻商城的产品,有素材有游戏。素材有不少免费的,可以按需下载)
2、在HMI软件中添加BlueprintFunctionLibrary类。
3、在**.Build.cs 文件中添加依赖模型“Sockets”, “Networking”,代码示例如下:
// Fill out your copyright notice in the Description page of Project Settings.
using UnrealBuildTool;
public class Cluster : ModuleRules
{
public Cluster(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "Sockets", "NetWorking" });
PrivateDependencyModuleNames.AddRange(new string[] { });
// Uncomment if you are using Slate UI
// PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
// Uncomment if you are using online features
// PrivateDependencyModuleNames.Add("OnlineSubsystem");
// To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
}
}
4、UDPSocketServer.cpp代码如下:
// Fill out your copyright notice in the Description page of Project Settings.
#include "UDPSocketServer.h"
TSharedPtr<FInternetAddr> RemoteAddr;
FSocket* SenderSocket;
FSocket* ListenSocket;
FUdpSocketReceiver* UDPReceiver = nullptr;
void ScreenMsg(const FString& Msg)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, *Msg);
}
void ScreenMsg(const FString& Msg,const float Value)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("%s %f"), *Msg, Value));
}
void ScreenMsg(const FString& Msg, const FString& Msg2)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("%s %s"), *Msg, *Msg2));
}
void UUDPSocketServer::CloseUDPSender()
{
if (SenderSocket) //Clear all sockets!
{
SenderSocket->Close();
ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->DestroySocket(SenderSocket);
}
}
bool UUDPSocketServer::RamaUDPSenderSendString(FString ToSend) //发送消息处理
{
if (!SenderSocket)
{
ScreenMsg("No sender socket");
return false;
}
//~~~~~~~~~~~~~~~~
//发送消息
int32 BytesSent = 0;
FString serialized = ToSend;
TCHAR* serializedChar = serialized.GetCharArray().GetData();
int32 size = FCString::Strlen(serializedChar);
int32 sent = 0;
//SenderSocket->SendTo(Writer.GetData(), Writer.Num(), BytesSent, *RemoteAddr);
SenderSocket->SendTo((uint8*)TCHAR_TO_UTF8(serializedChar), size, BytesSent, *RemoteAddr);//发送给远端地址
if (BytesSent <= 0)
{
const FString Str = "Socket is valid but the receiver received 0 bytes, make sure it is listening properly!";
UE_LOG(LogTemp, Error, TEXT("%s"), *Str);
ScreenMsg(Str);
return false;
}
ScreenMsg("UDP Send Succcess! INFO Sent = ", ToSend);
return true;
}
void UUDPSocketServer::DataRecv(FString& str, bool& success)
{
if (!ListenSocket)
{
ScreenMsg("No sender socket");
success = false;
//return success;
}
TSharedRef<FInternetAddr> targetAddr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
TArray<uint8> ReceivedData;//定义一个接收器
uint32 Size;
if (ListenSocket->HasPendingData(Size))
{
success = true;
str = "";
uint8* Recv = new uint8[Size];
int32 BytesRead = 0;
ReceivedData.SetNumUninitialized(FMath::Min(Size, 65507u));
ListenSocket->RecvFrom(ReceivedData.GetData(), ReceivedData.Num(), BytesRead, *targetAddr);//创建远程接收地址
char ansiiData[1024];
memcpy(ansiiData, ReceivedData.GetData(), BytesRead);//拷贝数据到接收器
ansiiData[BytesRead] = 0; //判断数据结束
FString debugData = ANSI_TO_TCHAR(ansiiData); //字符串转换
str = debugData;
// memset(ansiiData,0,1024);//清空
ScreenMsg(str);
}
else
{
success = false;
}
}
bool UUDPSocketServer::StartUDPSender(const FString& YourChosenSocketName, const FString& TheIP, const int32 ThePort)///初始化远端IP 发送信息前
{
//FIPv4Endpoint Endpoint(FIPv4Address::Any, 6789);
//Create Remote Address.
RemoteAddr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
bool bIsValid;
RemoteAddr->SetIp(*TheIP, bIsValid);
RemoteAddr->SetPort(ThePort);
if (!bIsValid)
{
ScreenMsg("Rama UDP Sender>> IP address was not valid!", TheIP);
return false;
}
SenderSocket = FUdpSocketBuilder(*YourChosenSocketName)
.AsReusable()
.WithBroadcast() // 广播
.WithSendBufferSize(2 * 1024 * 1024)
//.BoundToEndpoint(Endpoint)
;
//check(SenderSocket->GetSocketType() == SOCKTYPE_Datagram);
//Set Send Buffer Size
int32 SendSize = 2 * 1024 * 1024;
SenderSocket->SetSendBufferSize(SendSize, SendSize);
SenderSocket->SetReceiveBufferSize(SendSize, SendSize);
if (bIsValid)
{
bIsValid = true;
}
return bIsValid;
}
void UUDPSocketServer::CloseUDPReceiver()
{
delete UDPReceiver;
UDPReceiver = nullptr;
//Clear all sockets!
// makes sure repeat plays in Editor dont hold on to old sockets!
if (ListenSocket)
{
ListenSocket->Close();
ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->DestroySocket(ListenSocket);
}
}
void UUDPSocketServer::StartUDPReceiver(const FString& YourChosenSocketName, const FString& TheIP, const int32 ThePort, bool& success)
{
TSharedRef<FInternetAddr> targetAddr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
FIPv4Address Addr;
FIPv4Address::Parse(TheIP, Addr);
//Create Socket
FIPv4Endpoint Endpoint(FIPv4Address::Any, ThePort); //所有ip地址本地
//FIPv4Endpoint Endpoint(Addr, ThePort); //指定ip地址
ListenSocket = FUdpSocketBuilder(*YourChosenSocketName)
.AsNonBlocking()
.AsReusable()
.BoundToEndpoint(Endpoint)
.WithReceiveBufferSize(2 * 1024 * 1024)
;
//BUFFER SIZE
int32 BufferSize = 2 * 1024 * 1024;
ListenSocket->SetSendBufferSize(BufferSize, BufferSize);
ListenSocket->SetReceiveBufferSize(BufferSize, BufferSize);
if (!ListenSocket)
{
ScreenMsg("No socket");
success = false;
}
if (ListenSocket)
{
ScreenMsg("The receiver is initialized");
success = true;
}
//return true;
}
5、UDPSocketServer.h代码如下:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "Runtime/Networking/Public/Networking.h"
#include "Engine.h"
#include "UDPSocketServer.generated.h"
/**
*
*/
UCLASS()
class UDPCLUSTER0505_API UUDPSocketServer : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, Category = "UDP")
static void StartUDPReceiver(const FString& YourChosenSocketName, const FString& TheIp, const int32 ThePort, bool& success);
UFUNCTION(BlueprintCallable, Category = "UDP")
static bool StartUDPSender(const FString& YourChosenSocketName, const FString& TheIp, const int32 ThePort);
UFUNCTION(BlueprintCallable, Category = "UDP")
static void CloseUDPReceiver();
UFUNCTION(BlueprintCallable, Category = "UDP")
static void CloseUDPSender();
UFUNCTION(BlueprintCallable, Category = "UDP")
static bool RamaUDPSenderSendString(FString ToSend);
UFUNCTION(BlueprintCallable, Category = "UDP")
static void DataRecv(FString& str, bool& success);
};
6、蓝图调用如下:
7、搞定收工!