2017-03-03 7 views
1

http://netty.io/wiki/user-guide-for-4.x.html 링크를 사용하여 netty 서버를 작성했습니다. 하지만 최대 16384 바이트까지만 데이터를 가져오고 있습니다.netty SimpleChannelInboundHandler 메시지는 16384에만 수신

public class DiscardServerHandler extends ChannelInboundHandlerAdapter 
{ 
    byte bNullArray[] = "".getBytes(); 
    String strFullData= new String(bNullArray,StandardCharsets.UTF_8);   
    @Override 
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception 
    { 
     try 
     { 
      String MsgRead; 
      ByteBuf in = (ByteBuf) msg; 
      MsgRead=in.toString(io.netty.util.CharsetUtil.UTF_8); 
      // here I get data only upto 1024 and this method get called 16 times. 
      // So total data received is == 1024*16 = 16384   
      strFullData = strFullData + MsgRead;  
     } 
     finally 
     { 
      ReferenceCountUtil.release(msg); 
     } 
    }  
    @Override 
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception 
    { 
     //WriteMyLog(strFullData);  
     //Here size of strFullData is 16384   
     strFullData = ProcessMyData(strFullData);  
     byte[] respByteBuf = strFullData.getBytes();     
     ByteBuf Resp1 = ctx.alloc().buffer(respByteBuf.length);  
     Resp1.writeBytes(respByteBuf);     
     ctx.write(Resp1);    
     ctx.flush(); 
     ctx.close(); 
    } 
} 

더 많은 데이터를 얻으려면 어떻게해야합니까?

답변

1

OS가 소켓에서 데이터를 읽으면 사용자 공간 (Java는 사용자의 경우 netty)으로 전달됩니다. 16 * 1024는 OS가 소켓에서 읽고 당신에게 넘기는 버퍼 크기입니다. 즉, 메시지가이 크기를 초과하는 경우 ChannelInboundHandlerAdapter 처리기가 적합하지 않습니다. ByteToMessageDecoder을 사용해야합니다. 뭔가 같은 :

public class MyBigMessageDecoder extends ByteToMessageDecoder { 
    @Override 
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { 
     if (in.readableBytes() < MY_BIG_MESSAGE_SIZE) { 
      return; 
     } 

     out.add(in.readBytes(MY_BIG_MESSAGE_SIZE)); 
    } 
} 

의 Netty는 등 LineBasedFrameDecoder, LengthFieldBasedFrameDecoder, FixedLengthFrameDecoder처럼 당신이 그들 중 일부를 사용할 수 있다고 생각, 다양한 시나리오에 대한 준비가 핸들러의 무리가 있습니다.

일반적으로 모두 동일합니다. 일부 조건이 충족 될 때까지 소득 바이트를 계속 읽습니다. 준비가되면 read 바이트를 더 전달합니다.