nps/lib/mux/mux.go

221 lines
4.5 KiB
Go
Raw Normal View History

2019-02-26 14:40:28 +00:00
package mux
import (
"bytes"
"encoding/binary"
"errors"
"math"
"net"
"sync"
"sync/atomic"
"time"
2019-08-10 03:15:25 +00:00
"github.com/cnlh/nps/lib/pool"
2019-02-26 14:40:28 +00:00
)
const (
MUX_PING_FLAG int32 = iota
MUX_NEW_CONN_OK
MUX_NEW_CONN_Fail
MUX_NEW_MSG
MUX_MSG_SEND_OK
MUX_NEW_CONN
MUX_PING
MUX_CONN_CLOSE
2019-03-02 09:43:21 +00:00
MUX_PING_RETURN
2019-02-26 14:40:28 +00:00
)
type Mux struct {
net.Listener
2019-03-18 06:18:58 +00:00
conn net.Conn
connMap *connMap
newConnCh chan *conn
id int32
closeChan chan struct{}
IsClose bool
2019-03-19 14:41:40 +00:00
pingOk int
connType string
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-03-18 06:18:58 +00:00
conn: c,
connMap: NewConnMap(),
id: 0,
closeChan: make(chan struct{}),
newConnCh: make(chan *conn),
IsClose: false,
2019-03-19 14:41:40 +00:00
connType: connType,
2019-02-26 14:40:28 +00:00
}
//read session by flag
go m.readSession()
//ping
go m.ping()
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-03-29 02:41:57 +00:00
if err := s.sendInfo(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-03-15 06:03:49 +00:00
func (s *Mux) sendInfo(flag int32, id int32, content []byte) error {
raw := bytes.NewBuffer([]byte{})
binary.Write(raw, binary.LittleEndian, flag)
binary.Write(raw, binary.LittleEndian, id)
if content != nil && len(content) > 0 {
binary.Write(raw, binary.LittleEndian, int32(len(content)))
binary.Write(raw, binary.LittleEndian, content)
}
2019-03-18 06:18:58 +00:00
if _, err := s.conn.Write(raw.Bytes()); err != nil {
2019-03-15 06:03:49 +00:00
s.Close()
return err
}
return nil
}
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-03-19 14:41:40 +00:00
if err := s.sendInfo(MUX_PING_FLAG, 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-03-15 06:03:49 +00:00
var buf []byte
2019-02-26 14:40:28 +00:00
go func() {
for {
var flag, i int32
var n int
var err error
2019-02-26 14:40:28 +00:00
if binary.Read(s.conn, binary.LittleEndian, &flag) == nil {
if binary.Read(s.conn, binary.LittleEndian, &i) != nil {
break
}
2019-03-19 14:41:40 +00:00
s.pingOk = 0
2019-02-26 14:40:28 +00:00
switch flag {
case MUX_NEW_CONN: //new conn
2019-03-15 06:03:49 +00:00
conn := NewConn(i, s)
2019-02-26 14:40:28 +00:00
s.connMap.Set(i, conn) //it has been set before send ok
s.newConnCh <- conn
2019-03-15 06:03:49 +00:00
s.sendInfo(MUX_NEW_CONN_OK, i, nil)
2019-02-26 14:40:28 +00:00
continue
case MUX_PING_FLAG: //ping
2019-03-15 06:03:49 +00:00
s.sendInfo(MUX_PING_RETURN, MUX_PING, nil)
2019-03-02 09:43:21 +00:00
continue
case MUX_PING_RETURN:
2019-02-26 14:40:28 +00:00
continue
case MUX_NEW_MSG:
2019-03-15 06:03:49 +00:00
buf = pool.GetBufPoolCopy()
if n, err = ReadLenBytes(buf, s.conn); err != nil {
break
}
2019-02-26 14:40:28 +00:00
}
if conn, ok := s.connMap.Get(i); ok && !conn.isClose {
2019-02-26 14:40:28 +00:00
switch flag {
case MUX_NEW_MSG: //new msg from remote conn
2019-03-15 06:03:49 +00:00
//insert wait queue
conn.waitQueue.Push(NewBufNode(buf, n))
//judge len if >xxx ,send stop
if conn.readWait {
2019-03-15 06:03:49 +00:00
conn.readWait = false
conn.readCh <- struct{}{}
2019-02-26 14:40:28 +00:00
}
case MUX_MSG_SEND_OK: //the remote has read
2019-03-18 06:18:58 +00:00
select {
case conn.getStatusCh <- struct{}{}:
default:
2019-03-15 06:03:49 +00:00
}
2019-08-10 03:15:25 +00:00
conn.hasWrite--
2019-02-26 14:40:28 +00:00
case MUX_NEW_CONN_OK: //conn ok
conn.connStatusOkCh <- struct{}{}
case MUX_NEW_CONN_Fail:
conn.connStatusFailCh <- struct{}{}
case MUX_CONN_CLOSE: //close the connection
2019-03-15 06:03:49 +00:00
go conn.Close()
s.connMap.Delete(i)
2019-02-26 14:40:28 +00:00
}
2019-03-15 06:03:49 +00:00
} else if flag == MUX_NEW_MSG {
pool.PutBufPoolCopy(buf)
2019-02-26 14:40:28 +00:00
}
} else {
break
}
}
s.Close()
}()
select {
case <-s.closeChan:
}
}
func (s *Mux) Close() error {
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-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
func (s *Mux) getId() int32 {
return atomic.AddInt32(&s.id, 1)
}