Search in sources :

Example 1 with Http2Codec

use of com.firenio.codec.http2.Http2Codec in project baseio by generallycloud.

the class TestHttp2Server method main.

public static void main(String[] args) throws Exception {
    IoEventHandle eventHandleAdaptor = new IoEventHandle() {

        @Override
        public void accept(Channel ch, Frame frame) throws Exception {
            frame.write("Hello World", ch);
            ch.writeAndFlush(frame);
        }
    };
    NioEventLoopGroup group = new NioEventLoopGroup();
    group.setMemoryCapacity(1024 * 1024 * 4);
    group.setMemoryUnit(512);
    group.setEnableMemoryPool(true);
    ChannelAcceptor context = new ChannelAcceptor(group, 443);
    context.addProtocolCodec(new Http2Codec());
    context.setIoEventHandle(eventHandleAdaptor);
    // context.setApplicationProtocols(new String[]{"h2", "http/1.1"});
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.bind();
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) Frame(com.firenio.component.Frame) Channel(com.firenio.component.Channel) ChannelAcceptor(com.firenio.component.ChannelAcceptor) NioEventLoopGroup(com.firenio.component.NioEventLoopGroup) Http2Codec(com.firenio.codec.http2.Http2Codec) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener)

Example 2 with Http2Codec

use of com.firenio.codec.http2.Http2Codec in project jersey by jersey.

the class JerseyServerInitializer method configureClearText.

/**
     * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.
     */
private void configureClearText(SocketChannel ch) {
    final ChannelPipeline p = ch.pipeline();
    final HttpServerCodec sourceCodec = new HttpServerCodec();
    p.addLast(sourceCodec);
    p.addLast(new HttpServerUpgradeHandler(sourceCodec, new HttpServerUpgradeHandler.UpgradeCodecFactory() {

        @Override
        public HttpServerUpgradeHandler.UpgradeCodec newUpgradeCodec(CharSequence protocol) {
            if (AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) {
                return new Http2ServerUpgradeCodec(new Http2Codec(true, new JerseyHttp2ServerHandler(baseUri, container)));
            } else {
                return null;
            }
        }
    }));
    p.addLast(new SimpleChannelInboundHandler<HttpMessage>() {

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws Exception {
            // If this handler is hit then no upgrade has been attempted and the client is just talking HTTP.
            // "Directly talking: " + msg.protocolVersion() + " (no upgrade was attempted)");
            ChannelPipeline pipeline = ctx.pipeline();
            ChannelHandlerContext thisCtx = pipeline.context(this);
            pipeline.addAfter(thisCtx.name(), null, new JerseyServerHandler(baseUri, container));
            pipeline.replace(this, null, new ChunkedWriteHandler());
            ctx.fireChannelRead(msg);
        }
    });
}
Also used : Http2ServerUpgradeCodec(io.netty.handler.codec.http2.Http2ServerUpgradeCodec) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpServerUpgradeHandler(io.netty.handler.codec.http.HttpServerUpgradeHandler) ChannelPipeline(io.netty.channel.ChannelPipeline) Http2Codec(io.netty.handler.codec.http2.Http2Codec) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) HttpMessage(io.netty.handler.codec.http.HttpMessage)

Example 3 with Http2Codec

use of com.firenio.codec.http2.Http2Codec in project baseio by generallycloud.

the class TestHttpBootstrapEngine method bootstrap.

