liuxiaolong
2021-01-20 0120491f6551545b19114de049713c7e928ebc01
hbusc.go
@@ -156,22 +156,25 @@
         var rMsg []bhomebus.Mesg
         n := regSock.Sendandrecv(regAddr, msg, &rMsg) //n代表成功发送的节点的个数
         if n == 1 && len(rMsg) == 1 {
            var cr CommonReply
            var cr Reply
            if err = json.Unmarshal(rMsg[0].Data, &cr);err != nil {
               handle.printLog("unmarshal regReply err:", err)
               return nil, errors.New("unmarshal regReply err:"+err.Error())
            } else {
               if cr.Status == REPLY_SUCCESS {
               if cr.Success {
                  if rpd,err := json.Marshal(cr.Data);err ==nil {
                  var rr RegisterReply
                  if err = json.Unmarshal(cr.Body, &rr);err ==nil {
                     if err = json.Unmarshal(rpd, &rr); err == nil {
                     regR = &rr
                     break loop
                  } else {
                     handle.printLog("unmarshal RegisterReply err:", err)
                  }
               } else {
                  handle.printLog("cr.Status:", cr.Status, "Desc:", cr.Desc)
                     handle.printLog("marshal cr.Data err:", err)
                  }
               } else {
                  handle.printLog("cr:", cr)
               }
            }
@@ -388,31 +391,37 @@
   var ret []bhomebus.Mesg
   n := h.sockWorker.sock.SendandrecvTimeout(reqNetNode, reqD, &ret, h.conf.sendTimeOut)
   if n > 0 {
      var reply CommonReply
      var reply Reply
      err = json.Unmarshal(ret[0].Data, &reply)
      if err != nil {
         h.printLog("unmarshal err:", err)
         return nil, err
      }
      if reply.Status == REPLY_SUCCESS {
         err = json.Unmarshal(reply.Body, &nodes)
      if reply.Success {
         rd,err := json.Marshal(reply.Data)
         if err == nil {
            err = json.Unmarshal(rd, &nodes)
         if err == nil {
            return nodes, nil
         } else {
            h.printLog("unmarshal err:", err, "nodes:", nodes)
            return nil, fmt.Errorf("unmarshal reply.Body err:%s", err.Error())
               return nil, fmt.Errorf("unmarshal to nodes err:%s", err.Error())
         }
      } else {
         h.printLog("reply status:", reply.Status, "desc:", reply.Desc, "body:", string(reply.Body))
         return nil, fmt.Errorf("REPLY STATUS:%d", reply.Status)
            return nil, fmt.Errorf("marshal reply.Data err:%s", err.Error())
         }
      } else {
         h.printLog("reply success:", reply.Success, "msg:", reply.Msg, "data:", reply.Data)
         return nil, fmt.Errorf("REPLY msg:%s", reply.Msg)
      }
   } else {
      return nil,   fmt.Errorf("GetNetNodeByTopic ret n:%d", n)
   }
}
func (h *BHBus) Request(serverId string, req *MsgInfo, milliSecs int) (*MsgInfo, error) {
func (h *BHBus) Request(serverId string, req *MsgInfo, milliSecs int) (*Reply, error) {
   //1.首先需要通过topic拿到本机对应的NetNode
   rNodes, err := h.GetNetNodeByTopic(serverId, &req.SrcProc, req.Topic)
   h.printLog("topic", req.Topic, "rNodes:", rNodes, "err:", err)
@@ -431,7 +440,7 @@
   h.printLog("Request n: ", n, " len(ret): ", len(ret))
   if n > 0 && len(ret) > 0 {
      var resp MsgInfo
      var resp Reply
      if err = json.Unmarshal(ret[0].Data, &resp); err == nil {
         return &resp, nil
      } else {
@@ -442,8 +451,8 @@
   return nil, fmt.Errorf("request err")
}
func (h *BHBus) Reply(replyKey int, i MsgInfo) error {
   data,err := json.Marshal(i)
func (h *BHBus) Reply(replyKey int, i *Reply) error {
   data,err := json.Marshal(*i)
   if err != nil {
      return err
   }
@@ -473,7 +482,7 @@
   return nil
}
func (h *BHBus) RequestCenter(req *MsgInfo) (*CommonReply, error) {
func (h *BHBus) RequestCenter(req *MsgInfo) (*Reply, error) {
   data, err := json.Marshal(*req)
   if err != nil {
      return nil, err
@@ -487,7 +496,7 @@
   n := h.sockWorker.sock.SendandrecvTimeout(rNodes, data,&ret, h.conf.sendTimeOut)
   h.printLog("requestCenter n:", n, "len(ret):", len(ret))
   if n > 0 && len(ret) > 0{
      var cr CommonReply
      var cr Reply
      if err = json.Unmarshal(ret[0].Data, &cr); err == nil {
         return &cr, nil
      } else {