Skip to content

Commit

Permalink
fix: tcp socket can not detect remote server shutdown problem
Browse files Browse the repository at this point in the history
  • Loading branch information
lory committed Mar 22, 2024
1 parent aa38b18 commit 5f29740
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@

# Go workspace file
go.work
/examples/tcp/server/server
/examples/tcp/client/client
/examples/udp/server/server
/examples/udp/client/client
/examples/unix/server/server
/examples/unix/client/client
/examples/websocket/server/server
/examples/websocket/client/client
4 changes: 3 additions & 1 deletion socketx_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/civet148/socketx/api"
"github.com/civet148/socketx/types"
"sync"
"time"
)

type SocketHandler interface {
Expand Down Expand Up @@ -136,8 +137,9 @@ func (w *SocketServer) recvSocket(s api.Socket) (msg *api.SockMessage, err error

func (w *SocketServer) onAccept(s api.Socket) {
c := w.addClient(s)
go w.readSocket(s)
w.handler.OnAccept(c)
time.Sleep(100 * time.Millisecond)
go w.readSocket(s)
}

func (w *SocketServer) onClose(s api.Socket) {
Expand Down
11 changes: 0 additions & 11 deletions tcpsock/tcp_socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import (
"github.com/civet148/log"
"github.com/civet148/socketx/api"
"github.com/civet148/socketx/types"
"io"
"net"
"sync"
"time"
)

type socket struct {
Expand Down Expand Up @@ -95,24 +93,15 @@ func (s *socket) Recv(length int) (msg *api.SockMessage, err error) {
left = length
data := s.makeBuffer(length)

DATA_RECV:
var n int
if once {
if n, err = s.conn.Read(data); err != nil {
if err == io.EOF {
time.Sleep(time.Millisecond)
goto DATA_RECV
}
return nil, log.Errorf("read data from %s error [%v]", s.GetRemoteAddr(), err.Error())
}
recv = n
} else {
for left > 0 {
if n, err = s.conn.Read(data[recv:]); err != nil {
if err == io.EOF {
time.Sleep(time.Millisecond)
goto DATA_RECV
}
return nil, log.Errorf("read data from %s error [%v]", s.GetRemoteAddr(), err.Error())
}
left -= n
Expand Down

0 comments on commit 5f29740

Please sign in to comment.