Home / Class/ HAProxyMessageEncoder Class — netty Architecture

HAProxyMessageEncoder Class — netty Architecture

Architecture documentation for the HAProxyMessageEncoder class in HAProxyMessageEncoder.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  b42a18dd_ae6a_f2e4_51f0_5f6605567255["HAProxyMessageEncoder"]
  e2d95ab2_9987_8e54_2ea3_87c51e97b560["HAProxyMessageEncoder.java"]
  b42a18dd_ae6a_f2e4_51f0_5f6605567255 -->|defined in| e2d95ab2_9987_8e54_2ea3_87c51e97b560
  4f8c711e_4e72_581d_83fe_3739fed2eed6["HAProxyMessageEncoder()"]
  b42a18dd_ae6a_f2e4_51f0_5f6605567255 -->|method| 4f8c711e_4e72_581d_83fe_3739fed2eed6
  a87bc120_6d1a_d0f4_f2a7_ccb9bedf10e5["encode()"]
  b42a18dd_ae6a_f2e4_51f0_5f6605567255 -->|method| a87bc120_6d1a_d0f4_f2a7_ccb9bedf10e5
  342e378f_7a0c_5abc_da2e_26918c348787["encodeV1()"]
  b42a18dd_ae6a_f2e4_51f0_5f6605567255 -->|method| 342e378f_7a0c_5abc_da2e_26918c348787
  deef989c_de3d_8f0c_8771_ef49cf56d11a["encodeV2()"]
  b42a18dd_ae6a_f2e4_51f0_5f6605567255 -->|method| deef989c_de3d_8f0c_8771_ef49cf56d11a
  eec6303c_23cc_e133_feca_dbd4a64d9228["encodeTlv()"]
  b42a18dd_ae6a_f2e4_51f0_5f6605567255 -->|method| eec6303c_23cc_e133_feca_dbd4a64d9228
  c3cfdec7_54a2_8856_e2fc_229fff7a1dc8["encodeTlvs()"]
  b42a18dd_ae6a_f2e4_51f0_5f6605567255 -->|method| c3cfdec7_54a2_8856_e2fc_229fff7a1dc8

Relationship Graph

Source Code

codec-haproxy/src/main/java/io/netty/handler/codec/haproxy/HAProxyMessageEncoder.java lines 34–135

@Sharable
public final class HAProxyMessageEncoder extends MessageToByteEncoder<HAProxyMessage> {

    private static final int V2_VERSION_BITMASK = 0x02 << 4;

    // Length for source/destination addresses for the UNIX family must be 108 bytes each.
    static final int UNIX_ADDRESS_BYTES_LENGTH = 108;
    static final int TOTAL_UNIX_ADDRESS_BYTES_LENGTH = UNIX_ADDRESS_BYTES_LENGTH * 2;

    public static final HAProxyMessageEncoder INSTANCE = new HAProxyMessageEncoder();

    private HAProxyMessageEncoder() {
        super(HAProxyMessage.class);
    }

    @Override
    protected void encode(ChannelHandlerContext ctx, HAProxyMessage msg, ByteBuf out) throws Exception {
        switch (msg.protocolVersion()) {
            case V1:
                encodeV1(msg, out);
                break;
            case V2:
                encodeV2(msg, out);
                break;
            default:
                throw new HAProxyProtocolException("Unsupported version: " + msg.protocolVersion());
        }
    }

    private static void encodeV1(HAProxyMessage msg, ByteBuf out) {
        out.writeBytes(TEXT_PREFIX);
        out.writeByte((byte) ' ');
        out.writeCharSequence(msg.proxiedProtocol().name(), CharsetUtil.US_ASCII);
        out.writeByte((byte) ' ');
        out.writeCharSequence(msg.sourceAddress(), CharsetUtil.US_ASCII);
        out.writeByte((byte) ' ');
        out.writeCharSequence(msg.destinationAddress(), CharsetUtil.US_ASCII);
        out.writeByte((byte) ' ');
        out.writeCharSequence(String.valueOf(msg.sourcePort()), CharsetUtil.US_ASCII);
        out.writeByte((byte) ' ');
        out.writeCharSequence(String.valueOf(msg.destinationPort()), CharsetUtil.US_ASCII);
        out.writeByte((byte) '\r');
        out.writeByte((byte) '\n');
    }

    private static void encodeV2(HAProxyMessage msg, ByteBuf out) {
        out.writeBytes(BINARY_PREFIX);
        out.writeByte(V2_VERSION_BITMASK | msg.command().byteValue());
        out.writeByte(msg.proxiedProtocol().byteValue());

        switch (msg.proxiedProtocol().addressFamily()) {
            case AF_IPv4:
            case AF_IPv6:
                byte[] srcAddrBytes = NetUtil.createByteArrayFromIpAddressString(msg.sourceAddress());
                byte[] dstAddrBytes = NetUtil.createByteArrayFromIpAddressString(msg.destinationAddress());
                // srcAddrLen + dstAddrLen + 4 (srcPort + dstPort) + numTlvBytes
                out.writeShort(srcAddrBytes.length + dstAddrBytes.length + 4 + msg.tlvNumBytes());
                out.writeBytes(srcAddrBytes);
                out.writeBytes(dstAddrBytes);
                out.writeShort(msg.sourcePort());
                out.writeShort(msg.destinationPort());
                encodeTlvs(msg.tlvs(), out);
                break;
            case AF_UNIX:
                out.writeShort(TOTAL_UNIX_ADDRESS_BYTES_LENGTH + msg.tlvNumBytes());
                int srcAddrBytesWritten = out.writeCharSequence(msg.sourceAddress(), CharsetUtil.US_ASCII);
                out.writeZero(UNIX_ADDRESS_BYTES_LENGTH - srcAddrBytesWritten);
                int dstAddrBytesWritten = out.writeCharSequence(msg.destinationAddress(), CharsetUtil.US_ASCII);
                out.writeZero(UNIX_ADDRESS_BYTES_LENGTH - dstAddrBytesWritten);
                encodeTlvs(msg.tlvs(), out);
                break;
            case AF_UNSPEC:
                out.writeShort(0);
                break;
            default:
                throw new HAProxyProtocolException("unexpected addrFamily");
        }
    }

    private static void encodeTlv(HAProxyTLV haProxyTLV, ByteBuf out) {
        if (haProxyTLV instanceof HAProxySSLTLV) {

Frequently Asked Questions

What is the HAProxyMessageEncoder class?
HAProxyMessageEncoder is a class in the netty codebase, defined in codec-haproxy/src/main/java/io/netty/handler/codec/haproxy/HAProxyMessageEncoder.java.
Where is HAProxyMessageEncoder defined?
HAProxyMessageEncoder is defined in codec-haproxy/src/main/java/io/netty/handler/codec/haproxy/HAProxyMessageEncoder.java at line 34.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free