當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。