/
ccfixed.go
107 lines (83 loc) · 2.88 KB
/
ccfixed.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2010 GoDCCP Authors. All rights reserved.
// Use of this source code is governed by a
// license that can be found in the LICENSE file.
package dccp
import (
"os"
"time"
)
// NewFixedRateSenderControlFunc creates a function that makes new HC-Sender Congestion Control
// which sends packets at a constant rate of sendsPerSecond packets per second
func NewFixedRateSenderControlFunc (sendsPerSecond int64) NewSenderCongestionControlFunc {
return func() SenderCongestionControl {
return newFixedRateSenderControl(1e9 / sendsPerSecond)
}
}
// NewFixedRateReceiverControlFunc creates a function that makes new HC-Receiver Congestion Control
// which sends packets at a constant rate of sendsPerSecond packets per second
func NewFixedRateReceiverControlFunc () NewReceiverCongestionControlFunc {
return func() ReceiverCongestionControl {
return newFixedRateReceiverControl()
}
}
// ---> Fixed-rate HC-Sender Congestion Control
type fixedRateSenderControl struct {
Mutex
every int64 // Strobe every every nanoseconds
strobeRead chan int
strobeWrite chan int
}
func newFixedRateSenderControl(every int64) *fixedRateSenderControl {
strobe := make(chan int)
return &fixedRateSenderControl{ every: every, strobeRead: strobe, strobeWrite: strobe }
}
func (scc *fixedRateSenderControl) Open() {
go func() {
for {
scc.Lock()
if scc.strobeWrite == nil {
scc.Unlock()
break
}
scc.strobeWrite <- 1
scc.Unlock()
time.Sleep(scc.every)
}
}()
}
const CCID_FIXED = 0xf
func (scc *fixedRateSenderControl) GetID() byte { return CCID_FIXED }
func (scc *fixedRateSenderControl) GetCCMPS() int32 { return 1e9 }
func (scc *fixedRateSenderControl) GetRTT() int64 { return RTT_DEFAULT }
func (scc *fixedRateSenderControl) OnWrite(htype byte, x bool, seqno int64) (ccval byte, options []*Option) { return 0, nil }
func (scc *fixedRateSenderControl) OnRead(htype byte, x bool, seqno int64, options []*Option) os.Error { return nil }
func (scc *fixedRateSenderControl) Strobe() os.Error {
_, ok := <-scc.strobeRead
if !ok {
return os.EBADF
}
return nil
}
func (scc *fixedRateSenderControl) Close() os.Error {
scc.Lock()
defer scc.Unlock()
if scc.strobeWrite != nil {
close(scc.strobeWrite)
scc.strobeWrite = nil
return nil
}
return os.EBADF
}
// ---> Fixed-rate HC-Receiver Congestion Control
type fixedRateReceiverControl struct {}
func newFixedRateReceiverControl() *fixedRateReceiverControl {
return &fixedRateReceiverControl{}
}
func (rcc *fixedRateReceiverControl) Open() {
}
func (rcc *fixedRateReceiverControl) GetID() byte { return CCID_FIXED }
func (rcc *fixedRateReceiverControl) OnWrite(htype byte, x bool, seqno int64) (options []*Option) { return nil }
func (rcc *fixedRateReceiverControl) OnRead(htype byte, x bool, seqno int64, ccval byte, options []*Option) os.Error {
return nil
}
func (rcc *fixedRateReceiverControl) Close() os.Error { return nil }