(1)优化重连和心跳检测

This commit is contained in:
Hurricanezwf
2016-02-05 14:18:26 +08:00
parent af6fc61537
commit 5d6f37aa82
5 changed files with 104 additions and 48 deletions

View File

@@ -1,8 +1,8 @@
package models
import (
"sync"
"container/list"
"sync"
"frp/pkg/utils/conn"
"frp/pkg/utils/log"
@@ -14,22 +14,24 @@ const (
)
type ProxyServer struct {
Name string
Passwd string
BindAddr string
ListenPort int64
Name string
Passwd string
BindAddr string
ListenPort int64
Status int64
Listener *conn.Listener // accept new connection from remote users
CtlMsgChan chan int64 // every time accept a new user conn, put "1" to the channel
CliConnChan chan *conn.Conn // get client conns from control goroutine
UserConnList *list.List // store user conns
Mutex sync.Mutex
Status int64
Listener *conn.Listener // accept new connection from remote users
CtlMsgChan chan int64 // every time accept a new user conn, put "1" to the channel
StopBlockChan chan int64 // put any number to the channel, if you want to stop wait user conn
CliConnChan chan *conn.Conn // get client conns from control goroutine
UserConnList *list.List // store user conns
Mutex sync.Mutex
}
func (p *ProxyServer) Init() {
p.Status = Idle
p.CtlMsgChan = make(chan int64)
p.StopBlockChan = make(chan int64)
p.CliConnChan = make(chan *conn.Conn)
p.UserConnList = list.New()
}
@@ -55,7 +57,7 @@ func (p *ProxyServer) Start() (err error) {
go func() {
for {
// block
c := p.Listener.GetConn()
c := p.Listener.GetConn()
log.Debug("ProxyName [%s], get one new user conn [%s]", p.Name, c.GetRemoteAddr())
// put to list
@@ -93,7 +95,7 @@ func (p *ProxyServer) Start() (err error) {
// msg will transfer to another without modifying
log.Debug("Join two conns, (l[%s] r[%s]) (l[%s] r[%s])", cliConn.GetLocalAddr(), cliConn.GetRemoteAddr(),
userConn.GetLocalAddr(), userConn.GetRemoteAddr())
userConn.GetLocalAddr(), userConn.GetRemoteAddr())
go conn.Join(cliConn, userConn)
}
}()
@@ -110,7 +112,15 @@ func (p *ProxyServer) Close() {
p.Unlock()
}
func (p *ProxyServer) WaitUserConn() (res int64) {
res = <-p.CtlMsgChan
return
func (p *ProxyServer) WaitUserConn() (res int64, isStop bool) {
select {
case res = <-p.CtlMsgChan:
return res, false
case <-p.StopBlockChan:
return 0, true
}
}
func (p *ProxyServer) StopWaitUserConn() {
p.StopBlockChan <- 1
}

View File

@@ -1,18 +1,18 @@
package conn
import (
"fmt"
"net"
"bufio"
"sync"
"fmt"
"io"
"net"
"sync"
"frp/pkg/utils/log"
)
type Listener struct {
Addr net.Addr
Conns chan *Conn
Addr net.Addr
Conns chan *Conn
}
// wait util get one
@@ -22,8 +22,8 @@ func (l *Listener) GetConn() (conn *Conn) {
}
type Conn struct {
TcpConn *net.TCPConn
Reader *bufio.Reader
TcpConn *net.TCPConn
Reader *bufio.Reader
}
func (c *Conn) ConnectServer(host string, port int64) (err error) {
@@ -59,7 +59,9 @@ func (c *Conn) Write(content string) (err error) {
}
func (c *Conn) Close() {
c.TcpConn.Close()
if c.TcpConn != nil { // ZWF:我觉得应该加一个非空保护
c.TcpConn.Close()
}
}
func Listen(bindAddr string, bindPort int64) (l *Listener, err error) {
@@ -70,8 +72,8 @@ func Listen(bindAddr string, bindPort int64) (l *Listener, err error) {
}
l = &Listener{
Addr: listener.Addr(),
Conns: make(chan *Conn),
Addr: listener.Addr(),
Conns: make(chan *Conn),
}
go func() {
@@ -83,7 +85,7 @@ func Listen(bindAddr string, bindPort int64) (l *Listener, err error) {
}
c := &Conn{
TcpConn: conn,
TcpConn: conn,
}
c.Reader = bufio.NewReader(c.TcpConn)
l.Conns <- c