All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.digitalpetri.enip.EnipCodec Maven / Gradle / Ivy

There is a newer version: 1.1.2
Show newest version
package com.digitalpetri.enip;

import java.nio.ByteOrder;
import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageCodec;

public class EnipCodec extends ByteToMessageCodec {

    private static final int HEADER_SIZE = 24;
    private static final int LENGTH_OFFSET = 2;

    @Override
    protected void encode(ChannelHandlerContext ctx, EnipPacket packet, ByteBuf out) throws Exception {
        EnipPacket.encode(packet, out.order(ByteOrder.LITTLE_ENDIAN));
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception {
        ByteBuf buffer = in.order(ByteOrder.LITTLE_ENDIAN);

        int startIndex = buffer.readerIndex();

        while (buffer.readableBytes() >= HEADER_SIZE &&
                buffer.readableBytes() >= HEADER_SIZE + getLength(buffer, startIndex)) {

            out.add(EnipPacket.decode(buffer));

            startIndex = buffer.readerIndex();
        }
    }

    private int getLength(ByteBuf buffer, int startIndex) {
        return buffer.getUnsignedShort(startIndex + LENGTH_OFFSET);
    }

}