From af6fc61537eaa8012c0091a0eb18353de9604c2c Mon Sep 17 00:00:00 2001 From: Hurricanezwf <1094646850@qq.com> Date: Thu, 4 Feb 2016 11:29:04 +0800 Subject: [PATCH] =?UTF-8?q?(1)=E6=96=B0=E5=A2=9Eclient=E5=90=91server?= =?UTF-8?q?=E5=8F=91=E9=80=81=E5=BF=83=E8=B7=B3=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/frpc/control.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/cmd/frpc/control.go b/cmd/frpc/control.go index 78facb2..332a06c 100644 --- a/cmd/frpc/control.go +++ b/cmd/frpc/control.go @@ -11,12 +11,13 @@ import ( "frp/pkg/utils/log" ) -// 重连时的间隔时间区间 const ( - sleepMinDuration = 1 - sleepMaxDuration = 60 + heartbeatDuration = 2 //心跳检测时间间隔,单位秒 ) +// client与server之间连接的保护锁 +var connProtect sync.Mutex + func ControlProcess(cli *models.ProxyClient, wait *sync.WaitGroup) { defer wait.Done() @@ -27,11 +28,13 @@ func ControlProcess(cli *models.ProxyClient, wait *sync.WaitGroup) { } defer c.Close() + go startHeartBeat(c) + for { // ignore response content now _, err := c.ReadLine() if err == io.EOF { - // reconnect when disconnect + connProtect.Lock() // 除了这里,其他地方禁止对连接进行任何操作 log.Debug("ProxyName [%s], server close this control conn", cli.Name) var sleepTime time.Duration = 1 for { @@ -48,6 +51,7 @@ func ControlProcess(cli *models.ProxyClient, wait *sync.WaitGroup) { } time.Sleep(sleepTime * time.Second) } + connProtect.Unlock() continue } else if err != nil { log.Warn("ProxyName [%s], read from server error, %v", cli.Name, err) @@ -108,3 +112,16 @@ func loginToServer(cli *models.ProxyClient) (connection *conn.Conn) { return } + +func startHeartBeat(con *conn.Conn) { + for { + time.Sleep(heartbeatDuration * time.Second) + + connProtect.Lock() + err := con.Write("\r\n") + connProtect.Unlock() + if err != nil { + log.Error("Send hearbeat to server failed! Err:%s", err.Error()) + } + } +}