Optimize and test fiber for QT UI.

This commit is contained in:
郑树新 2024-09-09 09:45:38 +08:00
parent 1247f1d8dd
commit 80602947cf
7 changed files with 150 additions and 13 deletions

View File

@ -12,7 +12,7 @@
// 201703L (C++17)
// 202002L (C++20)
#if __cplusplus >= 201103L // Support c++11 ?
#if defined(USE_CPP11) || __cplusplus >= 201103L // Support c++11 ?
struct ACL_FIBER;

View File

@ -0,0 +1,24 @@
#include "inputdialog.h"
InputDialog::InputDialog(QWidget *parent) : QDialog(parent)
{
setWindowTitle("input: ");
setGeometry(200, 200, 300, 200);
QVBoxLayout *layout = new QVBoxLayout(this);
lineEdit = new QLineEdit(this);
lineEdit->setPlaceholderText("输入内容...");
layout->addWidget(lineEdit);
button = new QPushButton("确定", this);
layout->addWidget(button);
connect(button, &QPushButton::clicked, this, &InputDialog::onAccept);
}
void InputDialog::onAccept()
{
emit dialogAccepted(lineEdit->text());
accept();
}

View File

@ -0,0 +1,27 @@
#ifndef INPUTDIALOG_H
#define INPUTDIALOG_H
#include <QDialog>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
class InputDialog : public QDialog
{
Q_OBJECT
public:
explicit InputDialog(QWidget *parent = nullptr);
signals:
void dialogAccepted(const QString &text);
private slots:
void onAccept();
private:
QLineEdit *lineEdit;
QPushButton *button;
};
#endif // INPUTDIALOG_H

View File

