UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
DataChannel.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3/*=============================================================================
4 DataChannel.h: Unreal datachannel class.
5=============================================================================*/
6
7#pragma once
8
9#include "CoreMinimal.h"
10#include "Misc/Guid.h"
11#include "Misc/NetworkGuid.h"
12#include "Misc/NetworkVersion.h"
15#include "Net/NetIDVariant.h"
16#include "Net/DataBunch.h"
18
19// Forward declarations
20enum class ENetPingControlMessage : uint8;
21
22namespace UE::Net
23{
24template<int32 Index, typename... ParamTypes>
25class TNetControlMessageImpl;
26}
27
28/*-----------------------------------------------------------------------------
29 UControlChannel base class.
30-----------------------------------------------------------------------------*/
31
41template<uint8 MessageType> class FNetControlMessage
42{
43};
46{
47public:
48 static inline const TCHAR* GetName(uint8 MessageIndex)
49 {
50 CheckInitialized();
51 return Names[MessageIndex];
52 }
53 static inline bool IsRegistered(uint8 MessageIndex)
54 {
55 CheckInitialized();
56 return Names[MessageIndex][0] != 0;
57 }
58 template<int32 Index, typename... ParamTypes> friend class UE::Net::TNetControlMessageImpl;
59private:
60 static void CheckInitialized()
61 {
62 if (!bInitialized)
63 {
64 for (int32 i = 0; i < UE_ARRAY_COUNT(Names); i++)
65 {
66 Names[i] = TEXT("");
67 }
68 bInitialized = true;
69 }
70 }
71 static void SetName(uint8 MessageType, const TCHAR* InName)
72 {
73 CheckInitialized();
74 Names[MessageType] = InName;
75 }
76
77 template<typename... ParamTypes>
78 static void SendParams(FControlChannelOutBunch& Bunch, ParamTypes&... Params) {}
79
80 template<typename FirstParamType, typename... ParamTypes>
81 static void SendParams(FControlChannelOutBunch& Bunch, FirstParamType& FirstParam, ParamTypes&... Params)
82 {
83 Bunch << FirstParam;
84 SendParams(Bunch, Params...);
85 }
86
87 template<typename... ParamTypes>
88 static void ReceiveParams(FInBunch& Bunch, ParamTypes&... Params) {}
89
90 template<typename FirstParamType, typename... ParamTypes>
91 static void ReceiveParams(FInBunch& Bunch, FirstParamType& FirstParam, ParamTypes&... Params)
92 {
93 Bunch << FirstParam;
94 ReceiveParams(Bunch, Params...);
95 }
96
97 static constexpr int MaxNames = TNumericLimits<uint8>::Max() + 1;
98 static ENGINE_API const TCHAR* Names[MaxNames];
99 static ENGINE_API bool bInitialized;
100};
101
102namespace UE::Net
103{
104
105template<int32 Index, typename... ParamTypes>
107{
108public:
109 static uint8 Initialize(const TCHAR* InName)
110 {
111 FNetControlMessageInfo::SetName(Index, InName);
112 return 0;
113 }
114
119 static void Send(UNetConnection* Conn, ParamTypes&... Params)
120 {
121 static_assert(Index < FNetControlMessageInfo::MaxNames, "Control channel message must be a byte.");
122 checkSlow(!Conn->IsA(UChildConnection::StaticClass()));
123 if (Conn->Channels[0] != nullptr && !Conn->Channels[0]->Closing)
124 {
125 FControlChannelOutBunch Bunch(Conn->Channels[0], false);
126 uint8 MessageType = Index;
127 Bunch << MessageType;
128 FNetControlMessageInfo::SendParams(Bunch, Params...);
129 Conn->Channels[0]->SendBunch(&Bunch, true);
130 }
131 }
132
134 [[nodiscard]] static bool Receive(FInBunch& Bunch, ParamTypes&... Params)
135 {
136 FNetControlMessageInfo::ReceiveParams(Bunch, Params...);
137 return !Bunch.IsError();
138 }
139
141 static void Discard(FInBunch& Bunch)
142 {
143 TTuple<ParamTypes...> Params;
144 VisitTupleElements([&Bunch](auto& Param)
145 {
146 Bunch << Param;
147 },
148 Params);
149 }
150};
151
152}
153
154#define DEFINE_CONTROL_CHANNEL_MESSAGE(Name, Index, ...) \
155enum { NMT_##Name = Index }; \
156template<> class FNetControlMessage<Index> : public UE::Net::TNetControlMessageImpl<Index, ##__VA_ARGS__> \
157{ \
158};
159
160#define DEFINE_CONTROL_CHANNEL_MESSAGE_ZEROPARAM(Name, Index) DEFINE_CONTROL_CHANNEL_MESSAGE(Name, Index)
161#define DEFINE_CONTROL_CHANNEL_MESSAGE_ONEPARAM(Name, Index, TypeA) DEFINE_CONTROL_CHANNEL_MESSAGE(Name, Index, TypeA)
162#define DEFINE_CONTROL_CHANNEL_MESSAGE_TWOPARAM(Name, Index, TypeA, TypeB) DEFINE_CONTROL_CHANNEL_MESSAGE(Name, Index, TypeA, TypeB)
163#define DEFINE_CONTROL_CHANNEL_MESSAGE_THREEPARAM(Name, Index, TypeA, TypeB, TypeC) DEFINE_CONTROL_CHANNEL_MESSAGE(Name, Index, TypeA, TypeB, TypeC)
164#define DEFINE_CONTROL_CHANNEL_MESSAGE_FOURPARAM(Name, Index, TypeA, TypeB, TypeC, TypeD) DEFINE_CONTROL_CHANNEL_MESSAGE(Name, Index, TypeA, TypeB, TypeC, TypeD)
165#define DEFINE_CONTROL_CHANNEL_MESSAGE_FIVEPARAM(Name, Index, TypeA, TypeB, TypeC, TypeD, TypeE) DEFINE_CONTROL_CHANNEL_MESSAGE(Name, Index, TypeA, TypeB, TypeC, TypeD, TypeE)
166#define DEFINE_CONTROL_CHANNEL_MESSAGE_SIXPARAM(Name, Index, TypeA, TypeB, TypeC, TypeD, TypeE, TypeF) DEFINE_CONTROL_CHANNEL_MESSAGE(Name, Index, TypeA, TypeB, TypeC, TypeD, TypeE, TypeF)
167#define DEFINE_CONTROL_CHANNEL_MESSAGE_SEVENPARAM(Name, Index, TypeA, TypeB, TypeC, TypeD, TypeE, TypeF, TypeG) DEFINE_CONTROL_CHANNEL_MESSAGE(Name, Index, TypeA, TypeB, TypeC, TypeD, TypeE, TypeF, TypeG)
168#define DEFINE_CONTROL_CHANNEL_MESSAGE_EIGHTPARAM(Name, Index, TypeA, TypeB, TypeC, TypeD, TypeE, TypeF, TypeG, TypeH) DEFINE_CONTROL_CHANNEL_MESSAGE(Name, Index, TypeA, TypeB, TypeC, TypeD, TypeE, TypeF, TypeG, TypeH)
169
170#define IMPLEMENT_CONTROL_CHANNEL_MESSAGE(Name) static uint8 Dummy##_FNetControlMessage_##Name = FNetControlMessage<NMT_##Name>::Initialize(TEXT(#Name));
171
172// message type definitions
173DEFINE_CONTROL_CHANNEL_MESSAGE(Hello, 0, uint8, uint32, FString, EEngineNetworkRuntimeFeatures); // initial client connection message
174DEFINE_CONTROL_CHANNEL_MESSAGE(Welcome, 1, FString, FString, FString); // server tells client they're ok'ed to load the server's level
175DEFINE_CONTROL_CHANNEL_MESSAGE(Upgrade, 2, uint32, EEngineNetworkRuntimeFeatures); // server tells client their version is incompatible
176DEFINE_CONTROL_CHANNEL_MESSAGE(Challenge, 3, FString); // server sends client challenge string to verify integrity
177DEFINE_CONTROL_CHANNEL_MESSAGE(Netspeed, 4, int32); // client sends requested transfer rate
178DEFINE_CONTROL_CHANNEL_MESSAGE(Login, 5, FString, FString, FUniqueNetIdRepl, FString); // client requests to be admitted to the game
179DEFINE_CONTROL_CHANNEL_MESSAGE(Failure, 6, FString); // indicates connection failure
180DEFINE_CONTROL_CHANNEL_MESSAGE(Join, 9); // final join request (spawns PlayerController)
181DEFINE_CONTROL_CHANNEL_MESSAGE(JoinSplit, 10, FString, FUniqueNetIdRepl); // child player (splitscreen) join request
182DEFINE_CONTROL_CHANNEL_MESSAGE(Skip, 12, FGuid); // client request to skip an optional package
183DEFINE_CONTROL_CHANNEL_MESSAGE(Abort, 13, FGuid); // client informs server that it aborted a not-yet-verified package due to an UNLOAD request
184DEFINE_CONTROL_CHANNEL_MESSAGE(PCSwap, 15, int32); // client tells server it has completed a swap of its Connection->Actor
185DEFINE_CONTROL_CHANNEL_MESSAGE(ActorChannelFailure, 16, int32); // client tells server that it failed to open an Actor channel sent by the server (e.g. couldn't serialize Actor archetype)
186DEFINE_CONTROL_CHANNEL_MESSAGE(DebugText, 17, FString); // debug text sent to all clients or to server
187DEFINE_CONTROL_CHANNEL_MESSAGE(NetGUIDAssign, 18, FNetworkGUID, FString); // Explicit NetworkGUID assignment. This is rare and only happens if a netguid is only serialized client->server (this msg goes server->client to tell client what ID to use in that case)
188DEFINE_CONTROL_CHANNEL_MESSAGE(SecurityViolation, 19, FString); // server tells client that it has violated security and has been disconnected
189DEFINE_CONTROL_CHANNEL_MESSAGE(GameSpecific, 20, uint8, FString); // custom game-specific message routed to UGameInstance for processing
192DEFINE_CONTROL_CHANNEL_MESSAGE(CloseReason, 23, FString); // Reason for client NetConnection Close, for analytics/logging
193DEFINE_CONTROL_CHANNEL_MESSAGE(NetPing, 24, ENetPingControlMessage /* MessageType */, FString /* MessageStr */);
194
195// Beacon control channel flow
196// Client Server
197// Send<Hello>
198// Receive<Hello> - compare version / game id
199// Send<Upgrade> if incompatible
200// Send<Failure> if wrong game
201// Send<BeaconWelcome> if good so far
202// Receive<BeaconWelcome>
203// Send<NetSpeed>
204// Send<BeaconJoin> with beacon type
205// Receive<Netspeed>
206// Receive<BeaconJoin> - create requested beacon type and create NetGUID
207// Send<Failure> if unable to create or bad type
208// Send<BeaconAssignGUID> with NetGUID for new beacon actor
209// Receive<BeaconAssignGUID> - assign NetGUID to client actor
210// Send<BeaconNetGUIDAck> acknowledging receipt of NetGUID
211// Receive<BeaconNetGUIDAck> - connection complete
212
213DEFINE_CONTROL_CHANNEL_MESSAGE(BeaconWelcome, 25); // server tells client they're ok to attempt to join (client sends netspeed/beacontype)
214DEFINE_CONTROL_CHANNEL_MESSAGE(BeaconJoin, 26, FString, FUniqueNetIdRepl); // server tries to create beacon type requested by client, sends NetGUID for actor sync
215DEFINE_CONTROL_CHANNEL_MESSAGE(BeaconAssignGUID, 27, UE::Net::FNetIDVariant); // client assigns NetGUID from server to beacon actor, sends NetGUIDAck
216DEFINE_CONTROL_CHANNEL_MESSAGE(BeaconNetGUIDAck, 28, FString); // server received NetGUIDAck from client, connection established successfully
217
218DEFINE_CONTROL_CHANNEL_MESSAGE(IrisProtocolMismatch, 29, uint64); // client has a different protocol hash from the server
219DEFINE_CONTROL_CHANNEL_MESSAGE(IrisNetRefHandleError, 30, UE::Net::ENetRefHandleError, uint64, TArray<uint64>); // a specific handle caused an error and we want the remote connection to log all information it has on it
220
221// Called in place of Join or JoinSplit during connection handshake, and will created a connection that has the following properties:
222// * Won't spawn an instance of APawn.
223// * Won't call AGameModeBase::Login().
224// * Spawns ANoPawnPlayerController instead of using the class in AGameMode::PlayerControllerClass.
225// * Network relevency will be based on the location of ANoPawnPlayerController.
226//
227// Relevancy: Since network relevancy will be based on the location of ANoPawnPlayerController, it's important to set the location of
228// the actor using the RPC ANoPawnPlayerController::SetViewTargetPosition(), either from the server or client using this connection.
229//
230// These messages are only intended to be used when called from one game server to another game server. For this reason it is disabled
231// by default and must be explicitly enabled on the receiving server using the cvar net.EnableSupportForNoPawnConnection or calling
232// UNetDriver::SetSupportForNoPawnConnection() directly.
235
236// Close a child connection remotely without closing all other child and parent connections.
237//
238// The argument to this message is the value of NetPlayerIndex associated with the child connection that must be closed.
239//
240// It is expected that the local child connection will be cleaned up manually and does not depend on this message being
241// successfully processed by the remote server.
242DEFINE_CONTROL_CHANNEL_MESSAGE(CloseChildConnection, 34, int32);
#define checkSlow(expr)
Definition AssertionMacros.h:332
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define DEFINE_CONTROL_CHANNEL_MESSAGE(Name, Index,...)
Definition DataChannel.h:154
ENetPingControlMessage
Definition NetPing.h:90
EEngineNetworkRuntimeFeatures
Definition NetworkVersion.h:45
UE_FORCEINLINE_HINT void VisitTupleElements(FuncType &&Func, FirstTupleType &&FirstTuple, TupleTypes &&... Tuples)
Definition Tuple.h:878
#define UE_ARRAY_COUNT(array)
Definition UnrealTemplate.h:212
uint8_t uint8
Definition binka_ue_file_header.h:8
uint32_t uint32
Definition binka_ue_file_header.h:6
UE_FORCEINLINE_HINT bool IsError() const
Definition Archive.h:362
Definition DataBunch.h:127
Definition DataChannel.h:46
static const TCHAR * GetName(uint8 MessageIndex)
Definition DataChannel.h:48
static bool IsRegistered(uint8 MessageIndex)
Definition DataChannel.h:53
Definition DataChannel.h:42
Definition NetworkGuid.h:12
Definition Array.h:670
Definition NetIDVariant.h:20
Definition DataChannel.h:107
static void Send(UNetConnection *Conn, ParamTypes &... Params)
Definition DataChannel.h:119
static bool Receive(FInBunch &Bunch, ParamTypes &... Params)
Definition DataChannel.h:134
static uint8 Initialize(const TCHAR *InName)
Definition DataChannel.h:109
static void Discard(FInBunch &Bunch)
Definition DataChannel.h:141
Definition NetConnection.h:284
FUniformParams Params
Definition MeshPaintVirtualTexture.cpp:162
Definition NetworkVersion.cpp:28
ENetRefHandleError
Definition NetEnums.h:130
U16 Index
Definition radfft.cpp:71
Definition DataBunch.h:202
Definition Guid.h:109
Definition OnlineReplStructs.h:26
Definition NumericLimits.h:41
Definition Tuple.h:652