内网穿透、路由转发、代理Buffer实现循环缓冲提高效率,运行参数:本地端口 内网地址(域名) 内网端口 端口类型(固定1)
while (selectionKeyIterator.hasNext()) { SelectionKey selectionKey = selectionKeyIterator.next(); SelectableChannel channel = selectionKey.channel(); HostConnectionAttachment channelAttach = (HostConnectionAttachment) selectionKey.attachment(); CircleBufferDelegate sendBufferDelegate = channelAttach.sendBufferDelegate; CircleBufferDelegate recvBufferDelegate = channelAttach.recvBuffDelegate; if (selectionKey.isValid() && selectionKey.isReadable() && channel instanceof SocketChannel) { SocketChannel socketChannel = (SocketChannel) channel; int read = -1; try { while ((read = socketChannel.read(sendBufferDelegate.getBuffer())) > 0) { SocketChannel otherChannel = channelAttach.socketChannel; sendBufferDelegate.flip(); while (sendBufferDelegate.hasRemaining()) { // if ( i % 4 == 3) break;//模拟数据未读取完毕 int w = otherChannel.write(sendBufferDelegate.getBuffer()); if (w < 0) { throw new FCenterException("host write error"); } if (w == 0) { break; } } sendBufferDelegate.back(); } if (read == -1) { closeHostConnect(socketChannel, channelAttach); } else { sendBufferDelegate.flip(); if (sendBufferDelegate.hasRemaining()) channelAttach.socketKey.interestOps(channelAttach.socketKey.interestOps() | SelectionKey.OP_WRITE); sendBufferDelegate.back(); } } catch (Exception e) { closeHostConnect(socketChannel, channelAttach); logger.error("host connection thread read error,cause:" e.getMessage()); } } else if (selectionKey.isValid() && selectionKey.isWritable() && channel instanceof SocketChannel) { SocketChannel socketChannel = (SocketChannel) channel; recvBufferDelegate.flip(); while (recvBufferDelegate.hasRemaining()) { // if ( j % 4 == 3) break;//模拟数据未读取完毕 int w = socketChannel.write(recvBufferDelegate.getBuffer()); if (w < 0) { throw new FCenterException("host write error"); } } if (!recvBufferDelegate.hasRemaining()) selectionKey.interestOps(selectionKey.interestOps() ^ SelectionKey.OP_WRITE); recvBufferDelegate.back(); } selectionKeyIterator.remove(); }
评论