github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/tcp/stream-timing.go (about) 1 /* For license and copyright information please see the LEGAL file in the code repository */ 2 3 package tcp 4 5 import ( 6 "github.com/GeniusesGroup/libgo/protocol" 7 "github.com/GeniusesGroup/libgo/time/monotonic" 8 "github.com/GeniusesGroup/libgo/timer" 9 ) 10 11 type timing struct { 12 st *Stream 13 // TODO::: one timer or many per handler?? 14 socketTimer timer.Timer 15 16 keepAlive 17 delayedAcknowledgment 18 } 19 20 func (t *timing) Init(st *Stream) { 21 var now = monotonic.Now() 22 var next protocol.Duration 23 24 t.st = st 25 t.socketTimer.Init(t) 26 27 if KeepAlive { 28 var nxt = t.keepAlive.Init(now) 29 if nxt > 0 && nxt < next { 30 next = nxt 31 } 32 } 33 34 if DelayedAcknowledgment { 35 var nxt = t.delayedAcknowledgment.Init(now) 36 if nxt > 0 && nxt < next { 37 next = nxt 38 } 39 } 40 41 if next > 0 { 42 t.socketTimer.Tick(next, next, -1) 43 } 44 } 45 func (t *timing) Reinit() { 46 if KeepAlive { 47 t.keepAlive.Reinit() 48 } 49 if DelayedAcknowledgment { 50 t.delayedAcknowledgment.Reinit() 51 } 52 t.socketTimer.Stop() 53 } 54 func (t *timing) Deinit() { 55 if KeepAlive { 56 t.keepAlive.Deinit() 57 } 58 if DelayedAcknowledgment { 59 t.delayedAcknowledgment.Deinit() 60 } 61 t.socketTimer.Stop() 62 } 63 64 // Don't block the caller 65 func (t *timing) TimerHandler() { 66 var next protocol.Duration 67 var now = monotonic.Now() 68 var st = t.st 69 70 if KeepAlive { 71 var nxt = t.keepAlive.CheckInterval(st, now) 72 if nxt > 0 && nxt < next { 73 next = nxt 74 } 75 } 76 77 if DelayedAcknowledgment { 78 var nxt = t.delayedAcknowledgment.CheckInterval(st, now) 79 if nxt > 0 && nxt < next { 80 next = nxt 81 } 82 } 83 84 // TODO::: add more handler 85 86 if next > 0 { 87 t.socketTimer.Reset(protocol.Duration(next)) 88 } 89 }