Merge branch 'master' into gitlab-upstream-master

This commit is contained in:
zhengshuxin 2024-09-09 09:49:53 +08:00
commit 59efcee7d3
17 changed files with 327 additions and 54 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,137 @@
CC = $(ENV_CC)
CFLAGS = -c -g -W \
-Wall \
-Wcast-qual \
-Wcast-align \
-Wno-long-long \
-Wpointer-arith \
-Werror \
-Wshadow \
-D_REENTRANT \
-D_POSIX_PTHREAD_SEMANTICS
###########################################################
#Check system:
# Linux, SunOS, Solaris, BSD variants, AIX, HP-UX
SYSLIB = -lpthread -ldl
CHECKSYSRES = @echo "Unknow system type!";exit 1
UNIXNAME = $(shell uname -sm)
ifeq ($(CC),)
CC = g++
endif
ifeq ($(findstring clang++, $(CC)), clang++)
CFLAGS += -Wno-invalid-source-encoding \
-Wno-invalid-offsetof
endif
# For FreeBSD
ifeq ($(findstring FreeBSD, $(UNIXNAME)), FreeBSD)
CFLAGS += -DFREEBSD
SYSLIB += -lcrypt -rdynamic
endif
# For Darwin
ifeq ($(findstring Darwin, $(UNIXNAME)), Darwin)
# CC += -arch x86_64 -arch arm64
CFLAGS += -DMACOSX -Wno-invalid-source-encoding \
-Wno-invalid-offsetof
SYSLIB += -rdynamic
UNIXTYPE = MACOSX
endif
#Path for Linux
ifeq ($(findstring Linux, $(UNIXNAME)), Linux)
CFLAGS += -DLINUX2
SYSLIB += -lcrypt -rdynamic
endif
# For MINGW
ifeq ($(findstring MINGW, $(UNIXNAME)), MINGW)
SYSLIB = -lpthread-2 -rdynamic
CFLAGS += -DLINUX2 -DMINGW
UNIXTYPE = LINUX
endif
#Path for SunOS
ifeq ($(findstring SunOS, $(UNIXNAME)), SunOS)
ifeq ($(findstring 86, $(UNIXNAME)), 86)
SYSLIB += -lsocket -lnsl -lrt
endif
ifeq ($(findstring sun4u, $(UNIXNAME)), sun4u)
SYSLIB += -lsocket -lnsl -lrt
endif
CFLAGS += -DSUNOS5
SYSLIB += -lcrypt -rdynamic
endif
#Path for HP-UX
ifeq ($(findstring HP-UX, $(UNIXNAME)), HP-UX)
PLAT_NAME=hp-ux
CFLAGS += -DHP_UX -DHPUX11
SYSLIB += -lcrypt -rdynamic
endif
#Find system type.
ifneq ($(SYSPATH),)
CHECKSYSRES = @echo "System is $(shell uname -sm)"
endif
###########################################################
ACL_PATH = ../../../../lib_acl
ACL_INC = $(ACL_PATH)/include
ACL_LIB = $(ACL_PATH)/lib
ACL_CPP_PATH = ../../../../lib_acl_cpp
ACL_CPP_INC = $(ACL_CPP_PATH)/include
ACL_CPP_LIB = $(ACL_CPP_PATH)/lib
PRO_PATH = ../../../../lib_protocol
PRO_INC = $(PRO_PATH)/include
PRO_LIB = $(PRO_PATH)/lib
FIBER_PATH = ../../../../lib_fiber
FIBER_INC = $(FIBER_PATH)/c/include
FIBER_LIB = $(FIBER_PATH)/lib
EXTLIBS =
CFLAGS += -I.. -I$(PRO_INC) -I$(ACL_INC) -I$(FIBER_INC) -I$(FIBER_PATH)/cpp/include -I$(ACL_CPP_INC)
LDFLAGS = -L$(FIBER_LIB) -lfiber_cpp \
-L$(ACL_CPP_LIB) -lacl_cpp \
-L$(PRO_LIB) -l_protocol \
-L$(ACL_LIB) -l_acl \
-L$(FIBER_LIB) -lfiber \
$(EXTLIBS) $(SYSLIB)
###########################################################
OUT_PATH = .
OBJ_PATH = $(OUT_PATH)
#Project's objs
SRC = $(wildcard *.cpp)
OBJ = $(patsubst %.cpp, $(OBJ_PATH)/%.o, $(notdir $(SRC)))
###########################################################
.PHONY = all clean
PROG =
COMPILE = $(CC) $(CFLAGS)
#-Wl,-rpath,$(ACL_LIB) -Wl,-rpath,$(PROTO_LIB) -o $(OBJ_PATH)/$(PROG)
all: RM $(OBJ)
$(CC) $(OBJ) $(LDFLAGS) -o $(OBJ_PATH)/$(PROG)
@echo ""
@echo "All ok! Output:$(PROG)"
@echo ""
$(OBJ_PATH)/%.o: %.cpp
$(COMPILE) $< -o $@
RM:
rm -f $(PROG)
clean cl:
rm -f $(PROG)
rm -f $(OBJ)
rebuild rb: clean all
###########################################################

