Skip to content

Commit

Permalink
bug: return io.EOF in Read(), comply with io.Reader and io.Writer
Browse files Browse the repository at this point in the history
Fixes #391
  • Loading branch information
panjf2000 committed Aug 9, 2022
1 parent f2e2fa3 commit 3c3c519
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 6 additions & 3 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,10 @@ func (c *conn) Read(p []byte) (n int, err error) {
if c.inboundBuffer.IsEmpty() {
n = copy(p, c.buffer)
c.buffer = c.buffer[n:]
return n, nil
if n == 0 && len(p) > 0 {
err = io.EOF
}
return
}
n, _ = c.inboundBuffer.Read(p)
if n == len(p) {
Expand Down Expand Up @@ -353,7 +356,7 @@ func (c *conn) Discard(n int) (int, error) {
func (c *conn) Write(p []byte) (int, error) {
if c.isDatagram {
if err := c.sendTo(p); err != nil {
return -1, err
return 0, err
}
return len(p), nil
}
Expand All @@ -362,7 +365,7 @@ func (c *conn) Write(p []byte) (int, error) {

func (c *conn) Writev(bs [][]byte) (int, error) {
if c.isDatagram {
return -1, gerrors.ErrUnsupportedOp
return 0, gerrors.ErrUnsupportedOp
}
return c.writev(bs)
}
Expand Down
2 changes: 1 addition & 1 deletion gnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ func testStop(t *testing.T, network, addr string) {
func TestClosedWakeUp(t *testing.T) {
events := &testClosedWakeUpServer{
tester: t,
BuiltinEventEngine: &BuiltinEventEngine{}, network: "tcp", addr: ":8888", protoAddr: "tcp://:8888",
BuiltinEventEngine: &BuiltinEventEngine{}, network: "tcp", addr: ":9998", protoAddr: "tcp://:9998",
clientClosed: make(chan struct{}),
serverClosed: make(chan struct{}),
wakeup: make(chan struct{}),
Expand Down

0 comments on commit 3c3c519

Please sign in to comment.