2019-07-28 10:31:56 +08:00
|
|
|
|
// master_threads.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"
|
2017-03-31 23:04:16 +08:00
|
|
|
|
#include "lib_acl.h"
|
2014-11-19 00:25:21 +08:00
|
|
|
|
#include "acl_cpp/stdlib/log.hpp"
|
|
|
|
|
#include "acl_cpp/stdlib/util.hpp"
|
2017-03-31 23:04:16 +08:00
|
|
|
|
#include "acl_cpp/stream/server_socket.hpp"
|
2014-11-19 00:25:21 +08:00
|
|
|
|
#include "acl_cpp/master/master_threads.hpp"
|
|
|
|
|
#include "acl_cpp/event/event_timer.hpp"
|
|
|
|
|
#include "acl_cpp/stream/socket_stream.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_loop;
|
|
|
|
|
|
|
|
|
|
static acl::master_bool_tbl var_conf_bool_tab[] = {
|
|
|
|
|
{ "debug_enable", 1, &var_cfg_debug_enable },
|
|
|
|
|
{ "keep_alive", 1, &var_cfg_keep_alive },
|
|
|
|
|
{ "loop_read", 1, &var_cfg_loop },
|
|
|
|
|
|
|
|
|
|
{ 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;
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
class master_timer_test : public acl::event_timer
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
master_timer_test(acl::socket_stream* stream)
|
|
|
|
|
: max_(0)
|
|
|
|
|
, count_(0)
|
|
|
|
|
, stream_(stream)
|
|
|
|
|
{
|
2015-01-04 22:51:01 +08:00
|
|
|
|
(void) stream_;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void set_max(int max)
|
|
|
|
|
{
|
|
|
|
|
max_ = max;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>麯<EFBFBD><E9BAAF>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
virtual void timer_callback(unsigned int id)
|
|
|
|
|
{
|
|
|
|
|
format("timer callback, id: %u\r\n", id);
|
|
|
|
|
if (count_++ >= max_)
|
|
|
|
|
{
|
|
|
|
|
printf("clear all timer task now\r\n");
|
|
|
|
|
clear();
|
|
|
|
|
}
|
|
|
|
|
//else
|
|
|
|
|
// set_task(1000, 1000000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void destroy()
|
|
|
|
|
{
|
|
|
|
|
format("destroy called\r\n");
|
|
|
|
|
delete this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
int max_;
|
|
|
|
|
int count_;
|
|
|
|
|
acl::socket_stream* stream_;
|
|
|
|
|
|
|
|
|
|
~master_timer_test()
|
|
|
|
|
{
|
|
|
|
|
format("timer destroy now!\r\n");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
class master_threads_test : public acl::master_threads
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
master_threads_test()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~master_threads_test()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD>ി<EFBFBD>麯<EFBFBD><E9BAAF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿɶ<DDBF><C9B6><EFBFBD><EFBFBD>ر<EFBFBD>ʱ<EFBFBD>ص<EFBFBD><D8B5>˺<EFBFBD><CBBA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> true <20><>ʾ
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><CDBB>˱<EFBFBD><CBB1>ֳ<EFBFBD><D6B3><EFBFBD><EFBFBD>ӣ<EFBFBD><D3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>Ҫ<EFBFBD>رտͻ<D5BF><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
virtual bool thread_on_read(acl::socket_stream* stream)
|
|
|
|
|
{
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
if (on_read(stream) == false)
|
|
|
|
|
return false;
|
|
|
|
|
if (var_cfg_loop == 0)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool on_read(acl::socket_stream* stream)
|
|
|
|
|
{
|
|
|
|
|
format("%s(%d)", __FILE__, __LINE__);
|
|
|
|
|
acl::string buf;
|
|
|
|
|
if (stream->gets(buf) == false)
|
|
|
|
|
{
|
|
|
|
|
format("gets error: %s", acl::last_serror());
|
|
|
|
|
format("%s(%d)", __FILE__, __LINE__);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (buf == "quit")
|
|
|
|
|
{
|
|
|
|
|
stream->puts("bye!");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (buf == "timer")
|
|
|
|
|
{
|
|
|
|
|
int max = 0;
|
|
|
|
|
master_timer_test* timer = new master_timer_test(stream);
|
|
|
|
|
timer->keep_timer(true);
|
|
|
|
|
|
|
|
|
|
timer->set_task(1000, 1000000);
|
|
|
|
|
max += 2;
|
|
|
|
|
timer->set_task(1001, 1000000);
|
|
|
|
|
max += 2;
|
|
|
|
|
timer->set_task(1002, 1000000);
|
|
|
|
|
max += 2;
|
|
|
|
|
timer->set_task(1003, 1000000);
|
|
|
|
|
max += 2;
|
|
|
|
|
timer->set_max(max);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD>û<EFBFBD><C3BB><EFBFBD><E0B7BD><EFBFBD><EFBFBD><EFBFBD>ö<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
proc_set_timer(timer);
|
|
|
|
|
stream->format("set timer ok\r\n");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (buf.empty())
|
|
|
|
|
{
|
|
|
|
|
if (stream->write("\r\n") == -1)
|
|
|
|
|
{
|
|
|
|
|
format("write 1 error: %s", acl::last_serror());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (stream->write(buf) == -1)
|
|
|
|
|
{
|
|
|
|
|
format("write 2 error: %s, buf(%s), len: %d",
|
|
|
|
|
acl::last_serror(), buf.c_str(), (int) buf.length());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else if (stream->write("\r\n") == -1)
|
|
|
|
|
{
|
|
|
|
|
format("write 3 client error: %s", acl::last_serror());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>麯<EFBFBD><E9BAAF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>ô˺<C3B4><CBBA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȶԿͻ<D4BF><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽ<EFBFBD><D3BD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> true <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
// Ҫ<><D2AA><EFBFBD>رոÿͻ<C3BF><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
virtual bool thread_on_accept(acl::socket_stream* stream)
|
|
|
|
|
{
|
2015-03-11 09:09:28 +08:00
|
|
|
|
stream->set_rw_timeout(2);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
format("accept one client, peer: %s, local: %s, var_cfg_io_timeout: %d\r\n",
|
|
|
|
|
stream->get_peer(), stream->get_local(), var_cfg_io_timeout);
|
|
|
|
|
if (stream->format("hello, you're welcome!\r\n") == -1)
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>麯<EFBFBD><E9BAAF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӹر<D3B9>ʱ<EFBFBD><CAB1><EFBFBD>ô˺<C3B4><CBBA><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
virtual void thread_on_close(acl::socket_stream*)
|
|
|
|
|
{
|
|
|
|
|
format("client closed now\r\n");
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>麯<EFBFBD><E9BAAF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̳߳ش<CCB3><D8B4><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>ô˺<C3B4><CBBA><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
virtual void thread_on_init()
|
|
|
|
|
{
|
|
|
|
|
#ifdef WIN32
|
|
|
|
|
format("thread init: tid: %lu\r\n", GetCurrentThreadId());
|
|
|
|
|
#else
|
|
|
|
|
format("thread init: tid: %lu\r\n", pthread_self());
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>麯<EFBFBD><E9BAAF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̳߳<DFB3><CCB3>е<EFBFBD>һ<EFBFBD><D2BB><EFBFBD>߳<EFBFBD><DFB3>˳<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>ô˺<C3B4><CBBA><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
virtual void thread_on_exit()
|
|
|
|
|
{
|
|
|
|
|
#ifdef WIN32
|
|
|
|
|
format("thread exit: tid: %lu\r\n", GetCurrentThreadId());
|
|
|
|
|
#else
|
|
|
|
|
format("thread exit: tid: %lu\r\n", pthread_self());
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <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");
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <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");
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <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:
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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_threads_test mt;
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
mt.set_cfg_int(var_conf_int_tab);
|
|
|
|
|
mt.set_cfg_int64(NULL);
|
|
|
|
|
mt.set_cfg_str(var_conf_str_tab);
|
|
|
|
|
mt.set_cfg_bool(var_conf_bool_tab);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2021-01-17 18:57:37 +08:00
|
|
|
|
if (argc == 1 || (argc >= 2 && strcmp(argv[1], "alone") == 0))
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
2015-03-12 15:25:03 +08:00
|
|
|
|
int task_count = 2, threads_count = 2;
|
2021-01-17 18:57:37 +08:00
|
|
|
|
format("listen: 127.0.0.1|8888\r\n");
|
2015-03-11 09:09:28 +08:00
|
|
|
|
acl::log::stdout_open(true);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD>ʽ
|
2014-11-19 00:25:21 +08:00
|
|
|
|
if (argc >= 3)
|
2021-01-17 18:57:37 +08:00
|
|
|
|
mt.run_alone("127.0.0.1|8888", argv[2],
|
2015-02-11 09:28:05 +08:00
|
|
|
|
task_count, threads_count);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
else
|
2021-01-17 18:57:37 +08:00
|
|
|
|
mt.run_alone("127.0.0.1|8888", NULL,
|
2015-02-11 09:28:05 +08:00
|
|
|
|
task_count, threads_count);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// acl_master <20><><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
else
|
2015-02-11 09:28:05 +08:00
|
|
|
|
{
|
|
|
|
|
#ifdef WIN32
|
|
|
|
|
int task_count = 2, threads_count = 2;
|
2021-01-17 18:57:37 +08:00
|
|
|
|
format("listen: 127.0.0.1|8888\r\n");
|
2015-02-11 09:28:05 +08:00
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD>ʽ
|
2021-01-17 18:57:37 +08:00
|
|
|
|
mt.run_alone("127.0.0.1|8888", NULL,
|
2015-02-11 09:28:05 +08:00
|
|
|
|
task_count, threads_count);
|
|
|
|
|
#else
|
2014-11-19 00:25:21 +08:00
|
|
|
|
mt.run_daemon(argc, argv);
|
2015-02-11 09:28:05 +08:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-19 00:25:21 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|