当前位置: 首页>>代码示例>>Java>>正文


Java ServerChannel类代码示例

本文整理汇总了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()));
}
 
开发者ID:datastax,项目名称:simulacron,代码行数:25,代码来源:Server.java

示例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());
}
 
开发者ID:D3adspaceEnterprises,项目名称:echidna,代码行数:27,代码来源:SimpleEchidnaServer.java

示例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());
  }
}
 
开发者ID:linkedin,项目名称:flashback,代码行数:28,代码来源:ProxyServer.java

示例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;
}
 
开发者ID:wuyinxian124,项目名称:netty4.0.27Learn,代码行数:20,代码来源:DefaultChannelGroup.java

示例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);
}
 
开发者ID:blynkkk,项目名称:blynk-server,代码行数:22,代码来源:BaseServer.java

示例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;
}
 
开发者ID:nathanchen,项目名称:netty-netty-5.0.0.Alpha1,代码行数:25,代码来源:DefaultChannelGroup.java

示例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();
}
   }
 
开发者ID:ipkn,项目名称:crow-benchmark,代码行数:18,代码来源:HelloWebServer.java

示例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;
}
 
开发者ID:relayrides,项目名称:pushy,代码行数:31,代码来源:ServerSocketChannelClassUtil.java

示例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);
  }
}
 
开发者ID:Tencent,项目名称:angel,代码行数:12,代码来源:NettyUtils.java

示例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());
}
 
开发者ID:D3adspaceEnterprises,项目名称:cardea,代码行数:36,代码来源:SimpleCardeaServer.java

示例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();
}
 
开发者ID:anyflow,项目名称:lannister,代码行数:33,代码来源:MqttServer.java

示例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;
    }
}
 
开发者ID:wuyinxian124,项目名称:netty4.0.27Learn,代码行数:14,代码来源:DefaultChannelGroup.java

示例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;
}
 
开发者ID:wuyinxian124,项目名称:netty4.0.27Learn,代码行数:12,代码来源:DefaultChannelGroup.java

示例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;
}
 
开发者ID:scalecube,项目名称:scalecube,代码行数:42,代码来源:TransportImpl.java

示例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;
}
 
开发者ID:xjdr,项目名称:xio,代码行数:9,代码来源:ServerChannelConfiguration.java


注:本文中的io.netty.channel.ServerChannel类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。