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