Пример #1
0
QTcpSocket* RyProxyServer::getSocket(QString address,quint16 port,bool* isFromCache,QThread* _desThread){

    //QDateTime time = QDateTime::currentDateTime();
    //qDebug()<<"getSocket "<<time.toMSecsSinceEpoch();
    if(isFromCache) *isFromCache = false;
    QTcpSocket *theSocket = NULL;
    QString theKey = QString("%1:%2").arg(address.toLower()).arg(port);


    QMutexLocker locker(&_socketsOpMutex);
    if(/*_cachedSockets.size() >= _maxOfSocket
            || */_cachedSockets.contains( theKey )){
        //QList<QTcpSocket*> sockets = _cachedSockets.values(theKey);
        theSocket = _cachedSockets.take(theKey);
        locker.unlock();
        if(isFromCache) *isFromCache = true;
        //qDebug()<<"return cached socket";
        //int n = _cachedSockets.remove(theKey,theSocket);

    }else{
        locker.unlock();
        //qDebug()<<"new response socket";
        theSocket = new QTcpSocket();
    }
    theSocket->moveToThread(_desThread);
    //qint64 getSocketDatTime = time.msecsTo(QDateTime::currentDateTime());
    //qDebug()<<"== get socket time(ms)"<<getSocketDatTime<<" "<<time.toMSecsSinceEpoch();
    return theSocket;
}
Пример #2
0
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();
        }
    }


}
Пример #3
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);
  }
}
Пример #4
0
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;
}