From d7b569c102320552301c1778c8c118be67d8b898 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E6=A0=91=E6=96=B0?= Date: Sun, 22 Sep 2024 15:17:25 +0800 Subject: [PATCH] Optimize and test QtFiber demo. --- .../samples-gui/QtFiber/fiber_client.cpp | 2 +- lib_fiber/samples-gui/QtFiber/inputdialog.cpp | 18 ++--- lib_fiber/samples-gui/QtFiber/inputdialog.h | 6 +- lib_fiber/samples-gui/QtFiber/mainwindow.cpp | 57 +++++++++++--- lib_fiber/samples-gui/QtFiber/mainwindow.h | 6 +- lib_fiber/samples-gui/QtFiber/mainwindow.ui | 76 +++++++++++++++---- 6 files changed, 126 insertions(+), 39 deletions(-) diff --git a/lib_fiber/samples-gui/QtFiber/fiber_client.cpp b/lib_fiber/samples-gui/QtFiber/fiber_client.cpp index 3a78ecca7..281d1ea53 100644 --- a/lib_fiber/samples-gui/QtFiber/fiber_client.cpp +++ b/lib_fiber/samples-gui/QtFiber/fiber_client.cpp @@ -43,7 +43,7 @@ void fiber_client::run() { qDebug() << "Fiber-" << acl::fiber::self() << " recv: " << buf; } - parent_->setProgress((100 * i) / max_); + parent_->setProgress((100 * (int) i) / (int) max_); if (i % 10 == 0 && delay_ > 0) { acl::fiber::delay(delay_); } diff --git a/lib_fiber/samples-gui/QtFiber/inputdialog.cpp b/lib_fiber/samples-gui/QtFiber/inputdialog.cpp index 5dc057129..cf50fb499 100644 --- a/lib_fiber/samples-gui/QtFiber/inputdialog.cpp +++ b/lib_fiber/samples-gui/QtFiber/inputdialog.cpp @@ -2,23 +2,23 @@ InputDialog::InputDialog(QWidget *parent) : QDialog(parent) { - setWindowTitle("input: "); - setGeometry(200, 200, 300, 200); + setWindowTitle("Http Options: "); + setGeometry(200, 200, 600, 400); QVBoxLayout *layout = new QVBoxLayout(this); - lineEdit = new QLineEdit(this); - lineEdit->setPlaceholderText("输入内容..."); - layout->addWidget(lineEdit); + options = new QTextEdit(this); + options->setPlaceholderText("输入内容..."); + layout->addWidget(options); - button = new QPushButton("确定", this); - layout->addWidget(button); + confirm = new QPushButton("确定", this); + layout->addWidget(confirm); - connect(button, &QPushButton::clicked, this, &InputDialog::onAccept); + connect(confirm, &QPushButton::clicked, this, &InputDialog::onAccept); } void InputDialog::onAccept() { - emit dialogAccepted(lineEdit->text()); + emit dialogAccepted(options->toPlainText()); accept(); } diff --git a/lib_fiber/samples-gui/QtFiber/inputdialog.h b/lib_fiber/samples-gui/QtFiber/inputdialog.h index 37582911c..f912342de 100644 --- a/lib_fiber/samples-gui/QtFiber/inputdialog.h +++ b/lib_fiber/samples-gui/QtFiber/inputdialog.h @@ -2,7 +2,7 @@ #define INPUTDIALOG_H #include -#include +#include #include #include @@ -20,8 +20,8 @@ private slots: void onAccept(); private: - QLineEdit *lineEdit; - QPushButton *button; + QTextEdit *options; + QPushButton *confirm; }; #endif // INPUTDIALOG_H diff --git a/lib_fiber/samples-gui/QtFiber/mainwindow.cpp b/lib_fiber/samples-gui/QtFiber/mainwindow.cpp index d3ce040dc..5c42467da 100644 --- a/lib_fiber/samples-gui/QtFiber/mainwindow.cpp +++ b/lib_fiber/samples-gui/QtFiber/mainwindow.cpp @@ -25,10 +25,12 @@ MainWindow::MainWindow(QWidget *parent) connect(ui_->stopServer, &QPushButton::clicked, this, &MainWindow::onStopServer); connect(ui_->startClient, &QPushButton::clicked, this, &MainWindow::onStartClient); connect(ui_->urlGet, &QPushButton::clicked, this, &MainWindow::onUrlGet); + connect(ui_->httpOptions, &QPushButton::clicked, this, &MainWindow::onHttpOptions); ui_->startSchedule->setEnabled(false); ui_->stopServer->setEnabled(false); ui_->startClient->setEnabled(false); + ui_->url->setText(url_.c_str()); } MainWindow::~MainWindow() @@ -63,6 +65,7 @@ void MainWindow::closeEvent(QCloseEvent *event) void MainWindow::onClear() { ui_->display->clear(); + ui_->reqHdr->clear(); } void MainWindow::setProgress(int n) @@ -138,24 +141,40 @@ void MainWindow::onStartClient() void MainWindow::onUrlGet() { + const auto url = ui_->url->text(); + if (url.isEmpty()) { + QMessageBox::information(this, "UrlGet", "url is empty!"); + return; + } + if (!acl::fiber::scheduled()) { qDebug() << "gui fiber not scheudled yet!"; return; } + url_ = url.toStdString(); + 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); + acl::http_request req(url_.c_str()); + acl::http_header& reqHdr = req.request_header(); + if (!host_.empty()) { + reqHdr.set_host(host_.c_str()); + } + + for (const auto& it : headers_) { + reqHdr.add_entry(it.first.c_str(), it.second.c_str()); + } + + acl::string buf; + reqHdr.build_request(buf); + ui_->reqHdr->setText(buf.c_str()); + if (!req.request(nullptr, 0)) { - qDebug() << "Send http request to " << addr << " error: " << acl::last_serror(); + qDebug() << "Send http request to " << url_.c_str() << " error: " << acl::last_serror(); return; } - acl::string buf; + buf.clear(); if (req.get_body(buf)) { this->onDownloadFinish(true, req); } else { @@ -187,6 +206,7 @@ void MainWindow::onStartSchedule() ui_->stopSchedule->setEnabled(true); ui_->urlGet->setEnabled(true); + ui_->startServer->setEnabled(true); qDebug() << "Begin schedule_gui!"; acl::fiber::schedule_gui(); @@ -205,11 +225,15 @@ void MainWindow::onStopSchedule() ui_->stopSchedule->setEnabled(false); ui_->startSchedule->setEnabled(true); + ui_->startClient->setEnabled(false); + ui_->startServer->setEnabled(false); + ui_->stopServer->setEnabled(false); + acl::fiber::schedule_stop(); qDebug() << "Fiber schedule stopped!"; } -void MainWindow::onInputClicked() +void MainWindow::onHttpOptions() { InputDialog dialog(this); QRect mainWindowGeometry = this->frameGeometry(); @@ -223,7 +247,20 @@ void MainWindow::onInputClicked() void MainWindow::onDialogAccepted(const QString &text) { - //input_display_->setText("输入内容: " + text); + ui_->reqHdr->setText("输入内容:\r\n" + text); + headers_.clear(); + acl::string buf(text.toStdString().c_str()); + std::vector& lines = buf.split2("\r\n"); + for (const auto it : lines) { + acl::string line = it.c_str(); + auto& kv = line.split_nameval(':'); + if (kv.first.empty() || kv.second.empty()) { + continue; + } + kv.first.trim_space(); + kv.second.trim_left_space().trim_right_space(); + headers_[kv.first.c_str()] = kv.second.c_str(); + } } void MainWindow::keyPressEvent(QKeyEvent *event) diff --git a/lib_fiber/samples-gui/QtFiber/mainwindow.h b/lib_fiber/samples-gui/QtFiber/mainwindow.h index 718df57f7..61880ffd8 100644 --- a/lib_fiber/samples-gui/QtFiber/mainwindow.h +++ b/lib_fiber/samples-gui/QtFiber/mainwindow.h @@ -39,7 +39,7 @@ class MainWindow : public QMainWindow void onStartClient(); void onStartSchedule(); void onStopSchedule(); - void onInputClicked(); + void onHttpOptions(); void onUrlGet(); void onClear(); @@ -59,5 +59,9 @@ class MainWindow : public QMainWindow fiber_server *server_ = nullptr; QProcess *process_; struct timeval *stamp_; + + std::string url_ = "http://www.baidu.com/"; + std::string host_; + std::map headers_; }; #endif // MAINWINDOW_H diff --git a/lib_fiber/samples-gui/QtFiber/mainwindow.ui b/lib_fiber/samples-gui/QtFiber/mainwindow.ui index ace77907f..74321e3c1 100644 --- a/lib_fiber/samples-gui/QtFiber/mainwindow.ui +++ b/lib_fiber/samples-gui/QtFiber/mainwindow.ui @@ -6,7 +6,7 @@ 0 0 - 1600 + 2000 800 @@ -18,9 +18,9 @@ 390 - 20 + 79 800 - 600 + 541 @@ -28,7 +28,7 @@ 70 - 20 + 80 291 51 @@ -41,7 +41,7 @@ 70 - 80 + 130 291 51 @@ -54,7 +54,7 @@ 70 - 140 + 200 291 51 @@ -67,7 +67,7 @@ 70 - 200 + 250 291 51 @@ -80,7 +80,7 @@ 70 - 260 + 300 291 51 @@ -92,9 +92,9 @@ - 70 - 320 - 291 + 1270 + 130 + 301 51 @@ -105,9 +105,9 @@ - 1210 - 20 - 241 + 70 + 380 + 291 51 @@ -128,13 +128,59 @@ 0 + + + + 1570 + 130 + 291 + 51 + + + + Http options + + + + + + 1270 + 80 + 591 + 51 + + + + + + + 1220 + 90 + 41 + 41 + + + + URL: + + + + + + 1270 + 180 + 591 + 441 + + + 0 0 - 1600 + 2000 21