I was wondering how to send packet to client in client-server architecture with RakNet. In this sample code we have this line:
peer->Send(&bsOut,HIGH_PRIORITY,RELIABLE_ORDERED,0,packet->systemAddress,false);
However, the prototype is the following (from the interface class):
virtual uint32_t Send( const RakNet::BitStream * bitStream,
PacketPriority priority,
PacketReliability reliability,
char orderingChannel,
const AddressOrGUID systemIdentifier,
bool broadcast,
uint32_t forceReceiptNumber=0 )=0;
As you can see, the 5th parameter takes a AddressOrGUID, it means we can send the SystemAddress as in the sample, but also can send the unique GUID of a connected machine.
There is a function called:
RakNet::GetSystemAddressFromGUID();
But I'm not sure if RakNet uses it to convert the GUID we can send as a parameter (I didn't find any use of this method in RakPeer (implemention of RakPeerInterface) and I'm not able to find how buffered packet are sent each tick).
The problem is the following:
The sample code replies directly to the received packet. However, in a game, server has to send information without receiving packet from client. So I don't have access to something like
packet->systemAddress
because there is no received packet.
So I will have to stock something in my Player class to know how to send them packets: SystemAddress or RakNetGUID. RakNetGUID is simpler and lighter to stock than a SystemAddress.
But, if RakNet uses GetSystemAddressFromGUID(), it's not worth because is has a O(log(n)) algorithm.
Do I need to stock the SystemAddress for each Player myself or RakNet::Send() doesn't use this method with a RakNetGUID ?
Thank you!
Ok I just did a mistake by not correctly following condition statement the first time I tried to understand the source code, and because this question is really specific, I think it would be great to look in source code.
The simple answer is Yes, store RakNetGUID in Player class
Details here:
Ok so first, file concerned is RakPeer.cpp only. The starting point is:
Then, we have this line where SendBuffered is called:
In the method above, we can know the name of the buffer variable:
And by searching every place where bufferedCommands is used, we find a meaningful method name:
We can find a loop that sends every buffered message here:
RakPeer::SendImmediate() will ask RakPeer::GetSystemIndexFromGuid() to find the appropriate Index:
Finally, this last method will store the index directly in the RakNet::RakNetGUID when found:
If we call Send() with RakNetGUID, then it will check if RakNetGUID::systemIndex is set. If yes, it doesn't need to search. Else, it will have a linear searching time O(n) (n = maximumNumbersOfPeers) for the first packet sent.
I wrote this to help people understand how it works if they have the same question in mind.