1
This commit is contained in:
parent
7843e73481
commit
389b2df366
186
f8/app.cc
186
f8/app.cc
@ -1,6 +1,190 @@
|
|||||||
|
#include <signal.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <mutex>
|
||||||
|
#include <condition_variable>
|
||||||
|
|
||||||
#include <a8/a8.h>
|
#include <a8/a8.h>
|
||||||
#include <a8/uuid.h>
|
#include <a8/uuid.h>
|
||||||
|
|
||||||
#include <f8/f8.h>
|
#include <f8/f8.h>
|
||||||
|
#include <f8/udplog.h>
|
||||||
#include <f8/app.h>
|
#include <f8/app.h>
|
||||||
|
#include <f8/msgqueue.h>
|
||||||
|
#include <f8/timer.h>
|
||||||
|
|
||||||
|
const int MAX_NODE_ID = 8;
|
||||||
|
const int MAX_INSTANCE_ID = 500;
|
||||||
|
|
||||||
|
const char* const PROJ_NAME_FMT = "game%d_wsproxy";
|
||||||
|
const char* const PROJ_ROOT_FMT = "/data/logs/%s";
|
||||||
|
const char* const PROJ_LOG_ROOT_FMT = "/data/logs/%s/logs";
|
||||||
|
const char* const PROJ_LOG_FILENAME_FMT = "log_$pid_%Y%m%d.log";
|
||||||
|
|
||||||
|
namespace f8
|
||||||
|
{
|
||||||
|
|
||||||
|
void App::Init()
|
||||||
|
{
|
||||||
|
signal(SIGPIPE, SIG_IGN);
|
||||||
|
srand(time(nullptr));
|
||||||
|
|
||||||
|
uuid_ = std::make_shared<a8::uuid::SnowFlake>();
|
||||||
|
loop_mutex_ = new std::mutex();
|
||||||
|
loop_cond_ = new std::condition_variable();
|
||||||
|
|
||||||
|
if (!ParseOpt()) {
|
||||||
|
terminated_ = true;
|
||||||
|
if (node_id_ <= 0) {
|
||||||
|
a8::XPrintf("启动失败,缺少-n参数\n", {});
|
||||||
|
} else if (node_id_ > MAX_NODE_ID) {
|
||||||
|
a8::XPrintf("启动失败,-n参数不能大于%d\n", {MAX_NODE_ID});
|
||||||
|
} else if (instance_id_ <= 0) {
|
||||||
|
a8::XPrintf("启动失败,缺少-i参数\n", {});
|
||||||
|
} else if (instance_id_ > MAX_INSTANCE_ID) {
|
||||||
|
a8::XPrintf("启动失败,-i参数不能大于%d\n", {MAX_INSTANCE_ID});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
a8::XPrintf("wsproxy starting node_id:%d instance_id:%d pid:%d\n",
|
||||||
|
{
|
||||||
|
node_id_,
|
||||||
|
instance_id_,
|
||||||
|
GetPid()
|
||||||
|
});
|
||||||
|
|
||||||
|
uuid_->SetMachineId((node_id_ - 1) * MAX_NODE_ID + instance_id_);
|
||||||
|
f8::MsgQueue::Instance()->Init();
|
||||||
|
f8::Timer::Instance()->Init();
|
||||||
|
user_app_->Init();
|
||||||
|
}
|
||||||
|
|
||||||
|
void App::UnInit()
|
||||||
|
{
|
||||||
|
user_app_->UnInit();
|
||||||
|
f8::Timer::Instance()->UnInit();
|
||||||
|
f8::MsgQueue::Instance()->UnInit();
|
||||||
|
|
||||||
|
delete loop_cond_;
|
||||||
|
loop_cond_ = nullptr;
|
||||||
|
delete loop_mutex_;
|
||||||
|
loop_mutex_ = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
int App::Run(int argc, char* argv[], UserApp* user_app)
|
||||||
|
{
|
||||||
|
argc_ = argc;
|
||||||
|
argv_ = argv;
|
||||||
|
user_app_ = user_app;
|
||||||
|
Init();
|
||||||
|
while (!Terminated()) {
|
||||||
|
f8::Timer::Instance()->Update();
|
||||||
|
f8::MsgQueue::Instance()->Update();
|
||||||
|
user_app->Update();
|
||||||
|
Schedule();
|
||||||
|
}
|
||||||
|
UnInit();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool App::ParseOpt()
|
||||||
|
{
|
||||||
|
int ch = 0;
|
||||||
|
while ((ch = getopt(argc_, argv_, "n:i:f:")) != -1) {
|
||||||
|
switch (ch) {
|
||||||
|
case 'n':
|
||||||
|
{
|
||||||
|
node_id_ = a8::XValue(optarg);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'i':
|
||||||
|
{
|
||||||
|
instance_id_ = a8::XValue(optarg);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'f':
|
||||||
|
{
|
||||||
|
std::vector<std::string> strings;
|
||||||
|
a8::Split(optarg, strings, ',');
|
||||||
|
for (auto& str : strings) {
|
||||||
|
flags_.insert(a8::XValue(str).GetInt());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return instance_id_ > 0 && node_id_ > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool App::HasFlag(int flag)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void App::NotifyLoopCond()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
long long App::NewNodeUuid()
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string App::NewGlobalUuid()
|
||||||
|
{
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
int App::GetPid()
|
||||||
|
{
|
||||||
|
return getpid();
|
||||||
|
}
|
||||||
|
|
||||||
|
void App::InitLog()
|
||||||
|
{
|
||||||
|
std::string filename_fmt = PROJ_LOG_FILENAME_FMT;
|
||||||
|
a8::ReplaceString(filename_fmt, "$pid",
|
||||||
|
a8::XValue(f8::App::Instance()->GetPid()));
|
||||||
|
|
||||||
|
std::string proj_root_dir = a8::Format
|
||||||
|
(PROJ_ROOT_FMT,
|
||||||
|
{a8::Format(PROJ_NAME_FMT,{GAME_ID})});
|
||||||
|
std::string proj_log_root_dir = a8::Format
|
||||||
|
(PROJ_LOG_ROOT_FMT,
|
||||||
|
{a8::Format(PROJ_NAME_FMT, {GAME_ID})});
|
||||||
|
std::string log_file_name = a8::Format
|
||||||
|
(PROJ_LOG_ROOT_FMT,
|
||||||
|
{a8::Format(PROJ_NAME_FMT, {GAME_ID})}) + "/" + filename_fmt;
|
||||||
|
|
||||||
|
a8::MkDir(proj_root_dir);
|
||||||
|
a8::MkDir(proj_log_root_dir);
|
||||||
|
f8::UdpLog::Instance()->SetLogFileName(log_file_name);
|
||||||
|
f8::UdpLog::Instance()->Init();
|
||||||
|
f8::UdpLog::Instance()->Info("proj_root_dir:%s", {proj_root_dir});
|
||||||
|
f8::UdpLog::Instance()->Info("proj_log_root_dir:%s", {proj_log_root_dir});
|
||||||
|
f8::UdpLog::Instance()->Info("log_file_name:%s", {log_file_name});
|
||||||
|
}
|
||||||
|
|
||||||
|
void App::UnInitLog()
|
||||||
|
{
|
||||||
|
f8::UdpLog::Instance()->UnInit();
|
||||||
|
}
|
||||||
|
|
||||||
|
void App::Schedule()
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lk(*loop_mutex_);
|
||||||
|
if (!user_app_->HasTask()) {
|
||||||
|
#if 1
|
||||||
|
int sleep_time = 1;
|
||||||
|
loop_cond_->wait_for(lk, std::chrono::milliseconds(sleep_time));
|
||||||
|
#else
|
||||||
|
int sleep_time = f8::Timer::Instance()->GetIdleableMillSeconds();
|
||||||
|
loop_cond_->wait_for(lk, std::chrono::milliseconds(sleep_time));
|
||||||
|
if (sleep_time > perf.max_timer_idle) {
|
||||||
|
perf.max_timer_idle = sleep_time;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
21
f8/app.h
21
f8/app.h
@ -20,6 +20,7 @@ namespace f8
|
|||||||
virtual void Init() = 0;
|
virtual void Init() = 0;
|
||||||
virtual void UnInit() = 0;
|
virtual void UnInit() = 0;
|
||||||
virtual void Update() = 0;
|
virtual void Update() = 0;
|
||||||
|
virtual bool HasTask() = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
class App : public a8::Singleton<App>
|
class App : public a8::Singleton<App>
|
||||||
@ -28,22 +29,36 @@ namespace f8
|
|||||||
App() {};
|
App() {};
|
||||||
friend class a8::Singleton<App>;
|
friend class a8::Singleton<App>;
|
||||||
|
|
||||||
void Run(UserApp* user_app);
|
public:
|
||||||
|
|
||||||
|
int Run(int argc, char* argv[], UserApp* user_app);
|
||||||
|
|
||||||
const std::string GetPkgName();
|
const std::string GetPkgName();
|
||||||
void NotifyLoopCond();
|
void NotifyLoopCond();
|
||||||
bool HasFlag(int flag);
|
bool HasFlag(int flag);
|
||||||
long long NewUuid();
|
long long NewNodeUuid();
|
||||||
|
const std::string NewGlobalUuid();
|
||||||
int GetZoneId() { return zone_id_; }
|
int GetZoneId() { return zone_id_; }
|
||||||
int GetNodeId() { return node_id_; }
|
int GetNodeId() { return node_id_; }
|
||||||
int GetInstanceId() { return instance_id_; }
|
int GetInstanceId() { return instance_id_; }
|
||||||
|
int GetNowTime();
|
||||||
|
int GetPid();
|
||||||
|
auto Terminated() { return terminated_; };
|
||||||
|
void Terminate() { terminated_ = true; };
|
||||||
|
|
||||||
|
private:
|
||||||
|
void Init();
|
||||||
|
void UnInit();
|
||||||
|
bool ParseOpt();
|
||||||
|
void InitLog();
|
||||||
|
void UnInitLog();
|
||||||
|
void Schedule();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
UserApp* user_app_ = nullptr;
|
UserApp* user_app_ = nullptr;
|
||||||
int argc_ = 0;
|
int argc_ = 0;
|
||||||
char** argv_ = nullptr;
|
char** argv_ = nullptr;
|
||||||
volatile bool terminated_ = false;
|
volatile bool terminated_ = false;
|
||||||
volatile bool shutdowned_ = false;
|
|
||||||
|
|
||||||
int zone_id_ = 0;
|
int zone_id_ = 0;
|
||||||
int node_id_ = 0;
|
int node_id_ = 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user