github.com/alejandroesc/spdy@v0.0.0-20200317064415-01a02f0eb389/spdy3/frames/ping.go (about) 1 // Copyright 2014 Jamie Hall. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package frames 6 7 import ( 8 "bytes" 9 "fmt" 10 "io" 11 12 "github.com/SlyMarbo/spdy/common" 13 ) 14 15 type PING struct { 16 PingID uint32 17 } 18 19 func (frame *PING) Compress(comp common.Compressor) error { 20 return nil 21 } 22 23 func (frame *PING) Decompress(decomp common.Decompressor) error { 24 return nil 25 } 26 27 func (frame *PING) Name() string { 28 return "PING" 29 } 30 31 func (frame *PING) ReadFrom(reader io.Reader) (int64, error) { 32 c := common.ReadCounter{R: reader} 33 data, err := common.ReadExactly(&c, 12) 34 if err != nil { 35 return c.N, err 36 } 37 38 err = controlFrameCommonProcessing(data[:5], _PING, 0) 39 if err != nil { 40 return c.N, err 41 } 42 43 // Get and check length. 44 length := int(common.BytesToUint24(data[5:8])) 45 if length != 4 { 46 return c.N, common.IncorrectDataLength(length, 4) 47 } 48 49 frame.PingID = common.BytesToUint32(data[8:12]) 50 51 return c.N, nil 52 } 53 54 func (frame *PING) String() string { 55 buf := new(bytes.Buffer) 56 57 buf.WriteString("PING {\n\t") 58 buf.WriteString(fmt.Sprintf("Version: 3\n\t")) 59 buf.WriteString(fmt.Sprintf("Ping ID: %d\n}\n", frame.PingID)) 60 61 return buf.String() 62 } 63 64 func (frame *PING) WriteTo(writer io.Writer) (int64, error) { 65 c := common.WriteCounter{W: writer} 66 out := make([]byte, 12) 67 68 out[0] = 128 // Control bit and Version 69 out[1] = 3 // Version 70 out[2] = 0 // Type 71 out[3] = 6 // Type 72 out[4] = 0 // common.Flags 73 out[5] = 0 // Length 74 out[6] = 0 // Length 75 out[7] = 4 // Length 76 out[8] = byte(frame.PingID >> 24) // Ping ID 77 out[9] = byte(frame.PingID >> 16) // Ping ID 78 out[10] = byte(frame.PingID >> 8) // Ping ID 79 out[11] = byte(frame.PingID) // Ping ID 80 81 err := common.WriteExactly(&c, out) 82 if err != nil { 83 return c.N, err 84 } 85 86 return c.N, nil 87 }