acl/lib_fiber/samples/master_fiber/main.cpp

103 lines
2.1 KiB
C++
Raw Normal View History

#include "stdafx.h"
2018-11-30 14:38:22 +08:00
#include <stdio.h>
#include <stdlib.h>
#include "http_servlet.h"
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 acl::master_bool_tbl var_conf_bool_tab[] = {
{ "debug_enable", 1, &var_cfg_debug_enable },
{ 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 }
};
//////////////////////////////////////////////////////////////////////////
class master_fiber_test : public acl::master_fiber
{
public:
master_fiber_test(void) {}
~master_fiber_test(void) {}
protected:
// @override
2021-08-18 16:07:35 +08:00
void on_accept(acl::socket_stream& conn) {
2018-11-30 14:38:22 +08:00
acl_msg_info(">>>accept connection: %d", conn.sock_handle());
conn.set_rw_timeout(0);
acl::memcache_session session("127.0.0.1:11211");
http_servlet servlet(&conn, &session);
servlet.setLocalCharset("gb2312");
2021-08-18 16:07:35 +08:00
while (true) {
if (!servlet.doRun()) {
2018-11-30 14:38:22 +08:00
break;
2021-08-18 16:07:35 +08:00
}
2018-11-30 14:38:22 +08:00
}
acl_msg_info(">>>close one connection: %d, %s",
conn.sock_handle(), acl::last_serror());
}
// @override
2021-08-18 16:07:35 +08:00
void proc_pre_jail(void) {
2018-11-30 14:38:22 +08:00
acl_msg_info(">>>proc_pre_jail<<<");
}
// @override
2021-08-18 16:07:35 +08:00
void proc_on_init(void) {
2018-11-30 14:38:22 +08:00
acl_msg_info(">>>proc_on_init<<<");
}
// @override
2021-08-18 16:07:35 +08:00
void proc_on_exit(void) {
2018-11-30 14:38:22 +08:00
acl_msg_info(">>>proc_on_exit<<<");
}
// @override
2021-08-18 16:07:35 +08:00
bool proc_on_sighup(acl::string&) {
2018-11-30 14:38:22 +08:00
acl_msg_info(">>>proc_on_sighup<<<");
return true;
}
};
int main(int argc, char *argv[])
{
master_fiber_test& mf =
acl::singleton2<master_fiber_test>::get_instance();
acl::acl_cpp_init();
acl::log::stdout_open(true);
mf.set_cfg_int(var_conf_int_tab);
mf.set_cfg_int64(NULL);
mf.set_cfg_str(var_conf_str_tab);
mf.set_cfg_bool(var_conf_bool_tab);
2021-08-18 16:07:35 +08:00
if (argc >= 2 && strcasecmp(argv[1], "alone") == 0) {
const char* addr = "0.0.0.0|9101";
2018-11-30 14:38:22 +08:00
printf("listen: %s\r\n", addr);
mf.run_alone(addr, argc >= 3 ? argv[2] : NULL);
2021-08-18 16:07:35 +08:00
} else {
2018-11-30 14:38:22 +08:00
mf.run_daemon(argc, argv);
2021-08-18 16:07:35 +08:00
}
2018-11-30 14:38:22 +08:00
return 0;
}