@@ -30,9 +30,11 @@ namespace SocketTools {
3030#ifdef TARGET_OS_MAC
3131 struct kevent event;
3232 bzero (&event, sizeof (event));
33- event.ident = socket;
34- EV_SET (&event, socket, EVFILT_READ, EV_ADD, 0 , 0 , NULL );
35- assert (-1 != kevent (kq, &event, 1 , NULL , 0 , NULL ));
33+ event.ident = socket.m_sock ;
34+ EV_SET (&event, event.ident , EVFILT_READ, EV_ADD, 0 , 0 , NULL );
35+ kevent (kq, &event, 1 , NULL , 0 , NULL );
36+ EV_SET (&event, event.ident , EVFILT_WRITE, EV_ADD, 0 , 0 , NULL );
37+ kevent (kq, &event, 1 , NULL , 0 , NULL );
3638#endif
3739 m_sockets.push_back (SocketData ());
3840 m_sockets.back ().socket = socket;
@@ -117,7 +119,13 @@ namespace SocketTools {
117119 if (-1 == kevent (kq, &event, 1 , NULL , 0 , NULL ))
118120 {
119121 // // Already removed?
120- // LOG_ERROR("cannot delete fd=%d from kqueue!", event.ident);
122+ LOG_TRACE (" cannot delete fd=%d from kqueue!" , event.ident );
123+ }
124+ EV_SET (&event, socket, EVFILT_WRITE, EV_DELETE, 0 , 0 , NULL );
125+ if (-1 == kevent (kq, &event, 1 , NULL , 0 , NULL ))
126+ {
127+ // // Already removed?
128+ LOG_TRACE (" cannot delete fd=%d from kqueue!" , event.ident );
121129 }
122130#endif
123131 m_sockets.erase (it);
@@ -153,7 +161,13 @@ namespace SocketTools {
153161 if (-1 == kevent (kq, &event, 1 , NULL , 0 , NULL ))
154162 {
155163 // // Already removed?
156- // LOG_ERROR("cannot delete fd=%d from kqueue!", event.ident);
164+ LOG_TRACE (" cannot delete fd=%d from kqueue!" , event.ident );
165+ }
166+ EV_SET (&event, sd.socket , EVFILT_WRITE, EV_DELETE, 0 , 0 , NULL );
167+ if (-1 == kevent (kq, &event, 1 , NULL , 0 , NULL ))
168+ {
169+ // // Already removed?
170+ LOG_TRACE (" cannot delete fd=%d from kqueue!" , event.ident );
157171 }
158172#endif
159173 }
@@ -255,21 +269,8 @@ namespace SocketTools {
255269 Socket socket = it->socket ;
256270 int flags = it->flags ;
257271
258- // LOG_TRACE("Reactor: Handling socket %d active flags 0x%x (armed 0x%x)", static_cast<int>(socket), event.flags, event.fflags);
259-
260- if ((event.flags & EV_EOF)||(event.flags & EV_ERROR))
261- {
262- m_callback.onSocketClosed (socket);
263- it->flags =Closed;
264- struct kevent kevt;
265- EV_SET (&kevt, event.ident , EVFILT_READ, EV_DELETE, 0 , 0 , NULL );
266- if (-1 == kevent (kq, &kevt, 1 , NULL , 0 , NULL ))
267- {
268- // // Already removed?
269- // LOG_ERROR("cannot delete fd=%d from kqueue!", event.ident);
270- }
271- continue ;
272- }
272+ LOG_TRACE (" Handling socket %d active flags 0x%08x (armed 0x%08x)" ,
273+ static_cast <int >(socket), event.flags , event.fflags );
273274
274275 if (event.filter ==EVFILT_READ)
275276 {
@@ -292,6 +293,25 @@ namespace SocketTools {
292293 }
293294 continue ;
294295 }
296+
297+ if ((event.flags & EV_EOF)||(event.flags & EV_ERROR))
298+ {
299+ LOG_TRACE (" event.filter=%s" , " EVFILT_WRITE" );
300+ m_callback.onSocketClosed (socket);
301+ it->flags =Closed;
302+ struct kevent kevt;
303+ EV_SET (&kevt, event.ident , EVFILT_READ, EV_DELETE, 0 , 0 , NULL );
304+ if (-1 == kevent (kq, &kevt, 1 , NULL , 0 , NULL ))
305+ {
306+ LOG_TRACE (" cannot delete fd=%d from kqueue!" , event.ident );
307+ }
308+ EV_SET (&kevt, event.ident , EVFILT_WRITE, EV_DELETE, 0 , 0 , NULL );
309+ if (-1 == kevent (kq, &kevt, 1 , NULL , 0 , NULL ))
310+ {
311+ LOG_TRACE (" cannot delete fd=%d from kqueue!" , event.ident );
312+ }
313+ continue ;
314+ }
295315 LOG_ERROR (" Reactor: unhandled kevent!" );
296316 }
297317#endif
0 commit comments