func Test_DecodeMessage_pingresp(t *testing.T) { bs := bytes.NewBuffer([]byte{ 0xD0, 0x00, }) presp, _ := packets.ReadPacket(bs) if presp.(*packets.PingrespPacket).MessageType != packets.Pingresp { t.Errorf("DecodeMessage ping response wrong msg type: %v", presp.(*packets.PingrespPacket).MessageType) } if presp.(*packets.PingrespPacket).RemainingLength != 0 { t.Errorf("DecodeMessage ping response wrong rem len: %d", presp.(*packets.PingrespPacket).RemainingLength) } }
// Get will retrieve a message from the store, the one associated with // the provided key value. func (store *FileStore) Get(key string) packets.ControlPacket { store.RLock() defer store.RUnlock() chkcond(store.opened) filepath := fullpath(store.directory, key) if !exists(filepath) { return nil } mfile, oerr := os.Open(filepath) chkerr(oerr) //all, rerr := ioutil.ReadAll(mfile) //chkerr(rerr) msg, rerr := packets.ReadPacket(mfile) chkerr(rerr) cerr := mfile.Close() chkerr(cerr) return msg }
// actually read incoming messages off the wire // send Message object into ibound channel func incoming(c *Client) { var err error defer func(c *Client) { c.workers.Done() if err != nil { ERROR.Println(NET, c.options.ClientID, ":", "incoming closed:", err) c.internalConnLost(err) return } if e := recover(); e != nil { ERROR.Println(NET, c.options.ClientID, ":", "incoming panic closed :", e) c.internalConnLost(fmt.Errorf("读取数据出现严重错误:%v", e)) return } ERROR.Println(NET, c.options.ClientID, ":", "incoming closing") }(c) var cp packets.ControlPacket DEBUG.Println(NET, "incoming started") for { if cp, err = packets.ReadPacket(c.conn); err != nil { return } DEBUG.Println(NET, "Received Message") c.lastContact.update() c.pingOutstanding = false select { case c.ibound <- cp: case <-c.stop: DEBUG.Println(NET, "incoming stopped") return } } }
// This function is only used for receiving a connack // when the connection is first started. // This prevents receiving incoming data while resume // is in progress if clean session is false. func (c *Client) connect() byte { DEBUG.Println(NET, "connect started") ca, err := packets.ReadPacket(c.conn) if err != nil { ERROR.Println(NET, c.options.ClientID+":connect got error", err) return packets.ErrNetworkError } if ca == nil { ERROR.Println(NET, c.options.ClientID+":received nil packet") return packets.ErrNetworkError } msg, ok := ca.(*packets.ConnackPacket) if !ok { ERROR.Println(NET, c.options.ClientID+":received msg that was not CONNACK") return packets.ErrNetworkError } DEBUG.Println(NET, c.options.ClientID+":received connack") return msg.ReturnCode }