2013-10-09 27 views
0

我在Netty中編寫了一個透明的反向代理,並且在建立連接之後和第一個字節通過之間似乎存在大量的延遲(大約700ms)。連接和字節之間的Netty極端延遲

b.connect(remoteIp, remotePort).addListener(new ChannelFutureListener() { 
     public void operationComplete(ChannelFuture future) throws Exception { 
      ByteBuf buff = future.channel().alloc().buffer(); 
      if (IS_PING) { 
       buff.writeByte(-2); 
       buff.writeByte(1); 
       buff.writeByte(250); 
       writeString(buff, "MC|PingHost"); 
       ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
       DataOutputStream flush = new DataOutputStream(bos); 
       flush.writeByte(protoVersion); 
       writeString(flush, remoteIp); 
       flush.writeInt(port); 
       buff.writeBytes(bos.toByteArray()); 
       flush.close(); 
      } else { 
       buff.writeByte(2); 
       buff.writeByte(protoVersion); 
       writeString(buff, username); 
       writeString(buff, host); 
       buff.writeInt(port); 
      } 
      future.channel().writeAndFlush(buff); 
      RelayHandler.this.hasConnection = true; 
      RelayHandler.this.outboundChannel = future.channel(); 
     } 

線之間的延遲RelayHandler.this.hasConnection =真,並且當從所述遠程IP的第一個字節進來是大約600毫秒

然而,當我編寫一個簡單的「代理」像這樣,

public static void main(String[] args) throws Exception { 
    ServerSocket socket = new ServerSocket(25565); 
    Socket client = socket.accept(); 
    DataInputStream dis = new DataInputStream(client.getInputStream()); 
    DataOutputStream dos = new DataOutputStream(client.getOutputStream()); 
    Socket out = new Socket("5.9.106.20", 25565); 
    DataOutputStream outboundDos = new DataOutputStream((out.getOutputStream())); 
    DataInputStream outboundDis = new DataInputStream(out.getInputStream()); 
    while (true) { 
     if (dis.available() > 0) { 
      byte[] buff = new byte[dis.available()]; 
      dis.read(buff); 
      outboundDos.write(buff); 
     } 
     if (outboundDis.available() > 0) { 
      byte[] buff = new byte[outboundDis.available()]; 
      outboundDis.read(buff); 
      dos.write(buff); 
     } 
    } 
} 

延遲是不明顯的 - 我甚至不知道我是否路由它。我究竟做錯了什麼?

回答

0

不確定延遲,但最好在處理程序的方法channelActive()被調用後開始寫入通道。這將保證通道的設置和通道的流水線的建立和準備。