213 lines
8.3 KiB
C++
213 lines
8.3 KiB
C++
#include "precompile.h"
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <a8/openssl.h>
|
|
|
|
#include <f8/timer.h>
|
|
#include <f8/netmsghandler.h>
|
|
|
|
#include "mastersvrmgr.h"
|
|
#include "mastersvr.h"
|
|
#include "jsondatamgr.h"
|
|
#include "ss_proto.pb.h"
|
|
#include "target_conn.h"
|
|
#include "target_conn_mgr.h"
|
|
#include "app.h"
|
|
#include "gameclientmgr.h"
|
|
|
|
void MasterSvrMgr::Init()
|
|
{
|
|
curr_context_id_ = a8::MakeInt64(0, time(nullptr) + 1000 * 60 * 10);
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
|
|
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) {
|
|
int socket_handle = context_hdr->socket_handle;
|
|
if (msg.error_code() == 0) {
|
|
TargetConn* conn = TargetConnMgr::Instance()->RecreateTargetConn(
|
|
msg.host(),
|
|
msg.port()
|
|
);
|
|
assert(conn);
|
|
if (conn) {
|
|
RemoveRequest(socket_handle, msg.context_id(), false);
|
|
conn->ForwardClientMsgEx(context_hdr);
|
|
return;
|
|
}
|
|
}
|
|
RemoveRequest(socket_handle, msg.context_id(), true);
|
|
}
|
|
}
|
|
|
|
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,
|
|
const std::string& server_info,
|
|
int is_reconnect,
|
|
int proto_version)
|
|
{
|
|
#if 0
|
|
#if GAME_ID == 9003
|
|
{
|
|
TargetConn* conn = TargetConnMgr::Instance()->RecreateTargetConn(
|
|
"10.10.4.4",
|
|
8951
|
|
);
|
|
if (conn) {
|
|
f8::MsgHdr* new_hdr = hdr.Clone();
|
|
conn->ForwardClientMsgEx(new_hdr);
|
|
return;
|
|
} else {
|
|
abort();
|
|
}
|
|
}
|
|
#endif
|
|
#endif
|
|
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_;
|
|
#if 0
|
|
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_account_id(account_id);
|
|
msg.set_team_id(team_uuid);
|
|
msg.set_server_info(server_info);
|
|
msg.set_is_reconnect(is_reconnect);
|
|
msg.set_proto_version(proto_version);
|
|
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;
|
|
if (account_id == azw_account_id) {
|
|
a8::UdpLog::Instance()->Info("%s request", {account_id});
|
|
}
|
|
auto timer_func =
|
|
[] (const a8::XParams& param)
|
|
{
|
|
if (param.param2.GetString() == azw_account_id) {
|
|
a8::UdpLog::Instance()->Info("%s timeout",
|
|
{
|
|
param.param2.GetString()
|
|
});
|
|
}
|
|
a8::Timer::Instance()->RemoveTimerAfterFunc(a8::Timer::Instance()->GetRunningTimer());
|
|
MasterSvrMgr::Instance()->RemoveRequest(
|
|
param.param1,
|
|
param.sender,
|
|
true
|
|
);
|
|
};
|
|
auto timer_after_func =
|
|
[] (const a8::XParams& param)
|
|
{
|
|
if (param.param2.GetString() == azw_account_id) {
|
|
a8::UdpLog::Instance()->Info("%s response time:%d",
|
|
{
|
|
param.param2.GetString(),
|
|
a8::XGetTickCount() - param.param3.GetInt64()
|
|
});
|
|
}
|
|
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
|
|
);
|
|
#endif
|
|
}
|
|
}
|
|
|
|
void MasterSvrMgr::RemoveRequest(int socket_handle, long long context_id, bool auto_free)
|
|
{
|
|
#if 0
|
|
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);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
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;
|
|
}
|