QstProtocol::QstProtocol(QObject *parent)
    : QTcpSocket(parent)
    , tx_msg_id(1)
    , port(0)
    , onData_busy(false)
    , enable_reconnect(false)
    , reconnect_interval(10000)
    , last_data_received(false)
    , connection_valid(false)
    , debug_on(false)
    , m_auto_unpack(true)
{
    static int id1 = qRegisterMetaType<QstMessage>(); Q_UNUSED(id1);
    static int id2 = qRegisterMetaType<QstMessage*>(); Q_UNUSED(id2);
    static int id3 = qRegisterMetaType<const QstMessage*>(); Q_UNUSED(id3);

    unique_id = QString::number(++g_unique_id);
    if (debug_on) {
        qDebug() << QString("%1 QstProtocol::QstProtocol()").arg(uniqueId()).toLatin1();
    }
    cur_message = 0;
    rx_busy = false;

    QObject::connect( &connect_timer, SIGNAL(timeout()), this, SLOT(connectTimeout()), Qt::DirectConnection );

    QObject::connect( this,SIGNAL(connected()),this,SLOT(onSocketConnected()), Qt::DirectConnection );
    QObject::connect( this,SIGNAL(disconnected()),this,SLOT(onSocketClosed()), Qt::DirectConnection );
    QObject::connect( this,SIGNAL(readyRead()),this,SLOT(onData()), Qt::DirectConnection );

    // initialize. Any time is better than no time.
    rx_timer.start();
}
void QstProtocol::disconnect( bool disableReconnect )
{
    if (state() == ConnectedState) {
        if (debug_on) {
            qDebug() << ( QString("%1 QstProtocol::disconnect(disableReconnect=%2)").
                                arg(uniqueId()).
                                arg(disableReconnect).toLatin1());
        }
        // be polite and tell the other side we are closing
        postMessage( QstMessage("QTEST_CLOSING_CONNECTION") );

        // we are closing ourselves, so we don't want to reconnect
        if (disableReconnect) enable_reconnect = false;

        onSocketClosed();
    }
}
Ejemplo n.º 3
0
void Server::onNewConnection()
{
    QLocalSocket* socket = m_server->nextPendingConnection();
    if (!socket) {
        log("No pending client connections!", LogError);
    } else if ( socket->state() != QLocalSocket::ConnectedState ) {
        log("Client is not connected!", LogError);
        socket->deleteLater();
    } else {
        QScopedPointer<ClientSocket> clientSocket( new ClientSocket(socket) );

        const Arguments args = clientSocket->readArguments();
        if ( !args.isEmpty() ) {
            ++m_socketCount;
            connect( clientSocket.data(), SIGNAL(destroyed()),
                     this, SLOT(onSocketClosed()) );
            connect( this, SIGNAL(destroyed()),
                     clientSocket.data(), SLOT(close()) );
            connect( this, SIGNAL(destroyed()),
                     clientSocket.data(), SLOT(deleteAfterDisconnected()) );
            emit newConnection( args, clientSocket.take() );
        }
    }
}