2024-06-05 20:10:03 +00:00
|
|
|
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2022-03-10 09:44:48 +00:00
|
|
|
package sctp
|
|
|
|
|
|
|
|
import (
|
2024-06-05 20:10:03 +00:00
|
|
|
"math"
|
2022-03-10 09:44:48 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ackInterval time.Duration = 200 * time.Millisecond
|
|
|
|
)
|
|
|
|
|
|
|
|
// ackTimerObserver is the inteface to an ack timer observer.
|
|
|
|
type ackTimerObserver interface {
|
|
|
|
onAckTimeout()
|
|
|
|
}
|
|
|
|
|
2024-10-10 11:33:36 +00:00
|
|
|
type ackTimerState uint8
|
2024-06-05 20:10:03 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
ackTimerStopped ackTimerState = iota
|
|
|
|
ackTimerStarted
|
|
|
|
ackTimerClosed
|
|
|
|
)
|
|
|
|
|
2022-03-10 09:44:48 +00:00
|
|
|
// ackTimer provides the retnransmission timer conforms with RFC 4960 Sec 6.3.1
|
|
|
|
type ackTimer struct {
|
2024-10-10 11:33:36 +00:00
|
|
|
timer *time.Timer
|
2022-03-10 09:44:48 +00:00
|
|
|
observer ackTimerObserver
|
2024-10-10 11:33:36 +00:00
|
|
|
mutex sync.Mutex
|
2024-06-05 20:10:03 +00:00
|
|
|
state ackTimerState
|
2024-10-10 11:33:36 +00:00
|
|
|
pending uint8
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newAckTimer creates a new acknowledgement timer used to enable delayed ack.
|
|
|
|
func newAckTimer(observer ackTimerObserver) *ackTimer {
|
2024-06-05 20:10:03 +00:00
|
|
|
t := &ackTimer{observer: observer}
|
|
|
|
t.timer = time.AfterFunc(math.MaxInt64, t.timeout)
|
|
|
|
t.timer.Stop()
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *ackTimer) timeout() {
|
|
|
|
t.mutex.Lock()
|
2024-10-10 11:33:36 +00:00
|
|
|
if t.pending--; t.pending == 0 && t.state == ackTimerStarted {
|
2024-06-05 20:10:03 +00:00
|
|
|
t.state = ackTimerStopped
|
|
|
|
defer t.observer.onAckTimeout()
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
2024-06-05 20:10:03 +00:00
|
|
|
t.mutex.Unlock()
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// start starts the timer.
|
|
|
|
func (t *ackTimer) start() bool {
|
|
|
|
t.mutex.Lock()
|
|
|
|
defer t.mutex.Unlock()
|
|
|
|
|
2024-06-05 20:10:03 +00:00
|
|
|
// this timer is already closed or already running
|
|
|
|
if t.state != ackTimerStopped {
|
2022-03-10 09:44:48 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-06-05 20:10:03 +00:00
|
|
|
t.state = ackTimerStarted
|
2024-10-10 11:33:36 +00:00
|
|
|
t.pending++
|
2024-06-05 20:10:03 +00:00
|
|
|
t.timer.Reset(ackInterval)
|
2022-03-10 09:44:48 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// stops the timer. this is similar to stop() but subsequent start() call
|
|
|
|
// will fail (the timer is no longer usable)
|
|
|
|
func (t *ackTimer) stop() {
|
|
|
|
t.mutex.Lock()
|
|
|
|
defer t.mutex.Unlock()
|
|
|
|
|
2024-06-05 20:10:03 +00:00
|
|
|
if t.state == ackTimerStarted {
|
2024-10-10 11:33:36 +00:00
|
|
|
if t.timer.Stop() {
|
|
|
|
t.pending--
|
|
|
|
}
|
2024-06-05 20:10:03 +00:00
|
|
|
t.state = ackTimerStopped
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// closes the timer. this is similar to stop() but subsequent start() call
|
|
|
|
// will fail (the timer is no longer usable)
|
|
|
|
func (t *ackTimer) close() {
|
|
|
|
t.mutex.Lock()
|
|
|
|
defer t.mutex.Unlock()
|
|
|
|
|
2024-10-10 11:33:36 +00:00
|
|
|
if t.state == ackTimerStarted && t.timer.Stop() {
|
|
|
|
t.pending--
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
2024-06-05 20:10:03 +00:00
|
|
|
t.state = ackTimerClosed
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// isRunning tests if the timer is running.
|
|
|
|
// Debug purpose only
|
|
|
|
func (t *ackTimer) isRunning() bool {
|
2024-10-10 11:33:36 +00:00
|
|
|
t.mutex.Lock()
|
|
|
|
defer t.mutex.Unlock()
|
2022-03-10 09:44:48 +00:00
|
|
|
|
2024-06-05 20:10:03 +00:00
|
|
|
return t.state == ackTimerStarted
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|