add debug mode

master v1.2.2
兔子 3 years ago
parent 9065a12b99
commit 48bbc5b776

@ -15,6 +15,10 @@ import (
"time" "time"
) )
func init() {
Register(TransferMsg{})
}
type ClientCommon struct { type ClientCommon struct {
alive atomic.Value alive atomic.Value
status Status status Status
@ -46,6 +50,7 @@ type ClientCommon struct {
useHeartBeat bool useHeartBeat bool
sequenceDe func([]byte) (interface{}, error) sequenceDe func([]byte) (interface{}, error)
sequenceEn func(interface{}) ([]byte, error) sequenceEn func(interface{}) ([]byte, error)
debugMode bool
} }
func (c *ClientCommon) Connect(network string, addr string) error { 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() 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 { func (c *ClientCommon) ConnectTimeout(network string, addr string, timeout time.Duration) error {
if c.alive.Load().(bool) { if c.alive.Load().(bool) {
return errors.New("client already run") return errors.New("client already run")
@ -184,9 +199,14 @@ func (c *ClientCommon) Heartbeat() {
c.lastHeartbeat = time.Now().Unix() c.lastHeartbeat = time.Now().Unix()
failedCount = 0 failedCount = 0
} }
if c.debugMode {
fmt.Println("failed to recv heartbeat,timeout!")
}
failedCount++ failedCount++
if failedCount >= 3 { 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.alive.Store(false)
c.mu.Lock() c.mu.Lock()
c.status = Status{ c.status = Status{
@ -229,7 +249,7 @@ func (c *ClientCommon) readMessage() {
continue continue
} }
if err != nil { if err != nil {
if c.showError { if c.showError || c.debugMode {
fmt.Println("client read error", err) fmt.Println("client read error", err)
} }
c.alive.Store(false) c.alive.Store(false)
@ -279,7 +299,7 @@ func (c *ClientCommon) loadMessage() {
//transfer to Msg //transfer to Msg
msg, err := c.sequenceDe(c.msgDe(c.SecretKey, data.Msg)) msg, err := c.sequenceDe(c.msgDe(c.SecretKey, data.Msg))
if err != nil { if err != nil {
if c.showError { if c.showError || c.debugMode {
fmt.Println("client decode data error", err) fmt.Println("client decode data error", err)
} }
return return

@ -39,6 +39,8 @@ type Client interface {
StopMonitorChan() <-chan struct{} StopMonitorChan() <-chan struct{}
Status() Status Status() Status
ShowError(bool) ShowError(bool)
DebugMode(bool)
IsDebugMode() bool
GetSequenceEn() func(interface{}) ([]byte, error) GetSequenceEn() func(interface{}) ([]byte, error)
SetSequenceEn(func(interface{}) ([]byte, error)) SetSequenceEn(func(interface{}) ([]byte, error))

@ -80,7 +80,3 @@ func defaultMsgEn(key []byte, d []byte) []byte {
func defaultMsgDe(key []byte, d []byte) []byte { func defaultMsgDe(key []byte, d []byte) []byte {
return starcrypto.AesDecryptCFB(d, key) return starcrypto.AesDecryptCFB(d, key)
} }
func init() {
Register(TransferMsg{})
}

@ -15,6 +15,10 @@ import (
"time" "time"
) )
func init() {
Register(TransferMsg{})
}
type ServerCommon struct { type ServerCommon struct {
msgID uint64 msgID uint64
alive atomic.Value alive atomic.Value
@ -42,6 +46,7 @@ type ServerCommon struct {
sequenceDe func([]byte) (interface{}, error) sequenceDe func([]byte) (interface{}, error)
sequenceEn func(interface{}) ([]byte, error) sequenceEn func(interface{}) ([]byte, error)
showError bool showError bool
debugMode bool
} }
func NewServer() Server { func NewServer() Server {
@ -65,6 +70,19 @@ func NewServer() Server {
} }
return &server return &server
} }
func (s *ServerCommon) DebugMode(dmg bool) {
s.mu.Lock()
s.debugMode = dmg
s.mu.Unlock()
}
func (s *ServerCommon) IsDebugMode() bool {
s.mu.RLock()
defer s.mu.RUnlock()
return s.debugMode
}
func (s *ServerCommon) ShowError(std bool) { func (s *ServerCommon) ShowError(std bool) {
s.mu.Lock() s.mu.Lock()
s.showError = std s.showError = std
@ -185,16 +203,22 @@ func (s *ServerCommon) acceptTU() {
for { for {
select { select {
case <-s.stopCtx.Done(): case <-s.stopCtx.Done():
if s.debugMode {
fmt.Println("accept goroutine recv exit signal,exit")
}
return return
default: default:
} }
conn, err := s.listener.Accept() conn, err := s.listener.Accept()
if err != nil { if err != nil {
if s.showError { if s.showError || s.debugMode {
fmt.Println("error accept:", err) fmt.Println("error accept:", err)
} }
continue continue
} }
if s.debugMode {
fmt.Println("accept new connection from", conn.RemoteAddr())
}
var id string var id string
for { for {
id = fmt.Sprintf("%s%d%d", conn.RemoteAddr().String(), time.Now().UnixNano(), rand.Int63()) id = fmt.Sprintf("%s%d%d", conn.RemoteAddr().String(), time.Now().UnixNano(), rand.Int63())
@ -282,7 +306,7 @@ func (s *ServerCommon) loadMessage() {
//fmt.Println("received:", float64(time.Now().UnixNano()-nowd)/1000000) //fmt.Println("received:", float64(time.Now().UnixNano()-nowd)/1000000)
msg, err := s.sequenceDe(cc.msgDe(cc.SecretKey, data.Msg)) msg, err := s.sequenceDe(cc.msgDe(cc.SecretKey, data.Msg))
if err != nil { if err != nil {
if s.showError { if s.showError || s.debugMode {
fmt.Println("server decode data error", err) fmt.Println("server decode data error", err)
} }
return return
@ -526,6 +550,9 @@ func (s *ServerCommon) acceptUDP() {
for { for {
select { select {
case <-s.stopCtx.Done(): case <-s.stopCtx.Done():
if s.debugMode {
fmt.Println("accept goroutine recv exit signal,exit")
}
return return
default: default:
} }
@ -535,6 +562,9 @@ func (s *ServerCommon) acceptUDP() {
data := make([]byte, 4096) data := make([]byte, 4096)
num, addr, err := s.udpListener.ReadFromUDP(data) num, addr, err := s.udpListener.ReadFromUDP(data)
id := addr.String() id := addr.String()
if s.debugMode {
fmt.Println("accept new udp message from", id)
}
//fmt.Println("s recv udp:", float64(time.Now().UnixNano()-nowd)/1000000) //fmt.Println("s recv udp:", float64(time.Now().UnixNano()-nowd)/1000000)
s.mu.RLock() s.mu.RLock()
if _, ok := s.clientPool[id]; !ok { if _, ok := s.clientPool[id]; !ok {

@ -41,6 +41,8 @@ type Server interface {
GetSequenceDe() func([]byte) (interface{}, error) GetSequenceDe() func([]byte) (interface{}, error)
SetSequenceDe(func([]byte) (interface{}, error)) SetSequenceDe(func([]byte) (interface{}, error))
ShowError(bool) ShowError(bool)
DebugMode(bool)
IsDebugMode() bool
HeartbeatTimeoutSec() int64 HeartbeatTimeoutSec() int64
SetHeartbeatTimeoutSec(int64) SetHeartbeatTimeoutSec(int64)

Loading…
Cancel
Save