net.dongliu.prettypb.rpc.server.RpcServerChannelInitializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of prettypb-rpc Show documentation
Show all versions of prettypb-rpc Show documentation
proto rpc libs, compatible with proto-rpc-pro
package net.dongliu.prettypb.rpc.server;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.ssl.SslHandler;
import net.dongliu.prettypb.rpc.coder.ProtobufDecoder;
import net.dongliu.prettypb.rpc.coder.ProtobufEncoder;
import net.dongliu.prettypb.rpc.coder.VarIntFrameDecoder;
import net.dongliu.prettypb.rpc.coder.VarIntLengthFieldPrepender;
import net.dongliu.prettypb.rpc.protocol.WirePayload;
import net.dongliu.prettypb.rpc.utils.RpcSSLContext;
import net.dongliu.prettypb.rpc.utils.Handlers;
import net.dongliu.prettypb.runtime.ExtensionRegistry;
/**
* @author Dong Liu
*/
public class RpcServerChannelInitializer extends ChannelInitializer {
private RpcSSLContext sslContext;
/**
* extension for wire payload
*/
private ExtensionRegistry wpExtensionRegistry;
private RequestHandler requestHandler;
public RpcServerChannelInitializer(RpcSSLContext sslContext,
ExtensionRegistry wpExtensionRegistry,
RequestHandler requestHandler) {
this.sslContext = sslContext;
this.wpExtensionRegistry = wpExtensionRegistry;
this.requestHandler = requestHandler;
}
@Override
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
// for ssl
if (this.sslContext != null) {
p.addLast(Handlers.SSL, new SslHandler(sslContext.createServerEngine()));
}
p.addLast(Handlers.FRAME_DECODER, new VarIntFrameDecoder());
p.addLast(Handlers.PROTOBUF_DECODER, new ProtobufDecoder(WirePayload.class,
wpExtensionRegistry));
p.addLast(Handlers.FRAME_ENCODER, new VarIntLengthFieldPrepender());
p.addLast(Handlers.PROTOBUF_ENCODER, new ProtobufEncoder());
p.addLast(Handlers.SERVER_CONNECT, requestHandler); // one instance shared by all channels
}
}