From d8fcdb4296059621ca1b678aa1af405a146429ff Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期一, 25 一月 2021 15:07:09 +0800
Subject: [PATCH] 在库里使用OpenSocket会导致Ctrl-C退出时共享内存无法释放,改为调用者传入doReq

---
 sbusClient.go |  126 ++++++++++++++++++++++++++++++------------
 1 files changed, 90 insertions(+), 36 deletions(-)

diff --git a/sbusClient.go b/sbusClient.go
index b1e91df..39839f2 100644
--- a/sbusClient.go
+++ b/sbusClient.go
@@ -4,7 +4,6 @@
 	"basic.com/valib/bhomebus.git"
 	"encoding/json"
 	"errors"
-	"fmt"
 	"strconv"
 )
 
@@ -25,7 +24,7 @@
 	Body 			[]byte				`json:"body"`			//璇锋眰鍐呭鎴栬�呭弽棣堢粨鏋�
 }
 
-type request struct {
+type Request struct {
 	Path        			string              	`json:"path"`
 	Method      			string              	`json:"method"`
 	ContentType 			string              	`json:"contentType"`
@@ -47,21 +46,35 @@
 		return nil, errors.New("invalid netNodes")
 	}
 
-	req := request {
+	req := Request{
 		Path:        url,
 		Method:      "GET",
 		ContentType: CONTENT_TYPE_JSON,
 	}
 	fillParam(&req, headers, params, nil)
 
-	return doReq(req, sc.nodes)
+	rb, err := json.Marshal(req)
+	if err !=nil {
+		return nil,err
+	}
+	rMsg := MsgInfo{
+		Topic: req.Path,
+		Body: rb,
+	}
+	rData, err := json.Marshal(rMsg)
+	if err != nil {
+		return nil, err
+	}
+
+	return busReq(rData, sc.nodes)
+	//return doReq(req, sc.nodes)
 }
 
 func (sc SBusClient) DoPostRequest(url string, contentType string, body map[string]interface{}, params map[string]string, headers map[string]string) ([]byte, error) {
 	if sc.nodes == nil || len(sc.nodes) == 0 {
 		return nil, errors.New("invalid port")
 	}
-	req := request {
+	req := Request{
 		Path:        url,
 		Method:      "POST",
 		ContentType: contentType,
@@ -96,38 +109,77 @@
 		}
 	}
 
-	return doReq(req, sc.nodes)
+	rb, err := json.Marshal(req)
+	if err !=nil {
+		return nil,err
+	}
+	rMsg := MsgInfo{
+		Topic: req.Path,
+		Body: rb,
+	}
+	rData, err := json.Marshal(rMsg)
+	if err != nil {
+		return nil, err
+	}
+
+	return busReq(rData, sc.nodes)
 }
 
 func (sc SBusClient) DoPutRequest(url string, contentType string, body map[string]interface{}, headers map[string]string) ([]byte, error) {
 	if sc.nodes == nil || len(sc.nodes) == 0 {
 		return nil, errors.New("invalid port")
 	}
-	req := request {
+	req := Request{
 		Path:        url,
 		Method:      "PUT",
 		ContentType: contentType,
 	}
 	fillParam(&req, headers, nil, body)
 
-	return doReq(req, sc.nodes)
+	rb, err := json.Marshal(req)
+	if err !=nil {
+		return nil,err
+	}
+	rMsg := MsgInfo{
+		Topic: req.Path,
+		Body: rb,
+	}
+	rData, err := json.Marshal(rMsg)
+	if err != nil {
+		return nil, err
+	}
+
+	return busReq(rData, sc.nodes)
 }
 
 func (sc SBusClient) DoDeleteRequest(url string, contentType string, body map[string]interface{}, headers map[string]string) ([]byte, error) {
 	if sc.nodes == nil || len(sc.nodes) == 0 {
 		return nil, errors.New("invalid port")
 	}
-	req := request {
+	req := Request{
 		Path:        url,
 		Method:      "DELETE",
 		ContentType: contentType,
 	}
 	fillParam(&req, headers, nil, body)
 
-	return doReq(req, sc.nodes)
+	rb, err := json.Marshal(req)
+	if err !=nil {
+		return nil,err
+	}
+	rMsg := MsgInfo{
+		Topic: req.Path,
+		Body: rb,
+	}
+	rData, err := json.Marshal(rMsg)
+	if err != nil {
+		return nil, err
+	}
+
+	return busReq(rData, sc.nodes)
 }
 
-func fillParam(req *request,headers map[string]string, params map[string]string, body map[string]interface{}) {
+func fillParam(req *Request,headers map[string]string, params map[string]string, body map[string]interface{}) {
 	headerMap := make(map[string][]string)
 	queryMap := make(map[string][]string)
 	if headers != nil {
@@ -151,28 +203,30 @@
 
 }
 
-func doReq(req request, nodes []bhomebus.NetNode) ([]byte,error) {
-	rb, err := json.Marshal(req)
-	if err !=nil {
-		return nil,err
-	}
-	rMsg := MsgInfo{
-		Topic: req.Path,
-		Body: rb,
-	}
-	data, err := json.Marshal(rMsg)
-	if err != nil {
-		return nil, err
-	}
-	s := bhomebus.OpenSocket()
-	defer s.Close()
-	var ret []bhomebus.Mesg
-	if n := s.SendandrecvTimeout(nodes, data, &ret, 5000);n == 0 {  //n==0琛ㄧず娌℃湁璇锋眰鎴愬姛
-		return nil, fmt.Errorf("doReq s.SendandrecvTimeout result n:%d", n)
-	} else {
-		if len(ret) > 0 {
-			return ret[0].Data, nil
-		}
-		return nil, fmt.Errorf("no any response")
-	}
-}
\ No newline at end of file
+//鍦ㄦ澶勪娇鐢∣penSocket浼氬湪Ctrl-C鐨勬椂鍊欙紝瀵艰嚧socket骞舵湭鎴愬姛Close锛屽叡浜唴瀛樺潡涓嶄細閲婃斁.
+//鎵�浠ユ帶鍒跺叡浜唴瀛樺潡鐨勬垚鍔熷洖鏀堕渶瑕佸湪涓婂眰鍋�,鐒跺悗璋僆nitDoReq灏嗗嚱鏁版寚閽堜紶閫掕繘鏉�
+//func doReq(req Request, nodes []bhomebus.NetNode) ([]byte,error) {
+//	rb, err := json.Marshal(req)
+//	if err !=nil {
+//		return nil,err
+//	}
+//	rMsg := MsgInfo{
+//		Topic: req.Path,
+//		Body: rb,
+//	}
+//	data, err := json.Marshal(rMsg)
+//	if err != nil {
+//		return nil, err
+//	}
+//	s := bhomebus.OpenSocket()
+//	defer s.Close()
+//	var ret []bhomebus.Mesg
+//	if n := s.SendandrecvTimeout(nodes, data, &ret, 5000);n == 0 {  //n==0琛ㄧず娌℃湁璇锋眰鎴愬姛
+//		return nil, fmt.Errorf("doReq s.SendandrecvTimeout result n:%d", n)
+//	} else {
+//		if len(ret) > 0 {
+//			return ret[0].Data, nil
+//		}
+//		return nil, fmt.Errorf("no any response")
+//	}
+//}
\ No newline at end of file

--
Gitblit v1.8.0