1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty5.example.proxy;
17
18 import io.netty5.channel.Channel;
19 import io.netty5.channel.ChannelHandler;
20 import io.netty5.channel.ChannelHandlerContext;
21
22 public class HexDumpProxyBackendHandler implements ChannelHandler {
23
24 private final Channel inboundChannel;
25
26 public HexDumpProxyBackendHandler(Channel inboundChannel) {
27 this.inboundChannel = inboundChannel;
28 }
29
30 @Override
31 public void channelActive(ChannelHandlerContext ctx) {
32 ctx.read();
33 }
34
35 @Override
36 public void channelRead(final ChannelHandlerContext ctx, Object msg) {
37 inboundChannel.writeAndFlush(msg).addListener(inboundChannel, (inbound, future) -> {
38 if (future.isSuccess()) {
39 ctx.channel().read();
40 } else {
41 inbound.close();
42 }
43 });
44 }
45
46 @Override
47 public void channelInactive(ChannelHandlerContext ctx) {
48 HexDumpProxyFrontendHandler.closeOnFlush(inboundChannel);
49 }
50
51 @Override
52 public void channelExceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
53 cause.printStackTrace();
54 HexDumpProxyFrontendHandler.closeOnFlush(ctx.channel());
55 }
56 }