1
This commit is contained in:
parent
0d3cd07a12
commit
6cd0a7d958
@ -14,6 +14,23 @@
|
||||
#include "jsondatamgr.h"
|
||||
#include "handlermgr.h"
|
||||
|
||||
class SocketDisconnectHandler : public std::enable_shared_from_this<SocketDisconnectHandler>
|
||||
{
|
||||
public:
|
||||
a8::CommonCbProc cb;
|
||||
list_head entry;
|
||||
std::shared_ptr<SocketDisconnectHandler> holder;
|
||||
|
||||
SocketDisconnectHandler()
|
||||
{
|
||||
}
|
||||
|
||||
~SocketDisconnectHandler()
|
||||
{
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
class GCClientSession: public a8::MixedSession
|
||||
{
|
||||
public:
|
||||
@ -113,6 +130,20 @@ void GGListener::Init()
|
||||
int error_id = args.Get<int>(0);
|
||||
GGListener::Instance()->OnListenError(error_id);
|
||||
});
|
||||
f8::MsgQueue::Instance()->RegisterCallBack
|
||||
(
|
||||
IM_ClientSocketDisconnect,
|
||||
[this] (const a8::Args& args)
|
||||
{
|
||||
int gg_socket = args.Get<int>(0);
|
||||
auto itr = disconnect_listener_hash_.find(gg_socket);
|
||||
if (itr != disconnect_listener_hash_.end()) {
|
||||
class SocketDisconnectHandler *handle = nullptr, *tmp = nullptr;
|
||||
list_for_each_entry_safe(handle, tmp, &itr->second, entry) {
|
||||
handle->cb(a8::Args({}));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void GGListener::UnInit()
|
||||
@ -161,3 +192,28 @@ bool GGListener::DebugNetMsg(int msg_id)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
std::weak_ptr<SocketDisconnectHandler> GGListener::AddSocketDisconnectListener(int socket_handle, a8::CommonCbProc cb)
|
||||
{
|
||||
auto itr = disconnect_listener_hash_.find(socket_handle);
|
||||
if (itr == disconnect_listener_hash_.end()) {
|
||||
disconnect_listener_hash_[socket_handle] = list_head();
|
||||
itr = disconnect_listener_hash_.find(socket_handle);
|
||||
INIT_LIST_HEAD(&itr->second);
|
||||
}
|
||||
auto p = std::make_shared<SocketDisconnectHandler>();
|
||||
p->cb = cb;
|
||||
list_add_tail(&p->entry, &itr->second);
|
||||
p->holder = p;
|
||||
return p;
|
||||
}
|
||||
|
||||
void GGListener::RemoveSocketDisconnectHandler(std::weak_ptr<SocketDisconnectHandler> handler)
|
||||
{
|
||||
if (!handler.expired()) {
|
||||
auto p = handler.lock();
|
||||
p->cb = nullptr;
|
||||
p->holder = nullptr;
|
||||
list_del_init(&p->entry);
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ namespace a8
|
||||
class TcpListener;
|
||||
}
|
||||
|
||||
class SocketDisconnectHandler;
|
||||
class GGListener : public a8::Singleton<GGListener>
|
||||
{
|
||||
private:
|
||||
@ -65,6 +66,8 @@ public:
|
||||
long long GetSendNodeNum();
|
||||
long long GetSentNodeNum();
|
||||
long long GetSentBytesNum();
|
||||
std::weak_ptr<SocketDisconnectHandler> AddSocketDisconnectListener(int socket_handle, a8::CommonCbProc cb);
|
||||
void RemoveSocketDisconnectHandler(std::weak_ptr<SocketDisconnectHandler> handler);
|
||||
bool DebugNetMsg(int msg_id);
|
||||
|
||||
private:
|
||||
@ -72,4 +75,5 @@ private:
|
||||
|
||||
private:
|
||||
std::shared_ptr<a8::TcpListener> tcp_listener_;
|
||||
std::map<int, list_head> disconnect_listener_hash_;
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user