netty를 기반으로 REST 서버를 작성했습니다. 4. 클라이언트 처리기는 다음과 유사합니다.소켓 메시지가 netty로 분할 됨
netty가 제공하는 msg의 바이트 버퍼 용량은 다양합니다. 클라이언트 메시지가 버퍼보다 클 경우 메시지는 분할됩니다. 내가 찾은 것은 각 조각에 대해 channelRead와 ChannelReadComplete가 모두 호출된다는 것입니다. 내가 보통 보는 것은 ByteBuf가 약 512이고 메시지가 600 주위에 있다는 것입니다. 첫 번째 512 바이트는 channelReadComplete, 그 다음 나머지 100 바이트는 channelReadComplete, channelReadComplete는 channelReadComplete가 있습니다. 2 대신에 2 개의 메시지.
여기 몇 가지 관련 질문을 발견했지만 channelReadComplete의 요점은 무엇입니까? 모든 채널을 읽은 후에 실제로 호출됩니까? 사용 가능한 바이트가있는 한, channelReadComplete가 호출되기 전에 이들을 읽지 않아야합니까?
public class ClientHandler extends ChannelInboundHandlerAdapter {
....
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
Report.debug("Read from client");
ByteBuf buf = (ByteBuf) msg;
String contents = buf.toString(io.netty.util.CharsetUtil.US_ASCII);
ReferenceCountUtil.release(msg);
ClientConnection client = ClientConnection.get(ctx);
if (client != null) {
client.messageText(contents); // adds text to buffer
return;
}
((parse serial number from contents, process registration))
ClientConnection.online(serialNumber, ctx); // register success, create the client object
}
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ClientConnection client = ClientConnection.get(ctx);
if (client == null)
Report.debug("completed read of message from unregistered client");
else {
Report.debug("completed read of message from client " + client.serialNumber());
String contents = client.messageText();
... ((process message))
}
}
}