unreal4 网络代码分析
来源:互联网 发布:js中layout什么意思 编辑:程序博客网 时间:2024/06/07 04:08
ue4实现了rpc,还是比较好用的,下面简单分析一下ue4网络部分的实现。
ue4中主要处理网络的类是UNetDriver, UNetDriver类中有如下两个成员:
- class UNetConnection* ServerConnection;
- TArray<class UNetConnection*> ClientConnections;
从名字就能看出ServerConnection代表了客户端向服务器建立的连接,而ClientConnections是服务器持有的客户端的连接。
ue4是通过UActorChannel来实现Actor在服务器和客户端之间同步的,其中每个需要在服务器和客户端之间同步的actor都会关联一个UActorChannel,我们看UNetConnection的代码可以验证这一点
- TMap<tweakobjectptr,class UActorChannel*> ActorChannels;
看UActorChannel的实现,有如下几个重要的成员函数
发送数据:
- virtual FPacketIdRange SendBunch(FOutBunch* Bunch, bool Merge);
- int32 SendRawBunch(FOutBunch* Bunch, bool Merge);
接收数据:
- virtual void ReceivedBunch( FInBunch& Bunch ) PURE_VIRTUAL(UChannel::ReceivedBunch,);
- bool ReceivedNextBunch( FInBunch & Bunch, bool & bOutSkipAck );
- void ReceivedRawBunch( FInBunch & Bunch, bool & bOutSkipAck );
- bool ReceivedSequencedBunch( FInBunch& Bunch );
拿发送数据来说,UActorChannel中SendRawBunch实现还是调用UNetConnection中相应的函数,而在UNetConnection中,有如下成员变量
- FBitWriter SendBuffer;
ps:以上讨论为了简化,忽略了类的层次(比如:UIpConnection从UNetConnection派生,所以有些函数具体实现是在UIpConnection中)
下面我们来具体分析一下rpc过程
当调用一个远端函数的时候,主要是在UNetDriver的InternalProcessRemoteFunction函数中处理的,先根据调用Actor来查找对应的UActorChannel,代码如下:
- UActorChannel* Ch = Connection->ActorChannels.FindRef(Actor);
如果没有找到,则需要创建一个UActorChannel,如下:
- Ch = (UActorChannel *)Connection->CreateChannel( CHTYPE_Actor, 1 );
注意, 对端主要是通过ChIndex来区分不同的Actor,所以需要为每个UActorChannel分配不同的ChIndex,这个过程是在CreateChannel中实现的,当不传递第三个参数的时候,如上边代码那样,会找到第一个可用的ChIndex,如下代码所示:
- // Search the channel array for an available location
- for( ChIndex=FirstChannel; ChIndex<max_channels; chindex++ ) < span="" style="word-wrap: break-word;"></max_channels; chindex++ ) <>
- {
- if( !Channels[ChIndex] )
- {
- break;
- }
- }
- unreal4 网络代码分析
- Unreal4 基础代码Link错误
- Unreal4
- unreal4
- Unreal4
- ue4 网络代码分析
- ue4 网络代码分析
- UE4网络代码分析
- Unreal4 入门
- Unreal4 Architecture
- Linux网络层 ip_rcv()函数代码分析
- linux内核网络栈代码分析
- linux内核网络栈代码分析
- linux内核网络栈代码分析
- Linux网络层 ip_rcv()函数代码分析
- Linux内核网络协议栈代码分析
- Linux内核网络栈代码分析
- Linux内核网络协议栈代码分析
- Android Studio生成APK,追加版本号区分
- 多线程之Thread.interrupt()方法使用
- Python中装饰器浅析
- noip式mengbier (noip 2016)
- React组件的生命周期
- unreal4 网络代码分析
- python的引用、浅拷贝与深拷贝
- iOS 获取网络图片的宽高
- libevent 入门教程
- 计算机网络 时延、发送时延、传输时延、处理时延、排队时延、时延带宽积
- Builder(建造者模式)
- 线上应用故障排查之二:高内存占用
- UE4自定义MovementComponent组件
- linux忘记mysql密码