relation/server/imserver/synchelper.cc
aozhiwei 0849fdba14 1
2020-12-24 11:46:50 +08:00

417 lines
14 KiB
C++

#include "precompile.h"
#include <a8/openssl.h>
#include <a8/timer.h>
#include "synchelper.h"
#include "player.h"
#include "playermgr.h"
#include "app.h"
#include "IMConn.h"
#include "IMConnMgr.h"
#include "dbhelper.h"
#include "guild.h"
#include "guildmgr.h"
#include "cs_proto.pb.h"
#include "ss_proto.pb.h"
void SyncHelper::Init()
{
}
void SyncHelper::UnInit()
{
}
void SyncHelper::SyncNewFriend(Player* hum, const std::string& target_id)
{
ss::SS_IM_FriendAgreeRequest* notifymsg = new ss::SS_IM_FriendAgreeRequest;
hum->FillIMMsgConext(notifymsg->mutable_context());
notifymsg->set_target_id(target_id);
BroadcastIMConnMsg(*notifymsg);
pending_request_hash_[notifymsg->context().seqid()] =
a8::Timer::Instance()->AddDeadLineTimer
(
1000 * 10,
a8::XParams()
.SetSender(notifymsg)
.SetParam1(notifymsg->context().seqid()),
[] (const a8::XParams& param)
{
ss::SS_IM_FriendAgreeRequest* notifymsg =
(ss::SS_IM_FriendAgreeRequest*)param.sender.GetUserData();
SyncHelper::Instance()->SS_IM_FriendAgreeRequest_TimeOut(notifymsg);
},
[] (const a8::XParams& param)
{
ss::SS_IM_FriendAgreeRequest* notifymsg =
(ss::SS_IM_FriendAgreeRequest*)param.sender.GetUserData();
delete notifymsg;
SyncHelper::Instance()->RemovePendingRequest(param.param1);
}
);
}
void SyncHelper::SyncDeleteFriend(Player* hum, const std::string& target_id, int reason)
{
ss::SS_IM_FriendDeleteRequest* notifymsg = new ss::SS_IM_FriendDeleteRequest();
hum->FillIMMsgConext(notifymsg->mutable_context());
notifymsg->set_target_id(target_id);
notifymsg->set_reason(reason);
BroadcastIMConnMsg(*notifymsg);
pending_request_hash_[notifymsg->context().seqid()] =
a8::Timer::Instance()->AddDeadLineTimer
(
1000 * 10,
a8::XParams()
.SetSender(notifymsg)
.SetParam1(notifymsg->context().seqid()),
[] (const a8::XParams& param)
{
ss::SS_IM_FriendDeleteRequest* notifymsg =
(ss::SS_IM_FriendDeleteRequest*)param.sender.GetUserData();
SyncHelper::Instance()->SS_IM_FriendDeleteRequest_TimeOut(notifymsg);
},
[] (const a8::XParams& param)
{
ss::SS_IM_FriendDeleteRequest* notifymsg =
(ss::SS_IM_FriendDeleteRequest*)param.sender.GetUserData();
delete notifymsg;
SyncHelper::Instance()->RemovePendingRequest(param.param1);
}
);
}
void SyncHelper::SyncUpdateFriend(Player* hum, const std::string& target_id)
{
}
void SyncHelper::SyncApplyFriend(Player* hum, const std::string& target_id)
{
ss::SS_IM_FriendApply* notifymsg = new ss::SS_IM_FriendApply();
hum->FillIMMsgConext(notifymsg->mutable_context());
notifymsg->set_target_id(target_id);
BroadcastIMConnMsg(*notifymsg);
delete notifymsg;
}
void SyncHelper::SyncGuildMemberUpdate(Guild* guild,
GuildMember* member,
int reason)
{
InternalSyncGuildMemberUpdate(guild, member, reason, false);
}
void SyncHelper::SyncGuildMemberUpdateOnlyOnline(Guild* guild,
GuildMember* member,
int reason)
{
InternalSyncGuildMemberUpdate(guild, member, reason, true);
}
void SyncHelper::SyncGuildMemberQuit(Guild* guild,
const std::string& sender_id,
const std::string& target_id,
int reason)
{
InternalSyncGuildMemberQuit(guild, sender_id, target_id, reason, false);
}
void SyncHelper::SyncGuildMemberQuitOnlyOnline(Guild* guild,
const std::string& sender_id,
const std::string& target_id,
int reason)
{
InternalSyncGuildMemberQuit(guild, sender_id, target_id, reason, true);
}
void SyncHelper::BroadcastIMConnMsg(int msgid, ::google::protobuf::Message& msg)
{
IMConnMgr::Instance()->TraverseIMConn
(
[msgid, &msg] (IMConn* conn)
{
conn->SendMsg(msgid, msg);
return true;
}
);
int packlen = msg.ByteSize();
char* buff = nullptr;
if (packlen > 0) {
buff = (char*)malloc(packlen);
msg.SerializeToArray(buff, packlen);
}
App::Instance()->AddSocketMsg
(
SF_IMConn,
0,
0,
msgid,
0,
buff,
packlen
);
if (buff) {
free(buff);
}
}
void SyncHelper::_SS_IM_FriendAgreeResponse(f8::MsgHdr& hdr, const ss::SS_IM_FriendAgreeResponse& msg)
{
auto itr = pending_request_hash_.find(msg.context().seqid());
if (itr != pending_request_hash_.end()) {
a8::Timer::Instance()->DeleteTimer(itr->second);
RemovePendingRequest(msg.context().seqid());
}
}
void SyncHelper::_SS_IM_FriendDeleteResponse(f8::MsgHdr& hdr, const ss::SS_IM_FriendDeleteResponse& msg)
{
auto itr = pending_request_hash_.find(msg.context().seqid());
if (itr != pending_request_hash_.end()) {
a8::Timer::Instance()->DeleteTimer(itr->second);
RemovePendingRequest(msg.context().seqid());
}
}
void SyncHelper::_SS_IM_GuildMemberQuitResponse(f8::MsgHdr& hdr, const ss::SS_IM_GuildMemberQuitResponse& msg)
{
auto itr = pending_request_hash_.find(msg.seqid());
if (itr != pending_request_hash_.end()) {
a8::Timer::Instance()->DeleteTimer(itr->second);
RemovePendingRequest(msg.seqid());
}
}
void SyncHelper::_SS_IM_GuildMemberUpdateResponse(f8::MsgHdr& hdr, const ss::SS_IM_GuildMemberUpdateResponse& msg)
{
auto itr = pending_request_hash_.find(msg.seqid());
if (itr != pending_request_hash_.end()) {
a8::Timer::Instance()->DeleteTimer(itr->second);
RemovePendingRequest(msg.seqid());
}
}
void SyncHelper::_SS_IM_ApplyChangeResponse(f8::MsgHdr& hdr, const ss::SS_IM_ApplyChangeResponse& msg)
{
auto itr = pending_request_hash_.find(msg.seqid());
if (itr != pending_request_hash_.end()) {
a8::Timer::Instance()->DeleteTimer(itr->second);
RemovePendingRequest(msg.seqid());
}
}
void SyncHelper::RemovePendingRequest(long long seqid)
{
pending_request_hash_.erase(seqid);
}
void SyncHelper::SS_IM_FriendAgreeRequest_TimeOut(ss::SS_IM_FriendAgreeRequest* msg)
{
std::string event_data;
msg->context().user_info().SerializeToString(&event_data);
DBHelper::Instance()->AddEvent
(
msg->context().user_info().base_data().account_id(),
msg->target_id(),
EVENT_FRIEND_AGREE,
event_data
);
}
void SyncHelper::SS_IM_FriendDeleteRequest_TimeOut(ss::SS_IM_FriendDeleteRequest* msg)
{
std::string event_data;
msg->context().user_info().SerializeToString(&event_data);
DBHelper::Instance()->AddEvent
(
msg->context().user_info().base_data().account_id(),
msg->target_id(),
EVENT_FRIEND_DELETE,
event_data
);
}
void SyncHelper::SS_IM_GuildMemberQuitRequest_TimeOut(ss::SS_IM_GuildMemberQuitRequest* msg)
{
DBHelper::Instance()->UpdateUserGuild(msg->target_id(),
0,
0);
}
void SyncHelper::SS_IM_GuildMemberUpdateRequest_TimeOut(ss::SS_IM_GuildMemberUpdateRequest* msg)
{
DBHelper::Instance()->UpdateUserGuild(msg->target_id(),
msg->guild_id(),
msg->guild_job());
}
void SyncHelper::SendIMConnMsg(int instance_id, int msgid, const ::google::protobuf::Message& msg)
{
if (instance_id == App::Instance()->instance_id) {
int packlen = msg.ByteSize();
char* buff = nullptr;
if (packlen > 0) {
buff = (char*)malloc(packlen);
msg.SerializeToArray(buff, packlen);
}
App::Instance()->AddSocketMsg
(
SF_IMConn,
0,
0,
msgid,
0,
buff,
packlen
);
if (buff) {
free(buff);
}
} else {
IMConn* conn = IMConnMgr::Instance()->GetConnByInstanceId(instance_id);
if (conn) {
conn->SendMsg(msgid, msg);
}
}
}
void SyncHelper::InternalSyncGuildMemberUpdate(Guild* guild,
GuildMember* member,
int reason,
bool only_online)
{
ss::SS_IM_GuildMemberUpdateRequest* notifymsg = new ss::SS_IM_GuildMemberUpdateRequest;
notifymsg->set_seqid(App::Instance()->NewUUID());
notifymsg->set_guild_id(guild->GuildId());
notifymsg->set_guild_job(guild->GetMemberJob(member->account_id));
notifymsg->set_sender_id(member->account_id);
notifymsg->set_target_id(member->account_id);
notifymsg->set_reason(reason);
BroadcastIMConnMsg(*notifymsg);
if (!only_online) {
pending_request_hash_[notifymsg->seqid()] =
a8::Timer::Instance()->AddDeadLineTimer
(
1000 * 3,
a8::XParams()
.SetSender(notifymsg)
.SetParam1(notifymsg->seqid()),
[] (const a8::XParams& param)
{
ss::SS_IM_GuildMemberUpdateRequest* notifymsg =
(ss::SS_IM_GuildMemberUpdateRequest*)param.sender.GetUserData();
SyncHelper::Instance()->SS_IM_GuildMemberUpdateRequest_TimeOut(notifymsg);
},
[] (const a8::XParams& param)
{
ss::SS_IM_GuildMemberUpdateRequest* notifymsg =
(ss::SS_IM_GuildMemberUpdateRequest*)param.sender.GetUserData();
delete notifymsg;
SyncHelper::Instance()->RemovePendingRequest(param.param1);
}
);
}
}
void SyncHelper::InternalSyncGuildMemberQuit(Guild* guild,
const std::string& sender_id,
const std::string& target_id,
int reason,
bool only_online)
{
ss::SS_IM_GuildMemberQuitRequest* notifymsg = new ss::SS_IM_GuildMemberQuitRequest;
notifymsg->set_seqid(App::Instance()->NewUUID());
notifymsg->set_guild_id(guild->GuildId());
notifymsg->set_sender_id(sender_id);
notifymsg->set_target_id(target_id);
notifymsg->set_reason(reason);
BroadcastIMConnMsg(*notifymsg);
if (!only_online) {
pending_request_hash_[notifymsg->seqid()] =
a8::Timer::Instance()->AddDeadLineTimer
(
1000 * 3,
a8::XParams()
.SetSender(notifymsg)
.SetParam1(notifymsg->seqid()),
[] (const a8::XParams& param)
{
ss::SS_IM_GuildMemberQuitRequest* notifymsg =
(ss::SS_IM_GuildMemberQuitRequest*)param.sender.GetUserData();
SyncHelper::Instance()->SS_IM_GuildMemberQuitRequest_TimeOut(notifymsg);
},
[] (const a8::XParams& param)
{
ss::SS_IM_GuildMemberQuitRequest* notifymsg =
(ss::SS_IM_GuildMemberQuitRequest*)param.sender.GetUserData();
delete notifymsg;
SyncHelper::Instance()->RemovePendingRequest(param.param1);
}
);
}
}
void SyncHelper::SyncGuildApplyed(Guild* guild,
const std::string& target_id
)
{
InternalSyncGuildApplyState(guild, target_id, 0);
}
void SyncHelper::SyncGuildRefuse(Guild* guild,
const std::string& target_id
)
{
InternalSyncGuildApplyState(guild, target_id, 1);
}
void SyncHelper::SS_GS_ApplyChangeRequest_TimeOut(ss::SS_GS_ApplyChangeRequest* msg)
{
if (msg->is_refuse()) {
DBHelper::Instance()->AddEvent
(
a8::XValue(msg->guild_id()).GetString(),
msg->account_id(),
EVENT_GUILD_REFUSE,
"",
a8::XValue(msg->guild_id()).GetString()
);
}
}
void SyncHelper::InternalSyncGuildApplyState(Guild* guild,
const std::string& target_id,
int is_refuse)
{
ss::SS_GS_ApplyChangeRequest* notifymsg = new ss::SS_GS_ApplyChangeRequest;
notifymsg->set_seqid(App::Instance()->NewUUID());
notifymsg->set_guild_id(guild->GuildId());
notifymsg->set_account_id(target_id);
notifymsg->set_is_refuse(is_refuse);
BroadcastIMConnMsg(*notifymsg);
pending_request_hash_[notifymsg->seqid()] =
a8::Timer::Instance()->AddDeadLineTimer
(
1000 * 3,
a8::XParams()
.SetSender(notifymsg)
.SetParam1(notifymsg->seqid()),
[] (const a8::XParams& param)
{
ss::SS_GS_ApplyChangeRequest* notifymsg =
(ss::SS_GS_ApplyChangeRequest*)param.sender.GetUserData();
SyncHelper::Instance()->SS_GS_ApplyChangeRequest_TimeOut(notifymsg);
},
[] (const a8::XParams& param)
{
ss::SS_GS_ApplyChangeRequest* notifymsg =
(ss::SS_GS_ApplyChangeRequest*)param.sender.GetUserData();
delete notifymsg;
SyncHelper::Instance()->RemovePendingRequest(param.param1);
}
);
}