Example #1
1
void ApplicationCore::slotHandleUniqueApplicationConnection()
{
    QLocalSocket *socket = m_uniqueApplicationServer.nextPendingConnection();
    connect(socket, &QLocalSocket::readyRead, socket, [this, socket](){
        if (!socket->canReadLine())
            return;
        while (socket->canReadLine()) {
            const QByteArray data = socket->readLine().trimmed();
            if (data.startsWith(StartTaskCommand)) {
                bool ok = true;
                const TaskId id = data.mid(StartTaskCommand.length()).toInt(&ok);
                if (ok) {
                    m_timeTracker.slotStartEvent(id);
                } else {
                    qWarning() << "Received invalid argument:" << data;
                }
            } else if (data.startsWith(RaiseWindowCommand)) {
                // nothing to do, see below
            }
        }
        socket->deleteLater();
        showMainWindow(ApplicationCore::ShowMode::ShowAndRaise);
    });
}
void MobilityConnection::connectToSimulator()
{
    // 1. check availability
    QLocalSocket *socket = new QLocalSocket;
    socket->connectToServer(QLatin1String(SIMULATOR_MOBILITY_SERVERNAME), QLocalSocket::ReadWrite);
    if (!socket->waitForConnected(1000)) {
        qFatal("Could not connect to mobility server");
        socket->deleteLater();
        return;
    }
    mSendSocket = socket;

    // 2. Create the local server used for initiating the backchannel.
    const qint64 pid = QCoreApplication::applicationPid();
    QLocalServer *server = new QLocalServer(this);
    if (!server->listen(qt_mobilityServerName(pid)))
        qFatal("Can't create local mobility server for this application.");

    // 3. Send initial application connect command.
    ApplicationConnectCommand command;
    ApplicationConnectCommand::Request &request = command.request;
    request.applicationPid = pid;
    request.applicationName[0] = 0;
    request.version = mobilityVersion;
    qt_sendAndReceiveApplicationConnect(socket, command);

    mSimulatorVersion = command.reply.version;

    // We usually want to get the initial state from the simulator directly, probably
    // before the event loop is started up. Hence we block until the simulator has established
    // the back channel.
    if (!server->waitForNewConnection(1000))
        qFatal("Simulator didn't establish mobility-backchannel on time");
    mReceiveSocket = server->nextPendingConnection();
    server->close();

    connect(mReceiveSocket, SIGNAL(readyRead()), SLOT(onReadyRead()));
}
Example #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() );
        }
    }
}
void CLocalSvrCommunication::HandleServerDisconnect( )
{
    QLocalSocket* pSocket = qobject_cast< QLocalSocket* >( sender( ) );
    pSocket->close( );
    pSocket->deleteLater( );
}
Example #5
0
void MainWindow::clientLeave()
{
    if(connectedMode=="Tcp socket (QTcpServer)")
    {
        // Wich client leave
        QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
        if (socket == 0) // If not found
            return;
        int index=-1;
        for (int i = 0; i < tcpSocket.size(); ++i) {
            if(tcpSocket.at(i)->tcpSocket==socket)
                index=i;
        }
        if(index==-1)
            ui->statusBarApp->showMessage("Unable to locate client, internal error",10000);
        else
        {
            if(TcpServer.isListening())
                ui->statusBarApp->showMessage("The client: "+socket->peerAddress().toString()+" as leave",10000);
            tcpSocket[index]->ItemInList->setIcon(QIcon(":/images/list-disconnected.png"));
            tcpSocket[index]->isConnected=false;
            tcpSocket[index]->tcpSocket=NULL;
            socket->deleteLater();

            //compression
            if(tcpSocket[index]->compression!=NULL)
            {
                delete tcpSocket[index]->compression;
                tcpSocket[index]->compression=NULL;
            }
            if(tcpSocket[index]->compression!=NULL)
            {
                delete tcpSocket[index]->compression;
                tcpSocket[index]->compression=NULL;
            }
        }
    }
    if(connectedMode==COMBOBOXTEXTLOCALSOCK)
    {
        // Wich client leave
        QLocalSocket *socket = qobject_cast<QLocalSocket *>(sender());
        if (socket == 0) // If not found
            return;
        int index=-1;
        for (int i = 0; i < localSocket.size(); ++i) {
            if(localSocket.at(i)->localSocket==socket)
                index=i;
        }
        if(index==-1)
            ui->statusBarApp->showMessage("Unable to locate client, internal error",10000);
        else
        {
            if(LocalServer.isListening())
                ui->statusBarApp->showMessage("Client as leave",10000);
            localSocket[index]->ItemInList->setIcon(QIcon(":/images/list-disconnected.png"));
            localSocket[index]->isConnected=false;
            localSocket[index]->localSocket=NULL;
            socket->deleteLater();
        }

        //compression
        if(localSocket[index]->compression!=NULL)
        {
            delete localSocket[index]->compression;
            localSocket[index]->compression=NULL;
        }
        if(localSocket[index]->compression!=NULL)
        {
            delete localSocket[index]->compression;
            localSocket[index]->compression=NULL;
        }
    }
}