[英]Netty, Strings and flushing
我正在嘗試修改示例Netty代理,以使其在途中修改某些內容。
我在FTP客戶端和服務器之間進行代理,因此行以CRLF結尾-這很重要。 我還沒有對FTP數據端口做任何事情,所以這不是一個因素。
我從以下示例代碼開始: https : //netty.io/4.0/xref/io/netty/example/proxy/package-summary.html
...這樣建立了一個管道:
ch.pipeline().addLast(
new LoggingHandler(LogLevel.INFO),
new HexDumpProxyFrontendHandler(remoteHost, remotePort));
...效果很好。
如果添加new LineBasedFrameDecoder(maxLen)
則ftp客戶端將掛起等待服務器,因為代理已剝離CRLF,並且服務器仍在等待。 我可以通過告訴幀解碼器不要刪除定界符來解決此問題: new LineBasedFrameDecoder(maxLen, false, false)
。
ch.pipeline().addLast(
new LoggingHandler(LogLevel.INFO),
new LineBasedFrameDecoder(maxLen, false, false),
new HexDumpProxyFrontendHandler(remoteHost, remotePort));
到現在為止還挺好。 但是,如果添加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));
在調試器中,不會觸發StringEncoder.encode()中的斷點。
如何告訴Netty解碼后處理String?
StringEncoder是出站通道適配器。 目的是在寫入時從String轉換為ByteBuf,所以我不希望對入站數據調用編碼。
為了使代碼正常工作,您需要用入站通道適配器替換StringEncoder,該適配器在讀取時將從String轉換為ByteBuf。 我懷疑Netty庫中是否存在這樣的編解碼器,因為解碼器通常會從較低級別的格式轉換為較高級別的格式,而不是相反。
由於LineBasedFrameDecoder發出ByteBuf,而HexDumpProxyFrontendHandler消耗ByteBuf,因此我建議您同時刪除StringDecoder和StringEncoder並插入客戶重寫器。 但是..使其成為ByteBuf到ByteBuf解碼器。 在解碼器中,您可以將傳入的ByteBuf轉換為String,進行工作,然后將其轉換回ByteBuf,然后將其向下傳遞到管道中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.