|
|
|
@ -2,6 +2,7 @@
|
|
|
|
|
package notify
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"net"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
@ -20,13 +21,13 @@ type StarNotifyS struct {
|
|
|
|
|
// Queue 是用来处理收发信息的简单消息队列
|
|
|
|
|
Queue *starainrt.StarQueue
|
|
|
|
|
// FuncLists 记录了被通知项所记录的函数
|
|
|
|
|
FuncLists map[string]func(SMsg) string
|
|
|
|
|
defaultFunc func(SMsg) string
|
|
|
|
|
serverStopSign chan int
|
|
|
|
|
notifychan chan int
|
|
|
|
|
connPool map[string]net.Conn
|
|
|
|
|
udpPool map[string]*net.UDPAddr
|
|
|
|
|
isUDP bool
|
|
|
|
|
FuncLists map[string]func(SMsg) string
|
|
|
|
|
defaultFunc func(SMsg) string
|
|
|
|
|
stopSign context.Context
|
|
|
|
|
cancel context.CancelFunc
|
|
|
|
|
connPool map[string]net.Conn
|
|
|
|
|
udpPool map[string]*net.UDPAddr
|
|
|
|
|
isUDP bool
|
|
|
|
|
// UDPConn UDP监听
|
|
|
|
|
UDPConn *net.UDPConn
|
|
|
|
|
// Online 当前链接是否处于活跃状态
|
|
|
|
@ -53,9 +54,9 @@ type SMsg struct {
|
|
|
|
|
func (nmsg *SMsg) Reply(msg string) error {
|
|
|
|
|
var err error
|
|
|
|
|
if nmsg.uconn == nil {
|
|
|
|
|
_, err = nmsg.Conn.Write(builder.BuildMessage(nmsg.Key + "||" + msg))
|
|
|
|
|
_, err = nmsg.Conn.Write(builder.BuildMessage([]byte(nmsg.Key + "||" + msg)))
|
|
|
|
|
} else {
|
|
|
|
|
_, err = nmsg.uconn.WriteToUDP(builder.BuildMessage(nmsg.Key+"||"+msg), nmsg.UDP)
|
|
|
|
|
_, err = nmsg.uconn.WriteToUDP(builder.BuildMessage([]byte(nmsg.Key+"||"+msg)), nmsg.UDP)
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
@ -64,20 +65,21 @@ func (nmsg *SMsg) Reply(msg string) error {
|
|
|
|
|
func (nmsg *SMsg) Send(key, value string) error {
|
|
|
|
|
var err error
|
|
|
|
|
if nmsg.uconn == nil {
|
|
|
|
|
_, err = nmsg.Conn.Write(builder.BuildMessage(key + "||" + value))
|
|
|
|
|
_, err = nmsg.Conn.Write(builder.BuildMessage([]byte(key + "||" + value)))
|
|
|
|
|
} else {
|
|
|
|
|
_, err = nmsg.uconn.WriteToUDP(builder.BuildMessage(key+"||"+value), nmsg.UDP)
|
|
|
|
|
_, err = nmsg.uconn.WriteToUDP(builder.BuildMessage([]byte(key+"||"+value)), nmsg.UDP)
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (star *StarNotifyS) starinits() {
|
|
|
|
|
star.serverStopSign, star.notifychan = make(chan int, 1), make(chan int, 5)
|
|
|
|
|
star.stopSign, star.cancel = context.WithCancel(context.Background())
|
|
|
|
|
star.Queue = starainrt.NewQueue()
|
|
|
|
|
star.udpPool = make(map[string]*net.UDPAddr)
|
|
|
|
|
star.FuncLists = make(map[string]func(SMsg) string)
|
|
|
|
|
star.connPool = make(map[string]net.Conn)
|
|
|
|
|
star.Online = false
|
|
|
|
|
star.Queue.RestoreDuration(time.Second * 2)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewNotifyS 开启一个新的Server端通知
|
|
|
|
@ -102,11 +104,9 @@ func doudps(netype, value string) (*StarNotifyS, error) {
|
|
|
|
|
}
|
|
|
|
|
go star.notify()
|
|
|
|
|
go func() {
|
|
|
|
|
<-star.serverStopSign
|
|
|
|
|
star.notifychan <- 1
|
|
|
|
|
star.notifychan <- 2
|
|
|
|
|
<-star.stopSign.Done()
|
|
|
|
|
for k, v := range star.udpPool {
|
|
|
|
|
star.UDPConn.WriteToUDP(star.Queue.BuildMessage("b612ryzstop"), v)
|
|
|
|
|
star.UDPConn.WriteToUDP(star.Queue.BuildMessage([]byte("b612ryzstop")), v)
|
|
|
|
|
delete(star.connPool, k)
|
|
|
|
|
}
|
|
|
|
|
star.UDPConn.Close()
|
|
|
|
@ -140,9 +140,7 @@ func notudps(netype, value string) (*StarNotifyS, error) {
|
|
|
|
|
}
|
|
|
|
|
go star.notify()
|
|
|
|
|
go func() {
|
|
|
|
|
<-star.serverStopSign
|
|
|
|
|
star.notifychan <- 3
|
|
|
|
|
star.notifychan <- 4
|
|
|
|
|
<-star.stopSign.Done()
|
|
|
|
|
for k, v := range star.connPool {
|
|
|
|
|
v.Close()
|
|
|
|
|
delete(star.connPool, k)
|
|
|
|
@ -156,7 +154,7 @@ func notudps(netype, value string) (*StarNotifyS, error) {
|
|
|
|
|
conn, err := listener.Accept()
|
|
|
|
|
if err != nil {
|
|
|
|
|
select {
|
|
|
|
|
case <-star.notifychan:
|
|
|
|
|
case <-star.stopSign.Done():
|
|
|
|
|
listener.Close()
|
|
|
|
|
return
|
|
|
|
|
default:
|
|
|
|
@ -206,7 +204,7 @@ func (star *StarNotifyS) SetDefaultNotify(name string, data func(SMsg) string) {
|
|
|
|
|
func (star *StarNotifyS) notify() {
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-star.notifychan:
|
|
|
|
|
case <-star.stopSign.Done():
|
|
|
|
|
return
|
|
|
|
|
default:
|
|
|
|
|
}
|
|
|
|
@ -215,7 +213,7 @@ func (star *StarNotifyS) notify() {
|
|
|
|
|
time.Sleep(time.Millisecond * 20)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
key, value := analyseData(data.Msg)
|
|
|
|
|
key, value := analyseData(string(data.Msg))
|
|
|
|
|
var rmsg SMsg
|
|
|
|
|
if !star.isUDP {
|
|
|
|
|
rmsg = SMsg{data.Conn.(net.Conn), key, value, nil, nil}
|
|
|
|
@ -256,5 +254,5 @@ func analyseData(msg string) (key, value string) {
|
|
|
|
|
|
|
|
|
|
// ServerStop 用于终止Server端运行
|
|
|
|
|
func (star *StarNotifyS) ServerStop() {
|
|
|
|
|
star.serverStopSign <- 0
|
|
|
|
|
star.cancel()
|
|
|
|
|
}
|
|
|
|
|