#include "precompile.h" #include #include #include #include "mastersvrmgr.h" #include "mastersvr.h" #include "jsondatamgr.h" #include "ss_proto.pb.h" #include "app.h" #include "gameclientmgr.h" #include "framework/cpp/netmsghandler.h" void MasterSvrMgr::Init() { curr_context_id_ = a8::MakeInt64(0, time(nullptr) + 1000 * 60 * 10); #if MASTER_MODE auto master_svr_cluster_conf = JsonDataMgr::Instance()->GetMasterServerClusterConf(); for (int i = 0; i < master_svr_cluster_conf->Size(); ++i) { auto master_svr_conf = master_svr_cluster_conf->At(i); int instance_id = master_svr_conf->At("instance_id")->AsXValue(); std::string remote_ip = master_svr_conf->At("ip")->AsXValue(); int remote_port = master_svr_conf->At("port")->AsXValue(); { MasterSvr* conn = new MasterSvr(); conn->Init(instance_id, remote_ip, remote_port); mastersvr_hash_[conn->instance_id] = conn; conn->Open(); } } #endif } void MasterSvrMgr::UnInit() { } void MasterSvrMgr::_SS_MS_ResponseTargetServer(f8::MsgHdr& hdr, const ss::SS_MS_ResponseTargetServer& msg) { f8::MsgHdr* context_hdr = GetHdrByContextId(msg.context_id()); if (context_hdr) { bool auto_free = true; int socket_handle = context_hdr->socket_handle; if (msg.error_code() == 0) { #if 0 TargetConn* conn = TargetConnMgr::Instance()->RecreateTargetConn( msg.host(), msg.port() ); assert(conn); if (conn) { auto_free = false; conn->ForwardClientMsgEx(context_hdr); } #endif } RemoveRequest(socket_handle, msg.context_id(), auto_free); } } MasterSvr* MasterSvrMgr::GetConnById(int instance_id) { auto itr = mastersvr_hash_.find(instance_id); return itr != mastersvr_hash_.end() ? itr->second : nullptr; } void MasterSvrMgr::RequestTargetServer(f8::MsgHdr& hdr, const std::string& team_id, const std::string& account_id) { if (GetContextIdBySocket(hdr.socket_handle) != 0) { return; } unsigned int code = 0; std::string team_uuid = team_id; if (!team_id.empty()) { code = a8::openssl::Crc32((unsigned char*)team_id.data(), team_id.size()); } else { std::string data = a8::Format("!%s_%s_%d_%d", { account_id, App::Instance()->uuid.Generate(), getpid(), rand() }); team_uuid = data; code = a8::openssl::Crc32((unsigned char*)data.data(), data.size()); } MasterSvr* svr = GetConnById(code % mastersvr_hash_.size() + 1); if (svr) { ++curr_context_id_; a8::TimerAttacher* timer_attacher = new a8::TimerAttacher(); f8::MsgHdr* new_hdr = hdr.Clone(); new_hdr->user_data = timer_attacher; ss::SS_WSP_RequestTargetServer msg; msg.set_context_id(curr_context_id_); msg.set_team_id(team_uuid); svr->SendMsg(msg); pending_socket_hash_[hdr.socket_handle] = curr_context_id_; assert(pending_request_hash_.find(curr_context_id_) == pending_request_hash_.end()); pending_request_hash_[curr_context_id_] = new_hdr; auto timer_func = [] (const a8::XParams& param) { a8::Timer::Instance()->RemoveTimerAfterFunc(a8::Timer::Instance()->GetRunningTimer()); MasterSvrMgr::Instance()->RemoveRequest( param.param1, param.sender, true ); }; auto timer_after_func = [] (const a8::XParams& param) { long long req_handle_time = a8::XGetTickCount() - param.param3.GetInt64(); if (req_handle_time > App::Instance()->perf.max_login_time) { App::Instance()->perf.max_login_time = req_handle_time; } GameClientMgr::Instance()->AddPendingAccount(param.param2.GetString(), param.param1, param.param3); }; a8::Timer::Instance()->AddDeadLineTimerAndAttach(1000 * 10, a8::XParams() .SetSender(curr_context_id_) .SetParam1(hdr.socket_handle) .SetParam2(account_id) .SetParam3(a8::XGetTickCount()), timer_func, &timer_attacher->timer_list_, timer_after_func ); } } void MasterSvrMgr::RemoveRequest(int socket_handle, long long context_id, bool auto_free) { if (context_id == GetContextIdBySocket(socket_handle)) { f8::MsgHdr* hdr = GetHdrByContextId(context_id); if (hdr) { a8::TimerAttacher* timer_attacher = (a8::TimerAttacher*)hdr->user_data; delete timer_attacher; hdr->user_data = nullptr; if (auto_free) { if (hdr->buf) { free((char*)hdr->buf); } free(hdr); } } pending_request_hash_.erase(context_id); pending_socket_hash_.erase(socket_handle); } } long long MasterSvrMgr::GetContextIdBySocket(int socket_handle) { auto itr = pending_socket_hash_.find(socket_handle); return itr != pending_socket_hash_.end() ? itr->second : 0; } f8::MsgHdr* MasterSvrMgr::GetHdrByContextId(long long context_id) { auto itr = pending_request_hash_.find(context_id); return itr != pending_request_hash_.end() ? itr->second : nullptr; }