|
| 1 | +/* |
| 2 | + * Copyright DataStax, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.datastax.driver.core; |
| 17 | + |
| 18 | +import com.datastax.driver.core.Message.Response.Type; |
| 19 | +import io.netty.channel.ChannelHandlerContext; |
| 20 | +import io.netty.channel.ChannelPipeline; |
| 21 | +import io.netty.handler.codec.MessageToMessageDecoder; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +/** |
| 25 | + * A handler to deal with different protocol framing formats. |
| 26 | + * |
| 27 | + * <p>This handler detects when a handshake is successful; then, if necessary, adapts the pipeline |
| 28 | + * to the modern framing format introduced in protocol v5. |
| 29 | + */ |
| 30 | +public class FramingFormatHandler extends MessageToMessageDecoder<Frame> { |
| 31 | + |
| 32 | + private final Connection.Factory factory; |
| 33 | + |
| 34 | + FramingFormatHandler(Connection.Factory factory) { |
| 35 | + this.factory = factory; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + protected void decode(ChannelHandlerContext ctx, Frame frame, List<Object> out) throws Exception { |
| 40 | + boolean handshakeSuccessful = |
| 41 | + frame.header.opcode == Type.READY.opcode || frame.header.opcode == Type.AUTHENTICATE.opcode; |
| 42 | + if (handshakeSuccessful) { |
| 43 | + // By default, the pipeline is configured for legacy framing since this is the format used |
| 44 | + // by all protocol versions until handshake; after handshake however, we need to switch to |
| 45 | + // modern framing for protocol v5 and higher. |
| 46 | + if (frame.header.version.compareTo(ProtocolVersion.V5) >= 0) { |
| 47 | + switchToModernFraming(ctx); |
| 48 | + } |
| 49 | + // once the handshake is successful, the framing format cannot change anymore; |
| 50 | + // we can safely remove ourselves from the pipeline. |
| 51 | + ctx.pipeline().remove("framingFormatHandler"); |
| 52 | + } |
| 53 | + out.add(frame); |
| 54 | + } |
| 55 | + |
| 56 | + private void switchToModernFraming(ChannelHandlerContext ctx) { |
| 57 | + ChannelPipeline pipeline = ctx.pipeline(); |
| 58 | + SegmentCodec segmentCodec = |
| 59 | + new SegmentCodec( |
| 60 | + ctx.channel().alloc(), factory.configuration.getProtocolOptions().getCompression()); |
| 61 | + |
| 62 | + // Outbound: "message -> segment -> bytes" instead of "message -> frame -> bytes" |
| 63 | + Message.ProtocolEncoder requestEncoder = |
| 64 | + (Message.ProtocolEncoder) pipeline.get("messageEncoder"); |
| 65 | + pipeline.replace( |
| 66 | + "messageEncoder", |
| 67 | + "messageToSegmentEncoder", |
| 68 | + new MessageToSegmentEncoder(ctx.channel().alloc(), requestEncoder)); |
| 69 | + pipeline.replace( |
| 70 | + "frameEncoder", "segmentToBytesEncoder", new SegmentToBytesEncoder(segmentCodec)); |
| 71 | + |
| 72 | + // Inbound: "frame <- segment <- bytes" instead of "frame <- bytes" |
| 73 | + pipeline.replace( |
| 74 | + "frameDecoder", "bytesToSegmentDecoder", new BytesToSegmentDecoder(segmentCodec)); |
| 75 | + pipeline.addAfter( |
| 76 | + "bytesToSegmentDecoder", "segmentToFrameDecoder", new SegmentToFrameDecoder()); |
| 77 | + } |
| 78 | +} |
0 commit comments