Merge branch 'gitee-master' into gitlab-upstream

This commit is contained in:
zhengshuxin 2023-06-10 14:53:25 +08:00
commit 27ca9d4944
6 changed files with 234 additions and 61 deletions

View File

@ -16,11 +16,9 @@ static int __timeout = 0;
/** /**
* *
*/ */
class timer_reader: public acl::aio_timer_reader class timer_reader: public acl::aio_timer_reader {
{
public: public:
timer_reader(int delay) timer_reader(int delay) {
{
delay_ = delay; delay_ = delay;
std::cout << "timer_reader init, delay: " << delay << std::endl; std::cout << "timer_reader init, delay: " << delay << std::endl;
} }
@ -30,16 +28,14 @@ protected:
// aio_timer_reader 的子类必须重载 destroy 方法 // aio_timer_reader 的子类必须重载 destroy 方法
// @override // @override
void destroy(void) void destroy(void) {
{
std::cout << "timer_reader delete, delay: " << delay_ << std::endl; std::cout << "timer_reader delete, delay: " << delay_ << std::endl;
delete this; delete this;
} }
// 重载基类回调方法 // 重载基类回调方法
// @override // @override
void timer_callback(unsigned int id) void timer_callback(unsigned int id) {
{
std::cout << "timer_reader(" << id std::cout << "timer_reader(" << id
<< "): timer_callback, delay: " << delay_ << std::endl; << "): timer_callback, delay: " << delay_ << std::endl;
@ -54,11 +50,9 @@ private:
/** /**
* *
*/ */
class timer_writer: public acl::aio_timer_writer class timer_writer: public acl::aio_timer_writer {
{
public: public:
timer_writer(int delay) timer_writer(int delay) {
{
delay_ = delay; delay_ = delay;
std::cout << "timer_writer init, delay: " << delay << std::endl; std::cout << "timer_writer init, delay: " << delay << std::endl;
} }
@ -68,16 +62,14 @@ protected:
// aio_timer_reader 的子类必须重载 destroy 方法 // aio_timer_reader 的子类必须重载 destroy 方法
// @override // @override
void destroy(void) void destroy(void) {
{
std::cout << "timer_writer delete, delay: " << delay_ << std::endl; std::cout << "timer_writer delete, delay: " << delay_ << std::endl;
delete this; delete this;
} }
// 重载基类回调方法 // 重载基类回调方法
// @override // @override
void timer_callback(unsigned int id) void timer_callback(unsigned int id) {
{
std::cout << "timer_writer(" << id << "): timer_callback, delay: " std::cout << "timer_writer(" << id << "): timer_callback, delay: "
<< delay_ << std::endl; << delay_ << std::endl;
@ -92,16 +84,13 @@ private:
/** /**
* *
*/ */
class io_callback : public acl::aio_callback class io_callback : public acl::aio_callback {
{
public: public:
io_callback(acl::aio_socket_stream* client) io_callback(acl::aio_socket_stream* client)
: client_(client) : client_(client), i_(0) {}
, i_(0) {}
protected: protected:
~io_callback(void) ~io_callback(void) {
{
std::cout << "delete io_callback now ..." << std::endl; std::cout << "delete io_callback now ..." << std::endl;
} }
@ -111,8 +100,7 @@ protected:
* @param len {int} * @param len {int}
* @return {bool} true * @return {bool} true
*/ */
bool read_callback(char* data, int len) bool read_callback(char* data, int len) {
{
i_++; i_++;
if (i_ < 5) { if (i_ < 5) {
std::cout << ">>gets(i:" << i_ << "): " std::cout << ">>gets(i:" << i_ << "): "
@ -176,16 +164,14 @@ protected:
* *
* @return {bool} true * @return {bool} true
*/ */
bool write_callback(void) bool write_callback(void) {
{
return true; return true;
} }
/** /**
* *
*/ */
void close_callback(void) void close_callback(void) {
{
// 必须在此处删除该动态分配的回调类对象以防止内存泄露 // 必须在此处删除该动态分配的回调类对象以防止内存泄露
delete this; delete this;
} }
@ -194,8 +180,7 @@ protected:
* *
* @return {bool} true * @return {bool} true
*/ */
bool timeout_callback(void) bool timeout_callback(void) {
{
std::cout << "Timeout, delete it ..." << std::endl; std::cout << "Timeout, delete it ..." << std::endl;
return (false); return (false);
} }
@ -213,8 +198,8 @@ class io_accept_callback : public acl::aio_accept_callback
{ {
public: public:
io_accept_callback(void) {} io_accept_callback(void) {}
~io_accept_callback(void)
{ ~io_accept_callback(void) {
printf(">>io_accept_callback over!\n"); printf(">>io_accept_callback over!\n");
} }
@ -223,8 +208,7 @@ public:
* @param client {aio_socket_stream*} * @param client {aio_socket_stream*}
* @return {bool} true * @return {bool} true
*/ */
bool accept_callback(acl::aio_socket_stream* client) bool accept_callback(acl::aio_socket_stream* client) {
{
printf("proactor accept one\r\n"); printf("proactor accept one\r\n");
return handle_client(client); return handle_client(client);
} }
@ -234,8 +218,7 @@ public:
* @param server {acl::aio_listen_stream&} * @param server {acl::aio_listen_stream&}
* @return {bool} * @return {bool}
*/ */
bool listen_callback(acl::aio_listen_stream& server) bool listen_callback(acl::aio_listen_stream& server) {
{
// reactor 模式下需要用户自己调用 accept 方法 // reactor 模式下需要用户自己调用 accept 方法
acl::aio_socket_stream* client = server.accept(); acl::aio_socket_stream* client = server.accept();
if (client == NULL) { if (client == NULL) {
@ -248,8 +231,7 @@ public:
} }
private: private:
bool handle_client(acl::aio_socket_stream* client) bool handle_client(acl::aio_socket_stream* client) {
{
// 创建异步客户端流的回调对象并与该异步流进行绑定 // 创建异步客户端流的回调对象并与该异步流进行绑定
io_callback* callback = new io_callback(client); io_callback* callback = new io_callback(client);
@ -309,8 +291,7 @@ private:
//#include <sys/epoll.h> //#include <sys/epoll.h>
static void aio_run(bool use_reactor, acl::aio_handle& handle, static void aio_run(bool use_reactor, acl::aio_handle& handle,
acl::aio_listen_stream* sstream) acl::aio_listen_stream* sstream) {
{
// 创建回调类对象,当有新连接到达时自动调用此类对象的回调过程 // 创建回调类对象,当有新连接到达时自动调用此类对象的回调过程
io_accept_callback callback; io_accept_callback callback;
@ -351,8 +332,7 @@ static void aio_run(bool use_reactor, acl::aio_handle& handle,
} }
static acl::aio_listen_stream* bind_addr(acl::aio_handle& handle, static acl::aio_listen_stream* bind_addr(acl::aio_handle& handle,
const acl::string& addr) const acl::string& addr) {
{
// 创建监听异步流 // 创建监听异步流
acl::aio_listen_stream* sstream = new acl::aio_listen_stream(&handle); acl::aio_listen_stream* sstream = new acl::aio_listen_stream(&handle);
@ -371,45 +351,43 @@ static acl::aio_listen_stream* bind_addr(acl::aio_handle& handle,
return sstream; return sstream;
} }
static void usage(const char* procname) static void usage(const char* procname) {
{
printf("usage: %s -h[help]\r\n" printf("usage: %s -h[help]\r\n"
" -l ip:port\r\n" " -s ip:port, default: 127.0.0.1:9001\r\n"
" -L line_max_length\r\n" " -d line_max_length\r\n"
" -t timeout\r\n" " -t timeout\r\n"
" -r [use reactor mode other proactor mode, default: proactor mode]\r\n" " -R [use reactor mode other proactor mode, default: proactor mode]\r\n"
" -f [if use fiber mode]\r\n" " -F [if use fiber mode]\r\n"
" -k[use kernel event: epoll/iocp/kqueue/devpool]\r\n", " -K [use kernel event: epoll/iocp/kqueue/devpool]\r\n",
procname); procname);
} }
int main(int argc, char* argv[]) int main(int argc, char* argv[]) {
{
bool use_kernel = false, use_reactor = false, use_fiber = false; bool use_kernel = false, use_reactor = false, use_fiber = false;
acl::string addr("127.0.0.1:9001");
int ch; int ch;
acl::string addr(":9001");
while ((ch = getopt(argc, argv, "l:hkL:t:rf")) > 0) { while ((ch = getopt(argc, argv, "s:hKd:t:RF")) > 0) {
switch (ch) { switch (ch) {
case 'h': case 'h':
usage(argv[0]); usage(argv[0]);
return 0; return 0;
case 'l': case 's':
addr = optarg; addr = optarg;
break; break;
case 'k': case 'K':
use_kernel = true; use_kernel = true;
break; break;
case 'L': case 'd':
__max = atoi(optarg); __max = atoi(optarg);
break; break;
case 't': case 't':
__timeout = atoi(optarg); __timeout = atoi(optarg);
break; break;
case 'r': case 'R':
use_reactor = true; use_reactor = true;
break; break;
case 'f': case 'F':
use_fiber = true; use_fiber = true;
break; break;
default: default:
@ -423,7 +401,6 @@ int main(int argc, char* argv[])
acl::fiber::stdout_open(true); acl::fiber::stdout_open(true);
acl::log::stdout_open(true); acl::log::stdout_open(true);
if (use_fiber) { if (use_fiber) {
go[&] { go[&] {
// 构建异步引擎类对象 // 构建异步引擎类对象

View File

@ -0,0 +1,3 @@
include ../Makefile_cpp.in
CFLAGS += -std=c++11
PROG = fiber_pool

View File

@ -0,0 +1,173 @@
#include "stdafx.h"
#include <memory>
#include <atomic>
class client_socket {
public:
client_socket(acl::socket_stream* conn, std::atomic<long>& nusers)
: conn_(conn), nusers_(nusers) {}
~client_socket(void) {
printf("delete conn=%p\r\n", conn_);
--nusers_;
delete conn_;
}
acl::socket_stream& get_conn(void) {
return *conn_;
}
private:
acl::socket_stream* conn_;
std::atomic<long>& nusers_;
};
using shared_client = std::shared_ptr<client_socket>;
class message {
public:
message(shared_client client, std::atomic<long>& nmsgs,
const char* buf, size_t len)
: client_(client), nmsgs_(nmsgs), buf_(buf, len) {}
~message(void) { --nmsgs_; }
const std::string& get_data(void) const {
return buf_;
}
shared_client get_client(void) {
return client_;
}
private:
shared_client client_;
std::atomic<long>& nmsgs_;
std::string buf_;
};
using shared_message = std::shared_ptr<message>;
class mybox {
public:
mybox(void) : sem_(0) {}
~mybox(void) {}
void push(shared_message msg) {
msgs_.emplace_back(msg);
sem_.post();
}
shared_message pop(void) {
(void) sem_.wait();
shared_message msg = msgs_.front();
msgs_.pop_front();
return msg;
}
private:
acl::fiber_sem sem_;
std::list<shared_message> msgs_;
};
static void usage(const char* procname) {
printf("usage: %s -h[help]\r\n"
" -s ip:port, default: 127.0.0.1:9001\r\n"
" -c fiber_pool_count [default: 100] \r\n"
" -r timeout\r\n"
, procname);
}
int main(int argc, char* argv[]) {
acl::string addr("127.0.0.1:9001");
int ch, nfibers = 100;
while ((ch = getopt(argc, argv, "hs:c:r:")) > 0) {
switch (ch) {
case 'h':
usage(argv[0]);
return 0;
case 's':
addr = optarg;
break;
case 'c':
nfibers = atoi(optarg);
break;
default:
break;
}
}
acl::fiber::stdout_open(true);
acl::log::stdout_open(true);
acl::server_socket ss;
if (!ss.open(addr)) {
printf("listen %s error %s\r\n", addr.c_str(), acl::last_serror());
return 1;
}
printf("listen on %s, fiber pool: %d\r\n", addr.c_str(), nfibers);
mybox box;
for (int i = 0; i < nfibers; i++) {
go[&box] {
while (true) {
shared_message msg = box.pop();
auto client = msg->get_client();
auto data = msg->get_data();
if (client->get_conn().write(data.c_str(), data.size()) == -1) {
printf("write error: %s\r\n", acl::last_serror());
break;
}
}
};
}
std::atomic<long> nusers, nmsgs;
go[&nusers, &nmsgs] {
while (true) {
std::cout << "client count: " << nusers << "; message count: " << nmsgs << std::endl;
::sleep(1);
}
};
go[&ss, &box, &nusers, &nmsgs] {
while (true) {
acl::socket_stream* conn = ss.accept();
if (conn == NULL) {
printf("accept error %s\r\n", acl::last_serror());
break;
}
++nusers;
auto client = std::make_shared<client_socket>(conn, nusers);
go[&box, &nmsgs, client] {
char buf[4096];
while (true) {
int ret = client->get_conn().read(buf, sizeof(buf), false);
if (ret <= 0) {
break;
}
if (client->get_conn().write(buf, ret) != ret) {
break;
} else {
continue;
}
++nmsgs;
auto msg = std::make_shared<message>(client, nmsgs, buf, ret);
box.push(msg);
}
};
}
};
acl::fiber::schedule();
return 0;
}

View File

@ -0,0 +1,18 @@
// stdafx.h : 标准系统包含文件的包含文件,
// 或是常用但不常更改的项目特定的包含文件
//
#pragma once
//#include <iostream>
//#include <tchar.h>
// TODO: 在此处引用程序要求的附加头文件
#include <assert.h>
#include <iostream>
#include "lib_acl.h" // just for getopt on Windows
#include "acl_cpp/lib_acl.hpp"
#include "fiber/libfiber.hpp"
#include "fiber/go_fiber.hpp"

View File

@ -1,3 +1,5 @@
#define _GNU_SOURCE
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
@ -189,7 +191,6 @@ static void fiber_connect(ACL_FIBER *fiber acl_unused, void *ctx acl_unused)
printf("fiber-%d: connect %s:%d error %s\r\n", printf("fiber-%d: connect %s:%d error %s\r\n",
acl_fiber_self(), __server_ip, __server_port, acl_fiber_self(), __server_ip, __server_port,
acl_last_serror()); acl_last_serror());
return;
} else { } else {
__total_clients++; __total_clients++;
printf("fiber-%d: connect %s:%d ok, clients: %d, fd: %d\r\n", printf("fiber-%d: connect %s:%d ok, clients: %d, fd: %d\r\n",

View File

@ -1,3 +1,4 @@
#define _GNU_SOURCE
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <signal.h> #include <signal.h>