forked from quic-go/quic-go
add method to Write ConnectionClose frames
This commit is contained in:
@@ -3,6 +3,7 @@ package frames
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"math"
|
||||
|
||||
"github.com/lucas-clemente/quic-go/utils"
|
||||
)
|
||||
@@ -13,12 +14,7 @@ type ConnectionCloseFrame struct {
|
||||
ReasonPhrase string
|
||||
}
|
||||
|
||||
// Write writes an ACK frame.
|
||||
func (f *ConnectionCloseFrame) Write(b *bytes.Buffer) error {
|
||||
return errors.New("ConnectionCloseFrame: Write not yet implemented")
|
||||
}
|
||||
|
||||
// ParseConnectionCloseFrame reads an ACK frame
|
||||
// ParseConnectionCloseFrame reads a CONNECTION_CLOSE frame
|
||||
func ParseConnectionCloseFrame(r *bytes.Reader) (*ConnectionCloseFrame, error) {
|
||||
frame := &ConnectionCloseFrame{}
|
||||
|
||||
@@ -49,3 +45,22 @@ func ParseConnectionCloseFrame(r *bytes.Reader) (*ConnectionCloseFrame, error) {
|
||||
|
||||
return frame, nil
|
||||
}
|
||||
|
||||
// Write writes an CONNECTION_CLOSE frame.
|
||||
func (f *ConnectionCloseFrame) Write(b *bytes.Buffer) error {
|
||||
b.WriteByte(0x02)
|
||||
utils.WriteUint32(b, f.ErrorCode)
|
||||
|
||||
if len(f.ReasonPhrase) > math.MaxUint16 {
|
||||
return errors.New("ConnectionFrame: ReasonPhrase too long")
|
||||
}
|
||||
|
||||
reasonPhraseLen := uint16(len(f.ReasonPhrase))
|
||||
utils.WriteUint16(b, reasonPhraseLen)
|
||||
|
||||
for i := 0; i < int(reasonPhraseLen); i++ {
|
||||
b.WriteByte(uint8(f.ReasonPhrase[i]))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user