用RakNet发送数据包的最佳方法

用RakNet发送数据包的最佳方法

好的,我只是做了一个错误,在我第一次尝试理解源代码的时候没有正确地遵循条件声明,因为这个问题是非常具体的,所以我认为在源代码中查找是很好的。

简单的答案是是的,将RakNetGUID存储在Player类中

详情如下:

好的,首先,有关的文件仅为RakPeer.cpp。起点是:

代码语言:javascript运行复制uint32_t RakPeer::Send( const RakNet::BitStream * bitStream,

PacketPriority priority,

PacketReliability reliability,

char orderingChannel,

const AddressOrGUID systemIdentifier,

bool broadcast,

uint32_t forceReceiptNumber ) // Line 1366然后,我们有一个名为SendBuffered的行:

代码语言:javascript运行复制SendBuffered((const char*)bitStream->GetData(),

bitStream->GetNumberOfBitsUsed(),

priority,

reliability,

orderingChannel,

systemIdentifier, // This is the initial AddressOrGUID

broadcast,

RemoteSystemStruct::NO_ACTION,

usedSendReceipt); // Line 1408在上面的方法中,我们可以知道缓冲区变量的名称:

代码语言:javascript运行复制bufferedCommands.Push(bcs); // Line 4216通过搜索每个使用bufferedCommands的地方,我们找到了一个有意义的方法名:

代码语言:javascript运行复制bool RakPeer::RunUpdateCycle(BitStream &updateBitStream ) // Line 5567我们可以在这里找到一个发送所有缓冲消息的循环:

代码语言:javascript运行复制callerDataAllocationUsed=SendImmediate((char*)bcs->data,

bcs->numberOfBitsToSend,

bcs->priority,

bcs->reliability,

bcs->orderingChannel,

bcs->systemIdentifier, // Initial AddressOfGUID

bcs->broadcast,

true,

timeNS,

bcs->receipt); // Line 5630RakPeer::SendImmediate()将要求RakPeer::GetSystemIndexFromGuid()查找适当的索引:

代码语言:javascript运行复制else if (systemIdentifier.rakNetGuid!=UNASSIGNED_RAKNET_GUID)

remoteSystemIndex=GetSystemIndexFromGuid(systemIdentifier.rakNetGuid); // Line 4300最后,最后一个方法将直接将索引存储在RakNet::RakNetGUID中:

代码语言:javascript运行复制unsigned int i;

for ( i = 0; i < maximumNumberOfPeers; i++ )

{

if (remoteSystemList[ i ].guid == input )

{

// Set the systemIndex so future lookups will be fast

remoteSystemList[i].guid.systemIndex = (SystemIndex) i;

return i;

}

} // Line 2440如果我们用RakNetGUID调用Send(),那么它将检查是否设置了RakNetGUID::systemIndex。如果是的话,它不需要搜索。否则,它将对发送的第一个数据包具有线性搜索时间O (n ) (n= maximumNumbersOfPeers)。

我写这篇文章是为了帮助人们理解,如果他们有同样的问题,他们是如何工作的。

相关推荐

成语词典
beat365体育亚洲版

成语词典

📅 07-13 👁️ 552
网络热梗‘V50’:从疯狂星期四到北京炫富调侃的奇妙演变
电脑公司的系统怎么样?
beat365体育亚洲版

电脑公司的系统怎么样?

📅 07-10 👁️ 7089