acl/app/wizard_demo/pkv/master_service.cpp

122 lines
3.0 KiB
C++
Raw Normal View History

#include "stdafx.h"
2023-07-23 23:18:37 +08:00
#include "proto/redis_coder.h"
#include "action/redis_handler.h"
#include "master_service.h"
2023-07-23 23:18:37 +08:00
static char *var_cfg_dbpath;
2023-07-27 14:34:53 +08:00
static char *var_cfg_dbtype;
acl::master_str_tbl var_conf_str_tab[] = {
2023-07-27 14:34:53 +08:00
{ "dbpath", "./dbpath", &var_cfg_dbpath },
{ "dbtype", "rdb", &var_cfg_dbtype },
2023-07-27 14:34:53 +08:00
{ 0, 0, 0 }
};
2023-07-27 14:34:53 +08:00
int var_cfg_disable_serialize;
int var_cfg_disable_save;
acl::master_bool_tbl var_conf_bool_tab[] = {
2023-07-27 14:34:53 +08:00
{ "disable_serialize", 0, &var_cfg_disable_serialize },
{ "disable_save", 0, &var_cfg_disable_save },
2023-07-27 14:34:53 +08:00
{ 0, 0, 0 }
};
static int var_cfg_io_timeout;
static int var_cfg_buf_size;
acl::master_int_tbl var_conf_int_tab[] = {
2023-07-27 14:34:53 +08:00
{ "io_timeout", 120, &var_cfg_io_timeout, 0, 0 },
{ "buf_size", 8192, &var_cfg_buf_size, 0, 0 },
{ 0, 0 , 0 , 0, 0 }
};
acl::master_int64_tbl var_conf_int64_tab[] = {
2023-07-23 23:18:37 +08:00
{ 0, 0 , 0 , 0, 0 }
};
//////////////////////////////////////////////////////////////////////////
using namespace pkv;
2023-07-23 23:18:37 +08:00
void master_service::on_accept(acl::socket_stream& conn) {
//conn.set_rw_timeout(var_cfg_io_timeout);
logger(">>>accept connection: %d", conn.sock_handle());
run(conn, var_cfg_buf_size);
logger("Disconnect from peer, fd=%d", conn.sock_handle());
}
void master_service::run(acl::socket_stream& conn, size_t size) {
2023-07-23 23:18:37 +08:00
pkv::redis_coder parser;
pkv::redis_handler handler(db_, parser, conn);
char buf[size];
2023-07-23 23:18:37 +08:00
while(true) {
2023-07-24 18:42:07 +08:00
int ret = conn.read(buf, sizeof(buf) - 1, false);
2023-07-23 23:18:37 +08:00
if (ret <= 0) {
break;
}
2023-07-24 18:42:07 +08:00
2023-07-23 23:18:37 +08:00
buf[ret] = 0;
2023-07-24 18:42:07 +08:00
2023-07-23 23:18:37 +08:00
size_t len = (size_t) ret;
const char* data = parser.update(buf, len);
auto obj = parser.get_curr();
assert(obj);
if (obj->failed()) {
break;
}
2023-07-23 23:18:37 +08:00
assert(*data == '\0' && len == 0);
2023-07-23 23:18:37 +08:00
if (!handler.handle()) {
break;
2023-07-21 19:12:12 +08:00
}
2023-07-23 23:18:37 +08:00
parser.clear();
}
}
2023-07-23 23:18:37 +08:00
void master_service::proc_pre_jail() {
logger(">>>proc_pre_jail<<<");
}
2023-07-23 23:18:37 +08:00
void master_service::proc_on_listen(acl::server_socket& ss) {
logger(">>>listen %s ok<<<", ss.get_addr());
}
2023-07-23 23:18:37 +08:00
void master_service::proc_on_init() {
logger(">>>proc_on_init<<<");
2023-07-27 14:34:53 +08:00
if (strcasecmp(var_cfg_dbtype, "rdb") == 0) {
db_ = db::create_rdb();
if (!db_->open(var_cfg_dbpath)) {
logger_error("open db(%s) error %s", var_cfg_dbpath,
acl::last_serror());
exit(1);
}
} else if (strcasecmp(var_cfg_dbtype, "wdb") == 0) {
db_ = db::create_wdb();
if (!db_->open(var_cfg_dbpath)) {
logger_error("open db(%s) error", var_cfg_dbpath);
exit(1);
}
} else {
logger_error("unknown dbtype=%s", var_cfg_dbtype);
2023-07-23 23:18:37 +08:00
exit(1);
}
}
2023-07-23 23:18:37 +08:00
void master_service::proc_on_exit() {
logger(">>>proc_on_exit<<<");
}
2023-07-23 23:18:37 +08:00
bool master_service::proc_on_sighup(acl::string&) {
logger(">>>proc_on_sighup<<<");
return true;
}