mirror of https://github.com/ehang-io/nps
mux test
parent
00900c1315
commit
f35a73f734
|
@ -45,13 +45,13 @@ func (Self *BasePackager) Pack(writer io.Writer) (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = binary.Write(writer, binary.LittleEndian, Self.Content)
|
err = binary.Write(writer, binary.LittleEndian, Self.Content)
|
||||||
//logs.Warn(Self.Length, string(Self.Content))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//Unpack 会导致传入的数字类型转化成float64!!
|
//Unpack 会导致传入的数字类型转化成float64!!
|
||||||
//主要原因是json unmarshal并未传入正确的数据类型
|
//主要原因是json unmarshal并未传入正确的数据类型
|
||||||
func (Self *BasePackager) UnPack(reader io.Reader) (err error) {
|
func (Self *BasePackager) UnPack(reader io.Reader) (err error) {
|
||||||
|
Self.clean()
|
||||||
err = binary.Read(reader, binary.LittleEndian, &Self.Length)
|
err = binary.Read(reader, binary.LittleEndian, &Self.Length)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"github.com/cnlh/nps/lib/crypt"
|
"github.com/cnlh/nps/lib/crypt"
|
||||||
"github.com/cnlh/nps/lib/pool"
|
"github.com/cnlh/nps/lib/pool"
|
||||||
|
"github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
@ -268,8 +269,10 @@ func CopyBuffer(dst io.Writer, src io.Reader) (written int64, err error) {
|
||||||
defer pool.PutBufPoolCopy(buf)
|
defer pool.PutBufPoolCopy(buf)
|
||||||
for {
|
for {
|
||||||
nr, er := src.Read(buf)
|
nr, er := src.Read(buf)
|
||||||
|
logs.Warn("read finish", nr, er)
|
||||||
if nr > 0 {
|
if nr > 0 {
|
||||||
nw, ew := dst.Write(buf[0:nr])
|
nw, ew := dst.Write(buf[0:nr])
|
||||||
|
logs.Warn("write finish", nw, ew)
|
||||||
if nw > 0 {
|
if nw > 0 {
|
||||||
written += int64(nw)
|
written += int64(nw)
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,11 +22,12 @@ type conn struct {
|
||||||
endRead int //now end read
|
endRead int //now end read
|
||||||
readFlag bool
|
readFlag bool
|
||||||
readCh chan struct{}
|
readCh chan struct{}
|
||||||
waitQueue *sliceEntry
|
readQueue *sliceEntry
|
||||||
stopWrite bool
|
stopWrite bool
|
||||||
connId int32
|
connId int32
|
||||||
isClose bool
|
isClose bool
|
||||||
readWait bool
|
readWait bool
|
||||||
|
sendClose bool
|
||||||
hasWrite int
|
hasWrite int
|
||||||
mux *Mux
|
mux *Mux
|
||||||
}
|
}
|
||||||
|
@ -37,7 +38,7 @@ func NewConn(connId int32, mux *Mux) *conn {
|
||||||
getStatusCh: make(chan struct{}),
|
getStatusCh: make(chan struct{}),
|
||||||
connStatusOkCh: make(chan struct{}),
|
connStatusOkCh: make(chan struct{}),
|
||||||
connStatusFailCh: make(chan struct{}),
|
connStatusFailCh: make(chan struct{}),
|
||||||
waitQueue: NewQueue(),
|
readQueue: NewQueue(),
|
||||||
connId: connId,
|
connId: connId,
|
||||||
mux: mux,
|
mux: mux,
|
||||||
}
|
}
|
||||||
|
@ -45,11 +46,12 @@ func NewConn(connId int32, mux *Mux) *conn {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *conn) Read(buf []byte) (n int, err error) {
|
func (s *conn) Read(buf []byte) (n int, err error) {
|
||||||
|
logs.Warn("starting read ", s.connId)
|
||||||
if s.isClose || buf == nil {
|
if s.isClose || buf == nil {
|
||||||
return 0, errors.New("the conn has closed")
|
return 0, errors.New("the conn has closed")
|
||||||
}
|
}
|
||||||
if s.endRead-s.startRead == 0 { //read finish or start
|
if s.endRead-s.startRead == 0 { //read finish or start
|
||||||
if s.waitQueue.Size() == 0 {
|
if s.readQueue.Size() == 0 {
|
||||||
s.readWait = true
|
s.readWait = true
|
||||||
if t := s.readTimeOut.Sub(time.Now()); t > 0 {
|
if t := s.readTimeOut.Sub(time.Now()); t > 0 {
|
||||||
timer := time.NewTimer(t)
|
timer := time.NewTimer(t)
|
||||||
|
@ -67,19 +69,22 @@ func (s *conn) Read(buf []byte) (n int, err error) {
|
||||||
if s.isClose { //If the connection is closed instead of continuing command
|
if s.isClose { //If the connection is closed instead of continuing command
|
||||||
return 0, errors.New("the conn has closed")
|
return 0, errors.New("the conn has closed")
|
||||||
}
|
}
|
||||||
if node, err := s.waitQueue.Pop(); err != nil {
|
if node, err := s.readQueue.Pop(); err != nil {
|
||||||
s.Close()
|
s.Close()
|
||||||
return 0, io.EOF
|
return 0, io.EOF
|
||||||
} else {
|
} else {
|
||||||
pool.PutBufPoolCopy(s.readBuffer)
|
pool.PutBufPoolCopy(s.readBuffer)
|
||||||
if node.val == nil {
|
if node.val == nil {
|
||||||
//close
|
//close
|
||||||
|
s.sendClose = true
|
||||||
s.Close()
|
s.Close()
|
||||||
logs.Warn("close from read msg ", s.connId)
|
logs.Warn("close from read msg ", s.connId)
|
||||||
|
return 0, io.EOF
|
||||||
} else {
|
} else {
|
||||||
s.readBuffer = node.val
|
s.readBuffer = node.val
|
||||||
s.endRead = node.l
|
s.endRead = node.l
|
||||||
s.startRead = 0
|
s.startRead = 0
|
||||||
|
logs.Warn("get a new data buffer ", s.connId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -90,10 +95,12 @@ func (s *conn) Read(buf []byte) (n int, err error) {
|
||||||
n = copy(buf, s.readBuffer[s.startRead:s.endRead])
|
n = copy(buf, s.readBuffer[s.startRead:s.endRead])
|
||||||
s.startRead += n
|
s.startRead += n
|
||||||
}
|
}
|
||||||
|
logs.Warn("end read ", s.connId)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *conn) Write(buf []byte) (n int, err error) {
|
func (s *conn) Write(buf []byte) (n int, err error) {
|
||||||
|
logs.Warn("trying write", s.connId)
|
||||||
if s.isClose {
|
if s.isClose {
|
||||||
return 0, errors.New("the conn has closed")
|
return 0, errors.New("the conn has closed")
|
||||||
}
|
}
|
||||||
|
@ -113,6 +120,7 @@ func (s *conn) Write(buf []byte) (n int, err error) {
|
||||||
if s.isClose {
|
if s.isClose {
|
||||||
return 0, io.EOF
|
return 0, io.EOF
|
||||||
}
|
}
|
||||||
|
logs.Warn("write success ", s.connId)
|
||||||
return len(buf), nil
|
return len(buf), nil
|
||||||
}
|
}
|
||||||
func (s *conn) write(buf []byte, ch chan struct{}) {
|
func (s *conn) write(buf []byte, ch chan struct{}) {
|
||||||
|
@ -130,7 +138,8 @@ func (s *conn) write(buf []byte, ch chan struct{}) {
|
||||||
ch <- struct{}{}
|
ch <- struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *conn) Close() error {
|
func (s *conn) Close() (err error) {
|
||||||
|
logs.Warn("start closing ", s.connId)
|
||||||
if s.isClose {
|
if s.isClose {
|
||||||
logs.Warn("already closed", s.connId)
|
logs.Warn("already closed", s.connId)
|
||||||
return errors.New("the conn has closed")
|
return errors.New("the conn has closed")
|
||||||
|
@ -140,12 +149,22 @@ func (s *conn) Close() error {
|
||||||
if s.readWait {
|
if s.readWait {
|
||||||
s.readCh <- struct{}{}
|
s.readCh <- struct{}{}
|
||||||
}
|
}
|
||||||
s.waitQueue.Clear()
|
s.readQueue.Clear()
|
||||||
s.mux.connMap.Delete(s.connId)
|
s.mux.connMap.Delete(s.connId)
|
||||||
if !s.mux.IsClose {
|
if !s.mux.IsClose {
|
||||||
s.mux.sendInfo(common.MUX_CONN_CLOSE, s.connId, nil)
|
if !s.sendClose {
|
||||||
|
logs.Warn("start send closing msg", s.connId)
|
||||||
|
err = s.mux.sendInfo(common.MUX_CONN_CLOSE, s.connId, nil)
|
||||||
|
logs.Warn("send closing msg ok ", s.connId)
|
||||||
|
if err != nil {
|
||||||
|
logs.Warn(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
logs.Warn("send mux conn close pass ", s.connId)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *conn) LocalAddr() net.Addr {
|
func (s *conn) LocalAddr() net.Addr {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package mux
|
package mux
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/cnlh/nps/lib/common"
|
"github.com/cnlh/nps/lib/common"
|
||||||
"github.com/cnlh/nps/lib/pool"
|
"github.com/cnlh/nps/lib/pool"
|
||||||
|
@ -22,7 +23,7 @@ type Mux struct {
|
||||||
IsClose bool
|
IsClose bool
|
||||||
pingOk int
|
pingOk int
|
||||||
connType string
|
connType string
|
||||||
writeQueue *sliceEntry
|
writeQueue chan *bytes.Buffer
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,12 +36,13 @@ func NewMux(c net.Conn, connType string) *Mux {
|
||||||
newConnCh: make(chan *conn),
|
newConnCh: make(chan *conn),
|
||||||
IsClose: false,
|
IsClose: false,
|
||||||
connType: connType,
|
connType: connType,
|
||||||
writeQueue: NewQueue(),
|
writeQueue: make(chan *bytes.Buffer, 20),
|
||||||
}
|
}
|
||||||
//read session by flag
|
//read session by flag
|
||||||
go m.readSession()
|
go m.readSession()
|
||||||
//ping
|
//ping
|
||||||
go m.ping()
|
go m.ping()
|
||||||
|
//go m.writeSession()
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,8 +84,10 @@ func (s *Mux) Addr() net.Addr {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Mux) sendInfo(flag uint8, id int32, content []byte) (err error) {
|
func (s *Mux) sendInfo(flag uint8, id int32, content []byte) (err error) {
|
||||||
|
if flag == common.MUX_NEW_MSG {
|
||||||
|
logs.Warn("trying write to mux new msg", id)
|
||||||
|
}
|
||||||
buf := pool.BuffPool.Get()
|
buf := pool.BuffPool.Get()
|
||||||
defer pool.BuffPool.Put(buf)
|
|
||||||
pack := common.MuxPackager{}
|
pack := common.MuxPackager{}
|
||||||
err = pack.NewPac(flag, id, content)
|
err = pack.NewPac(flag, id, content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -97,14 +101,39 @@ func (s *Mux) sendInfo(flag uint8, id int32, content []byte) (err error) {
|
||||||
logs.Warn("pack err", err)
|
logs.Warn("pack err", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
//s.writeQueue <- buf
|
||||||
_, err = buf.WriteTo(s.conn)
|
_, err = buf.WriteTo(s.conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.Close()
|
s.Close()
|
||||||
logs.Warn("write err", err)
|
logs.Warn("write err", err)
|
||||||
}
|
}
|
||||||
|
pool.BuffPool.Put(buf)
|
||||||
|
if flag == common.MUX_CONN_CLOSE {
|
||||||
|
logs.Warn("write to mux conn close success", id)
|
||||||
|
}
|
||||||
|
if flag == common.MUX_NEW_MSG {
|
||||||
|
logs.Warn("write to mux new msg success", id)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Mux) ping() {
|
func (s *Mux) ping() {
|
||||||
go func() {
|
go func() {
|
||||||
ticker := time.NewTicker(time.Second * 1)
|
ticker := time.NewTicker(time.Second * 1)
|
||||||
|
@ -138,7 +167,7 @@ func (s *Mux) readSession() {
|
||||||
s.pingOk = 0
|
s.pingOk = 0
|
||||||
switch pack.Flag {
|
switch pack.Flag {
|
||||||
case common.MUX_NEW_CONN: //new conn
|
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)
|
conn := NewConn(pack.Id, s)
|
||||||
s.connMap.Set(pack.Id, conn) //it has been set before send ok
|
s.connMap.Set(pack.Id, conn) //it has been set before send ok
|
||||||
s.newConnCh <- conn
|
s.newConnCh <- conn
|
||||||
|
@ -151,22 +180,30 @@ func (s *Mux) readSession() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if conn, ok := s.connMap.Get(pack.Id); ok && !conn.isClose {
|
if conn, ok := s.connMap.Get(pack.Id); ok && !conn.isClose {
|
||||||
|
logs.Warn("read session flag id", pack.Flag, pack.Id)
|
||||||
switch pack.Flag {
|
switch pack.Flag {
|
||||||
case common.MUX_NEW_MSG: //new msg from remote conn
|
case common.MUX_NEW_MSG: //new msg from remote conn
|
||||||
//insert wait queue
|
//insert wait queue
|
||||||
conn.waitQueue.Push(NewBufNode(pack.Content, int(pack.Length)))
|
conn.readQueue.Push(NewBufNode(pack.Content, int(pack.Length)))
|
||||||
//judge len if >xxx ,send stop
|
//judge len if >xxx ,send stop
|
||||||
if conn.readWait {
|
if conn.readWait {
|
||||||
conn.readWait = false
|
conn.readWait = false
|
||||||
conn.readCh <- struct{}{}
|
conn.readCh <- struct{}{}
|
||||||
}
|
}
|
||||||
|
logs.Warn("push a read buffer ", conn.connId, pack.Id)
|
||||||
case common.MUX_NEW_CONN_OK: //conn ok
|
case common.MUX_NEW_CONN_OK: //conn ok
|
||||||
conn.connStatusOkCh <- struct{}{}
|
conn.connStatusOkCh <- struct{}{}
|
||||||
case common.MUX_NEW_CONN_Fail:
|
case common.MUX_NEW_CONN_Fail:
|
||||||
conn.connStatusFailCh <- struct{}{}
|
conn.connStatusFailCh <- struct{}{}
|
||||||
case common.MUX_CONN_CLOSE: //close the connection
|
case common.MUX_CONN_CLOSE: //close the connection
|
||||||
conn.waitQueue.Push(NewBufNode(nil, 0))
|
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)
|
s.connMap.Delete(pack.Id)
|
||||||
|
logs.Warn("read session mux conn close finish", pack.Id)
|
||||||
}
|
}
|
||||||
} else if pack.Flag == common.MUX_NEW_MSG {
|
} else if pack.Flag == common.MUX_NEW_MSG {
|
||||||
pool.PutBufPoolCopy(pack.Content)
|
pool.PutBufPoolCopy(pack.Content)
|
||||||
|
@ -192,6 +229,8 @@ func (s *Mux) Close() error {
|
||||||
select {
|
select {
|
||||||
case s.closeChan <- struct{}{}:
|
case s.closeChan <- struct{}{}:
|
||||||
}
|
}
|
||||||
|
s.closeChan <- struct{}{}
|
||||||
|
close(s.writeQueue)
|
||||||
close(s.newConnCh)
|
close(s.newConnCh)
|
||||||
return s.conn.Close()
|
return s.conn.Close()
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,22 +26,25 @@ func TestNewMux(t *testing.T) {
|
||||||
time.Sleep(time.Second * 3)
|
time.Sleep(time.Second * 3)
|
||||||
go func() {
|
go func() {
|
||||||
m2 := NewMux(conn2, "tcp")
|
m2 := NewMux(conn2, "tcp")
|
||||||
|
connCh := make(chan bool, 1)
|
||||||
for {
|
for {
|
||||||
c, err := m2.Accept()
|
c, err := m2.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
go func(c net.Conn) {
|
connCh <- true
|
||||||
|
go func(c net.Conn, ch chan bool) {
|
||||||
c2, err := net.Dial("tcp", "127.0.0.1:80")
|
c2, err := net.Dial("tcp", "127.0.0.1:80")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
go common.CopyBuffer(c2, c)
|
go common.CopyBuffer(c2, c)
|
||||||
common.CopyBuffer(c, c2)
|
common.CopyBuffer(c, c2)
|
||||||
c.Close()
|
|
||||||
//logs.Warn("close from out npc ")
|
|
||||||
c2.Close()
|
c2.Close()
|
||||||
}(c)
|
c.Close()
|
||||||
|
logs.Warn("close npc")
|
||||||
|
<-ch
|
||||||
|
}(c, connCh)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
@ -51,12 +54,14 @@ func TestNewMux(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
connCh := make(chan bool, 1)
|
||||||
for {
|
for {
|
||||||
conn, err := l.Accept()
|
conn, err := l.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
go func(conn net.Conn) {
|
connCh <- true
|
||||||
|
go func(conn net.Conn, ch chan bool) {
|
||||||
tmpCpnn, err := m1.NewConn()
|
tmpCpnn, err := m1.NewConn()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
|
@ -64,9 +69,10 @@ func TestNewMux(t *testing.T) {
|
||||||
go common.CopyBuffer(tmpCpnn, conn)
|
go common.CopyBuffer(tmpCpnn, conn)
|
||||||
common.CopyBuffer(conn, tmpCpnn)
|
common.CopyBuffer(conn, tmpCpnn)
|
||||||
conn.Close()
|
conn.Close()
|
||||||
tmpCpnn.Close()
|
//tmpCpnn.Close()
|
||||||
logs.Warn("close from out nps ", tmpCpnn.connId)
|
logs.Warn("close from out nps ", tmpCpnn.connId)
|
||||||
}(conn)
|
<-ch
|
||||||
|
}(conn, connCh)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue