本文整理汇总了Java中io.netty.channel.ServerChannel类的典型用法代码示例。如果您正苦于以下问题:Java ServerChannel类的具体用法?Java ServerChannel怎么用?Java ServerChannel使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ServerChannel类属于io.netty.channel包,在下文中一共展示了ServerChannel类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: Server
import io.netty.channel.ServerChannel; //导入依赖的package包/类
private Server(
AddressResolver addressResolver,
EventLoopGroup eventLoopGroup,
Class<? extends ServerChannel> channelClass,
boolean customEventLoop,
Timer timer,
boolean customTimer,
long bindTimeoutInNanos,
StubStore stubStore,
boolean activityLogging) {
this(
addressResolver,
eventLoopGroup,
customEventLoop,
timer,
customTimer,
bindTimeoutInNanos,
stubStore,
activityLogging,
new ServerBootstrap()
.group(eventLoopGroup)
.channel(channelClass)
.childHandler(new Initializer()));
}
示例2: start
import io.netty.channel.ServerChannel; //导入依赖的package包/类
@Override
public void start() {
this.bossGroup = NettyUtils.createEventLoopGroup(1);
this.workerGroup = NettyUtils.createEventLoopGroup(4);
Class<? extends ServerChannel> serverChannelClass = NettyUtils.getServerChannelClass();
this.logger.info("I am going to start a server on {}:{}.", this.config.getServerHost(),
this.config.getServerPort());
ServerBootstrap serverBootstrap = new ServerBootstrap();
try {
channel = serverBootstrap
.group(bossGroup, workerGroup)
.channel(serverChannelClass)
.childHandler(new ServerChannelInitializer(this))
.childOption(ChannelOption.SO_KEEPALIVE, true)
.bind(config.getServerHost(), config.getServerPort())
.sync().channel();
} catch (InterruptedException e) {
e.printStackTrace();
}
this.logger.info("Started the server on {}:{}.", this.config.getServerHost(),
this.config.getServerPort());
}
示例3: start
import io.netty.channel.ServerChannel; //导入依赖的package包/类
/**
* Start proxy server
* */
public void start()
throws InterruptedException {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(_acceptorGroup, _upstreamWorkerGroup);
serverBootstrap.channelFactory(new ChannelFactory<ServerChannel>() {
@Override
public ServerChannel newChannel() {
return new NioServerSocketChannel();
}
});
serverBootstrap.childHandler(new ProxyInitializer(this));
//bind
ChannelFuture future = serverBootstrap.bind(_host, _port);
//wait for the future
future.awaitUninterruptibly();
if (!future.isSuccess()) {
future.channel().closeFuture().awaitUninterruptibly();
throw new ChannelException(String.format("Failed to bind to: %s:%d", _host, _port), future.cause());
} else {
_allChannels.add(future.channel());
}
}
示例4: remove
import io.netty.channel.ServerChannel; //导入依赖的package包/类
@Override
public boolean remove(Object o) {
if (!(o instanceof Channel)) {
return false;
}
boolean removed;
Channel c = (Channel) o;
if (c instanceof ServerChannel) {
removed = serverChannels.remove(c);
} else {
removed = nonServerChannels.remove(c);
}
if (!removed) {
return false;
}
c.closeFuture().removeListener(remover);
return true;
}
示例5: buildServerAndRun
import io.netty.channel.ServerChannel; //导入依赖的package包/类
private void buildServerAndRun(EventLoopGroup bossGroup, EventLoopGroup workerGroup,
Class<? extends ServerChannel> channelClass) throws Exception {
ServerBootstrap b = new ServerBootstrap();
try {
b.group(bossGroup, workerGroup)
.channel(channelClass)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childHandler(getChannelInitializer());
InetSocketAddress listenTo = (listenAddress == null || listenAddress.isEmpty())
? new InetSocketAddress(port)
: new InetSocketAddress(listenAddress, port);
this.cf = b.bind(listenTo).sync();
} catch (Exception e) {
log.error("Error initializing {}, port {}", getServerName(), port, e);
throw e;
}
log.info("{} server listening at {} port.", getServerName(), port);
}
示例6: remove
import io.netty.channel.ServerChannel; //导入依赖的package包/类
@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;
}
示例7: doRun
import io.netty.channel.ServerChannel; //导入依赖的package包/类
private void doRun(EventLoopGroup loupGroup, Class<? extends ServerChannel> serverChannelClass) throws InterruptedException {
try {
ServerBootstrap b = new ServerBootstrap();
b.option(ChannelOption.SO_BACKLOG, 1024);
b.option(ChannelOption.SO_REUSEADDR, true);
b.group(loupGroup).channel(serverChannelClass).childHandler(new HelloServerInitializer(loupGroup.next()));
b.option(ChannelOption.MAX_MESSAGES_PER_READ, Integer.MAX_VALUE);
b.childOption(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(true));
b.childOption(ChannelOption.SO_REUSEADDR, true);
b.childOption(ChannelOption.MAX_MESSAGES_PER_READ, Integer.MAX_VALUE);
Channel ch = b.bind(port).sync().channel();
ch.closeFuture().sync();
} finally {
loupGroup.shutdownGracefully().sync();
}
}
示例8: getServerSocketChannelClass
import io.netty.channel.ServerChannel; //导入依赖的package包/类
/**
* Returns a server socket channel class suitable for specified event loop group.
*
* @param eventLoopGroup the event loop group for which to identify an appropriate socket channel class; must not
* be {@code null}
*
* @return a server socket channel class suitable for use with the given event loop group
*
* @throws IllegalArgumentException in case of null or unrecognized event loop group
*/
@SuppressWarnings("unchecked")
static Class<? extends ServerChannel> getServerSocketChannelClass(final EventLoopGroup eventLoopGroup) {
Objects.requireNonNull(eventLoopGroup);
final Class<? extends ServerChannel> serverSocketChannelClass;
if (eventLoopGroup instanceof NioEventLoopGroup) {
serverSocketChannelClass = NioServerSocketChannel.class;
} else if (eventLoopGroup instanceof OioEventLoopGroup) {
serverSocketChannelClass = OioServerSocketChannel.class;
} else if (EPOLL_EVENT_LOOP_GROUP_CLASS.equals(eventLoopGroup.getClass().getName())) {
serverSocketChannelClass = (Class<? extends ServerChannel>) loadSocketChannelClass(EPOLL_SERVER_SOCKET_CHANNEL_CLASS);
} else if (KQUEUE_EVENT_LOOP_GROUP_CLASS.equals(eventLoopGroup.getClass().getName())) {
serverSocketChannelClass = (Class<? extends ServerChannel>) loadSocketChannelClass(KQUEUE_SERVER_SOCKET_CHANNEL_CLASS);
} else {
throw new IllegalArgumentException("Could not find server socket class for event loop group class: " + eventLoopGroup.getClass().getName());
}
return serverSocketChannelClass;
}
示例9: getServerChannelClass
import io.netty.channel.ServerChannel; //导入依赖的package包/类
/** Returns the correct ServerSocketChannel class based on IOMode. */
public static Class<? extends ServerChannel> getServerChannelClass(IOMode mode) {
switch (mode) {
case NIO:
return NioServerSocketChannel.class;
case EPOLL:
return EpollServerSocketChannel.class;
default:
throw new IllegalArgumentException("Unknown io mode: " + mode);
}
}
示例10: start
import io.netty.channel.ServerChannel; //导入依赖的package包/类
@Override
public void start() {
this.bossGroup = NettyUtils.createEventLoopGroup(1);
this.workerGroup = new NioEventLoopGroup(4);
Class<? extends ServerChannel> serverChannelClazz = NettyUtils.getServerChannelClass();
ChannelHandler channelHandler = new CardeaServerChannelInitializer(this.backendManager);
this.logger.info("Starting backend handling tasks.");
this.executorService
.scheduleAtFixedRate(new CheckDeadBackendsTask(this.backendManager), 10, 10,
TimeUnit.SECONDS);
this.executorService
.scheduleAtFixedRate(new BackendRecoverTask(this.backendManager), 10, 10,
TimeUnit.SECONDS);
this.logger.info("Starting server and proxying all connections on *:",
this.config.getServerPort());
ServerBootstrap serverBootstrap = new ServerBootstrap();
try {
serverBootstrap
.channel(serverChannelClazz)
.group(this.bossGroup, this.workerGroup)
.childHandler(channelHandler)
.childOption(ChannelOption.AUTO_READ, false)
.bind(this.config.getServerPort())
.sync().channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
}
this.logger.info("Started reverse proxy on *:", this.config.getServerPort());
}
示例11: executeBootstrap
import io.netty.channel.ServerChannel; //导入依赖的package包/类
private void executeBootstrap(ScheduledExecutor scheduledExecutor, int port, boolean useWebSocket, boolean useSsl)
throws InterruptedException {
ServerBootstrap bootstrap = new ServerBootstrap();
Class<? extends ServerChannel> serverChannelClass;
if (Literals.NETTY_EPOLL.equals(Settings.INSTANCE.nettyTransportMode())) {
serverChannelClass = EpollServerSocketChannel.class;
}
else {
serverChannelClass = NioServerSocketChannel.class;
}
bootstrap = bootstrap.group(bossGroup, workerGroup).channel(serverChannelClass);
bootstrap.option(ChannelOption.TCP_NODELAY, true);
if (scheduledExecutor != null) {
bootstrap.handler(scheduledExecutor);
}
bootstrap.childHandler(new MqttChannelInitializer(useWebSocket, useSsl));
bootstrap.childOption(ChannelOption.TCP_NODELAY, true)
// setting buffer size can improve I/O
.childOption(ChannelOption.SO_SNDBUF, 1048576).childOption(ChannelOption.SO_RCVBUF, 1048576)
// recommended in
// http://normanmaurer.me/presentations/2014-facebook-eng-netty/slides.html#11.0
.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(8 * 1024, 32 * 1024))
.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
bootstrap.bind(port).sync();
}
示例12: contains
import io.netty.channel.ServerChannel; //导入依赖的package包/类
@Override
public boolean contains(Object o) {
if (o instanceof Channel) {
Channel c = (Channel) o;
if (o instanceof ServerChannel) {
return serverChannels.contains(c);
} else {
return nonServerChannels.contains(c);
}
} else {
return false;
}
}
示例13: add
import io.netty.channel.ServerChannel; //导入依赖的package包/类
@Override
public boolean add(Channel channel) {
ConcurrentSet<Channel> set =
channel instanceof ServerChannel? serverChannels : nonServerChannels;
boolean added = set.add(channel);
if (added) {
channel.closeFuture().addListener(remover);
}
return added;
}
示例14: bind0
import io.netty.channel.ServerChannel; //导入依赖的package包/类
/**
* Starts to accept connections on local address.
*/
public CompletableFuture<Transport> bind0() {
incomingMessagesSubject.subscribeOn(Schedulers.from(bootstrapFactory.getWorkerGroup()));
// Resolve listen IP address
final InetAddress listenAddress =
Addressing.getLocalIpAddress(config.getListenAddress(), config.getListenInterface(), config.isPreferIPv6());
// Resolve listen port
int bindPort = config.isPortAutoIncrement()
? Addressing.getNextAvailablePort(listenAddress, config.getPort(), config.getPortCount()) // Find available port
: config.getPort();
// Listen address
address = Address.create(listenAddress.getHostAddress(), bindPort);
ServerBootstrap server = bootstrapFactory.serverBootstrap().childHandler(incomingChannelInitializer);
ChannelFuture bindFuture = server.bind(listenAddress, address.port());
final CompletableFuture<Transport> result = new CompletableFuture<>();
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().thenAccept(result::complete);
} else {
LOGGER.error("Failed to bind to: {}, cause: {}", address, cause);
result.completeExceptionally(cause);
}
}
});
return result;
}
示例15: ServerChannelConfiguration
import io.netty.channel.ServerChannel; //导入依赖的package包/类
ServerChannelConfiguration(
EventLoopGroup bossGroup,
EventLoopGroup workerGroup,
Class<? extends ServerChannel> channelClass) {
this.bossGroup = bossGroup;
this.workerGroup = workerGroup;
this.channelClass = channelClass;
}