f8/f8/app.h
aozhiwei 19fd619f28 1
2023-11-24 14:25:08 +08:00

97 lines
2.5 KiB
C++

#pragma once
#include <atomic>
#include <a8/singleton.h>
#include <a8/queue.h>
namespace a8
{
namespace uuid
{
class SnowFlake;
}
}
namespace f8
{
class UserApp
{
public:
virtual const std::string GetPkgName() = 0;
virtual void Init() = 0;
virtual void UnInit() = 0;
virtual void Update(int delta_time) = 0;
virtual bool HasTask() = 0;
virtual void DispatchSocketMsg(MsgHdr* hdr) = 0;
};
class App : public a8::Singleton<App>
{
private:
App() {};
friend class a8::Singleton<App>;
public:
int Run(int argc, char* argv[], UserApp* user_app);
const std::string GetPkgName();
void NotifyLoopCond();
bool HasFlag(int flag);
long long NewNodeUuid();
const std::string NewGlobalUuid();
int GetZoneId() { return zone_id_; }
int GetNodeId() { return node_id_; }
int GetInstanceId() { return instance_id_; }
int GetNowTime();
int GetPid();
auto Terminated() { return terminated_; };
void Terminate() { terminated_ = true; };
void AddSocketMsg(int sockfrom,
int sockhandle,
long ip_saddr,
unsigned short msgid,
unsigned int seqid,
const char *msgbody,
int bodylen,
int tag);
void FreeSocketMsg(MsgHdr* hdr);
char** GetArgv() { return argv_; }
int GetArgc() { return argc_; }
long long GetMsgNodeSize() { return msgnode_size_; }
long long GetWorkingMsgNodeSize() { return working_msgnode_size_; }
private:
bool Init();
void UnInit();
bool ParseOpt();
void InitLog();
void UnInitLog();
void Schedule();
void DispatchNetMsg();
private:
UserApp* user_app_ = nullptr;
int argc_ = 0;
char** argv_ = nullptr;
int exit_code_ = 0;
volatile bool terminated_ = false;
int zone_id_ = 0;
int node_id_ = 0;
int instance_id_ = 0;
std::set<int> flags_;
a8::Queue queue_;
std::atomic<long long> msgnode_size_ = {0};
std::atomic<long long> working_msgnode_size_ = {0};
std::shared_ptr<a8::uuid::SnowFlake> uuid_;
std::mutex *loop_mutex_ = nullptr;
std::condition_variable *loop_cond_ = nullptr;
};
}