This commit is contained in:
2019-11-22 10:24:39 +08:00
parent 0ca8a71e8d
commit 1595a73e7e
2 changed files with 110 additions and 71 deletions
+58 -32
View File
@@ -8,13 +8,22 @@ import (
"b612.me/starainrt"
)
// Queue 是用来处理收发信息的简单消息队列
var Queue *starainrt.StarQueue
var builder *starainrt.StarQueue
// FuncLists 记录了被通知项所记录的函数
var FuncLists map[string]func(NetMsg) string
var serverStopSign chan int
var notifychan chan int
func init() {
builder = starainrt.NewQueue()
}
// StarNotifyS 为Server端
type StarNotifyS struct {
// Queue 是用来处理收发信息的简单消息队列
Queue *starainrt.StarQueue
// FuncLists 记录了被通知项所记录的函数
FuncLists map[string]func(NetMsg) string
defaultFunc func(NetMsg) string
serverStopSign chan int
notifychan chan int
}
// NetMsg 指明当前被通知的关键字
type NetMsg struct {
@@ -24,27 +33,30 @@ type NetMsg struct {
// Send 用于向client端发送数据
func (nmsg *NetMsg) Send(msg string) error {
_, err := nmsg.Conn.Write(Queue.BuildMessage(nmsg.key + "||" + msg))
_, err := nmsg.Conn.Write(builder.BuildMessage(nmsg.key + "||" + msg))
return err
}
func init() {
serverStopSign, notifychan = make(chan int), make(chan int)
Queue = starainrt.NewQueue()
FuncLists = make(map[string]func(NetMsg) string)
func (star *StarNotifyS) starinits() {
star.serverStopSign, star.notifychan = make(chan int), make(chan int, 3)
star.Queue = starainrt.NewQueue()
star.FuncLists = make(map[string]func(NetMsg) string)
}
// NewNotifyS 开启一个新的Server端通知
func NewNotifyS(netype, value string) error {
func NewNotifyS(netype, value string) (StarNotifyS, error) {
var star StarNotifyS
star.starinits()
listener, err := net.Listen(netype, value)
if err == nil {
go notify()
go star.notify()
go func() {
for {
select {
case <-serverStopSign:
case <-star.serverStopSign:
star.notifychan <- 1
listener.Close()
break
return
default:
}
conn, err := listener.Accept()
@@ -54,14 +66,16 @@ func NewNotifyS(netype, value string) error {
go func(conn net.Conn) {
for {
select {
case <-serverStopSign:
break
case <-star.serverStopSign:
star.notifychan <- 1
conn.Close()
return
default:
}
buf := make([]byte, 8192)
n, err := conn.Read(buf)
if n != 0 {
Queue.ParseMessage(buf[0:n], conn)
star.Queue.ParseMessage(buf[0:n], conn)
}
if err != nil {
conn.Close()
@@ -72,40 +86,52 @@ func NewNotifyS(netype, value string) error {
}
}()
}
return err
return star, err
}
// SetNotify 用于设置通知关键词调用函数
func SetNotify(name string, data func(NetMsg) string) {
FuncLists[name] = data
// SetNotify 用于设置通知关键词调用函数
func (star *StarNotifyS) SetNotify(name string, data func(NetMsg) string) {
star.FuncLists[name] = data
}
func notify() {
// SetDefaultNotify 用于设置默认关键词的调用函数
func (star *StarNotifyS) SetDefaultNotify(name string, data func(NetMsg) string) {
star.defaultFunc = data
}
func (star *StarNotifyS) notify() {
for {
select {
case <-serverStopSign:
break
case <-notifychan:
break
case <-star.notifychan:
return
default:
}
data, err := Queue.RestoreOne()
data, err := star.Queue.RestoreOne()
if err != nil {
time.Sleep(time.Millisecond * 20)
continue
}
if msg, ok := FuncLists[data.Msg]; ok {
if msg, ok := star.FuncLists[data.Msg]; ok {
sdata := msg(NetMsg{data.Conn.(net.Conn), data.Msg})
if sdata == "" {
continue
}
sdata = data.Msg + "||" + sdata
data.Conn.(net.Conn).Write(Queue.BuildMessage(sdata))
data.Conn.(net.Conn).Write(star.Queue.BuildMessage(sdata))
} else {
if star.defaultFunc != nil {
sdata := star.defaultFunc(NetMsg{data.Conn.(net.Conn), data.Msg})
if sdata == "" {
continue
}
sdata = data.Msg + "||" + sdata
data.Conn.(net.Conn).Write(star.Queue.BuildMessage(sdata))
}
}
}
}
// ServerStop 用于终止Server端运行
func ServerStop() {
serverStopSign <- 0
func (star *StarNotifyS) ServerStop() {
star.serverStopSign <- 0
}