diff --git a/server/mymangosd/CMakeLists.txt b/server/mymangosd/CMakeLists.txt index 773908a..c436512 100644 --- a/server/mymangosd/CMakeLists.txt +++ b/server/mymangosd/CMakeLists.txt @@ -9,11 +9,11 @@ else() message("debug mode") endif() -set(CMAKE_CXX_FLAGS_RELEASE "-Wall -g -fmodules-ts -std=c++20 -DNDEBUG -DDT_VIRTUAL_QUERYFILTER") +set(CMAKE_CXX_FLAGS_RELEASE "-Wall -g -std=c++17-DNDEBUG -DDT_VIRTUAL_QUERYFILTER") if (${ASAN}) - set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -fmodules-ts -std=c++20 -DMYDEBUG -DDT_VIRTUAL_QUERYFILTER -fsanitize=address -fno-omit-frame-pointer") + set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -std=c++17 -DMYDEBUG -DDT_VIRTUAL_QUERYFILTER -fsanitize=address -fno-omit-frame-pointer") else() - set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -fmodules-ts -std=c++20 -DMYDEBUG -DDT_VIRTUAL_QUERYFILTER") + set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -std=c++17 -DMYDEBUG -DDT_VIRTUAL_QUERYFILTER") endif() include_directories( diff --git a/server/mymangosd/main.cc b/server/mymangosd/main.cc index 7206870..e83f814 100644 --- a/server/mymangosd/main.cc +++ b/server/mymangosd/main.cc @@ -1,8 +1,6 @@ -import a8m.constant; +#include "pch.h" int main(int argc, char* argv[]) { - auto a = a8m::INVALID_SOCKET; - //a8m::INVALID_SOCKET = 100; return 0; } diff --git a/third_party/a8/a8/a8.h b/third_party/a8/a8/a8.h new file mode 100644 index 0000000..5f49fc1 --- /dev/null +++ b/third_party/a8/a8/a8.h @@ -0,0 +1,64 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#define A8_SAFE_DELETE(p) { if(p){delete(p); (p)=nullptr;} } +#define A8_DEFINE_RANGE_BIT(type, begin, end) ((((type)1) << (end + 1)) - 1) & (~((((type)1) << (begin + 0)) - 1)) + +#define A8_ABORT() do{printf("abort file:%s line:%d func:%s\n", __FILE__, __LINE__, __func__);fflush(stdout);fflush(stderr);abort();}while(0); + +#define A8_DECLARE_ENUM(E, ...) \ +enum E \ +{ \ +__VA_ARGS__ \ +}; \ +template<> constexpr const char* GetEnumString() { return #__VA_ARGS__; }; + +#define A8_DECLARE_CLASS_ENUM(E, T, ...) \ +enum class E : T \ +{ \ +__VA_ARGS__ \ +}; \ +template<> constexpr const char* GetEnumString() { return #__VA_ARGS__; }; + +#define A8_MAKE_ANON_STRUCT_SHARED(...) \ +[] () \ +{ \ +struct Context \ +{ \ + __VA_ARGS__ \ +}; \ +return std::make_shared();}() + +#define A8_MAKE_SMART_ANON_STRUCT_SHARED(...) \ +[] () \ +{ \ +struct Context : public std::enable_shared_from_this \ +{ \ + __VA_ARGS__; \ + std::function _destory_cb; \ + std::weak_ptr GetWp() { return shared_from_this();}; \ + ~Context() { if (_destory_cb) { _destory_cb(); };}; \ +}; \ +return std::make_shared();}() diff --git a/third_party/a8/a8/args.cc b/third_party/a8/a8/args.h similarity index 60% rename from third_party/a8/a8/args.cc rename to third_party/a8/a8/args.h index 7e8a763..b0abd0b 100644 --- a/third_party/a8/a8/args.cc +++ b/third_party/a8/a8/args.h @@ -1,11 +1,10 @@ -module; +#pragma once #include #include -export module a8m.args; +namespace a8 { -export namespace a8m { class Args { public: @@ -13,7 +12,7 @@ export namespace a8m { Args(std::vector args):args_(std::move(args)) {}; template - T Get(size_t index) const { /*return std::any_cast(args_.at(index));*/ return T(); }; + T Get(size_t index) const { return std::any_cast(args_.at(index)); }; private: std::vector args_; diff --git a/third_party/a8/a8/basehttpsession.cc b/third_party/a8/a8/basehttpsession.cc index ce3839e..f1b4c22 100644 --- a/third_party/a8/a8/basehttpsession.cc +++ b/third_party/a8/a8/basehttpsession.cc @@ -2,6 +2,7 @@ #include #include +#include namespace a8 { diff --git a/third_party/a8/a8/constant.cc b/third_party/a8/a8/constant.cc index 05ee099..e69de29 100644 --- a/third_party/a8/a8/constant.cc +++ b/third_party/a8/a8/constant.cc @@ -1,16 +0,0 @@ -module; - -export module a8m.constant; - -export namespace a8m { - constexpr int INVALID_FD() { return -1; } - constexpr int INVALID_SOCKET() { return -1; } - constexpr int INVALID_SOCKET_HANDLE() { return 0; } - - constexpr int TIMER_EXEC_EVENT() { return 1; } - constexpr int TIMER_DELETE_EVENT() { return 2; } - constexpr int TIMER_DESTORY_EVENT() { return 3; } - constexpr int TIMER_USER_EVENT() { return 66; } - - constexpr float A8_PI() { return 3.1415926f; } -} diff --git a/third_party/a8/a8/tcpclient.cc b/third_party/a8/a8/tcpclient.cc index cbc10e6..69f0af0 100644 --- a/third_party/a8/a8/tcpclient.cc +++ b/third_party/a8/a8/tcpclient.cc @@ -15,8 +15,6 @@ const int MAX_RECV_BUFFERSIZE = 1024 * 64; -import a8m.constant; - namespace a8 { TcpClient::TcpClient(const std::string& remote_ip, int remote_port) @@ -55,7 +53,7 @@ namespace a8 bool TcpClient::IsActive() { - return socket_ != a8m::INVALID_SOCKET(); + return socket_ != a8::INVALID_SOCKET; } bool TcpClient::Connected() @@ -105,7 +103,7 @@ namespace a8 bool TcpClient::ActiveStart() { socket_ = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - if (a8m::INVALID_SOCKET() == socket_) { + if (a8::INVALID_SOCKET == socket_) { if (on_error) { on_error(this, errno); } @@ -121,7 +119,7 @@ namespace a8 on_error(this, errno); } ::close(socket_); - socket_ = a8m::INVALID_SOCKET(); + socket_ = a8::INVALID_SOCKET; return false; } //set nodelay @@ -147,7 +145,7 @@ namespace a8 void TcpClient::ActiveStop() { connected_ = false; - if (socket_ != a8m::INVALID_SOCKET()) { + if (socket_ != a8::INVALID_SOCKET) { shutdown(socket_, 2); ::close(socket_); } @@ -157,7 +155,7 @@ namespace a8 delete worker_thread_; worker_thread_ = nullptr; } - socket_ = a8m::INVALID_SOCKET(); + socket_ = a8::INVALID_SOCKET; } void TcpClient::WorkerThreadProc() @@ -195,7 +193,7 @@ namespace a8 senderthread->join(); delete senderthread; senderthread = nullptr; - socket_ = a8m::INVALID_SOCKET(); + socket_ = a8::INVALID_SOCKET; } void TcpClient::SenderThreadProc() diff --git a/third_party/a8/a8/tcpclient.h b/third_party/a8/a8/tcpclient.h index 709b84d..5b8db51 100644 --- a/third_party/a8/a8/tcpclient.h +++ b/third_party/a8/a8/tcpclient.h @@ -12,8 +12,6 @@ namespace std #include -import a8m.constant; - namespace a8 { class TcpClient @@ -39,7 +37,7 @@ namespace a8 std::string remote_address_; int remote_port_ = 0; - volatile int socket_ = a8m::INVALID_SOCKET(); + volatile int socket_ = a8::INVALID_SOCKET; volatile bool connected_ = false; volatile bool sender_thread_shutdown_ = false; volatile bool worker_thread_shutdown_ = false; diff --git a/third_party/a8/a8/tcplistener.cc b/third_party/a8/a8/tcplistener.cc index 723d809..7766b61 100644 --- a/third_party/a8/a8/tcplistener.cc +++ b/third_party/a8/a8/tcplistener.cc @@ -15,7 +15,6 @@ #include #include -import a8m.constant; namespace a8 { @@ -23,7 +22,7 @@ namespace a8 struct TcpListenerImpl { a8::TcpListener* master = nullptr; - int listen_socket = a8m::INVALID_SOCKET(); + int listen_socket = a8::INVALID_SOCKET; std::thread* accept_thread = nullptr; std::thread* worker_thread = nullptr; volatile bool accept_thread_shutdown = false; @@ -35,7 +34,7 @@ namespace a8 unsigned short curr_socket_handle = 1000; unsigned short max_clients = 0xEFFF; a8::TcpSessionPool free_client_pool; - volatile int epoll_fd = a8m::INVALID_FD(); + volatile int epoll_fd = a8::INVALID_FD; #if 0 list_head session_list; #endif @@ -45,7 +44,7 @@ namespace a8 bool IsActive() { - return listen_socket != a8m::INVALID_SOCKET(); + return listen_socket != a8::INVALID_SOCKET; } void SetActive(bool active) @@ -68,7 +67,7 @@ namespace a8 bool ActiveStart() { listen_socket = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - if(listen_socket == a8m::INVALID_SOCKET()){ + if(listen_socket == a8::INVALID_SOCKET){ if (master->on_error){ master->on_error(master, a8::TCPLISTENER_E::TE_CREATE_ERR, errno); } @@ -85,7 +84,7 @@ namespace a8 master->on_error(master, a8::TCPLISTENER_E::TE_SETSOCKOPT_ERR, errno); } ::close(listen_socket); - listen_socket = a8m::INVALID_SOCKET(); + listen_socket = a8::INVALID_SOCKET; return false; } sockaddr_in sa; @@ -100,7 +99,7 @@ namespace a8 master->on_error(master, a8::TCPLISTENER_E::TE_BIND_ERR, errno); } ::close(listen_socket); - listen_socket = a8m::INVALID_SOCKET(); + listen_socket = a8::INVALID_SOCKET; return false; } if (::listen(listen_socket, max_clients) < 0) { @@ -108,11 +107,11 @@ namespace a8 master->on_error(master, a8::TCPLISTENER_E::TE_LISTEN_ERR, errno); } ::close(listen_socket); - listen_socket = a8m::INVALID_SOCKET(); + listen_socket = a8::INVALID_SOCKET; return false; } epoll_fd = ::epoll_create(max_clients); - assert(epoll_fd != a8m::INVALID_FD()); + assert(epoll_fd != a8::INVALID_FD); accept_thread_shutdown = false; accept_thread = new std::thread(&a8::TcpListenerImpl::AcceptThreadProc, this); return true; @@ -120,14 +119,14 @@ namespace a8 void ActiveStop() { - if (listen_socket != a8m::INVALID_SOCKET()) { + if (listen_socket != a8::INVALID_SOCKET) { ::shutdown(listen_socket, SHUT_RDWR); ::close(listen_socket); - listen_socket = a8m::INVALID_SOCKET(); + listen_socket = a8::INVALID_SOCKET; } - if(epoll_fd != a8m::INVALID_FD()) { + if(epoll_fd != a8::INVALID_FD) { ::close(epoll_fd); - epoll_fd = a8m::INVALID_FD(); + epoll_fd = a8::INVALID_FD; } if (accept_thread) { accept_thread_shutdown = true; @@ -305,13 +304,13 @@ namespace a8 sockaddr_in addr; socklen_t addr_len = sizeof(sockaddr_in); while (!accept_thread_shutdown) { - if (listen_socket == a8m::INVALID_SOCKET()) { + if (listen_socket == a8::INVALID_SOCKET) { break; } addr_len = sizeof(sockaddr_in); memset(&addr, 0, sizeof(addr)); int sock = ::accept(listen_socket, (sockaddr*)&addr, (socklen_t*)&addr_len); - if (sock != a8m::INVALID_SOCKET()) { + if (sock != a8::INVALID_SOCKET) { if (accept_thread_shutdown) { ::close(sock); } else { @@ -362,7 +361,7 @@ namespace a8 list_del_init(&session->session_entry); #endif client_hash.erase(itr); - if (session->Socket() != a8m::INVALID_SOCKET()) { + if (session->Socket() != a8::INVALID_SOCKET) { session->_ForceClose(); } client_handle_hash.erase(session->socket_handle); @@ -428,7 +427,7 @@ namespace a8 bool TcpListener::IsActive() { - return impl_->listen_socket != a8m::INVALID_SOCKET(); + return impl_->listen_socket != a8::INVALID_SOCKET; } bool TcpListener::SendClientMsg(unsigned short sockhandle, const char *buff, int buffLen) @@ -458,7 +457,7 @@ namespace a8 impl_->clients_mutex.lock(); a8::TcpSession *p = GetClientSession(sockhandle); if(p){ - if(p->Socket() != a8m::INVALID_SOCKET()){ + if(p->Socket() != a8::INVALID_SOCKET){ p->_ForceClose(); } } diff --git a/third_party/a8/a8/types.h b/third_party/a8/a8/types.h index 246eb4b..1504587 100644 --- a/third_party/a8/a8/types.h +++ b/third_party/a8/a8/types.h @@ -2,16 +2,7 @@ #include #include - -import a8m.args; - -namespace google -{ - namespace protobuf - { - class Message; - } -} +#include namespace a8 { @@ -26,14 +17,26 @@ namespace a8 SendQueueNode* next; }; - typedef std::function CommonCbProc; - typedef std::function CommonCbProcEx; + typedef std::function CommonCbProc; + typedef std::function CommonCbProcEx; typedef std::weak_ptr XTimerWp; + typedef std::function TimerCb; namespace reflect { class Class; } + + const int INVALID_FD = -1; + const int INVALID_SOCKET = -1; + const int INVALID_SOCKET_HANDLE = 0; + + const int TIMER_EXEC_EVENT = 1; + const int TIMER_DELETE_EVENT = 2; + const int TIMER_DESTORY_EVENT = 3; + const int TIMER_USER_EVENT = 66; + + const float A8_PI = 3.1415926f; }; template diff --git a/third_party/a8/a8/udplistener.cc b/third_party/a8/a8/udplistener.cc index 909ae9d..0196432 100644 --- a/third_party/a8/a8/udplistener.cc +++ b/third_party/a8/a8/udplistener.cc @@ -18,8 +18,6 @@ #include #endif -import a8m.constant; - namespace a8 { @@ -77,7 +75,7 @@ namespace a8 master->on_error(errno); } ::close(listen_socket); - listen_socket = a8m::INVALID_SOCKET(); + listen_socket = a8::INVALID_SOCKET; return false; } sockaddr_in sa; diff --git a/third_party/a8/a8/xtimer.cc b/third_party/a8/a8/xtimer.cc index fcfd918..1a68e4b 100644 --- a/third_party/a8/a8/xtimer.cc +++ b/third_party/a8/a8/xtimer.cc @@ -2,8 +2,6 @@ #include #include -import a8m.constant; - enum TimerType_e { kTimeOutTimer = 0, @@ -143,9 +141,9 @@ namespace a8 base_->timer_tick = get_tick_count_func_(context_); InternalSetInterval (gc_time_, - [this] (int event, const a8m::Args* args) + [this] (int event, const a8::Args* args) { - if (a8m::TIMER_EXEC_EVENT() == event) { + if (a8::TIMER_EXEC_EVENT == event) { int i = 0; while (!list_empty(&base_->free_timer) && base_->free_timer_num > cache_timer_num_ && i < 1000) { @@ -210,7 +208,7 @@ namespace a8 } } - void InternalFireEvent(xtimer_list* timer, int event, a8m::Args* args) + void InternalFireEvent(xtimer_list* timer, int event, a8::Args* args) { if (timer->cb) { timer->cb(event, args); @@ -245,11 +243,11 @@ namespace a8 #endif if (is_destory) { if (timer->cb) { - timer->cb(a8m::TIMER_DESTORY_EVENT(), nullptr); + timer->cb(a8::TIMER_DESTORY_EVENT, nullptr); } } else { if (timer->cb) { - timer->cb(a8m::TIMER_DELETE_EVENT(), nullptr); + timer->cb(a8::TIMER_DELETE_EVENT, nullptr); } } timer->cb = nullptr; @@ -344,7 +342,7 @@ namespace a8 timer = list_first_entry(head, struct xtimer_list,entry); base->running_timer = timer; if (timer->cb) { - timer->cb(a8m::TIMER_EXEC_EVENT(), nullptr); + timer->cb(a8::TIMER_EXEC_EVENT, nullptr); } if (base_->running_timer) { switch (timer->timer_type) { @@ -556,7 +554,7 @@ namespace a8 return result; } - void XTimer::FireEvent(XTimerWp& timer_wp, int event, a8m::Args* args) + void XTimer::FireEvent(XTimerWp& timer_wp, int event, a8::Args* args) { if (timer_wp.expired()) { abort(); diff --git a/third_party/a8/a8/xtimer.h b/third_party/a8/a8/xtimer.h index 2dcf60d..2e0b9f8 100644 --- a/third_party/a8/a8/xtimer.h +++ b/third_party/a8/a8/xtimer.h @@ -4,11 +4,8 @@ #include -import a8m.args; - namespace a8 { - typedef std::function TimerCb; typedef long long (*XGetTickCountFunc)(void*); class Attacher; @@ -31,7 +28,7 @@ namespace a8 XTimerWp SetIntervalWp(int expire_time, TimerCb cb); XTimerWp SetIntervalWpEx(int expire_time, TimerCb cb, Attacher* attacher); - void FireEvent(XTimerWp& timer_wp, int event, a8m::Args* args); + void FireEvent(XTimerWp& timer_wp, int event, a8::Args* args); void ModifyTime(XTimerWp& timer_wp, int expire_time); void ResetTimer(XTimerWp& timer_wp); void Delete(XTimerWp& timer_wp); diff --git a/third_party/f8/f8/app.cc b/third_party/f8/f8/app.cc index a36d18d..dcd4809 100644 --- a/third_party/f8/f8/app.cc +++ b/third_party/f8/f8/app.cc @@ -1,23 +1,21 @@ +#include #include #include #include #include #include +#include #include -#include -#include -#include #include -#include #include #include -#include #include #include #include +#include static const int MAX_ZONE_ID = 100; static const int MAX_NODE_ID = 8; @@ -34,227 +32,321 @@ static const char* const PROJ_LOG_FILENAME_FMT = "log_$pid_%Y%m%d.log"; namespace f8 { - bool App::Init() + class AppImpl { - signal(SIGPIPE, SIG_IGN); - srand(time(nullptr)); - nowtime_ = time(nullptr); + private: + UserApp* user_app_ = nullptr; + int argc_ = 0; + char** argv_ = nullptr; + int exit_code_ = 0; + volatile bool terminated_ = false; + int nowtime_ = 0; + long long max_run_delay_time_ = 0; - if (!ParseOpt()) { - exit_code_ = 1; - return false; + int zone_id_ = 0; + int node_id_ = 0; + int instance_id_ = 0; + std::set flags_; + + a8::Queue queue_; + std::atomic msgnode_size_ = {0}; + std::atomic working_msgnode_size_ = {0}; + + std::shared_ptr uuid_; + std::mutex *loop_mutex_ = nullptr; + std::condition_variable *loop_cond_ = nullptr; + + public: + + bool Init() + { + signal(SIGPIPE, SIG_IGN); + srand(time(nullptr)); + nowtime_ = time(nullptr); + + if (!ParseOpt()) { + exit_code_ = 1; + return false; + } + + uuid_ = std::make_shared(); + loop_mutex_ = new std::mutex(); + loop_cond_ = new std::condition_variable(); + + uuid_->SetMachineId((node_id_ - 1) * MAX_NODE_ID + instance_id_); + InitLog(); + f8::MsgQueue::Instance()->Init(); + f8::Timer::Instance()->Init(); + f8::TGLog::Instance()->Init(user_app_->GetPkgName(), false, 0); + f8::HttpClientPool::Instance()->Init(MAX_ALL_HTTP_NUM, MAX_SYS_HTTP_NUM, MAX_USER_HTTP_NUM); + user_app_->Init(); + return true; } - uuid_ = std::make_shared(); - loop_mutex_ = new std::mutex(); - loop_cond_ = new std::condition_variable(); + void UnInit() + { + user_app_->UnInit(); + f8::TGLog::Instance()->UnInit(); + f8::HttpClientPool::Instance()->UnInit(); + f8::Timer::Instance()->UnInit(); + f8::MsgQueue::Instance()->UnInit(); + UnInitLog(); - uuid_->SetMachineId((node_id_ - 1) * MAX_NODE_ID + instance_id_); - InitLog(); - f8::MsgQueue::Instance()->Init(); - f8::Timer::Instance()->Init(); - f8::TGLog::Instance()->Init(user_app_->GetPkgName(), false, 0); - f8::HttpClientPool::Instance()->Init(MAX_ALL_HTTP_NUM, MAX_SYS_HTTP_NUM, MAX_USER_HTTP_NUM); - user_app_->Init(); - return true; - } + delete loop_cond_; + loop_cond_ = nullptr; + delete loop_mutex_; + loop_mutex_ = nullptr; + } - void App::UnInit() - { - user_app_->UnInit(); - f8::TGLog::Instance()->UnInit(); - f8::HttpClientPool::Instance()->UnInit(); - f8::Timer::Instance()->UnInit(); - f8::MsgQueue::Instance()->UnInit(); - UnInitLog(); + int Run(int argc, char* argv[], UserApp* user_app) + { + argc_ = argc; + argv_ = argv; + user_app_ = user_app; + int delta_time = 0; + if (Init()) { + a8::tick_t last_stat_tick = a8::XGetTickCount(); + while (!Terminated()) { + a8::tick_t begin_tick = a8::XGetTickCount(); + nowtime_ = time(nullptr); + f8::Timer::Instance()->Update(); + f8::MsgQueue::Instance()->Update(); + DispatchNetMsg(); + user_app->Update(delta_time); + a8::tick_t end_tick = a8::XGetTickCount(); + if (end_tick - begin_tick > max_run_delay_time_) { + max_run_delay_time_ = end_tick - begin_tick; + } + Schedule(); + end_tick = a8::XGetTickCount(); + if (end_tick - last_stat_tick > 0) { + delta_time = end_tick - last_stat_tick; + last_stat_tick = end_tick; + } else { + delta_time = 0; + } + } + UnInit(); + } + return exit_code_; + } - delete loop_cond_; - loop_cond_ = nullptr; - delete loop_mutex_; - loop_mutex_ = nullptr; - } + bool HasFlag(int flag) + { + return flags_.find(flag) != flags_.end(); + } + + void NotifyLoopCond() + { + std::unique_lock lk(*loop_mutex_); + loop_cond_->notify_all(); + } + + long long NewNodeUuid() + { + return uuid_->Generate(); + } + + const std::string NewGlobalUuid() + { + std::string id = a8::Format("%d%d", {100 + zone_id_, uuid_->Generate()}); + return id; + } + + void Schedule() + { + std::unique_lock lk(*loop_mutex_); + bool has_task = false; + { + queue_.Fetch(); + if (!list_empty(queue_.GetWorkList())) { + has_task = true; + } + } + if (!has_task) { + has_task = user_app_->HasTask(); + } + if (!has_task) { + int sleep_time = f8::Timer::Instance()->GetIdleTime(); + loop_cond_->wait_for(lk, std::chrono::milliseconds(sleep_time)); + } else { + int sleep_time = 1; + loop_cond_->wait_for(lk, std::chrono::milliseconds(sleep_time)); + } + } + + void DispatchNetMsg() + { + queue_.Fetch(); + list_head* work_list = queue_.GetWorkList(); + while (!list_empty(work_list)){ + MsgHdr* hdr = list_first_entry(work_list, MsgHdr, entry); + list_del_init(&hdr->entry); + user_app_->DispatchSocketMsg(hdr); + --msgnode_size_; + MsgHdr::Destroy(hdr); + } + } + + void AddSocketMsg(int sockfrom, + int sockhandle, + long ip_saddr, + unsigned short msgid, + unsigned int seqid, + const char *msgbody, + int bodylen, + int tag) + { + char *p = (char*)malloc(sizeof(MsgHdr) + bodylen); + MsgHdr* hdr = (MsgHdr*)p; + hdr->sockfrom = sockfrom; + hdr->seqid = seqid; + hdr->msgid = msgid; + hdr->socket_handle = sockhandle; + hdr->ip_saddr = ip_saddr; + hdr->buf = p + sizeof(MsgHdr); + hdr->buflen = bodylen; + hdr->offset = 0; + hdr->hum = nullptr; + hdr->user_data = nullptr; + hdr->tag = tag; + if (bodylen > 0) { + memmove((void*)hdr->buf, msgbody, bodylen); + } + ++msgnode_size_; + queue_.Push(&hdr->entry); + NotifyLoopCond(); + } + + bool ParseOpt() + { + int ch = 0; + while ((ch = getopt(argc_, argv_, "z:n:i:f:")) != -1) { + switch (ch) { + case 'z': + { + zone_id_ = a8::XValue(optarg); + } + break; + case 'n': + { + node_id_ = a8::XValue(optarg); + } + break; + case 'i': + { + instance_id_ = a8::XValue(optarg); + } + break; + case 'f': + { + std::vector strings; + a8::Split(optarg, strings, ','); + for (auto& str : strings) { + flags_.insert(a8::XValue(str).GetInt()); + } + } + break; + } + } + if (zone_id_ <= 0) { + a8::XPrintf("启动失败,缺少-z参数\n", {}); + return false; + } else if (node_id_ > MAX_ZONE_ID) { + a8::XPrintf("启动失败,-z参数不能大于%d\n", {MAX_ZONE_ID}); + return false; + } else if (node_id_ <= 0) { + a8::XPrintf("启动失败,缺少-n参数\n", {}); + return false; + } else if (node_id_ > MAX_NODE_ID) { + a8::XPrintf("启动失败,-n参数不能大于%d\n", {MAX_NODE_ID}); + return false; + } else if (instance_id_ <= 0) { + a8::XPrintf("启动失败,缺少-i参数\n", {}); + return false; + } else if (instance_id_ > MAX_INSTANCE_ID) { + a8::XPrintf("启动失败,-i参数不能大于%d\n", {MAX_INSTANCE_ID}); + return false; + } + a8::XPrintf("starting zone_id:%d node_id:%d instance_id:%d pid:%d\n", + { + zone_id_, + node_id_, + instance_id_, + GetPid() + }); + return true; + } + + int GetPid() + { + return getpid(); + } + + void 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, {user_app_->GetPkgName()}); + std::string proj_log_root_dir = a8::Format + (PROJ_LOG_ROOT_FMT, {user_app_->GetPkgName()}); + std::string log_file_name = a8::Format + (PROJ_LOG_ROOT_FMT, {user_app_->GetPkgName()}) + "/" + filename_fmt; + + a8::MkDir(proj_root_dir); + a8::MkDir(proj_log_root_dir); + a8::XPrintf("log_file_name:%s\n", {log_file_name}); + 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 UnInitLog() + { + f8::UdpLog::Instance()->UnInit(); + } + + bool Terminated() + { + return terminated_; + }; + + }; int App::Run(int argc, char* argv[], UserApp* user_app) { - argc_ = argc; - argv_ = argv; - user_app_ = user_app; - int delta_time = 0; - if (Init()) { - a8::tick_t last_stat_tick = a8::XGetTickCount(); - while (!Terminated()) { - a8::tick_t begin_tick = a8::XGetTickCount(); - nowtime_ = time(nullptr); - f8::Timer::Instance()->Update(); - f8::MsgQueue::Instance()->Update(); - DispatchNetMsg(); - user_app->Update(delta_time); - a8::tick_t end_tick = a8::XGetTickCount(); - if (end_tick - begin_tick > max_run_delay_time_) { - max_run_delay_time_ = end_tick - begin_tick; - } - Schedule(); - end_tick = a8::XGetTickCount(); - if (end_tick - last_stat_tick > 0) { - delta_time = end_tick - last_stat_tick; - last_stat_tick = end_tick; - } else { - delta_time = 0; - } - } - UnInit(); - } - return exit_code_; - } - - bool App::ParseOpt() - { - int ch = 0; - while ((ch = getopt(argc_, argv_, "z:n:i:f:")) != -1) { - switch (ch) { - case 'z': - { - zone_id_ = a8::XValue(optarg); - } - break; - case 'n': - { - node_id_ = a8::XValue(optarg); - } - break; - case 'i': - { - instance_id_ = a8::XValue(optarg); - } - break; - case 'f': - { - std::vector strings; - a8::Split(optarg, strings, ','); - for (auto& str : strings) { - flags_.insert(a8::XValue(str).GetInt()); - } - } - break; - } - } - if (zone_id_ <= 0) { - a8::XPrintf("启动失败,缺少-z参数\n", {}); - return false; - } else if (node_id_ > MAX_ZONE_ID) { - a8::XPrintf("启动失败,-z参数不能大于%d\n", {MAX_ZONE_ID}); - return false; - } else if (node_id_ <= 0) { - a8::XPrintf("启动失败,缺少-n参数\n", {}); - return false; - } else if (node_id_ > MAX_NODE_ID) { - a8::XPrintf("启动失败,-n参数不能大于%d\n", {MAX_NODE_ID}); - return false; - } else if (instance_id_ <= 0) { - a8::XPrintf("启动失败,缺少-i参数\n", {}); - return false; - } else if (instance_id_ > MAX_INSTANCE_ID) { - a8::XPrintf("启动失败,-i参数不能大于%d\n", {MAX_INSTANCE_ID}); - return false; - } - a8::XPrintf("starting zone_id:%d node_id:%d instance_id:%d pid:%d\n", - { - zone_id_, - node_id_, - instance_id_, - GetPid() - }); - return true; + impl_ = std::make_unique(); + return impl_->Run(argc, argv, user_app); } bool App::HasFlag(int flag) { - return flags_.find(flag) != flags_.end(); + return impl_->HasFlag(flag); } void App::NotifyLoopCond() { - std::unique_lock lk(*loop_mutex_); - loop_cond_->notify_all(); + impl_->NotifyLoopCond(); } long long App::NewNodeUuid() { - return uuid_->Generate(); + return impl_->NewNodeUuid(); } const std::string App::NewGlobalUuid() { - std::string id = a8::Format("%d%d", {100 + zone_id_, uuid_->Generate()}); - return id; + return impl_->NewGlobalUuid(); } 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, {user_app_->GetPkgName()}); - std::string proj_log_root_dir = a8::Format - (PROJ_LOG_ROOT_FMT, {user_app_->GetPkgName()}); - std::string log_file_name = a8::Format - (PROJ_LOG_ROOT_FMT, {user_app_->GetPkgName()}) + "/" + filename_fmt; - - a8::MkDir(proj_root_dir); - a8::MkDir(proj_log_root_dir); - a8::XPrintf("log_file_name:%s\n", {log_file_name}); - 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 lk(*loop_mutex_); - bool has_task = false; - { - queue_.Fetch(); - if (!list_empty(queue_.GetWorkList())) { - has_task = true; - } - } - if (!has_task) { - has_task = user_app_->HasTask(); - } - if (!has_task) { - int sleep_time = f8::Timer::Instance()->GetIdleTime(); - loop_cond_->wait_for(lk, std::chrono::milliseconds(sleep_time)); - } else { - int sleep_time = 1; - loop_cond_->wait_for(lk, std::chrono::milliseconds(sleep_time)); - } - } - - void App::DispatchNetMsg() - { - queue_.Fetch(); - list_head* work_list = queue_.GetWorkList(); - while (!list_empty(work_list)){ - MsgHdr* hdr = list_first_entry(work_list, MsgHdr, entry); - list_del_init(&hdr->entry); - user_app_->DispatchSocketMsg(hdr); - --msgnode_size_; - MsgHdr::Destroy(hdr); - } + return impl_->GetPid(); } void App::AddSocketMsg(int sockfrom, @@ -266,25 +358,14 @@ namespace f8 int bodylen, int tag) { - char *p = (char*)malloc(sizeof(MsgHdr) + bodylen); - MsgHdr* hdr = (MsgHdr*)p; - hdr->sockfrom = sockfrom; - hdr->seqid = seqid; - hdr->msgid = msgid; - hdr->socket_handle = sockhandle; - hdr->ip_saddr = ip_saddr; - hdr->buf = p + sizeof(MsgHdr); - hdr->buflen = bodylen; - hdr->offset = 0; - hdr->hum = nullptr; - hdr->user_data = nullptr; - hdr->tag = tag; - if (bodylen > 0) { - memmove((void*)hdr->buf, msgbody, bodylen); - } - ++msgnode_size_; - queue_.Push(&hdr->entry); - NotifyLoopCond(); + impl_->AddSocketMsg(sockfrom, + sockhandle, + ip_saddr, + msgid, + seqid, + msgbody, + bodylen, + tag); } } diff --git a/third_party/f8/f8/app.h b/third_party/f8/f8/app.h index e142c01..19b57af 100644 --- a/third_party/f8/f8/app.h +++ b/third_party/f8/f8/app.h @@ -1,18 +1,5 @@ #pragma once -#include - -#include -#include - -namespace a8 -{ - namespace uuid - { - class SnowFlake; - } -} - namespace f8 { @@ -42,13 +29,13 @@ namespace f8 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() { return nowtime_; }; + int GetZoneId(); + int GetNodeId(); + int GetInstanceId(); + int GetNowTime(); int GetPid(); - auto Terminated() { return terminated_; }; - void Terminate() { terminated_ = true; }; + bool Terminated(); + void Terminate(); void AddSocketMsg(int sockfrom, int sockhandle, long ip_saddr, @@ -57,43 +44,15 @@ namespace f8 const char *msgbody, int bodylen, int tag); - char** GetArgv() { return argv_; } - int GetArgc() { return argc_; } - long long GetMsgNodeSize() { return msgnode_size_; } - long long GetWorkingMsgNodeSize() { return working_msgnode_size_; } - long long GetMaxRunDelayTime() { return max_run_delay_time_; } - void ResetMaxRunDelayTime() { max_run_delay_time_ = 0; } + char** GetArgv(); + int GetArgc(); + long long GetMsgNodeSize(); + long long GetWorkingMsgNodeSize(); + long long GetMaxRunDelayTime(); + void ResetMaxRunDelayTime(); 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 nowtime_ = 0; - long long max_run_delay_time_ = 0; - - int zone_id_ = 0; - int node_id_ = 0; - int instance_id_ = 0; - std::set flags_; - - a8::Queue queue_; - std::atomic msgnode_size_ = {0}; - std::atomic working_msgnode_size_ = {0}; - - std::shared_ptr uuid_; - std::mutex *loop_mutex_ = nullptr; - std::condition_variable *loop_cond_ = nullptr; + std::unique_ptr impl_; }; } diff --git a/third_party/f8/f8/dataset.h b/third_party/f8/f8/dataset.h new file mode 100644 index 0000000..5b9d6c6 --- /dev/null +++ b/third_party/f8/f8/dataset.h @@ -0,0 +1,9 @@ +#pragma once + +namespace f8 +{ + class DataSet + { + + }; +} diff --git a/third_party/f8/f8/dbpool.cc b/third_party/f8/f8/dbpool.cc index faf52b5..523b2bb 100644 --- a/third_party/f8/f8/dbpool.cc +++ b/third_party/f8/f8/dbpool.cc @@ -1,25 +1,18 @@ +#include #include -#include - -#include -#include #include #include #include -#include -#include #include #include #include #include #include -#include -#if 0 namespace f8 { @@ -41,10 +34,14 @@ namespace f8 std::vector _sql_params; a8::XObject conn_info; #endif + #if 0 a8::XParams param; + #endif time_t add_time = 0; + #if 0 AsyncDBOnOkFunc on_ok = nullptr; AsyncDBOnErrorFunc on_error = nullptr; + #endif f8::Attacher timer_attacher; }; @@ -68,6 +65,7 @@ namespace f8 void Init() { + #if 0 loop_mutex_ = new std::mutex(); loop_cond_ = new std::condition_variable(); @@ -78,10 +76,12 @@ namespace f8 work_node_ = nullptr; msg_mutex_ = new std::mutex(); work_thread_ = new std::thread(&DBThread::WorkThreadProc, this); + #endif } void UnInit() { + #if 0 terminated_ = true; work_thread_->join(); delete work_thread_; @@ -95,10 +95,12 @@ namespace f8 delete loop_mutex_; loop_mutex_ = nullptr; + #endif } void AddAsyncQuery(AsyncQueryNode* p) { + #if 0 std::unique_lock lk(*loop_mutex_); msg_mutex_->lock(); if (bot_node_) { @@ -110,12 +112,14 @@ namespace f8 } msg_mutex_->unlock(); loop_cond_->notify_all(); + #endif } private: void WorkThreadProc() { + #if 0 mysql_thread_init(); while (!terminated_) { @@ -135,10 +139,12 @@ namespace f8 last_conn_ = nullptr; } mysql_thread_end(); + #endif } void CheckDB() { + #if 0 if (a8::XGetTickCount() - last_checkdb_tick_ < 1000 * 60 * 5) { return; } @@ -156,10 +162,12 @@ namespace f8 #endif } } + #endif } void ProcessMsg() { + #if 0 if (!work_node_ && top_node_) { msg_mutex_->lock(); work_node_ = top_node_; @@ -174,10 +182,12 @@ namespace f8 ++DBPool::Instance()->exec_query_num; delete pdelnode; } + #endif } void WaitLoopCond() { + #if 0 std::unique_lock lk(*loop_mutex_); { msg_mutex_->lock(); @@ -191,10 +201,12 @@ namespace f8 if (!work_node_) { loop_cond_->wait_for(lk, std::chrono::seconds(10)); } + #endif } bool ReCreateConn(a8::XObject& conn_info) { + #if 0 if (last_query_) { delete last_query_; last_query_ = nullptr; @@ -214,11 +226,13 @@ namespace f8 )) { f8::InitMysqlConnection(last_query_); } + #endif return true; } bool NeedReCreateConn(a8::XObject& conn_info) { + #if 0 if (!last_conn_) { return true; } @@ -230,11 +244,13 @@ namespace f8 ) { return false; } + #endif return true; } void ProcAsyncQuery(AsyncQueryNode* node) { + #if 0 if (NeedReCreateConn(node->conn_info)) { ReCreateConn(node->conn_info); } @@ -322,6 +338,7 @@ namespace f8 } break; } + #endif } private: @@ -333,7 +350,7 @@ namespace f8 a8::mysql::Query* last_query_ = nullptr; long long last_checkdb_tick_ = 0; - std::thread *work_thread_ = nullptr; + //std::thread *work_thread_ = nullptr; AsyncQueryNode *top_node_ = nullptr; AsyncQueryNode *bot_node_ = nullptr; AsyncQueryNode *work_node_ = nullptr; @@ -375,6 +392,7 @@ namespace f8 void AsyncSqlOnOk(long long seqid, DataSet* data_set) { + #if 0 AsyncQueryRequest* request = GetAsyncQueryRequest(seqid); if (!request) { return; @@ -384,10 +402,12 @@ namespace f8 } async_query_hash.erase(seqid); delete request; + #endif } void AsyncSqlOnError(long long seqid, int errcode, const std::string& errmsg) { + #if 0 AsyncQueryRequest* request = GetAsyncQueryRequest(seqid); if (!request) { return; @@ -400,8 +420,10 @@ namespace f8 } async_query_hash.erase(seqid); delete request; + #endif } + #if 0 void InternalExecAsyncSql(int exec_type, a8::XObject& conn_info, const char* querystr, std::vector& args, a8::XParams& param, AsyncDBOnOkFunc on_ok, AsyncDBOnErrorFunc on_error, long long hash_code) @@ -462,6 +484,7 @@ namespace f8 &p->timer_attacher); ++DBPool::Instance()->total_query_num; } + #endif private: @@ -488,7 +511,7 @@ namespace f8 void DBPool::Init() { - impl_ = new DBPoolImpl(); + impl_ = std::make_unique(); impl_->Init(); MsgQueue::Instance()->RegisterCallBack (IM_DbPool, @@ -512,7 +535,6 @@ namespace f8 void DBPool::UnInit() { impl_->UnInit(); - delete impl_; impl_ = nullptr; } @@ -521,17 +543,20 @@ namespace f8 impl_->SetThreadNum(thread_num); } - void DBPool::ExecAsyncQuery(a8::XObject conn_info, const char* querystr, std::vector args, - a8::XParams param, AsyncDBOnOkFunc on_ok, AsyncDBOnErrorFunc on_error, long long hash_code) + void DBPool::ExecAsyncQuery(int ds, + const std::string sql, + std::vector args, + f8::DbQueryResultCb cb) { - impl_->InternalExecAsyncSql(0, conn_info, querystr, args, param, on_ok, on_error, hash_code); + } - void DBPool::ExecAsyncScript(a8::XObject conn_info, const char* querystr, std::vector args, - a8::XParams param, AsyncDBOnOkFunc on_ok, AsyncDBOnErrorFunc on_error, long long hash_code) + void DBPool::ExecAsyncScript(int ds, + const std::string sql, + std::vector args, + f8::DbExecResultCb cb) { - impl_->InternalExecAsyncSql(1, conn_info, querystr, args, param, on_ok, on_error, hash_code); + } } -#endif diff --git a/third_party/f8/f8/dbpool.h b/third_party/f8/f8/dbpool.h index 589f7b0..4b436f9 100644 --- a/third_party/f8/f8/dbpool.h +++ b/third_party/f8/f8/dbpool.h @@ -1,41 +1,57 @@ #pragma once -#include +#include -#if 0 namespace f8 { - typedef std::vector> DataSet; - typedef void (*AsyncDBOnOkFunc)(a8::XParams& param, const DataSet* data_set); - typedef void (*AsyncDBOnErrorFunc)(a8::XParams& param, int error_code, const std::string& error_msg); - class DBPoolImpl; + class DbOpResult + { + public: + virtual int GetErrCode() = 0; + virtual std::string GetErrMsg() = 0; + }; + + class DbQueryResult : public DbOpResult + { + public: + virtual std::shared_ptr GetDataSet() = 0; + }; + + class DbExecResult : public DbOpResult + { + public: + virtual long long GetLastInsertId() = 0; + virtual long long GetRowsAffected() = 0; + }; + + typedef std::function)> DbQueryResultCb; + typedef std::function)> DbExecResultCb; + class DBPool : public a8::Singleton { private: DBPool() {}; friend class a8::Singleton; - public: - AsyncDBOnErrorFunc on_dberror = nullptr; - std::atomic total_query_num = {0}; - std::atomic exec_query_num = {0}; - std::atomic run_loop_num = {0}; - public: void Init(); void UnInit(); void SetThreadNum(int thread_num); //执行异步并行查询 - void ExecAsyncQuery(a8::XObject conn_info, const char* querystr, std::vector args, - a8::XParams param, AsyncDBOnOkFunc on_ok, AsyncDBOnErrorFunc on_error, long long hash_code); + void ExecAsyncQuery(int ds, + const std::string sql, + std::vector args, + f8::DbQueryResultCb cb); //执行异步并行sql - void ExecAsyncScript(a8::XObject conn_info, const char* querystr, std::vector args, - a8::XParams param, AsyncDBOnOkFunc on_ok, AsyncDBOnErrorFunc on_error, long long hash_code); + void ExecAsyncScript(int ds, + const std::string sql, + std::vector args, + f8::DbExecResultCb cb); private: - DBPoolImpl* impl_ = nullptr; + std::unique_ptr impl_; }; + } -#endif diff --git a/third_party/f8/f8/dynmodule.cc b/third_party/f8/f8/dynmodule.cc index a3e6281..5f435c0 100644 --- a/third_party/f8/f8/dynmodule.cc +++ b/third_party/f8/f8/dynmodule.cc @@ -1,4 +1,5 @@ -#include "f8/dynmodule.h" +#include +#include namespace f8 { diff --git a/third_party/f8/f8/f8.h b/third_party/f8/f8/f8.h new file mode 100644 index 0000000..92fa3cc --- /dev/null +++ b/third_party/f8/f8/f8.h @@ -0,0 +1,5 @@ +#pragma once + +#include +#include +#include diff --git a/third_party/f8/f8/httpclientpool.cc b/third_party/f8/f8/httpclientpool.cc index 9661170..e958504 100644 --- a/third_party/f8/f8/httpclientpool.cc +++ b/third_party/f8/f8/httpclientpool.cc @@ -1,7 +1,6 @@ +#include #include -#include - #include #include #include @@ -27,8 +26,6 @@ static const int AHE_NO_ERROR = 0; static const int AHE_NO_CONN = 1; -import a8m.args; - namespace f8 { @@ -220,7 +217,7 @@ namespace f8 if (xobj->ReadFromJsonString(response)) { f8::MsgQueue::Instance()->PostMsg (IM_HttpClientPool, - a8m::Args + a8::Args ( { node->context_id, @@ -231,7 +228,7 @@ namespace f8 } else { f8::MsgQueue::Instance()->PostMsg (IM_HttpClientPool, - a8m::Args + a8::Args ( { node->context_id, @@ -244,7 +241,7 @@ namespace f8 } else { f8::MsgQueue::Instance()->PostMsg (IM_HttpClientPool, - a8m::Args + a8::Args ( { node->context_id, @@ -406,7 +403,7 @@ namespace f8 } f8::Timer::Instance()->SetTimeoutEx (1000 * 10, - [p] (int event, const a8m::Args* args) + [p] (int event, const a8::Args* args) { }, @@ -449,7 +446,7 @@ namespace f8 impl_->Init(); MsgQueue::Instance()->RegisterCallBack (IM_HttpClientPool, - [] (const a8m::Args& args) + [] (const a8::Args& args) { --(HttpClientPool::Instance()->impl_->pending_num); #if 1 diff --git a/third_party/f8/f8/httpclientpool.h b/third_party/f8/f8/httpclientpool.h index 289f03f..c0e6e80 100644 --- a/third_party/f8/f8/httpclientpool.h +++ b/third_party/f8/f8/httpclientpool.h @@ -1,10 +1,5 @@ #pragma once -#include - -#include -#include - namespace f8 { diff --git a/third_party/f8/f8/internal/pch.h b/third_party/f8/f8/internal/pch.h new file mode 100644 index 0000000..e0896a8 --- /dev/null +++ b/third_party/f8/f8/internal/pch.h @@ -0,0 +1,3 @@ +#pragma once + +#include diff --git a/third_party/f8/f8/jsonhttprequest.cc b/third_party/f8/f8/jsonhttprequest.cc index eb1a27c..2163639 100644 --- a/third_party/f8/f8/jsonhttprequest.cc +++ b/third_party/f8/f8/jsonhttprequest.cc @@ -1,9 +1,8 @@ +#include #include -#include #include -#include #include namespace f8 @@ -37,7 +36,7 @@ namespace f8 if (cb_) { std::string response; resp_xobj_->ToJsonStr(response); - cb_(a8m::Args({a8::HttpResponse(response)})); + cb_(a8::Args({a8::HttpResponse(response)})); } resped_ = true; } diff --git a/third_party/f8/f8/jsonhttprequest.h b/third_party/f8/f8/jsonhttprequest.h index 5f59640..e024acb 100644 --- a/third_party/f8/f8/jsonhttprequest.h +++ b/third_party/f8/f8/jsonhttprequest.h @@ -1,12 +1,7 @@ #pragma once -#include - -import a8m.args; - namespace a8 { - class XObject; class MutableXObject; } diff --git a/third_party/f8/f8/jsonlog.cc b/third_party/f8/f8/jsonlog.cc index c2a5a3d..0f71157 100644 --- a/third_party/f8/f8/jsonlog.cc +++ b/third_party/f8/f8/jsonlog.cc @@ -1,11 +1,9 @@ +#include #include #include #include #include -#include - -#include #include namespace f8 diff --git a/third_party/f8/f8/jsonlog.h b/third_party/f8/f8/jsonlog.h index 4b5826f..22e0024 100644 --- a/third_party/f8/f8/jsonlog.h +++ b/third_party/f8/f8/jsonlog.h @@ -1,8 +1,5 @@ #pragma once -#include -#include - namespace f8 { diff --git a/third_party/f8/f8/msgqueue.cc b/third_party/f8/f8/msgqueue.cc index 77b96e6..3c8fb29 100644 --- a/third_party/f8/f8/msgqueue.cc +++ b/third_party/f8/f8/msgqueue.cc @@ -1,12 +1,7 @@ +#include + #include - #include -#include - - -#include - -#include #include @@ -21,10 +16,10 @@ namespace f8 struct IMMsgNode { int msgid; - const a8m::Args args; + const a8::Args args; IMMsgNode* next = nullptr; - IMMsgNode(const a8m::Args& args1):args(std::move(args1)) + IMMsgNode(const a8::Args& args1):args(std::move(args1)) { } @@ -83,7 +78,7 @@ namespace f8 } } - void ProcessMsg(int msgid, const a8m::Args& args) + void ProcessMsg(int msgid, const a8::Args& args) { auto itr = msg_handlers.find(msgid); if (itr != msg_handlers.end()) { @@ -113,7 +108,7 @@ namespace f8 return &node->entry; } - void PostMsg(int msgid, const a8m::Args args) + void PostMsg(int msgid, const a8::Args args) { IMMsgNode *p = new IMMsgNode(args); p->msgid = msgid; @@ -183,7 +178,7 @@ namespace f8 return imp_->RegisterCallBack(msgid, handle_func); } - void MsgQueue::PostMsg(int msgid, const a8m::Args args) + void MsgQueue::PostMsg(int msgid, const a8::Args args) { imp_->PostMsg(msgid, std::move(args)); } diff --git a/third_party/f8/f8/msgqueue.h b/third_party/f8/f8/msgqueue.h index b887730..bbea8f9 100644 --- a/third_party/f8/f8/msgqueue.h +++ b/third_party/f8/f8/msgqueue.h @@ -1,14 +1,8 @@ #pragma once -#include - -#include - -import a8m.args; - namespace f8 { - typedef std::function MsgHandleFunc; + typedef std::function MsgHandleFunc; typedef list_head* CallBackHandle; class MsgQueue : public a8::Singleton @@ -26,7 +20,7 @@ namespace f8 CallBackHandle RegisterCallBack(int msgid, MsgHandleFunc cb); void RemoveCallBack(CallBackHandle handle); - void PostMsg(int msgid, const a8m::Args args); + void PostMsg(int msgid, const a8::Args args); private: std::shared_ptr imp_; diff --git a/third_party/f8/f8/netmsghandler.cc b/third_party/f8/f8/netmsghandler.cc index 97b6f77..a2eed8c 100644 --- a/third_party/f8/f8/netmsghandler.cc +++ b/third_party/f8/f8/netmsghandler.cc @@ -1,7 +1,7 @@ +#include #include #include -#include #include #include #include diff --git a/third_party/f8/f8/netmsghandler.h b/third_party/f8/f8/netmsghandler.h index 3cf18cf..e6b418b 100644 --- a/third_party/f8/f8/netmsghandler.h +++ b/third_party/f8/f8/netmsghandler.h @@ -1,7 +1,5 @@ #pragma once -#include - #include #include diff --git a/third_party/f8/f8/protoutils.cc b/third_party/f8/f8/protoutils.cc index c0106e8..d34b1c7 100644 --- a/third_party/f8/f8/protoutils.cc +++ b/third_party/f8/f8/protoutils.cc @@ -1,6 +1,6 @@ -#include +#include -#include "f8/protoutils.h" +#include #include #include diff --git a/third_party/f8/f8/protoutils.h b/third_party/f8/f8/protoutils.h index 9ac3ffd..89c613b 100644 --- a/third_party/f8/f8/protoutils.h +++ b/third_party/f8/f8/protoutils.h @@ -7,15 +7,6 @@ namespace a8 class AsioTcpClient; } -namespace google { - namespace protobuf { - class Message; - } -} - -#include -#include - class Player; namespace f8 { diff --git a/third_party/f8/f8/scriptengine.cc b/third_party/f8/f8/scriptengine.cc index 8a4d656..7fce24f 100644 --- a/third_party/f8/f8/scriptengine.cc +++ b/third_party/f8/f8/scriptengine.cc @@ -1,3 +1,4 @@ +#include #include #include diff --git a/third_party/f8/f8/tglog.cc b/third_party/f8/f8/tglog.cc index a12d0b9..b9e7aa4 100644 --- a/third_party/f8/f8/tglog.cc +++ b/third_party/f8/f8/tglog.cc @@ -1,3 +1,4 @@ +#include #include #include diff --git a/third_party/f8/f8/tglog.h b/third_party/f8/f8/tglog.h index 5359cc8..8e687fc 100644 --- a/third_party/f8/f8/tglog.h +++ b/third_party/f8/f8/tglog.h @@ -1,7 +1,5 @@ #pragma once -#include - namespace f8 { struct TGLogImpl; diff --git a/third_party/f8/f8/timer.cc b/third_party/f8/f8/timer.cc index 904f15d..e2230e3 100644 --- a/third_party/f8/f8/timer.cc +++ b/third_party/f8/f8/timer.cc @@ -1,24 +1,35 @@ -#include +#include -#include +#include #include - namespace f8 { + class TimerImpl + { + public: + bool initialized_ = false; + a8::XTimer xtimer_; + }; Attacher::Attacher() { - p_.SetOwner(&f8::Timer::Instance()->xtimer_); + real_obj_.SetOwner(&f8::Timer::Instance()->impl_->xtimer_); if (!f8::Timer::Instance()->Initialized()) { abort(); } } + void Attacher::ClearTimerList() + { + real_obj_.ClearTimerList(); + } + void Timer::Init() { - initialized_ = true; - xtimer_.Init + impl_ = std::make_shared(); + impl_->initialized_ = true; + impl_->xtimer_.Init ( [] (void* context) { @@ -32,92 +43,92 @@ namespace f8 void Timer::UnInit() { - initialized_ = false; + impl_->initialized_ = false; } bool Timer::Initialized() { - return initialized_; + return impl_->initialized_; } void Timer::Update() { - xtimer_.Update(); + impl_->xtimer_.Update(); } void Timer::SetTimeout(int expire_time, a8::TimerCb cb) { - xtimer_.SetTimeout(expire_time, cb); + impl_->xtimer_.SetTimeout(expire_time, cb); } void Timer::SetTimeoutEx(int expire_time, a8::TimerCb cb, Attacher* attacher) { - xtimer_.SetTimeoutEx(expire_time, cb, &attacher->p_); + impl_->xtimer_.SetTimeoutEx(expire_time, cb, &attacher->real_obj_); } TimerWp Timer::SetTimeoutWp(int expire_time, a8::TimerCb cb) { - return TimerWp(xtimer_.SetTimeoutWp(expire_time, cb)); + return TimerWp(impl_->xtimer_.SetTimeoutWp(expire_time, cb)); } TimerWp Timer::SetTimeoutWpEx(int expire_time, a8::TimerCb cb, Attacher* attacher) { - return TimerWp(xtimer_.SetTimeoutWpEx(expire_time, cb, &attacher->p_)); + return TimerWp(impl_->xtimer_.SetTimeoutWpEx(expire_time, cb, &attacher->real_obj_)); } void Timer::SetInterval(int expire_time, a8::TimerCb cb) { - xtimer_.SetInterval(expire_time, cb); + impl_->xtimer_.SetInterval(expire_time, cb); } void Timer::SetIntervalEx(int expire_time, a8::TimerCb cb, Attacher* attacher) { - xtimer_.SetIntervalEx(expire_time, cb, &attacher->p_); + impl_->xtimer_.SetIntervalEx(expire_time, cb, &attacher->real_obj_); } TimerWp Timer::SetIntervalWp(int expire_time, a8::TimerCb cb) { - return TimerWp(xtimer_.SetIntervalWp(expire_time, cb)); + return TimerWp(impl_->xtimer_.SetIntervalWp(expire_time, cb)); } TimerWp Timer::SetIntervalWpEx(int expire_time, a8::TimerCb cb, Attacher* attacher) { - return TimerWp(xtimer_.SetIntervalWpEx(expire_time, cb, &attacher->p_)); + return TimerWp(impl_->xtimer_.SetIntervalWpEx(expire_time, cb, &attacher->real_obj_)); } - void Timer::FireEvent(TimerWp& timer_wp, int event, a8m::Args* args) + void Timer::FireEvent(TimerWp& timer_wp, int event, a8::Args* args) { - xtimer_.FireEvent(timer_wp.p_, event, args); + impl_->xtimer_.FireEvent(timer_wp.real_obj_, event, args); } void Timer::ModifyTime(TimerWp& timer_wp, int expire_time) { - xtimer_.ModifyTime(timer_wp.p_, expire_time); + impl_->xtimer_.ModifyTime(timer_wp.real_obj_, expire_time); } void Timer::Delete(TimerWp& timer_wp) { - xtimer_.Delete(timer_wp.p_); + impl_->xtimer_.Delete(timer_wp.real_obj_); } long long Timer::GetRemainTime(TimerWp& timer_wp) { - return xtimer_.GetRemainTime(timer_wp.p_); + return impl_->xtimer_.GetRemainTime(timer_wp.real_obj_); } void Timer::DeleteCurrentTimer() { - xtimer_.DeleteCurrentTimer(); + impl_->xtimer_.DeleteCurrentTimer(); } bool Timer::IsRunning() { - return xtimer_.IsRunning(); + return impl_->xtimer_.IsRunning(); } long long Timer::GetIdleTime() { - return xtimer_.GetIdleTime(); + return impl_->xtimer_.GetIdleTime(); } } diff --git a/third_party/f8/f8/timer.h b/third_party/f8/f8/timer.h index 8f9ae91..6057920 100644 --- a/third_party/f8/f8/timer.h +++ b/third_party/f8/f8/timer.h @@ -1,21 +1,16 @@ #pragma once -#include -#include -#include - namespace f8 { class TimerWp { public: - TimerWp(a8::XTimerWp wp): p_(wp) {}; + TimerWp(a8::XTimerWp wp): real_obj_(wp) {}; TimerWp() {}; - bool expired() { return p_.expired();}; - auto lock() { return p_.lock(); }; + bool expired() { return real_obj_.expired();}; private: - a8::XTimerWp p_; + a8::XTimerWp real_obj_; friend class Timer; }; @@ -23,10 +18,10 @@ namespace f8 { public: Attacher(); - void ClearTimerList() { p_.ClearTimerList(); }; + void ClearTimerList(); private: - a8::Attacher p_; + a8::Attacher real_obj_; friend class Timer; }; @@ -53,7 +48,7 @@ namespace f8 TimerWp SetIntervalWp(int expire_time, a8::TimerCb cb); TimerWp SetIntervalWpEx(int expire_time, a8::TimerCb cb, Attacher* attacher); - void FireEvent(TimerWp& timer_wp, int event, a8m::Args* args); + void FireEvent(TimerWp& timer_wp, int event, a8::Args* args); void ModifyTime(TimerWp& timer_wp, int expire_time); void Delete(TimerWp& timer_wp); long long GetRemainTime(TimerWp& timer_wp); @@ -62,8 +57,7 @@ namespace f8 long long GetIdleTime(); private: - bool initialized_ = false; - a8::XTimer xtimer_; + std::shared_ptr impl_; friend class Attacher; }; diff --git a/third_party/f8/f8/types.cc b/third_party/f8/f8/types.cc index 270c0ab..f8eb3bb 100644 --- a/third_party/f8/f8/types.cc +++ b/third_party/f8/f8/types.cc @@ -1,6 +1,6 @@ +#include #include -#include #include namespace f8 diff --git a/third_party/f8/f8/types.h b/third_party/f8/f8/types.h index 68eafdb..88d7153 100644 --- a/third_party/f8/f8/types.h +++ b/third_party/f8/f8/types.h @@ -1,5 +1,11 @@ #pragma once +namespace google { + namespace protobuf { + class Message; + } +} + namespace f8 { diff --git a/third_party/f8/f8/udplog.cc b/third_party/f8/f8/udplog.cc index c203bfb..41df865 100644 --- a/third_party/f8/f8/udplog.cc +++ b/third_party/f8/f8/udplog.cc @@ -1,11 +1,9 @@ +#include + #include #include #include -#include -#include - -#include #include namespace f8 diff --git a/third_party/f8/f8/udplog.h b/third_party/f8/f8/udplog.h index 7c49760..f5535d9 100644 --- a/third_party/f8/f8/udplog.h +++ b/third_party/f8/f8/udplog.h @@ -1,11 +1,5 @@ #pragma once -#include - -#include - -#include - namespace f8 { diff --git a/third_party/f8/f8/utils.cc b/third_party/f8/f8/utils.cc index e9b6dd2..8f69536 100644 --- a/third_party/f8/f8/utils.cc +++ b/third_party/f8/f8/utils.cc @@ -1,3 +1,5 @@ +#include + #include #include #include