Skip to content

Commit

Permalink
bug: fix the data corruption in some default codecs
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Nov 26, 2021
1 parent 58d2031 commit a56d2f3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
17 changes: 10 additions & 7 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,15 @@ func (cc *LineBasedFrameCodec) Encode(c Conn, buf []byte) ([]byte, error) {
}

// Decode ...
func (cc *LineBasedFrameCodec) Decode(c Conn) ([]byte, error) {
func (cc *LineBasedFrameCodec) Decode(c Conn) (b []byte, err error) {
buf := c.Read()
idx := bytes.IndexByte(buf, CRLFByte)
if idx == -1 {
return nil, errorset.ErrCRLFNotFound
}
b = append(b, buf[:idx]...)
c.ShiftN(idx + 1)
return buf[:idx], nil
return
}

// NewDelimiterBasedFrameCodec instantiates and returns a codec with a specific delimiter.
Expand All @@ -102,14 +103,15 @@ func (cc *DelimiterBasedFrameCodec) Encode(c Conn, buf []byte) ([]byte, error) {
}

// Decode ...
func (cc *DelimiterBasedFrameCodec) Decode(c Conn) ([]byte, error) {
func (cc *DelimiterBasedFrameCodec) Decode(c Conn) (b []byte, err error) {
buf := c.Read()
idx := bytes.IndexByte(buf, cc.delimiter)
if idx == -1 {
return nil, errorset.ErrDelimiterNotFound
}
b = append(b, buf[:idx]...)
c.ShiftN(idx + 1)
return buf[:idx], nil
return
}

// NewFixedLengthFrameCodec instantiates and returns a codec with fixed length.
Expand All @@ -126,13 +128,14 @@ func (cc *FixedLengthFrameCodec) Encode(c Conn, buf []byte) ([]byte, error) {
}

// Decode ...
func (cc *FixedLengthFrameCodec) Decode(c Conn) ([]byte, error) {
func (cc *FixedLengthFrameCodec) Decode(c Conn) (b []byte, err error) {
size, buf := c.ReadN(cc.frameLength)
if size == 0 {
if size != cc.frameLength {
return nil, errorset.ErrUnexpectedEOF
}
b = append(b, buf...)
c.ShiftN(size)
return buf, nil
return
}

// NewLengthFieldBasedFrameCodec instantiates and returns a codec based on the length field.
Expand Down
6 changes: 3 additions & 3 deletions connection_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (c *conn) Read() []byte {
return c.buffer
}
c.byteBuffer = c.inboundBuffer.WithByteBuffer(c.buffer)
return c.byteBuffer.Bytes()
return c.byteBuffer.B
}

func (c *conn) ResetBuffer() {
Expand All @@ -197,13 +197,13 @@ func (c *conn) ReadN(n int) (size int, buf []byte) {
_, _ = c.byteBuffer.Write(head)
_, _ = c.byteBuffer.Write(tail)
if inBufferLen >= n {
buf = c.byteBuffer.Bytes()
buf = c.byteBuffer.B
return
}

restSize := n - inBufferLen
_, _ = c.byteBuffer.Write(c.buffer[:restSize])
buf = c.byteBuffer.Bytes()
buf = c.byteBuffer.B
return
}

Expand Down
11 changes: 6 additions & 5 deletions gnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"math/rand"
"net"
"runtime"
"strings"
"sync/atomic"
"testing"
"time"
Expand Down Expand Up @@ -258,10 +259,9 @@ func testCodecServe(
network+"://"+addr,
WithMulticore(multicore),
WithTicker(true),
WithReadBufferCap(8*1024),
WithLogLevel(logging.DebugLevel),
WithTCPKeepAlive(
time.Minute*5,
),
WithTCPKeepAlive(time.Minute*5),
WithSocketRecvBuffer(8*1024),
WithSocketSendBuffer(8*1024),
WithCodec(codec),
Expand All @@ -283,8 +283,9 @@ func startCodecClient(t *testing.T, network, addr string, multicore, async bool,
start := time.Now()
for time.Since(start) < duration {
// data := []byte("Hello, World")
data := make([]byte, 1024)
rand.Read(data)
//data := make([]byte, 1024)
//rand.Read(data)
data := []byte(strings.Repeat("x", 1024))
reqData, _ := codec.Encode(nil, data)
_, err = c.Write(reqData)
require.NoError(t, err)
Expand Down

0 comments on commit a56d2f3

Please sign in to comment.