Example #1
0
ConnectionBackend *ConnectionBackend::nextPendingConnection()
{
    Q_ASSERT(state == Listening);
    Q_ASSERT(localServer || tcpServer);
    Q_ASSERT(!socket);

    //qCDebug(KIO_CORE) << "Got a new connection";

    QTcpSocket *newSocket;
    if (mode == LocalSocketMode) {
        newSocket = localServer->nextPendingConnection();
    } else {
        newSocket = tcpServer->nextPendingConnection();
    }

    if (!newSocket) {
        return 0;    // there was no connection...
    }

    ConnectionBackend *result = new ConnectionBackend(Mode(mode));
    result->state = Connected;
    result->socket = newSocket;
    newSocket->setParent(result);
    connect(newSocket, SIGNAL(readyRead()), result, SLOT(socketReadyRead()));
    connect(newSocket, SIGNAL(disconnected()), result, SLOT(socketDisconnected()));

    return result;
}
void NetworkManagerThread::run(){

    _server = new QTcpServer();

    int retryCount = 0;
    bool success = false;
    while(!success && retryCount < 40){
        success = this->_server->listen(QHostAddress::Any, Config::current()->serverPort()+1);
        retryCount++;

        if(!success)
            msleep(200);
    }

    if(!success){
        Log::add(tr("Could not listen on port %1").arg(QString::number(Config::current()->serverPort()+1)), Log::Error);
        return;
    }


    bool ok;
    while(true){
        /*
          Eingebauter Stopp 1
          */
        this->checkSuspended();
        if(!_server)
            return;
        _server->waitForNewConnection(1000,&ok);
        if(!ok){
            /*
              Eingebauter Stop 2
              */
            this->checkSuspended();

            QTcpSocket *connection = _server->nextPendingConnection();
            if(connection == 0)
                continue;

            Log::add(tr("New connection from %1").arg(connection->peerAddress().toString()), Log::Information);

            RootConsoleThread *thread = new RootConsoleThread(connection);
            connection->setParent(0);
            connection->moveToThread(thread);

            this->_runningConsoles.append(thread);
            this->_sockets.append(connection);

            thread->start();
        }
    }


}
Example #3
0
bool DesignerExternalEditor::startEditor(const QString &fileName, QString *errorMessage)
{
    EditorLaunchData data;
    // Find the editor binary
    if (!getEditorLaunchData(fileName, &QtSupport::BaseQtVersion::designerCommand,
                            QLatin1String(designerBinaryC),
                            QStringList(), false, &data, errorMessage)) {
        return false;
    }
    // Known one?
    const ProcessCache::iterator it = m_processCache.find(data.binary);
    if (it != m_processCache.end()) {
        // Process is known, write to its socket to cause it to open the file
        if (debug)
           qDebug() << Q_FUNC_INFO << "\nWriting to socket:" << data.binary << fileName;
        QTcpSocket *socket = it.value();
        if (!socket->write(fileName.toUtf8() + '\n')) {
            *errorMessage = tr("Qt Designer is not responding (%1).").arg(socket->errorString());
            return false;
        }
        return true;
    }
    // No process yet. Create socket & launch the process
    QTcpServer server;
    if (!server.listen(QHostAddress::LocalHost)) {
        *errorMessage = tr("Unable to create server socket: %1").arg(server.errorString());
        return false;
    }
    const quint16 port = server.serverPort();
    if (debug)
        qDebug() << Q_FUNC_INFO << "\nLaunching server:" << port << data.binary << fileName;
    // Start first one with file and socket as '-client port file'
    // Wait for the socket listening
    data.arguments.push_front(QString::number(port));
    data.arguments.push_front(QLatin1String("-client"));

    if (!startEditorProcess(data, errorMessage))
        return false;
    // Set up signal mapper to track process termination via socket
    if (!m_terminationMapper) {
        m_terminationMapper = new QSignalMapper(this);
        connect(m_terminationMapper, SIGNAL(mapped(QString)), this, SLOT(processTerminated(QString)));
    }
    // Insert into cache if socket is created, else try again next time
    if (server.waitForNewConnection(3000)) {
        QTcpSocket *socket = server.nextPendingConnection();
        socket->setParent(this);
        m_processCache.insert(data.binary, socket);
        m_terminationMapper->setMapping(socket, data.binary);
        connect(socket, SIGNAL(disconnected()), m_terminationMapper, SLOT(map()));
        connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), m_terminationMapper, SLOT(map()));
    }
    return true;
}
Example #4
0
void QLocalServerPrivate::_q_onNewConnection()
{
    Q_Q(QLocalServer);
    QTcpSocket* tcpSocket = tcpServer.nextPendingConnection();
    if (!tcpSocket) {
        qWarning("QLocalServer: no pending connection");
        return;
    }

    tcpSocket->setParent(q);
    const quintptr socketDescriptor = tcpSocket->socketDescriptor();
    q->incomingConnection(socketDescriptor);
}
Example #5
0
void TcpListener::newConnection() {
  QTcpSocket *socket = _server->nextPendingConnection();
  if (!socket) // should never happen
    return;
  Session session = SessionManager::createSession();
  QString clientaddr = "["+socket->peerAddress().toString()
      +"]:"+QString::number(socket->peerPort());
  session.setParam("clientaddr", clientaddr);
  if (_idleHandlers.size() == 0) {
    Log::warning(session.id()) << "cannot handle connection: no idle handler"
                               << clientaddr;
    // LATER leave a reply to client
    delete socket;
  } else {
    TcpConnectionHandler *handler = _idleHandlers.takeFirst();
    socket->setParent(0);
    socket->moveToThread(handler->thread());
    handler->processConnection(socket, session);
  }
}
void NetworkServiceProvider::run(){

    bool dbAvail = false;

    while(!dbAvail){
        msleep(100);
        QList<ProcessManager::Process*> processes = ProcessManager::instance()->processesFromClassName("SearchStorageThread");
        if(processes.isEmpty()){
            dbAvail = false;
        }else{
            if(processes.first()->description == "Saves search databases"){
                break;
            }
        }
    }

    this->_serverSocket = new QTcpServer(this);

    int retryCount = 0;
    bool success = false;
    while(!success && retryCount < 40){
        success = this->_serverSocket->listen(QHostAddress::Any, Config::current()->serverPort());
        retryCount++;

        if(!success)
            msleep(200);
    }

    if(!success){
        Log::add(tr("Could not listen on port %1").arg(QString::number(Config::current()->serverPort())), Log::Error);
        return;
    }


    Log::add(tr("Bringing search interface up"),Log::Information);

    forever{
        if(_stop){
            break;
        }
        bool ok;
        this->_serverSocket->waitForNewConnection(500,&ok);

        this->checkForFinishedThreads();

        //Kein Timeout sondern verbindung
        if(!ok){

            QTcpSocket *socket = this->_serverSocket->nextPendingConnection();
            if(!socket)
                continue;
            socket->setParent(0);

            SearchRequestHandler *handler = new SearchRequestHandler(socket);
            socket->moveToThread(handler);

            connect(handler,SIGNAL(finished()),handler,SLOT(cleanUp()));
            this->_requests.append(handler);

            handler->start();


        }
    }
    _stopped = true;
}