| | |
| | | package bhomeclient |
| | | |
| | | import ( |
| | | "basic.com/valib/bhshmq.git/api/bhsgo" |
| | | "basic.com/valib/bhshmq.git/proto/source/bhome_msg" |
| | | "basic.com/valib/c_bhomebus.git/api/bhsgo" |
| | | "basic.com/valib/c_bhomebus.git/proto/source/bhome_msg" |
| | | "context" |
| | | "encoding/json" |
| | | "errors" |
| | |
| | | "sync" |
| | | "time" |
| | | "unsafe" |
| | | |
| | | "github.com/bytedance/sonic" |
| | | ) |
| | | |
| | | type MsgReq struct { |
| | |
| | | |
| | | handle.printLog("register done!" ) |
| | | |
| | | handle.wg = &sync.WaitGroup{} |
| | | |
| | | //有订阅消息才需要启动协程接收消息 |
| | | if len(ri.SubTopic) > 0 { |
| | | handle.printLog("sub topics") |
| | |
| | | for { |
| | | select { |
| | | case <-ctx.Done(): |
| | | logFn("recvRoutine ctx.Done") |
| | | logFn("recvSubRoutine ctx.Done") |
| | | wg.Done() |
| | | return |
| | | default: |
| | | if bhsgo.ReadSub(&procId, &msg, 100) { |
| | | ch <- msg |
| | | logFn("ReadSub topic:", string(msg.Topic), " data:", string(msg.Data)) |
| | | logFn("ReadSub topic:", string(msg.Topic), " len(data):", len(msg.Data)) |
| | | |
| | | procId = "" |
| | | msg.Reset() |
| | |
| | | func (h *BHBus) DeRegister(dri *RegisterInfo) error { |
| | | |
| | | h.printLog("DeRegister") |
| | | req := bhome_msg.ProcInfo{ |
| | | ProcId: []byte(h.ri.Proc.ID), |
| | | Name: []byte(h.ri.Proc.Name), |
| | | } |
| | | reply := bhome_msg.MsgCommonReply{} |
| | | if !bhsgo.Unregister(&req, &reply, h.conf.sendTimeOut) { |
| | | h.printLog("Unregister false! ") |
| | | return errors.New("Unregister false! ") |
| | | } |
| | | return nil |
| | | } |
| | | |
| | |
| | | if bhsgo.Request(&dest, req, &pid, &mrt, milliSecs) { |
| | | var reply Reply |
| | | if err := json.Unmarshal(mrt.Data, &reply); err != nil { |
| | | h.printLog("bhsgo.Request ret true, but unmarshal err:", err, " mrt.Data:", string(mrt.Data)) |
| | | return nil,err |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | func (h *BHBus) Reply(src unsafe.Pointer, i *Reply) error { |
| | | data,err := json.Marshal(*i) |
| | | defer func() { |
| | | i = nil |
| | | }() |
| | | |
| | | data,err := sonic.Marshal(i) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | rep := bhome_msg.MsgRequestTopicReply{ |
| | | Data: data, |
| | | } |
| | | |
| | | if bhsgo.SendReply(src, &rep) { |
| | | return nil |
| | | } |