diff --git a/Makefile b/Makefile index b7c28c5e05ee197b9b79ab5f02a71cc8048e3d01..ef36a56e93ad40692a1c6ae7c2431a10324b8731 100644 --- a/Makefile +++ b/Makefile @@ -1,46 +1,4 @@ -CURRENT_DIR := $(shell pwd) -export FAS_BASE := $(CURRENT_DIR)/base -export FAS_UTILS := $(CURRENT_DIR)/utils -export FAS_HTTP := $(CURRENT_DIR)/http -export FAS_TEST := $(CURRENT_DIR)/test -export FAS_LIBS_DIR := $(CURRENT_DIR)/lib -export FAS_TEST_BIN := $(CURRENT_DIR)/test_bin - -export STATIC_LIB_CMD := ar rcs -export SHARED_LIB_CMD := g++ -shared -fPIC -export CFLAG := -std=c++11 -Wall -static -export TEST_CFLAG := -std=c++11 -Wall -static -export CC := g++ - -export UTILS_HEADERS := $(wildcard $(FAS_UTILS)/*.h) -export BASE_HEADERS := $(wildcard $(FAS_BASE)/*.h) $(UTILS_HEADERS) -export HTTP_HEADERS := $(wildcard $(FAS_HTTP)/*.h) $(BASE_HEADERS) -export TEST_HEADERS := $(wildcard $(FAS_TEST)/*.h) $(HTTP_HEADERS) - -export UTILS_INCLUDE_DIR := -I$(FAS_UTILS) -export BASE_INCLUDE_DIR := -I$(FAS_BASE) -I$(FAS_UTILS) -export HTTP_INCLUDE_DIR := -I$(FAS_BASE) -I$(FAS_UTILS) -I$(FAS_HTTP) -export TEST_INCLUDE_DIR := $(HTTP_INCLUDE_DIR) - -export UTILS_LIBS := -lpthread -export BASE_LIBS := -llog4cplus -lpthread -lfasutils -ljsoncpp -export HTTP_LIBS := -lfas -llog4cplus -lpthread -lfasutils -export TEST_BIN_LIBS := -lfashttp -lfas -llog4cplus -lpthread -lfasutils -ljsoncpp - all: - mkdir -p $(FAS_LIBS_DIR) - mkdir -p $(FAS_TEST_BIN) - - make -C $(FAS_UTILS) - make -C $(FAS_BASE) - make -C $(FAS_HTTP) - make -C $(FAS_TEST) - -.PHONY : clean + scons clean: - make clean -C $(FAS_BASE) - make clean -C $(FAS_UTILS) - make clean -C $(FAS_HTTP) - make clean -C $(FAS_TEST) - rm $(FAS_LIBS) -rf - rm $(FAS_TEST_BIN) -rf + scons -c diff --git a/SConstruct b/SConstruct new file mode 100644 index 0000000000000000000000000000000000000000..12d3c93a6d24a4cc1967a959ffebb3183f2eac16 --- /dev/null +++ b/SConstruct @@ -0,0 +1,45 @@ +FAS_CFLAGS = [ + '-Wall', + '-static', + '-std=c++11' + ] + +StaticLibrary('./lib/fasutils', + Glob("utils/*.cpp"), + CPPPATH=['.', 'utils'], + CCFLAGS = FAS_CFLAGS + ) +StaticLibrary('./lib/fas', + Glob("base/*.cpp"), + LIBPATH = ['lib'], + CPPPATH = ['.', 'utils', 'base'], + LIBS = ['fasutils', 'glog', 'jsoncpp', 'pthread'], + CCFLAGS = FAS_CFLAGS + ) +StaticLibrary('./lib/fashttp', + Glob("http/*.cpp"), + LIBPATH = ['lib'], + CPPPATH = ['.', 'utils', 'base', 'http'], + LIBS = ['fasutils', 'fas', 'glog', 'jsoncpp', 'pthread'], + CCFLAGS = FAS_CFLAGS + ) +StaticLibrary('./lib/fasmdm', + Glob("mdm/*.cpp"), + LIBPATH = ['lib'], + CPPPATH = ['.', 'utils', 'base', 'mdm'], + LIBS = ['fasutils', 'fas', 'glog', 'jsoncpp', 'pthread'], + CCFLAGS = FAS_CFLAGS + ) + +Program('bin/TcpServer', 'test/TcpServerTest.cpp', + LIBPATH = ['lib'], + CPPPATH = ['.', 'utils', 'base'], + LIBS = ['fasutils', 'fas', 'glog', 'jsoncpp', 'pthread'], + CCFLAGS = ['-Wall', '-static', '-std=c++11'] + ) +Program('bin/MultiProcessTcpServer', 'test/MultiProcessServer.cpp', + LIBPATH = ['lib'], + CPPPATH = ['.', 'utils', 'base'], + LIBS = ['fasutils', 'fas', 'glog', 'jsoncpp', 'pthread'], + CCFLAGS = ['-Wall', '-static', '-std=c++11'] + ) diff --git a/base/Buffer.h b/base/Buffer.h index fd20dd9390d4a48f3e24d56413ee0baa02a06dbe..3fae581995f622f71d40f95a41f50eae515ab70c 100644 --- a/base/Buffer.h +++ b/base/Buffer.h @@ -4,11 +4,10 @@ #include #include #include - +#include #include - #include using std::string; diff --git a/base/Environment.cpp b/base/Environment.cpp index 79ae7763a839779b54976d094b6674e0b9b17e1e..864ce00c4a22116cda2073362accc87fbee32df2 100644 --- a/base/Environment.cpp +++ b/base/Environment.cpp @@ -5,6 +5,12 @@ #include fas::Environment* fas::Environment::env_ = nullptr; +fas::Environment::destroy_env fas::Environment::ds_env; +fas::Environment::~Environment() { + for (auto iter = reloaders_.begin(); iter != reloaders_.end(); ++iter) { + delete iter->second; + } +} fas::Environment* fas::Environment::instance() { if (env_ == nullptr) { @@ -17,9 +23,6 @@ bool fas::Environment::init() { if (!Environment::instance()) { return false; } - if (fas::LoggerInit() == false) { - return false; - } FasInfoLoader *loader = new (std::nothrow) FasInfoLoader("./conf/fas.conf"); if (!env_->insertReloader("fasinfo", loader)) { LOGGER_ERROR("Environment insert FasInfoLoader error."); @@ -110,3 +113,13 @@ fas::Reloader* fas::Environment::getReloader(const std::string& info) { } return iter->second; } + +fas::Environment* GET_ENV() { + return fas::Environment::instance(); +} +bool ENV_INIT() { + return GET_ENV()->init(); +} +void ENV_DESTROY() { + delete GET_ENV(); +} diff --git a/base/Environment.h b/base/Environment.h index 391ba8bc38b840c3f21ccd6f60c22901deb8ed5c..6bb4227b3c20cc34cd6dc085565e81501c0da102 100644 --- a/base/Environment.h +++ b/base/Environment.h @@ -6,20 +6,27 @@ #include #include -#define GET_ENV() (fas::Environment::instance()) -#define ENV_INIT() (GET_ENV()->init()) - namespace fas { class Environment { public: static Environment* instance(); + ~Environment(); static bool init(); bool check_load(); bool insertReloader(const std::string& info, Reloader *loader); Reloader* getReloader(const std::string& info) const; Reloader* getReloader(const std::string& info); private: + class destroy_env { + public: + ~destroy_env() { + if (Environment::env_) { + delete Environment::env_; + } + } + }; + bool load_files(); Environment() = default; static Environment *env_; @@ -27,8 +34,12 @@ private: Environment& operator=(const Environment&); std::map reloaders_; + static destroy_env ds_env; }; } +fas::Environment* GET_ENV(); +bool ENV_INIT(); +void ENV_DESTROY(); #endif // FAS_ENVIRONMENT_H diff --git a/base/Epoll.cpp b/base/Epoll.cpp index 1c870169bb18bed1fe3edfc958a67a8b6f8fc699..e7207f35cc1f17c6f16602990719e90051240b9d 100644 --- a/base/Epoll.cpp +++ b/base/Epoll.cpp @@ -26,7 +26,7 @@ bool fas::Epoll::eventCtl(int op, int sd, EpollEvent* event) { assert(epoll_fd_ != -1); int ret = ::epoll_ctl(epoll_fd_, op, sd, event); if (ret == -1) { - LOGGER_SYSERR("epoll_ctl error : " << ::strerror(errno)); + LOGGER_SYSERR("epoll_ctl error : " << ::strerror(errno) << " fd : " << sd); return false; } else { return true; diff --git a/base/EventLoop.cpp b/base/EventLoop.cpp index 9507476950ed5b922828891e00c817a23fc9d672..67bb7dc74f9a279d18f6aa11643737a37b39b9be 100644 --- a/base/EventLoop.cpp +++ b/base/EventLoop.cpp @@ -53,7 +53,8 @@ fas::EventLoop::EventLoop() : int time_out = GET_FAS_INFO()->getPollerTimeout(); pollDelayTime_ = time_out > 0 ? time_out : pollDelayTime_; wakeUpHandle_->setHandleRead(boost::bind(&EventLoop::handWakeUp, this, _1, _2)); - addHandle(wakeUpHandle_); + addSHandle(wakeUpHandle_); + LOGGER_TRACE("wakeup fd = " << wakeUpFd_); } int fas::EventLoop::getTid() const{ @@ -189,6 +190,10 @@ void fas::EventLoop::handWakeUp(Events event, Timestamp time) { } } +void fas::EventLoop::resetOwnerTid() { + tid_ = gettid(); +} + int fas::createEventfd() { int evtfd = ::eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); if (evtfd < 0) { @@ -246,7 +251,6 @@ void fas::EventLoop::quit() { bool fas::EventLoop::loop() { assertInOwnerThread(); Timestamp looptime; - while (!quit_) { if (!updates_.empty()) { updateHandles(); @@ -267,21 +271,16 @@ bool fas::EventLoop::loop() { LOGGER_DEBUG("handle'state != STATE_LOOP!"); continue; } - //handle revents handle->handleEvent(*iter, looptime); - } //for + } assert(!runningFunctors_); runFunctors(); - } //while + } return true; } -fas::EventLoop::~EventLoop() { - delete wakeUpHandle_; - wakeUpHandle_ = nullptr; - ::close(wakeUpFd_); - delete timerScheduler_; - timerScheduler_ = nullptr; +fas::EventLoop::~EventLoop() { + LOGGER_TRACE("EventLoop will be destroyed in process " << getpid()); } diff --git a/base/EventLoop.h b/base/EventLoop.h index f35f932dc89e40ab86abb704a50b2e2bffd033b1..274720f550fee50dd597cf9090349864fdf201e0 100644 --- a/base/EventLoop.h +++ b/base/EventLoop.h @@ -12,6 +12,7 @@ #include #include +#include #include #include @@ -138,6 +139,8 @@ public: * Polling events in this function. */ bool loop(); + + void resetOwnerTid(); private: bool updateHandle(SHandlePtr handle); bool updateHandles(); @@ -157,12 +160,12 @@ private: pid_t tid_; /*!< The pid of the Thread which own this EventLoop. */ int wakeUpFd_; /*!< eventfd */ - Handle *wakeUpHandle_; /*!< The handle of wakeUpFd_. */ + boost::shared_ptr wakeUpHandle_; /*!< The handle of wakeUpFd_. */ std::vector functors_; bool runningFunctors_; /*!< Judge functor in functors_ was executing. */ - TimersScheduler *timerScheduler_; /*!< Timer's Scheduler */ + boost::scoped_ptr timerScheduler_; /*!< Timer's Scheduler */ bool quit_; }; diff --git a/base/EventLoopThreadPool.cpp b/base/EventLoopThreadPool.cpp index c25c508a1fa56709e99cef9b437bc209154d8459..297a3fa39ea04a7bbd3c212cd4bcb14d8543e900 100644 --- a/base/EventLoopThreadPool.cpp +++ b/base/EventLoopThreadPool.cpp @@ -58,6 +58,19 @@ bool fas::EventLoopThreadPool::start() { return started_; } +bool fas::EventLoopThreadPool::stop() { + if (started_) { + for (size_t idx = 0; idx < loops_.size(); ++idx) { + loops_[idx]->quit(); + } + for (auto iter = threads_.begin(); iter < threads_.end(); iter++) { + (*iter)->join(); + } + } + started_ = false; + return true; +} + fas::EventLoop *fas::EventLoopThreadPool::getNextEventLoop() { assertInOwnerThread(); fas::EventLoop* loop = baseloop_; @@ -74,6 +87,9 @@ fas::EventLoop *fas::EventLoopThreadPool::getNextEventLoop() { fas::EventLoopThreadPool::~EventLoopThreadPool() { if (started_) { + for (size_t idx = 0; idx < loops_.size(); ++idx) { + loops_[idx]->quit(); + } for (auto iter = threads_.begin(); iter < threads_.end(); iter++) { (*iter)->join(); } diff --git a/base/EventLoopThreadPool.h b/base/EventLoopThreadPool.h index 7b440e00c23accfcc96e7c3a06da937ca27e9990..5a3052ce6f4e41952154ad0b6b66b0a6713b3101 100644 --- a/base/EventLoopThreadPool.h +++ b/base/EventLoopThreadPool.h @@ -30,6 +30,7 @@ public: void assertInOwnerThread(); bool start(); + bool stop(); EventLoop *getNextEventLoop(); private: diff --git a/base/FasInfo.cpp b/base/FasInfo.cpp index feb139c6438edf3253fd80f09d8ec67b2050bea1..4fc061e7d4af4faaba6c8012d96e8a1a1e80bd8f 100644 --- a/base/FasInfo.cpp +++ b/base/FasInfo.cpp @@ -42,7 +42,6 @@ int fas::FasInfo::load(const std::string& filename) { fasInfo_["thread_num"] = root["fas"]["thread_num"].asString(); fasInfo_["server_ip"] = root["fas"]["server_ip"].asString(); fasInfo_["server_port"] = root["fas"]["server_port"].asString(); - fasInfo_["log_conf"] = root["fas"]["log_conf"].asString(); } else { LOGGER_ERROR(" can not find fas json root!"); } @@ -58,10 +57,6 @@ int fas::FasInfo::load(const std::string& filename) { return 0; } -std::string fas::FasInfo::getLogConf() const { - return getValue("fas", "log_conf"); -} - std::string fas::FasInfo::getServerIp() const { return getValue("fas", "server_ip"); } @@ -82,10 +77,6 @@ int fas::FasInfo::getThreadNum() const { return boost::lexical_cast(num); } -std::string fas::FasInfo::getPollerType() const { - return getValue("poller", "type"); -} - int fas::FasInfo::getPollerTimeout() const { std::string time_out = getValue("poller", "time_out"); if (time_out.empty()) { @@ -102,6 +93,10 @@ int fas::FasInfo::getPollerNum() const { return boost::lexical_cast(event_num); } +std::string fas::FasInfo::getPollerType() const { + return getValue("poller", "type"); +} + std::string fas::FasInfo::getValue(const std::string& root, const std::string& key) const { std::string value = ""; @@ -124,3 +119,4 @@ fas::FasInfo* GET_FAS_INFO() { } return info->get_content(); } + diff --git a/base/FasInfo.h b/base/FasInfo.h index 057ad464bfedf3f39801c0627a3f8bfde2073de3..3de471e58fb59fe7cc480a1bf38baacc33c4b6b9 100644 --- a/base/FasInfo.h +++ b/base/FasInfo.h @@ -12,7 +12,6 @@ public: FasInfo(); int load(const std::string& filename); std::string getValue(const std::string& root, const std::string& key) const; - std::string getLogConf() const; std::string getPollerType() const; int getPollerTimeout() const; int getPollerNum() const; diff --git a/base/Log.cpp b/base/Log.cpp index bd08e930d86124538785898066b3689e264b7a96..5649805f11ba9b16c469d850288d9c7c60fe0089 100644 --- a/base/Log.cpp +++ b/base/Log.cpp @@ -1,17 +1,12 @@ #include +#include +#include +#include #include -#include fas::CommonLog* fas::CommonLog::logger_ = nullptr; -bool fas::LoggerInit() { - std::vector logNames({"TRACE", "DEBUG", \ - "INFO", "ERROR", \ - "WARN", "SYS_ERROR", "FETAL"}); - return CommonLog::instance()->init("./conf/log.conf", logNames) == 0 ? true : false; -} - fas::CommonLog* fas::CommonLog::instance() { if (NULL == logger_) { logger_ = new(std::nothrow) CommonLog(); @@ -19,4 +14,31 @@ fas::CommonLog* fas::CommonLog::instance() { return logger_; } +bool fas::InitGoogleLog(const std::string& log_dir) { + google::InitGoogleLogging(""); + google::SetStderrLogging(google::GLOG_INFO); //设置级别高于 google::INFO 的日志同时输出到屏幕 +// google::SetStderrLogging(google::GLOG_FATAL);//设置级别高于 google::FATAL 的日志同时输出到屏幕 + FLAGS_colorlogtostderr = true; //设置输出到屏幕的日志显示相应颜色 + //FLAGS_servitysinglelog = true;// 用来按照等级区分log文件 + if (::access(log_dir.c_str(), F_OK|W_OK|R_OK)) { + std::cerr << "fas has no permission of log dir "<< log_dir << std::endl; + return false; + } + pid_t pid = getpid(); + google::SetLogDestination(google::GLOG_FATAL, (log_dir + "/pid_" + std::to_string(pid) + "_log_fatal_").c_str()); // 设置 google::FATAL 级别的日志存储路径和文件名前缀 + google::SetLogDestination(google::GLOG_ERROR, (log_dir + "/pid_" + std::to_string(pid) + "_log_error_").c_str()); //设置 google::ERROR 级别的日志存储路径和文件名前缀 + google::SetLogDestination(google::GLOG_WARNING, (log_dir + "/pid_" + std::to_string(pid) + "_log_warning_").c_str()); //设置 google::WARNING 级别的日志存储路径和文件名前缀 + google::SetLogDestination(google::GLOG_INFO, (log_dir + "/pid_" + std::to_string(pid) + "_log_info_").c_str()); //设置 google::INFO 级别的日志存储路径和文件名前缀 + FLAGS_logbufsecs = 0; //缓冲日志输出,默认为30秒,此处改为立即输出 + FLAGS_max_log_size = 100; //最大日志大小为 100MB + FLAGS_stop_logging_if_full_disk = true; //当磁盘被写满时,停止日志输出 + google::SetLogFilenameExtension(".log"); //设置文件名扩展,如平台?或其它需要区分的信息 + //google::InstallFailureSignalHandler(); //捕捉 core dumped (linux) + //google::InstallFailureWriter(&Log); //默认捕捉 SIGSEGV 信号信息输出会输出到 stderr (linux) + return true; +} +void fas::CloseGoogleLog() { + google::ShutdownGoogleLogging(); +} +// google::ShutdownGoogleLogging(); diff --git a/base/Log.cpp.bak b/base/Log.cpp.bak new file mode 100644 index 0000000000000000000000000000000000000000..ebae01888613749d3580165345b9202b67afcdbc --- /dev/null +++ b/base/Log.cpp.bak @@ -0,0 +1,21 @@ +#include + +#include + +fas::CommonLog* fas::CommonLog::logger_ = nullptr; + +bool fas::LoggerInit() { + std::vector logNames({"TRACE", "DEBUG", \ + "INFO", "ERROR", \ + "WARN", "SYS_ERROR", "FETAL"}); + return CommonLog::instance()->init("./conf/log.conf", logNames) == 0 ? true : false; +} + +fas::CommonLog* fas::CommonLog::instance() { + if (NULL == logger_) { + logger_ = new(std::nothrow) CommonLog(); + } + return logger_; +} + + diff --git a/base/Log.h b/base/Log.h index f738546d30312ae70dfc5857c824142e9f61d0cf..8e761db4527a63e48d6181655e359e6f31addeab 100644 --- a/base/Log.h +++ b/base/Log.h @@ -1,75 +1,37 @@ #ifndef FAS_LOG_H #define FAS_LOG_H -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include namespace fas { - +#define LOGGER_TRACE(MSG) (LOG(INFO) << MSG) +#define LOGGER_DEBUG(MSG) (LOG(INFO) << MSG) +#define LOGGER_INFO(MSG) (LOG(INFO) << MSG) +#define LOGGER_WARN(MSG) (LOG(WARNING) << MSG) +#define LOGGER_ERROR(MSG) (LOG(ERROR) << MSG) +#define LOGGER_FATAL(MSG) (LOG(FATAL) << MSG) +#define LOGGER_SYSERR(MSG) (LOG(ERROR) << MSG) static const std::string CRLF = "\r\n"; - -class CommonLog -{ -public: - log4cplus::Logger* trace_; - log4cplus::Logger* debug_; - log4cplus::Logger* info_; - log4cplus::Logger* warn_; - log4cplus::Logger* error_; - log4cplus::Logger* sys_error_; - log4cplus::Logger* fetal_; - +bool InitGoogleLog(const std::string& log_dir); +void CloseGoogleLog(); +class CommonLog { public: - int init(const std::string& name, std::vector& log_name) { - if (name.empty()) { - return -1; - } - - log4cplus::initialize(); - log4cplus::helpers::LogLog::getLogLog()->setInternalDebugging(true); - log4cplus::PropertyConfigurator::doConfigure(name); - -#define CREATE_LOG_OBJECT(KEY, POINT) \ - if (log_name.end() != std::find(log_name.begin(), log_name.end(), #KEY)) { \ - log4cplus::Logger logger = log4cplus::Logger::getInstance(#KEY);\ - POINT = new log4cplus::Logger(logger); \ - } - - CREATE_LOG_OBJECT(TRACE, trace_) - CREATE_LOG_OBJECT(DEBUG, debug_) - CREATE_LOG_OBJECT(INFO, info_) - CREATE_LOG_OBJECT(WARN, warn_) - CREATE_LOG_OBJECT(ERROR, error_) - CREATE_LOG_OBJECT(SYS_ERROR, sys_error_) - CREATE_LOG_OBJECT(FETAL, fetal_) - return 0; + int init(const std::string& log_dir, std::vector& log_name) { + InitGoogleLog(log_dir); + return 0; } - static CommonLog* instance(); + static CommonLog *instance(); + void closeLog() { + google::ShutdownGoogleLogging(); + } + ~CommonLog() { + google::ShutdownGoogleLogging(); + } private: - CommonLog() {} - - static CommonLog* logger_; + static CommonLog *logger_; }; -bool LoggerInit(); - } - -#define LOGGER_TRACE(MSG) LOG4CPLUS_TRACE(*fas::CommonLog::instance()->trace_, MSG) -#define LOGGER_DEBUG(MSG) LOG4CPLUS_DEBUG(*fas::CommonLog::instance()->debug_, MSG) -#define LOGGER_INFO(MSG) LOG4CPLUS_INFO(*fas::CommonLog::instance()->info_, MSG) -#define LOGGER_WARN(MSG) LOG4CPLUS_WARN(*fas::CommonLog::instance()->warn_, MSG) -#define LOGGER_ERROR(MSG) LOG4CPLUS_ERROR(*fas::CommonLog::instance()->error_, MSG) -#define LOGGER_FATAL(MSG) LOG4CPLUS_FATAL(*fas::CommonLog::instance()->fetal_, MSG) -#define LOGGER_SYSERR(MSG) LOG4CPLUS_ERROR(*fas::CommonLog::instance()->error_, MSG) - #endif // FAS_LOG_H diff --git a/base/Log.h.bak b/base/Log.h.bak new file mode 100644 index 0000000000000000000000000000000000000000..20aa1696397df9ee07f4326489a0ff529a063d50 --- /dev/null +++ b/base/Log.h.bak @@ -0,0 +1,75 @@ +#ifndef FAS_LOG_H +#define FAS_LOG_H +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace fas { + +#define LOGGER_TRACE(MSG) LOG4CPLUS_TRACE(*CommonLog::instance()->trace_, MSG) +#define LOGGER_DEBUG(MSG) LOG4CPLUS_DEBUG(*CommonLog::instance()->debug_, MSG) +#define LOGGER_INFO(MSG) LOG4CPLUS_INFO(*CommonLog::instance()->info_, MSG) +#define LOGGER_WARN(MSG) LOG4CPLUS_WARN(*CommonLog::instance()->warn_, MSG) +#define LOGGER_ERROR(MSG) LOG4CPLUS_ERROR(*CommonLog::instance()->error_, MSG) +#define LOGGER_FATAL(MSG) LOG4CPLUS_FATAL(*CommonLog::instance()->fetal_, MSG) +#define LOGGER_SYSERR(MSG) LOG4CPLUS_ERROR(*CommonLog::instance()->error_, MSG) + +static const std::string CRLF = "\r\n"; + +class CommonLog +{ +public: + log4cplus::Logger* trace_; + log4cplus::Logger* debug_; + log4cplus::Logger* info_; + log4cplus::Logger* warn_; + log4cplus::Logger* error_; + log4cplus::Logger* sys_error_; + log4cplus::Logger* fetal_; + +public: + int init(const std::string& name, std::vector& log_name) { + if (name.empty()) { + return -1; + } + + log4cplus::initialize(); + log4cplus::helpers::LogLog::getLogLog()->setInternalDebugging(true); + log4cplus::PropertyConfigurator::doConfigure(name); + +#define CREATE_LOG_OBJECT(KEY, POINT) \ + if (log_name.end() != std::find(log_name.begin(), log_name.end(), #KEY)) { \ + log4cplus::Logger logger = log4cplus::Logger::getInstance(#KEY);\ + POINT = new log4cplus::Logger(logger); \ + } + + CREATE_LOG_OBJECT(TRACE, trace_) + CREATE_LOG_OBJECT(DEBUG, debug_) + CREATE_LOG_OBJECT(INFO, info_) + CREATE_LOG_OBJECT(WARN, warn_) + CREATE_LOG_OBJECT(ERROR, error_) + CREATE_LOG_OBJECT(SYS_ERROR, sys_error_) + CREATE_LOG_OBJECT(FETAL, fetal_) + return 0; + } + + static CommonLog* instance(); + +private: + CommonLog() {} + + static CommonLog* logger_; +}; + +bool LoggerInit(); + +} + +#endif // FAS_LOG_H + diff --git a/base/Makefile b/base/Makefile deleted file mode 100644 index fa451763d84bb499b92736428e66e6ee3ed394c6..0000000000000000000000000000000000000000 --- a/base/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -FAS_STATIC_LIB := $(FAS_LIBS_DIR)/libfas.a -FAS_SHARED_LIB := $(FAS_LIBS_DIR)/libfas.so - -OBJECTS := $(patsubst %.cpp, %.o, $(wildcard *.cpp)) - -all:$(OBJECTS) - $(SHARED_LIB_CMD) $^ -L$(FAS_LIBS_DIR) $(THIRD_LIB_DIR) -o $(FAS_SHARED_LIB) - $(STATIC_LIB_CMD) $(FAS_STATIC_LIB) $^ - -%.o:%.cpp $(BASE_HEADERS) - $(CC) -c $< ${BASE_INCLUDE_DIR} $(CFLAG) -o $@ $(BASE_LIBS) - -.PHONY : clean -clean: - rm *.o -rf diff --git a/base/MultiProcessTcpServer.cpp b/base/MultiProcessTcpServer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..071cd69bfc01a595223db984bbcd15b94d4363f3 --- /dev/null +++ b/base/MultiProcessTcpServer.cpp @@ -0,0 +1,217 @@ +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +fas::MultiProcessTcpServer *fas::MultiProcessTcpServer::instance = nullptr; +fas::MultiProcessTcpServer::destroy_multiprocess fas::MultiProcessTcpServer::ds_mulp; +fas::MultiProcessTcpServer::MultiProcessTcpServer(): + pipes_(), + process_(), + pids_(), + threadNum_(0), + loop_(nullptr), + server_(nullptr), + quit_(false), + waiting_(false) { + sigemptyset(&maskset_); + sigemptyset(&maskold_); + sigemptyset(&waitset_); + } + +fas::MultiProcessTcpServer *fas::MultiProcessTcpServer::Instance() { + if (nullptr == instance) { + instance = new (std::nothrow) MultiProcessTcpServer; + } + return instance; +} + +void fas::MultiProcessTcpServer::signalHandler(int signo) { + if (SIGINT == signo) { + std::string cmd = "[quit]"; + for (size_t i = 0; i < instance->pids_.size(); i++) { + write(instance->pipes_[i]->getWriteEnd(), cmd.c_str(), cmd.size()); + } + LOGGER_TRACE("Recive a SIGQUIT, will be exit."); + instance->quit_ = true; + } else if (SIGQUIT == signo) { + std::string cmd = "[quit]"; + for (size_t i = 0; i < instance->pids_.size(); i++) { + write(instance->pipes_[i]->getWriteEnd(), cmd.c_str(), cmd.size()); + } + LOGGER_TRACE("Recive a SIGQUIT, will be exit."); + } else if (SIGUSR1 == signo) { + //do some things. + } else if (SIGUSR2 == signo) { + //do some things. + } else if (SIGCHLD == signo) { + LOGGER_TRACE("Maybe a multiPrecess server child exit."); + int status = 0; + if (-1 == waitpid(-1, &status, WNOHANG | WCONTINUED)) { + LOGGER_ERROR("wait child error."); + return; + } + + if (WIFEXITED(status)) { + LOGGER_TRACE("Child terminated normally."); + } + if (WIFSIGNALED(status)) { + LOGGER_TRACE("The child process was terminated by a signal:" << WTERMSIG(status)); + if (WCOREDUMP(status)) { + LOGGER_TRACE("The child terminated produced a core dump"); + } + } + } +} + +bool fas::MultiProcessTcpServer::reloadInfo() { + if (!ENV_INIT()) { + std::cout << "Environment init error!" << std::endl; + return false; + } else { + LOGGER_TRACE("Environment init success!"); + return true; + } +} + +void fas::MultiProcessTcpServer::setConnMessageCallback(fas::TcpServer::TcpConnMessageCallback callback) { + messageCb_ = callback; +} +void fas::MultiProcessTcpServer::setConnBuildCallback(fas::TcpServer::OnConnectionCallBack callback) { + connBuildCb_ = callback; +} +void fas::MultiProcessTcpServer::setConnRemoveCallback(fas::TcpServer::OnConnectionRemovedCallBack callback) { + connRemoveCb_ = callback; +} + +#define ProcessNum 2 + +bool fas::MultiProcessTcpServer::start() { + InitGoogleLog("./faslog"); + //block some signal + sigaddset(&maskset_, SIGPIPE); + sigaddset(&maskset_, SIGALRM); + sigaddset(&maskset_, SIGCONT); + sigaddset(&maskset_, SIGSTOP); + if (-1 == sigprocmask(SIG_BLOCK, &maskset_, &maskold_)) { + std::cout << "mask signal set (SIGPIPE, SIGALRM, SIGCONT, SIGSTOP) error. in MultiProcessTcpServer" << std::endl; + return false; + } + //munipulate some signal + signal(SIGCHLD, &MultiProcessTcpServer::signalHandler); + signal(SIGQUIT, &MultiProcessTcpServer::signalHandler); + signal(SIGUSR1, &MultiProcessTcpServer::signalHandler); + signal(SIGUSR2, &MultiProcessTcpServer::signalHandler); + signal(SIGINT, &MultiProcessTcpServer::signalHandler); + signal(SIGSTOP, &MultiProcessTcpServer::signalHandler); + + while (!quit_) { + if (!reloadInfo()) { + return false; + } + LOGGER_TRACE("Reload info succeed, Multi Server begin to start."); + + short port = GET_FAS_INFO()->getServerPort(); + if (port < 0) { + LOGGER_ERROR("get server Port error!"); + port = 6686; + } + std::string ip = GET_FAS_INFO()->getServerIp(); + if (ip.empty()) { + LOGGER_ERROR("get server ip error!"); + ip = "127.0.0.1"; + } + int thread_num = GET_FAS_INFO()->getThreadNum(); + if (thread_num < 0) { + LOGGER_ERROR("get server thread_num error!"); + thread_num = 4; + } + //由于epollfd是全局唯一的,最好在子进程里面创建,不然多个子进程可能会共享一个。 + server_ = boost::shared_ptr(new TcpServer(nullptr, NetAddress(AF_INET, port, ip.c_str()), thread_num)); + if (!server_) { + LOGGER_ERROR("New TcpServer error in MultiProcessTcpServer."); + return false; + } + + for (int idx = 0; idx < ProcessNum; idx++) { + auto pipe = boost::shared_ptr(new PipeFd()); + if (!pipe) { + LOGGER_ERROR("New pipe fd array error."); + return false; + } + if (-1 == ::pipe(pipe->End)) { + LOGGER_ERROR("Pipe error in MultiProcessTcpServer : " << ::strerror(errno)); + return false; + } + pipes_.push_back(pipe); + LOGGER_TRACE("idx = " << idx << " read = " << pipes_[idx]->getReadEnd() << " write = " << pipes_[idx]->getWriteEnd()); + } + for (int idx = 0; idx < ProcessNum; ++idx) { + auto proce = boost::shared_ptr(new (std::nothrow) ProcessTcpServer(server_, pipes_[idx])); + if (!proce) { + LOGGER_ERROR("New ProcessTcpServer in MultiProcessTcpServer."); + return false; + } + process_.push_back(proce); + } + + for (size_t idx = 0; idx < process_.size(); ++idx) { + //再循环中创建进程,要保证在子进程使用idx的时候,idx的值未变。 + pid_t pid = fork(); + if (pid == 0) { + if (this->messageCb_) { + process_[idx]->setConnMessageCallback(this->messageCb_); + } else { + LOGGER_WARN("TcpServer message callback wasn't set."); + } + if (this->connBuildCb_) { + process_[idx]->setConnBuildCallback(this->connBuildCb_); + } else { + LOGGER_WARN("TcpServer conn build callback wasn't set."); + } + if (this->connRemoveCb_) { + process_[idx]->setConnRemoveCallback(this->connRemoveCb_); + } else { + LOGGER_WARN("TcpServer conn removed callback wasn't set."); + } + process_[idx]->initProc("./faslog"); + process_[idx]->startLoop(); + LOGGER_TRACE("a child quit. idx = " << idx); + exit(EXIT_SUCCESS); + } else if (pid < 0) { + LOGGER_ERROR("Fork child error in MultiProcessTcpServer."); + } + //master进程记录所有子进程的pid + pids_.push_back(pid); + //关闭管道的读端,master主要用来通知子进程,后期可能会添加上读,让父子进程交互。 + pipes_[idx]->closeReadEnd(); + } + + waiting_ = true; + while (waiting_ && (!quit_)) { + sleep(1); + } + waiting_ = false; + } + + return true; +} + +fas::MultiProcessTcpServer::~MultiProcessTcpServer() { + LOGGER_TRACE("MultiProcessTcpServer will be destroyed."); + quit_ = true; + if (loop_) { + delete loop_; + } +} diff --git a/base/MultiProcessTcpServer.h b/base/MultiProcessTcpServer.h new file mode 100644 index 0000000000000000000000000000000000000000..0e5161efc7f7123a0f1c10a8837bbbf9962d0998 --- /dev/null +++ b/base/MultiProcessTcpServer.h @@ -0,0 +1,61 @@ +#ifndef FAS_MULTIPROCESSTCPSERVER_H +#define FAS_MULTIPROCESSTCPSERVER_H +#include + +#include +#include +#include +#include + +#include +#include + +namespace fas { + +class MultiProcessTcpServer : boost::noncopyable { +public: + ~MultiProcessTcpServer(); + static MultiProcessTcpServer *Instance(); + + static void signalHandler(int signo); + + bool reloadInfo(); + + void setConnMessageCallback(TcpServer::TcpConnMessageCallback callback); + void setConnBuildCallback(TcpServer::OnConnectionCallBack callback); + void setConnRemoveCallback(TcpServer::OnConnectionRemovedCallBack callback); + bool start(); +private: + class destroy_multiprocess { + public: + ~destroy_multiprocess() { + if (MultiProcessTcpServer::instance) { + delete MultiProcessTcpServer::instance; + } + } + }; + + MultiProcessTcpServer(); + std::vector> pipes_; + std::vector> process_; + std::vector pids_; + int threadNum_; + EventLoop *loop_; //do not used by serevr_ + boost::shared_ptr server_; + bool quit_; + bool waiting_; + sigset_t maskset_; + sigset_t maskold_; + sigset_t waitset_; + + TcpServer::TcpConnMessageCallback messageCb_; + TcpServer::OnConnectionCallBack connBuildCb_; + TcpServer::OnConnectionRemovedCallBack connRemoveCb_; + + static MultiProcessTcpServer *instance; + static destroy_multiprocess ds_mulp; //要在其他地方定义 +}; + +} + +#endif diff --git a/base/PipeFd.h b/base/PipeFd.h new file mode 100644 index 0000000000000000000000000000000000000000..6791d288fbb3e248ecfc544c293aaf31b83dcbca --- /dev/null +++ b/base/PipeFd.h @@ -0,0 +1,28 @@ +#ifndef FAS_PIPEFD_H +#define FAS_PIPEFD_H +#include + +namespace fas { + +struct PipeFd { + int End[2]; + int getWriteEnd() const { + return End[1]; + } + int getReadEnd() const { + return End[0]; + } + void closeWriteEnd() const { + ::close(End[1]); + } + void closeReadEnd() const { + ::close(End[0]); + } + ~PipeFd() { + ::close(End[1]); + ::close(End[0]); + } +}; + +} +#endif diff --git a/base/ProcessTcpServer.cpp b/base/ProcessTcpServer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8b0bfb97835aafb5eb7d5cde28eeed162687df83 --- /dev/null +++ b/base/ProcessTcpServer.cpp @@ -0,0 +1,146 @@ +#include + +#include +#include +#include +#include + +#include + +fas::ProcessTcpServer::ProcessTcpServer(boost::shared_ptr server, boost::shared_ptr pipe) : + server_(server), + pipe_(pipe), + loop_(nullptr), + readBuffer_(1024), + readEndHandle_(nullptr), + reloaderTimer_(nullptr) { +} + +fas::ProcessTcpServer::~ProcessTcpServer() { + //不要在析构函数中做太多工作? + //因为这个ProcessTcpServer是在父进程中创建的,所以在父进程中也会调用这个析构。 + //如果在这里面直接释放父进程中未构造完成的对象,就会出现段错误,要做有效行判断。 + if (loop_) { + delete loop_; + loop_ = nullptr; + } + LOGGER_TRACE("ProcessTcpServer wil be destroyed in process " << getpid()); +} + +bool fas::ProcessTcpServer::initProc(const std::string& logpf) { + CloseGoogleLog(); + //每个进程进行日志的重新初始化,将日志打印到含有自己pid的日志文件中。 + InitGoogleLog(logpf); + loop_ = new (std::nothrow) EventLoop; + if (!loop_) { + LOGGER_ERROR("New EventLoop error in MultiProcessTcpServer child process."); + return false; + } + if (pipe_) { + pipe_->closeWriteEnd(); + } + //本进程的server在启动之时,必须使用本进程的EventLoop. + if (server_) { + if (nullptr != server_->getLoop()) { + LOGGER_WARN("TcpServer's loop should be set in ProcessTcpServer, EventLoop is reset in ProcessTcpServer. usage:TcpServer(nullptr, address, threadNum)"); + } + server_->resetLoop(loop_); + } else { + LOGGER_ERROR("ProcessTcpServer's TcpServer is nullptr."); + return false; + } + if (messageCb_) { + server_->setMessageCallback(messageCb_); + } + if (this->connBuildCb_) { + server_->setOnConnectionCallBack(this->connBuildCb_); + } + if (this->connRemoveCb_) { + server_->setOnConnRemovedCallBack(connRemoveCb_); + } + + if ((pipe_) && !initReadEndHandle()) { + LOGGER_ERROR("Init readEnd handle error. readEnd = " << pipe_->getReadEnd()); + return false; + } + + if (!reloaderTimer_) { + reloaderTimer_ = new Timer(boost::bind(&ProcessTcpServer::reloaderTimerfunc, this), addTime(Timestamp::now(), 20), 20); + } + + return true; +} + +bool fas::ProcessTcpServer::startLoop() { + if (!loop_) { + LOGGER_ERROR("ProcessTcpServer's loop_ is nullptr."); + return false; + } + server_->start(); + loop_->addHandle(readEndHandle_); + loop_->addTimer(this->reloaderTimer_); + loop_->loop(); + + //不要在析构函数中做这步。 + CloseGoogleLog(); + return true; +} + +void fas::ProcessTcpServer::reloaderTimerfunc() { + if (GET_ENV()->check_load()) { + LOGGER_ERROR("Reloader success in process " << getpid()); + } else { + LOGGER_ERROR("Reloader failed in process " << getpid()); + } +} + +bool fas::ProcessTcpServer::initReadEndHandle() { + readEndHandle_ = new (std::nothrow) Handle(loop_, fas::Events(pipe_->getReadEnd(), kReadEvent)); + if (!readEndHandle_) { + return false; + } + readEndHandle_->setHandleRead(boost::bind(&ProcessTcpServer::handleRead, this, _1, _2)); + readEndHandle_->setHandleWrite(boost::bind(&ProcessTcpServer::handleWrite, this, _1, _2)); + readEndHandle_->setHandleError(boost::bind(&ProcessTcpServer::handleError, this, _1, _2)); + readEndHandle_->setHandleClose(boost::bind(&ProcessTcpServer::handleClose, this, _1, _2)); + return true; +} +//在各个事件处理函数中添加判断,处理事件。 +void fas::ProcessTcpServer::handleRead(const fas::Events& event, Timestamp time) { + if (event.getFd() == pipe_->getReadEnd()) { + int error = 0; + if (0 > readBuffer_.readFd(event.getFd(), &error)) { + LOGGER_ERROR("Read pipe read end error : " << ::strerror(error)); + return; + } + const char *cmddelimitend = readBuffer_.findChars("]", 1); + if (NULL == cmddelimitend) { + // receive cmd is not complete. + return; + } + std::string cmd = readBuffer_.retrieveAsString(cmddelimitend + 1 - readBuffer_.peek()); + if (0 == strcmp("[quit]", cmd.c_str())) { + LOGGER_TRACE("Receive quit cmd, process server will be exit."); + loop_->quit(); + } + } +} +void fas::ProcessTcpServer::handleWrite(const fas::Events& event, Timestamp time) { + +} +void fas::ProcessTcpServer::handleError(const fas::Events& event, Timestamp time) { + LOGGER_ERROR("Handle error."); +} +void fas::ProcessTcpServer::handleClose(const fas::Events& event, Timestamp time) { + loop_->quit(); +} +void fas::ProcessTcpServer::setConnMessageCallback(fas::TcpServer::TcpConnMessageCallback callback) { + messageCb_ = callback; +} +void fas::ProcessTcpServer::setConnBuildCallback(fas::TcpServer::OnConnectionCallBack callback) { + connBuildCb_ = callback; +} +void fas::ProcessTcpServer::setConnRemoveCallback(fas::TcpServer::OnConnectionRemovedCallBack callback) { + connRemoveCb_ = callback; +} + diff --git a/base/ProcessTcpServer.h b/base/ProcessTcpServer.h new file mode 100644 index 0000000000000000000000000000000000000000..117aeb0c0fe1f5a89669216356379ead3c24147e --- /dev/null +++ b/base/ProcessTcpServer.h @@ -0,0 +1,51 @@ +#ifndef FAS_PROCESSTCPSERVER_H +#define FAS_PROCESSTCPSERVER_H +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace fas { + +class ProcessTcpServer : boost::noncopyable { +public: + ProcessTcpServer(boost::shared_ptr server, boost::shared_ptr pipe); + ~ProcessTcpServer(); + + bool startLoop(); + bool initProc(const std::string& info); + + bool initReadEndHandle(); + void handleRead(const fas::Events& event, Timestamp time); + void handleWrite(const fas::Events& event, Timestamp time); + void handleError(const fas::Events& event, Timestamp time); + void handleClose(const fas::Events& event, Timestamp time); + + void reloaderTimerfunc(); + + void setConnMessageCallback(TcpServer::TcpConnMessageCallback callback); + void setConnBuildCallback(TcpServer::OnConnectionCallBack callback); + void setConnRemoveCallback(TcpServer::OnConnectionRemovedCallBack callback); +private: + boost::shared_ptr server_; + boost::shared_ptr pipe_; + EventLoop* loop_; + int threadNum_; + Buffer readBuffer_; + Handle *readEndHandle_; + Timer *reloaderTimer_; + + TcpServer::TcpConnMessageCallback messageCb_; + TcpServer::OnConnectionCallBack connBuildCb_; + TcpServer::OnConnectionRemovedCallBack connRemoveCb_; +}; + +} + +#endif diff --git a/base/Reloader.h b/base/Reloader.h index 0ed4eae3c9e9f09bb2ff3acb1879031976828f8e..a7b39963924e2da92e6fd8a08fb802e773770d5e 100644 --- a/base/Reloader.h +++ b/base/Reloader.h @@ -6,6 +6,8 @@ namespace fas { class Reloader { public: + virtual ~Reloader() { + } virtual int need_reload() const = 0; virtual int reload() = 0; virtual int load() = 0; diff --git a/base/TcpConnection.cpp b/base/TcpConnection.cpp index 63759dcee6ce19458195950b29070557bd16c062..86af3f873853a49d1f5f05a4227ab257d05f45b8 100644 --- a/base/TcpConnection.cpp +++ b/base/TcpConnection.cpp @@ -133,9 +133,9 @@ void fas::TcpConnection::handleWrite(const fas::Events& revents, fas::Timestamp loop_->assertInOwnerThread(); int writeSd = connfd_.getSocket(); - int readablesizes = writeBuffer_->readableBytes(); + size_t readablesizes = writeBuffer_->readableBytes(); reWrite: - int ret = ::write(writeSd, writeBuffer_->peek(), readablesizes); + ssize_t ret = ::write(writeSd, writeBuffer_->peek(), readablesizes); if (ret < 0) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) { return; diff --git a/base/TcpServer.cpp b/base/TcpServer.cpp index 0f98c2be075435db298a5cfa7c6a1cc980a31b6c..72db28858d5a5d85d73c84e38f386d95d596dccf 100644 --- a/base/TcpServer.cpp +++ b/base/TcpServer.cpp @@ -26,14 +26,21 @@ fas::TcpServer::TcpServer(fas::EventLoop* loop, const NetAddress& addr, int thre conns_(), handleQueue_(nullptr), handleMap_(nullptr) { - assert(loop_ != NULL); + server_.setNoBlocking(); + server_.setExecClose(); + server_.bind(addr_); + server_.listen(listenBacklog_); + LOGGER_TRACE("server listen fd = " << server_.getSocket()); } fas::EventLoop* fas::TcpServer::getLoop() const{ - assert(loop_ != NULL); return loop_; } +void fas::TcpServer::resetLoop(fas::EventLoop *loop) { + loop_ = loop; +} + fas::TcpServer::TcpConnShreadPtr fas::TcpServer::getConn(fas::TcpServer::connkey_t key) const { return conns_.find(key)->second; } @@ -43,15 +50,15 @@ fas::TcpServer::TcpConnShreadPtr fas::TcpServer::getConn(fas::TcpServer::connkey } bool fas::TcpServer::start() { - server_.setNoBlocking(); - server_.setExecClose(); + if (!loop_) { + LOGGER_ERROR("TcpServer's loop is nullptr."); + return false; + } handle_ = new (std::nothrow) Handle(loop_, events_); if (!handle_) { LOGGER_ERROR("new server handle_ error!"); return false; } - server_.bind(addr_); - server_.listen(listenBacklog_); handle_->setHandleRead(boost::bind(&TcpServer::defHandleAccept, this, _1, _2)); loop_->addHandle(handle_); @@ -144,4 +151,6 @@ void fas::TcpServer::removeConnectionInLoop(fas::TcpServer::connkey_t key) { fas::TcpServer::~TcpServer() { delete handle_; + handle_ = nullptr; + LOGGER_TRACE("TcpServer will be destroyed in process " << getpid()); } diff --git a/base/TcpServer.h b/base/TcpServer.h index c07f51299d7e831b6a00fd3fc7af2826ecbf7419..709a3e29870804b0038b5a6ddf065a2b301226d9 100644 --- a/base/TcpServer.h +++ b/base/TcpServer.h @@ -42,6 +42,8 @@ public: EventLoop* getLoop() const; + void resetLoop(fas::EventLoop *loop); + TcpConnShreadPtr getConn(connkey_t key) const; TcpConnShreadPtr getConn(connkey_t key); diff --git a/base/TimersScheduler.cpp b/base/TimersScheduler.cpp index 0686d69db10d0461a141ccc16c6bbe996393cefa..c621dd5d936c56bae95c0400fc1beced53cab3a8 100644 --- a/base/TimersScheduler.cpp +++ b/base/TimersScheduler.cpp @@ -27,6 +27,7 @@ fas::TimersScheduler::TimersScheduler(fas::EventLoop *loop) : LOGGER_SYSERR("timerfd_create error : " << ::strerror(errno)); } + LOGGER_TRACE("Timer fd = " << timerfd_); if (handle_->getLoop() != loop_) { LOGGER_ERROR("handle_->getLoop() == loop_"); } diff --git a/base/fcgiserver.h.autosave b/base/fcgiserver.h.autosave deleted file mode 100644 index cb12980f73269c9cbda4cd1f53dca508948094e0..0000000000000000000000000000000000000000 --- a/base/fcgiserver.h.autosave +++ /dev/null @@ -1,5 +0,0 @@ -#ifndef FAS_FCGISERVER_H -#define FCGISERVER_H - -#endif // FCGISERVER_H - diff --git a/bin/purejson b/bin/purejson new file mode 100644 index 0000000000000000000000000000000000000000..752c47a5f4bf4ba594acc4f859cbd60fcb3cc7bf --- /dev/null +++ b/bin/purejson @@ -0,0 +1,15 @@ +{ +"id":"20337ffa98c4b666", "bidid":"4dxdfsfxww", "cur":"CNY", +"seatbid":[ +{ +"seat":"45244333555", "group":0, +"bid":[ +{ +"id":"CFDDFSEXKl", +"bundle":"com.cleanmaster.mguard", +"impid":"1", +"price":3.2, "nurl":"http://www.test.com/?winprice={AUCTION_PRICE}", +"adm":{"banner":{"imptrackers":["test1.com"],"link":{"url":"www.aa.com","clicktrackers ":["click.com"]},"img":{"url":"www.image.com","w":1200,"h":628}}} +} ] +} ] +} \ No newline at end of file diff --git a/conf/fas.conf b/conf/fas.conf index 35e0e1730ee9e35dba02ffa331fd0537497a8a63..a2e25a7398c661bf9dd6c850c692bae804fbb7dd 100644 --- a/conf/fas.conf +++ b/conf/fas.conf @@ -5,7 +5,7 @@ "type" : "epoll" }, "fas" : { - "thread_num" : "4", + "thread_num" : "0", "server_ip" : "127.0.0.1", "server_port": "6686" } diff --git a/conf/log.conf b/conf/log.conf deleted file mode 100644 index aa0a6122f47371b8165fb96eaaee577a34bfa33a..0000000000000000000000000000000000000000 --- a/conf/log.conf +++ /dev/null @@ -1,60 +0,0 @@ -log4cplus.logger.TRACE=TRACE,TRACE -log4cplus.logger.DEBUG=DEBUG,DEBUG -log4cplus.logger.INFO=INFO,INFO -log4cplus.logger.WARN=WARN,WARN -log4cplus.logger.ERROR=ERROR,ERROR -log4cplus.logger.SYS_ERROR=ERROR,ERROR -log4cplus.logger.FATAL=FATAL,FATAL - -log4cplus.appender.TRACE=log4cplus::ConsoleAppender -log4cplus.appender.TRACE.layout=log4cplus::PatternLayout -log4cplus.appender.TRACE.Append=true -log4cplus.appender.TRACE.layout.ConversionPattern=%D{%Y-%m-%d %H:%M:%S} [%t] %-5p [%l]: %m%n - -log4cplus.appender.DEBUG=log4cplus::DailyRollingFileAppender -log4cplus.appender.DEBUG.Schedule=DAILY -log4cplus.appender.DEBUG.DatePattern=%Y-%m-%d -log4cplus.appender.DEBUG.layout=log4cplus::PatternLayout -log4cplus.appender.DEBUG.File=./log/fas.log.debug -log4cplus.appender.DEBUG.Append=true -log4cplus.appender.DEBUG.layout.ConversionPattern=%D{%Y-%m-%d %H:%M:%S} [%t] %-5p [%l]: %m%n - -log4cplus.appender.INFO=log4cplus::DailyRollingFileAppender -log4cplus.appender.INFO.Schedule=DAILY -log4cplus.appender.INFO.layout=log4cplus::PatternLayout -log4cplus.appender.INFO.File=./log/fas.log.info -log4cplus.appender.INFO.Append=true -log4cplus.appender.INFO.DatePattern=%Y-%m-%d -log4cplus.appender.INFO.layout.ConversionPattern=%D{%Y-%m-%d %H:%M:%S} [%t] %-5p [%l]: %m%n - -log4cplus.appender.WARN=log4cplus::DailyRollingFileAppender -log4cplus.appender.WARN.Schedule=DAILY -log4cplus.appender.WARN.layout=log4cplus::PatternLayout -log4cplus.appender.WARN.File=./log/fas.log.warn -log4cplus.appender.WARN.Append=true -log4cplus.appender.WARN.DatePattern=%Y-%m-%d -log4cplus.appender.WARN.layout.ConversionPattern=%D{%Y-%m-%d %H:%M:%S} [%t] %-5p [%l]: %m%n - -log4cplus.appender.ERROR=log4cplus::DailyRollingFileAppender -log4cplus.appender.ERROR.Schedule=DAILY -log4cplus.appender.ERROR.layout=log4cplus::PatternLayout -log4cplus.appender.ERROR.File=./log/fas.log.error -log4cplus.appender.ERROR.Append=true -log4cplus.appender.ERROR.DatePattern=%Y-%m-%d -log4cplus.appender.ERROR.layout.ConversionPattern=%D{%Y-%m-%d %H:%M:%S} [%t] %-5p [%l]: %m%n - -log4cplus.appender.SYS_ERROR=log4cplus::DailyRollingFileAppender -log4cplus.appender.SYS_ERROR.Schedule=DAILY -log4cplus.appender.SYS_ERROR.layout=log4cplus::PatternLayout -log4cplus.appender.SYS_ERROR.File=./log/fas.log.sys_error -log4cplus.appender.SYS_ERROR.Append=true -log4cplus.appender.SYS_ERROR.DatePattern=%Y-%m-%d -log4cplus.appender.SYS_ERROR.layout.ConversionPattern=%D{%Y-%m-%d %H:%M:%S} [%t] %-5p [%l]: %m%n - -log4cplus.appender.FATAL=log4cplus::DailyRollingFileAppender -log4cplus.appender.FATAL.Schedule=DAILY -log4cplus.appender.FATAL.layout=log4cplus::PatternLayout -log4cplus.appender.FATAL.File=./log/fas.log.fatal -log4cplus.appender.FATAL.Append=true -log4cplus.appender.FATAL.DatePattern=%Y-%m-%d -log4cplus.appender.FATAL.layout.ConversionPattern=%D{%Y-%m-%d %H:%M:%S} [%t] %-5p [%l]: %m%n diff --git a/conf/log.conf.allInputTofile b/conf/log.conf.allInputTofile deleted file mode 100644 index 69b7f497bc135b4e3d26d0457e9a721691fc7aa0..0000000000000000000000000000000000000000 --- a/conf/log.conf.allInputTofile +++ /dev/null @@ -1,63 +0,0 @@ -log4cplus.logger.TRACE=TRACE,TRACE -log4cplus.logger.DEBUG=DEBUG,DEBUG -log4cplus.logger.INFO=INFO,INFO -log4cplus.logger.WARN=WARN,WARN -log4cplus.logger.ERROR=ERROR,ERROR -log4cplus.logger.SYS_ERROR=ERROR,ERROR -log4cplus.logger.FATAL=FATAL,FATAL - -log4cplus.appender.TRACE=log4cplus::DailyRollingFileAppender -log4cplus.appender.TRACE.Schedule=DAILY -log4cplus.appender.TRACE.DatePattern=%Y-%m-%d -log4cplus.appender.TRACE.layout=log4cplus::PatternLayout -log4cplus.appender.TRACE.File=./log/fas.log.trace -log4cplus.appender.TRACE.Append=true -log4cplus.appender.TRACE.layout.ConversionPattern=%D{%Y-%m-%d %H:%M:%S} [%t] %-5p [%l]: %m%n - -log4cplus.appender.DEBUG=log4cplus::DailyRollingFileAppender -log4cplus.appender.DEBUG.Schedule=DAILY -log4cplus.appender.DEBUG.DatePattern=%Y-%m-%d -log4cplus.appender.DEBUG.layout=log4cplus::PatternLayout -log4cplus.appender.DEBUG.File=./log/fas.log.debug -log4cplus.appender.DEBUG.Append=true -log4cplus.appender.DEBUG.layout.ConversionPattern=%D{%Y-%m-%d %H:%M:%S} [%t] %-5p [%l]: %m%n - -log4cplus.appender.INFO=log4cplus::DailyRollingFileAppender -log4cplus.appender.INFO.Schedule=DAILY -log4cplus.appender.INFO.layout=log4cplus::PatternLayout -log4cplus.appender.INFO.File=./log/fas.log.info -log4cplus.appender.INFO.Append=true -log4cplus.appender.INFO.DatePattern=%Y-%m-%d -log4cplus.appender.INFO.layout.ConversionPattern=%D{%Y-%m-%d %H:%M:%S} [%t] %-5p [%l]: %m%n - -log4cplus.appender.WARN=log4cplus::DailyRollingFileAppender -log4cplus.appender.WARN.Schedule=DAILY -log4cplus.appender.WARN.layout=log4cplus::PatternLayout -log4cplus.appender.WARN.File=./log/fas.log.warn -log4cplus.appender.WARN.Append=true -log4cplus.appender.WARN.DatePattern=%Y-%m-%d -log4cplus.appender.WARN.layout.ConversionPattern=%D{%Y-%m-%d %H:%M:%S} [%t] %-5p [%l]: %m%n - -log4cplus.appender.ERROR=log4cplus::DailyRollingFileAppender -log4cplus.appender.ERROR.Schedule=DAILY -log4cplus.appender.ERROR.layout=log4cplus::PatternLayout -log4cplus.appender.ERROR.File=./log/fas.log.error -log4cplus.appender.ERROR.Append=true -log4cplus.appender.ERROR.DatePattern=%Y-%m-%d -log4cplus.appender.ERROR.layout.ConversionPattern=%D{%Y-%m-%d %H:%M:%S} [%t] %-5p [%l]: %m%n - -log4cplus.appender.SYS_ERROR=log4cplus::DailyRollingFileAppender -log4cplus.appender.SYS_ERROR.Schedule=DAILY -log4cplus.appender.SYS_ERROR.layout=log4cplus::PatternLayout -log4cplus.appender.SYS_ERROR.File=./log/fas.log.sys_error -log4cplus.appender.SYS_ERROR.Append=true -log4cplus.appender.SYS_ERROR.DatePattern=%Y-%m-%d -log4cplus.appender.SYS_ERROR.layout.ConversionPattern=%D{%Y-%m-%d %H:%M:%S} [%t] %-5p [%l]: %m%n - -log4cplus.appender.FATAL=log4cplus::DailyRollingFileAppender -log4cplus.appender.FATAL.Schedule=DAILY -log4cplus.appender.FATAL.layout=log4cplus::PatternLayout -log4cplus.appender.FATAL.File=./log/fas.log.fatal -log4cplus.appender.FATAL.Append=true -log4cplus.appender.FATAL.DatePattern=%Y-%m-%d -log4cplus.appender.FATAL.layout.ConversionPattern=%D{%Y-%m-%d %H:%M:%S} [%t] %-5p [%l]: %m%n diff --git a/faslog/pid_30344_log_info_.log20170709-211004.30344 b/faslog/pid_30344_log_info_.log20170709-211004.30344 new file mode 100644 index 0000000000000000000000000000000000000000..f4bfb122cc26eed19a17fd65797e7f27dc9c4ac0 --- /dev/null +++ b/faslog/pid_30344_log_info_.log20170709-211004.30344 @@ -0,0 +1,19 @@ +Log file created at: 2017/07/09 21:10:04 +Running on machine: fas +Log line format: [IWEF]mmdd hh:mm:ss.uuuuuu threadid file:line] msg +I0709 21:10:04.166541 30344 Environment.cpp:62] load ./conf/fas.conf success. +I0709 21:10:04.166834 30344 MultiProcessTcpServer.cpp:83] Environment init success! +I0709 21:10:04.166872 30344 MultiProcessTcpServer.cpp:123] Reload info succeed, Multi Server begin to start. +I0709 21:10:04.166991 30344 TcpServer.cpp:33] server listen fd = 4 +I0709 21:10:04.167042 30344 MultiProcessTcpServer.cpp:158] idx = 0 read = 5 write = 6 +I0709 21:10:04.167095 30344 MultiProcessTcpServer.cpp:158] idx = 1 read = 7 write = 8 +W0709 21:10:04.167610 30345 MultiProcessTcpServer.cpp:181] TcpServer conn build callback wasn't set. +W0709 21:10:04.168154 30345 MultiProcessTcpServer.cpp:186] TcpServer conn removed callback wasn't set. +W0709 21:10:04.168948 30346 MultiProcessTcpServer.cpp:181] TcpServer conn build callback wasn't set. +W0709 21:10:04.169428 30346 MultiProcessTcpServer.cpp:186] TcpServer conn removed callback wasn't set. +I0709 21:10:06.704218 30344 MultiProcessTcpServer.cpp:46] Recive a SIGQUIT, will be exit. +I0709 21:10:06.704347 30344 MultiProcessTcpServer.cpp:212] MultiProcessTcpServer will be destroyed. +I0709 21:10:06.704385 30344 ProcessTcpServer.cpp:27] ProcessTcpServer wil be destroyed in process 30344 +I0709 21:10:06.704421 30344 ProcessTcpServer.cpp:27] ProcessTcpServer wil be destroyed in process 30344 +I0709 21:10:06.704450 30344 TcpServer.cpp:155] TcpServer will be destroyed in process 30344 +I0709 21:10:06.704484 30344 Socket.cpp:102] tid : 30344 socket close! diff --git a/faslog/pid_30344_log_warning_.log20170709-211004.30345 b/faslog/pid_30344_log_warning_.log20170709-211004.30345 new file mode 100644 index 0000000000000000000000000000000000000000..5beb9908d18293f161dfbd31c5d49f1b725a3cbe --- /dev/null +++ b/faslog/pid_30344_log_warning_.log20170709-211004.30345 @@ -0,0 +1,5 @@ +Log file created at: 2017/07/09 21:10:04 +Running on machine: fas +Log line format: [IWEF]mmdd hh:mm:ss.uuuuuu threadid file:line] msg +W0709 21:10:04.167610 30345 MultiProcessTcpServer.cpp:181] TcpServer conn build callback wasn't set. +W0709 21:10:04.168154 30345 MultiProcessTcpServer.cpp:186] TcpServer conn removed callback wasn't set. diff --git a/faslog/pid_30344_log_warning_.log20170709-211004.30346 b/faslog/pid_30344_log_warning_.log20170709-211004.30346 new file mode 100644 index 0000000000000000000000000000000000000000..16d7b5acf0d9f9c272019d88ae6fb168a906ec72 --- /dev/null +++ b/faslog/pid_30344_log_warning_.log20170709-211004.30346 @@ -0,0 +1,5 @@ +Log file created at: 2017/07/09 21:10:04 +Running on machine: fas +Log line format: [IWEF]mmdd hh:mm:ss.uuuuuu threadid file:line] msg +W0709 21:10:04.168948 30346 MultiProcessTcpServer.cpp:181] TcpServer conn build callback wasn't set. +W0709 21:10:04.169428 30346 MultiProcessTcpServer.cpp:186] TcpServer conn removed callback wasn't set. diff --git a/faslog/pid_30345_log_info_.log20170709-211004.30345 b/faslog/pid_30345_log_info_.log20170709-211004.30345 new file mode 100644 index 0000000000000000000000000000000000000000..f3f82802bf891a7186753cde155277516612e217 --- /dev/null +++ b/faslog/pid_30345_log_info_.log20170709-211004.30345 @@ -0,0 +1,10 @@ +Log file created at: 2017/07/09 21:10:04 +Running on machine: fas +Log line format: [IWEF]mmdd hh:mm:ss.uuuuuu threadid file:line] msg +I0709 21:10:04.168330 30345 TimersScheduler.cpp:30] Timer fd = 9 +I0709 21:10:04.168534 30345 EventLoop.cpp:57] wakeup fd = 3 +I0709 21:10:04.168889 30347 EventLoopThread.cpp:57] Thread tid :30347 +I0709 21:10:04.169041 30347 TimersScheduler.cpp:30] Timer fd = 12 +I0709 21:10:04.169142 30347 EventLoop.cpp:57] wakeup fd = 6 +I0709 21:10:06.704988 30345 MultiProcessTcpServer.cpp:46] Recive a SIGQUIT, will be exit. +I0709 21:10:06.705067 30345 ProcessTcpServer.cpp:123] Receive quit cmd, process server will be exit. diff --git a/faslog/pid_30346_log_error_.log20170709-211006.30346 b/faslog/pid_30346_log_error_.log20170709-211006.30346 new file mode 100644 index 0000000000000000000000000000000000000000..8892e5ec52c113ca931a61a9a9316eb2d13fcb0a --- /dev/null +++ b/faslog/pid_30346_log_error_.log20170709-211006.30346 @@ -0,0 +1,4 @@ +Log file created at: 2017/07/09 21:10:06 +Running on machine: fas +Log line format: [IWEF]mmdd hh:mm:ss.uuuuuu threadid file:line] msg +E0709 21:10:06.704314 30346 Epoll.cpp:54] epoll_wait error : Interrupted system call diff --git a/faslog/pid_30346_log_info_.log20170709-211004.30346 b/faslog/pid_30346_log_info_.log20170709-211004.30346 new file mode 100644 index 0000000000000000000000000000000000000000..2c5bb974b4a731268a9f3ec4675e7326a9266bac --- /dev/null +++ b/faslog/pid_30346_log_info_.log20170709-211004.30346 @@ -0,0 +1,11 @@ +Log file created at: 2017/07/09 21:10:04 +Running on machine: fas +Log line format: [IWEF]mmdd hh:mm:ss.uuuuuu threadid file:line] msg +I0709 21:10:04.169574 30346 TimersScheduler.cpp:30] Timer fd = 5 +I0709 21:10:04.169760 30346 EventLoop.cpp:57] wakeup fd = 3 +I0709 21:10:04.169989 30348 EventLoopThread.cpp:57] Thread tid :30348 +I0709 21:10:04.170078 30348 TimersScheduler.cpp:30] Timer fd = 11 +I0709 21:10:04.170156 30348 EventLoop.cpp:57] wakeup fd = 8 +I0709 21:10:06.704211 30346 MultiProcessTcpServer.cpp:46] Recive a SIGQUIT, will be exit. +E0709 21:10:06.704314 30346 Epoll.cpp:54] epoll_wait error : Interrupted system call +I0709 21:10:06.704659 30346 ProcessTcpServer.cpp:123] Receive quit cmd, process server will be exit. diff --git a/faslog/pid_30346_log_warning_.log20170709-211006.30346 b/faslog/pid_30346_log_warning_.log20170709-211006.30346 new file mode 100644 index 0000000000000000000000000000000000000000..8892e5ec52c113ca931a61a9a9316eb2d13fcb0a --- /dev/null +++ b/faslog/pid_30346_log_warning_.log20170709-211006.30346 @@ -0,0 +1,4 @@ +Log file created at: 2017/07/09 21:10:06 +Running on machine: fas +Log line format: [IWEF]mmdd hh:mm:ss.uuuuuu threadid file:line] msg +E0709 21:10:06.704314 30346 Epoll.cpp:54] epoll_wait error : Interrupted system call diff --git a/http/HttpReqHandle.cpp b/http/HttpReqHandle.cpp index 6b0bf18891c52a770360f2ba0fe8d85085a40029..60a5d240f76e75cc1fbc37a80f78f89988aab35f 100644 --- a/http/HttpReqHandle.cpp +++ b/http/HttpReqHandle.cpp @@ -200,8 +200,8 @@ fas::http::HttpReqHandle::~HttpReqHandle() { fas::http::HttpReqHandle::SendMassDataContext::SendMassDataContext(int fd, - int length, - int rdstart) : + size_t length, + size_t rdstart) : fd_(fd), length_(length), rdstart_(rdstart), @@ -210,8 +210,8 @@ fas::http::HttpReqHandle::SendMassDataContext::SendMassDataContext(int fd, } void fas::http::HttpReqHandle::SendMassDataContext::ContextReset(int fd, - int length, - int rdstart) { + size_t length, + size_t rdstart) { fd_ = fd; length_ = length; rdstart_ = rdstart; diff --git a/http/HttpReqHandle.h b/http/HttpReqHandle.h index 65bc6bf0888be0c6b77aaaa95a724c65844b8857..a9c6ac8aafc0ab81dd8d4359cf169e8f3bac3ce3 100644 --- a/http/HttpReqHandle.h +++ b/http/HttpReqHandle.h @@ -36,9 +36,9 @@ using TcpConnShreadPtr = fas::TcpConnection::TcpConnShreadPtr; class SendMassDataContext { public: SendMassDataContext() = default; - SendMassDataContext(int fd, int length, int rdstart); + SendMassDataContext(int fd, size_t length, size_t rdstart); - void ContextReset(int fd, int length, int rdstart); + void ContextReset(int fd, size_t length, size_t rdstart); void addSizeToRdstartAndUpdateRemind(ssize_t size); diff --git a/http/HttpServer.cpp b/http/HttpServer.cpp index 716709f8f6f9d57699eb9c08b4dd63d4e383c87b..cf6d5f1f332c9b9d4fa9f06f65361e354a1b6286 100644 --- a/http/HttpServer.cpp +++ b/http/HttpServer.cpp @@ -35,7 +35,7 @@ void fas::http::HttpServer::OnConnectionRemoved(fas::TcpServer::connkey_t key) { return; } - int ret = this->reqHandles_.erase(key); + ssize_t ret = this->reqHandles_.erase(key); assert(ret == 1); LOGGER_TRACE("out of fas::http::HttpServer::OnConnectionRemoved"); } diff --git a/http/Makefile b/http/Makefile deleted file mode 100644 index 841c5074dd6447839a9d062ca6d5e04e48346fcb..0000000000000000000000000000000000000000 --- a/http/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -FAS_HTTP_STATIC_LIB := $(FAS_LIBS_DIR)/libfashttp.a -FAS_HTTP_SHARED_LIB := $(FAS_LIBS_DIR)/libfashttp.so - -OBJECTS := $(patsubst %.cpp, %.o, $(wildcard *.cpp)) - -all:$(OBJECTS) - $(SHARED_LIB_CMD) $^ -L$(FAS_LIBS_DIR) -o $(FAS_HTTP_SHARED_LIB) - $(STATIC_LIB_CMD) $(FAS_HTTP_STATIC_LIB) $^ - -%.o:%.cpp $(HTTP_HEADERS) - $(CC) -c $< $(HTTP_INCLUDE_DIR) $(CFLAG) -o $@ $(HTTP_LIBS) - -.PHONY : clean -clean: - rm *.o -rf diff --git a/lib/libfas.a b/lib/libfas.a deleted file mode 100644 index c741d3f2fdd8c0036194ad60570b74ed5794f1ef..0000000000000000000000000000000000000000 Binary files a/lib/libfas.a and /dev/null differ diff --git a/lib/libfas.so b/lib/libfas.so deleted file mode 100755 index 9ff76301675dc937cf6a84577a0bbb10aba1bf64..0000000000000000000000000000000000000000 Binary files a/lib/libfas.so and /dev/null differ diff --git a/lib/libfashttp.a b/lib/libfashttp.a deleted file mode 100644 index 6e277d514bc073654624c47fabb135d57b88349a..0000000000000000000000000000000000000000 Binary files a/lib/libfashttp.a and /dev/null differ diff --git a/lib/libfashttp.so b/lib/libfashttp.so deleted file mode 100755 index 13c5998db85d97794a7758f13fc7cc8e9eaf523b..0000000000000000000000000000000000000000 Binary files a/lib/libfashttp.so and /dev/null differ diff --git a/lib/libfasutils.a b/lib/libfasutils.a deleted file mode 100644 index 5eb7d683ecb1492482b70ef0498811081f98099e..0000000000000000000000000000000000000000 Binary files a/lib/libfasutils.a and /dev/null differ diff --git a/lib/libfasutils.so b/lib/libfasutils.so deleted file mode 100755 index 201c5eff9cdace31cb67908dad9ac6b334cf2d3d..0000000000000000000000000000000000000000 Binary files a/lib/libfasutils.so and /dev/null differ diff --git a/mdm/Makefile b/mdm/Makefile deleted file mode 100644 index 2175d9716556d93b3de363c0dd6bfb5f37df0a62..0000000000000000000000000000000000000000 --- a/mdm/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -FAS_MDM_STATIC_LIB := $(FAS_LIBS_DIR)/libfasmdm.a -FAS_MDM_SHARED_LIB := $(FAS_LIBS_DIR)/libfasmdm.so - -OBJECTS := $(patsubst %.cpp, %.o, $(wildcard *.cpp)) - -all:$(OBJECTS) - $(SHARED_LIB_CMD) $^ -L$(FAS_LIBS_DIR) -o $(FAS_MDM_SHARED_LIB) - $(STATIC_LIB_CMD) $(FAS_MDM_STATIC_LIB) $^ - -%.o:%.cpp $(MDM_HEADERS) - $(CC) -c $< $(MDM_INCLUDE_DIR) $(CFLAG) -o $@ $(MDM_LIBS) - -.PHONY : clean -clean: - rm *.o -rf diff --git a/mdm/Md5.cpp b/mdm/Md5.cpp index 9175fae3ba2de55b3ffba2ca0bed3e6c1562ec82..3325c05dde3a63d1323d6651defd220dc79a2b98 100644 --- a/mdm/Md5.cpp +++ b/mdm/Md5.cpp @@ -274,7 +274,7 @@ int _httoi(const char *value) int result = 0; if (*s == '0' && *(s + 1) == 'X') s += 2; bool firsttime = true; - while (*s != '/0') + while (s[0] != '\0') { bool found = false; for (int i = 0; i < HexMapL; i++) diff --git a/mdm/Md5.h b/mdm/Md5.h index a101fd8bc07c61d5047b42f87919d66febfa5565..4b66d942b261ca31bac73f6b677aeb3a8e07423d 100644 --- a/mdm/Md5.h +++ b/mdm/Md5.h @@ -16,7 +16,7 @@ class MD5 { private: #define uint8 unsigned char - #define uint32 unsigned long int + #define uint32 uint32_t struct md5_context { diff --git a/mdm/mcacheTask.cpp b/mdm/mcacheTask.cpp index ad22e19e0971418c54ef7aa52f858ea5c9a2027e..4cc0e451616d97a4b288741a8b6e126f835eee94 100644 --- a/mdm/mcacheTask.cpp +++ b/mdm/mcacheTask.cpp @@ -146,7 +146,7 @@ bool fas::mdm::mcacheTask::handleOtherCmd(TcpConnShreadPtr conn, fas::Buffer *bu head_ = head_ + "\r\n"; const char *buf = head_.data(); - int ret = writeSizeBytes2NoBlockSock(sock.getSocket(), buf, head_.size()); + ssize_t ret = writeSizeBytes2NoBlockSock(sock.getSocket(), buf, head_.size()); if (ret == -1 || (ret != static_cast(head_.size()))) { setState(TaskState::BAD); conn->sendString("SERVER_ERROR\r\n"); @@ -214,7 +214,7 @@ bool fas::mdm::mcacheTask::handleIncrDecrCmd(TcpConnShreadPtr conn, fas::Buffer head_ = head_ + "\r\n"; const char *buf = head_.data(); - int ret = writeSizeBytes2NoBlockSock(sock.getSocket(), buf, head_.size()); + ssize_t ret = writeSizeBytes2NoBlockSock(sock.getSocket(), buf, head_.size()); if (ret == -1 || (ret != static_cast(head_.size()))) { setState(TaskState::BAD); conn->sendString("SERVER_ERROR\r\n"); @@ -280,7 +280,7 @@ bool fas::mdm::mcacheTask::handleDeleteCmd(TcpConnShreadPtr conn, fas::Buffer *b head_ = head_ + "\r\n"; const char *buf = head_.data(); - int ret = writeSizeBytes2NoBlockSock(sock.getSocket(), buf, head_.size()); + ssize_t ret = writeSizeBytes2NoBlockSock(sock.getSocket(), buf, head_.size()); if (ret == -1 || (ret != static_cast(head_.size()))) { setState(TaskState::BAD); conn->sendString("SERVER_ERROR\r\n"); @@ -400,7 +400,7 @@ bool fas::mdm::mcacheTask::handleRetriveCmd(TcpConnShreadPtr conn, fas::Buffer * std::string reqHead = method_ + " " +gets_socks_v.find((*iter).getFd())->second + "\r\n"; const char *buf = reqHead.data(); - int ret = writeSizeBytes2NoBlockSock((*iter).getFd(), buf, reqHead.size()); + ssize_t ret = writeSizeBytes2NoBlockSock((*iter).getFd(), buf, reqHead.size()); if (ret == -1 || (ret != static_cast(reqHead.size()))) { continue; } @@ -501,7 +501,7 @@ bool fas::mdm::mcacheTask::handleStrogeCmd(TcpConnShreadPtr conn, fas::Buffer *b head_ = head_ + "\r\n"; const char *buf = head_.c_str(); - int ret = writeSizeBytes2NoBlockSock(sock.getSocket(), buf, head_.size()); + ssize_t ret = writeSizeBytes2NoBlockSock(sock.getSocket(), buf, head_.size()); if (ret == -1 || (ret != static_cast(head_.size()))) { setState(TaskState::BAD); conn->sendString("SERVER_ERROR\r\n"); @@ -631,12 +631,12 @@ int fas::mdm::mcacheTask::getBytes() const { return this->bytes_; } -int writeSizeBytes2NoBlockSock(int sock, const char *buf, int size) { +ssize_t writeSizeBytes2NoBlockSock(int sock, const char *buf, ssize_t size) { if (size <= 0) { return size; } - int hasRet = 0, ret = 0; + ssize_t hasRet = 0, ret = 0; while (hasRet < size) { ret = write(sock, buf + hasRet, size - hasRet); if (ret < 0) { @@ -654,13 +654,13 @@ int writeSizeBytes2NoBlockSock(int sock, const char *buf, int size) { return size; } -int readUntilStrWithEnd(int sock, std::string& str, std::string end) { +ssize_t readUntilStrWithEnd(int sock, std::string& str, std::string end) { #define BUFSIZE_L 53 #define READSIZE_L 52 char buf[BUFSIZE_L] = {0}; while (true) { - int ret = read(sock, buf, READSIZE_L); + ssize_t ret = read(sock, buf, READSIZE_L); if (ret < 0) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK) || (errno == EINTR)) { continue; diff --git a/mdm/mcacheTask.h b/mdm/mcacheTask.h index 0b5f72a4330aab130c23167d94769e8b64809fd6..12f73a0a863d9b5ddb74caf4b3d73c065aef9e79 100644 --- a/mdm/mcacheTask.h +++ b/mdm/mcacheTask.h @@ -75,7 +75,7 @@ private: } -int writeSizeBytes2NoBlockSock(int sock, const char *buf, int size); -int readUntilStrWithEnd(int sock, std::string& str, std::string end); +ssize_t writeSizeBytes2NoBlockSock(int sock, const char *buf, ssize_t size); +ssize_t readUntilStrWithEnd(int sock, std::string& str, std::string end); #endif // FAS_MCACHETASK_H diff --git a/mdm/mcacheTaskServer.cpp b/mdm/mcacheTaskServer.cpp index 9b766902d02929fb428b9fb50f01fde514eec241..3e1e228093299b00228e19aa63d6f8173a0fa107 100644 --- a/mdm/mcacheTaskServer.cpp +++ b/mdm/mcacheTaskServer.cpp @@ -29,7 +29,7 @@ void fas::mdm::mcacheTaskServer::OnConnectionRemoved(connkey_t key) { return; } - int ret = this->reqHandles_.erase(key); + ssize_t ret = this->reqHandles_.erase(key); assert(ret == 1); } diff --git a/test/.sconsign.dblite b/test/.sconsign.dblite new file mode 100644 index 0000000000000000000000000000000000000000..ed92283174749469fddc8437870ec199ab2d2e5f Binary files /dev/null and b/test/.sconsign.dblite differ diff --git a/test/JsonTest.cpp b/test/JsonTest.cpp new file mode 100644 index 0000000000000000000000000000000000000000..73be9d867b6d8f14c7c4f7091364969589bfbb3e --- /dev/null +++ b/test/JsonTest.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include + +int main () { + std::ifstream in; + in.open("./purejson", std::ios_base::in); + if (!in.is_open()) { + std::cerr << "open purejson error." << std::endl; + return -1; + } + Json::Reader reader; + Json::FastWriter write; + Json::Value root; + + if (!reader.parse(in, root)) { + std::cerr << "parse purejson error." << std::endl; + return -1; + } + if (root.isMember("seatbid") && root["seatbid"].isArray()) { + int index = 0; + root = root["seatbid"][index]; + if (root.isMember("bid") && root["bid"].isArray()) { + if (root["bid"].size() <= 0) { + std::cerr << "bid array is empty." << std::endl; + return -1; + } + std::cout << "bid array size:" << root["bid"].size() << std::endl; + Json::Value bid = root["bid"][index]; + + if (bid.isMember("adm") && bid["adm"].isObject()) { + std::cout << "adm:" << write.write(bid["adm"]) << std::endl; + } else { + if (!bid.isMember("adm")) { + std::cerr << "adm is not adm member." << std::endl; + } else { + std::cerr << "bid[adm] value is not object." << std::endl; + std::cout << "adm:" << write.write(bid["adm"]) << std::endl; + } + } + } + } else { + std::cerr << "can't find seatbid member or seatbid is null." << std::endl; + return -1; + } +} diff --git a/test/JsonTest.o b/test/JsonTest.o new file mode 100644 index 0000000000000000000000000000000000000000..e7f842b2d46ab0b2e2d9411b42bab0fa11ab7a58 Binary files /dev/null and b/test/JsonTest.o differ diff --git a/test/Makefile b/test/Makefile deleted file mode 100644 index db65b1ebfdac470068cd75500af60935b1c37723..0000000000000000000000000000000000000000 --- a/test/Makefile +++ /dev/null @@ -1,14 +0,0 @@ -all: - $(CC) TcpServerTest.cpp $(TEST_INCLUDE_DIR) -L$(FAS_LIBS_DIR) $(THIRD_LIB_DIR) $(TEST_CFLAG) -o $(FAS_TEST_BIN)/TcpServerTest $(TEST_BIN_LIBS) - $(CC) HttpServerTest.cpp $(TEST_INCLUDE_DIR) -L$(FAS_LIBS_DIR) $(THIRD_LIB_DIR) $(TEST_CFLAG) -o $(FAS_TEST_BIN)/HttpServerTest $(TEST_BIN_LIBS) -# $(CC) ThreadLoggerTest.cpp -I${fas_base} -L${fas_libs} $(cflags) -o $(fas_test_bin)/ThreadLoggerTest $(libs) -# $(CC) EventHandleClientTest.cpp -I${fas_base} -L${fas_libs} $(cflags) -o $(fas_test_bin)/EventHandleClientTest $(libs) -# $(CC) LogTest.cpp -I${fas_base} -L${fas_libs} $(cflags) -o $(fas_test_bin)/LogTest $(libs) -# $(CC) TimerHeapTest.cpp -I${fas_base} -L${fas_libs} $(cflags) -o $(fas_test_bin)/TimerHeapTest $(libs) -# $(CC) TimersSchedulerTest.cpp -I${fas_base} -L${fas_libs} $(cflags) -o $(fas_test_bin)/TimersSchedulerTest $(libs) -# $(CC) EventFdTest.cpp -I${fas_base} -L${fas_libs} $(cflags) -o $(fas_test_bin)/EventFdTest $(libs) - - -.PHONY : clean -clean: - rm *.o -rf diff --git a/test/MultiProcessServer.cpp b/test/MultiProcessServer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a62ca801fd08fe73322a574d4ba034b821f4474d --- /dev/null +++ b/test/MultiProcessServer.cpp @@ -0,0 +1,21 @@ +#include +#include + +using namespace fas; + +void ConnMessageCallback(fas::TcpServer::TcpConnShreadPtr conn, Buffer *buffer, Timestamp time) { + /*! + * \brief str + * 把收到的内容存到一个字符串中,供后面打印出来。 + */ + std::string str(buffer->retrieveAllAsString()); + std::cout << time.toFormattedString() << ":" << str << std::endl; + conn->sendString(str); + conn->shutdown(); +} + +int main () { + MultiProcessTcpServer* server = MultiProcessTcpServer::Instance(); + server->setConnMessageCallback(ConnMessageCallback); + server->start(); +} diff --git a/test/TcpServerTest.cpp b/test/TcpServerTest.cpp index 14846fc80279025c2d1c5e5b5fe6f564575260d9..8307c8030773c891295349594c6ecdaf013d2616 100644 --- a/test/TcpServerTest.cpp +++ b/test/TcpServerTest.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include @@ -72,5 +73,6 @@ int main() ser->start(); //最终程序会在这个loop函数里面进行各种事件的监听。 - loop->loop(); +// loop->loop(); + delete loop; } diff --git a/test/TimerHeapTest.cpp b/test/TimerHeapTest.cpp index 99032191572aa09e1d3823d1c3779010489b1346..e86ed019cd68bd78b09fcd561eb559d4da11ecf2 100644 --- a/test/TimerHeapTest.cpp +++ b/test/TimerHeapTest.cpp @@ -1,11 +1,9 @@ #include - #include #include #include - #include using namespace std; diff --git a/test/purejson b/test/purejson new file mode 100644 index 0000000000000000000000000000000000000000..752c47a5f4bf4ba594acc4f859cbd60fcb3cc7bf --- /dev/null +++ b/test/purejson @@ -0,0 +1,15 @@ +{ +"id":"20337ffa98c4b666", "bidid":"4dxdfsfxww", "cur":"CNY", +"seatbid":[ +{ +"seat":"45244333555", "group":0, +"bid":[ +{ +"id":"CFDDFSEXKl", +"bundle":"com.cleanmaster.mguard", +"impid":"1", +"price":3.2, "nurl":"http://www.test.com/?winprice={AUCTION_PRICE}", +"adm":{"banner":{"imptrackers":["test1.com"],"link":{"url":"www.aa.com","clicktrackers ":["click.com"]},"img":{"url":"www.image.com","w":1200,"h":628}}} +} ] +} ] +} \ No newline at end of file diff --git a/utils/Makefile b/utils/Makefile deleted file mode 100644 index 6d7d99e27953aaa93776f46bb6720cc5fab39272..0000000000000000000000000000000000000000 --- a/utils/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -FAS_UTILS_STATIC_LIB := $(FAS_LIBS_DIR)/libfasutils.a -FAS_UTILS_SHARED_LIB := $(FAS_LIBS_DIR)/libfasutils.so - -OBJECTS := $(patsubst %.cpp, %.o, $(wildcard *.cpp)) - -all:$(OBJECTS) - $(SHARED_LIB_CMD) $^ -L$(FAS_LIBS_DIR) -o $(FAS_UTILS_SHARED_LIB) - $(STATIC_LIB_CMD) $(FAS_UTILS_STATIC_LIB) $^ - -%.o:%.cpp $(UTILS_HEADERS) - $(CC) -c $< $(UTILS_INCLUDE_DIR) $(CFLAG) -o $@ $(UTILS_LIBS) - -.PHONY : clean -clean: - rm *.o -rf