Search in sources :

Example 1 with ProtocolException

use of com.generallycloud.baseio.protocol.ProtocolException in project baseio by generallycloud.

the class ProtobaseProtocolEncoder method encode.

@Override
public void encode(SocketChannel channel, ChannelFuture future) throws IOException {
    ByteBufAllocator allocator = channel.getByteBufAllocator();
    if (future.isHeartbeat()) {
        ByteBuf buf = future.isPING() ? PING.duplicate() : PONG.duplicate();
        future.setByteBuf(buf);
        return;
    }
    ProtobaseFuture f = (ProtobaseFuture) future;
    String futureName = f.getFutureName();
    if (StringUtil.isNullOrBlank(futureName)) {
        throw new ProtocolException("future name is empty");
    }
    byte[] futureNameBytes = futureName.getBytes(channel.getEncoding());
    if (futureNameBytes.length > Byte.MAX_VALUE) {
        throw new ProtocolException("future name max length 127");
    }
    byte futureNameLength = (byte) futureNameBytes.length;
    int allLen = 6 + futureNameLength;
    int textWriteSize = f.getWriteSize();
    int binaryWriteSize = f.getWriteBinarySize();
    byte h1 = 0b01000000;
    if (f.isBroadcast()) {
        h1 |= 0b00100000;
    }
    if (f.getFutureId() > 0) {
        h1 |= 0b00010000;
        allLen += 4;
    }
    if (f.getSessionId() > 0) {
        h1 |= 0b00001000;
        allLen += 4;
    }
    if (f.getHashCode() > 0) {
        h1 |= 0b00000100;
        allLen += 4;
    }
    if (f.getWriteBinarySize() > 0) {
        h1 |= 0b00000010;
        allLen += 4;
        allLen += f.getWriteBinarySize();
    }
    if (textWriteSize > 0) {
        allLen += textWriteSize;
    }
    ByteBuf buf = allocator.allocate(allLen);
    buf.putByte(h1);
    buf.putByte(futureNameLength);
    buf.putInt(textWriteSize);
    if (f.getFutureId() > 0) {
        buf.putInt(f.getFutureId());
    }
    if (f.getSessionId() > 0) {
        buf.putInt(f.getSessionId());
    }
    if (f.getHashCode() > 0) {
        buf.putInt(f.getHashCode());
    }
    if (binaryWriteSize > 0) {
        buf.putInt(binaryWriteSize);
    }
    buf.put(futureNameBytes);
    if (textWriteSize > 0) {
        buf.put(f.getWriteBuffer(), 0, textWriteSize);
    }
    if (binaryWriteSize > 0) {
        buf.put(f.getWriteBinary(), 0, binaryWriteSize);
    }
    future.setByteBuf(buf.flip());
}
Also used : UnpooledByteBufAllocator(com.generallycloud.baseio.buffer.UnpooledByteBufAllocator) ByteBufAllocator(com.generallycloud.baseio.buffer.ByteBufAllocator) ProtocolException(com.generallycloud.baseio.protocol.ProtocolException) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) ByteBuf(com.generallycloud.baseio.buffer.ByteBuf)

Aggregations

ByteBuf (com.generallycloud.baseio.buffer.ByteBuf)1 ByteBufAllocator (com.generallycloud.baseio.buffer.ByteBufAllocator)1 UnpooledByteBufAllocator (com.generallycloud.baseio.buffer.UnpooledByteBufAllocator)1 ProtobaseFuture (com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture)1 ProtocolException (com.generallycloud.baseio.protocol.ProtocolException)1