private void actualClose(final long closeContextID, final Handler<Void> done) { if (id != null) { servers.remove(id); } for (NetSocket sock : socketMap.values()) { sock.internalClose(); } // We need to reset it since sock.internalClose() above can call into the close handlers of // sockets on the same thread // which can cause context id for the thread to change! VertxInternal.instance.setContextID(closeContextID); ChannelGroupFuture fut = serverChannelGroup.close(); if (done != null) { fut.addListener( new ChannelGroupFutureListener() { public void operationComplete(ChannelGroupFuture channelGroupFuture) throws Exception { executeCloseDone(closeContextID, done); } }); } }
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) { Channel ch = e.getChannel(); NetSocket sock = socketMap.get(ch); if (sock != null) { ChannelBuffer buff = (ChannelBuffer) e.getMessage(); sock.handleDataReceived(new Buffer(buff.slice())); } }
@Override public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) { final NioSocketChannel ch = (NioSocketChannel) e.getChannel(); final NetSocket sock = socketMap.remove(ch); if (sock != null) { VertxInternal.instance.executeOnContext( sock.getContextID(), new Runnable() { public void run() { sock.handleClosed(); } }); } }
@Override public void channelInterestChanged(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { final NioSocketChannel ch = (NioSocketChannel) e.getChannel(); final NetSocket sock = socketMap.get(ch); ChannelState state = e.getState(); if (state == ChannelState.INTEREST_OPS) { VertxInternal.instance.executeOnContext( sock.getContextID(), new Runnable() { public void run() { sock.handleInterestedOpsChanged(); } }); } }
@Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) { final NioSocketChannel ch = (NioSocketChannel) e.getChannel(); final NetSocket sock = socketMap.get(ch); ch.close(); final Throwable t = e.getCause(); if (sock != null && t instanceof Exception) { VertxInternal.instance.executeOnContext( sock.getContextID(), new Runnable() { public void run() { sock.handleException((Exception) t); } }); } else { t.printStackTrace(); } }