View File

@ -24,7 +24,7 @@ static void fiber_main(ACL_FIBER *fiber, void *ctx acl_unused)
int main(void)
{
int ch, i;
int i;
/* 创建协程 */
for (i = 0; i < __max_fiber; i++)

View File

@ -1,7 +1,2 @@
fiber: main.o
gcc -o fiber main.o -L../../../lib -l_fiber -L../../../../lib_acl/lib -l_acl -lpthread -ldl
main.o: main.c
gcc -O3 -c main.c -DLINUX2 -I.. -I../../../c/include -I../../../../lib_acl/include
clean:
rm -f fiber main.o
include ../Makefile.in
PROG = fiber

View File

@ -1,11 +1,2 @@
fiber: main.o
g++ -o fiber main.o -L../../../lib -l_fiber_cpp -l_fiber \
-L../../../../lib_acl_cpp/lib -l_acl_cpp \
-L../../../../lib_acl/lib -l_acl \
-lpthread -ldl
main.o: main.cpp
g++ -O3 -Wall -c main.cpp -DLINUX2 -I.. -I../../../cpp/include \
-I../../../../lib_acl_cpp/include
clean:
rm -f fiber main.o
include ../Makefile_cpp.in
PROG = fiber

View File

@ -2,6 +2,7 @@
#include <iostream>
#include "acl_cpp/lib_acl.hpp"
#include "fiber/lib_fiber.hpp"
#include "fiber/go_fiber.hpp"
static void fiber_main(int max_loop)
{

View File

@ -1,11 +1,3 @@
fiber: main.o
g++ -o fiber main.o -L../../../lib -l_fiber_cpp -l_fiber \
-L../../../../lib_acl_cpp/lib -l_acl_cpp \
-L../../../../lib_acl/lib -l_acl \
-lpthread -ldl
main.o: main.cpp
g++ -std=c++11 -O3 -Wall -c main.cpp -DLINUX2 -I.. -I../../../cpp/include \
-I../../../../lib_acl_cpp/include
clean:
rm -f fiber main.o
include ../Makefile_cpp.in
CFLAGS += -std=c++11
PROG = fiber

View File

@ -1,11 +1,2 @@
fiber: main.o
g++ -o fiber main.o -L../../../lib -l_fiber_cpp -l_fiber \
-L../../../../lib_acl_cpp/lib -l_acl_cpp \
-L../../../../lib_acl/lib -l_acl \
-lpthread -ldl
main.o: main.cpp
g++ -O3 -Wall -c main.cpp -DLINUX2 -I.. -I../../../cpp/include \
-I../../../../lib_acl_cpp/include
clean:
rm -f fiber main.o
include ../Makefile_cpp.in
PROG = fiber

View File

@ -0,0 +1,2 @@
include ../Makefile.in
PROG = fiber

View File

@ -0,0 +1,23 @@
#include "lib_acl.h"
#include <stdio.h>
#include <stdlib.h>
#include "fiber/lib_fiber.h"
/* 协程处理入口函数 */
static void fiber_main(ACL_FIBER *fiber acl_unused, void *ctx acl_unused)
{
printf("---fiber is running---\r\n");
acl_fiber_delay(2000);
printf("---fiber is exiting---\r\n");
}
int main(void)
{
acl_fiber_schedule_init(1);
printf("---begin create one fiber----\r\n");
acl_fiber_create(fiber_main, NULL, 64000);
printf("---- all fibers exit ----\r\n");
return 0;
}

View File

@ -0,0 +1,4 @@
#!/bin/sh
#valgrind --tool=memcheck --leak-check=yes --leak-check=full --show-reachable=yes --max-stackframe=3426305034400000 -v ./fiber -n 10 -m 20
valgrind --tool=memcheck --leak-check=yes --leak-check=full --show-reachable=yes -v ./fiber -n 1 -m 2

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>