Networking源码浅析

image-20240426221216657

定义

Bunch:表示一组相关的网络数据,包含 RPC / 属性同步 等信息,UEDS/Client 通信的基本单位。

PacketUDP 的基本传输单位,可以打包一个或多个 Bunch,在接受端进行解析。

NetDriver:管理网络通信,比如管理连接、处理数据包的发送和接收、处理网络事件。

NetConnection:表示一个网络连接,与一个 NetDriver 关联,并通过 NetDriver 进行实际的网络通信。

Channel:管理网络传输,进行数据分类,提供 SendBunchReceivedRawBunch 方法,由 NetConnection 管理,一个 NetConnection 可以包含多种 Channel

FObjectReplicator:处理对象的网络同步,保存属性快照,进行状态改变发送、更新同步状态、接受与发送消息。

ActorChannel:关联特定的 Actor 负责其同步,包含多个 FObjectReplicator 用于复制其 ActorActorComponent

FRepLayout:管理复制的属性,定义底层数据内存布局,数据比较、序列化与反序列化等。

RPC

Server RPC

Calling Machine Owning Connection Executing Machine
Server Client Server
Server Server Server
Server None Server
Client Invoking Client Server
Client Different Client Dropped
Client Server Dropped
Client None Dropped

Client RPC

Calling Machine Owning Connection Executing Machine
Server Owning Client Owning Client
Server Server Server
Server None Server
Client Invoking Client Invoking Client
Client Different Client Invoking Client
Client Server Invoking Client
Client None Invoking Client

NetMulticast RPC

Calling Machine Owning Connection Executing Machine
Server Client Server and all Clients the invoking actor is relevant for
Server Server Server and all Clients the invoking actor is relevant for
Server None Server and all Clients the invoking actor is relevant for
Client Invoking Client Invoking Client
Client Different Client Invoking Client
Client Server Invoking Client
Client None Invoking Client

Send RPC

flowchart TB

UObject::ProcessEvent
-- FunctionCallspace::Remote --> AActor::CallRemoteFunction
--> UNetDriver::ProcessRemoteFunction

ServerMulticast(bIsServerMulticast)

UNetDriver::ProcessRemoteFunction
--> ServerMulticast

ServerMulticast
--TRUE-->UNetDriver::GetFunctionRepLayout
--Actor IsNetRelevant || FUNC_NetReliable --> FRepLayout::BuildSharedSerializationForRPC
--> UNetDriver::InternalProcessRemoteFunctionPrivate
--> UNetDriver::ProcessRemoteFunctionForChannelPrivate

ServerMulticast
--FALSE--> UReplicationGraph::ProcessRemoteFunction
--> UNetDriver::ProcessRemoteFunctionForChannel
--> UNetDriver::ProcessRemoteFunctionForChannelPrivate
-- FRepLayout::SendPropertiesForRPC --> QueueBunch

QueueBunch(!bReliable & NetMulticast)

QueueBunch--TRUE-->UChannel::SendBunch
QueueBunch--FALSE-->UActorChannel::QueueRemoteFunctionBunch

Receive RPC

flowchart TB

UIpNetDriver::TickDispatch
--> UNetConnection::ReceivedRawPacket
--> UNetConnection::ReceivedPacket
--> UNetConnection::DispatchPacket
--> UChannel::ReceivedRawBunch
--> UChannel::ReceivedNextBunch
--> UChannel::ReceivedSequencedBunch
--> UActorChannel::ReceivedBunch
--> UActorChannel::ProcessBunch
--> FObjectReplicator::ReceivedBunch
-- FObjectReplicator::ReceivedRPC
--> UObject::ProcessEvent

属性同步

Replicate Diff

flowchart TB

UNetDriver::TickFlush
--> UNetDriver::ServerReplicateActors
--> UActorChannel::ReplicateActor
--> FObjectReplicator::ReplicateProperties
--> FNetSerializeCB::UpdateChangelistMgr
--> FRepLayout::UpdateChangelistMgr
--> FRepLayout::CompareProperties

Replicate Send

flowchart TB

UNetDriver::TickFlush
--> UNetDriver::ServerReplicateActors
--> UActorChannel::ReplicateActor
--> FObjectReplicator::ReplicateProperties
--> FRepLayout::ReplicateProiperties
--> FRepLayout::SendProperties
--> FProperty::NetSerializeItem
--> UChannel::SendBunch

Replicate Receive

flowchart TB

UIpNetDriver::TickDispatch
--> UNetConnection::ReceivedRawPacket
--> UNetConnection::ReceivedPacket
--> UNetConnection::DispatchPacket
--> UChannel::ReceivedRawBunch
--> UChannel::ReceivedNextBunch
--> UChannel::ReceivedSequencedBunch
--> UActorChannel::ReceivedBunch
--> UActorChannel::ProcessBunch
--> FObjectReplicator::ReceivedBunch
--> FRepLayout::ReceiveProperties
--> FRepLayout::ReceivePropertyHelper
--> FProperty::NetSerializeItem
--> FObjectReplicator::PostReceivedBunch
--> FObjectReplicator::CallRepNotifies