org.apache.cassandra.transport.Frame Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cassandra-unit-shaded Show documentation
Show all versions of cassandra-unit-shaded Show documentation
Shaded version of cassandra-unit
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.cassandra.transport;
import java.io.IOException;
import java.util.EnumSet;
import java.util.List;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.codec.MessageToMessageEncoder;
import io.netty.util.Attribute;
import org.apache.cassandra.config.DatabaseDescriptor;
import org.apache.cassandra.exceptions.InvalidRequestException;
import org.apache.cassandra.transport.messages.ErrorMessage;
public class Frame
{
public static final byte PROTOCOL_VERSION_MASK = 0x7f;
public final Header header;
public final ByteBuf body;
/**
* An on-wire frame consists of a header and a body.
*
* The header is defined the following way in native protocol version 3 and later:
*
* 0 8 16 24 32 40
* +---------+---------+---------+---------+---------+
* | version | flags | stream | opcode |
* +---------+---------+---------+---------+---------+
* | length |
* +---------+---------+---------+---------+
*/
private Frame(Header header, ByteBuf body)
{
this.header = header;
this.body = body;
}
public void retain()
{
body.retain();
}
public boolean release()
{
return body.release();
}
public static Frame create(Message.Type type, int streamId, ProtocolVersion version, EnumSet flags, ByteBuf body)
{
Header header = new Header(version, flags, streamId, type);
return new Frame(header, body);
}
public static class Header
{
// 9 bytes in protocol version 3 and later
public static final int LENGTH = 9;
public static final int BODY_LENGTH_SIZE = 4;
public final ProtocolVersion version;
public final EnumSet flags;
public final int streamId;
public final Message.Type type;
private Header(ProtocolVersion version, EnumSet flags, int streamId, Message.Type type)
{
this.version = version;
this.flags = flags;
this.streamId = streamId;
this.type = type;
}
public enum Flag
{
// The order of that enum matters!!
COMPRESSED,
TRACING,
CUSTOM_PAYLOAD,
WARNING,
USE_BETA;
private static final Flag[] ALL_VALUES = values();
public static EnumSet deserialize(int flags)
{
EnumSet set = EnumSet.noneOf(Flag.class);
for (int n = 0; n < ALL_VALUES.length; n++)
{
if ((flags & (1 << n)) != 0)
set.add(ALL_VALUES[n]);
}
return set;
}
public static int serialize(EnumSet flags)
{
int i = 0;
for (Flag flag : flags)
i |= 1 << flag.ordinal();
return i;
}
}
}
public Frame with(ByteBuf newBody)
{
return new Frame(header, newBody);
}
public static class Decoder extends ByteToMessageDecoder
{
private static final int MAX_FRAME_LENGTH = DatabaseDescriptor.getNativeTransportMaxFrameSize();
private boolean discardingTooLongFrame;
private long tooLongFrameLength;
private long bytesToDiscard;
private int tooLongStreamId;
private final Connection.Factory factory;
public Decoder(Connection.Factory factory)
{
this.factory = factory;
}
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List