nps/lib/mux/mux.go

245 lines
5.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"
2019-02-26 14:40:28 +00:00
"github.com/cnlh/nps/lib/pool"
2019-08-23 10:53:36 +00:00
"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()
2019-08-26 10:47:23 +00:00
//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)
2019-08-23 10:53:36 +00:00
if err := s.sendInfo(common.MUX_NEW_CONN, conn.connId, nil); err != nil {
2019-02-26 14:40:28 +00:00
return nil, err
}
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 {
2019-02-26 14:40:28 +00:00
return nil, errors.New("accpet error,the conn has closed")
}
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()
}
2019-08-23 10:53:36 +00:00
func (s *Mux) sendInfo(flag uint8, id int32, content []byte) (err error) {
2019-08-26 10:47:23 +00:00
if flag == common.MUX_NEW_MSG {
}
2019-08-23 10:53:36 +00:00
buf := pool.BuffPool.Get()
pack := common.MuxPackager{}
err = pack.NewPac(flag, id, content)
if err != nil {
s.Close()
logs.Warn("new pack err", err)
return
2019-03-15 06:03:49 +00:00
}
2019-08-23 10:53:36 +00:00
err = pack.Pack(buf)
if err != nil {
2019-03-15 06:03:49 +00:00
s.Close()
2019-08-23 10:53:36 +00:00
logs.Warn("pack err", err)
return
2019-03-15 06:03:49 +00:00
}
2019-08-26 10:47:23 +00:00
//s.writeQueue <- buf
2019-08-23 10:53:36 +00:00
_, err = buf.WriteTo(s.conn)
if err != nil {
s.Close()
logs.Warn("write err", err)
}
2019-08-26 10:47:23 +00:00
pool.BuffPool.Put(buf)
if flag == common.MUX_CONN_CLOSE {
}
if flag == common.MUX_NEW_MSG {
}
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)
pool.BuffPool.Put(buf)
if err != nil || int(n) != l {
logs.Warn("close from write to ", err, n, l)
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
}
2019-08-23 10:53:36 +00:00
if err := s.sendInfo(common.MUX_PING_FLAG, common.MUX_PING, nil); err != nil || (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() {
2019-08-23 10:53:36 +00:00
var pack common.MuxPackager
2019-02-26 14:40:28 +00:00
go func() {
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 {
if pack.Length>10 {
logs.Warn(pack.Flag, pack.Id, pack.Length,string(pack.Content[:10]))
}
}
2019-08-23 10:53:36 +00:00
s.pingOk = 0
switch pack.Flag {
case common.MUX_NEW_CONN: //new conn
2019-08-26 10:47:23 +00:00
//logs.Warn("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)
continue
case common.MUX_PING_FLAG: //ping
s.sendInfo(common.MUX_PING_RETURN, common.MUX_PING, nil)
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
2019-08-26 10:47:23 +00:00
conn.readQueue.Push(NewBufNode(pack.Content, 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
}
2019-08-23 10:53:36 +00:00
case common.MUX_NEW_CONN_OK: //conn ok
conn.connStatusOkCh <- struct{}{}
case common.MUX_NEW_CONN_Fail:
conn.connStatusFailCh <- struct{}{}
case common.MUX_CONN_CLOSE: //close the connection
2019-08-26 10:47:23 +00:00
conn.readQueue.Push(NewBufNode(nil, 0))
if conn.readWait {
logs.Warn("close read wait", pack.Id)
conn.readWait = false
conn.readCh <- struct{}{}
}
2019-08-23 10:53:36 +00:00
s.connMap.Delete(pack.Id)
2019-02-26 14:40:28 +00:00
}
2019-08-23 10:53:36 +00:00
} else if pack.Flag == common.MUX_NEW_MSG {
pool.PutBufPoolCopy(pack.Content)
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{}{}:
}
2019-08-26 10:47:23 +00:00
s.closeChan <- struct{}{}
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
}