2019-07-27 22:44:32 +08:00
|
|
|
|
// master_aio.cpp : 定义控制台应用程序的入口点。
|
2014-11-19 00:25:21 +08:00
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
|
#include "acl_cpp/lib_acl.hpp"
|
|
|
|
|
|
|
|
|
|
static char *var_cfg_debug_msg;
|
|
|
|
|
|
|
|
|
|
static acl::master_str_tbl var_conf_str_tab[] = {
|
|
|
|
|
{ "debug_msg", "test_msg", &var_cfg_debug_msg },
|
|
|
|
|
|
|
|
|
|
{ 0, 0, 0 }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int var_cfg_debug_enable;
|
|
|
|
|
static int var_cfg_keep_alive;
|
|
|
|
|
static int var_cfg_send_banner;
|
|
|
|
|
|
|
|
|
|
static acl::master_bool_tbl var_conf_bool_tab[] = {
|
|
|
|
|
{ "debug_enable", 1, &var_cfg_debug_enable },
|
|
|
|
|
{ "keep_alive", 1, &var_cfg_keep_alive },
|
|
|
|
|
{ "send_banner", 1, &var_cfg_send_banner },
|
|
|
|
|
|
|
|
|
|
{ 0, 0, 0 }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int var_cfg_io_timeout;
|
|
|
|
|
|
|
|
|
|
static acl::master_int_tbl var_conf_int_tab[] = {
|
|
|
|
|
{ "io_timeout", 120, &var_cfg_io_timeout, 0, 0 },
|
|
|
|
|
|
|
|
|
|
{ 0, 0 , 0 , 0, 0 }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void (*format)(const char*, ...) = acl::log::msg1;
|
|
|
|
|
|
|
|
|
|
using namespace acl;
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 延迟读回调处理类
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
|
|
|
|
class timer_reader: public aio_timer_reader
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
timer_reader(int delay)
|
|
|
|
|
{
|
|
|
|
|
delay_ = delay;
|
|
|
|
|
format("timer_reader init, delay: %d\r\n", delay);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~timer_reader()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// aio_timer_reader 的子类必须重载 destroy 方法
|
2014-11-19 00:25:21 +08:00
|
|
|
|
void destroy()
|
|
|
|
|
{
|
|
|
|
|
format("timer_reader delete, delay: %d\r\n", delay_);
|
|
|
|
|
delete this;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 重载基类回调方法
|
2014-11-19 00:25:21 +08:00
|
|
|
|
virtual void timer_callback(unsigned int id)
|
|
|
|
|
{
|
|
|
|
|
format("timer_reader(%u): timer_callback, delay: %d\r\n", id, delay_);
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
class timer_writer: public aio_timer_writer
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
timer_writer(int delay)
|
|
|
|
|
{
|
|
|
|
|
delay_ = delay;
|
|
|
|
|
format("timer_writer init, delay: %d\r\n", delay);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~timer_writer()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// aio_timer_reader 的子类必须重载 destroy 方法
|
2014-11-19 00:25:21 +08:00
|
|
|
|
void destroy()
|
|
|
|
|
{
|
|
|
|
|
format("timer_writer delete, delay: %d\r\n", delay_);
|
|
|
|
|
delete this;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 重载基类回调方法
|
2014-11-19 00:25:21 +08:00
|
|
|
|
virtual void timer_callback(unsigned int id)
|
|
|
|
|
{
|
|
|
|
|
format("timer_writer(%u): timer_callback, delay: %u\r\n", id, delay_);
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 调用基类的处理过程
|
2014-11-19 00:25:21 +08:00
|
|
|
|
aio_timer_writer::timer_callback(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
int delay_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class timer_test : public aio_timer_callback
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
timer_test() : aio_timer_callback(true) {}
|
|
|
|
|
~timer_test() {}
|
|
|
|
|
protected:
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 基类纯虚函数
|
2014-11-19 00:25:21 +08:00
|
|
|
|
virtual void timer_callback(unsigned int id)
|
|
|
|
|
{
|
|
|
|
|
format("id: %u\r\n", id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void destroy(void)
|
|
|
|
|
{
|
|
|
|
|
delete this;
|
|
|
|
|
format("timer delete now\r\n");
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
};
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 异步客户端流的回调类的子类
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
|
|
|
|
class io_callback : public aio_callback
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
io_callback(aio_socket_stream* client)
|
|
|
|
|
: client_(client)
|
|
|
|
|
, i_(0)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~io_callback()
|
|
|
|
|
{
|
|
|
|
|
format("delete io_callback now ...\r\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
*/
|
|
|
|
|
virtual bool read_callback(char* data, int len)
|
|
|
|
|
{
|
|
|
|
|
if (++i_ < 10)
|
|
|
|
|
format(">>gets(i: %d): %s\r\n", i_, data);
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 如果远程客户端希望退出,则关闭之
|
2014-11-19 00:25:21 +08:00
|
|
|
|
if (strncmp(data, "quit", 4) == 0)
|
|
|
|
|
{
|
|
|
|
|
client_->format("Bye!\r\n");
|
|
|
|
|
client_->close();
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 如果远程客户端希望服务端也关闭,则中止异步事件过程
|
2014-11-19 00:25:21 +08:00
|
|
|
|
else if (strncmp(data, "stop", 4) == 0)
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
if (strncmp(data, "write_delay", strlen("write_delay")) == 0)
|
|
|
|
|
{
|
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);
|
|
|
|
|
if (delay > 0)
|
|
|
|
|
{
|
|
|
|
|
format(">> write delay %d second ...\r\n", delay);
|
|
|
|
|
timer_writer* timer = new timer_writer(delay);
|
|
|
|
|
client_->write(data, len, delay * 1000000, timer);
|
|
|
|
|
client_->gets(10, false);
|
|
|
|
|
return (true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (strncmp(data, "read_delay", strlen("read_delay")) == 0)
|
|
|
|
|
{
|
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);
|
|
|
|
|
if (delay > 0)
|
|
|
|
|
{
|
|
|
|
|
client_->write(data, len);
|
|
|
|
|
format(">> read delay %d second ...\r\n", delay);
|
|
|
|
|
timer_reader* timer = new timer_reader(delay);
|
|
|
|
|
client_->gets(10, false, delay * 1000000, timer);
|
|
|
|
|
return (true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client_->write(data, len);
|
|
|
|
|
//client_->gets(10, false);
|
|
|
|
|
return (true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 实现父类中的虚函数,客户端流的写成功回调过程
|
|
|
|
|
* @return {bool} 返回 true 表示继续,否则希望关闭该异步流
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
|
|
|
|
virtual bool write_callback()
|
|
|
|
|
{
|
|
|
|
|
return (true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 实现父类中的虚函数,客户端流的超时回调过程
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
|
|
|
|
virtual void close_callback()
|
|
|
|
|
{
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 必须在此处删除该动态分配的回调类对象以防止内存泄露
|
2014-11-19 00:25:21 +08:00
|
|
|
|
format("Close\r\n");
|
|
|
|
|
delete this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 实现父类中的虚函数,客户端流的超时回调过程
|
|
|
|
|
* @return {bool} 返回 true 表示继续,否则希望关闭该异步流
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
|
|
|
|
virtual bool timeout_callback()
|
|
|
|
|
{
|
|
|
|
|
format("Timeout ...\r\n");
|
|
|
|
|
return (true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
aio_socket_stream* client_;
|
|
|
|
|
int i_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
class master_aio_test : public master_aio
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
master_aio_test() { timer_test_ = new timer_test(); }
|
|
|
|
|
|
|
|
|
|
~master_aio_test() { }
|
|
|
|
|
|
|
|
|
|
protected:
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 基类纯虚函数:当接收到一个新的连接时调用此函数
|
2014-11-19 00:25:21 +08:00
|
|
|
|
virtual bool on_accept(aio_socket_stream* client)
|
|
|
|
|
{
|
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
|
|
|
|
// 写欢迎信息
|
2014-11-19 00:25:21 +08:00
|
|
|
|
if (var_cfg_send_banner)
|
|
|
|
|
client->format("hello, you're welcome\r\n");
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 从异步流读一行数据
|
2014-11-19 00:25:21 +08:00
|
|
|
|
client->gets(10, false);
|
|
|
|
|
//client->read();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 基类虚函数:服务进程切换用户身份前调用此函数
|
2014-11-19 00:25:21 +08:00
|
|
|
|
virtual void proc_pre_jail()
|
|
|
|
|
{
|
|
|
|
|
format("proc_pre_jail\r\n");
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 只有当程序启动后才能获得异步引擎句柄
|
2014-11-19 00:25:21 +08:00
|
|
|
|
handle_ = get_handle();
|
2019-07-27 22:44:32 +08:00
|
|
|
|
//handle_->keep_timer(true); // 允许定时器被重复触发
|
|
|
|
|
// 设置第一个定时任务,每隔1秒触发一次,定时任务ID为0
|
2014-11-19 00:25:21 +08:00
|
|
|
|
handle_->set_timer(timer_test_, 1000000, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 基类虚函数:服务进程切换用户身份后调用此函数
|
2014-11-19 00:25:21 +08:00
|
|
|
|
virtual void proc_on_init()
|
|
|
|
|
{
|
|
|
|
|
format("proc init\r\n");
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 设置第二个定时任务,每隔2秒触发一次,定时任务ID为1
|
2014-11-19 00:25:21 +08:00
|
|
|
|
handle_->set_timer(timer_test_, 2000000, 1);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 基类虚函数:服务进程退出前调用此函数
|
2014-11-19 00:25:21 +08:00
|
|
|
|
virtual void proc_on_exit()
|
|
|
|
|
{
|
|
|
|
|
format("proc exit\r\n");
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
timer_test* timer_test_;
|
|
|
|
|
aio_handle* handle_;
|
|
|
|
|
};
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
|
{
|
|
|
|
|
#if 0
|
|
|
|
|
int base = 8, nslice = 1024, nalloc_gc = 1000000;
|
|
|
|
|
unsigned int slice_flag = ACL_SLICE_FLAG_GC2 | ACL_SLICE_FLAG_RTGC_OFF;
|
|
|
|
|
|
|
|
|
|
acl_mem_slice_init(base, nslice, nalloc_gc, slice_flag);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
master_aio_test ma;
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 设置配置参数表
|
2014-11-19 00:25:21 +08:00
|
|
|
|
ma.set_cfg_int(var_conf_int_tab);
|
|
|
|
|
ma.set_cfg_int64(NULL);
|
|
|
|
|
ma.set_cfg_str(var_conf_str_tab);
|
|
|
|
|
ma.set_cfg_bool(var_conf_bool_tab);
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 开始运行
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
if (argc >= 2 && strcmp(argv[1], "alone") == 0)
|
|
|
|
|
{
|
|
|
|
|
const char* addr = "127.0.0.1:8888, 127.0.0.1:8889";
|
|
|
|
|
|
|
|
|
|
if (argc >= 3)
|
|
|
|
|
addr = argv[2];
|
|
|
|
|
|
|
|
|
|
const char* conf;
|
|
|
|
|
if (argc >= 4)
|
|
|
|
|
conf = argv[3];
|
|
|
|
|
else
|
|
|
|
|
conf = NULL;
|
|
|
|
|
|
|
|
|
|
format = (void (*)(const char*, ...)) printf;
|
|
|
|
|
format("listen: %s now\r\n", addr);
|
2019-07-27 22:44:32 +08:00
|
|
|
|
ma.run_alone(addr, conf); // 单独运行方式
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
2015-02-11 09:28:05 +08:00
|
|
|
|
{
|
|
|
|
|
#ifdef WIN32
|
|
|
|
|
const char* addr = "127.0.0.1:8888, 127.0.0.1:8889";
|
|
|
|
|
const char* conf = "./master_aio.cf";
|
|
|
|
|
|
|
|
|
|
format = (void (*)(const char*, ...)) printf;
|
|
|
|
|
format("listen: %s now\r\n", addr);
|
2019-07-27 22:44:32 +08:00
|
|
|
|
ma.run_alone(addr, conf); // 单独运行方式
|
2015-02-11 09:28:05 +08:00
|
|
|
|
|
|
|
|
|
#else
|
2019-07-27 22:44:32 +08:00
|
|
|
|
ma.run_daemon(argc, argv); // acl_master 控制模式运行
|
2015-02-11 09:28:05 +08:00
|
|
|
|
#endif
|
|
|
|
|
}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|