简体   繁体   English

净额,字符串和冲洗

[英]Netty, Strings and flushing

I'm experimenting with adapting the example Netty proxy, to make it modify some of the content en-route. 我正在尝试修改示例Netty代理,以使其在途中修改某些内容。

I'm proxying between an FTP client and server, so lines end in CRLF -- that's important. 我在FTP客户端和服务器之间进行代理,因此行以CRLF结尾-这很重要。 I'm not yet doing anything with FTP data ports, so that is not a factor. 我还没有对FTP数据端口做任何事情,所以这不是一个因素。

I started with this example code: https://netty.io/4.0/xref/io/netty/example/proxy/package-summary.html 我从以下示例代码开始: https : //netty.io/4.0/xref/io/netty/example/proxy/package-summary.html

... which sets up a pipeline like this: ...这样建立了一个管道:

ch.pipeline().addLast(
      new LoggingHandler(LogLevel.INFO),
      new HexDumpProxyFrontendHandler(remoteHost, remotePort));

... and that works fine. ...效果很好。

If I add a new LineBasedFrameDecoder(maxLen) the ftp client hangs waiting for the server, because the proxy has stripped off CRLF, and the server is still waiting. 如果添加new LineBasedFrameDecoder(maxLen)则ftp客户端将挂起等待服务器,因为代理已剥离CRLF,并且服务器仍在等待。 I can fix this by telling the frame decoder not to remove the delimiters: new LineBasedFrameDecoder(maxLen, false, false) . 我可以通过告诉帧解码器不要删除定界符来解决此问题: new LineBasedFrameDecoder(maxLen, false, false)

ch.pipeline().addLast(
      new LoggingHandler(LogLevel.INFO),
      new LineBasedFrameDecoder(maxLen, false, false),
      new HexDumpProxyFrontendHandler(remoteHost, remotePort));

So far, so good. 到现在为止还挺好。 But if I add a String decoder, I get the same hanging symptom, this time because the pipeline step after StringDecoder is not invoked. 但是,如果添加String解码器,则会出现相同的挂起症状,这是因为未调用StringDecoder之后的管道步骤。

ch.pipeline().addLast(
      new LoggingHandler(LogLevel.INFO),
      new LineBasedFrameDecoder(maxLen, false, false),
      new StringDecoder(StandardCharsets.UTF_8),
      // aim is for my own string rewriter to go here
      new StringEncoder(StandardCharsets.UTF_8),
      new HexDumpProxyFrontendHandler(remoteHost, remotePort));

In a debugger, a breakpoint in StringEncoder.encode() does not trigger. 在调试器中,不会触发StringEncoder.encode()中的断点。

How can I tell Netty to process the String after decoding? 如何告诉Netty解码后处理String?

StringEncoder is an outbound channel adapter. StringEncoder是出站通道适配器。 It's purpose is to convert from String to ByteBuf when writing, so I would not expect encode to be called on inbound data. 目的是在写入时从String转换为ByteBuf,所以我不希望对入站数据调用编码。

In order for your code to work, you would need to replace StringEncoder with an inbound channel adapter that converts from String to ByteBuf when reading. 为了使代码正常工作,您需要用入站通道适配器替换StringEncoder,该适配器在读取时将从String转换为ByteBuf。 I doubt that any such codec exists in the Netty library because decoders generally convert from a lower level format to a higher level format, not the other way around. 我怀疑Netty库中是否存在这样的编解码器,因为解码器通常会从较低级别的格式转换为较高级别的格式,而不是相反。

Since LineBasedFrameDecoder emits ByteBuf's and HexDumpProxyFrontendHandler consumes ByteBufs I would recommend that you remove both StringDecoder and StringEncoder and insert your customer rewriter. 由于LineBasedFrameDecoder发出ByteBuf,而HexDumpProxyFrontendHandler消耗ByteBuf,因此我建议您同时删除StringDecoder和StringEncoder并插入客户重写器。 But .. make it a ByteBuf to ByteBuf decoder. 但是..使其成为ByteBuf到ByteBuf解码器。 Within your decoder you can convert the incoming ByteBuf to a String, do your work, then convert it back to ByteBuf then pass it down the pipeline. 在解码器中,您可以将传入的ByteBuf转换为String,进行工作,然后将其转换回ByteBuf,然后将其向下传递到管道中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM