29 UE5 UPlayer玩家管理和网络介绍

UPlayer

  • 继承UObject和FExec(支持控制台命令)
  • UPlayer和PlayerController关联
  • 可以具有属性、函数和事件

ULocalPlayer

  • 本地玩家(专用服务器不存在, 只在客户端或监听服务器)
  • 会在GameInstance里保存有LocalPlayer列表。
  • 比UPlayer多了Viewport相关
  • LocalPlayer最终和PlayerState对应起来。
  • 网络联机时,其他玩家的PlayerState通过Replicated过来。
  • APlayerController::SetPlayer( UPlayer* InPlayer ) 重点函数
  • 设置InPlayer->PlayerController = this 和 初始化InitInputSystem();
image.png

UNetConnection

  • 代表连接到网络游戏的一个客户端
  • 有自己的一组Channel
  • 包含建立连接的逻辑(断线重连)
  • 从网络接收的数据包,并将数据包传递给适当的Channel

UNetDriver

  • 管理UNetConnections,并在UNetConnection之间共享数据。
  • UNetDriver::TickDispatch负责接收网络数据
  • RPC处理: ProcessRemoteFunction() , ProcessRemoteFunctionForChannel()
  • TickDispatch中接收网络数据
  • 通过UNetConnection::ReceivedRawPacket传递到对应连接
  • Server端NetDriver管理多个NetConnections
  • Client端NetDriver管理一个NetConnection
image.png

UNetConnection的子类

  • UIpConnection: 管理基于IP协议的网络连接和通信。
  • UChildConnection: 表示二级分屏连接.
  • UDemoNetConnection: 模拟网络连接记录和播放游戏会话。

UChannel(被UNetConnection管理)

  • 通道的基类
  • UControlChannel 用于发送有关连接状态的信息(连接是否应该关闭等)。
  • UVoiceChannel 可用于在客户端和服务器之间发送语音数据。
  • UActorChannel 从服务器复制到客户端的每个Actor都有一个唯一Actor通道。
  • ActorChannel 同步游戏中角色数据和进行RPC.

Bunch和Packet

  • Packet 是服务器和客户端的NetConnections之间发送
  • Bunch 是服务器和客户端的通道之间发送
  • Packet 包含0~N个Bunch
  • Bunch太大会被拆分

RPC介绍 客户端调用Server_RPC

  • 客户端调用Server_RPC。
  • 请求被转发(通过NetDriver和NetConnection)到拥有调用RPC的Actor的UActorChannel 。
  • Actor通道将RPC标识符和参数序列化成Bunch。Bunch也包含它的UActorChannel ID。
  • UActorChannel 将请求NetConnection发送Bunch。
  • 稍后,NetConnection将把这个(和其他)数据组装成一个Packet ,发送给服务器。
  • 在服务器端,Packet 数据包将被NetDriver接收。
  • NetDriver将检查发送数据包的地址,并将数据包移交给适当的NetConnection。
  • NetConnection将把数据包分解成Bunch(一个接一个)。
  • NetConnection将使用Channel ID将Bunch路由到相应的UActorChannel 。
  • ActorChannel将Bunch拆开,看里面的RPC数据,使用RPC ID和序列化的参数
  • 在对应的Actor上调用相应的函数。
image.png

总结

  • NetConnections和NetDrivers通常与所使用的底层通信方法/技术无关。
  • 类如UIpConnection / UIpNetDriver或UWebSocketConnection / UWebSocketNetDriver
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容