Search in sources :

Example 1 with CodecRegistration

use of com.flowpowered.network.Codec.CodecRegistration in project Glowstone by GlowstoneMC.

the class CodecsHandler method encode.

@Override
protected void encode(ChannelHandlerContext ctx, Message msg, List<Object> out) throws Exception {
    // find codec
    Class<? extends Message> clazz = msg.getClass();
    CodecRegistration reg = protocol.getCodecRegistration(clazz);
    if (reg == null) {
        throw new EncoderException("Unknown message type: " + clazz + ".");
    }
    // write header
    ByteBuf headerBuf = ctx.alloc().buffer(8);
    ByteBufUtils.writeVarInt(headerBuf, reg.getOpcode());
    // write body
    ByteBuf messageBuf = ctx.alloc().buffer();
    messageBuf = reg.getCodec().encode(messageBuf, msg);
    out.add(Unpooled.wrappedBuffer(headerBuf, messageBuf));
}
Also used : EncoderException(io.netty.handler.codec.EncoderException) CodecRegistration(com.flowpowered.network.Codec.CodecRegistration) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

CodecRegistration (com.flowpowered.network.Codec.CodecRegistration)1 ByteBuf (io.netty.buffer.ByteBuf)1 EncoderException (io.netty.handler.codec.EncoderException)1