throw IOException with msg "connection broken" when in.read() < 0

This commit is contained in:
YuQing
2025-11-27 17:13:23 +08:00
parent 7b7a7d76f6
commit cc95bc0244
6 changed files with 22 additions and 12 deletions
+1
View File
@@ -1,6 +1,7 @@
Version 1.38 2025-11-27
* bugfixed: loadStorageServersFromTracker correctly with rw option
* throw IOException with msg "connection broken" when in.read() < 0
Version 1.37 2025-11-06
* query file info support two combined flags:
+1 -1
View File
@@ -4,7 +4,7 @@
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.37-SNAPSHOT</version>
<version>1.38-SNAPSHOT</version>
<name>fastdfs-client-java</name>
<description>fastdfs client for java</description>
<packaging>jar</packaging>
@@ -180,9 +180,13 @@ public class ProtoCommon {
long pkg_len;
header = new byte[FDFS_PROTO_PKG_LEN_SIZE + 2];
if ((bytes = in.read(header)) != header.length) {
throw new IOException("recv package size " + bytes + " != " + header.length);
bytes = in.read(header);
if (bytes != header.length) {
if (bytes < 0) {
throw new IOException("connection broken");
} else {
throw new IOException("recv package size " + bytes + " != " + header.length);
}
}
if (header[PROTO_HEADER_CMD_INDEX] != expect_cmd) {
@@ -222,7 +226,7 @@ public class ProtoCommon {
byte[] body = new byte[(int) header.body_len];
int totalBytes = 0;
int remainBytes = (int) header.body_len;
int bytes;
int bytes = 0;
while (totalBytes < header.body_len) {
if ((bytes = in.read(body, totalBytes, remainBytes)) < 0) {
@@ -234,7 +238,12 @@ public class ProtoCommon {
}
if (totalBytes != header.body_len) {
throw new IOException("recv package size " + totalBytes + " != " + header.body_len);
if (totalBytes == 0) {
throw new IOException("connection broken");
} else {
throw new IOException("connection broken, recv length: " + totalBytes
+ ", expect length: " + header.body_len);
}
}
return new RecvPackageInfo((byte) 0, body);
@@ -85,13 +85,13 @@ public class TrackerClient {
connection = trackerServer.getConnection();
} catch (IOException e) {
if (failOver) {
System.err.println("trackerServer get connection error, emsg:" + e.getMessage());
System.err.println("trackerServer get connection error, " + e.getMessage());
} else {
throw e;
}
} catch (MyException e) {
if (failOver) {
System.err.println("trackerServer get connection error, emsg:" + e.getMessage());
System.err.println("trackerServer get connection error, " + e.getMessage());
} else {
throw e;
}
@@ -23,7 +23,7 @@ public class ConnectionFactory {
sock.connect(socketAddress, ClientGlobal.g_connect_timeout);
return new Connection(sock, socketAddress);
} catch (Exception e) {
throw new MyException("connect to server " + socketAddress.getAddress().getHostAddress() + ":" + socketAddress.getPort() + " fail, emsg:" + e.getMessage());
throw new MyException("connect to server " + socketAddress.getAddress().getHostAddress() + ":" + socketAddress.getPort() + " fail, emsg: " + e.getMessage());
}
}
}
@@ -62,7 +62,7 @@ public class ConnectionManager {
try {
isActive = connection.activeTest();
} catch (IOException e) {
System.err.println("send to server[" + inetSocketAddress.getAddress().getHostAddress() + ":" + inetSocketAddress.getPort() + "] active test error ,emsg:" + e.getMessage());
System.err.println("send to server[" + inetSocketAddress.getAddress().getHostAddress() + ":" + inetSocketAddress.getPort() + "] active test error, emsg: " + e.getMessage());
isActive = false;
}
if (!isActive) {
@@ -84,7 +84,7 @@ public class ConnectionManager {
throw new MyException("connect to server " + inetSocketAddress.getAddress().getHostAddress() + ":" + inetSocketAddress.getPort() + " fail, wait_time > " + ClientGlobal.g_connection_pool_max_wait_time_in_ms + "ms");
} catch (InterruptedException e) {
e.printStackTrace();
throw new MyException("connect to server " + inetSocketAddress.getAddress().getHostAddress() + ":" + inetSocketAddress.getPort() + " fail, emsg:" + e.getMessage());
throw new MyException("connect to server " + inetSocketAddress.getAddress().getHostAddress() + ":" + inetSocketAddress.getPort() + " fail, emsg: " + e.getMessage());
}
}
return connection;
@@ -117,7 +117,7 @@ public class ConnectionManager {
connection.closeDirectly();
}
} catch (IOException e) {
System.err.println("close socket[" + inetSocketAddress.getAddress().getHostAddress() + ":" + inetSocketAddress.getPort() + "] error ,emsg:" + e.getMessage());
System.err.println("close socket[" + inetSocketAddress.getAddress().getHostAddress() + ":" + inetSocketAddress.getPort() + "] error, emsg: " + e.getMessage());
e.printStackTrace();
}
}