-
Notifications
You must be signed in to change notification settings - Fork 37
/
ping.go
77 lines (60 loc) · 1.21 KB
/
ping.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
package http2
import (
"encoding/binary"
"time"
)
const FramePing FrameType = 0x6
var _ Frame = &Ping{}
// Ping https://tools.ietf.org/html/rfc7540#section-6.7
type Ping struct {
ack bool
data [8]byte
}
func (p *Ping) IsAck() bool {
return p.ack
}
func (p *Ping) SetAck(ack bool) {
p.ack = ack
}
func (p *Ping) Type() FrameType {
return FramePing
}
func (p *Ping) Reset() {
p.ack = false
}
func (p *Ping) CopyTo(other *Ping) {
p.ack = other.ack
}
func (p *Ping) Write(b []byte) (n int, err error) {
copy(p.data[:], b)
return
}
func (p *Ping) SetData(b []byte) {
copy(p.data[:], b)
}
func (p *Ping) SetCurrentTime() {
ts := time.Now().UnixNano()
binary.BigEndian.PutUint64(p.data[:], uint64(ts))
}
func (p *Ping) DataAsTime() time.Time {
return time.Unix(
0, int64(binary.BigEndian.Uint64(p.data[:])),
)
}
func (p *Ping) Deserialize(frh *FrameHeader) error {
p.ack = frh.Flags().Has(FlagAck)
if len(frh.payload) != 8 {
return NewGoAwayError(FrameSizeError, "invalid ping payload")
}
p.SetData(frh.payload)
return nil
}
func (p *Ping) Data() []byte {
return p.data[:]
}
func (p *Ping) Serialize(fr *FrameHeader) {
if p.ack {
fr.SetFlags(fr.Flags().Add(FlagAck))
}
fr.setPayload(p.data[:])
}