introduce an ErrorCode

This commit is contained in:
Marten Seemann
2016-04-19 12:43:04 +07:00
parent 30998cef1f
commit ec64be840e
3 changed files with 11 additions and 5 deletions

View File

@@ -5,12 +5,13 @@ import (
"errors"
"math"
"github.com/lucas-clemente/quic-go/protocol"
"github.com/lucas-clemente/quic-go/utils"
)
// A ConnectionCloseFrame in QUIC
type ConnectionCloseFrame struct {
ErrorCode uint32
ErrorCode protocol.ErrorCode
ReasonPhrase string
}
@@ -24,10 +25,11 @@ func ParseConnectionCloseFrame(r *bytes.Reader) (*ConnectionCloseFrame, error) {
return nil, err
}
frame.ErrorCode, err = utils.ReadUint32(r)
errorCode, err := utils.ReadUint32(r)
if err != nil {
return nil, err
}
frame.ErrorCode = protocol.ErrorCode(errorCode)
reasonPhraseLen, err := utils.ReadUint16(r)
if err != nil {
@@ -49,7 +51,7 @@ func ParseConnectionCloseFrame(r *bytes.Reader) (*ConnectionCloseFrame, error) {
// Write writes an CONNECTION_CLOSE frame.
func (f *ConnectionCloseFrame) Write(b *bytes.Buffer) error {
b.WriteByte(0x02)
utils.WriteUint32(b, f.ErrorCode)
utils.WriteUint32(b, uint32(f.ErrorCode))
if len(f.ReasonPhrase) > math.MaxUint16 {
return errors.New("ConnectionFrame: ReasonPhrase too long")

View File

@@ -3,6 +3,7 @@ package frames
import (
"bytes"
"github.com/lucas-clemente/quic-go/protocol"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
@@ -13,7 +14,7 @@ var _ = Describe("ConnectionCloseFrame", func() {
b := bytes.NewReader([]byte{0x40, 0x19, 0x00, 0x00, 0x00, 0x1B, 0x00, 0x4e, 0x6f, 0x20, 0x72, 0x65, 0x63, 0x65, 0x6e, 0x74, 0x20, 0x6e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x20, 0x61, 0x63, 0x74, 0x69, 0x76, 0x69, 0x74, 0x79, 0x2e})
frame, err := ParseConnectionCloseFrame(b)
Expect(err).ToNot(HaveOccurred())
Expect(frame.ErrorCode).To(Equal(uint32(0x19)))
Expect(frame.ErrorCode).To(Equal(protocol.ErrorCode(0x19)))
Expect(frame.ReasonPhrase).To(Equal("No recent network activity."))
Expect(b.Len()).To(Equal(0))
})
@@ -22,7 +23,7 @@ var _ = Describe("ConnectionCloseFrame", func() {
b := bytes.NewReader([]byte{0x02, 0xAD, 0xFB, 0xCA, 0xDE, 0x00, 0x00})
frame, err := ParseConnectionCloseFrame(b)
Expect(err).ToNot(HaveOccurred())
Expect(frame.ErrorCode).To(Equal(uint32(0xDECAFBAD)))
Expect(frame.ErrorCode).To(Equal(protocol.ErrorCode(0xDECAFBAD)))
Expect(len(frame.ReasonPhrase)).To(Equal(0))
Expect(b.Len()).To(Equal(0))
})