nps/lib/mux/mux.go

292 lines
7.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()
2019-09-01 16:18:52 +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)
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()
}
2019-09-08 15:49:16 +00:00
func (s *Mux) sendInfo(flag uint8, id int32, data interface{}) {
var err error
2019-08-26 10:47:23 +00:00
if flag == common.MUX_NEW_MSG {
2019-09-08 15:49:16 +00:00
if len(data.([]byte)) == 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-09-08 15:49:16 +00:00
err = pack.NewPac(flag, id, data)
2019-08-23 10:53:36 +00:00
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)
}
2019-09-01 16:18:52 +00:00
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-09-08 15:49:16 +00:00
pack = common.MuxPack.Get()
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 {
2019-09-08 15:49:16 +00:00
logs.Warn("unpack mux new connection", pack.Id)
}
2019-08-23 10:53:36 +00:00
s.pingOk = 0
switch pack.Flag {
2019-09-08 15:49:16 +00:00
case common.MUX_NEW_CONN: //new connection
logs.Warn("rec mux new connection", 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
2019-09-08 15:49:16 +00:00
go conn.sendWindow.SetAllowSize(512) // set the initial receive window
2019-08-23 10:53:36 +00:00
s.sendInfo(common.MUX_NEW_CONN_OK, pack.Id, nil)
2019-09-08 15:49:16 +00:00
logs.Warn("send mux new connection 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
}
2019-09-08 15:49:16 +00:00
if connection, ok := s.connMap.Get(pack.Id); ok && !connection.isClose {
2019-08-23 10:53:36 +00:00
switch pack.Flag {
2019-09-08 15:49:16 +00:00
case common.MUX_NEW_MSG: //new msg from remote connection
2019-08-23 10:53:36 +00:00
//insert wait queue
2019-09-08 15:49:16 +00:00
if connection.isClose {
logs.Warn("rec mux new msg closed", pack.Id, string(pack.Content[0:15]))
continue
2019-02-26 14:40:28 +00:00
}
2019-09-08 15:49:16 +00:00
connection.receiveWindow.WriteWg.Add(1)
logs.Warn("rec mux new msg ", pack.Id, string(pack.Content[0:15]))
go func(connection *conn, content []byte) { // do not block read session
_, err := connection.receiveWindow.Write(content)
if err != nil {
logs.Warn("mux new msg err close", err)
s.Close()
}
size := connection.receiveWindow.Size()
if size == 0 {
connection.receiveWindow.WindowFull = true
}
s.sendInfo(common.MUX_MSG_SEND_OK, connection.connId, size)
logs.Warn("send mux new msg ok", pack.Id, size)
connection.receiveWindow.WriteWg.Done()
}(connection, pack.Content)
continue
2019-09-08 15:49:16 +00:00
case common.MUX_NEW_CONN_OK: //connection ok
logs.Warn("rec mux new connection ok ", pack.Id)
connection.connStatusOkCh <- struct{}{}
go connection.sendWindow.SetAllowSize(512)
// set the initial receive window both side
continue
2019-08-23 10:53:36 +00:00
case common.MUX_NEW_CONN_Fail:
2019-09-08 15:49:16 +00:00
logs.Warn("rec mux new connection fail", pack.Id)
connection.connStatusFailCh <- struct{}{}
continue
case common.MUX_MSG_SEND_OK:
if connection.isClose {
logs.Warn("rec mux msg send ok id window closed!", pack.Id, pack.Window)
continue
}
logs.Warn("rec mux msg send ok id window", pack.Id, pack.Window)
go connection.sendWindow.SetAllowSize(pack.Window)
continue
2019-08-23 10:53:36 +00:00
case common.MUX_CONN_CLOSE: //close the connection
2019-09-08 15:49:16 +00:00
logs.Warn("rec mux connection close", pack.Id)
s.connMap.Delete(pack.Id)
2019-09-08 15:49:16 +00:00
connection.closeFlag = true
go func(connection *conn) {
connection.receiveWindow.WriteWg.Wait()
connection.receiveWindow.WriteEndOp <- struct{}{} // close signal to receive window
logs.Warn("receive mux connection close, finish", connection.connId)
}(connection)
continue
2019-02-26 14:40:28 +00:00
}
} else if pack.Flag == common.MUX_CONN_CLOSE {
2019-09-08 15:49:16 +00:00
logs.Warn("rec mux connection close no id ", pack.Id)
continue
2019-02-26 14:40:28 +00:00
}
2019-09-08 15:49:16 +00:00
common.MuxPack.Put(pack)
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
}