add debug mode

This commit is contained in:
2022-05-20 09:27:19 +08:00
parent 9065a12b99
commit 48bbc5b776
5 changed files with 59 additions and 9 deletions
+23 -3
View File
@@ -15,6 +15,10 @@ import (
"time"
)
func init() {
Register(TransferMsg{})
}
type ClientCommon struct {
alive atomic.Value
status Status
@@ -46,6 +50,7 @@ type ClientCommon struct {
useHeartBeat bool
sequenceDe func([]byte) (interface{}, error)
sequenceEn func(interface{}) ([]byte, error)
debugMode bool
}
func (c *ClientCommon) Connect(network string, addr string) error {
@@ -67,6 +72,16 @@ func (c *ClientCommon) Connect(network string, addr string) error {
return c.clientPostInit()
}
func (c *ClientCommon) DebugMode(dmg bool) {
c.mu.Lock()
c.debugMode = dmg
c.mu.Unlock()
}
func (c *ClientCommon) IsDebugMode() bool {
return c.debugMode
}
func (c *ClientCommon) ConnectTimeout(network string, addr string, timeout time.Duration) error {
if c.alive.Load().(bool) {
return errors.New("client already run")
@@ -184,9 +199,14 @@ func (c *ClientCommon) Heartbeat() {
c.lastHeartbeat = time.Now().Unix()
failedCount = 0
}
if c.debugMode {
fmt.Println("failed to recv heartbeat,timeout!")
}
failedCount++
if failedCount >= 3 {
//fmt.Println("heatbeat failed,stop client")
if c.debugMode {
fmt.Println("heatbeat failed more than 3 times,stop client")
}
c.alive.Store(false)
c.mu.Lock()
c.status = Status{
@@ -229,7 +249,7 @@ func (c *ClientCommon) readMessage() {
continue
}
if err != nil {
if c.showError {
if c.showError || c.debugMode {
fmt.Println("client read error", err)
}
c.alive.Store(false)
@@ -279,7 +299,7 @@ func (c *ClientCommon) loadMessage() {
//transfer to Msg
msg, err := c.sequenceDe(c.msgDe(c.SecretKey, data.Msg))
if err != nil {
if c.showError {
if c.showError || c.debugMode {
fmt.Println("client decode data error", err)
}
return