com.digitalpetri.modbus.ModbusTcpCodec Maven / Gradle / Ivy
package com.digitalpetri.modbus;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageCodec;
import java.nio.ByteBuffer;
import java.util.List;
public class ModbusTcpCodec extends ByteToMessageCodec {
public static final int MBAP_TOTAL_LENGTH = 7;
public static final int MBAP_LENGTH_FIELD_OFFSET = 4;
@Override
protected void encode(ChannelHandlerContext ctx, ModbusTcpFrame msg, ByteBuf out) {
var buffer = ByteBuffer.allocate(MBAP_TOTAL_LENGTH + msg.pdu().limit() - msg.pdu().position());
MbapHeader.Serializer.encode(msg.header(), buffer);
buffer.put(msg.pdu());
buffer.flip();
out.writeBytes(buffer);
}
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List