Exemplo n.º 1
0
// handlePUBREL handles the PUBREL Packet.
func (cli *Client) handlePUBREL(p packet.Packet) error {
	// Lock for update.
	cli.muSess.Lock()

	// Unlock.
	defer cli.muSess.Unlock()

	// Extract the Packet Identifier of the Packet.
	id := p.(*packet.PUBREL).PacketID

	// Validate the Packet Identifier.
	if err := cli.validatePacketID(cli.sess.receivingPackets, id, packet.TypePUBLISH); err != nil {
		return err
	}

	// Get the Packet from the Session.
	publish := cli.sess.receivingPackets[id].(*packet.PUBLISH)

	// Lock for reading.
	cli.muConn.RLock()

	// Handle the Application Message.
	cli.handleMessage(publish.TopicName, publish.Message)

	// Unlock.
	cli.muConn.RUnlock()

	// Delete the Packet from the Session
	delete(cli.sess.receivingPackets, id)

	// Create a PUBCOMP Packet.
	pubcomp, err := packet.NewPUBCOMP(&packet.PUBCOMPOptions{
		PacketID: id,
	})
	if err != nil {
		return err
	}

	// Send the Packet to the Server.
	cli.conn.send <- pubcomp

	return nil
}
Exemplo n.º 2
0
func TestClient_handlePacket_PUBCOMP(t *testing.T) {
	cli := New(&Options{
		ErrorHandler: func(_ error) {},
	})

	err := cli.Connect(&ConnectOptions{
		Network:  "tcp",
		Address:  testAddress,
		ClientID: []byte("clientID"),
	})
	if err != nil {
		nilErrorExpected(t, err)
	}

	defer cli.Disconnect()

	p, err := packet.NewPUBCOMP(&packet.PUBCOMPOptions{
		PacketID: 1,
	})

	cli.handlePacket(p)
}