Пример #1
0
func (c *client) recvLoop() {
	delay := backoff.Exp{Min: time.Millisecond * 5}
loop:
	for {
		p, err := packet.SplitDecode(c.r)
		if err != nil {
			if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
				c.logTemporaryError(nerr)
				delay.Wait()
				continue
			}
			c.stop(err)
			break loop
		}
		delay.Reset()
		if err := c.dispatch(p); err != nil {
			c.stop(err)
			break loop
		}
	}
	if c.p.OnDisconnect != nil {
		c.p.OnDisconnect(c.derr, c.p)
	}
	c.r = nil
}
Пример #2
0
// Connect connects to MQTT broker and returns a Client.
func Connect(p Param) (Client, error) {
	c, err := dial(p)
	if err != nil {
		return nil, err
	}
	r := p.newPacketReader(c)

	// send CONNECT packet.
	bc, err := p.connectPacket().Encode()
	if err != nil {
		c.Close()
		return nil, err
	}
	_, err = c.Write(bc)
	if err != nil {
		c.Close()
		return nil, err
	}

	// receive CONNACK packet.
	rp, err := packet.SplitDecode(r)
	if err != nil {
		c.Close()
		return nil, err
	}
	ack, ok := rp.(*packet.ConnACK)
	if !ok {
		c.Close()
		return nil, errors.New("received non CONNACK")
	}
	if ack.ReturnCode != packet.ConnectAccept {
		return nil, ack.ReturnCode
	}

	opts := p.options()
	cl := &client{
		conn: c,
		quit: make(chan bool, 1),
		r:    r,
		p:    p,
		log:  opts.Logger,
		kd:   opts.keepAliveInterval(),
	}
	cl.start()
	return cl, nil
}
Пример #3
0
func (c *client) recvLoop() error {
	delay := backoff.Exp{Min: time.Millisecond * 5}
	for {
		p, err := packet.SplitDecode(c.rd)
		select {
		case <-c.quit:
			return nil
		default:
		}
		if err != nil {
			if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
				c.srv.logTemporaryError(nerr, &delay, c)
				delay.Wait()
				continue
			}
			c.terminate()
			return err
		}
		delay.Reset()
		c.monitorExtend()
		err = c.process(p)
		if err != nil {
			if aerr, ok := err.(AdapterError); ok {
				if aerr.Continue() {
					c.srv.logAdapterError(aerr, p, c)
					continue
				}
				if aerr == ErrDisconnected {
					c.terminate()
					return nil
				}
			}
			c.terminate()
			return err
		}
	}
}