forked from SlyMarbo/spdy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping.go
87 lines (68 loc) · 1.98 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
78
79
80
81
82
83
84
85
86
87
// Copyright 2014 Jamie Hall. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package frames
import (
"bytes"
"fmt"
"io"
"github.com/SlyMarbo/spdy/common"
)
type PING struct {
PingID uint32
}
func (frame *PING) Compress(comp common.Compressor) error {
return nil
}
func (frame *PING) Decompress(decomp common.Decompressor) error {
return nil
}
func (frame *PING) Name() string {
return "PING"
}
func (frame *PING) ReadFrom(reader io.Reader) (int64, error) {
c := common.ReadCounter{R: reader}
data, err := common.ReadExactly(&c, 12)
if err != nil {
return c.N, err
}
err = controlFrameCommonProcessing(data[:5], _PING, 0)
if err != nil {
return c.N, err
}
// Get and check length.
length := int(common.BytesToUint24(data[5:8]))
if length != 4 {
return c.N, common.IncorrectDataLength(length, 4)
}
frame.PingID = common.BytesToUint32(data[8:12])
return c.N, nil
}
func (frame *PING) String() string {
buf := new(bytes.Buffer)
buf.WriteString("PING {\n\t")
buf.WriteString(fmt.Sprintf("Version: 3\n\t"))
buf.WriteString(fmt.Sprintf("Ping ID: %d\n}\n", frame.PingID))
return buf.String()
}
func (frame *PING) WriteTo(writer io.Writer) (int64, error) {
c := common.WriteCounter{W: writer}
out := make([]byte, 12)
out[0] = 128 // Control bit and Version
out[1] = 3 // Version
out[2] = 0 // Type
out[3] = 6 // Type
out[4] = 0 // common.Flags
out[5] = 0 // Length
out[6] = 0 // Length
out[7] = 4 // Length
out[8] = byte(frame.PingID >> 24) // Ping ID
out[9] = byte(frame.PingID >> 16) // Ping ID
out[10] = byte(frame.PingID >> 8) // Ping ID
out[11] = byte(frame.PingID) // Ping ID
err := common.WriteExactly(&c, out)
if err != nil {
return c.N, err
}
return c.N, nil
}