nps/lib/mux/conn.go

438 lines
10 KiB
Go
Raw Normal View History

2019-02-26 14:40:28 +00:00
package mux
import (
"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
"io"
"net"
"sync"
2019-02-26 14:40:28 +00:00
"time"
)
type conn struct {
net.Conn
getStatusCh chan struct{}
connStatusOkCh chan struct{}
connStatusFailCh chan struct{}
readTimeOut time.Time
writeTimeOut time.Time
2019-09-15 07:02:10 +00:00
connId int32
isClose bool
closeFlag bool // close conn flag
receiveWindow *window
sendWindow *window
readCh waitingCh
writeCh waitingCh
mux *Mux
once sync.Once
2019-02-26 14:40:28 +00:00
}
2019-03-15 06:03:49 +00:00
func NewConn(connId int32, mux *Mux) *conn {
c := &conn{
2019-02-26 14:40:28 +00:00
getStatusCh: make(chan struct{}),
connStatusOkCh: make(chan struct{}),
connStatusFailCh: make(chan struct{}),
connId: connId,
2019-09-08 15:49:16 +00:00
receiveWindow: new(window),
sendWindow: new(window),
2019-02-26 14:40:28 +00:00
mux: mux,
once: sync.Once{},
2019-02-26 14:40:28 +00:00
}
2019-09-08 15:49:16 +00:00
c.receiveWindow.NewReceive()
c.sendWindow.NewSend()
2019-09-15 07:02:10 +00:00
c.readCh.new()
c.writeCh.new()
2019-03-15 06:03:49 +00:00
return c
2019-02-26 14:40:28 +00:00
}
func (s *conn) Read(buf []byte) (n int, err error) {
2019-09-15 07:02:10 +00:00
//logs.Warn("starting conn read", s.connId)
2019-03-15 06:03:49 +00:00
if s.isClose || buf == nil {
2019-02-26 14:40:28 +00:00
return 0, errors.New("the conn has closed")
}
2019-09-08 15:49:16 +00:00
// waiting for takeout from receive window finish or timeout
2019-09-15 07:02:10 +00:00
go s.readWindow(buf, s.readCh.nCh, s.readCh.errCh)
2019-09-08 15:49:16 +00:00
if t := s.readTimeOut.Sub(time.Now()); t > 0 {
timer := time.NewTimer(t)
defer timer.Stop()
select {
case <-timer.C:
return 0, errors.New("read timeout")
2019-09-15 07:02:10 +00:00
case n = <-s.readCh.nCh:
err = <-s.readCh.errCh
2019-02-26 14:40:28 +00:00
}
} else {
2019-09-15 07:02:10 +00:00
n = <-s.readCh.nCh
err = <-s.readCh.errCh
}
2019-09-22 14:08:51 +00:00
//logs.Warn("read window finish conn read n err buf", n, err, string(buf[:15]), s.connId)
return
2019-02-26 14:40:28 +00:00
}
2019-09-08 15:49:16 +00:00
func (s *conn) readWindow(buf []byte, nCh chan int, errCh chan error) {
n, err := s.receiveWindow.Read(buf)
//logs.Warn("readwindow goroutine status n err buf", n, err, string(buf[:15]))
if s.receiveWindow.WindowFull {
if s.receiveWindow.Size() > 0 {
// window.Read may be invoked before window.Write, and WindowFull flag change to true
// so make sure that receiveWindow is free some space
s.receiveWindow.WindowFull = false
logs.Warn("defer send mux msg send ok size", s.receiveWindow.Size())
s.mux.sendInfo(common.MUX_MSG_SEND_OK, s.connId, s.receiveWindow.Size())
// acknowledge other side, have empty some receive window space
}
}
nCh <- n
errCh <- err
}
2019-08-23 10:53:36 +00:00
func (s *conn) Write(buf []byte) (n int, err error) {
2019-09-15 07:02:10 +00:00
//logs.Warn("write starting", s.connId)
//defer logs.Warn("write end ", s.connId)
2019-02-26 14:40:28 +00:00
if s.isClose {
return 0, errors.New("the conn has closed")
}
if s.closeFlag {
2019-09-15 07:02:10 +00:00
//logs.Warn("conn close by write ", s.connId)
2019-09-08 15:49:16 +00:00
//s.Close()
return 0, errors.New("io: write on closed conn")
}
2019-09-08 15:49:16 +00:00
s.sendWindow.SetSendBuf(buf) // set the buf to send window
2019-09-15 07:02:10 +00:00
//logs.Warn("write set send buf success")
go s.write(s.writeCh.nCh, s.writeCh.errCh)
2019-09-08 15:49:16 +00:00
// waiting for send to other side or timeout
2019-02-26 14:40:28 +00:00
if t := s.writeTimeOut.Sub(time.Now()); t > 0 {
timer := time.NewTimer(t)
2019-03-15 06:03:49 +00:00
defer timer.Stop()
2019-02-26 14:40:28 +00:00
select {
case <-timer.C:
return 0, errors.New("write timeout")
2019-09-15 07:02:10 +00:00
case n = <-s.writeCh.nCh:
err = <-s.writeCh.errCh
2019-02-26 14:40:28 +00:00
}
} else {
2019-09-15 07:02:10 +00:00
n = <-s.writeCh.nCh
err = <-s.writeCh.errCh
2019-02-26 14:40:28 +00:00
}
2019-09-22 14:08:51 +00:00
//logs.Warn("write window finish n err buf id", n, err, string(buf[:15]), s.connId)
2019-09-08 15:49:16 +00:00
return
2019-02-26 14:40:28 +00:00
}
2019-09-08 15:49:16 +00:00
func (s *conn) write(nCh chan int, errCh chan error) {
var n int
var err error
for {
2019-09-08 15:49:16 +00:00
buf, err := s.sendWindow.WriteTo()
// get the usable window size buf from send window
if buf == nil && err == io.EOF {
// send window is drain, break the loop
err = nil
break
}
if err != nil {
break
}
2019-09-08 15:49:16 +00:00
n += len(buf)
//logs.Warn("send window buf len", len(buf))
s.mux.sendInfo(common.MUX_NEW_MSG, s.connId, buf)
// send to other side, not send nil data to other side
}
2019-09-08 15:49:16 +00:00
nCh <- n
errCh <- err
}
2019-08-26 10:47:23 +00:00
func (s *conn) Close() (err error) {
s.once.Do(s.closeProcess)
return
}
func (s *conn) closeProcess() {
2019-02-26 14:40:28 +00:00
s.isClose = true
s.mux.connMap.Delete(s.connId)
2019-03-02 09:43:21 +00:00
if !s.mux.IsClose {
2019-09-15 07:02:10 +00:00
//logs.Warn("conn send close", s.connId)
2019-09-08 15:49:16 +00:00
// if server or user close the conn while reading, will get a io.EOF
// and this Close method will be invoke, send this signal to close other side
s.mux.sendInfo(common.MUX_CONN_CLOSE, s.connId, nil)
}
2019-09-08 15:49:16 +00:00
s.sendWindow.CloseWindow()
s.receiveWindow.CloseWindow()
2019-08-26 10:47:23 +00:00
return
2019-02-26 14:40:28 +00:00
}
func (s *conn) LocalAddr() net.Addr {
return s.mux.conn.LocalAddr()
}
func (s *conn) RemoteAddr() net.Addr {
return s.mux.conn.RemoteAddr()
}
func (s *conn) SetDeadline(t time.Time) error {
s.readTimeOut = t
s.writeTimeOut = t
return nil
}
func (s *conn) SetReadDeadline(t time.Time) error {
s.readTimeOut = t
return nil
}
func (s *conn) SetWriteDeadline(t time.Time) error {
s.writeTimeOut = t
return nil
}
2019-09-08 15:49:16 +00:00
type window struct {
windowBuff []byte
off uint16
readOp chan struct{}
readWait bool
2019-09-08 15:49:16 +00:00
WindowFull bool
usableReceiveWindow chan uint16
WriteWg sync.WaitGroup
closeOp bool
closeOpCh chan struct{}
WriteEndOp chan struct{}
mutex sync.Mutex
}
func (Self *window) NewReceive() {
// initial a window for receive
Self.windowBuff = common.WindowBuff.Get()
Self.readOp = make(chan struct{})
Self.WriteEndOp = make(chan struct{})
2019-09-15 07:02:10 +00:00
Self.closeOpCh = make(chan struct{}, 3)
2019-09-08 15:49:16 +00:00
}
func (Self *window) NewSend() {
// initial a window for send
Self.usableReceiveWindow = make(chan uint16)
2019-09-15 07:02:10 +00:00
Self.closeOpCh = make(chan struct{}, 3)
2019-09-08 15:49:16 +00:00
}
func (Self *window) SetSendBuf(buf []byte) {
// send window buff from conn write method, set it to send window
Self.mutex.Lock()
Self.windowBuff = buf
Self.off = 0
Self.mutex.Unlock()
}
func (Self *window) fullSlide() {
// slide by allocate
newBuf := common.WindowBuff.Get()
copy(newBuf[0:Self.len()], Self.windowBuff[Self.off:])
Self.off = 0
common.WindowBuff.Put(Self.windowBuff)
Self.windowBuff = newBuf
return
}
func (Self *window) liteSlide() {
// slide by re slice
Self.windowBuff = Self.windowBuff[Self.off:]
Self.off = 0
return
}
func (Self *window) Size() (n int) {
// receive Window remaining
n = common.PoolSizeWindow - Self.len()
return
}
func (Self *window) len() (n int) {
n = len(Self.windowBuff[Self.off:])
return
}
func (Self *window) cap() (n int) {
n = cap(Self.windowBuff[Self.off:])
return
}
func (Self *window) grow(n int) {
Self.windowBuff = Self.windowBuff[:Self.len()+n]
}
func (Self *window) Write(p []byte) (n int, err error) {
if Self.closeOp {
return 0, errors.New("conn.receiveWindow: write on closed window")
}
if len(p) > Self.Size() {
return 0, errors.New("conn.receiveWindow: write too large")
}
Self.mutex.Lock()
// slide the offset
if len(p) > Self.cap()-Self.len() {
// not enough space, need to allocate
Self.fullSlide()
} else {
// have enough space, re slice
Self.liteSlide()
}
length := Self.len() // length before grow
Self.grow(len(p)) // grow for copy
n = copy(Self.windowBuff[length:], p) // must copy data before allow Read
if Self.readWait {
// if there condition is length == 0 and
// Read method just take away all the windowBuff,
// this method will block until windowBuff is empty again
2019-09-15 07:02:10 +00:00
// allow continue read
defer Self.allowRead()
}
2019-09-08 15:49:16 +00:00
Self.mutex.Unlock()
return n, nil
}
func (Self *window) allowRead() (closed bool) {
if Self.closeOp {
close(Self.readOp)
return true
}
Self.mutex.Lock()
Self.readWait = false
Self.mutex.Unlock()
2019-09-08 15:49:16 +00:00
select {
case <-Self.closeOpCh:
close(Self.readOp)
return true
case Self.readOp <- struct{}{}:
return false
}
}
func (Self *window) Read(p []byte) (n int, err error) {
if Self.closeOp {
return 0, io.EOF // Write method receive close signal, returns eof
}
2019-09-15 07:02:10 +00:00
Self.mutex.Lock()
length := Self.len() // protect the length data, it invokes
// before Write lock and after Write unlock
if length == 0 {
2019-09-08 15:49:16 +00:00
// window is empty, waiting for Write method send a success readOp signal
// or get timeout or close
Self.readWait = true
Self.mutex.Unlock()
2019-09-08 15:49:16 +00:00
ticker := time.NewTicker(2 * time.Minute)
defer ticker.Stop()
select {
case _, ok := <-Self.readOp:
if !ok {
return 0, errors.New("conn.receiveWindow: window closed")
}
case <-Self.WriteEndOp:
return 0, io.EOF // receive eof signal, returns eof
case <-ticker.C:
return 0, errors.New("conn.receiveWindow: read time out")
case <-Self.closeOpCh:
close(Self.readOp)
return 0, io.EOF // receive close signal, returns eof
}
} else {
Self.mutex.Unlock()
2019-09-08 15:49:16 +00:00
}
2019-09-22 14:08:51 +00:00
minCopy := 512
for {
Self.mutex.Lock()
if len(p) == n || Self.len() == 0 {
Self.mutex.Unlock()
break
}
if n+minCopy > len(p) {
minCopy = len(p) - n
}
i := copy(p[n:n+minCopy], Self.windowBuff[Self.off:])
Self.off += uint16(i)
n += i
Self.mutex.Unlock()
}
2019-09-08 15:49:16 +00:00
p = p[:n]
return
}
func (Self *window) WriteTo() (p []byte, err error) {
if Self.closeOp {
2019-09-15 07:02:10 +00:00
//logs.Warn("window write to closed")
2019-09-08 15:49:16 +00:00
return nil, errors.New("conn.writeWindow: window closed")
}
if Self.len() == 0 {
return nil, io.EOF
// send window buff is drain, return eof and get another one
}
var windowSize uint16
var ok bool
waiting:
ticker := time.NewTicker(2 * time.Minute)
defer ticker.Stop()
// waiting for receive usable window size, or timeout
select {
case windowSize, ok = <-Self.usableReceiveWindow:
if !ok {
return nil, errors.New("conn.writeWindow: window closed")
}
case <-ticker.C:
return nil, errors.New("conn.writeWindow: write to time out")
2019-09-15 07:02:10 +00:00
case <-Self.closeOpCh:
return nil, errors.New("conn.writeWindow: window closed")
2019-09-08 15:49:16 +00:00
}
if windowSize == 0 {
goto waiting // waiting for another usable window size
}
Self.mutex.Lock()
if windowSize > uint16(Self.len()) {
// usable window size is bigger than window buff size, send the full buff
windowSize = uint16(Self.len())
}
p = Self.windowBuff[Self.off : windowSize+Self.off]
Self.off += windowSize
Self.mutex.Unlock()
return
}
func (Self *window) SetAllowSize(value uint16) (closed bool) {
defer func() {
if recover() != nil {
closed = true
}
}()
if Self.closeOp {
close(Self.usableReceiveWindow)
return true
}
select {
case Self.usableReceiveWindow <- value:
return false
case <-Self.closeOpCh:
close(Self.usableReceiveWindow)
return true
}
}
func (Self *window) CloseWindow() {
Self.closeOp = true
Self.closeOpCh <- struct{}{}
Self.closeOpCh <- struct{}{}
2019-09-15 07:02:10 +00:00
Self.closeOpCh <- struct{}{}
2019-09-08 15:49:16 +00:00
close(Self.closeOpCh)
return
}
2019-09-15 07:02:10 +00:00
type waitingCh struct {
nCh chan int
errCh chan error
}
func (Self *waitingCh) new() {
Self.nCh = make(chan int)
Self.errCh = make(chan error)
}
func (Self *waitingCh) close() {
close(Self.nCh)
close(Self.errCh)
}