001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.ipc; 019 020import java.nio.ByteBuffer; 021import org.apache.yetus.audience.InterfaceAudience; 022 023import org.apache.hbase.thirdparty.io.netty.buffer.ByteBuf; 024import org.apache.hbase.thirdparty.io.netty.channel.Channel; 025import org.apache.hbase.thirdparty.io.netty.channel.ChannelHandlerContext; 026import org.apache.hbase.thirdparty.io.netty.channel.ChannelPipeline; 027import org.apache.hbase.thirdparty.io.netty.channel.SimpleChannelInboundHandler; 028 029/** 030 * Handle connection preamble. 031 * @since 2.0.0` 032 */ 033@InterfaceAudience.Private 034class NettyRpcServerPreambleHandler extends SimpleChannelInboundHandler<ByteBuf> { 035 036 private final NettyRpcServer rpcServer; 037 038 public NettyRpcServerPreambleHandler(NettyRpcServer rpcServer) { 039 this.rpcServer = rpcServer; 040 } 041 042 @Override 043 protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception { 044 NettyServerRpcConnection conn = createNettyServerRpcConnection(ctx.channel()); 045 ByteBuffer buf = ByteBuffer.allocate(msg.readableBytes()); 046 msg.readBytes(buf); 047 buf.flip(); 048 if (!conn.processPreamble(buf)) { 049 conn.close(); 050 return; 051 } 052 ChannelPipeline p = ctx.pipeline(); 053 ((NettyRpcFrameDecoder) p.get("frameDecoder")).setConnection(conn); 054 ((NettyRpcServerRequestDecoder) p.get("decoder")).setConnection(conn); 055 p.remove(this); 056 p.remove("preambleDecoder"); 057 } 058 059 protected NettyServerRpcConnection createNettyServerRpcConnection(Channel channel) { 060 return new NettyServerRpcConnection(rpcServer, channel); 061 } 062}