public final class HttpServer
implements NettyConnector<HttpServerRequest, HttpServerResponse> {
public BlockingNettyContext startRouter(Consumer<? super HttpServerRoutes> routesBuilder) {
Objects.requireNonNull(routesBuilder, "routeBuilder");
// 构建路由信息久类
HttpServerRoutes routes = HttpServerRoutes.newRoutes();
// 回调给:routesBuilder处理,看上一章
routesBuilder.accept(routes);
// ***********************************************
// 1.启动,调用父类方法(NettyConnector.start)
// ***********************************************
return start(routes);
}// end startRouter
}
default <T extends BiFunction<INBOUND, OUTBOUND, ? extends Publisher<Void>>>
// 2.start方法
BlockingNettyContext start(T handler) {
return new BlockingNettyContext(
//调用子类:HttpServer.newHandler()
newHandler(handler),
getClass().getSimpleName()
);
} // end start
public Mono<? extends NettyContext> newHandler(BiFunction<? super HttpServerRequest, ? super HttpServerResponse, ? extends Publisher<Void>> handler) {
Objects.requireNonNull(handler, "handler");
// 把DefaultHttpServerRoutes强制转换成(BiFunction<HttpServerRequest, HttpServerResponse, Publisher<Void>>)
// 3.委托给:TcpBridgeServer.newHandler
// TcpBridgeServer继承于:TcpServer.newHandler
return server.newHandler((BiFunction<NettyInbound, NettyOutbound, Publisher<Void>>) handler);
}
public final Mono<? extends NettyContext> newHandler(BiFunction<? super NettyInbound, ? super NettyOutbound, ? extends Publisher<Void>> handler) {
Objects.requireNonNull(handler, "handler");
return Mono.create(sink -> {
ServerBootstrap b = options.get();
SocketAddress local = options.getAddress();
b.localAddress(local);
// 4.调用子类:HttpServer.TcpBridgeServer.doHandler(handler, sink)
ContextHandler<Channel> contextHandler = doHandler(handler, sink);
b.childHandler(contextHandler);
if(log.isDebugEnabled()){
b.handler(loggingHandler());
}
contextHandler.setFuture(b.bind());
});
}
protected ContextHandler<Channel> doHandler(
BiFunction<? super NettyInbound, ? super NettyOutbound, ? extends Publisher<Void>> handler,
MonoSink<NettyContext> sink) {
// 5.调用:ContextHandler.newServerContext
return ContextHandler.newServerContext(
sink,
options,
loggingHandler,
(ch, c, msg) -> HttpServerOperations.bindHttp(ch, handler, c, msg)
)
.onPipeline(this)
.autoCreateOperations(false);
}
// ContextHandler<Channel> 继承了Netty的:ChannelInitializer
public static ContextHandler<Channel> newServerContext(
MonoSink<NettyContext> sink,
ServerOptions options,
LoggingHandler loggingHandler,
ChannelOperations.OnNew<Channel> channelOpFactory) {
// 6.创建ServerContextHandler(属于:ContextHandler的子类)
return new ServerContextHandler(channelOpFactory, options, sink, loggingHandler, options.getAddress());
}