代码优化

master
fengfei 7 years ago
parent 1bbec8f492
commit 92af2d87a3
  1. 4
      proxy-client/src/main/java/org/fengfei/lanproxy/client/ClientChannelMannager.java
  2. 2
      proxy-client/src/main/java/org/fengfei/lanproxy/client/handlers/ClientChannelHandler.java
  3. 2
      proxy-client/src/main/java/org/fengfei/lanproxy/client/handlers/RealServerChannelHandler.java
  4. 0
      proxy-protocol/Constants.java
  5. 2
      proxy-protocol/src/main/java/org/fengfei/lanproxy/protocol/Constants.java
  6. 8
      proxy-protocol/src/main/java/org/fengfei/lanproxy/protocol/IdleCheckHandler.java
  7. 1
      proxy-server/src/main/java/org/fengfei/lanproxy/server/ProxyChannelManager.java
  8. 7
      proxy-server/src/main/java/org/fengfei/lanproxy/server/handlers/ServerChannelHandler.java
  9. 3
      proxy-server/src/main/java/org/fengfei/lanproxy/server/handlers/UserChannelHandler.java

@ -8,6 +8,7 @@ import java.util.concurrent.ConcurrentLinkedQueue;
import org.fengfei.lanproxy.client.listener.ProxyChannelBorrowListener; import org.fengfei.lanproxy.client.listener.ProxyChannelBorrowListener;
import org.fengfei.lanproxy.common.Config; import org.fengfei.lanproxy.common.Config;
import org.fengfei.lanproxy.protocol.Constants;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -16,6 +17,7 @@ import io.netty.buffer.Unpooled;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelOption;
import io.netty.util.AttributeKey; import io.netty.util.AttributeKey;
/** /**
@ -67,6 +69,8 @@ public class ClientChannelMannager {
if (proxyChannelPool.size() > MAX_POOL_SIZE) { if (proxyChannelPool.size() > MAX_POOL_SIZE) {
proxyChanel.close(); proxyChanel.close();
} else { } else {
proxyChanel.config().setOption(ChannelOption.AUTO_READ, true);
proxyChanel.attr(Constants.NEXT_CHANNEL).remove();
proxyChannelPool.offer(proxyChanel); proxyChannelPool.offer(proxyChanel);
logger.debug("return ProxyChanel to the pool, channel is {}, pool size is {} ", proxyChanel, proxyChannelPool.size()); logger.debug("return ProxyChanel to the pool, channel is {}, pool size is {} ", proxyChanel, proxyChannelPool.size());
} }

@ -1,10 +1,10 @@
package org.fengfei.lanproxy.client.handlers; package org.fengfei.lanproxy.client.handlers;
import org.fengfei.lanproxy.client.ClientChannelMannager; import org.fengfei.lanproxy.client.ClientChannelMannager;
import org.fengfei.lanproxy.client.Constants;
import org.fengfei.lanproxy.client.listener.ChannelStatusListener; import org.fengfei.lanproxy.client.listener.ChannelStatusListener;
import org.fengfei.lanproxy.client.listener.ProxyChannelBorrowListener; import org.fengfei.lanproxy.client.listener.ProxyChannelBorrowListener;
import org.fengfei.lanproxy.common.Config; import org.fengfei.lanproxy.common.Config;
import org.fengfei.lanproxy.protocol.Constants;
import org.fengfei.lanproxy.protocol.ProxyMessage; import org.fengfei.lanproxy.protocol.ProxyMessage;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;

@ -1,7 +1,7 @@
package org.fengfei.lanproxy.client.handlers; package org.fengfei.lanproxy.client.handlers;
import org.fengfei.lanproxy.client.ClientChannelMannager; import org.fengfei.lanproxy.client.ClientChannelMannager;
import org.fengfei.lanproxy.client.Constants; import org.fengfei.lanproxy.protocol.Constants;
import org.fengfei.lanproxy.protocol.ProxyMessage; import org.fengfei.lanproxy.protocol.ProxyMessage;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;

@ -1,4 +1,4 @@
package org.fengfei.lanproxy.client; package org.fengfei.lanproxy.protocol;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.util.AttributeKey; import io.netty.util.AttributeKey;

@ -17,9 +17,9 @@ public class IdleCheckHandler extends IdleStateHandler {
public static final int USER_CHANNEL_READ_IDLE_TIME = 1200; public static final int USER_CHANNEL_READ_IDLE_TIME = 1200;
public static final int READ_IDLE_TIME = 20; public static final int READ_IDLE_TIME = 40;
public static final int WRITE_IDLE_TIME = 10; public static final int WRITE_IDLE_TIME = 20;
private static Logger logger = LoggerFactory.getLogger(IdleCheckHandler.class); private static Logger logger = LoggerFactory.getLogger(IdleCheckHandler.class);
@ -30,6 +30,10 @@ public class IdleCheckHandler extends IdleStateHandler {
@Override @Override
protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception { protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception {
if (ctx.channel().attr(Constants.NEXT_CHANNEL) != null) {
return;
}
if (IdleStateEvent.FIRST_WRITER_IDLE_STATE_EVENT == evt) { if (IdleStateEvent.FIRST_WRITER_IDLE_STATE_EVENT == evt) {
logger.debug("channel write timeout {}", ctx.channel()); logger.debug("channel write timeout {}", ctx.channel());
ProxyMessage proxyMessage = new ProxyMessage(); ProxyMessage proxyMessage = new ProxyMessage();

@ -10,6 +10,7 @@ import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import org.fengfei.lanproxy.protocol.Constants;
import org.fengfei.lanproxy.server.config.ProxyConfig; import org.fengfei.lanproxy.server.config.ProxyConfig;
import org.fengfei.lanproxy.server.config.ProxyConfig.ConfigChangedListener; import org.fengfei.lanproxy.server.config.ProxyConfig.ConfigChangedListener;
import org.slf4j.Logger; import org.slf4j.Logger;

@ -2,8 +2,8 @@ package org.fengfei.lanproxy.server.handlers;
import java.util.List; import java.util.List;
import org.fengfei.lanproxy.protocol.Constants;
import org.fengfei.lanproxy.protocol.ProxyMessage; import org.fengfei.lanproxy.protocol.ProxyMessage;
import org.fengfei.lanproxy.server.Constants;
import org.fengfei.lanproxy.server.ProxyChannelManager; import org.fengfei.lanproxy.server.ProxyChannelManager;
import org.fengfei.lanproxy.server.config.ProxyConfig; import org.fengfei.lanproxy.server.config.ProxyConfig;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -70,6 +70,11 @@ public class ServerChannelHandler extends SimpleChannelInboundHandler<ProxyMessa
// 数据发送完成后再关闭连接,解决http1.0数据传输问题 // 数据发送完成后再关闭连接,解决http1.0数据传输问题
userChannel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); userChannel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
} }
// 通知客户端,用户连接已经断开
proxyMessage = new ProxyMessage();
proxyMessage.setType(ProxyMessage.TYPE_DISCONNECT);
proxyMessage.setUri(proxyMessage.getUri());
ctx.channel().writeAndFlush(proxyMessage);
return; return;
} }

@ -3,8 +3,8 @@ package org.fengfei.lanproxy.server.handlers;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import org.fengfei.lanproxy.protocol.Constants;
import org.fengfei.lanproxy.protocol.ProxyMessage; import org.fengfei.lanproxy.protocol.ProxyMessage;
import org.fengfei.lanproxy.server.Constants;
import org.fengfei.lanproxy.server.ProxyChannelManager; import org.fengfei.lanproxy.server.ProxyChannelManager;
import org.fengfei.lanproxy.server.config.ProxyConfig; import org.fengfei.lanproxy.server.config.ProxyConfig;
@ -98,6 +98,7 @@ public class UserChannelHandler extends SimpleChannelInboundHandler<ByteBuf> {
proxyChannel.attr(Constants.CLIENT_KEY).remove(); proxyChannel.attr(Constants.CLIENT_KEY).remove();
proxyChannel.attr(Constants.USER_ID).remove(); proxyChannel.attr(Constants.USER_ID).remove();
proxyChannel.config().setOption(ChannelOption.AUTO_READ, true);
// 通知客户端,用户连接已经断开 // 通知客户端,用户连接已经断开
ProxyMessage proxyMessage = new ProxyMessage(); ProxyMessage proxyMessage = new ProxyMessage();
proxyMessage.setType(ProxyMessage.TYPE_DISCONNECT); proxyMessage.setType(ProxyMessage.TYPE_DISCONNECT);

Loading…
Cancel
Save