all: some optimization

This commit is contained in:
fatedier
2016-03-31 18:03:44 +08:00
parent 45c21b2705
commit 52f99bbc00
9 changed files with 253 additions and 230 deletions

View File

@@ -51,8 +51,8 @@ func (p *ProxyClient) GetRemoteConn(addr string, port int64) (c *conn.Conn, err
return
}
req := &msg.ClientCtlReq{
Type: consts.WorkConn,
req := &msg.ControlReq{
Type: consts.NewWorkConn,
ProxyName: p.Name,
Passwd: p.Passwd,
}
@@ -79,7 +79,7 @@ func (p *ProxyClient) StartTunnel(serverAddr string, serverPort int64) (err erro
}
// l means local, r means remote
log.Debug("Join two conns, (l[%s] r[%s]) (l[%s] r[%s])", localConn.GetLocalAddr(), localConn.GetRemoteAddr(),
log.Debug("Join two connections, (l[%s] r[%s]) (l[%s] r[%s])", localConn.GetLocalAddr(), localConn.GetRemoteAddr(),
remoteConn.GetLocalAddr(), remoteConn.GetRemoteAddr())
// go conn.Join(localConn, remoteConn)
go conn.JoinMore(localConn, remoteConn, p.Passwd)

View File

@@ -20,18 +20,12 @@ const (
Working
)
// connection type
// msg type
const (
CtlConn = iota
WorkConn
)
// msg from client to server
const (
CSHeartBeatReq = 1
)
// msg from server to client
const (
SCHeartBeatRes = 100
NewCtlConn = iota
NewWorkConn
NoticeUserConn
NewCtlConnRes
HeartbeatReq
HeartbeatRes
)

View File

@@ -19,16 +19,15 @@ type GeneralRes struct {
Msg string `json:"msg"`
}
type ClientCtlReq struct {
// messages between control connection of frpc and frps
type ControlReq struct {
Type int64 `json:"type"`
ProxyName string `json:"proxy_name"`
Passwd string `json:"passwd"`
ProxyName string `json:"proxy_name,omitempty"`
Passwd string `json:"passwd, omitempty"`
}
type ClientCtlRes struct {
GeneralRes
}
type ServerCtlReq struct {
Type int64 `json:"type"`
type ControlRes struct {
Type int64 `json:"type"`
Code int64 `json:"code"`
Msg string `json:"msg"`
}

View File

@@ -33,14 +33,14 @@ type ProxyServer struct {
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
workConnChan chan *conn.Conn // get new work conns from control goroutine
userConnList *list.List // store user conns
mutex sync.Mutex
}
func (p *ProxyServer) Init() {
p.Status = consts.Idle
p.cliConnChan = make(chan *conn.Conn)
p.workConnChan = make(chan *conn.Conn)
p.ctlMsgChan = make(chan int64)
p.userConnList = list.New()
}
@@ -109,7 +109,7 @@ func (p *ProxyServer) Start() (err error) {
// start another goroutine for join two conns from client and user
go func() {
for {
cliConn, ok := <-p.cliConnChan
workConn, ok := <-p.workConnChan
if !ok {
return
}
@@ -122,7 +122,7 @@ func (p *ProxyServer) Start() (err error) {
userConn = element.Value.(*conn.Conn)
p.userConnList.Remove(element)
} else {
cliConn.Close()
workConn.Close()
p.Unlock()
continue
}
@@ -130,10 +130,10 @@ func (p *ProxyServer) Start() (err error) {
// msg will transfer to another without modifying
// l means local, r means remote
log.Debug("Join two conns, (l[%s] r[%s]) (l[%s] r[%s])", cliConn.GetLocalAddr(), cliConn.GetRemoteAddr(),
log.Debug("Join two connections, (l[%s] r[%s]) (l[%s] r[%s])", workConn.GetLocalAddr(), workConn.GetRemoteAddr(),
userConn.GetLocalAddr(), userConn.GetRemoteAddr())
// go conn.Join(cliConn, userConn)
go conn.JoinMore(userConn, cliConn, p.Passwd)
// go conn.Join(workConn, userConn)
go conn.JoinMore(userConn, workConn, p.Passwd)
}
}()
@@ -147,7 +147,7 @@ func (p *ProxyServer) Close() {
p.listener.Close()
}
close(p.ctlMsgChan)
close(p.cliConnChan)
close(p.workConnChan)
p.userConnList = list.New()
p.Unlock()
}
@@ -162,6 +162,6 @@ func (p *ProxyServer) WaitUserConn() (closeFlag bool) {
return
}
func (p *ProxyServer) GetNewCliConn(c *conn.Conn) {
p.cliConnChan <- c
func (p *ProxyServer) RecvNewWorkConn(c *conn.Conn) {
p.workConnChan <- c
}