示例#1
0
func TestClient_validatePacketID_ErrInvalidPacketID(t *testing.T) {
	cli := New(nil)

	p, err := packet.NewPUBACK(&packet.PUBACKOptions{
		PacketID: 1,
	})
	if err != nil {
		nilErrorExpected(t, err)
	}

	if err := cli.validatePacketID(map[uint16]packet.Packet{1: p}, 1, packet.TypePUBLISH); err != packet.ErrInvalidPacketID {
		invalidError(t, err, packet.ErrInvalidPacketID)
	}
}
示例#2
0
func TestClient_handlePacket_PUBACK(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.NewPUBACK(&packet.PUBACKOptions{
		PacketID: 1,
	})

	cli.handlePacket(p)
}
示例#3
0
文件: client.go 项目: mezzato/gmq
// handlePUBLISH handles the PUBLISH Packet.
func (cli *Client) handlePUBLISH(p packet.Packet) error {
	// Get the PUBLISH Packet.
	publish := p.(*packet.PUBLISH)

	switch publish.QoS {
	case mqtt.QoS0:
		// Lock for reading.
		cli.muConn.RLock()

		// Unlock.
		defer cli.muConn.RUnlock()

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

		return nil
	case mqtt.QoS1:
		// Lock for reading.
		cli.muConn.RLock()

		// Unlock.
		defer cli.muConn.RUnlock()

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

		// Create a PUBACK Packet.
		puback, err := packet.NewPUBACK(&packet.PUBACKOptions{
			PacketID: publish.PacketID,
		})
		if err != nil {
			return err
		}

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

		return nil
	default:
		// Lock for update.
		cli.muSess.Lock()

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

		// Validate the Packet Identifier.
		if _, exist := cli.sess.receivingPackets[publish.PacketID]; exist {
			return packet.ErrInvalidPacketID
		}

		// Set the Packet to the Session.
		cli.sess.receivingPackets[publish.PacketID] = p

		// Create a PUBREC Packet.
		pubrec, err := packet.NewPUBREC(&packet.PUBRECOptions{
			PacketID: publish.PacketID,
		})
		if err != nil {
			return err
		}

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

		return nil
	}
}