Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize unregister pollerEvent in macos platform #50

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

overtalk
Copy link

@overtalk overtalk commented May 15, 2020

kqueue 会在 fd 被 closed 之后自动移除该 fd 上的所有事件,所以 kqueue 是不需要手动删除 fd 的事件的,看 kqueue 的 manual:

Calling close() on a file descriptor will remove any kevents that reference the descriptor.

https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2

@IronsDu
Copy link
Owner

IronsDu commented May 15, 2020

@overtalk

void onClose() override
{
if (mAlreadyClose)
{
return;
}
mAlreadyClose = true;
#if defined BRYNET_PLATFORM_LINUX || defined BRYNET_PLATFORM_DARWIN
unregisterPollerEvent();

onClose里不代表socket已经被close了,只代表socket链接已经断开了或者业务层请求断开socket(也就是说onClose执行完毕之后,哪怕socket本身没有断开,那也会被认为是“断开”状态)!
所以是需要手动unRegister的哈。

void postDisConnect()
{
auto sharedThis = shared_from_this();
mEventLoop->runAsyncFunctor([sharedThis]() {
sharedThis->procCloseInLoop();
});
}

可以从这个请求断开看procCloseInLoop函数的流程,就能看到socket本身可能是没有断开的(所以要unRegister,表示不处理此socket事件了)。

另外,Brynet的TcpConnection里没有手动closesocket操作,我们是使用

const TcpSocket::Ptr mSocket;
const EventLoop::Ptr mEventLoop;

来自动管理socket。 也就是只有当TCPConnection资源释放时,才closesocket。

@IronsDu
Copy link
Owner

IronsDu commented May 15, 2020

@overtalk 感谢PR,我们可以就此问题多探讨。 一方面为了资源安全,另一方面为了处理linux和windows(IOCP)的差异性,做了不少不清晰的代码。

@overtalk
Copy link
Author

overtalk commented May 15, 2020

@IronsDu 看ARK项目用到了你的网络库,特意来学习一波。针对你上面说的我回头再仔细研究一些你的代码哈。

@IronsDu
Copy link
Owner

IronsDu commented May 21, 2020

之前说的不台严谨,其实socket的资源部受限于session的生命周期。
我会在

std::shared_ptr<TcpSocket> socket = std::move(const_cast<TcpSocket::Ptr&&>(mSocket));
这里会把socket move出来,等onClose函数退出,socket对象就会销毁 (若过之前socket没有被close -因为调用到onClose不代表socket已经关闭,那么总会被close-真正关闭)。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants