@ -2,6 +2,7 @@ package main
import (
"encoding/json"
"fmt"
"io"
"sync"
"time"
@ -18,8 +19,8 @@ var isHeartBeatContinue bool = true
func ControlProcess ( cli * client . ProxyClient , wait * sync . WaitGroup ) {
defer wait . Done ( )
c := loginToServer ( cli )
if c = = nil {
c , err := loginToServer ( cli )
if err ! = nil {
log . Error ( "ProxyName [%s], connect to server failed!" , cli . Name )
return
}
@ -34,15 +35,15 @@ func ControlProcess(cli *client.ProxyClient, wait *sync.WaitGroup) {
var sleepTime time . Duration = 1
for {
log . Debug ( "ProxyName [%s], try to reconnect to server[%s:%d]..." , cli . Name , client . ServerAddr , client . ServerPort )
tmpConn := loginToServer ( cli )
if tmpConn ! = nil {
tmpConn , err := loginToServer ( cli )
if err = = nil {
c . Close ( )
c = tmpConn
break
}
if sleepTime < 60 {
sleepTime ++
sleepTime = sleepTime * 2
}
time . Sleep ( sleepTime * time . Second )
}
@ -56,15 +57,11 @@ func ControlProcess(cli *client.ProxyClient, wait *sync.WaitGroup) {
}
}
func loginToServer ( cli * client . ProxyClient ) ( connection * conn . Conn ) {
c := & conn . Conn { }
connection = nil
for i := 0 ; i < 1 ; i ++ {
err := c . ConnectServer ( client . ServerAddr , client . ServerPort )
func loginToServer ( cli * client . ProxyClient ) ( c * conn . Conn , err error ) {
c , err = conn . ConnectServer ( client . ServerAddr , client . ServerPort )
if err != nil {
log . Error ( "ProxyName [%s], connect to server [%s:%d] error, %v" , cli . Name , client . ServerAddr , client . ServerPort , err )
break
return
}
req := & msg . ClientCtlReq {
@ -76,51 +73,46 @@ func loginToServer(cli *client.ProxyClient) (connection *conn.Conn) {
err = c . Write ( string ( buf ) + "\n" )
if err != nil {
log . Error ( "ProxyName [%s], write to server error, %v" , cli . Name , err )
break
return
}
res , err := c . ReadLine ( )
if err != nil {
log . Error ( "ProxyName [%s], read from server error, %v" , cli . Name , err )
break
return
}
log . Debug ( "ProxyName [%s], read [%s]" , cli . Name , res )
clientCtlRes := & msg . ClientCtlRes { }
if err = json . Unmarshal ( [ ] byte ( res ) , & clientCtlRes ) ; err != nil {
log . Error ( "ProxyName [%s], format server response error, %v" , cli . Name , err )
break
return
}
if clientCtlRes . Code != 0 {
log . Error ( "ProxyName [%s], start proxy error, %s" , cli . Name , clientCtlRes . Msg )
break
return c , fmt . Errorf ( "%s" , clientCtlRes . Msg )
}
connection = c
go startHeartBeat ( connection )
go startHeartBeat ( c )
log . Debug ( "ProxyName [%s], connect to server[%s:%d] success!" , cli . Name , client . ServerAddr , client . ServerPort )
}
if connection == nil {
c . Close ( )
}
return
}
func startHeartBeat ( con * conn . Conn ) {
isHeartBeatContinue = true
func startHeartBeat ( c * conn . Conn ) {
log . Debug ( "Start to send heartbeat" )
for {
time . Sleep ( time . Duration ( client . HeartBeatInterval ) * time . Second )
if isHeartBeatContinue {
err := c on . Write ( "\n" )
if ! c . IsClosed ( ) {
err := c . Write ( "\n" )
if err != nil {
log . Error ( "Send hearbeat to server failed! Err:%s" , err . Error ( ) )
continue
}
} else {
break
}
}
log . Info ( "heartbeat exit" )
}