This commit is contained in:
kamhung
2015-11-13 09:19:18 +08:00
parent 5bd35d38fe
commit 4589c4d9df

View File

@@ -40,8 +40,6 @@ public final class Transport {
protected final ObjectPool<ByteBuffer> bufferPool; protected final ObjectPool<ByteBuffer> bufferPool;
protected final AtomicInteger index = new AtomicInteger();
protected final ConcurrentHashMap<SocketAddress, BlockingQueue<AsyncConnection>> connPool = new ConcurrentHashMap<>(); protected final ConcurrentHashMap<SocketAddress, BlockingQueue<AsyncConnection>> connPool = new ConcurrentHashMap<>();
public Transport(Transport transport, InetSocketAddress localAddress, Collection<Transport> transports) { public Transport(Transport transport, InetSocketAddress localAddress, Collection<Transport> transports) {
@@ -142,10 +140,8 @@ public final class Transport {
try { try {
if (tcp) { if (tcp) {
AsynchronousSocketChannel channel = null; AsynchronousSocketChannel channel = null;
if (rand) { //随机取地址 if (rand) { //取地址
int p = 0; for (int i = 0; i < remoteAddres.length; i++) {
for (int i = index.get(); i < remoteAddres.length; i++) {
p = i;
addr = remoteAddres[i]; addr = remoteAddres[i];
BlockingQueue<AsyncConnection> queue = connPool.get(addr); BlockingQueue<AsyncConnection> queue = connPool.get(addr);
if (queue != null && !queue.isEmpty()) { if (queue != null && !queue.isEmpty()) {
@@ -155,19 +151,14 @@ public final class Transport {
} }
} }
if (channel == null) channel = AsynchronousSocketChannel.open(group); if (channel == null) channel = AsynchronousSocketChannel.open(group);
try { try {
channel.connect(addr).get(2, TimeUnit.SECONDS); channel.connect(addr).get(2, TimeUnit.SECONDS);
break; break;
} catch (Exception iex) { } catch (Exception iex) {
iex.printStackTrace(); iex.printStackTrace();
if (i == remoteAddres.length - 1) { if (i == remoteAddres.length - 1) channel = null;
p = 0;
channel = null;
}
} }
} }
index.set(p);
} else { } else {
channel = AsynchronousSocketChannel.open(group); channel = AsynchronousSocketChannel.open(group);
channel.connect(addr).get(2, TimeUnit.SECONDS); channel.connect(addr).get(2, TimeUnit.SECONDS);