@Override public InetSocketAddress getLocalAddress() { return (InetSocketAddress) channel.getLocalAddress(); }
@Override public InetSocketAddress getLocalAddress() { return (InetSocketAddress) channel.getLocalAddress(); }
@Override public int getPort() { return ((InetSocketAddress) serverChannel.getLocalAddress()).getPort(); }
@VisibleForTesting InetSocketAddress getBoundAddress() { SocketAddress localAddress = nettyChannel.getLocalAddress(); if (!(localAddress instanceof InetSocketAddress)) { throw new IllegalArgumentException("Not bound to an internet address"); } return (InetSocketAddress) localAddress; }
@VisibleForTesting public int getSourcePort() { SocketAddress localAddress = nettyChannel.getLocalAddress(); if (localAddress instanceof InetSocketAddress) { InetSocketAddress addr = (InetSocketAddress) localAddress; return addr.getPort(); } return 0; } }
@VisibleForTesting InetSocketAddress getBoundAddress() { SocketAddress localAddress = nettyChannel.getLocalAddress(); if (!(localAddress instanceof InetSocketAddress)) { throw new IllegalArgumentException("Not bound to an internet address"); } return (InetSocketAddress) localAddress; }
void setChannel(Channel newChannel) { final Channel oldChannel = channelRef.getAndSet(newChannel); if (newChannel != null) { retries.set(0); } final String oldLocalAddres = (oldChannel == null) ? "null" : oldChannel.getLocalAddress().toString(); String newLocalAddress = (newChannel == null) ? "null" : newChannel.getLocalAddress().toString(); LOG.info("Use new channel {} to replace old channel {}", newLocalAddress, oldLocalAddres); // avoid one netty client use too much connection, close old one if (oldChannel != newChannel && oldChannel != null) { closeChannel(oldChannel); LOG.info("Successfully close old channel " + oldLocalAddres); } }
@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 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 channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { Channel channel = ctx.getChannel(); String channelKey = getChannelKey((InetSocketAddress) channel.getLocalAddress(), (InetSocketAddress) channel.getRemoteAddress()); if (channels.size() > maxChannel) { // 超过最大连接数限制,直接close连接 LoggerUtil.warn("NettyServerChannelManage channelConnected channel size out of limit: limit={} current={}", maxChannel, channels.size()); channel.close(); } else { channels.put(channelKey, channel); ctx.sendUpstream(e); } }
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; } } }
/** * 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); }
public void start() throws Exception { ServerBootstrap bootstrap = new ServerBootstrap(selector); // Timer is shared across entire factory and must be released separately timer = new HashedWheelTimer(); try { pipelineFact = new HttpPipelineFactory(conf, timer); } catch (Exception ex) { throw new RuntimeException(ex); } bootstrap.setPipelineFactory(pipelineFact); bootstrap.setOption("backlog", NetUtil.SOMAXCONN); port = conf.getInt(SHUFFLE_PORT_CONFIG_KEY, DEFAULT_SHUFFLE_PORT); Channel ch = bootstrap.bind(new InetSocketAddress(port)); accepted.add(ch); port = ((InetSocketAddress)ch.getLocalAddress()).getPort(); conf.set(SHUFFLE_PORT_CONFIG_KEY, Integer.toString(port)); pipelineFact.SHUFFLE.setPort(port); if (dirWatcher != null) { dirWatcher.start(); } LOG.info("LlapShuffleHandler" + " listening on port " + port + " (SOMAXCONN: " + bootstrap.getOption("backlog") + ")"); }
private void fireInitialEvents() { // Fire the typical initial events. fireChannelOpen(channel); fireChannelBound(channel, channel.getLocalAddress()); fireChannelConnected(channel, channel.getRemoteAddress()); }
@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(); }
if (channel.getLocalAddress() != null && channel.getLocalAddress() instanceof InetSocketAddress) { localAddress = (InetSocketAddress) channel.getLocalAddress();
runtimePort = ((InetSocketAddress) channel.getLocalAddress()).getPort();
@Override public String getLocalAddressAndPort() { if (this.channel != null) { InetSocketAddress addr = (InetSocketAddress)this.channel.getLocalAddress(); return addr.getAddress().getHostAddress() + ":" + addr.getPort(); } else { return null; } }
@Override public synchronized List<InetSocketAddress> getListenAddresses() { ImmutableList.Builder<InetSocketAddress> builder = ImmutableList.builder(); for (Channel channel : ImmutableList.copyOf(channels.iterator())) { builder.add((InetSocketAddress) channel.getLocalAddress()); } return builder.build(); }