acl/lib_acl_cpp/samples/master/master_aio/master_aio.cpp

370 lines
7.9 KiB
C++
Raw Normal View History

// master_aio.cpp : <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̨Ӧ<CCA8>ó<EFBFBD><C3B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ㡣
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;
//////////////////////////////////////////////////////////////////////////
/**
* <EFBFBD>ӳٶ<EFBFBD><EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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()
{
}
// aio_timer_reader <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> destroy <20><><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
void destroy()
{
format("timer_reader delete, delay: %d\r\n", delay_);
delete this;
}
// <20><><EFBFBD>ػ<EFBFBD><D8BB><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
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_);
// <20><><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
aio_timer_reader::timer_callback(id);
}
private:
int delay_;
};
/**
* <EFBFBD>ӳ<EFBFBD>д<EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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()
{
}
// aio_timer_reader <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> destroy <20><><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
void destroy()
{
format("timer_writer delete, delay: %d\r\n", delay_);
delete this;
}
// <20><><EFBFBD>ػ<EFBFBD><D8BB><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
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_);
// <20><><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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:
// <20><><EFBFBD>ി<EFBFBD><EFBFBD><E9BAAF>
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:
};
/**
* <EFBFBD><EFBFBD>ͻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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");
}
/**
* ʵ<EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><EFBFBD>ɹ<EFBFBD><EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param data {char*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>ַ
* @param len {int} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD><EFBFBD>
* @return {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD> true <EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD><EFBFBD><EFBFBD>رո<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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);
// <20><><EFBFBD><EFBFBD>Զ<EFBFBD>̿ͻ<CCBF><CDBB><EFBFBD>ϣ<EFBFBD><CFA3><EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD>֮
2014-11-19 00:25:21 +08:00
if (strncmp(data, "quit", 4) == 0)
{
client_->format("Bye!\r\n");
client_->close();
}
// <20><><EFBFBD><EFBFBD>Զ<EFBFBD>̿ͻ<CCBF><CDBB><EFBFBD>ϣ<EFBFBD><CFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD>رգ<D8B1><D5A3><EFBFBD><EFBFBD><EFBFBD>ֹ<EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
else if (strncmp(data, "stop", 4) == 0)
{
client_->format("Stop now!\r\n");
client_->close(); // <20>ر<EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><ECB2BD>
2014-11-19 00:25:21 +08:00
// ֪ͨ<CDA8><EFBFBD><ECB2BD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
client_->get_handle().stop();
}
// <20><>Զ<EFBFBD>̿ͻ<CCBF><CDBB>˻<EFBFBD>д<EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
int delay = 0;
if (strncmp(data, "write_delay", strlen("write_delay")) == 0)
{
// <20>ӳ<EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD>
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)
{
// <20>ӳٶ<D3B3><D9B6><EFBFBD><EFBFBD><EFBFBD>
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);
}
/**
* ʵ<EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD>ɹ<EFBFBD><EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD> true <EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD><EFBFBD><EFBFBD>رո<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
*/
virtual bool write_callback()
{
return (true);
}
/**
* ʵ<EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD>ʱ<EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
*/
virtual void close_callback()
{
// <20><><EFBFBD><EFBFBD><EFBFBD>ڴ˴<DAB4>ɾ<EFBFBD><C9BE><EFBFBD>ö<EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Է<EFBFBD>ֹ<EFBFBD>ڴ<EFBFBD>й¶
2014-11-19 00:25:21 +08:00
format("Close\r\n");
delete this;
}
/**
* ʵ<EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD>ʱ<EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD> true <EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD><EFBFBD><EFBFBD>رո<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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:
// <20><><EFBFBD>ി<EFBFBD><EFBFBD><E9BAAF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD>µ<EFBFBD><C2B5><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>ô˺<C3B4><CBBA><EFBFBD>
2014-11-19 00:25:21 +08:00
virtual bool on_accept(aio_socket_stream* client)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECB2BD><EFBFBD><EFBFBD><EFBFBD>а<EFBFBD><D0B0><EFBFBD>
2014-11-19 00:25:21 +08:00
io_callback* callback = new io_callback(client);
// ע<><D7A2><EFBFBD><EFBFBD><ECB2BD><EFBFBD>Ķ<EFBFBD><C4B6>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
client->add_read_callback(callback);
// ע<><D7A2><EFBFBD><EFBFBD><ECB2BD><EFBFBD><EFBFBD>д<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
client->add_write_callback(callback);
// ע<><D7A2><EFBFBD><EFBFBD><ECB2BD><EFBFBD>Ĺرջص<D5BB><D8B5><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
client->add_close_callback(callback);
// ע<><D7A2><EFBFBD><EFBFBD><ECB2BD><EFBFBD>ij<EFBFBD>ʱ<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
client->add_timeout_callback(callback);
// д<><D0B4>ӭ<EFBFBD><D3AD>Ϣ
2014-11-19 00:25:21 +08:00
if (var_cfg_send_banner)
client->format("hello, you're welcome\r\n");
// <20><><EFBFBD><EFBFBD><ECB2BD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
client->gets(10, false);
//client->read();
return true;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9BAAF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><D0BB>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD>ô˺<C3B4><CBBA><EFBFBD>
2014-11-19 00:25:21 +08:00
virtual void proc_pre_jail()
{
format("proc_pre_jail\r\n");
// ֻ<>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܻ<EFBFBD><DCBB><EFBFBD><EFBFBD><EFBFBD><ECB2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
handle_ = get_handle();
//handle_->keep_timer(true); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD>
// <20><><EFBFBD>õ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF>1<EFBFBD><EFBFBD><EBB4A5>һ<EFBFBD>Σ<EFBFBD><CEA3><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>IDΪ0
2014-11-19 00:25:21 +08:00
handle_->set_timer(timer_test_, 1000000, 0);
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9BAAF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><D0BB>û<EFBFBD><C3BB><EFBFBD><EFBFBD>ݺ<EFBFBD><DDBA><EFBFBD><EFBFBD>ô˺<C3B4><CBBA><EFBFBD>
2014-11-19 00:25:21 +08:00
virtual void proc_on_init()
{
format("proc init\r\n");
// <20><><EFBFBD>õڶ<C3B5><DAB6><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF>2<EFBFBD><EFBFBD><EBB4A5>һ<EFBFBD>Σ<EFBFBD><CEA3><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>IDΪ1
2014-11-19 00:25:21 +08:00
handle_->set_timer(timer_test_, 2000000, 1);
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9BAAF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD>ô˺<C3B4><CBBA><EFBFBD>
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;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD>
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);
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
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);
ma.run_alone(addr, conf); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD>ʽ
2014-11-19 00:25:21 +08:00
}
else
{
#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);
ma.run_alone(addr, conf); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD>ʽ
#else
ma.run_daemon(argc, argv); // acl_master <20><><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD>
#endif
}
2014-11-19 00:25:21 +08:00
return 0;
}