master
兔子 3 years ago
parent 07e374b83f
commit 164c412c24

@ -24,8 +24,6 @@ type StarNotifyC struct {
stopSign context.Context stopSign context.Context
cancel context.CancelFunc cancel context.CancelFunc
defaultFunc func(CMsg) defaultFunc func(CMsg)
// Stop 停止信 号
Stop chan int
// UseChannel 是否使用channel作为信息传递 // UseChannel 是否使用channel作为信息传递
UseChannel bool UseChannel bool
isUDP bool isUDP bool
@ -53,11 +51,10 @@ func (star *StarNotifyC) starinitc() {
star.Queue.Encode = true star.Queue.Encode = true
star.FuncLists = make(map[string]func(CMsg)) star.FuncLists = make(map[string]func(CMsg))
star.UseChannel = false star.UseChannel = false
star.Stop = make(chan int, 5)
star.clientSign = make(map[string]chan string) star.clientSign = make(map[string]chan string)
star.Online = false star.Online = false
star.lockPool = make(map[string]CMsg) star.lockPool = make(map[string]CMsg)
star.Queue.RestoreDuration(time.Second * 2) star.Queue.RestoreDuration(time.Millisecond * 50)
} }
// Notify 用于获取一个通知 // Notify 用于获取一个通知
@ -167,6 +164,10 @@ func (star *StarNotifyC) Send(name string) error {
return star.SendValue(name, "") return star.SendValue(name, "")
} }
func (star *StarNotifyC) Stoped() <-chan struct{} {
return star.stopSign.Done()
}
func (star *StarNotifyC) SendValueRaw(key string, msg interface{}) error { func (star *StarNotifyC) SendValueRaw(key string, msg interface{}) error {
encodeData, err := encode(msg) encodeData, err := encode(msg)
if err != nil { if err != nil {
@ -273,7 +274,7 @@ func (star *StarNotifyC) cnotify() {
} }
data, err := star.Queue.RestoreOne() data, err := star.Queue.RestoreOne()
if err != nil { if err != nil {
time.Sleep(time.Microsecond * 2) time.Sleep(time.Millisecond * 500)
continue continue
} }
if string(data.Msg) == "b612ryzstop" { if string(data.Msg) == "b612ryzstop" {
@ -343,9 +344,6 @@ func (star *StarNotifyC) ClientStop() {
star.Send("b612ryzstop") star.Send("b612ryzstop")
} }
star.cancel() star.cancel()
star.Stop <- 1
star.Stop <- 1
star.Stop <- 1
} }
// SetNotify 用于设置关键词的调用函数 // SetNotify 用于设置关键词的调用函数

@ -54,8 +54,6 @@ type StarNotifyS struct {
listener net.Listener listener net.Listener
isUDP bool isUDP bool
Sync bool Sync bool
// Stop 停止信 号
Stop chan int
// UDPConn UDP监听 // UDPConn UDP监听
UDPConn *net.UDPConn UDPConn *net.UDPConn
// Online 当前链接是否处于活跃状态 // Online 当前链接是否处于活跃状态
@ -90,6 +88,9 @@ func (star *StarNotifyS) getName(conn string) string {
} }
return "" return ""
} }
func (star *StarNotifyS) Stoped() <-chan struct{} {
return star.stopSign.Done()
}
// GetConnPool 获取所有Client端信息 // GetConnPool 获取所有Client端信息
func (star *StarNotifyS) GetConnPool() []SMsg { func (star *StarNotifyS) GetConnPool() []SMsg {
@ -233,9 +234,8 @@ func (star *StarNotifyS) starinits() {
star.FuncLists = make(map[string]func(SMsg) string) star.FuncLists = make(map[string]func(SMsg) string)
star.nickName = make(map[string]string) star.nickName = make(map[string]string)
star.lockPool = make(map[string]SMsg) star.lockPool = make(map[string]SMsg)
star.Stop = make(chan int, 5)
star.Online = false star.Online = false
star.Queue.RestoreDuration(time.Second * 2) star.Queue.RestoreDuration(time.Millisecond * 50)
} }
// NewNotifyS 开启一个新的Server端通知 // NewNotifyS 开启一个新的Server端通知
@ -443,10 +443,13 @@ func (star *StarNotifyS) notify() {
} }
data, err := star.Queue.RestoreOne() data, err := star.Queue.RestoreOne()
if err != nil { if err != nil {
time.Sleep(time.Microsecond * 2) time.Sleep(time.Millisecond * 500)
continue continue
} }
mode, key, value := star.analyseData(string(data.Msg)) mode, key, value := star.analyseData(string(data.Msg))
if mode == key && mode == value && mode == "" {
continue
}
var rmsg SMsg var rmsg SMsg
if !star.isUDP { if !star.isUDP {
rmsg = SMsg{data.Conn.(net.Conn), key, value, nil, nil, mode, nil, star.setNickName, star.getName} rmsg = SMsg{data.Conn.(net.Conn), key, value, nil, nil, mode, nil, star.setNickName, star.getName}
@ -507,13 +510,13 @@ func (star *StarNotifyS) notify() {
func (star *StarNotifyS) analyseData(msg string) (mode, key, value string) { func (star *StarNotifyS) analyseData(msg string) (mode, key, value string) {
slice := strings.SplitN(msg, "||", 3) slice := strings.SplitN(msg, "||", 3)
if len(slice) < 3 {
return "", "", ""
}
return slice[0], star.trim(slice[1]), slice[2] return slice[0], star.trim(slice[1]), slice[2]
} }
// ServerStop 用于终止Server端运行 // ServerStop 用于终止Server端运行
func (star *StarNotifyS) ServerStop() { func (star *StarNotifyS) ServerStop() {
star.cancel() star.cancel()
star.Stop <- 1
star.Stop <- 1
star.Stop <- 1
} }

Loading…
Cancel
Save