/** * 启动端口绑定 * @param local * @return */ protected final boolean bind(InetSocketAddress local) { boolean isBind=false; try { log.debug(getName()+"端口绑定中……"+local.toString()); ChannelFuture cf=doBind(local); isBind=cf.channel()!=null && cf.channel().isActive(); if(isBind) { log.debug(getName()+"端口绑定成功!"+cf.channel()); serverCahnel=(ServerSocketChannel) cf.channel(); }else { log.debug(getName()+"端口绑定失败!"+cf.channel()); } } catch (Exception e) { log.error(e.getMessage(),e); throw e; } return isBind; }
@Override public void init(final InetAddress address, final int port, final boolean useEpoll) { final Class<? extends ServerSocketChannel> socketChannelClass; final LazyValue<? extends EventLoopGroup> lazyInit; if ((Epoll.isAvailable()) && useEpoll) { socketChannelClass = EpollServerSocketChannel.class; lazyInit = this.epollEventLoopGroupLazyValue; CoreMain.debug("[Netty] Using epoll channel type"); } else { socketChannelClass = NioServerSocketChannel.class; lazyInit = this.nioEventLoopGroupLazyValue; CoreMain.debug("[Netty] Using default channel type"); } this.channelFuture = new ServerBootstrap().channel(socketChannelClass).childHandler(new ServerConnectionChannel(this)).group(lazyInit.get()).localAddress(address, port).bind().syncUninterruptibly(); }
protected void startServer() throws InterruptedException { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new LoggingHandler(LogLevel.DEBUG)); p.addLast(new NettySimpleMessageHandler()); p.addLast(new NettyMasterHandler(DefaultRedisKeeperServer.this, new CommandHandlerManager(), keeperConfig.getTrafficReportIntervalMillis())); } }); serverSocketChannel = (ServerSocketChannel) b.bind(currentKeeperMeta.getPort()).sync().channel(); }
public ClassLoaderServer(ClassLoader classLoader) throws UnknownHostException, InterruptedException { String codeBase = System.getProperty("java.rmi.server.codebase", null); if (codeBase == null || codeBase.matches("[0-9]+")) { Transport transport = Modules.getInstance().getTransport(); int port = (codeBase == null) ? 0 : Integer.valueOf(codeBase); httpChannel = ClassLoaderServer.run(transport.getAcceptGroup(), transport.getWorkerGroup(), port, classLoader); InetSocketAddress inetSocketAddress = ((ServerSocketChannel) httpChannel).localAddress(); port = inetSocketAddress.getPort(); String hostName = Modules.getInstance().getConfiguration().getServerHostName(); if(hostName == null){ hostName = InetAddress.getLocalHost().getHostName(); } System.setProperty("java.rmi.server.codebase", "http://" + hostName + ":" + port + "/"); LoaderHandler.loadCodeBaseProperty(); logger.info("Embedded HTTP server run at {} java.rmi.server.codebase is set to {} ", inetSocketAddress, System.getProperty("java.rmi.server.codebase")); } else { httpChannel = null; } }
<T extends ServerSocketChannel> RpcServer(EventLoopGroup eventLoopGroup, EventExecutorGroup eventExecutor, Class<T> channel, SocketAddress address) { this.address = address; this.allChannels = new DefaultChannelGroup(eventLoopGroup.next()); this.handler = new ServerHandler(allChannels); this.bootstrap = new ServerBootstrap(); bootstrap.channel(channel); bootstrap.childHandler(new ServerInitializer(eventExecutor, handler)); bootstrap.group(eventLoopGroup); bootstrap.option(ChannelOption.TCP_NODELAY, true); }
public static Class<? extends ServerSocketChannel> getServerSocketChannel(){ if(SUPPORTS_EPOLL){ return EpollServerSocketChannel.class; }else{ return NioServerSocketChannel.class; } }
public static Class<? extends ServerSocketChannel> getServerSocketChannelClass(EventLoopGroup eventLoopGroup) { if (eventLoopGroup instanceof EpollEventLoopGroup) { return EpollServerSocketChannel.class; } else { return NioServerSocketChannel.class; } }
/** * Return remote address if remote channel {@link NettyContext} otherwise local * address if server selector channel. * * @return remote or local {@link InetSocketAddress} */ default InetSocketAddress address(){ Channel c = channel(); if (c instanceof SocketChannel) { return ((SocketChannel) c).remoteAddress(); } if (c instanceof ServerSocketChannel) { return ((ServerSocketChannel) c).localAddress(); } if (c instanceof DatagramChannel) { InetSocketAddress a = ((DatagramChannel) c).remoteAddress(); return a != null ? a : ((DatagramChannel)c ).localAddress(); } throw new IllegalStateException("Does not have an InetSocketAddress"); }
HttpServer(Supplier<EventLoopGroup> eventLoopFactory, ServerChannelFactory<ServerSocketChannel> channelFactory, ChannelInitializer channelInitializer, HttpEventBus eventBus) { super(eventLoopFactory, channelFactory, channelInitializer); this.eventBus = eventBus; }
public Server(Supplier<EventLoopGroup> eventLoopFactory, ServerChannelFactory<ServerSocketChannel> channelFactory, ChannelInitializer channelInitializer) { this.bossGroup = eventLoopFactory.get(); this.workerGroup = eventLoopFactory.get(); serverBootstrap = new ServerBootstrap() .group(bossGroup, workerGroup) .channelFactory(channelFactory) .childHandler(channelInitializer); }
@Override protected boolean isCompatible(EventLoop loop) { if (!(loop instanceof XnioEventLoop)) { return false; } ServerSocketChannel parent = parent(); if (parent != null) { // if this channel has a parent we need to ensure that both EventLoopGroups are the same for XNIO // to be sure it uses a Thread from the correct Worker. if (parent.eventLoop().parent() != loop.parent()) { return false; } } return true; }
public static Class<? extends ServerSocketChannel> serverSocketChannel() { return EPOLL ? EpollServerSocketChannel.class : NioServerSocketChannel.class; }
/** * Adds a channel that listens on publicly accessible network ports */ public void addLanEndpoint(InetAddress address, int port) throws IOException { synchronized (this.endpoints) { Class <? extends ServerSocketChannel > oclass; LazyLoadBase <? extends EventLoopGroup > lazyloadbase; if (Epoll.isAvailable() && this.mcServer.func_181035_ah()) { oclass = EpollServerSocketChannel.class; lazyloadbase = field_181141_b; logger.info("Using epoll channel type"); } else { oclass = NioServerSocketChannel.class; lazyloadbase = eventLoops; logger.info("Using default channel type"); } this.endpoints.add(((ServerBootstrap)((ServerBootstrap)(new ServerBootstrap()).channel(oclass)).childHandler(new ChannelInitializer<Channel>() { protected void initChannel(Channel p_initChannel_1_) throws Exception { try { p_initChannel_1_.config().setOption(ChannelOption.TCP_NODELAY, Boolean.valueOf(true)); } catch (ChannelException var3) { ; } p_initChannel_1_.pipeline().addLast((String)"timeout", (ChannelHandler)(new ReadTimeoutHandler(30))).addLast((String)"legacy_query", (ChannelHandler)(new PingResponseHandler(NetworkSystem.this))).addLast((String)"splitter", (ChannelHandler)(new MessageDeserializer2())).addLast((String)"decoder", (ChannelHandler)(new MessageDeserializer(EnumPacketDirection.SERVERBOUND))).addLast((String)"prepender", (ChannelHandler)(new MessageSerializer2())).addLast((String)"encoder", (ChannelHandler)(new MessageSerializer(EnumPacketDirection.CLIENTBOUND))); NetworkManager networkmanager = new NetworkManager(EnumPacketDirection.SERVERBOUND); NetworkSystem.this.networkManagers.add(networkmanager); p_initChannel_1_.pipeline().addLast((String)"packet_handler", (ChannelHandler)networkmanager); networkmanager.setNetHandler(new NetHandlerHandshakeTCP(NetworkSystem.this.mcServer, networkmanager)); } }).group((EventLoopGroup)lazyloadbase.getValue()).localAddress(address, port)).bind().syncUninterruptibly()); } }
/** * Adds a channel that listens on publicly accessible network ports */ public void addLanEndpoint(InetAddress address, int port) throws IOException { synchronized (this.endpoints) { Class <? extends ServerSocketChannel > oclass; LazyLoadBase <? extends EventLoopGroup > lazyloadbase; if (Epoll.isAvailable() && this.mcServer.shouldUseNativeTransport()) { oclass = EpollServerSocketChannel.class; lazyloadbase = SERVER_EPOLL_EVENTLOOP; LOGGER.info("Using epoll channel type"); } else { oclass = NioServerSocketChannel.class; lazyloadbase = SERVER_NIO_EVENTLOOP; LOGGER.info("Using default channel type"); } this.endpoints.add(((ServerBootstrap)((ServerBootstrap)(new ServerBootstrap()).channel(oclass)).childHandler(new ChannelInitializer<Channel>() { protected void initChannel(Channel p_initChannel_1_) throws Exception { try { p_initChannel_1_.config().setOption(ChannelOption.TCP_NODELAY, Boolean.valueOf(true)); } catch (ChannelException var3) { ; } p_initChannel_1_.pipeline().addLast((String)"timeout", (ChannelHandler)(new ReadTimeoutHandler(30))).addLast((String)"legacy_query", (ChannelHandler)(new LegacyPingHandler(NetworkSystem.this))).addLast((String)"splitter", (ChannelHandler)(new NettyVarint21FrameDecoder())).addLast((String)"decoder", (ChannelHandler)(new NettyPacketDecoder(EnumPacketDirection.SERVERBOUND))).addLast((String)"prepender", (ChannelHandler)(new NettyVarint21FrameEncoder())).addLast((String)"encoder", (ChannelHandler)(new NettyPacketEncoder(EnumPacketDirection.CLIENTBOUND))); NetworkManager networkmanager = new NetworkManager(EnumPacketDirection.SERVERBOUND); NetworkSystem.this.networkManagers.add(networkmanager); p_initChannel_1_.pipeline().addLast((String)"packet_handler", (ChannelHandler)networkmanager); networkmanager.setNetHandler(new NetHandlerHandshakeTCP(NetworkSystem.this.mcServer, networkmanager)); } }).group((EventLoopGroup)lazyloadbase.getValue()).localAddress(address, port)).bind().syncUninterruptibly()); } }
/** * Adds a channel that listens on publicly accessible network ports */ public void addLanEndpoint(InetAddress address, int port) throws IOException { synchronized (this.endpoints) { Class <? extends ServerSocketChannel > oclass; LazyLoadBase <? extends EventLoopGroup > lazyloadbase; if (Epoll.isAvailable() && this.mcServer.shouldUseNativeTransport()) { oclass = EpollServerSocketChannel.class; lazyloadbase = SERVER_EPOLL_EVENTLOOP; LOGGER.info("Using epoll channel type"); } else { oclass = NioServerSocketChannel.class; lazyloadbase = SERVER_NIO_EVENTLOOP; LOGGER.info("Using default channel type"); } this.endpoints.add(((ServerBootstrap)((ServerBootstrap)(new ServerBootstrap()).channel(oclass)).childHandler(new ChannelInitializer<Channel>() { protected void initChannel(Channel p_initChannel_1_) throws Exception { try { p_initChannel_1_.config().setOption(ChannelOption.TCP_NODELAY, Boolean.valueOf(true)); } catch (ChannelException var3) { ; } p_initChannel_1_.pipeline().addLast((String)"timeout", (ChannelHandler)(new ReadTimeoutHandler(net.minecraftforge.fml.common.network.internal.FMLNetworkHandler.READ_TIMEOUT))).addLast((String)"legacy_query", (ChannelHandler)(new LegacyPingHandler(NetworkSystem.this))).addLast((String)"splitter", (ChannelHandler)(new NettyVarint21FrameDecoder())).addLast((String)"decoder", (ChannelHandler)(new NettyPacketDecoder(EnumPacketDirection.SERVERBOUND))).addLast((String)"prepender", (ChannelHandler)(new NettyVarint21FrameEncoder())).addLast((String)"encoder", (ChannelHandler)(new NettyPacketEncoder(EnumPacketDirection.CLIENTBOUND))); NetworkManager networkmanager = new NetworkManager(EnumPacketDirection.SERVERBOUND); NetworkSystem.this.networkManagers.add(networkmanager); p_initChannel_1_.pipeline().addLast((String)"packet_handler", (ChannelHandler)networkmanager); networkmanager.setNetHandler(new NetHandlerHandshakeTCP(NetworkSystem.this.mcServer, networkmanager)); } }).group((EventLoopGroup)lazyloadbase.getValue()).localAddress(address, port)).bind().syncUninterruptibly()); } }
private Class<? extends ServerSocketChannel> socketChannelClass() { return USE_EPOLL ? EpollServerSocketChannel.class : NioServerSocketChannel.class; }
@Override public InetSocketAddress address() { return ((ServerSocketChannel) f.channel()).localAddress(); }
@Override public ServerSocketChannel parent() { return (ServerSocketChannel) super.parent(); }
@Deprecated public DefaultOioServerSocketChannelConfig(ServerSocketChannel channel, ServerSocket javaSocket) { super(channel, javaSocket); }
@Override protected Class<? extends ServerSocketChannel> newChannel() { return NioServerSocketChannel.class; }
private Class<? extends ServerSocketChannel> serverChannelClass() { return isEpollSupported() ? EpollServerSocketChannel.class : NioServerSocketChannel.class; }
@Nonnull public InetSocketAddress getLocalAddress() { ServerSocketChannel channel = (ServerSocketChannel) future.channel(); return channel.localAddress(); }
public DefaultOioServerSocketChannelConfig(ServerSocketChannel channel, ServerSocket javaSocket) { super(channel, javaSocket); }