Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

separate msgState for reader/writer #108

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ type Connection struct {
reader *wsutil.Reader
flateWriter *wsflate.Writer
writer *wsutil.Writer
msgState *wsflate.MessageState
msgStateR *wsflate.MessageState
msgStateW *wsflate.MessageState
}

func NewConnection(ctx context.Context, url string, requestHeader http.Header) (*Connection, error) {
Expand All @@ -51,9 +52,9 @@ func NewConnection(ctx context.Context, url string, requestHeader http.Header) (

// reader
var flateReader *wsflate.Reader
var msgState wsflate.MessageState
var msgStateR wsflate.MessageState
if enableCompression {
msgState.SetCompressed(true)
msgStateR.SetCompressed(true)

flateReader = wsflate.NewReader(nil, func(r io.Reader) wsflate.Decompressor {
return flate.NewReader(r)
Expand All @@ -67,13 +68,16 @@ func NewConnection(ctx context.Context, url string, requestHeader http.Header) (
OnIntermediate: controlHandler,
CheckUTF8: false,
Extensions: []wsutil.RecvExtension{
&msgState,
&msgStateR,
},
}

// writer
var flateWriter *wsflate.Writer
var msgStateW wsflate.MessageState
if enableCompression {
msgStateW.SetCompressed(true)

flateWriter = wsflate.NewWriter(nil, func(w io.Writer) wsflate.Compressor {
fw, err := flate.NewWriter(w, 4)
if err != nil {
Expand All @@ -84,22 +88,23 @@ func NewConnection(ctx context.Context, url string, requestHeader http.Header) (
}

writer := wsutil.NewWriter(conn, state, ws.OpText)
writer.SetExtensions(&msgState)
writer.SetExtensions(&msgStateW)

return &Connection{
conn: conn,
enableCompression: enableCompression,
controlHandler: controlHandler,
flateReader: flateReader,
reader: reader,
msgStateR: &msgStateR,
flateWriter: flateWriter,
msgState: &msgState,
writer: writer,
msgStateW: &msgStateW,
}, nil
}

func (c *Connection) WriteMessage(data []byte) error {
if c.msgState.IsCompressed() && c.enableCompression {
if c.msgStateW.IsCompressed() && c.enableCompression {
c.flateWriter.Reset(c.writer)
if _, err := io.Copy(c.flateWriter, bytes.NewReader(data)); err != nil {
return fmt.Errorf("failed to write message: %w", err)
Expand Down Expand Up @@ -149,7 +154,7 @@ func (c *Connection) ReadMessage(ctx context.Context, buf io.Writer) error {
}
}

if c.msgState.IsCompressed() && c.enableCompression {
if c.msgStateR.IsCompressed() && c.enableCompression {
c.flateReader.Reset(c.reader)
if _, err := io.Copy(buf, c.flateReader); err != nil {
return fmt.Errorf("failed to read message: %w", err)
Expand Down
Loading