use of io.netty.channel.ServerChannel in project tesla by linking12.
the class HttpProxyServer method doStart.
private void doStart() {
ServerBootstrap serverBootstrap = new ServerBootstrap().group(serverGroup.getClientToProxyAcceptorPoolForTransport(), serverGroup.getClientToProxyWorkerPoolForTransport());
ChannelInitializer<Channel> initializer = new ChannelInitializer<Channel>() {
protected void initChannel(Channel ch) throws Exception {
new ClientToProxyConnection(HttpProxyServer.this, ch.pipeline(), globalTrafficShapingHandler);
}
};
serverBootstrap.channelFactory(new ChannelFactory<ServerChannel>() {
public ServerChannel newChannel() {
return new NioServerSocketChannel();
}
});
serverBootstrap.childHandler(initializer);
ChannelFuture future = serverBootstrap.bind(requestedAddress).addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
registerChannel(future.channel());
}
}
}).awaitUninterruptibly();
Throwable cause = future.cause();
if (cause != null) {
throw new RuntimeException(cause);
}
this.boundAddress = ((InetSocketAddress) future.channel().localAddress());
LOG.info("Proxy started at address: " + this.boundAddress);
Runtime.getRuntime().addShutdownHook(jvmShutdownHook);
}
use of io.netty.channel.ServerChannel in project jocean-http by isdom.
the class DefaultHttpServerBuilder method defineServer.
public Observable<? extends HttpTrade> defineServer(final SocketAddress localAddress, final Func0<Feature[]> featuresBuilder, final Feature... features) {
return Observable.unsafeCreate(new Observable.OnSubscribe<HttpTrade>() {
@Override
public void call(final Subscriber<? super HttpTrade> subscriber) {
if (!subscriber.isUnsubscribed()) {
final ServerBootstrap bootstrap = _creator.newBootstrap();
final List<Channel> awaitChannels = new CopyOnWriteArrayList<>();
bootstrap.childHandler(new Initializer() {
@Override
protected void initChannel(final Channel channel) throws Exception {
channel.config().setAutoRead(false);
if (LOG.isDebugEnabled()) {
LOG.debug("dump inbound channel({})'s config: \n{}", channel, Nettys.dumpChannelConfig(channel.config()));
}
if (_inboundRecvBufSize > 0) {
if (LOG.isDebugEnabled()) {
LOG.debug("channel({})'s default SO_RCVBUF is {} bytes, and will be reset to {} bytes", channel, channel.config().getOption(ChannelOption.SO_RCVBUF), _inboundRecvBufSize);
}
channel.config().setOption(ChannelOption.SO_RCVBUF, _inboundRecvBufSize);
}
final Feature[] actualFeatures = JOArrays.addFirst(Feature[].class, featuresOf(featuresBuilder), features);
final Feature[] applyFeatures = (null != actualFeatures && actualFeatures.length > 0) ? actualFeatures : _defaultFeatures;
for (Feature feature : applyFeatures) {
if (feature instanceof FeatureOverChannelHandler) {
((FeatureOverChannelHandler) feature).call(_APPLY_BUILDER, channel.pipeline());
if (LOG.isDebugEnabled()) {
LOG.debug("initChannel with feature:{}", feature);
}
}
}
Nettys.applyHandler(channel.pipeline(), HttpHandlers.HTTPSERVER);
awaitInboundRequest(channel, subscriber, awaitChannels);
}
});
final ChannelFuture future = bootstrap.bind(localAddress);
try {
future.sync();
subscriber.add(RxNettys.subscriptionForCloseChannel(future.channel()));
subscriber.add(Subscriptions.create(new Action0() {
@Override
public void call() {
while (!awaitChannels.isEmpty()) {
try {
awaitChannels.remove(0).close();
} catch (Exception e) {
LOG.warn("exception when remove all awaitChannels, detail: {}", ExceptionUtils.exception2detail(e));
}
}
}
}));
if (null != features) {
final ServerChannelAware serverChannelAware = serverChannelAwareOf(features);
if (null != serverChannelAware) {
serverChannelAware.setServerChannel((ServerChannel) future.channel());
}
}
} catch (Exception e) {
subscriber.onError(e);
}
}
}
});
}
use of io.netty.channel.ServerChannel in project scalecube by scalecube.
the class TransportImpl method bind0.
/**
* Helper bind method to start accepting connections on {@code listenAddress} and {@code bindPort}.
*
* @param bindPort bind port.
* @param finalBindPort maximum port to bind.
* @throws NoSuchElementException if {@code bindPort} greater than {@code finalBindPort}.
* @throws IllegalArgumentException if {@code bindPort} doesnt belong to the range [{@link Addressing#MIN_PORT_NUMBER}
* .. {@link Addressing#MAX_PORT_NUMBER}].
*/
private CompletableFuture<Transport> bind0(ServerBootstrap server, InetAddress listenAddress, int bindPort, int finalBindPort) {
incomingMessagesSubject.subscribeOn(Schedulers.from(bootstrapFactory.getWorkerGroup()));
final CompletableFuture<Transport> result = new CompletableFuture<>();
// Perform basic bind port validation
if (bindPort < MIN_PORT_NUMBER || bindPort > MAX_PORT_NUMBER) {
result.completeExceptionally(new IllegalArgumentException("Invalid port number: " + bindPort));
return result;
}
if (bindPort > finalBindPort) {
result.completeExceptionally(new NoSuchElementException("Could not find an available port from: " + bindPort + " to: " + finalBindPort));
return result;
}
// Get address object and bind
address = Address.create(listenAddress.getHostAddress(), bindPort);
ChannelFuture bindFuture = server.bind(listenAddress, address.port());
bindFuture.addListener((ChannelFutureListener) channelFuture -> {
if (channelFuture.isSuccess()) {
serverChannel = (ServerChannel) channelFuture.channel();
networkEmulator = new NetworkEmulator(address, config.isUseNetworkEmulator());
networkEmulatorHandler = config.isUseNetworkEmulator() ? new NetworkEmulatorHandler(networkEmulator) : null;
LOGGER.info("Bound to: {}", address);
result.complete(TransportImpl.this);
} else {
Throwable cause = channelFuture.cause();
if (config.isPortAutoIncrement() && isAddressAlreadyInUseException(cause)) {
LOGGER.warn("Can't bind to address {}, try again on different port [cause={}]", address, cause.toString());
bind0(server, listenAddress, bindPort + 1, finalBindPort).thenAccept(result::complete);
} else {
LOGGER.error("Failed to bind to: {}, cause: {}", address, cause);
result.completeExceptionally(cause);
}
}
});
return result;
}
use of io.netty.channel.ServerChannel in project scalecube by scalecube.
the class NettyServerTransport method bind0.
private CompletableFuture<NettyServerTransport> bind0(InetAddress bindAddress, int bindPort, int finalBindPort) {
CompletableFuture<NettyServerTransport> result = new CompletableFuture<>();
// Perform basic bind port validation
if (bindPort < MIN_PORT_NUMBER || bindPort > MAX_PORT_NUMBER) {
result.completeExceptionally(new IllegalArgumentException("Invalid port number: " + bindPort));
return result;
}
if (bindPort > finalBindPort) {
result.completeExceptionally(new NoSuchElementException("Could not find an available port from: " + bindPort + " to: " + finalBindPort));
return result;
}
// Start binding
ChannelFuture bindFuture = serverBootstrap.bind(bindAddress, bindPort);
bindFuture.addListener((ChannelFutureListener) channelFuture -> {
if (channelFuture.isSuccess()) {
NettyServerTransport.this.init(bindAddress, bindPort, (ServerChannel) channelFuture.channel());
result.complete(NettyServerTransport.this);
} else {
Throwable cause = channelFuture.cause();
if (config.isPortAutoIncrement() && isAddressAlreadyInUseException(cause)) {
bind0(bindAddress, bindPort + 1, finalBindPort).thenAccept(result::complete);
} else {
result.completeExceptionally(cause);
}
}
});
return result;
}
use of io.netty.channel.ServerChannel in project netty by netty.
the class DefaultChannelGroup method remove.
@Override
public boolean remove(Object o) {
Channel c = null;
if (o instanceof ChannelId) {
c = nonServerChannels.remove(o);
if (c == null) {
c = serverChannels.remove(o);
}
} else if (o instanceof Channel) {
c = (Channel) o;
if (c instanceof ServerChannel) {
c = serverChannels.remove(c.id());
} else {
c = nonServerChannels.remove(c.id());
}
}
if (c == null) {
return false;
}
c.closeFuture().removeListener(remover);
return true;
}
Aggregations