nps/lib/mux/mux.go

274 lines
6.1 KiB
Go
Raw Normal View History

2019-02-26 14:40:28 +00:00
package mux
import (
2019-08-26 10:47:23 +00:00
"bytes"
2019-02-26 14:40:28 +00:00
"errors"
2019-08-23 10:53:36 +00:00
"github.com/cnlh/nps/lib/common"
"github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
2019-02-26 14:40:28 +00:00
"math"
"net"
"sync"
"sync/atomic"
"time"
)
type Mux struct {
net.Listener
2019-08-23 10:53:36 +00:00
conn net.Conn
connMap *connMap
newConnCh chan *conn
id int32
closeChan chan struct{}
IsClose bool
pingOk int
connType string
2019-08-26 10:47:23 +00:00
writeQueue chan *bytes.Buffer
2019-02-26 14:40:28 +00:00
sync.Mutex
}
2019-03-19 14:41:40 +00:00
func NewMux(c net.Conn, connType string) *Mux {
2019-02-26 14:40:28 +00:00
m := &Mux{
2019-08-23 10:53:36 +00:00
conn: c,
connMap: NewConnMap(),
id: 0,
closeChan: make(chan struct{}),
newConnCh: make(chan *conn),
IsClose: false,
connType: connType,
2019-08-26 10:47:23 +00:00
writeQueue: make(chan *bytes.Buffer, 20),
2019-02-26 14:40:28 +00:00
}
//read session by flag
go m.readSession()
//ping
go m.ping()
//go m.writeSession()
2019-02-26 14:40:28 +00:00
return m
}
func (s *Mux) NewConn() (*conn, error) {
2019-03-02 09:43:21 +00:00
if s.IsClose {
2019-02-26 14:40:28 +00:00
return nil, errors.New("the mux has closed")
}
2019-03-15 06:03:49 +00:00
conn := NewConn(s.getId(), s)
2019-02-26 14:40:28 +00:00
//it must be set before send
s.connMap.Set(conn.connId, conn)
s.sendInfo(common.MUX_NEW_CONN, conn.connId, nil)
logs.Warn("send mux new conn ", conn.connId)
2019-03-15 06:03:49 +00:00
//set a timer timeout 30 second
2019-03-29 05:31:11 +00:00
timer := time.NewTimer(time.Minute * 2)
2019-03-15 06:03:49 +00:00
defer timer.Stop()
2019-02-26 14:40:28 +00:00
select {
case <-conn.connStatusOkCh:
return conn, nil
case <-conn.connStatusFailCh:
2019-03-15 06:03:49 +00:00
case <-timer.C:
2019-02-26 14:40:28 +00:00
}
return nil, errors.New("create connection failthe server refused the connection")
}
func (s *Mux) Accept() (net.Conn, error) {
2019-03-02 09:43:21 +00:00
if s.IsClose {
return nil, errors.New("accpet error,the mux has closed")
2019-02-26 14:40:28 +00:00
}
2019-03-19 14:41:40 +00:00
conn := <-s.newConnCh
if conn == nil {
return nil, errors.New("accpet error,the conn has closed")
}
return conn, nil
2019-02-26 14:40:28 +00:00
}
func (s *Mux) Addr() net.Addr {
return s.conn.LocalAddr()
}
func (s *Mux) sendInfo(flag uint8, id int32, content []byte) {
var err error
2019-08-26 10:47:23 +00:00
if flag == common.MUX_NEW_MSG {
if len(content) == 0 {
logs.Warn("send info content is nil")
}
2019-08-26 10:47:23 +00:00
}
buf := common.BuffPool.Get()
//defer pool.BuffPool.Put(buf)
pack := common.MuxPack.Get()
defer common.MuxPack.Put(pack)
2019-08-23 10:53:36 +00:00
err = pack.NewPac(flag, id, content)
if err != nil {
logs.Warn("new pack err", err)
common.BuffPool.Put(buf)
2019-08-23 10:53:36 +00:00
return
2019-03-15 06:03:49 +00:00
}
2019-08-23 10:53:36 +00:00
err = pack.Pack(buf)
if err != nil {
logs.Warn("pack err", err)
common.BuffPool.Put(buf)
2019-08-23 10:53:36 +00:00
return
2019-03-15 06:03:49 +00:00
}
if pack.Flag == common.MUX_NEW_CONN {
logs.Warn("sendinfo mux new conn, insert to write queue", pack.Id)
}
//s.writeQueue <- buf
_, err = buf.WriteTo(s.conn)
if err != nil {
s.Close()
logs.Warn("write err, close mux", err)
}
common.BuffPool.Put(buf)
2019-08-23 10:53:36 +00:00
return
2019-03-15 06:03:49 +00:00
}
2019-08-26 10:47:23 +00:00
func (s *Mux) writeSession() {
go func() {
for {
buf := <-s.writeQueue
l := buf.Len()
n, err := buf.WriteTo(s.conn)
common.BuffPool.Put(buf)
2019-08-26 10:47:23 +00:00
if err != nil || int(n) != l {
logs.Warn("close from write session fail ", err, n, l)
2019-08-26 10:47:23 +00:00
s.Close()
break
}
}
}()
<-s.closeChan
}
2019-02-26 14:40:28 +00:00
func (s *Mux) ping() {
go func() {
2019-03-18 06:18:58 +00:00
ticker := time.NewTicker(time.Second * 1)
2019-02-26 14:40:28 +00:00
for {
select {
case <-ticker.C:
}
//Avoid going beyond the scope
if (math.MaxInt32 - s.id) < 10000 {
s.id = 0
}
//logs.Warn("send mux ping")
s.sendInfo(common.MUX_PING_FLAG, common.MUX_PING, nil)
if s.pingOk > 10 && s.connType == "kcp" {
2019-03-18 06:18:58 +00:00
s.Close()
2019-02-26 14:40:28 +00:00
break
}
2019-03-19 14:41:40 +00:00
s.pingOk++
2019-02-26 14:40:28 +00:00
}
}()
select {
case <-s.closeChan:
}
}
func (s *Mux) readSession() {
go func() {
pack := common.MuxPack.Get()
2019-02-26 14:40:28 +00:00
for {
2019-08-23 10:53:36 +00:00
if pack.UnPack(s.conn) != nil {
break
}
2019-08-26 13:42:20 +00:00
if pack.Flag != 0 && pack.Flag != 7 {
2019-08-26 15:29:22 +00:00
if pack.Length > 10 {
//logs.Warn(pack.Flag, pack.Id, pack.Length, string(pack.Content[:10]))
2019-08-26 13:42:20 +00:00
}
}
if pack.Flag == common.MUX_NEW_CONN {
logs.Warn("unpack mux new conn", pack.Id)
}
2019-08-23 10:53:36 +00:00
s.pingOk = 0
switch pack.Flag {
case common.MUX_NEW_CONN: //new conn
logs.Warn("rec mux new conn", pack.Id)
2019-08-23 10:53:36 +00:00
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)
2019-08-23 10:53:36 +00:00
continue
case common.MUX_PING_FLAG: //ping
//logs.Warn("send mux ping return")
go s.sendInfo(common.MUX_PING_RETURN, common.MUX_PING, nil)
2019-08-23 10:53:36 +00:00
continue
case common.MUX_PING_RETURN:
continue
}
if conn, ok := s.connMap.Get(pack.Id); ok && !conn.isClose {
switch pack.Flag {
case common.MUX_NEW_MSG: //new msg from remote conn
//insert wait queue
buf := common.CopyBuff.Get()
buf = pack.Content
logs.Warn("rec mux new msg ", pack.Id, string(buf[0:15]))
conn.readQueue.Push(NewBufNode(buf, int(pack.Length)))
2019-08-23 10:53:36 +00:00
//judge len if >xxx ,send stop
if conn.readWait {
conn.readWait = false
conn.readCh <- struct{}{}
2019-02-26 14:40:28 +00:00
}
continue
2019-08-23 10:53:36 +00:00
case common.MUX_NEW_CONN_OK: //conn ok
logs.Warn("rec mux new conn ok ", pack.Id)
2019-08-23 10:53:36 +00:00
conn.connStatusOkCh <- struct{}{}
continue
2019-08-23 10:53:36 +00:00
case common.MUX_NEW_CONN_Fail:
logs.Warn("rec mux new conn fail", pack.Id)
2019-08-23 10:53:36 +00:00
conn.connStatusFailCh <- struct{}{}
continue
2019-08-23 10:53:36 +00:00
case common.MUX_CONN_CLOSE: //close the connection
logs.Warn("rec mux conn close", pack.Id)
s.connMap.Delete(pack.Id)
conn.closeFlag = true
conn.sendClose = true
if !conn.isClose {
conn.readQueue.Push(NewBufNode(nil, 0))
if conn.readWait {
logs.Warn("mux conn close read wait", pack.Id)
conn.readWait = false
conn.readCh <- struct{}{}
logs.Warn("mux conn close read wait pass", pack.Id)
}
2019-08-26 10:47:23 +00:00
}
logs.Warn("receive mux conn close, finish", conn.connId)
continue
2019-02-26 14:40:28 +00:00
}
} else if pack.Flag == common.MUX_CONN_CLOSE {
logs.Warn("rec mux conn close no id ", pack.Id)
continue
2019-02-26 14:40:28 +00:00
}
}
common.MuxPack.Put(pack)
logs.Warn("read session put pack ", pack.Id)
2019-02-26 14:40:28 +00:00
s.Close()
}()
select {
case <-s.closeChan:
}
}
func (s *Mux) Close() error {
2019-08-23 10:53:36 +00:00
logs.Warn("close mux")
2019-03-02 09:43:21 +00:00
if s.IsClose {
2019-02-26 14:40:28 +00:00
return errors.New("the mux has closed")
}
2019-03-02 09:43:21 +00:00
s.IsClose = true
2019-02-26 14:40:28 +00:00
s.connMap.Close()
2019-03-18 06:18:58 +00:00
select {
case s.closeChan <- struct{}{}:
}
select {
case s.closeChan <- struct{}{}:
}
s.closeChan <- struct{}{}
2019-08-26 10:47:23 +00:00
close(s.writeQueue)
2019-03-19 14:41:40 +00:00
close(s.newConnCh)
2019-02-26 14:40:28 +00:00
return s.conn.Close()
}
//get new connId as unique flag
2019-08-23 10:53:36 +00:00
func (s *Mux) getId() (id int32) {
id = atomic.AddInt32(&s.id, 1)
if _, ok := s.connMap.Get(id); ok {
s.getId()
}
return
2019-02-26 14:40:28 +00:00
}