io.netty.bootstrap.ChannelFactory Java Examples

The following examples show how to use io.netty.bootstrap.ChannelFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractNettyServer.java    From spring-boot-protocol with Apache License 2.0 6 votes vote down vote up
public void init() throws Exception {
    if(initFlag.compareAndSet(false,true)) {
        this.bootstrap = newServerBootstrap();
        this.boss = newBossEventLoopGroup();
        this.worker = newWorkerEventLoopGroup();
        ChannelFactory<? extends ServerChannel> channelFactory = newServerChannelFactory();
        ChannelHandler bossChannelHandler = newBossChannelHandler();
        ChannelHandler workerChannelHandler = newWorkerChannelHandler();

        if (bossChannelHandler != null) {
            bootstrap.handler(bossChannelHandler);
        }
        bootstrap.group(boss, worker)
                .channelFactory(channelFactory)
                .childHandler(workerChannelHandler);
        config(bootstrap);
    }
}
 
Example #2
Source File: AbstractNettyClient.java    From spring-boot-protocol with Apache License 2.0 6 votes vote down vote up
protected AbstractNettyClient init() {
        this.bootstrap = newClientBootstrap();
        this.worker = newWorkerEventLoopGroup();
        ChannelFactory<?extends Channel> channelFactory = newClientChannelFactory();
        ChannelHandler bossChannelHandler = newBossChannelHandler();

        this.bootstrap
                .group(worker)
                .channelFactory(channelFactory)
                .handler(bossChannelHandler)
                .remoteAddress(remoteAddress)
                //用于构造服务端套接字ServerSocket对象,标识当服务器请求处理线程全满时,用于临时存放已完成三次握手的请求的队列的最大长度
//                    .option(ChannelOption.SO_BACKLOG, 1024) // determining the number of connections queued
                //netty boos的默认内存分配器
//                    .option(ChannelOption.ALLOCATOR, ByteBufAllocatorX.INSTANCE)
                //禁用Nagle算法,即数据包立即发送出去 (在TCP_NODELAY模式下,假设有3个小包要发送,第一个小包发出后,接下来的小包需要等待之前的小包被ack,在这期间小包会合并,直到接收到之前包的ack后才会发生)
                .option(ChannelOption.TCP_NODELAY, true)
                //开启TCP/IP协议实现的心跳机制
                .option(ChannelOption.SO_KEEPALIVE, true)
                //netty的默认内存分配器
                .option(ChannelOption.ALLOCATOR, ByteBufAllocatorX.INSTANCE);
        return this;
    }
 
Example #3
Source File: ProxyServer.java    From flashback with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * 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());
  }
}
 
Example #4
Source File: ProxyToServerConnection.java    From g4proxy with Apache License 2.0 5 votes vote down vote up
@Override
        protected Future<?> execute() {
            Bootstrap cb = new Bootstrap().group(proxyServer.getProxyToServerWorkerFor(transportProtocol));

            switch (transportProtocol) {
            case TCP:
                LOG.debug("Connecting to server with TCP");
                cb.channelFactory(new ChannelFactory<Channel>() {
                    @Override
                    public Channel newChannel() {
                        return new NioSocketChannel();
                    }
                });
                break;
            case UDT:
                LOG.debug("Connecting to server with UDT");
//                cb.channelFactory(NioUdtProvider.BYTE_CONNECTOR)
//                        .option(ChannelOption.SO_REUSEADDR, true);
//                break;
                throw new UnsupportedOperationException("unsupport udt proxy portocal");
            default:
                throw new UnknownTransportProtocolException(transportProtocol);
            }

            cb.handler(new ChannelInitializer<Channel>() {
                protected void initChannel(Channel ch) throws Exception {
                    initChannelPipeline(ch.pipeline(), initialRequest);
                };
            });
            cb.option(ChannelOption.CONNECT_TIMEOUT_MILLIS,
                    proxyServer.getConnectTimeout());

            if (localAddress != null) {
                return cb.connect(remoteAddress, localAddress);
            } else {
                return cb.connect(remoteAddress);
            }
        }
 
