2019-07-27 22:44:32 +08:00
|
|
|
|
#include <iostream>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
#include <assert.h>
|
2019-06-16 20:10:40 +08:00
|
|
|
|
#include "lib_acl.h" // just for getopt on Windows
|
2014-11-19 00:25:21 +08:00
|
|
|
|
#include "acl_cpp/acl_cpp_init.hpp"
|
2015-01-06 23:24:02 +08:00
|
|
|
|
#include "acl_cpp/stdlib/log.hpp"
|
2014-11-19 00:25:21 +08:00
|
|
|
|
#include "acl_cpp/stream/aio_handle.hpp"
|
|
|
|
|
#include "acl_cpp/stream/aio_istream.hpp"
|
|
|
|
|
#include "acl_cpp/stream/aio_listen_stream.hpp"
|
|
|
|
|
#include "acl_cpp/stream/aio_socket_stream.hpp"
|
|
|
|
|
|
|
|
|
|
static int __max = 0;
|
|
|
|
|
static int __timeout = 0;
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 延迟读回调处理类
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
2019-06-16 20:10:40 +08:00
|
|
|
|
class timer_reader: public acl::aio_timer_reader
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
timer_reader(int delay)
|
|
|
|
|
{
|
|
|
|
|
delay_ = delay;
|
|
|
|
|
std::cout << "timer_reader init, delay: " << delay << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
protected:
|
|
|
|
|
~timer_reader(void) {}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// aio_timer_reader 的子类必须重载 destroy 方法
|
2019-06-16 20:10:40 +08:00
|
|
|
|
// @override
|
|
|
|
|
void destroy(void)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
std::cout << "timer_reader delete, delay: " << delay_ << std::endl;
|
|
|
|
|
delete this;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 重载基类回调方法
|
2019-06-16 20:10:40 +08:00
|
|
|
|
// @override
|
|
|
|
|
void timer_callback(unsigned int id)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
std::cout << "timer_reader(" << id
|
|
|
|
|
<< "): timer_callback, delay: " << delay_ << std::endl;
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 调用基类的处理过程
|
2014-11-19 00:25:21 +08:00
|
|
|
|
aio_timer_reader::timer_callback(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
int delay_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 延迟写回调处理类
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
2019-06-16 20:10:40 +08:00
|
|
|
|
class timer_writer: public acl::aio_timer_writer
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
timer_writer(int delay)
|
|
|
|
|
{
|
|
|
|
|
delay_ = delay;
|
|
|
|
|
std::cout << "timer_writer init, delay: " << delay << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
protected:
|
|
|
|
|
~timer_writer(void) {}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// aio_timer_reader 的子类必须重载 destroy 方法
|
2019-06-16 20:10:40 +08:00
|
|
|
|
// @override
|
|
|
|
|
void destroy(void)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
std::cout << "timer_writer delete, delay: " << delay_ << std::endl;
|
|
|
|
|
delete this;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 重载基类回调方法
|
2019-06-16 20:10:40 +08:00
|
|
|
|
// @override
|
|
|
|
|
void timer_callback(unsigned int id)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
std::cout << "timer_writer(" << id << "): timer_callback, delay: "
|
|
|
|
|
<< delay_ << std::endl;
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 调用基类的处理过程
|
2019-06-16 20:10:40 +08:00
|
|
|
|
acl::aio_timer_writer::timer_callback(id);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
int delay_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 异步客户端流的回调类的子类
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
2019-06-16 20:10:40 +08:00
|
|
|
|
class io_callback : public acl::aio_callback
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
public:
|
2019-06-16 20:10:40 +08:00
|
|
|
|
io_callback(acl::aio_socket_stream* client)
|
|
|
|
|
: client_(client)
|
|
|
|
|
, i_(0) {}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
protected:
|
|
|
|
|
~io_callback(void)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
std::cout << "delete io_callback now ..." << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 实现父类中的虚函数,客户端流的读成功回调过程
|
|
|
|
|
* @param data {char*} 读到的数据地址
|
|
|
|
|
* @param len {int} 读到的数据长度
|
|
|
|
|
* @return {bool} 返回 true 表示继续,否则希望关闭该异步流
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
|
|
|
|
bool read_callback(char* data, int len)
|
|
|
|
|
{
|
|
|
|
|
i_++;
|
2019-06-16 20:10:40 +08:00
|
|
|
|
if (i_ < 10) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
std::cout << ">>gets(i:" << i_ << "): "
|
|
|
|
|
<< data << std::endl;
|
2019-06-16 20:10:40 +08:00
|
|
|
|
}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 如果远程客户端希望退出,则关闭之
|
2019-06-16 20:10:40 +08:00
|
|
|
|
if (strncasecmp(data, "quit", 4) == 0) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
client_->format("Bye!\r\n");
|
|
|
|
|
client_->close();
|
2019-06-16 20:10:40 +08:00
|
|
|
|
return false;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 如果远程客户端希望服务端也关闭,则中止异步事件过程
|
2019-06-16 20:10:40 +08:00
|
|
|
|
else if (strncasecmp(data, "stop", 4) == 0) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
client_->format("Stop now!\r\n");
|
2019-07-27 22:44:32 +08:00
|
|
|
|
client_->close(); // 关闭远程异步流
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 通知异步引擎关闭循环过程
|
2014-11-19 00:25:21 +08:00
|
|
|
|
client_->get_handle().stop();
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 向远程客户端回写收到的数据
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
int delay = 0;
|
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
if (!strncasecmp(data, "write_delay", strlen("write_delay"))) {
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 延迟写过程
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
const char* ptr = data + strlen("write_delay");
|
|
|
|
|
delay = atoi(ptr);
|
2019-06-16 20:10:40 +08:00
|
|
|
|
if (delay > 0) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
std::cout << ">> write delay " << delay
|
|
|
|
|
<< " second ..." << std::endl;
|
|
|
|
|
timer_writer* timer = new timer_writer(delay);
|
|
|
|
|
client_->write(data, len, delay * 1000000, timer);
|
|
|
|
|
client_->gets(10, false);
|
2019-06-16 20:10:40 +08:00
|
|
|
|
return true;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
2019-06-16 20:10:40 +08:00
|
|
|
|
} else if (!strncasecmp(data, "read_delay", strlen("read_delay"))) {
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 延迟读过程
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
const char* ptr = data + strlen("read_delay");
|
|
|
|
|
delay = atoi(ptr);
|
2019-06-16 20:10:40 +08:00
|
|
|
|
if (delay > 0) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
client_->write(data, len);
|
|
|
|
|
std::cout << ">> read delay " << delay
|
|
|
|
|
<< " second ..." << std::endl;
|
|
|
|
|
timer_reader* timer = new timer_reader(delay);
|
|
|
|
|
client_->gets(10, false, delay * 1000000, timer);
|
2019-06-16 20:10:40 +08:00
|
|
|
|
return true;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client_->write(data, len);
|
|
|
|
|
//client_->gets(10, false);
|
2019-06-16 20:10:40 +08:00
|
|
|
|
return true;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 实现父类中的虚函数,客户端流的写成功回调过程
|
|
|
|
|
* @return {bool} 返回 true 表示继续,否则希望关闭该异步流
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
2019-06-16 20:10:40 +08:00
|
|
|
|
bool write_callback(void)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
2019-06-16 20:10:40 +08:00
|
|
|
|
return true;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 实现父类中的虚函数,客户端流的超时回调过程
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
2019-06-16 20:10:40 +08:00
|
|
|
|
void close_callback(void)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 必须在此处删除该动态分配的回调类对象以防止内存泄露
|
2014-11-19 00:25:21 +08:00
|
|
|
|
delete this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 实现父类中的虚函数,客户端流的超时回调过程
|
|
|
|
|
* @return {bool} 返回 true 表示继续,否则希望关闭该异步流
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
2019-06-16 20:10:40 +08:00
|
|
|
|
bool timeout_callback(void)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
std::cout << "Timeout, delete it ..." << std::endl;
|
|
|
|
|
return (false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2019-06-16 20:10:40 +08:00
|
|
|
|
acl::aio_socket_stream* client_;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
int i_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 异步监听流的回调类的子类
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
2019-06-16 20:10:40 +08:00
|
|
|
|
class io_accept_callback : public acl::aio_accept_callback
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
public:
|
2019-06-16 20:10:40 +08:00
|
|
|
|
io_accept_callback(void) {}
|
|
|
|
|
~io_accept_callback(void)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
printf(">>io_accept_callback over!\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 基类虚函数,当有新连接到达后调用此回调过程
|
|
|
|
|
* @param client {aio_socket_stream*} 异步客户端流
|
|
|
|
|
* @return {bool} 返回 true 以通知监听流继续监听
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
2019-06-16 20:10:40 +08:00
|
|
|
|
bool accept_callback(acl::aio_socket_stream* client)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 创建异步客户端流的回调对象并与该异步流进行绑定
|
2014-11-19 00:25:21 +08:00
|
|
|
|
io_callback* callback = new io_callback(client);
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 注册异步流的读回调过程
|
2014-11-19 00:25:21 +08:00
|
|
|
|
client->add_read_callback(callback);
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 注册异步流的写回调过程
|
2014-11-19 00:25:21 +08:00
|
|
|
|
client->add_write_callback(callback);
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 注册异步流的关闭回调过程
|
2014-11-19 00:25:21 +08:00
|
|
|
|
client->add_close_callback(callback);
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 注册异步流的超时回调过程
|
2014-11-19 00:25:21 +08:00
|
|
|
|
client->add_timeout_callback(callback);
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 当限定了行数据最大长度时
|
2019-06-16 20:10:40 +08:00
|
|
|
|
if (__max > 0) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
client->set_buf_max(__max);
|
2019-06-16 20:10:40 +08:00
|
|
|
|
}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 从异步流读一行数据
|
2014-11-19 00:25:21 +08:00
|
|
|
|
client->gets(__timeout, false);
|
2019-06-16 20:10:40 +08:00
|
|
|
|
return true;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void usage(const char* procname)
|
|
|
|
|
{
|
|
|
|
|
printf("usage: %s -h[help]\r\n"
|
2015-01-04 22:51:01 +08:00
|
|
|
|
" -l ip:port\r\n"
|
2014-11-19 00:25:21 +08:00
|
|
|
|
" -L line_max_length\r\n"
|
|
|
|
|
" -t timeout\r\n"
|
|
|
|
|
" -k[use kernel event: epoll/iocp/kqueue/devpool]\r\n",
|
|
|
|
|
procname);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
|
{
|
|
|
|
|
bool use_kernel = false;
|
|
|
|
|
int ch;
|
2015-01-04 22:51:01 +08:00
|
|
|
|
acl::string addr(":9001");
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
while ((ch = getopt(argc, argv, "l:hkL:t:")) > 0) {
|
|
|
|
|
switch (ch) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
case 'h':
|
|
|
|
|
usage(argv[0]);
|
2019-06-16 20:10:40 +08:00
|
|
|
|
return 0;
|
2015-01-04 22:51:01 +08:00
|
|
|
|
case 'l':
|
|
|
|
|
addr = optarg;
|
|
|
|
|
break;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
case 'k':
|
|
|
|
|
use_kernel = true;
|
|
|
|
|
break;
|
|
|
|
|
case 'L':
|
|
|
|
|
__max = atoi(optarg);
|
|
|
|
|
break;
|
|
|
|
|
case 't':
|
|
|
|
|
__timeout = atoi(optarg);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 初始化ACL库(尤其是在WIN32下一定要调用此函数,在UNIX平台下可不调用)
|
2019-06-16 20:10:40 +08:00
|
|
|
|
acl::acl_cpp_init();
|
|
|
|
|
|
2015-01-06 23:24:02 +08:00
|
|
|
|
acl::log::stdout_open(true);
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 构建异步引擎类对象
|
2019-06-16 20:10:40 +08:00
|
|
|
|
acl::aio_handle handle(use_kernel ? acl::ENGINE_KERNEL : acl::ENGINE_SELECT);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 创建监听异步流
|
2019-06-16 20:10:40 +08:00
|
|
|
|
acl::aio_listen_stream* sstream = new acl::aio_listen_stream(&handle);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 监听指定的地址
|
2019-06-16 20:10:40 +08:00
|
|
|
|
if (!sstream->open(addr.c_str())) {
|
2015-01-04 22:51:01 +08:00
|
|
|
|
std::cout << "open " << addr.c_str() << " error!" << std::endl;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
sstream->close();
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// XXX: 为了保证能关闭监听流,应在此处再 check 一下
|
2014-11-19 00:25:21 +08:00
|
|
|
|
handle.check();
|
|
|
|
|
|
|
|
|
|
getchar();
|
2019-06-16 20:10:40 +08:00
|
|
|
|
return 1;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 创建回调类对象,当有新连接到达时自动调用此类对象的回调过程
|
2014-11-19 00:25:21 +08:00
|
|
|
|
io_accept_callback callback;
|
|
|
|
|
sstream->add_accept_callback(&callback);
|
2015-01-04 22:51:01 +08:00
|
|
|
|
std::cout << "Listen: " << addr.c_str() << " ok!" << std::endl;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
while (true) {
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 如果返回 false 则表示不再继续,需要退出
|
2019-06-16 20:10:40 +08:00
|
|
|
|
if (!handle.check()) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
std::cout << "aio_server stop now ..." << std::endl;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 关闭监听流并释放流对象
|
2014-11-19 00:25:21 +08:00
|
|
|
|
sstream->close();
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// XXX: 为了保证能关闭监听流,应在此处再 check 一下
|
2014-11-19 00:25:21 +08:00
|
|
|
|
handle.check();
|
|
|
|
|
|
2019-06-16 20:10:40 +08:00
|
|
|
|
return 0;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|