Exemplo n.º 1
0
void NetworkServer::slotNewConnection()
{
    QTcpSocket* socket = mTcpServer->nextPendingConnection();
    qDebug() << "NetworkServer::slotNewConnection(): accepting connection from" << socket->peerName() << "and port" << socket->peerPort();
    connect(socket, SIGNAL(disconnected()), SLOT(slotConnectionEnded()));
    connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(slotConnectionEnded()));
    connect(socket, SIGNAL(readyRead()), SLOT(slotReadSocket()));

    mTcpSockets.append(socket);
}
Exemplo n.º 2
0
void TvEventHandler::incomingConnection(qintptr socket)
{
    QTcpSocket* tcpSocket = new QTcpSocket(this);
    tcpSocket->setSocketDescriptor(socket);
    qCDebug(dcLgSmartTv) << "Event handler -> incoming connection" << tcpSocket->peerAddress().toString() << tcpSocket->peerName();

    connect(tcpSocket, &QTcpSocket::readyRead, this, &TvEventHandler::readClient);
    connect(tcpSocket, &QTcpSocket::disconnected, this, &TvEventHandler::onDisconnected);
}
Exemplo n.º 3
0
bool ZHttpServer::startServer(quint16 port)
{
    if(m_tcpServer->isListening())
        return true;

    if(!m_tcpServer->listen(QHostAddress::Any, port)){
        qWarning() << "HttpServer: error: " << m_tcpServer->errorString();
        return false;
    }else{
        qWarning() << "HttpServer: OK";
    }
    connect(m_tcpServer, &QTcpServer::newConnection, [this]{
        QTcpSocket *socket = m_tcpServer->nextPendingConnection();
        qWarning() << "HttpServer: new connect:" << socket->peerAddress().toString() << socket->peerName() << socket->peerPort();

        connect(socket, &QTcpSocket::readyRead, [socket, this]{
            HttpInfo info(socket->readAll());

            qWarning() << info.url();

            const QByteArray &query = info.url().query().toUtf8();
            QMap<QByteArray, QByteArray> command_map;

            QFileInfo fileInfo(sysroot + info.url().path());

            if (fileInfo.isFile() && fileInfo.isExecutable()) {
                execProcess((fileInfo.fileName() + " " + info.url().query(QUrl::FullyDecoded)).toLatin1(), socket);

                return;
            }

            if(!query.isEmpty()) {
                QByteArrayList commands = query.split('&');

                qWarning() << "HttpServer: command:" << commands;

                for(const QByteArray &comm : commands) {
                    if(comm.isEmpty())
                        continue;

                    const QByteArrayList &tmp_list = comm.split('=');

                    if(tmp_list.count() != 2 || tmp_list.first().isEmpty()) {
                        socket->write(messagePackage("", "text/Html", HttpInfo::BadRequest, QString("Grammatical errors: \"%1\"").arg(QString(comm))));
                        socket->close();
                        return;
                    }

                    command_map[tmp_list.first()] = tmp_list.last();
                }
            }

            if(command_map.value(ACTION) == ACTION_EXEC) {
                execProcess(QUrl::fromPercentEncoding(command_map.value(COMMAND)), socket);
            } else {
                QPointer<QTcpSocket> socket_pointer = socket;

                readFile(info.url(), socket);

                if (socket_pointer)
                    socket->close();
            }
        });
        connect(socket, &QTcpSocket::disconnected, [socket]{
            qWarning() << "HttpServer: disconnected: " << socket->peerAddress().toString() << socket->peerName() << socket->peerPort();
            socket->deleteLater();
        });
    });

    return true;
}