-
Notifications
You must be signed in to change notification settings - Fork 0
/
paths_frame.go
executable file
·91 lines (75 loc) · 2.09 KB
/
paths_frame.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
package wire
import (
"bytes"
"errors"
"time"
"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/utils"
)
var (
ErrTooManyPaths = errors.New("PathsFrame: more paths than the maximum enabled")
ErrPathsNumber = errors.New("PathsFrame: number of paths advertised and # of paths do not match")
ErrMissingRTT = errors.New("PathsFrame: number of paths IDs and number of remote RTTs do not match")
)
// A PathsFrame in QUIC
type PathsFrame struct {
MaxNumPaths uint8
NumPaths uint8
PathIDs []protocol.PathID
RemoteRTTs []time.Duration
}
func (f *PathsFrame) Write(b *bytes.Buffer, version protocol.VersionNumber) error {
typeByte := uint8(0x12)
b.WriteByte(typeByte)
b.WriteByte(f.MaxNumPaths)
b.WriteByte(f.NumPaths)
if int(f.NumPaths) != len(f.PathIDs) {
return ErrPathsNumber
}
if len(f.PathIDs) != len(f.RemoteRTTs) {
return ErrMissingRTT
}
for i := 0; i < len(f.PathIDs); i++ {
b.WriteByte(uint8(f.PathIDs[i]))
utils.GetByteOrder(version).WriteUfloat16(b, uint64(f.RemoteRTTs[i]/time.Microsecond))
}
return nil
}
func ParsePathsFrame(r *bytes.Reader, version protocol.VersionNumber) (*PathsFrame, error) {
frame := &PathsFrame{}
// read the TypeByte
_, err := r.ReadByte()
if err != nil {
return nil, err
}
maxNum, err := r.ReadByte()
if err != nil {
return nil, err
}
frame.MaxNumPaths = maxNum
num, err := r.ReadByte()
if err != nil {
return nil, err
}
frame.NumPaths = num
if frame.NumPaths > frame.MaxNumPaths {
return nil, ErrTooManyPaths
}
for i := 0; i < int(frame.NumPaths); i++ {
pathID, err := r.ReadByte()
if err != nil {
return nil, err
}
frame.PathIDs = append(frame.PathIDs, protocol.PathID(pathID))
remoteRTT, err := utils.GetByteOrder(version).ReadUfloat16(r)
if err != nil {
return nil, err
}
frame.RemoteRTTs = append(frame.RemoteRTTs, time.Duration(remoteRTT)*time.Microsecond)
}
return frame, nil
}
func (f *PathsFrame) MinLength(version protocol.VersionNumber) (protocol.ByteCount, error) {
length := 1 + 1 + 1 + (3 * f.NumPaths)
return protocol.ByteCount(length), nil
}