From 773e09cd3d78582a4e288a5f89ae679bab436b3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Mon, 30 Jan 2017 21:01:31 +0100 Subject: [PATCH] Fix (immediate) detection of remove connection close for EPOLL. --- source/eventcore/drivers/posix/epoll.d | 5 ++--- source/eventcore/drivers/posix/sockets.d | 8 +++++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/source/eventcore/drivers/posix/epoll.d b/source/eventcore/drivers/posix/epoll.d index 7702116..3460f1e 100644 --- a/source/eventcore/drivers/posix/epoll.d +++ b/source/eventcore/drivers/posix/epoll.d @@ -48,10 +48,9 @@ final class EpollEventLoop : PosixEventLoop { foreach (ref evt; m_events[0 .. ret]) { debug (EventCoreEpollDebug) print("Epoll event on %s: %s", evt.data.fd, evt.events); auto fd = cast(FD)evt.data.fd; + if (evt.events & (EPOLLERR|EPOLLHUP|EPOLLRDHUP)) notify!(EventType.status)(fd); if (evt.events & EPOLLIN) notify!(EventType.read)(fd); if (evt.events & EPOLLOUT) notify!(EventType.write)(fd); - if (evt.events & EPOLLERR) notify!(EventType.status)(fd); - else if (evt.events & EPOLLHUP) notify!(EventType.status)(fd); } return true; } else return false; @@ -70,7 +69,7 @@ final class EpollEventLoop : PosixEventLoop { ev.events |= EPOLLET; if (mask & EventMask.read) ev.events |= EPOLLIN; if (mask & EventMask.write) ev.events |= EPOLLOUT; - if (mask & EventMask.status) ev.events |= EPOLLERR|EPOLLRDHUP; + if (mask & EventMask.status) ev.events |= EPOLLERR|EPOLLRDHUP|EPOLLRDHUP; ev.data.fd = cast(int)fd; () @trusted { epoll_ctl(m_epoll, EPOLL_CTL_ADD, cast(int)fd, &ev); } (); } diff --git a/source/eventcore/drivers/posix/sockets.d b/source/eventcore/drivers/posix/sockets.d index c8f0fe2..354c341 100644 --- a/source/eventcore/drivers/posix/sockets.d +++ b/source/eventcore/drivers/posix/sockets.d @@ -65,6 +65,7 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets m_loop.initFD(sock); m_loop.registerFD(sock, EventMask.read|EventMask.write|EventMask.status); m_loop.m_fds[sock].specific = StreamSocketSlot.init; + m_loop.setNotifyCallback!(EventType.status)(sock, &onConnectError); auto ret = () @trusted { return connect(cast(sock_t)sock, address.name, address.nameLen); } (); if (ret == 0) { @@ -108,9 +109,8 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets with (m_loop.m_fds[sock].streamSocket) { state = ConnectionState.connected; assert(connectCallback !is null); - auto cb = connectCallback; + connectCallback(cast(StreamSocketFD)sock, ConnectStatus.connected); connectCallback = null; - cb(cast(StreamSocketFD)sock, ConnectStatus.connected); } } @@ -119,8 +119,9 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets // FIXME: determine the correct kind of error! with (m_loop.m_fds[sock].streamSocket) { state = ConnectionState.closed; - connectCallback(cast(StreamSocketFD)sock, ConnectStatus.refused); + auto cb = connectCallback; connectCallback = null; + if (cb) cb(cast(StreamSocketFD)sock, ConnectStatus.refused); } } @@ -198,6 +199,7 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets m_loop.m_fds[fd].specific = StreamSocketSlot.init; m_loop.m_fds[fd].streamSocket.state = ConnectionState.connected; m_loop.registerFD(fd, EventMask.read|EventMask.write|EventMask.status); + m_loop.setNotifyCallback!(EventType.status)(fd, &onConnectError); //print("accept %d", sockfd); scope RefAddress addrc = new RefAddress(() @trusted { return cast(sockaddr*)&addr; } (), addr_len); m_loop.m_fds[listenfd].streamListen.acceptCallback(cast(StreamListenSocketFD)listenfd, fd, addrc);