Change pool, change mux connection close ,change copy buffer

This commit is contained in:
ffdfgdfg
2019-08-30 20:05:09 +08:00
parent 41c282b38b
commit bb2cffe10a
11 changed files with 257 additions and 123 deletions

View File

@@ -4,7 +4,6 @@ import (
"bytes"
"errors"
"github.com/cnlh/nps/lib/common"
"github.com/cnlh/nps/lib/pool"
"github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
"math"
"net"
@@ -42,7 +41,7 @@ func NewMux(c net.Conn, connType string) *Mux {
go m.readSession()
//ping
go m.ping()
//go m.writeSession()
go m.writeSession()
return m
}
@@ -53,9 +52,8 @@ func (s *Mux) NewConn() (*conn, error) {
conn := NewConn(s.getId(), s)
//it must be set before send
s.connMap.Set(conn.connId, conn)
if err := s.sendInfo(common.MUX_NEW_CONN, conn.connId, nil); err != nil {
return nil, err
}
s.sendInfo(common.MUX_NEW_CONN, conn.connId, nil)
logs.Warn("send mux new conn ", conn.connId)
//set a timer timeout 30 second
timer := time.NewTimer(time.Minute * 2)
defer timer.Stop()
@@ -83,34 +81,41 @@ func (s *Mux) Addr() net.Addr {
return s.conn.LocalAddr()
}
func (s *Mux) sendInfo(flag uint8, id int32, content []byte) (err error) {
func (s *Mux) sendInfo(flag uint8, id int32, content []byte) {
var err error
if flag == common.MUX_NEW_MSG {
if len(content) == 0 {
logs.Warn("send info content is nil")
}
}
buf := pool.BuffPool.Get()
pack := common.MuxPackager{}
buf := common.BuffPool.Get()
//defer pool.BuffPool.Put(buf)
pack := common.MuxPack.Get()
err = pack.NewPac(flag, id, content)
if err != nil {
s.Close()
logs.Warn("new pack err", err)
common.BuffPool.Put(buf)
return
}
err = pack.Pack(buf)
if err != nil {
s.Close()
logs.Warn("pack err", err)
common.BuffPool.Put(buf)
return
}
//s.writeQueue <- buf
_, err = buf.WriteTo(s.conn)
if err != nil {
s.Close()
logs.Warn("write err", err)
}
pool.BuffPool.Put(buf)
if flag == common.MUX_CONN_CLOSE {
}
if flag == common.MUX_NEW_MSG {
}
s.writeQueue <- buf
common.MuxPack.Put(pack)
//_, err = buf.WriteTo(s.conn)
//if err != nil {
// s.Close()
// logs.Warn("write err, close mux", err)
//}
//if flag == common.MUX_CONN_CLOSE {
//}
//if flag == common.MUX_NEW_MSG {
//}
return
}
@@ -120,7 +125,7 @@ func (s *Mux) writeSession() {
buf := <-s.writeQueue
l := buf.Len()
n, err := buf.WriteTo(s.conn)
pool.BuffPool.Put(buf)
common.BuffPool.Put(buf)
if err != nil || int(n) != l {
logs.Warn("close from write to ", err, n, l)
s.Close()
@@ -142,7 +147,9 @@ func (s *Mux) ping() {
if (math.MaxInt32 - s.id) < 10000 {
s.id = 0
}
if err := s.sendInfo(common.MUX_PING_FLAG, common.MUX_PING, nil); err != nil || (s.pingOk > 10 && s.connType == "kcp") {
//logs.Warn("send mux ping")
s.sendInfo(common.MUX_PING_FLAG, common.MUX_PING, nil)
if s.pingOk > 10 && s.connType == "kcp" {
s.Close()
break
}
@@ -155,28 +162,30 @@ func (s *Mux) ping() {
}
func (s *Mux) readSession() {
var pack common.MuxPackager
go func() {
for {
pack := common.MuxPack.Get()
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]))
//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
//logs.Warn("mux new conn", pack.Id)
logs.Warn("mux new conn", pack.Id)
conn := NewConn(pack.Id, s)
s.connMap.Set(pack.Id, conn) //it has been set before send ok
s.newConnCh <- conn
s.sendInfo(common.MUX_NEW_CONN_OK, pack.Id, nil)
logs.Warn("send mux new conn ok", pack.Id)
continue
case common.MUX_PING_FLAG: //ping
s.sendInfo(common.MUX_PING_RETURN, common.MUX_PING, nil)
//logs.Warn("send mux ping return")
go s.sendInfo(common.MUX_PING_RETURN, common.MUX_PING, nil)
continue
case common.MUX_PING_RETURN:
continue
@@ -185,6 +194,7 @@ func (s *Mux) readSession() {
switch pack.Flag {
case common.MUX_NEW_MSG: //new msg from remote conn
//insert wait queue
logs.Warn("mux new msg ", pack.Id)
conn.readQueue.Push(NewBufNode(pack.Content, int(pack.Length)))
//judge len if >xxx ,send stop
if conn.readWait {
@@ -192,21 +202,29 @@ func (s *Mux) readSession() {
conn.readCh <- struct{}{}
}
case common.MUX_NEW_CONN_OK: //conn ok
logs.Warn("mux new conn ok ", pack.Id)
conn.connStatusOkCh <- struct{}{}
case common.MUX_NEW_CONN_Fail:
logs.Warn("mux new conn fail", pack.Id)
conn.connStatusFailCh <- struct{}{}
case common.MUX_CONN_CLOSE: //close the connection
logs.Warn("mux conn close", pack.Id)
s.connMap.Delete(pack.Id)
conn.writeClose = true
conn.readQueue.Push(NewBufNode(nil, 0))
if conn.readWait {
logs.Warn("close read wait", pack.Id)
conn.readWait = false
conn.readCh <- struct{}{}
}
s.connMap.Delete(pack.Id)
logs.Warn("receive mux conn close, finish", conn.connId)
}
} else if pack.Flag == common.MUX_NEW_MSG {
pool.CopyBuff.Put(pack.Content)
common.CopyBuff.Put(pack.Content)
} else if pack.Flag == common.MUX_CONN_CLOSE {
logs.Warn("mux conn close no id ", pack.Id)
}
common.MuxPack.Put(pack)
}
s.Close()
}()
@@ -228,7 +246,7 @@ func (s *Mux) Close() error {
select {
case s.closeChan <- struct{}{}:
}
//s.closeChan <- struct{}{}
s.closeChan <- struct{}{}
close(s.writeQueue)
close(s.newConnCh)
return s.conn.Close()