@ -4,23 +4,26 @@
#include <QApplication>
#include <QTimer>
class fiber_dummy : public acl::fiber {
class fiber_backend : public acl::fiber {
public:
fiber_dummy() {}
fiber_backend() {}
protected:
void run() override {
qDebug() << "fiber_dummy started!";
qDebug() << "fiber_backend started!";
while (true) {
acl::fiber::delay(1000);
}
delete this;
}
~fiber_dummy() = default;
~fiber_backend() = default;
};
static void startupCallback()
{
acl::fiber::init(acl::FIBER_EVENT_T_WMSG, true);
acl::fiber* fb = new fiber_dummy;
acl::fiber* fb = new fiber_backend;
fb->start();
}

View File

@ -6,6 +6,7 @@
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "childwindows.h"
#include "inputdialog.h"
#include "fiber_server.h"
#include "fiber_client.h"
@ -21,20 +22,31 @@ MainWindow::MainWindow(QWidget *parent)
connect(button_, &QPushButton::clicked, this, &MainWindow::onButtonClicked);
start_server_ = new QPushButton("Start fiber server", this);
start_server_->setGeometry(QRect(QPoint(100, 200), QSize(300, 50)));
start_server_->setGeometry(QRect(QPoint(100, 150), QSize(300, 50)));
connect(start_server_, &QPushButton::clicked, this, &MainWindow::onStartServer);
start_client_ = new QPushButton("Start fiber client", this);
start_client_->setGeometry(QRect(QPoint(100, 300), QSize(300, 50)));
start_client_->setGeometry(QRect(QPoint(100, 200), QSize(300, 50)));
connect(start_client_, &QPushButton::clicked, this, &MainWindow::onStartClient);
url_get_ = new QPushButton("Http download", this);
url_get_->setGeometry(QRect(QPoint(100, 250), QSize(300, 50)));
connect(url_get_, &QPushButton::clicked, this, &MainWindow::onUrlGet);
stop_fiber_ = new QPushButton("Stop fiber schedule", this);
stop_fiber_->setGeometry(QRect(QPoint(100, 400), QSize(300, 50)));
stop_fiber_->setGeometry(QRect(QPoint(100, 300), QSize(300, 50)));
connect(stop_fiber_, &QPushButton::clicked, this, &MainWindow::onStopSchedule);
open_child_ = new QPushButton("Open Child Window", this);
open_child_->setGeometry(QRect(QPoint(100, 500), QSize(300, 50)));
open_child_->setGeometry(QRect(QPoint(100, 350), QSize(300, 50)));
connect(open_child_, &QPushButton::clicked, this, &MainWindow::onOpenChildWindow);
input_button_= new QPushButton("Open dialog", this);
input_button_->setGeometry(100, 400, 300,50);
connect(input_button_, &QPushButton::clicked, this, &MainWindow::onInputClicked);
input_display_ = new QLabel("输入内容: ", this);
input_display_->setGeometry(100, 450, 200, 50);
}
MainWindow::~MainWindow()
@ -60,9 +72,9 @@ void MainWindow::onStartServer()
}
server_ = new fiber_server("127.0.0.1", 9001, this);
qDebug() << "Start one fiber";
qDebug() << "Start fiber server";
server_->start();
qDebug() << "Fiber started\r\n";
qDebug() << "Fiber server started";
}
void MainWindow::onStartClient()
@ -73,6 +85,42 @@ void MainWindow::onStartClient()
qDebug() << "Fiber client started!";
}
void MainWindow::onUrlGet()
{
go[this] {
const char *addr = "www.baidu.com:80";
const char *host = "www.baidu.com";
acl::http_request req(addr);
req.request_header()
.set_url("/")
.set_host(host);
if (!req.request(nullptr, 0)) {
qDebug() << "Send http request to " << addr << " error: " << acl::last_serror();
return;
}
acl::string buf;
if (req.get_body(buf)) {
this->onDownloadFinish(true, req);
} else {
this->onDownloadFinish(false, req);
}
};
}
void MainWindow::onDownloadFinish(bool ok, const acl::http_request& req)
{
if (ok) {
acl::http_client *client = req.get_client();
acl::string buf;
client->sprint_header(buf);
qDebug() << "Got response body ok!";
qDebug() << buf.c_str();
} else {
qDebug() << "Got response body error!";
}
}
void MainWindow::onStopSchedule()
{
if (server_) {
@ -109,6 +157,23 @@ void MainWindow::onOpenChildWindow()
qDebug() << "Second window isActiveWindow:" << child_window_->isActiveWindow();
}
void MainWindow::onInputClicked()
{
InputDialog dialog(this);
QRect mainWindowGeometry = this->frameGeometry();
QPoint mainWindowPos = this->pos();
int x = mainWindowPos.x() + (mainWindowGeometry.width() - dialog.width()) / 2;
int y = mainWindowPos.y() + (mainWindowGeometry.height() - dialog.height()) / 2;
dialog.move(x, y);
connect(&dialog, &InputDialog::dialogAccepted, this, &MainWindow::onDialogAccepted);
dialog.exec();
}
void MainWindow::onDialogAccepted(const QString &text)
{
input_display_->setText("输入内容: " + text);
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
if (event->modifiers() == Qt::ControlModifier) {

View File

@ -5,6 +5,11 @@
#include <QKeyEvent>
#include <QPushButton>
#include <QProcess.h>
#include <QLabel>
namespace acl {
class http_request;
}
class fiber_server;
@ -30,10 +35,17 @@ protected:
void onStartClient();
void onStopSchedule();
void onOpenChildWindow();
void onInputClicked();
void onUrlGet();
void onDialogAccepted(const QString &text);
public:
void onAboutToQuit();
private:
void onDownloadFinish(bool ok, const acl::http_request& req);
private:
Ui::MainWindow *ui_;
QPushButton *button_;
@ -41,6 +53,10 @@ private:
QPushButton *start_client_;
QPushButton *stop_fiber_;
QPushButton *open_child_;
QPushButton *url_get_;
std::string url_;
QPushButton *input_button_;
QLabel *input_display_;
fiber_server *server_ = nullptr;
QProcess *process_;

View File

@ -5,7 +5,9 @@
#include "acl_cpp/lib_acl.hpp"
#include "fiber/libfiber.h"
#include "fiber/libfiber.hpp"
#include "fiber/fiber_tbox.hpp"
#define USE_CPP11
#include "fiber/go_fiber.hpp"
#include "patch.h"
#include <string>