- Notification about each lost unreliable packet
- Transport-agnostic
- Simple logger
The project provides a helper class for detection and notification about lost unreliable packets. This is an important part of many advanced synchronization strategies. For example, Eventual Consistency uses it.
For simplicity, the server supports only one client.
Overview |
---|
Detection works by adding that header to each sent packet:
ushort
Local Sequence Idushort
Last Remote Sequence Iduint
Bitmask for a 32 previous remote sequence Ids
The workflow is like that:
- Peer Connected:
-
- Call
AddPeer
in helper
- Call
- Sending packet to Peer:
-
- Get empty BitBuffer
-
- Call
AddHeaderForPeerId
- Call
-
- Write actual payload to BitBuffer
-
- Copy BitBuffer's content to allocated memory
-
- Clear BitBuffer
-
- Call
EnqueueData
- Call
-
-
- If result is TRUE
-
-
-
-
- Send packet to peer
-
-
-
-
- If result is FALSE
-
-
-
-
- Disconnect that peer
-
-
- Received packet from Peer:
-
- Get packet's content into BitBuffer
-
- Call
ReadHeaderOfPeerId
with it
- Call
-
-
- If result is TRUE
-
-
-
-
- Read actual payload
-
-
- Periodically:
-
- Call
ExecuteLostPackets
- Call
- On lost packet:
-
- Get data into BitBuffer
-
- Call
ClearHeader
- Call
-
- Read actual payload
- Peer Disconnected:
-
- Call
RemovePeer
- Call
If packet is considered as Lost it means that it was most likely lost. When packet is considered as Arrived, then it is arrived for sure.
ILossHandler.OnPacketLost
will be called for each lost packet AND for each packet which was enqueued and still tracked when that peer got disconnected.
If LOSS_DETECTOR_DEBUG
symbol is present, then all lost packet sequences will be outputted to the logger.
- void LossDetector(
ILossHandler handler, ushort maxPeerCount, ushort ackWindow
) - That implementation ofILossHandler
will be called for each lost packet.MaxPeerCount
- Max count of peers connected to that peer.AckWindow
- Max count of local tracked packets per peer. - void AddPeer(
ushort peerId
) - Clears sequence number for that peerId. - void RemovePeer(
ushort peerId
) - Enqueues each tracked packet for that peerId as being lost. - void AddHeaderForPeerId(
ushort peerId, BitBuffer data
) - Writes header information into BitBuffer for that peerId. - bool ReadHeaderOfPeerId(
ushort peerId, BitBuffer data
) - Reads header from BitBuffer and detects if any tracked packet is ACKed or NACKed. In the case of ACK detector will free allocated memory. Returns True if sequenceId is greater than lastSequenceId for that peerId. - bool EnqueueData(
ushort peerId, PacketData data
) - Tries to put data into tracked packet queue for that peer. Returns False if the queue is already full. Probably you have not received packets from another peer for too long and should disconnect him. - void ExecuteLostPackets() - Forwards all buffered lost packets to the handler and frees their allocated memory.
- void ClearHeader(
BitBuffer data
) - Clears header from BitBuffer.
If LOSS_DETECTOR_DEBUG
symbol is present, then those methods are available too:
- void GetDebugString(
StringBuilder builder
) - Writes full state of buffers for each peerId into StringBuilder. - void GetDebugString(
ushort peerId, StringBuilder builder
) - Writes state of buffers for that peerId.
- Unity 2019.1
- NetStack.Serialization
Helper itself is transport-agnostic, but example project uses ENet
- ENet CSharp 2.2.6
fholm - The Master of Networking - has helped a lot.