All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.dongliu.prettypb.rpc.server.RpcServerChannelInitializer Maven / Gradle / Ivy

There is a newer version: 0.3.5
Show newest version
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
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy