update
This commit is contained in:
parent
0ca8a71e8d
commit
1595a73e7e
91
client.go
91
client.go
@ -4,83 +4,96 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"b612.me/starainrt"
|
||||||
)
|
)
|
||||||
|
|
||||||
var connc net.Conn
|
// StarNotifyC 为Client端
|
||||||
|
type StarNotifyC struct {
|
||||||
|
connc net.Conn
|
||||||
|
clientSign map[string]chan string
|
||||||
|
clientStopSign chan int
|
||||||
|
// Stop 停止信号
|
||||||
|
Stop chan int
|
||||||
|
notifychan chan int
|
||||||
|
// Queue 是用来处理收发信息的简单消息队列
|
||||||
|
Queue *starainrt.StarQueue
|
||||||
|
}
|
||||||
|
|
||||||
var clientSign map[string]chan string
|
func (star *StarNotifyC) starinitc() {
|
||||||
var clientStopSign chan int
|
star.Queue = starainrt.NewQueue()
|
||||||
|
star.clientStopSign, star.notifychan, star.Stop = make(chan int), make(chan int, 3), make(chan int, 5)
|
||||||
func init() {
|
star.clientSign = make(map[string]chan string)
|
||||||
clientStopSign = make(chan int)
|
|
||||||
clientSign = make(map[string]chan string)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Notify 用于获取一个通知
|
// Notify 用于获取一个通知
|
||||||
func Notify(key string) chan string {
|
func (star *StarNotifyC) Notify(key string) chan string {
|
||||||
if _, ok := clientSign[key]; !ok {
|
if _, ok := star.clientSign[key]; !ok {
|
||||||
ch := make(chan string, 5)
|
ch := make(chan string, 5)
|
||||||
clientSign[key] = ch
|
star.clientSign[key] = ch
|
||||||
}
|
}
|
||||||
return clientSign[key]
|
return star.clientSign[key]
|
||||||
}
|
}
|
||||||
|
|
||||||
func store(key, value string) {
|
func (star *StarNotifyC) store(key, value string) {
|
||||||
if _, ok := clientSign[key]; !ok {
|
if _, ok := star.clientSign[key]; !ok {
|
||||||
ch := make(chan string, 5)
|
ch := make(chan string, 5)
|
||||||
ch <- value
|
ch <- value
|
||||||
clientSign[key] = ch
|
star.clientSign[key] = ch
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
clientSign[key] <- value
|
star.clientSign[key] <- value
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewNotifyC 用于新建一个Client端进程
|
// NewNotifyC 用于新建一个Client端进程
|
||||||
func NewNotifyC(netype, value string) error {
|
func NewNotifyC(netype, value string) (StarNotifyC, error) {
|
||||||
var err error
|
var err error
|
||||||
connc, err = net.Dial(netype, value)
|
var star StarNotifyC
|
||||||
|
star.starinitc()
|
||||||
|
star.connc, err = net.Dial(netype, value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return star, err
|
||||||
}
|
}
|
||||||
go cnotify()
|
go star.cnotify()
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-clientStopSign:
|
case <-star.clientStopSign:
|
||||||
connc.Close()
|
star.notifychan <- 1
|
||||||
break
|
star.connc.Close()
|
||||||
|
star.Stop <- 1
|
||||||
|
return
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
buf := make([]byte, 8192)
|
buf := make([]byte, 8192)
|
||||||
n, err := connc.Read(buf)
|
n, err := star.connc.Read(buf)
|
||||||
Queue.ParseMessage(buf[0:n], connc)
|
star.Queue.ParseMessage(buf[0:n], star.connc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
connc.Close()
|
star.connc.Close()
|
||||||
notifychan <- 0
|
star.Stop <- 1
|
||||||
|
star.notifychan <- 0
|
||||||
go NewNotifyC(netype, value)
|
go NewNotifyC(netype, value)
|
||||||
break
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
return nil
|
return star, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send 用于向Server端发送数据
|
// Send 用于向Server端发送数据
|
||||||
func Send(name string) error {
|
func (star *StarNotifyC) Send(name string) error {
|
||||||
_, err := connc.Write(Queue.BuildMessage(name))
|
_, err := star.connc.Write(star.Queue.BuildMessage(name))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func cnotify() {
|
func (star *StarNotifyC) cnotify() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-clientStopSign:
|
case <-star.notifychan:
|
||||||
break
|
return
|
||||||
case <-notifychan:
|
|
||||||
break
|
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
data, err := Queue.RestoreOne()
|
data, err := star.Queue.RestoreOne()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
time.Sleep(time.Millisecond * 20)
|
time.Sleep(time.Millisecond * 20)
|
||||||
continue
|
continue
|
||||||
@ -89,11 +102,11 @@ func cnotify() {
|
|||||||
if len(strs) < 2 {
|
if len(strs) < 2 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
go store(strs[0], strs[1])
|
go star.store(strs[0], strs[1])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ClientStop 终止client端运行
|
// ClientStop 终止client端运行
|
||||||
func ClientStop() {
|
func (star *StarNotifyC) ClientStop() {
|
||||||
clientStopSign <- 0
|
star.clientStopSign <- 0
|
||||||
}
|
}
|
||||||
|
90
server.go
90
server.go
@ -8,13 +8,22 @@ import (
|
|||||||
"b612.me/starainrt"
|
"b612.me/starainrt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Queue 是用来处理收发信息的简单消息队列
|
var builder *starainrt.StarQueue
|
||||||
var Queue *starainrt.StarQueue
|
|
||||||
|
|
||||||
// FuncLists 记录了被通知项所记录的函数
|
func init() {
|
||||||
var FuncLists map[string]func(NetMsg) string
|
builder = starainrt.NewQueue()
|
||||||
var serverStopSign chan int
|
}
|
||||||
var notifychan chan int
|
|
||||||
|
// 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 指明当前被通知的关键字
|
// NetMsg 指明当前被通知的关键字
|
||||||
type NetMsg struct {
|
type NetMsg struct {
|
||||||
@ -24,27 +33,30 @@ type NetMsg struct {
|
|||||||
|
|
||||||
// Send 用于向client端发送数据
|
// Send 用于向client端发送数据
|
||||||
func (nmsg *NetMsg) Send(msg string) error {
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func (star *StarNotifyS) starinits() {
|
||||||
serverStopSign, notifychan = make(chan int), make(chan int)
|
star.serverStopSign, star.notifychan = make(chan int), make(chan int, 3)
|
||||||
Queue = starainrt.NewQueue()
|
star.Queue = starainrt.NewQueue()
|
||||||
FuncLists = make(map[string]func(NetMsg) string)
|
star.FuncLists = make(map[string]func(NetMsg) string)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewNotifyS 开启一个新的Server端通知
|
// 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)
|
listener, err := net.Listen(netype, value)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
go notify()
|
go star.notify()
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-serverStopSign:
|
case <-star.serverStopSign:
|
||||||
|
star.notifychan <- 1
|
||||||
listener.Close()
|
listener.Close()
|
||||||
break
|
return
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
conn, err := listener.Accept()
|
conn, err := listener.Accept()
|
||||||
@ -54,14 +66,16 @@ func NewNotifyS(netype, value string) error {
|
|||||||
go func(conn net.Conn) {
|
go func(conn net.Conn) {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-serverStopSign:
|
case <-star.serverStopSign:
|
||||||
break
|
star.notifychan <- 1
|
||||||
|
conn.Close()
|
||||||
|
return
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
buf := make([]byte, 8192)
|
buf := make([]byte, 8192)
|
||||||
n, err := conn.Read(buf)
|
n, err := conn.Read(buf)
|
||||||
if n != 0 {
|
if n != 0 {
|
||||||
Queue.ParseMessage(buf[0:n], conn)
|
star.Queue.ParseMessage(buf[0:n], conn)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
conn.Close()
|
conn.Close()
|
||||||
@ -72,40 +86,52 @@ func NewNotifyS(netype, value string) error {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
return err
|
return star, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetNotify 用于设置通知关键词和调用函数
|
// SetNotify 用于设置通知关键词的调用函数
|
||||||
func SetNotify(name string, data func(NetMsg) string) {
|
func (star *StarNotifyS) SetNotify(name string, data func(NetMsg) string) {
|
||||||
FuncLists[name] = data
|
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 {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-serverStopSign:
|
case <-star.notifychan:
|
||||||
break
|
return
|
||||||
case <-notifychan:
|
|
||||||
break
|
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
data, err := Queue.RestoreOne()
|
data, err := star.Queue.RestoreOne()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
time.Sleep(time.Millisecond * 20)
|
time.Sleep(time.Millisecond * 20)
|
||||||
continue
|
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})
|
sdata := msg(NetMsg{data.Conn.(net.Conn), data.Msg})
|
||||||
if sdata == "" {
|
if sdata == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
sdata = data.Msg + "||" + sdata
|
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端运行
|
// ServerStop 用于终止Server端运行
|
||||||
func ServerStop() {
|
func (star *StarNotifyS) ServerStop() {
|
||||||
serverStopSign <- 0
|
star.serverStopSign <- 0
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user