|
|
|
@ -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 {
|
|
|
|
|