|
@@ -250,11 +250,11 @@ func (h *Hub) outResponse(response *ResponseData) {
|
|
|
|
|
|
|
|
// 发送数据到网络接口
|
|
// 发送数据到网络接口
|
|
|
// 返回发送的数量
|
|
// 返回发送的数量
|
|
|
-func (h *Hub) sendRequest(gd *GetData) (count int) {
|
|
|
|
|
|
|
+func (h *Hub) sendRequest(gd *GetData) (count int, err error) {
|
|
|
outData, err := h.convertData(gd.Data)
|
|
outData, err := h.convertData(gd.Data)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
log.Println(err)
|
|
log.Println(err)
|
|
|
- return 0
|
|
|
|
|
|
|
+ return 0, err
|
|
|
}
|
|
}
|
|
|
doit := func(_ int, line *Line) bool {
|
|
doit := func(_ int, line *Line) bool {
|
|
|
// 检查连接是否OK
|
|
// 检查连接是否OK
|
|
@@ -331,13 +331,14 @@ func (h *Hub) sendRequest(gd *GetData) (count int) {
|
|
|
if i == 0 && h.connectHostFunc != nil && h.filterToChannelFunc != nil {
|
|
if i == 0 && h.connectHostFunc != nil && h.filterToChannelFunc != nil {
|
|
|
channel := h.filterToChannelFunc(gd.Filter)
|
|
channel := h.filterToChannelFunc(gd.Filter)
|
|
|
if channel == "" {
|
|
if channel == "" {
|
|
|
|
|
+ err = errors.New(IdMsg(NO_MATCH_CONNECT))
|
|
|
log.Println("not channel found")
|
|
log.Println("not channel found")
|
|
|
- return 0
|
|
|
|
|
|
|
+ return 0, err
|
|
|
}
|
|
}
|
|
|
err := h.ConnectToServer(channel, false, nil)
|
|
err := h.ConnectToServer(channel, false, nil)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
log.Println(err)
|
|
log.Println(err)
|
|
|
- return 0
|
|
|
|
|
|
|
+ return 0, err
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
time.Sleep(time.Millisecond * 400) // 故意将时间缩小一点
|
|
time.Sleep(time.Millisecond * 400) // 故意将时间缩小一点
|
|
@@ -430,9 +431,9 @@ func (h *Hub) requestFromNet(request *RequestData) {
|
|
|
// 当前调用将会阻塞,直到命令都执行结束,最后返回执行的数量
|
|
// 当前调用将会阻塞,直到命令都执行结束,最后返回执行的数量
|
|
|
// 如果 backFunc 返回为 false 则提前结束
|
|
// 如果 backFunc 返回为 false 则提前结束
|
|
|
// 最大数量和超时时间如果为0的话表示使用默认值
|
|
// 最大数量和超时时间如果为0的话表示使用默认值
|
|
|
-func (h *Hub) GetWithStruct(gd *GetData, backFunc GetBackFunc) (count int) {
|
|
|
|
|
|
|
+func (h *Hub) GetWithStruct(gd *GetData, backFunc GetBackFunc) (count int, err error) {
|
|
|
if gd.Filter == nil {
|
|
if gd.Filter == nil {
|
|
|
- return 0
|
|
|
|
|
|
|
+ return 0, errors.New("not filter found")
|
|
|
}
|
|
}
|
|
|
if gd.Timeout <= 0 {
|
|
if gd.Timeout <= 0 {
|
|
|
gd.Timeout = h.cf.WriteWait
|
|
gd.Timeout = h.cf.WriteWait
|
|
@@ -440,9 +441,12 @@ func (h *Hub) GetWithStruct(gd *GetData, backFunc GetBackFunc) (count int) {
|
|
|
if gd.backchan == nil {
|
|
if gd.backchan == nil {
|
|
|
gd.backchan = make(chan *ResponseData, 32)
|
|
gd.backchan = make(chan *ResponseData, 32)
|
|
|
}
|
|
}
|
|
|
- sendMax := h.sendRequest(gd)
|
|
|
|
|
|
|
+ sendMax, err := h.sendRequest(gd)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return 0, err
|
|
|
|
|
+ }
|
|
|
if sendMax <= 0 {
|
|
if sendMax <= 0 {
|
|
|
- return 0
|
|
|
|
|
|
|
+ return 0, nil
|
|
|
}
|
|
}
|
|
|
// 避免出现异常时线程无法退出
|
|
// 避免出现异常时线程无法退出
|
|
|
timer := time.NewTimer(time.Millisecond * time.Duration(gd.Timeout+h.cf.WriteWait*2))
|
|
timer := time.NewTimer(time.Millisecond * time.Duration(gd.Timeout+h.cf.WriteWait*2))
|
|
@@ -485,7 +489,7 @@ func (h *Hub) GetWithStruct(gd *GetData, backFunc GetBackFunc) (count int) {
|
|
|
// 请求频道并获取数据,采用回调的方式返回结果
|
|
// 请求频道并获取数据,采用回调的方式返回结果
|
|
|
// 当前调用将会阻塞,直到命令都执行结束,最后返回执行的数量
|
|
// 当前调用将会阻塞,直到命令都执行结束,最后返回执行的数量
|
|
|
// 如果 backFunc 返回为 false 则提前结束
|
|
// 如果 backFunc 返回为 false 则提前结束
|
|
|
-func (h *Hub) Get(filter FilterFunc, cmd string, data any, backFunc GetBackFunc) (count int) {
|
|
|
|
|
|
|
+func (h *Hub) Get(filter FilterFunc, cmd string, data any, backFunc GetBackFunc) (count int, err error) {
|
|
|
return h.GetWithStruct(&GetData{
|
|
return h.GetWithStruct(&GetData{
|
|
|
Filter: filter,
|
|
Filter: filter,
|
|
|
Cmd: cmd,
|
|
Cmd: cmd,
|