forked from quic-go/quic-go
introduce an ErrorCode
This commit is contained in:
@@ -5,12 +5,13 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"math"
|
"math"
|
||||||
|
|
||||||
|
"github.com/lucas-clemente/quic-go/protocol"
|
||||||
"github.com/lucas-clemente/quic-go/utils"
|
"github.com/lucas-clemente/quic-go/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A ConnectionCloseFrame in QUIC
|
// A ConnectionCloseFrame in QUIC
|
||||||
type ConnectionCloseFrame struct {
|
type ConnectionCloseFrame struct {
|
||||||
ErrorCode uint32
|
ErrorCode protocol.ErrorCode
|
||||||
ReasonPhrase string
|
ReasonPhrase string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,10 +25,11 @@ func ParseConnectionCloseFrame(r *bytes.Reader) (*ConnectionCloseFrame, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
frame.ErrorCode, err = utils.ReadUint32(r)
|
errorCode, err := utils.ReadUint32(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
frame.ErrorCode = protocol.ErrorCode(errorCode)
|
||||||
|
|
||||||
reasonPhraseLen, err := utils.ReadUint16(r)
|
reasonPhraseLen, err := utils.ReadUint16(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -49,7 +51,7 @@ func ParseConnectionCloseFrame(r *bytes.Reader) (*ConnectionCloseFrame, error) {
|
|||||||
// Write writes an CONNECTION_CLOSE frame.
|
// Write writes an CONNECTION_CLOSE frame.
|
||||||
func (f *ConnectionCloseFrame) Write(b *bytes.Buffer) error {
|
func (f *ConnectionCloseFrame) Write(b *bytes.Buffer) error {
|
||||||
b.WriteByte(0x02)
|
b.WriteByte(0x02)
|
||||||
utils.WriteUint32(b, f.ErrorCode)
|
utils.WriteUint32(b, uint32(f.ErrorCode))
|
||||||
|
|
||||||
if len(f.ReasonPhrase) > math.MaxUint16 {
|
if len(f.ReasonPhrase) > math.MaxUint16 {
|
||||||
return errors.New("ConnectionFrame: ReasonPhrase too long")
|
return errors.New("ConnectionFrame: ReasonPhrase too long")
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package frames
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
|
||||||
|
"github.com/lucas-clemente/quic-go/protocol"
|
||||||
. "github.com/onsi/ginkgo"
|
. "github.com/onsi/ginkgo"
|
||||||
. "github.com/onsi/gomega"
|
. "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})
|
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)
|
frame, err := ParseConnectionCloseFrame(b)
|
||||||
Expect(err).ToNot(HaveOccurred())
|
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(frame.ReasonPhrase).To(Equal("No recent network activity."))
|
||||||
Expect(b.Len()).To(Equal(0))
|
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})
|
b := bytes.NewReader([]byte{0x02, 0xAD, 0xFB, 0xCA, 0xDE, 0x00, 0x00})
|
||||||
frame, err := ParseConnectionCloseFrame(b)
|
frame, err := ParseConnectionCloseFrame(b)
|
||||||
Expect(err).ToNot(HaveOccurred())
|
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(len(frame.ReasonPhrase)).To(Equal(0))
|
||||||
Expect(b.Len()).To(Equal(0))
|
Expect(b.Len()).To(Equal(0))
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -8,3 +8,6 @@ type ConnectionID uint64
|
|||||||
|
|
||||||
// A StreamID in QUIC
|
// A StreamID in QUIC
|
||||||
type StreamID uint32
|
type StreamID uint32
|
||||||
|
|
||||||
|
// An ErrorCode in QUIC
|
||||||
|
type ErrorCode uint32
|
||||||
|
|||||||
Reference in New Issue
Block a user