add new feature

This commit is contained in:
2020-10-22 16:20:18 +08:00
parent f2c7a55eda
commit c9cfb6fd8a
4 changed files with 81 additions and 10 deletions
+48 -5
View File
@@ -9,13 +9,14 @@ import (
"strings"
"time"
"b612.me/starainrt"
"b612.me/starnet"
)
// StarNotifyC 为Client端
type StarNotifyC struct {
Connc net.Conn
clientSign map[string]chan string
Connc net.Conn
dialTimeout time.Duration
clientSign map[string]chan string
// FuncLists 当不使用channel时,使用此记录调用函数
FuncLists map[string]func(CMsg)
stopSign context.Context
@@ -27,7 +28,7 @@ type StarNotifyC struct {
UseChannel bool
isUDP bool
// Queue 是用来处理收发信息的简单消息队列
Queue *starainrt.StarQueue
Queue *starnet.StarQueue
// Online 当前链接是否处于活跃状态
Online bool
lockPool map[string]CMsg
@@ -43,7 +44,10 @@ type CMsg struct {
func (star *StarNotifyC) starinitc() {
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.UseChannel = false
star.Stop = make(chan int, 5)
@@ -71,6 +75,45 @@ func (star *StarNotifyC) store(key, value string) {
}
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端进程
func NewNotifyC(netype, value string) (*StarNotifyC, error) {