net.dongliu.prettypb.rpc.client.ProtobufChannelInitializer 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.client;
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 dongliu
*/
public class ProtobufChannelInitializer extends ChannelInitializer {
private RpcSSLContext sslContext;
/**
* extension registry for wire payload
*/
private ExtensionRegistry wpExtensionRegistry;
/**
* @param sslContext the ssl context. null if not use ssl
* @param wpExtensionRegistry wirePayload extension. not if not use
*/
public ProtobufChannelInitializer(RpcSSLContext sslContext,
ExtensionRegistry wpExtensionRegistry) {
this.sslContext = sslContext;
this.wpExtensionRegistry = wpExtensionRegistry;
}
@Override
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
if (sslContext != null) {
p.addLast(Handlers.SSL, new SslHandler(sslContext.createClientEngine()));
}
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());
// the connectResponseHandler is swapped after the client connection
// handshake with the RpcClient for the Channel
p.addLast(Handlers.CLIENT_CONNECT, new ClientConnectResponseHandler());
}
}