|
|
@ -9,13 +9,14 @@ import (
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"b612.me/starainrt"
|
|
|
|
"b612.me/starnet"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// StarNotifyC 为Client端
|
|
|
|
// StarNotifyC 为Client端
|
|
|
|
type StarNotifyC struct {
|
|
|
|
type StarNotifyC struct {
|
|
|
|
Connc net.Conn
|
|
|
|
Connc net.Conn
|
|
|
|
clientSign map[string]chan string
|
|
|
|
dialTimeout time.Duration
|
|
|
|
|
|
|
|
clientSign map[string]chan string
|
|
|
|
// FuncLists 当不使用channel时,使用此记录调用函数
|
|
|
|
// FuncLists 当不使用channel时,使用此记录调用函数
|
|
|
|
FuncLists map[string]func(CMsg)
|
|
|
|
FuncLists map[string]func(CMsg)
|
|
|
|
stopSign context.Context
|
|
|
|
stopSign context.Context
|
|
|
@ -27,7 +28,7 @@ type StarNotifyC struct {
|
|
|
|
UseChannel bool
|
|
|
|
UseChannel bool
|
|
|
|
isUDP bool
|
|
|
|
isUDP bool
|
|
|
|
// Queue 是用来处理收发信息的简单消息队列
|
|
|
|
// Queue 是用来处理收发信息的简单消息队列
|
|
|
|
Queue *starainrt.StarQueue
|
|
|
|
Queue *starnet.StarQueue
|
|
|
|
// Online 当前链接是否处于活跃状态
|
|
|
|
// Online 当前链接是否处于活跃状态
|
|
|
|
Online bool
|
|
|
|
Online bool
|
|
|
|
lockPool map[string]CMsg
|
|
|
|
lockPool map[string]CMsg
|
|
|
@ -43,7 +44,10 @@ type CMsg struct {
|
|
|
|
|
|
|
|
|
|
|
|
func (star *StarNotifyC) starinitc() {
|
|
|
|
func (star *StarNotifyC) starinitc() {
|
|
|
|
star.stopSign, star.cancel = context.WithCancel(context.Background())
|
|
|
|
star.stopSign, star.cancel = context.WithCancel(context.Background())
|
|
|
|
star.Queue = starainrt.NewQueue()
|
|
|
|
star.Queue = starnet.NewQueue()
|
|
|
|
|
|
|
|
star.Queue.EncodeFunc = encodeFunc
|
|
|
|
|
|
|
|
star.Queue.DecodeFunc = decodeFunc
|
|
|
|
|
|
|
|
star.Queue.Encode = true
|
|
|
|
star.FuncLists = make(map[string]func(CMsg))
|
|
|
|
star.FuncLists = make(map[string]func(CMsg))
|
|
|
|
star.UseChannel = false
|
|
|
|
star.UseChannel = false
|
|
|
|
star.Stop = make(chan int, 5)
|
|
|
|
star.Stop = make(chan int, 5)
|
|
|
@ -71,6 +75,45 @@ func (star *StarNotifyC) store(key, value string) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
star.clientSign[key] <- value
|
|
|
|
star.clientSign[key] <- value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewNotifyCWithTimeOut(netype, value string, timeout time.Duration) (*StarNotifyC, error) {
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
var star StarNotifyC
|
|
|
|
|
|
|
|
star.starinitc()
|
|
|
|
|
|
|
|
star.isUDP = false
|
|
|
|
|
|
|
|
if strings.Index(netype, "udp") >= 0 {
|
|
|
|
|
|
|
|
star.isUDP = true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
star.Connc, err = net.DialTimeout(netype, value, timeout)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
star.dialTimeout = timeout
|
|
|
|
|
|
|
|
go star.cnotify()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
|
|
|
<-star.stopSign.Done()
|
|
|
|
|
|
|
|
star.Connc.Close()
|
|
|
|
|
|
|
|
star.Online = false
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
buf := make([]byte, 8192)
|
|
|
|
|
|
|
|
n, err := star.Connc.Read(buf)
|
|
|
|
|
|
|
|
if n != 0 {
|
|
|
|
|
|
|
|
star.Queue.ParseMessage(buf[0:n], star.Connc)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
star.Connc.Close()
|
|
|
|
|
|
|
|
star.ClientStop()
|
|
|
|
|
|
|
|
//star, _ = NewNotifyC(netype, value)
|
|
|
|
|
|
|
|
star.Online = false
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
star.Online = true
|
|
|
|
|
|
|
|
return &star, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewNotifyC 用于新建一个Client端进程
|
|
|
|
// NewNotifyC 用于新建一个Client端进程
|
|
|
|
func NewNotifyC(netype, value string) (*StarNotifyC, error) {
|
|
|
|
func NewNotifyC(netype, value string) (*StarNotifyC, error) {
|
|
|
|