Qt5 slot being called multiple times from a single emit statement

632 views Asked by At

I'm relatively new to Qt, but I have done a little searching around. I have a base class that handles UDP broadcasting, and does the connect statements in the constructor of the class like this:

NetworkConnection::NetworkConnection(QObject *parent)
    : QObject(parent) // Based on QObject
    , m_server_search( new QUdpSocket ) // Our UDP Broadcast socket
    , m_waiting_for_server( false )
    , m_found_server( false )
{
    qDebug() << "NetworkConnection::constructor";
    connect( m_server_search, SIGNAL(readyRead()), this, SLOT(serverResponse()), Qt::UniqueConnection );
    if ( m_server_search->bind( QHostAddress::AnyIPv4, (quint16)PORT_MULTICAST, QUdpSocket::ShareAddress ) )
    {
        if ( m_server_search->joinMulticastGroup( QHostAddress( MULTICAST_GROUP ) ) )
        {
            connect( this, SIGNAL(broadcast(NetworkMessage)), this, SLOT(broadcast_message(NetworkMessage)), Qt::UniqueConnection );
            this->m_ping_timer = this->startTimer(2000);
            qDebug() << "Ping timer id=" << this->m_ping_timer;
        } else qDebug() << "Couldn't start multicast listener";
    } else qDebug() << "Couldn't bind multicast to port" << PORT_MULTICAST;
}

I set up a signal/slot interface for broadcasting:

signals:
    void serverFound();
    void serverNotFound();
    void broadcast(NetworkMessage);

private slots:
    void serverResponse();
    void broadcast_message( NetworkMessage msg );

And broadcast_message looks like this:

void NetworkConnection::broadcast_message( NetworkMessage msg )
{
    QByteArray raw = msg.toString();
    qDebug() << "NetworkConnection::broadcast_message>" << raw;
    if ( m_server_search->writeDatagram( raw.data(), raw.size(), QHostAddress(MULTICAST_GROUP), (quint16)PORT_MULTICAST ) < 1 ) qDebug() << "Failed broadcast last message";
}

My timer works well, and here is the code:

void NetworkConnection::timerEvent(QTimerEvent *event)
{
    qDebug() << "NetworkConnection::timerEvent with id" << event->timerId() << "(ping timer=" << this->m_ping_timer << ")";
    if ( event->timerId() == this->m_ping_timer )
    {
        qDebug() << "NetworkConnection::pingForServer";
        if ( m_waiting_for_server && !m_found_server )
        {
            qDebug() << "Server not found!";
            emit this->serverNotFound();
            return;
        }
        if ( !m_found_server )
        {
            qDebug() << "Sending a ping to the server";
            NetworkMessage msg( m_software_guid, get_microseconds(), QString("whoisaserver") );
            emit this->broadcast( msg );
            m_waiting_for_server = true;
            m_found_server = false;
        }
    }
}

I only get the text "Sending a pint to the server" once, but my broadcast_message outputs it's qDebug() multiple times.

I'm not explicitly using multiple threads, and as you can see I'm using Qt::UniqueConnection, which is apparently having no affect?

SO why would the slot be called multiple times? I've even tried debugging it a little and just calling this->broadcast( ... ) without using emit, and it still gets called multiple times.

Edit: I just added a counter to the broadcast_message slot, and it gets called 340 times. Is there any significance to that?

0

There are 0 answers