mirror of
https://github.com/Nan1t/NanoLimbo.git
synced 2025-07-15 05:30:15 +02:00
42 lines
1.5 KiB
Java
42 lines
1.5 KiB
Java
/*
|
|
* Copyright (C) 2020 Nan1t
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
package ru.nanit.limbo.connection.pipeline;
|
|
|
|
import io.netty.buffer.ByteBuf;
|
|
import io.netty.channel.ChannelHandler;
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
import io.netty.handler.codec.MessageToByteEncoder;
|
|
import ru.nanit.limbo.protocol.ByteMessage;
|
|
|
|
@ChannelHandler.Sharable
|
|
public class VarIntLengthEncoder extends MessageToByteEncoder<ByteBuf> {
|
|
|
|
@Override
|
|
protected void encode(ChannelHandlerContext ctx, ByteBuf buf, ByteBuf out) {
|
|
ByteMessage msg = new ByteMessage(out);
|
|
msg.writeVarInt(buf.readableBytes());
|
|
msg.writeBytes(buf);
|
|
}
|
|
|
|
@Override
|
|
protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean preferDirect) {
|
|
int anticipatedRequiredCapacity = 5 + msg.readableBytes();
|
|
return ctx.alloc().heapBuffer(anticipatedRequiredCapacity);
|
|
}
|
|
}
|