Example #5
Source File: AbstractNettyServer.java    From spring-boot-protocol with Apache License 2.0 5 votes vote down vote up
protected ChannelFactory<? extends ServerChannel> newServerChannelFactory() {
    ChannelFactory<? extends ServerChannel> channelFactory;
    if(enableEpoll){
        channelFactory = EpollServerSocketChannel::new;
    }else {
        channelFactory = NioServerSocketChannel::new;
    }
    return channelFactory;
}
 
Example #6
Source File: AbstractNettyClient.java    From spring-boot-protocol with Apache License 2.0 5 votes vote down vote up
protected ChannelFactory<? extends Channel> newClientChannelFactory() {
    ChannelFactory<? extends Channel> channelFactory;
    if(enableEpoll){
        channelFactory = EpollSocketChannel::new;
    }else {
        channelFactory = NioSocketChannel::new;
    }
    return channelFactory;
}
 
Example #7
Source File: ProxyToServerConnection.java    From yfs with Apache License 2.0 5 votes vote down vote up
@Override
protected Future<?> execute() {
    Bootstrap cb = new Bootstrap().group(proxyServer.getProxyToServerWorkerFor(transportProtocol));

    switch (transportProtocol) {
        case TCP:
            LOG.debug("Connecting to server with TCP");
            cb.channelFactory(new ChannelFactory<Channel>() {
                @Override
                public Channel newChannel() {
                    return new NioSocketChannel();
                }
            });
            break;
        case UDT:
            LOG.debug("Connecting to server with UDT");
            cb.channelFactory(NioUdtProvider.BYTE_CONNECTOR)
                    .option(ChannelOption.SO_REUSEADDR, true);
            break;
        default:
            throw new UnknownTransportProtocolException(transportProtocol);
    }

    cb.handler(new ChannelInitializer<Channel>() {
        protected void initChannel(Channel ch) throws Exception {
            initChannelPipeline(ch.pipeline(), initialRequest);
        };
    });
    cb.option(ChannelOption.CONNECT_TIMEOUT_MILLIS,
            proxyServer.getConnectTimeout());

    if (localAddress != null) {
        return cb.connect(remoteAddress, localAddress);
    } else {
        return cb.connect(remoteAddress);
    }
}
 
Example #8
Source File: SocketTestPermutation.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
public List<BootstrapComboFactory<Bootstrap, Bootstrap>> datagram() {
    // Make the list of Bootstrap factories.
    List<BootstrapFactory<Bootstrap>> bfs = Arrays.asList(
            new BootstrapFactory<Bootstrap>() {
                @Override
                public Bootstrap newInstance() {
                    return new Bootstrap().group(nioWorkerGroup).channelFactory(new ChannelFactory<Channel>() {
                        @Override
                        public Channel newChannel() {
                            return new NioDatagramChannel(InternetProtocolFamily.IPv4);
                        }

                        @Override
                        public String toString() {
                            return NioDatagramChannel.class.getSimpleName() + ".class";
                        }
                    });
                }
            },
            new BootstrapFactory<Bootstrap>() {
                @Override
                public Bootstrap newInstance() {
                    return new Bootstrap().group(oioWorkerGroup).channel(OioDatagramChannel.class);
                }
            }
    );

    // Populare the combinations.
    return combo(bfs, bfs);
}
 
Example #9
Source File: EpollSocketTestPermutation.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
public List<TestsuitePermutation.BootstrapComboFactory<Bootstrap, Bootstrap>> datagram() {
    // Make the list of Bootstrap factories.
    List<BootstrapFactory<Bootstrap>> bfs = Arrays.asList(
            new BootstrapFactory<Bootstrap>() {
                @Override
                public Bootstrap newInstance() {
                    return new Bootstrap().group(nioWorkerGroup).channelFactory(new ChannelFactory<Channel>() {
                        @Override
                        public Channel newChannel() {
                            return new NioDatagramChannel(InternetProtocolFamily.IPv4);
                        }

                        @Override
                        public String toString() {
                            return NioDatagramChannel.class.getSimpleName() + ".class";
                        }
                    });
                }
            },
            new BootstrapFactory<Bootstrap>() {
                @Override
                public Bootstrap newInstance() {
                    return new Bootstrap().group(EPOLL_WORKER_GROUP).channel(EpollDatagramChannel.class);
                }
            }
    );
    return combo(bfs, bfs);
}