forked from quic-go/quic-go
implement PingFrame
This commit is contained in:
33
frames/ping_frame.go
Normal file
33
frames/ping_frame.go
Normal 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
33
frames/ping_frame_test.go
Normal 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))
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user