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();
}
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);
}
});
}
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);
}
}
Aggregations