add debug mode
This commit is contained in:
parent
9065a12b99
commit
48bbc5b776
26
client.go
26
client.go
@ -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
|
||||
|
@ -39,6 +39,8 @@ type Client interface {
|
||||
StopMonitorChan() <-chan struct{}
|
||||
Status() Status
|
||||
ShowError(bool)
|
||||
DebugMode(bool)
|
||||
IsDebugMode() bool
|
||||
|
||||
GetSequenceEn() 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 {
|
||||
return starcrypto.AesDecryptCFB(d, key)
|
||||
}
|
||||
|
||||
func init() {
|
||||
Register(TransferMsg{})
|
||||
}
|
||||
|
34
server.go
34
server.go
@ -15,6 +15,10 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
Register(TransferMsg{})
|
||||
}
|
||||
|
||||
type ServerCommon struct {
|
||||
msgID uint64
|
||||
alive atomic.Value
|
||||
@ -42,6 +46,7 @@ type ServerCommon struct {
|
||||
sequenceDe func([]byte) (interface{}, error)
|
||||
sequenceEn func(interface{}) ([]byte, error)
|
||||
showError bool
|
||||
debugMode bool
|
||||
}
|
||||
|
||||
func NewServer() Server {
|
||||
@ -65,6 +70,19 @@ func NewServer() 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) {
|
||||
s.mu.Lock()
|
||||
s.showError = std
|
||||
@ -185,16 +203,22 @@ func (s *ServerCommon) acceptTU() {
|
||||
for {
|
||||
select {
|
||||
case <-s.stopCtx.Done():
|
||||
if s.debugMode {
|
||||
fmt.Println("accept goroutine recv exit signal,exit")
|
||||
}
|
||||
return
|
||||
default:
|
||||
}
|
||||
conn, err := s.listener.Accept()
|
||||
if err != nil {
|
||||
if s.showError {
|
||||
if s.showError || s.debugMode {
|
||||
fmt.Println("error accept:", err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if s.debugMode {
|
||||
fmt.Println("accept new connection from", conn.RemoteAddr())
|
||||
}
|
||||
var id string
|
||||
for {
|
||||
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)
|
||||
msg, err := s.sequenceDe(cc.msgDe(cc.SecretKey, data.Msg))
|
||||
if err != nil {
|
||||
if s.showError {
|
||||
if s.showError || s.debugMode {
|
||||
fmt.Println("server decode data error", err)
|
||||
}
|
||||
return
|
||||
@ -526,6 +550,9 @@ func (s *ServerCommon) acceptUDP() {
|
||||
for {
|
||||
select {
|
||||
case <-s.stopCtx.Done():
|
||||
if s.debugMode {
|
||||
fmt.Println("accept goroutine recv exit signal,exit")
|
||||
}
|
||||
return
|
||||
default:
|
||||
}
|
||||
@ -535,6 +562,9 @@ func (s *ServerCommon) acceptUDP() {
|
||||
data := make([]byte, 4096)
|
||||
num, addr, err := s.udpListener.ReadFromUDP(data)
|
||||
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)
|
||||
s.mu.RLock()
|
||||
if _, ok := s.clientPool[id]; !ok {
|
||||
|
@ -41,6 +41,8 @@ type Server interface {
|
||||
GetSequenceDe() func([]byte) (interface{}, error)
|
||||
SetSequenceDe(func([]byte) (interface{}, error))
|
||||
ShowError(bool)
|
||||
DebugMode(bool)
|
||||
IsDebugMode() bool
|
||||
|
||||
HeartbeatTimeoutSec() int64
|
||||
SetHeartbeatTimeoutSec(int64)
|
||||
|
Loading…
x
Reference in New Issue
Block a user