mirror of
https://github.com/happyfish100/fastdfs-client-java.git
synced 2026-09-20 04:08:59 +00:00
Added: 增加IPv6支持
1、增加IPv6地址支持功能。 2、增加 server_ipv6.enabled 配置参数,用来说明服务器端是否开启IPv6支持。 3、修改fdht客户端增加IPv6支持。
This commit is contained in:
@@ -13,6 +13,7 @@ import org.csource.common.MyException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
@@ -48,6 +49,8 @@ public class ClientGlobal {
|
||||
public static final String PROP_KEY_CONNECTION_POOL_MAX_IDLE_TIME = "fastdfs.connection_pool.max_idle_time";
|
||||
public static final String PROP_KEY_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS = "fastdfs.connection_pool.max_wait_time_in_ms";
|
||||
|
||||
public static final String PROP_KEY_SERVER_IPV6_ENABLED = "fastdfs.server_ipv6.enabled";
|
||||
|
||||
public static final int DEFAULT_CONNECT_TIMEOUT = 5; //second
|
||||
public static final int DEFAULT_NETWORK_TIMEOUT = 30; //second
|
||||
public static final String DEFAULT_CHARSET = "UTF-8";
|
||||
@@ -113,12 +116,19 @@ public class ClientGlobal {
|
||||
|
||||
InetSocketAddress[] tracker_servers = new InetSocketAddress[szTrackerServers.length];
|
||||
for (int i = 0; i < szTrackerServers.length; i++) {
|
||||
parts = szTrackerServers[i].split("\\:", 2);
|
||||
if(szTrackerServers[i].contains("[")){
|
||||
parts = new String[2];
|
||||
parts[0] = szTrackerServers[i].substring(1, szTrackerServers[i].indexOf("]"));
|
||||
parts[1] = szTrackerServers[i].substring(szTrackerServers[i].lastIndexOf(":") + 1);
|
||||
}else {
|
||||
parts = szTrackerServers[i].split("\\:", 2);
|
||||
}
|
||||
|
||||
if (parts.length != 2) {
|
||||
throw new MyException("the value of item \"tracker_server\" is invalid, the correct format is host:port");
|
||||
}
|
||||
|
||||
tracker_servers[i] = new InetSocketAddress(parts[0].trim(), Integer.parseInt(parts[1].trim()));
|
||||
tracker_servers[i] = new InetSocketAddress(InetAddress.getByName(parts[0].trim()), Integer.parseInt(parts[1].trim()));
|
||||
}
|
||||
g_tracker_group = new TrackerGroup(tracker_servers);
|
||||
|
||||
@@ -138,6 +148,9 @@ public class ClientGlobal {
|
||||
if (g_connection_pool_max_wait_time_in_ms < 0) {
|
||||
g_connection_pool_max_wait_time_in_ms = DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS;
|
||||
}
|
||||
if(iniReader.getBoolValue("server_ipv6.enabled",false)){
|
||||
ProtoCommon.useIPv6();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -179,6 +192,7 @@ public class ClientGlobal {
|
||||
String poolMaxCountPerEntry = props.getProperty(PROP_KEY_CONNECTION_POOL_MAX_COUNT_PER_ENTRY);
|
||||
String poolMaxIdleTime = props.getProperty(PROP_KEY_CONNECTION_POOL_MAX_IDLE_TIME);
|
||||
String poolMaxWaitTimeInMS = props.getProperty(PROP_KEY_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS);
|
||||
String serverIPv6Enabled = props.getProperty(PROP_KEY_SERVER_IPV6_ENABLED);
|
||||
if (connectTimeoutInSecondsConf != null && connectTimeoutInSecondsConf.trim().length() != 0) {
|
||||
g_connect_timeout = Integer.parseInt(connectTimeoutInSecondsConf.trim()) * 1000;
|
||||
}
|
||||
@@ -209,6 +223,11 @@ public class ClientGlobal {
|
||||
if (poolMaxWaitTimeInMS != null && poolMaxWaitTimeInMS.trim().length() != 0) {
|
||||
g_connection_pool_max_wait_time_in_ms = Integer.parseInt(poolMaxWaitTimeInMS);
|
||||
}
|
||||
if (serverIPv6Enabled != null && serverIPv6Enabled.trim().length() != 0) {
|
||||
if(Boolean.parseBoolean(poolEnabled)){
|
||||
ProtoCommon.useIPv6();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -224,10 +243,16 @@ public class ClientGlobal {
|
||||
String spr2 = ":";
|
||||
String[] arr1 = trackerServers.trim().split(spr1);
|
||||
for (String addrStr : arr1) {
|
||||
String[] arr2 = addrStr.trim().split(spr2);
|
||||
String host = arr2[0].trim();
|
||||
int port = Integer.parseInt(arr2[1].trim());
|
||||
list.add(new InetSocketAddress(host, port));
|
||||
if(addrStr.contains("[")){
|
||||
String host = addrStr.substring(1, addrStr.indexOf("]"));
|
||||
int port = Integer.parseInt(addrStr.substring(addrStr.lastIndexOf(":") + 1));
|
||||
list.add(new InetSocketAddress(InetAddress.getByName(host), port));
|
||||
}else {
|
||||
String[] arr2 = addrStr.trim().split(spr2);
|
||||
String host = arr2[0].trim();
|
||||
int port = Integer.parseInt(arr2[1].trim());
|
||||
list.add(new InetSocketAddress(InetAddress.getByName(host), port));
|
||||
}
|
||||
}
|
||||
InetSocketAddress[] trackerAddresses = list.toArray(new InetSocketAddress[list.size()]);
|
||||
initByTrackers(trackerAddresses);
|
||||
@@ -247,7 +272,7 @@ public class ClientGlobal {
|
||||
public static Socket getSocket(String ip_addr, int port) throws IOException {
|
||||
Socket sock = new Socket();
|
||||
sock.setSoTimeout(ClientGlobal.g_network_timeout);
|
||||
sock.connect(new InetSocketAddress(ip_addr, port), ClientGlobal.g_connect_timeout);
|
||||
sock.connect(new InetSocketAddress(InetAddress.getByName(ip_addr), port), ClientGlobal.g_connect_timeout);
|
||||
return sock;
|
||||
}
|
||||
|
||||
|
||||
@@ -72,15 +72,17 @@ public class ProtoCommon {
|
||||
public static final int FDFS_PROTO_PKG_LEN_SIZE = 8;
|
||||
public static final int FDFS_PROTO_CMD_SIZE = 1;
|
||||
public static final int FDFS_GROUP_NAME_MAX_LEN = 16;
|
||||
public static final int FDFS_IPADDR_SIZE = 16;
|
||||
public static final int FDFS_IPADDR_V4_SIZE = 16;
|
||||
public static final int FDFS_IPADDR_V6_SIZE = 46;
|
||||
public static int FDFS_IPADDR_SIZE = FDFS_IPADDR_V4_SIZE;
|
||||
public static final int FDFS_DOMAIN_NAME_MAX_SIZE = 128;
|
||||
public static final int FDFS_VERSION_SIZE = 6;
|
||||
public static final int FDFS_STORAGE_ID_MAX_SIZE = 16;
|
||||
public static final String FDFS_RECORD_SEPERATOR = "\u0001";
|
||||
public static final String FDFS_FIELD_SEPERATOR = "\u0002";
|
||||
public static final int TRACKER_QUERY_STORAGE_FETCH_BODY_LEN = FDFS_GROUP_NAME_MAX_LEN
|
||||
public static int TRACKER_QUERY_STORAGE_FETCH_BODY_LEN = FDFS_GROUP_NAME_MAX_LEN
|
||||
+ FDFS_IPADDR_SIZE - 1 + FDFS_PROTO_PKG_LEN_SIZE;
|
||||
public static final int TRACKER_QUERY_STORAGE_STORE_BODY_LEN = FDFS_GROUP_NAME_MAX_LEN
|
||||
public static int TRACKER_QUERY_STORAGE_STORE_BODY_LEN = FDFS_GROUP_NAME_MAX_LEN
|
||||
+ FDFS_IPADDR_SIZE + FDFS_PROTO_PKG_LEN_SIZE;
|
||||
public static final byte FDFS_FILE_EXT_NAME_MAX_LEN = 6;
|
||||
public static final byte FDFS_FILE_PREFIX_MAX_LEN = 16;
|
||||
@@ -502,4 +504,16 @@ public class ProtoCommon {
|
||||
this.body_len = body_len;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置系统使用IPv6地址进行通信
|
||||
*/
|
||||
public static void useIPv6(){
|
||||
FDFS_IPADDR_SIZE = FDFS_IPADDR_V6_SIZE;
|
||||
|
||||
TRACKER_QUERY_STORAGE_FETCH_BODY_LEN = FDFS_GROUP_NAME_MAX_LEN
|
||||
+ FDFS_IPADDR_SIZE - 1 + FDFS_PROTO_PKG_LEN_SIZE;
|
||||
TRACKER_QUERY_STORAGE_STORE_BODY_LEN = FDFS_GROUP_NAME_MAX_LEN
|
||||
+ FDFS_IPADDR_SIZE + FDFS_PROTO_PKG_LEN_SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user