ReliableWindowBuffer

class ReliableWindowBuffer(    coroutineScope: CoroutineScope,     windowSize: Int = 16,     resendTimeout: Duration = 10.toDuration(DurationUnit.SECONDS),     initialSequenceNumber: UShort = 0.toUShort())

This implements an asynchronous transfer window buffer with automatic repeat request (ARQ) semantics.

It keeps a window of windowSize packets that can ben transferred until an acknowledgement needs to be received by the other side. If it takes longer than the specified resendTimeout for an acknowledgement to arrive from the remote side the expired packets will be repeated. Once a packet is acknowledged it is removed from the window buffer. If the buffer is full adding more packets will suspend until there is more space available.

Constructors

Link copied to clipboard
fun ReliableWindowBuffer(    coroutineScope: CoroutineScope,     windowSize: Int = 16,     resendTimeout: Duration = 10.toDuration(DurationUnit.SECONDS),     initialSequenceNumber: UShort = 0.toUShort())

Functions

Link copied to clipboard
suspend fun acknowledge(sequenceNumber: UShort)

Acknowledges all packets in the buffer that come before the specified sequenceNumber. This should be called when the receiver sends back an acknowledgement sequence number. If any packets are acknowledged by this call it will fee up space in the sending window and notify any suspended sendData calls.

Link copied to clipboard
fun onDestroy()

Destroys the buffer, causing any suspending function to return false.

Link copied to clipboard
suspend fun receive(): Packet?

Returns the next packet that is scheduled to be send to the receiver. If there are no packets up for immediate sending this will suspend until one is added by a call to sendData or if the timeout of any packets in the buffer expire, triggering an automatic repeat request (a resend). Returns null when the buffer is currently being destroyed.

Link copied to clipboard
suspend fun sendAcknowledgement(sequenceNumber: UShort)

Sends an acknowledgement with the specified sequenceNumber to the receiver. The acknowledgement will be send immediately, either as part of a data packet that is queued for immediate sending or as a new pure acknowledgement packet.

Link copied to clipboard
suspend fun sendData(data: ByteArray): Boolean

Sends the specified data. This function suspends when the window is full and waits for acknowledgements to be sent back. Returns true on success, false when the buffer is currently being destroyed.