@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { NettyChannel channel = NettyChannel.getOrAddChannel(ctx.getChannel(), url, handler); try { handler.received(channel, e.getMessage()); } finally { NettyChannel.removeChannelIfDisconnected(ctx.getChannel()); } }
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { NettyChannel channel = NettyChannel.getOrAddChannel(ctx.getChannel(), url, handler); try { handler.received(channel, e.getMessage()); } finally { NettyChannel.removeChannelIfDisconnected(ctx.getChannel()); } }
@Override public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { NettyChannel channel = NettyChannel.getOrAddChannel(ctx.getChannel(), url, handler); try { if (channel != null) { channels.put(NetUtils.toAddressString((InetSocketAddress) ctx.getChannel().getRemoteAddress()), channel); } handler.connected(channel); } finally { NettyChannel.removeChannelIfDisconnected(ctx.getChannel()); } }
@Override public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { NettyChannel channel = NettyChannel.getOrAddChannel(ctx.getChannel(), url, handler); try { channels.remove(NetUtils.toAddressString((InetSocketAddress) ctx.getChannel().getRemoteAddress())); handler.disconnected(channel); } finally { NettyChannel.removeChannelIfDisconnected(ctx.getChannel()); } }
@Override public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { NettyChannel channel = NettyChannel.getOrAddChannel(ctx.getChannel(), url, handler); try { if (channel != null) { channels.put(NetUtils.toAddressString((InetSocketAddress) ctx.getChannel().getRemoteAddress()), channel); } handler.connected(channel); } finally { NettyChannel.removeChannelIfDisconnected(ctx.getChannel()); } }
@Override public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { NettyChannel channel = NettyChannel.getOrAddChannel(ctx.getChannel(), url, handler); try { channels.remove(NetUtils.toAddressString((InetSocketAddress) ctx.getChannel().getRemoteAddress())); handler.disconnected(channel); } finally { NettyChannel.removeChannelIfDisconnected(ctx.getChannel()); } }
@Override public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception { super.writeRequested(ctx, e); NettyChannel channel = NettyChannel.getOrAddChannel(ctx.getChannel(), url, handler); try { handler.sent(channel, e.getMessage()); } finally { NettyChannel.removeChannelIfDisconnected(ctx.getChannel()); } }
@Override public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception { super.writeRequested(ctx, e); NettyChannel channel = NettyChannel.getOrAddChannel(ctx.getChannel(), url, handler); try { handler.sent(channel, e.getMessage()); } finally { NettyChannel.removeChannelIfDisconnected(ctx.getChannel()); } }
@Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { NettyChannel channel = NettyChannel.getOrAddChannel(ctx.getChannel(), url, handler); try { handler.caught(channel, e.getCause()); } finally { NettyChannel.removeChannelIfDisconnected(ctx.getChannel()); } }
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent event) { client.handleResponse(ctx.getChannel(), event.getMessage()); }
@Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { NettyChannel channel = NettyChannel.getOrAddChannel(ctx.getChannel(), url, handler); try { handler.caught(channel, e.getCause()); } finally { NettyChannel.removeChannelIfDisconnected(ctx.getChannel()); } }
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { logger.error("something goes wrong with channel:{}, exception={}", ctx.getChannel(), ExceptionUtils.getStackTrace(e.getCause())); ctx.getChannel().close(); }
@Override public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { LoggerUtil.info("NettyChannelHandler channelConnected: remote=" + ctx.getChannel().getRemoteAddress() + " local=" + ctx.getChannel().getLocalAddress() + " event=" + e.getClass().getSimpleName()); }
@Override public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { LoggerUtil.info("NettyChannelHandler channelDisconnected: remote=" + ctx.getChannel().getRemoteAddress() + " local=" + ctx.getChannel().getLocalAddress() + " event=" + e.getClass().getSimpleName()); }
@Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { LoggerUtil.error("NettyChannelHandler exceptionCaught: remote=" + ctx.getChannel().getRemoteAddress() + " local=" + ctx.getChannel().getLocalAddress() + " event=" + e.getCause(), e.getCause()); ctx.getChannel().close(); }
/** * Sometime when connecting to a bad channel which isn't writable, this method will be called */ @Override public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent event) { // register the newly established channel Channel channel = event.getChannel(); LOG.info("connection established to :{}, local port:{}", client.getRemoteAddr(), channel.getLocalAddress()); client.connectChannel(ctx.getChannel()); client.handleResponse(ctx.getChannel(), null); }
private void checkMaxContext(int dataLength, ChannelHandlerContext ctx, Channel channel, boolean isRequest, long requestId) throws Exception { if (maxContentLength > 0 && dataLength > maxContentLength) { LoggerUtil.warn("NettyDecoder transport data content length over of limit, size: {} > {}. remote={} local={}", dataLength, maxContentLength, ctx.getChannel().getRemoteAddress(), ctx.getChannel().getLocalAddress()); Exception e = new MotanServiceException("NettyDecoder transport data content length over of limit, size: " + dataLength + " > " + maxContentLength); if (isRequest) { Response response = buildExceptionResponse(requestId, e); channel.write(response); throw e; } else { throw e; } } }
public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { // add new socket channel in channel container, used to manage sockets. if (childGroups != null) { childGroups.add(ctx.getChannel()); } byte[] body = Packet.newBuilder() .setType(CanalPacket.PacketType.HANDSHAKE) .setBody(Handshake.newBuilder().build().toByteString()) .build() .toByteArray(); NettyUtils.write(ctx.getChannel(), body, null); logger.info("send handshake initialization packet to : {}", ctx.getChannel()); } }
@Override public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { Channel channel = ctx.getChannel(); String channelKey = getChannelKey((InetSocketAddress) channel.getLocalAddress(), (InetSocketAddress) channel.getRemoteAddress()); channels.remove(channelKey); ctx.sendUpstream(e); }
@Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) { final Channel channel = ctx.getChannel(); if (channel == null) { // For the case where this pipeline is not attached yet. handshakeFuture.setFailure(new ChannelException( StringUtils.format("Channel is null. The context name is [%s]", ctx.getName()) )); return; } handshakeFuture.setFailure(e.getCause()); if (channel.isOpen()) { channel.close(); } } });