implement PingFrame

This commit is contained in:
Marten Seemann 2016-05-04 10:08:07 +07:00
parent b183b0edb3
commit 01c5d91acc
5 changed files with 72 additions and 5 deletions

33
frames/ping_frame.go Normal file
View file

@ -0,0 +1,33 @@
package frames
import (
"bytes"
"github.com/lucas-clemente/quic-go/protocol"
)
// A PingFrame is a ping frame
type PingFrame struct{}
// ParsePingFrame parses a Ping frame
func ParsePingFrame(r *bytes.Reader) (*PingFrame, error) {
frame := &PingFrame{}
_, err := r.ReadByte()
if err != nil {
return nil, err
}
return frame, nil
}
func (f *PingFrame) Write(b *bytes.Buffer, packetNumber protocol.PacketNumber, packetNumberLen uint8) error {
typeByte := uint8(0x07)
b.WriteByte(typeByte)
return nil
}
// MinLength of a written frame
func (f *PingFrame) MinLength() int {
return 1
}

33
frames/ping_frame_test.go Normal file
View file

@ -0,0 +1,33 @@
package frames
import (
"bytes"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("PingFrame", func() {
Context("when parsing", func() {
It("accepts sample frame", func() {
b := bytes.NewReader([]byte{0x07})
_, err := ParsePingFrame(b)
Expect(err).ToNot(HaveOccurred())
Expect(b.Len()).To(Equal(0))
})
})
Context("when writing", func() {
It("writes a sample frame", func() {
b := &bytes.Buffer{}
frame := PingFrame{}
frame.Write(b, 10, 6)
Expect(b.Bytes()).To(Equal([]byte{0x07}))
})
It("has the correct min length", func() {
frame := PingFrame{}
Expect(frame.MinLength()).To(Equal(1))
})
})
})

View file

@ -68,10 +68,7 @@ ReadLoop:
case 0x06:
frame, err = frames.ParseStopWaitingFrame(r, publicHeader.PacketNumber, publicHeader.PacketNumberLen)
case 0x07:
// PING, do nothing
fmt.Printf("\t<- Ping Frame\n")
r.ReadByte()
continue
frame, err = frames.ParsePingFrame(r)
default:
err = protocol.NewQuicError(errorcodes.QUIC_INVALID_FRAME_DATA, fmt.Sprintf("unknown type byte 0x%x", typeByte))
}

View file

@ -152,7 +152,9 @@ var _ = Describe("Packet unpacker", func() {
setReader([]byte{0x07})
packet, err := unpacker.Unpack(hdrBin, hdr, r)
Expect(err).ToNot(HaveOccurred())
Expect(packet.frames).To(HaveLen(0))
Expect(packet.frames).To(Equal([]frames.Frame{
&frames.PingFrame{},
}))
})
It("errors on invalid type", func() {

View file

@ -176,6 +176,8 @@ func (s *Session) handlePacket(remoteAddr interface{}, publicHeader *PublicHeade
err = s.handleWindowUpdateFrame(frame)
case *frames.BlockedFrame:
fmt.Printf("BLOCKED frame received for connection %x stream %d\n", s.connectionID, frame.StreamID)
case *frames.PingFrame:
fmt.Printf("\t<- %#v\n", frame)
default:
panic("unexpected frame type")
}