Home / Class/ EchoHandler Class — netty Architecture

EchoHandler Class — netty Architecture

Architecture documentation for the EchoHandler class in SocketFixedLengthEchoTest.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  a2e9f844_af44_d033_1895_cab3ff20fe3b["EchoHandler"]
  09f682ec_9784_e533_2448_adc18a769695["SocketFixedLengthEchoTest.java"]
  a2e9f844_af44_d033_1895_cab3ff20fe3b -->|defined in| 09f682ec_9784_e533_2448_adc18a769695
  960ada3c_0b7d_770c_020b_c33848daab0b["EchoHandler()"]
  a2e9f844_af44_d033_1895_cab3ff20fe3b -->|method| 960ada3c_0b7d_770c_020b_c33848daab0b
  845d9634_0175_ab36_8727_27661162977c["channelActive()"]
  a2e9f844_af44_d033_1895_cab3ff20fe3b -->|method| 845d9634_0175_ab36_8727_27661162977c
  dedc327b_8e63_2bc9_8fc3_7eafdef41fa5["channelRead0()"]
  a2e9f844_af44_d033_1895_cab3ff20fe3b -->|method| dedc327b_8e63_2bc9_8fc3_7eafdef41fa5
  dc6d60ea_4920_a46c_0f1e_4665d9d6874b["channelReadComplete()"]
  a2e9f844_af44_d033_1895_cab3ff20fe3b -->|method| dc6d60ea_4920_a46c_0f1e_4665d9d6874b
  dbcfbede_d579_ddb2_8c28_776ebfa526c1["exceptionCaught()"]
  a2e9f844_af44_d033_1895_cab3ff20fe3b -->|method| dbcfbede_d579_ddb2_8c28_776ebfa526c1

Relationship Graph

Source Code

testsuite/src/main/java/io/netty/testsuite/transport/socket/SocketFixedLengthEchoTest.java lines 144–200

    private static class EchoHandler extends SimpleChannelInboundHandler<ByteBuf> {
        private final boolean autoRead;
        volatile Channel channel;
        final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
        volatile int counter;

        EchoHandler(boolean autoRead) {
            this.autoRead = autoRead;
        }

        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            channel = ctx.channel();
            if (!autoRead) {
                ctx.read();
            }
        }

        @Override
        public void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
            assertEquals(1024, msg.readableBytes());

            byte[] actual = new byte[msg.readableBytes()];
            msg.getBytes(msg.readerIndex(), actual);

            int lastIdx = counter;
            for (int i = 0; i < actual.length; i ++) {
                assertEquals(data[i + lastIdx], actual[i]);
            }

            // Update the counter before calling write(...) as write could in theory trigger another channelRead(...)
            // which then would use the wrong lastIdx.
            counter += actual.length;

            if (channel.parent() != null) {
                channel.write(msg.retain());
            }
        }

        @Override
        public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
            try {
                ctx.flush();
            } finally {
                if (!autoRead) {
                    ctx.read();
                }
            }
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            if (exception.compareAndSet(null, cause)) {
                ctx.close();
            }
        }
    }

Frequently Asked Questions

What is the EchoHandler class?
EchoHandler is a class in the netty codebase, defined in testsuite/src/main/java/io/netty/testsuite/transport/socket/SocketFixedLengthEchoTest.java.
Where is EchoHandler defined?
EchoHandler is defined in testsuite/src/main/java/io/netty/testsuite/transport/socket/SocketFixedLengthEchoTest.java at line 144.

Analyze Your Own Codebase

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

Try Supermodel Free