1 package http2interop 2 3 import ( 4 "fmt" 5 "io" 6 ) 7 8 type PingFrame struct { 9 Header FrameHeader 10 Data []byte 11 } 12 13 const ( 14 PING_ACK = 0x01 15 ) 16 17 func (f *PingFrame) GetHeader() *FrameHeader { 18 return &f.Header 19 } 20 21 func (f *PingFrame) ParsePayload(r io.Reader) error { 22 raw := make([]byte, f.Header.Length) 23 if _, err := io.ReadFull(r, raw); err != nil { 24 return err 25 } 26 return f.UnmarshalPayload(raw) 27 } 28 29 func (f *PingFrame) UnmarshalPayload(raw []byte) error { 30 if f.Header.Length != len(raw) { 31 return fmt.Errorf("Invalid Payload length %d != %d", f.Header.Length, len(raw)) 32 } 33 if f.Header.Length != 8 { 34 return fmt.Errorf("Invalid Payload length %d", f.Header.Length) 35 } 36 37 f.Data = []byte(string(raw)) 38 39 return nil 40 } 41 42 func (f *PingFrame) MarshalPayload() ([]byte, error) { 43 if len(f.Data) != 8 { 44 return nil, fmt.Errorf("Invalid Payload length %d", len(f.Data)) 45 } 46 return []byte(string(f.Data)), nil 47 } 48 49 func (f *PingFrame) MarshalBinary() ([]byte, error) { 50 payload, err := f.MarshalPayload() 51 if err != nil { 52 return nil, err 53 } 54 55 f.Header.Length = len(payload) 56 f.Header.Type = PingFrameType 57 header, err := f.Header.MarshalBinary() 58 if err != nil { 59 return nil, err 60 } 61 62 header = append(header, payload...) 63 64 return header, nil 65 } 66