1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
#include "log.hpp" #include "util.hpp" #include "master_threads.hpp" #include "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_threads_test : public acl::master_threads { public: master_threads_test() { }
~master_threads_test() { }
protected: 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.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; }
bool thread_on_accept(acl::socket_stream* stream) { 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; }
void thread_on_close(acl::socket_stream*) { format("client closed now\r\n"); }
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 }
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 }
void proc_pre_jail() { format("proc_pre_jail\r\n"); }
void proc_on_init() { format("proc init\r\n"); }
void proc_on_exit() { format("proc exit\r\n"); } };
int main(int argc, char* argv[]) { master_threads_test mt;
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);
if (argc >= 2 && strcmp(argv[1], "alone") == 0) { format = (void (*)(const char*, ...)) printf; format("listen: 127.0.0.1:8888\r\n"); mt.run_alone("127.0.0.1:8888", NULL, 2, 10); } else mt.run_daemon(argc, argv); return 0; }
|