ue4 服务器登陆流程

389 篇文章 15 订阅
29 篇文章 0 订阅

http://blog.csdn.net/xiaozhi0999/article/details/51393704


ue4中,客户端登陆流程如下,调用UEngine::Browse方法,在这个方法中,判断如果是客户端,则创建UPendingNetGame实例,代码如下:

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. WorldContext.PendingNetGame = NewObject<UPendingNetGame>();  
  2. WorldContext.PendingNetGame->Initialize(URL);  
  3. WorldContext.PendingNetGame->InitNetDriver();  

而在InitNetDriver函数中,会向服务器发送 NMT_Hello 协议,我们转到NMT_Hello 的定义,如下:

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. DEFINE_CONTROL_CHANNEL_MESSAGE_TWOPARAM(Hello, 0, uint8, uint32); // initial client connection message  

参数1:大端还是小端
参数2:客户端版本号

客户端处理协议的地方是如下两个函数:

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. void UPendingNetGame::NotifyControlMessage(UNetConnection* Connection, uint8 MessageType, class FInBunch& Bunch)  
  2. void UWorld::NotifyControlMessage(UNetConnection* Connection, uint8 MessageType, class FInBunch& Bunch)  
服务器处理协议的地方是下面这个函数:

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. void UWorld::NotifyControlMessage(UNetConnection* Connection, uint8 MessageType, class FInBunch& Bunch)  

在服务器的协议处理函数,我们可以看到NMT_Hello协议的处理过程,先调用Receive接收消息,然后判断版本号是否一致,不一致发送NMT_Upgrade消息,一致则发送NMT_Challenge消息

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. DEFINE_CONTROL_CHANNEL_MESSAGE_ONEPARAM(Challenge, 3, FString); // server sends client challenge string to verify integrity  
参数1:服务器当前时间

客户端收到NMT_Challenge消息后,向服务器发送NMT_Login消息,NMT_Login消息携带三个参数,如下:

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. DEFINE_CONTROL_CHANNEL_MESSAGE_THREEPARAM(Login, 5, FString, FString, FUniqueNetIdRepl); // client requests to be admitted to the game  
参数1:客户端响应
参数2:客户端URL
参数3:客户端唯一ID

服务器收到NMT_Login消息,验证是否可以登录,失败返回NMT_Failure消息,成功则向服务器发送NMT_Welcome消息

NMT_Welcome消息定义如下: 

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. DEFINE_CONTROL_CHANNEL_MESSAGE_THREEPARAM(Welcome, 1, FString, FString, FString); // server tells client they're ok'ed to load the server's level  
参数1:服务器使用地图名

参数2:服务器使用GameMode名称

参数3:重定向URL

客户端收到NMT_Welcome消息,向服务器发送NMT_Netspeed消息,定义如下:

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. DEFINE_CONTROL_CHANNEL_MESSAGE_ONEPARAM(Netspeed, 4, int32); // client sends requested transfer rate  
参数1:当前网络传输速度

服务器收到NMT_Netspeed消息,只是简单记录一下客户端的NetSpeed即可

这里有个问题先要说明一下,

客户端何时加载地图?

在下面这个函数中:

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. void UEngine::TickWorldTravel(FWorldContext& Context, float DeltaSeconds)  
有如下这个判断:

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. else if( Context.PendingNetGame && Context.PendingNetGame->bSuccessfullyConnected && !Context.PendingNetGame->bSentJoinRequest )  

当bSuccessfullyConnected为true而bSentJoinRequest为false时,客户端会LoadMap,而bSuccessfullyConnected是在客户端处理完NMT_Welcome消息后设置为true的,也就是说客户端在处理完NMT_Welcome消息,下一次执行TickWorldTravel函数的时候,就会加载地图,而bSentJoinRequest变量,是为了防止地图被加载多次而设置的。

当客户端调用完LoadMap,会向服务器发送NMT_Join消息,

在服务器处理NMT_Join消息时,代码如下:

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. Connection->PlayerController = SpawnPlayActor( Connection, ROLE_AutonomousProxy, InURL, Connection->PlayerId, ErrorMsg );  

然后SpawnPlayActor中会调用GameMode的Login,而在Login内部,会调用如下代码,创建PlayerController

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. APlayerController* NewPlayerController = SpawnPlayerController(RemoteRole, FVector::ZeroVector, FRotator::ZeroRotator);  

在PlayerController的初始化函数中,如果是服务器,则创建PlayerState,用于服务器和客户端Player信息的同步,具体函数如下:

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. void APlayerController::PostInitializeComponents()  


  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值