Refine search
/** * Create a new {@link Channel} and bind it. */ public ChannelFuture bind(int inetPort) { return bind(new InetSocketAddress(inetPort)); }
@Override public String toString() { StringBuilder buf = new StringBuilder() .append(StringUtil.simpleClassName(this)) .append('(').append(config()).append(')'); return buf.toString(); }
/** * Returns the configured {@link EventLoopGroup} or {@code null} if non is configured yet. */ @SuppressWarnings("deprecation") public final EventLoopGroup group() { return bootstrap.group(); }
/** * Create a new {@link Channel} and register it with an {@link EventLoop}. */ public ChannelFuture register() { validate(); return initAndRegister(); }
public void start() throws Exception { InetSocketAddress endpoint; if (address == null) { endpoint = new InetSocketAddress(port); } else { endpoint = new InetSocketAddress(address, port); } Channel channel = bootstrap.bind(endpoint).sync().channel(); if (channel != null) { getChannelGroup().add(channel); } }
@Override @SuppressWarnings("unchecked") public void channelRead(ChannelHandlerContext ctx, Object msg) { final Channel child = (Channel) msg; child.pipeline().addLast(childHandler); setChannelOptions(child, childOptions, logger); for (Entry<AttributeKey<?>, Object> e: childAttrs) { child.attr((AttributeKey<Object>) e.getKey()).set(e.getValue()); } try { childGroup.register(child).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { forceClose(child, future.cause()); } } }); } catch (Throwable t) { forceClose(child, t); } }
/** * Build a {@link ChannelBindException} * * @param bootstrap a netty bootstrap * @param cause a root cause * * @return a new {@link ChannelBindException} */ public static ChannelBindException fail(AbstractBootstrap<?, ?> bootstrap, @Nullable Throwable cause) { Objects.requireNonNull(bootstrap, "bootstrap"); if (cause instanceof java.net.BindException) { cause = null; } if (!(bootstrap.config().localAddress() instanceof InetSocketAddress)) { return new ChannelBindException(bootstrap.config().localAddress().toString(), -1, cause); } InetSocketAddress address = (InetSocketAddress)bootstrap.config().localAddress(); return new ChannelBindException(address.getHostString(), address.getPort(), cause); }
try { channel = channelFactory.newChannel(); init(channel); } catch (Throwable t) { if (channel != null) { channel.unsafe().closeForcibly(); ChannelFuture regFuture = config().group().register(channel); if (regFuture.cause() != null) { if (channel.isRegistered()) { channel.close(); } else { channel.unsafe().closeForcibly();
public S start() { if (!serverStateRef.compareAndSet(ServerState.Created, ServerState.Starting)) { throw new IllegalStateException("Server already started"); } try { bindFuture = bootstrap.bind(port).sync(); if (!bindFuture.isSuccess()) { throw new RuntimeException(bindFuture.cause()); } } catch (InterruptedException e) { throw new RuntimeException(e); } serverStateRef.set(ServerState.Started); // It will come here only if this was the thread that transitioned to Starting logger.info("Rx server started at port: " + getServerPort()); return returnServer(); }
@Override protected void makeChannel(AbstractBootstrap<ServerBootstrap, ServerChannel> bootstrap, InetSocketAddress address, B builder) throws IllegalStateException, InterruptedException { // Bind and start to accept incoming connections. try { ChannelFuture cf = bootstrap.bind(address); cf.get(); listeningChannel = cf.channel(); logger.debug("bond to {}", address); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } catch (ExecutionException e) { throw new IllegalStateException("Failed to start listening on " + address, e.getCause()); } }
private <T extends AbstractBootstrap<?, ?>> T configBoot(T boot) { boot.option(ChannelOption.SO_KEEPALIVE, true); // boot.option(ChannelOption.SO_BACKLOG, 128); // boot.option(ChannelOption.SO_BACKLOG, 1024); // boot.option(ChannelOption.SO_RCVBUF, 1024 * 256); // boot.option(ChannelOption.SO_SNDBUF, 1024 * 256); boot.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); return boot; } }
/** * Returns the configured {@link ChannelFactory} or {@code null} if non is configured yet. */ @SuppressWarnings("deprecation") public final ChannelFactory<? extends C> channelFactory() { return bootstrap.channelFactory(); }
final Map<ChannelOption<?>, Object> options() { return copiedMap(options); }
/** * Create a new {@link Channel} and bind it. */ public ChannelFuture bind(String inetHost, int inetPort) { return bind(SocketUtils.socketAddress(inetHost, inetPort)); }
/** * Obtain and remove the current {@link ChannelOperations.OnSetup} from the bootstrap. * * @param b the bootstrap to scan * * @return current {@link ChannelOperations.OnSetup} factory or null * */ @SuppressWarnings("unchecked") public static ChannelOperations.OnSetup channelOperationFactory(AbstractBootstrap<?, ?> b) { Objects.requireNonNull(b, "bootstrap"); ChannelOperations.OnSetup ops = (ChannelOperations.OnSetup) b.config() .options() .get(OPS_OPTION); b.option(OPS_OPTION, null); if (ops == null) { return ChannelOperations.OnSetup.empty(); //will not be triggered in } return ops; }
/** * Returns a copy of the configured attributes. */ public final Map<AttributeKey<?>, Object> attrs() { return bootstrap.attrs(); }
@Override public Bootstrap validate() { super.validate(); if (config.handler() == null) { throw new IllegalStateException("handler not set"); } return this; }
@Override @SuppressWarnings("unchecked") public void channelRead(ChannelHandlerContext ctx, Object msg) { final Channel child = (Channel) msg; child.pipeline().addLast(childHandler); setChannelOptions(child, childOptions, logger); for (Entry<AttributeKey<?>, Object> e: childAttrs) { child.attr((AttributeKey<Object>) e.getKey()).set(e.getValue()); } try { childGroup.register(child).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { forceClose(child, future.cause()); } } }); } catch (Throwable t) { forceClose(child, t); } }
try { channel = channelFactory.newChannel(); init(channel); } catch (Throwable t) { if (channel != null) { channel.unsafe().closeForcibly(); ChannelFuture regFuture = config().group().register(channel); if (regFuture.cause() != null) { if (channel.isRegistered()) { channel.close(); } else { channel.unsafe().closeForcibly();