1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
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
}
}
//UDPSend.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Runtime/Networking/Public/Networking.h"
//#include <iostream>
//#include <list>
//#include <numeric>
//#include <algorithm>
#include "UDPSend.generated.h"
UCLASS()
class UDPTEST426_API AUDPSend : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AUDPSend();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
//新建isudp的bool变量,表示用UDP进行通信
bool IsUDP;
//新建函数RamaUDPSender_SendString(),用于发送消息
UFUNCTION(BlueprintCallable, Category = "UDP")
bool RamaUDPSender_SendString(FString ToSend);
public:
TSharedPtr<FInternetAddr> RemoteAddr;
FSocket* SenderSocket;
// SocketName,IP,Port,IsUdp
UFUNCTION(BlueprintCallable, Category = "UDP")
bool StartUDPSender(const FString& YourChosenSocketName, const FString& TheIP, const int32 ThePort, bool UDP);
//此函数用于在虚幻C++内获取本地IP,转化为String类型
UFUNCTION(BlueprintPure, Category = "UDP")
FString GetIP();
UFUNCTION(BlueprintCallable, Category = "UDP")
bool SendGameTime(FString ToSend);
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "UDP")
bool ShowOnScreenDebugMessages;
FORCEINLINE void ScreenMsg(const FString& Msg)
{
if (!ShowOnScreenDebugMessages) return;
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, *Msg);
}
FORCEINLINE void ScreenMsg(const FString& Msg, const float Value)
{
if (!ShowOnScreenDebugMessages) return;
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("%s %f"), *Msg, Value));
}
FORCEINLINE void ScreenMsg(const FString& Msg, const FString& Msg2)
{
if (!ShowOnScreenDebugMessages) return;
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("%s %s"), *Msg, *Msg2));
}
public:
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
};
//UDPSend.cpp
#include "UDPSend.h"
AUDPSend::AUDPSend()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
SenderSocket = NULL;
ShowOnScreenDebugMessages = true;
}
// Called when the game starts or when spawned
void AUDPSend::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AUDPSend::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
bool AUDPSend::StartUDPSender(const FString& YourChosenSocketName, const FString& TheIP, const int32 ThePort, bool UDP)
{
RemoteAddr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
bool bIsValid;
RemoteAddr->SetIp(*TheIP, bIsValid);
RemoteAddr->SetBroadcastAddress();
RemoteAddr->SetPort(ThePort);
if (!bIsValid)
{
return false;
}
SenderSocket = FUdpSocketBuilder(*YourChosenSocketName)
.AsReusable()//使绑定的地址可以被其他套接字重用
.WithBroadcast()
.WithSendBufferSize(2 * 1024 * 1024)
/*.BoundToEndpoint(Endpoint)*/
;
check(SenderSocket->GetSocketType() == SOCKTYPE_Datagram);
int32 SendSize = 2 * 1024 * 1024;
SenderSocket->SetSendBufferSize(SendSize, SendSize);
SenderSocket->SetReceiveBufferSize(SendSize, SendSize);
if (bIsValid)
{
bIsValid = true;
}
return bIsValid;
}
bool AUDPSend::RamaUDPSender_SendString(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;
|