mirror of
https://github.com/ehang-io/nps.git
synced 2025-09-02 11:56:53 +00:00
no message
This commit is contained in:
@@ -46,7 +46,6 @@ func NewConn(connId int32, mux *Mux) *conn {
|
||||
}
|
||||
|
||||
func (s *conn) Read(buf []byte) (n int, err error) {
|
||||
logs.Warn("starting read ", s.connId)
|
||||
if s.isClose || buf == nil {
|
||||
return 0, errors.New("the conn has closed")
|
||||
}
|
||||
@@ -73,18 +72,16 @@ func (s *conn) Read(buf []byte) (n int, err error) {
|
||||
s.Close()
|
||||
return 0, io.EOF
|
||||
} else {
|
||||
pool.PutBufPoolCopy(s.readBuffer)
|
||||
//pool.PutBufPoolCopy(s.readBuffer)
|
||||
if node.val == nil {
|
||||
//close
|
||||
s.sendClose = true
|
||||
s.Close()
|
||||
logs.Warn("close from read msg ", s.connId)
|
||||
return 0, io.EOF
|
||||
} else {
|
||||
s.readBuffer = node.val
|
||||
s.endRead = node.l
|
||||
s.startRead = 0
|
||||
logs.Warn("get a new data buffer ", s.connId)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -95,12 +92,10 @@ func (s *conn) Read(buf []byte) (n int, err error) {
|
||||
n = copy(buf, s.readBuffer[s.startRead:s.endRead])
|
||||
s.startRead += n
|
||||
}
|
||||
logs.Warn("end read ", s.connId)
|
||||
return
|
||||
}
|
||||
|
||||
func (s *conn) Write(buf []byte) (n int, err error) {
|
||||
logs.Warn("trying write", s.connId)
|
||||
if s.isClose {
|
||||
return 0, errors.New("the conn has closed")
|
||||
}
|
||||
@@ -120,7 +115,6 @@ func (s *conn) Write(buf []byte) (n int, err error) {
|
||||
if s.isClose {
|
||||
return 0, io.EOF
|
||||
}
|
||||
logs.Warn("write success ", s.connId)
|
||||
return len(buf), nil
|
||||
}
|
||||
func (s *conn) write(buf []byte, ch chan struct{}) {
|
||||
@@ -139,9 +133,7 @@ func (s *conn) write(buf []byte, ch chan struct{}) {
|
||||
}
|
||||
|
||||
func (s *conn) Close() (err error) {
|
||||
logs.Warn("start closing ", s.connId)
|
||||
if s.isClose {
|
||||
logs.Warn("already closed", s.connId)
|
||||
return errors.New("the conn has closed")
|
||||
}
|
||||
s.isClose = true
|
||||
@@ -153,7 +145,6 @@ func (s *conn) Close() (err error) {
|
||||
s.mux.connMap.Delete(s.connId)
|
||||
if !s.mux.IsClose {
|
||||
if !s.sendClose {
|
||||
logs.Warn("start send closing msg", s.connId)
|
||||
err = s.mux.sendInfo(common.MUX_CONN_CLOSE, s.connId, nil)
|
||||
logs.Warn("send closing msg ok ", s.connId)
|
||||
if err != nil {
|
||||
@@ -161,7 +152,6 @@ func (s *conn) Close() (err error) {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
logs.Warn("send mux conn close pass ", s.connId)
|
||||
}
|
||||
}
|
||||
return
|
||||
|
@@ -85,7 +85,6 @@ func (s *Mux) Addr() net.Addr {
|
||||
|
||||
func (s *Mux) sendInfo(flag uint8, id int32, content []byte) (err error) {
|
||||
if flag == common.MUX_NEW_MSG {
|
||||
logs.Warn("trying write to mux new msg", id)
|
||||
}
|
||||
buf := pool.BuffPool.Get()
|
||||
pack := common.MuxPackager{}
|
||||
@@ -109,10 +108,8 @@ func (s *Mux) sendInfo(flag uint8, id int32, content []byte) (err error) {
|
||||
}
|
||||
pool.BuffPool.Put(buf)
|
||||
if flag == common.MUX_CONN_CLOSE {
|
||||
logs.Warn("write to mux conn close success", id)
|
||||
}
|
||||
if flag == common.MUX_NEW_MSG {
|
||||
logs.Warn("write to mux new msg success", id)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -164,6 +161,11 @@ func (s *Mux) readSession() {
|
||||
if pack.UnPack(s.conn) != nil {
|
||||
break
|
||||
}
|
||||
if pack.Flag != 0 && pack.Flag != 7 {
|
||||
if pack.Length>10 {
|
||||
logs.Warn(pack.Flag, pack.Id, pack.Length,string(pack.Content[:10]))
|
||||
}
|
||||
}
|
||||
s.pingOk = 0
|
||||
switch pack.Flag {
|
||||
case common.MUX_NEW_CONN: //new conn
|
||||
@@ -180,7 +182,6 @@ func (s *Mux) readSession() {
|
||||
continue
|
||||
}
|
||||
if conn, ok := s.connMap.Get(pack.Id); ok && !conn.isClose {
|
||||
logs.Warn("read session flag id", pack.Flag, pack.Id)
|
||||
switch pack.Flag {
|
||||
case common.MUX_NEW_MSG: //new msg from remote conn
|
||||
//insert wait queue
|
||||
@@ -190,7 +191,6 @@ func (s *Mux) readSession() {
|
||||
conn.readWait = false
|
||||
conn.readCh <- struct{}{}
|
||||
}
|
||||
logs.Warn("push a read buffer ", conn.connId, pack.Id)
|
||||
case common.MUX_NEW_CONN_OK: //conn ok
|
||||
conn.connStatusOkCh <- struct{}{}
|
||||
case common.MUX_NEW_CONN_Fail:
|
||||
@@ -203,7 +203,6 @@ func (s *Mux) readSession() {
|
||||
conn.readCh <- struct{}{}
|
||||
}
|
||||
s.connMap.Delete(pack.Id)
|
||||
logs.Warn("read session mux conn close finish", pack.Id)
|
||||
}
|
||||
} else if pack.Flag == common.MUX_NEW_MSG {
|
||||
pool.PutBufPoolCopy(pack.Content)
|
||||
|
@@ -26,25 +26,20 @@ func TestNewMux(t *testing.T) {
|
||||
time.Sleep(time.Second * 3)
|
||||
go func() {
|
||||
m2 := NewMux(conn2, "tcp")
|
||||
connCh := make(chan bool, 1)
|
||||
for {
|
||||
c, err := m2.Accept()
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
connCh <- true
|
||||
go func(c net.Conn, ch chan bool) {
|
||||
c2, err := net.Dial("tcp", "127.0.0.1:80")
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
go common.CopyBuffer(c2, c)
|
||||
common.CopyBuffer(c, c2)
|
||||
c2.Close()
|
||||
c.Close()
|
||||
logs.Warn("close npc")
|
||||
<-ch
|
||||
}(c, connCh)
|
||||
c2, err := net.Dial("tcp", "127.0.0.1:8080")
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
go common.CopyBuffer(c2, c,0)
|
||||
common.CopyBuffer(c, c2,0)
|
||||
c2.Close()
|
||||
c.Close()
|
||||
logs.Warn("close npc")
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -54,25 +49,22 @@ func TestNewMux(t *testing.T) {
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
connCh := make(chan bool, 1)
|
||||
for {
|
||||
conn, err := l.Accept()
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
connCh <- true
|
||||
go func(conn net.Conn, ch chan bool) {
|
||||
tmpCpnn, err := m1.NewConn()
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
go common.CopyBuffer(tmpCpnn, conn)
|
||||
common.CopyBuffer(conn, tmpCpnn)
|
||||
conn.Close()
|
||||
//tmpCpnn.Close()
|
||||
logs.Warn("close from out nps ", tmpCpnn.connId)
|
||||
<-ch
|
||||
}(conn, connCh)
|
||||
|
||||
tmpCpnn, err := m1.NewConn()
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
go common.CopyBuffer(tmpCpnn, conn,tmpCpnn.connId)
|
||||
_, err = common.CopyBuffer(conn, tmpCpnn,tmpCpnn.connId)
|
||||
logs.Warn(err, tmpCpnn.connId)
|
||||
conn.Close()
|
||||
tmpCpnn.Close()
|
||||
logs.Warn("close from out nps ", tmpCpnn.connId)
|
||||
}
|
||||
}()
|
||||
|
||||
|
Reference in New Issue
Block a user