Skip to content

Commit

Permalink
Reuse buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
VHSgunzo committed Jun 23, 2024
1 parent dd3b19b commit 3e4d7fd
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions share/cnet/conn_ws.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cnet

import (
"bytes"
"net"
"time"

Expand All @@ -9,13 +10,14 @@ import (

type wsConn struct {
*websocket.Conn
buff []byte
buff *bytes.Buffer
}

// NewWebSocketConn converts a websocket.Conn into a net.Conn
func NewWebSocketConn(websocketConn *websocket.Conn) net.Conn {
c := wsConn{
Conn: websocketConn,
buff: &bytes.Buffer{},
}
return &c
}
Expand All @@ -26,9 +28,9 @@ func (c *wsConn) Read(dst []byte) (int, error) {
ldst := len(dst)
//use buffer or read new message
var src []byte
if len(c.buff) > 0 {
src = c.buff
c.buff = nil
if c.buff.Len() > 0 {
src = c.buff.Bytes()
c.buff.Reset()
} else if _, msg, err := c.Conn.ReadMessage(); err == nil {
src = msg
} else {
Expand All @@ -41,9 +43,7 @@ func (c *wsConn) Read(dst []byte) (int, error) {
n = copy(dst, src[:ldst])
//copy remainder into buffer
r := src[ldst:]
lr := len(r)
c.buff = make([]byte, lr)
copy(c.buff, r)
c.buff.Write(r)
} else {
//copy all of src into dst
n = copy(dst, src)
Expand Down

0 comments on commit 3e4d7fd

Please sign in to comment.