最近使用raknet 发送流媒体数据。发现在采用RELIABLE_ORDERED模式下,仍然有丢包发生,特别是在网络不好的情况下,测试了几天。猜测是由于底层拆包造成的。同事搜索这个问题,发现这个帖子
http://blog.youkuaiyun.com/ycf8788/article/details/52817902
果然是个坑。
最简单的方法是上层拆小包发送。那么mtu选多大呢?
raknet提供了一个接口可以获取一个连接的mtu。接口如下
/// \brief Returns the current MTU size
/// \param[in] target Which system to get MTU for. UNASSIGNED_SYSTEM_ADDRESS to get the default
/// \return The current MTU size of the target system.
int GetMTUSize( const SystemAddress target ) const;
这个是连接层的最大值,一般是1500-8=1492. 8个字节不知道是啥。然后
ip 20字节
udp 8 字节
raknet 的一个数据结构DatagramHeaderFormat 9个字节
raknet 的消息头 23个字节
一共需要消耗掉60个字节 1492-60 = 1