Skip to content

Commit

Permalink
修复作为TCP Client在断网状态下连接域名时报的异常
Browse files Browse the repository at this point in the history
修复作为TCP Client在断网状态下连接域名时报的异常
  • Loading branch information
tywo45 committed Sep 2, 2022
1 parent a2af9ad commit a6fbc18
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 54 deletions.
17 changes: 15 additions & 2 deletions src/core/src/main/java/org/tio/client/ClientChannelContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,21 @@ public ClientChannelContext(TioConfig tioConfig) {
*/
@Override
public Node createClientNode(AsynchronousSocketChannel asynchronousSocketChannel) throws IOException {
InetSocketAddress inetSocketAddress = (InetSocketAddress) asynchronousSocketChannel.getLocalAddress();
Node clientNode = new Node(inetSocketAddress.getHostString(), inetSocketAddress.getPort());
Node clientNode = null;
InetSocketAddress inetSocketAddress = null;
if (asynchronousSocketChannel == null) {
clientNode = createUnknowNode();
} else {
inetSocketAddress = (InetSocketAddress) asynchronousSocketChannel.getLocalAddress();
}

if (inetSocketAddress == null) {
clientNode = createUnknowNode();
}

if (clientNode == null) {
clientNode = new Node(inetSocketAddress.getHostString(), inetSocketAddress.getPort());
}
return clientNode;
}

Expand Down
21 changes: 14 additions & 7 deletions src/core/src/main/java/org/tio/client/TioClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -390,16 +390,23 @@ private ClientChannelContext connect(Node serverNode, String bindIp, Integer bin

CountDownLatch countDownLatch = new CountDownLatch(1);
attachment.setCountDownLatch(countDownLatch);
asynchronousSocketChannel.connect(inetSocketAddress, attachment, tioClientConfig.getConnectionCompletionHandler());
boolean f = countDownLatch.await(realTimeout, TimeUnit.SECONDS);
if (f) {
return attachment.getChannelContext();
} else {
log.error("countDownLatch.await(realTimeout, TimeUnit.SECONDS) 返回false ");

try {
asynchronousSocketChannel.connect(inetSocketAddress, attachment, tioClientConfig.getConnectionCompletionHandler());
} catch (Throwable e) {
tioClientConfig.getConnectionCompletionHandler().failed(e, attachment);
return attachment.getChannelContext();
}

@SuppressWarnings("unused")
boolean f = countDownLatch.await(realTimeout, TimeUnit.SECONDS);
return attachment.getChannelContext();
} else {
asynchronousSocketChannel.connect(inetSocketAddress, attachment, tioClientConfig.getConnectionCompletionHandler());
try {
asynchronousSocketChannel.connect(inetSocketAddress, attachment, tioClientConfig.getConnectionCompletionHandler());
} catch (Throwable e) {
tioClientConfig.getConnectionCompletionHandler().failed(e, attachment);
}
return null;
}
}
Expand Down
14 changes: 9 additions & 5 deletions src/core/src/main/java/org/tio/core/ChannelContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ recommend that a file or class name and description of purpose be included on
import org.tio.core.task.DecodeRunnable;
import org.tio.core.task.HandlerRunnable;
import org.tio.core.task.SendRunnable;
import org.tio.utils.hutool.CollUtil;
import org.tio.utils.hutool.StrUtil;
import org.tio.utils.lock.SetWithLock;
import org.tio.utils.prop.MapWithLockPropSupport;
Expand Down Expand Up @@ -322,10 +321,13 @@ public ChannelContext(TioConfig tioConfig, String id) {

initOther();
}

private void assignAnUnknownClientNode() {
Node clientNode = new Node(UNKNOWN_ADDRESS_IP, UNKNOWN_ADDRESS_PORT_SEQ.incrementAndGet());
setClientNode(clientNode);

protected void assignAnUnknownClientNode() {
setClientNode(createUnknowNode());
}

public static Node createUnknowNode() {
return new Node(UNKNOWN_ADDRESS_IP, UNKNOWN_ADDRESS_PORT_SEQ.incrementAndGet());
}

/**
Expand Down Expand Up @@ -363,6 +365,8 @@ public boolean equals(Object obj) {
}
return true;
}



/**
* 等价于:getAttribute(DEFAULT_ATTUBITE_KEY)
Expand Down
58 changes: 20 additions & 38 deletions src/core/src/main/java/org/tio/core/maintain/ClientNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ recommend that a file or class name and description of purpose be included on
package org.tio.core.maintain;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.locks.Lock;

import org.slf4j.Logger;
Expand All @@ -208,52 +209,34 @@ recommend that a file or class name and description of purpose be included on
* 2017年4月1日 上午9:35:20
*/
public class ClientNodes {
private static Logger log = LoggerFactory.getLogger(ClientNodes.class);
private static final Logger log = LoggerFactory.getLogger(ClientNodes.class);

/**
*
* @param channelContext
* @return
* @author tanyaowu
*/
public static String getKey(ChannelContext channelContext) {
Node clientNode = channelContext.getClientNode();
if (clientNode == null) {
throw new RuntimeException("client node is null");
}
String key = getKey(clientNode.getIp(), clientNode.getPort());
return key;
}
/** remoteAndChannelContext key: Node value: ChannelContext. */
private final MapWithLock<Node, ChannelContext> mapWithLock = new MapWithLock<>();

/**
*
* @param ip
* @param port
* @return
* @param channelContext ChannelContext
* @return Node
* @author tanyaowu
*/
public static String getKey(String ip, int port) {
String key = ip + ":" + port;
return key;
public static Node getKey(ChannelContext channelContext) {
Node clientNode = channelContext.getClientNode();
return Objects.requireNonNull(clientNode, "client node is null");
}

/** remoteAndChannelContext key: "ip:port" value: ChannelContext. */
private MapWithLock<String, ChannelContext> mapWithLock = new MapWithLock<>();

/**
*
* @param key
* @param clientNode
* @return
* @author tanyaowu
*/
public ChannelContext find(String key) {
public ChannelContext find(Node clientNode) {
Lock lock = mapWithLock.readLock();
lock.lock();
try {
Map<String, ChannelContext> m = mapWithLock.getObj();
return m.get(key);
} catch (Throwable e) {
throw e;
Map<Node, ChannelContext> m = mapWithLock.getObj();
return m.get(clientNode);
} finally {
lock.unlock();
}
Expand All @@ -267,31 +250,30 @@ public ChannelContext find(String key) {
* @author tanyaowu
*/
public ChannelContext find(String ip, int port) {
String key = getKey(ip, port);
return find(key);
return find(new Node(ip, port));
}

/**
*
* @return
* @author tanyaowu
*/
public MapWithLock<String, ChannelContext> getObjWithLock() {
public MapWithLock<Node, ChannelContext> getObjWithLock() {
return mapWithLock;
}

/**
* 添加映射
* @param channelContext
* @param channelContext ChannelContext
* @author tanyaowu
*/
public void put(ChannelContext channelContext) {
if (channelContext.tioConfig.isShortConnection) {
return;
}
try {
String key = getKey(channelContext);
mapWithLock.put(key, channelContext);
Node clientNode = getKey(channelContext);
mapWithLock.put(clientNode, channelContext);
} catch (Exception e) {
log.error(e.toString(), e);
}
Expand All @@ -307,8 +289,8 @@ public void remove(ChannelContext channelContext) {
return;
}
try {
String key = getKey(channelContext);
mapWithLock.remove(key);
Node clientNode = getKey(channelContext);
mapWithLock.remove(clientNode);
} catch (Throwable e) {
log.error(e.toString(), e);
}
Expand Down
17 changes: 15 additions & 2 deletions src/core/src/main/java/org/tio/server/ServerChannelContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,21 @@ public ServerChannelContext(TioConfig tioConfig, String id) {
*/
@Override
public Node createClientNode(AsynchronousSocketChannel asynchronousSocketChannel) throws IOException {
InetSocketAddress inetSocketAddress = (InetSocketAddress) asynchronousSocketChannel.getRemoteAddress();
Node clientNode = new Node(inetSocketAddress.getHostString(), inetSocketAddress.getPort());
Node clientNode = null;
InetSocketAddress inetSocketAddress = null;
if (asynchronousSocketChannel == null) {
clientNode = createUnknowNode();
} else {
inetSocketAddress = (InetSocketAddress) asynchronousSocketChannel.getRemoteAddress();
}

if (inetSocketAddress == null) {
clientNode = createUnknowNode();
}

if (clientNode == null) {
clientNode = new Node(inetSocketAddress.getHostString(), inetSocketAddress.getPort());
}
return clientNode;
}

Expand Down

0 comments on commit a6fbc18

Please sign in to comment.