update
This commit is contained in:
@@ -4,83 +4,96 @@ import (
|
||||
"net"
|
||||
"strings"
|
||||
"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
|
||||
var clientStopSign chan int
|
||||
|
||||
func init() {
|
||||
clientStopSign = make(chan int)
|
||||
clientSign = make(map[string]chan string)
|
||||
func (star *StarNotifyC) starinitc() {
|
||||
star.Queue = starainrt.NewQueue()
|
||||
star.clientStopSign, star.notifychan, star.Stop = make(chan int), make(chan int, 3), make(chan int, 5)
|
||||
star.clientSign = make(map[string]chan string)
|
||||
}
|
||||
|
||||
// Notify 用于获取一个通知
|
||||
func Notify(key string) chan string {
|
||||
if _, ok := clientSign[key]; !ok {
|
||||
func (star *StarNotifyC) Notify(key string) chan string {
|
||||
if _, ok := star.clientSign[key]; !ok {
|
||||
ch := make(chan string, 5)
|
||||
clientSign[key] = ch
|
||||
star.clientSign[key] = ch
|
||||
}
|
||||
return clientSign[key]
|
||||
return star.clientSign[key]
|
||||
}
|
||||
|
||||
func store(key, value string) {
|
||||
if _, ok := clientSign[key]; !ok {
|
||||
func (star *StarNotifyC) store(key, value string) {
|
||||
if _, ok := star.clientSign[key]; !ok {
|
||||
ch := make(chan string, 5)
|
||||
ch <- value
|
||||
clientSign[key] = ch
|
||||
star.clientSign[key] = ch
|
||||
return
|
||||
}
|
||||
clientSign[key] <- value
|
||||
star.clientSign[key] <- value
|
||||
}
|
||||
|
||||
// NewNotifyC 用于新建一个Client端进程
|
||||
func NewNotifyC(netype, value string) error {
|
||||
func NewNotifyC(netype, value string) (StarNotifyC, 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 {
|
||||
return err
|
||||
return star, err
|
||||
}
|
||||
go cnotify()
|
||||
go star.cnotify()
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-clientStopSign:
|
||||
connc.Close()
|
||||
break
|
||||
case <-star.clientStopSign:
|
||||
star.notifychan <- 1
|
||||
star.connc.Close()
|
||||
star.Stop <- 1
|
||||
return
|
||||
default:
|
||||
}
|
||||
buf := make([]byte, 8192)
|
||||
n, err := connc.Read(buf)
|
||||
Queue.ParseMessage(buf[0:n], connc)
|
||||
n, err := star.connc.Read(buf)
|
||||
star.Queue.ParseMessage(buf[0:n], star.connc)
|
||||
if err != nil {
|
||||
connc.Close()
|
||||
notifychan <- 0
|
||||
star.connc.Close()
|
||||
star.Stop <- 1
|
||||
star.notifychan <- 0
|
||||
go NewNotifyC(netype, value)
|
||||
break
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
return star, nil
|
||||
}
|
||||
|
||||
// Send 用于向Server端发送数据
|
||||
func Send(name string) error {
|
||||
_, err := connc.Write(Queue.BuildMessage(name))
|
||||
func (star *StarNotifyC) Send(name string) error {
|
||||
_, err := star.connc.Write(star.Queue.BuildMessage(name))
|
||||
return err
|
||||
}
|
||||
|
||||
func cnotify() {
|
||||
func (star *StarNotifyC) cnotify() {
|
||||
for {
|
||||
select {
|
||||
case <-clientStopSign:
|
||||
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
|
||||
@@ -89,11 +102,11 @@ func cnotify() {
|
||||
if len(strs) < 2 {
|
||||
continue
|
||||
}
|
||||
go store(strs[0], strs[1])
|
||||
go star.store(strs[0], strs[1])
|
||||
}
|
||||
}
|
||||
|
||||
// ClientStop 终止client端运行
|
||||
func ClientStop() {
|
||||
clientStopSign <- 0
|
||||
func (star *StarNotifyC) ClientStop() {
|
||||
star.clientStopSign <- 0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user