|
|
@ -2,12 +2,15 @@ package org.fengfei.lanproxy.client; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.Iterator; |
|
|
|
import java.util.Iterator; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
import java.util.Map.Entry; |
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
|
|
|
|
|
|
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import io.netty.buffer.Unpooled; |
|
|
|
import io.netty.channel.Channel; |
|
|
|
import io.netty.channel.Channel; |
|
|
|
|
|
|
|
import io.netty.channel.ChannelFutureListener; |
|
|
|
import io.netty.channel.ChannelOption; |
|
|
|
import io.netty.channel.ChannelOption; |
|
|
|
import io.netty.util.AttributeKey; |
|
|
|
import io.netty.util.AttributeKey; |
|
|
|
|
|
|
|
|
|
|
@ -61,8 +64,16 @@ public class ClientChannelMannager { |
|
|
|
return realServerChannels.remove(userId); |
|
|
|
return realServerChannels.remove(userId); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static boolean isRealServerReadable(Channel realServerChannel) { |
|
|
|
|
|
|
|
return realServerChannel.attr(CLIENT_CHANNEL_WRITEABLE).get() |
|
|
|
|
|
|
|
&& realServerChannel.attr(USER_CHANNEL_WRITEABLE).get(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void setRealServerChannelReadability(Channel realServerChannel, Boolean client, Boolean user) { |
|
|
|
public static void setRealServerChannelReadability(Channel realServerChannel, Boolean client, Boolean user) { |
|
|
|
logger.info("update real server channel readability, {} {}", client, user); |
|
|
|
logger.debug("update real server channel readability, {} {} {}", realServerChannel, client, user); |
|
|
|
|
|
|
|
if (realServerChannel == null) { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (client != null) { |
|
|
|
if (client != null) { |
|
|
|
realServerChannel.attr(CLIENT_CHANNEL_WRITEABLE).set(client); |
|
|
|
realServerChannel.attr(CLIENT_CHANNEL_WRITEABLE).set(client); |
|
|
@ -78,26 +89,25 @@ public class ClientChannelMannager { |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
realServerChannel.config().setOption(ChannelOption.AUTO_READ, false); |
|
|
|
realServerChannel.config().setOption(ChannelOption.AUTO_READ, false); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void notifyChannelWritabilityChanged(Channel channel) { |
|
|
|
public static void notifyChannelWritabilityChanged(Channel channel) { |
|
|
|
logger.info("channel writability changed, {}", channel.isWritable()); |
|
|
|
logger.debug("channel writability changed, {}", channel.isWritable()); |
|
|
|
|
|
|
|
Iterator<Entry<String, Channel>> entryIte = realServerChannels.entrySet().iterator(); |
|
|
|
Iterator<String> ite = realServerChannels.keySet().iterator(); |
|
|
|
while (entryIte.hasNext()) { |
|
|
|
while (ite.hasNext()) { |
|
|
|
setRealServerChannelReadability(entryIte.next().getValue(), channel.isWritable(), null); |
|
|
|
Channel realServerChannel = realServerChannels.get(ite.next()); |
|
|
|
|
|
|
|
setRealServerChannelReadability(realServerChannel, channel.isWritable(), null); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void clearRealServerChannels() { |
|
|
|
public static void clearRealServerChannels() { |
|
|
|
logger.warn("channel closed, clear real server channels"); |
|
|
|
logger.warn("channel closed, clear real server channels"); |
|
|
|
|
|
|
|
|
|
|
|
Iterator<String> ite = realServerChannels.keySet().iterator(); |
|
|
|
Iterator<Entry<String, Channel>> ite = realServerChannels.entrySet().iterator(); |
|
|
|
while (ite.hasNext()) { |
|
|
|
while (ite.hasNext()) { |
|
|
|
Channel realServerChannel = realServerChannels.get(ite.next()); |
|
|
|
Channel realServerChannel = ite.next().getValue(); |
|
|
|
realServerChannel.close(); |
|
|
|
if (realServerChannel.isActive()) { |
|
|
|
|
|
|
|
realServerChannel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
realServerChannels.clear(); |
|
|
|
realServerChannels.clear(); |
|
|
|