relation/server/imserver/synchelper.cc
aozhiwei e8fea344f4 1
2020-06-19 14:37:56 +08:00

150 lines
4.1 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 "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)
{
ss::SS_IM_FriendDeleteRequest* notifymsg = new ss::SS_IM_FriendDeleteRequest();
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_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::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::RemovePendingRequest(long long seqid)
{
pending_request_hash_.erase(seqid);
}
void SyncHelper::SS_IM_FriendAgreeRequest_TimeOut(ss::SS_IM_FriendAgreeRequest* msg)
{
}
void SyncHelper::SS_IM_FriendDeleteRequest_TimeOut(ss::SS_IM_FriendDeleteRequest* msg)
{
}