@Override
public void bootstrap(String rootPath, boolean prodMode) throws Exception {
    ClassLoader cl = this.getClass().getClassLoader();
    boolean debug = Util.isTrueValue(System.getProperty("http.debug"));
    if (debug) {
        for (; debug; ) {
            Util.sleep(100);
        }
    }
    DevelopConfig.NATIVE_DEBUG = true;
    DevelopConfig.BUF_DEBUG = true;
    DevelopConfig.BUF_PATH_DEBUG = true;
    DevelopConfig.SSL_DEBUG = true;
    DevelopConfig.CHANNEL_DEBUG = true;
    DevelopConfig.DEBUG_ERROR = true;
    Options.setEnableEpoll(true);
    Options.setEnableUnsafe(true);
    Options.setEnableOpenssl(true);
    Options.setBufThreadYield(true);
    // Options.setEnableUnsafeBuf(true);
    HttpDateUtil.start();
    final SpringHttpFrameHandle handle = new SpringHttpFrameHandle();
    Properties properties = FileUtil.readPropertiesByCls("server.properties", cl);
    NioEventLoopGroup group = new NioEventLoopGroup(true);
    ChannelAcceptor context = new ChannelAcceptor(group);
    ConfigurationParser.parseConfiguration("server.", context, properties);
    ConfigurationParser.parseConfiguration("server.", group, properties);
    context.setIoEventHandle(handle);
    context.addChannelIdleEventListener(new ChannelAliveListener());
    context.addChannelEventListener(new WebSocketChannelListener());
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.addChannelEventListener(new CountChannelListener());
    context.setExecutorGroup(new ThreadEventLoopGroup());
    context.addLifeCycleListener(new LifeCycleListener() {

        @Override
        public void lifeCycleStarting(LifeCycle lifeCycle) {
            try {
                handle.initialize(context, rootPath, prodMode);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }

        @Override
        public void lifeCycleStopped(LifeCycle lifeCycle) {
            handle.destroy(context);
        }
    });
    String[] applicationProtocols = null;
    if (properties.getBooleanProperty("app.enableHttp2")) {
        context.addProtocolCodec(new Http2Codec());
        applicationProtocols = new String[] { "h2", "http/1.1" };
    } else {
        context.addProtocolCodec(new HttpCodec(4));
        context.addProtocolCodec(new WebSocketCodec());
    }
    int defaultPort = 80;
    String pem = properties.getProperty("server.sslPem");
    if (!Util.isNullOrBlank(pem)) {
        defaultPort = 443;
        SslContext sslContext = loadSslContextFromPem(pem, applicationProtocols, cl);
        context.setSslContext(sslContext);
    }
    int port = properties.getIntegerProperty("server.port", defaultPort);
    context.setPort(port);
    try {
        context.bind();
    } catch (Exception e) {
        HttpDateUtil.stop();
        group.stop();
        throw e;
    }
    this.group = group;
    this.context = context;
    if (properties.getBooleanProperty("app.proxy")) {
        NetDataTransferServer.get().startup(group, 18088);
    }
}
Also used : LifeCycle(com.firenio.LifeCycle) ChannelAliveListener(com.firenio.component.ChannelAliveListener) ChannelAcceptor(com.firenio.component.ChannelAcceptor) LifeCycleListener(com.firenio.LifeCycleListener) Properties(com.firenio.common.Properties) WebSocketChannelListener(com.firenio.codec.http11.WebSocketChannelListener) SSLException(javax.net.ssl.SSLException) Http2Codec(com.firenio.codec.http2.Http2Codec) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener) ThreadEventLoopGroup(com.firenio.concurrent.ThreadEventLoopGroup) HttpCodec(com.firenio.codec.http11.HttpCodec) CountChannelListener(sample.http11.service.CountChannelListener) SpringHttpFrameHandle(sample.http11.SpringHttpFrameHandle) NioEventLoopGroup(com.firenio.component.NioEventLoopGroup) WebSocketCodec(com.firenio.codec.http11.WebSocketCodec) SslContext(com.firenio.component.SslContext)

Aggregations

Http2Codec (com.firenio.codec.http2.Http2Codec)2 ChannelAcceptor (com.firenio.component.ChannelAcceptor)2 LoggerChannelOpenListener (com.firenio.component.LoggerChannelOpenListener)2 NioEventLoopGroup (com.firenio.component.NioEventLoopGroup)2 LifeCycle (com.firenio.LifeCycle)1 LifeCycleListener (com.firenio.LifeCycleListener)1 HttpCodec (com.firenio.codec.http11.HttpCodec)1 WebSocketChannelListener (com.firenio.codec.http11.WebSocketChannelListener)1 WebSocketCodec (com.firenio.codec.http11.WebSocketCodec)1 Properties (com.firenio.common.Properties)1 Channel (com.firenio.component.Channel)1 ChannelAliveListener (com.firenio.component.ChannelAliveListener)1 Frame (com.firenio.component.Frame)1 IoEventHandle (com.firenio.component.IoEventHandle)1 SslContext (com.firenio.component.SslContext)1 ThreadEventLoopGroup (com.firenio.concurrent.ThreadEventLoopGroup)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 HttpMessage (io.netty.handler.codec.http.HttpMessage)1 HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)1