Home / Class/ Socks5CommandResponseDecoder Class — netty Architecture

Socks5CommandResponseDecoder Class — netty Architecture

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

Entity Profile

Dependency Diagram

graph TD
  b53af1b4_09b5_0fac_55e3_d5fcdb0017dd["Socks5CommandResponseDecoder"]
  ee5fb098_2227_62f7_41ac_1d8ca4aea931["Socks5CommandResponseDecoder.java"]
  b53af1b4_09b5_0fac_55e3_d5fcdb0017dd -->|defined in| ee5fb098_2227_62f7_41ac_1d8ca4aea931
  d821561d_740e_0115_d44f_ddd7d10f5f77["Socks5CommandResponseDecoder()"]
  b53af1b4_09b5_0fac_55e3_d5fcdb0017dd -->|method| d821561d_740e_0115_d44f_ddd7d10f5f77
  a718f076_b7bf_6fc3_a00d_26dbaf880859["decode()"]
  b53af1b4_09b5_0fac_55e3_d5fcdb0017dd -->|method| a718f076_b7bf_6fc3_a00d_26dbaf880859
  7a7f9937_ea59_fccf_5203_1c354e63f498["fail()"]
  b53af1b4_09b5_0fac_55e3_d5fcdb0017dd -->|method| 7a7f9937_ea59_fccf_5203_1c354e63f498

Relationship Graph

Source Code

codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5CommandResponseDecoder.java lines 38–106

public class Socks5CommandResponseDecoder extends ReplayingDecoder<State> {

    @UnstableApi
    public enum State {
        INIT,
        SUCCESS,
        FAILURE
    }

    private final Socks5AddressDecoder addressDecoder;

    public Socks5CommandResponseDecoder() {
        this(Socks5AddressDecoder.DEFAULT);
    }

    public Socks5CommandResponseDecoder(Socks5AddressDecoder addressDecoder) {
        super(State.INIT);
        this.addressDecoder = ObjectUtil.checkNotNull(addressDecoder, "addressDecoder");
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        try {
            switch (state()) {
            case INIT: {
                final byte version = in.readByte();
                if (version != SocksVersion.SOCKS5.byteValue()) {
                    throw new DecoderException(
                            "unsupported version: " + version + " (expected: " + SocksVersion.SOCKS5.byteValue() + ')');
                }
                final Socks5CommandStatus status = Socks5CommandStatus.valueOf(in.readByte());
                in.skipBytes(1); // Reserved
                final Socks5AddressType addrType = Socks5AddressType.valueOf(in.readByte());
                final String addr = addressDecoder.decodeAddress(addrType, in);
                final int port = ByteBufUtil.readUnsignedShortBE(in);

                out.add(new DefaultSocks5CommandResponse(status, addrType, addr, port));
                checkpoint(State.SUCCESS);
            }
            case SUCCESS: {
                int readableBytes = actualReadableBytes();
                if (readableBytes > 0) {
                    out.add(in.readRetainedSlice(readableBytes));
                }
                break;
            }
            case FAILURE: {
                in.skipBytes(actualReadableBytes());
                break;
            }
            }
        } catch (Exception e) {
            fail(out, e);
        }
    }

    private void fail(List<Object> out, Exception cause) {
        if (!(cause instanceof DecoderException)) {
            cause = new DecoderException(cause);
        }

        checkpoint(State.FAILURE);

        Socks5Message m = new DefaultSocks5CommandResponse(
                Socks5CommandStatus.FAILURE, Socks5AddressType.IPv4, null, 0);
        m.setDecoderResult(DecoderResult.failure(cause));
        out.add(m);
    }
}

Frequently Asked Questions

What is the Socks5CommandResponseDecoder class?
Socks5CommandResponseDecoder is a class in the netty codebase, defined in codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5CommandResponseDecoder.java.
Where is Socks5CommandResponseDecoder defined?
Socks5CommandResponseDecoder is defined in codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5CommandResponseDecoder.java at line 38.

Analyze Your Own Codebase

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

Try Supermodel Free