Skip to content

Commit da417c7

Browse files
authored
Merge pull request #108 from sunqiangwei1988/master
Added: 增加IPv6支持
2 parents a046265 + 31eb6be commit da417c7

File tree

5 files changed

+78
-12
lines changed

5 files changed

+78
-12
lines changed

fdfs_client.conf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ http.tracker_http_port = 8080
55
http.anti_steal_token = no
66
http.secret_key = FastDFS1234567890
77

8+
#tracker_server
9+
# IPv4:
10+
# for example: 192.168.2.100,122.244.141.46:22122
11+
#
12+
# IPv6:
13+
# for example: [2409:8a20:42d:2f40:587a:4c47:72c0:ad8e]:22122
14+
#
815
tracker_server = 10.0.11.247:22122
916
tracker_server = 10.0.11.248:22122
1017
tracker_server = 10.0.11.249:22122
@@ -13,3 +20,5 @@ connection_pool.enabled = true
1320
connection_pool.max_count_per_entry = 500
1421
connection_pool.max_idle_time = 3600
1522
connection_pool.max_wait_time_in_ms = 1000
23+
24+
server_ipv6.enabled = false

src/main/java/org/csource/fastdfs/ClientGlobal.java

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import java.io.IOException;
1515
import java.io.InputStream;
16+
import java.net.InetAddress;
1617
import java.net.InetSocketAddress;
1718
import java.net.Socket;
1819
import java.util.ArrayList;
@@ -48,6 +49,8 @@ public class ClientGlobal {
4849
public static final String PROP_KEY_CONNECTION_POOL_MAX_IDLE_TIME = "fastdfs.connection_pool.max_idle_time";
4950
public static final String PROP_KEY_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS = "fastdfs.connection_pool.max_wait_time_in_ms";
5051

52+
public static final String PROP_KEY_SERVER_IPV6_ENABLED = "fastdfs.server_ipv6.enabled";
53+
5154
public static final int DEFAULT_CONNECT_TIMEOUT = 5; //second
5255
public static final int DEFAULT_NETWORK_TIMEOUT = 30; //second
5356
public static final String DEFAULT_CHARSET = "UTF-8";
@@ -113,12 +116,19 @@ public static void init(String conf_filename) throws IOException, MyException {
113116

114117
InetSocketAddress[] tracker_servers = new InetSocketAddress[szTrackerServers.length];
115118
for (int i = 0; i < szTrackerServers.length; i++) {
116-
parts = szTrackerServers[i].split("\\:", 2);
119+
if(szTrackerServers[i].contains("[")){
120+
parts = new String[2];
121+
parts[0] = szTrackerServers[i].substring(1, szTrackerServers[i].indexOf("]"));
122+
parts[1] = szTrackerServers[i].substring(szTrackerServers[i].lastIndexOf(":") + 1);
123+
}else {
124+
parts = szTrackerServers[i].split("\\:", 2);
125+
}
126+
117127
if (parts.length != 2) {
118128
throw new MyException("the value of item \"tracker_server\" is invalid, the correct format is host:port");
119129
}
120130

121-
tracker_servers[i] = new InetSocketAddress(parts[0].trim(), Integer.parseInt(parts[1].trim()));
131+
tracker_servers[i] = new InetSocketAddress(InetAddress.getByName(parts[0].trim()), Integer.parseInt(parts[1].trim()));
122132
}
123133
g_tracker_group = new TrackerGroup(tracker_servers);
124134

@@ -138,6 +148,9 @@ public static void init(String conf_filename) throws IOException, MyException {
138148
if (g_connection_pool_max_wait_time_in_ms < 0) {
139149
g_connection_pool_max_wait_time_in_ms = DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS;
140150
}
151+
if(iniReader.getBoolValue("server_ipv6.enabled",false)){
152+
ProtoCommon.useIPv6();
153+
}
141154
}
142155

143156
/**
@@ -179,6 +192,7 @@ public static void initByProperties(Properties props) throws IOException, MyExce
179192
String poolMaxCountPerEntry = props.getProperty(PROP_KEY_CONNECTION_POOL_MAX_COUNT_PER_ENTRY);
180193
String poolMaxIdleTime = props.getProperty(PROP_KEY_CONNECTION_POOL_MAX_IDLE_TIME);
181194
String poolMaxWaitTimeInMS = props.getProperty(PROP_KEY_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS);
195+
String serverIPv6Enabled = props.getProperty(PROP_KEY_SERVER_IPV6_ENABLED);
182196
if (connectTimeoutInSecondsConf != null && connectTimeoutInSecondsConf.trim().length() != 0) {
183197
g_connect_timeout = Integer.parseInt(connectTimeoutInSecondsConf.trim()) * 1000;
184198
}
@@ -209,6 +223,11 @@ public static void initByProperties(Properties props) throws IOException, MyExce
209223
if (poolMaxWaitTimeInMS != null && poolMaxWaitTimeInMS.trim().length() != 0) {
210224
g_connection_pool_max_wait_time_in_ms = Integer.parseInt(poolMaxWaitTimeInMS);
211225
}
226+
if (serverIPv6Enabled != null && serverIPv6Enabled.trim().length() != 0) {
227+
if(Boolean.parseBoolean(poolEnabled)){
228+
ProtoCommon.useIPv6();
229+
}
230+
}
212231
}
213232

214233
/**
@@ -224,10 +243,16 @@ public static void initByTrackers(String trackerServers) throws IOException, MyE
224243
String spr2 = ":";
225244
String[] arr1 = trackerServers.trim().split(spr1);
226245
for (String addrStr : arr1) {
227-
String[] arr2 = addrStr.trim().split(spr2);
228-
String host = arr2[0].trim();
229-
int port = Integer.parseInt(arr2[1].trim());
230-
list.add(new InetSocketAddress(host, port));
246+
if(addrStr.contains("[")){
247+
String host = addrStr.substring(1, addrStr.indexOf("]"));
248+
int port = Integer.parseInt(addrStr.substring(addrStr.lastIndexOf(":") + 1));
249+
list.add(new InetSocketAddress(InetAddress.getByName(host), port));
250+
}else {
251+
String[] arr2 = addrStr.trim().split(spr2);
252+
String host = arr2[0].trim();
253+
int port = Integer.parseInt(arr2[1].trim());
254+
list.add(new InetSocketAddress(InetAddress.getByName(host), port));
255+
}
231256
}
232257
InetSocketAddress[] trackerAddresses = list.toArray(new InetSocketAddress[list.size()]);
233258
initByTrackers(trackerAddresses);
@@ -247,7 +272,7 @@ public static void initByTrackers(InetSocketAddress[] trackerAddresses) throws I
247272
public static Socket getSocket(String ip_addr, int port) throws IOException {
248273
Socket sock = new Socket();
249274
sock.setSoTimeout(ClientGlobal.g_network_timeout);
250-
sock.connect(new InetSocketAddress(ip_addr, port), ClientGlobal.g_connect_timeout);
275+
sock.connect(new InetSocketAddress(InetAddress.getByName(ip_addr), port), ClientGlobal.g_connect_timeout);
251276
return sock;
252277
}
253278

src/main/java/org/csource/fastdfs/ProtoCommon.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,17 @@ public class ProtoCommon {
7272
public static final int FDFS_PROTO_PKG_LEN_SIZE = 8;
7373
public static final int FDFS_PROTO_CMD_SIZE = 1;
7474
public static final int FDFS_GROUP_NAME_MAX_LEN = 16;
75-
public static final int FDFS_IPADDR_SIZE = 16;
75+
public static final int FDFS_IPADDR_V4_SIZE = 16;
76+
public static final int FDFS_IPADDR_V6_SIZE = 46;
77+
public static int FDFS_IPADDR_SIZE = FDFS_IPADDR_V4_SIZE;
7678
public static final int FDFS_DOMAIN_NAME_MAX_SIZE = 128;
7779
public static final int FDFS_VERSION_SIZE = 6;
7880
public static final int FDFS_STORAGE_ID_MAX_SIZE = 16;
7981
public static final String FDFS_RECORD_SEPERATOR = "\u0001";
8082
public static final String FDFS_FIELD_SEPERATOR = "\u0002";
81-
public static final int TRACKER_QUERY_STORAGE_FETCH_BODY_LEN = FDFS_GROUP_NAME_MAX_LEN
83+
public static int TRACKER_QUERY_STORAGE_FETCH_BODY_LEN = FDFS_GROUP_NAME_MAX_LEN
8284
+ FDFS_IPADDR_SIZE - 1 + FDFS_PROTO_PKG_LEN_SIZE;
83-
public static final int TRACKER_QUERY_STORAGE_STORE_BODY_LEN = FDFS_GROUP_NAME_MAX_LEN
85+
public static int TRACKER_QUERY_STORAGE_STORE_BODY_LEN = FDFS_GROUP_NAME_MAX_LEN
8486
+ FDFS_IPADDR_SIZE + FDFS_PROTO_PKG_LEN_SIZE;
8587
public static final byte FDFS_FILE_EXT_NAME_MAX_LEN = 6;
8688
public static final byte FDFS_FILE_PREFIX_MAX_LEN = 16;
@@ -502,4 +504,16 @@ public RecvHeaderInfo(byte errno, long body_len) {
502504
this.body_len = body_len;
503505
}
504506
}
507+
508+
/**
509+
* 设置系统使用IPv6地址进行通信
510+
*/
511+
public static void useIPv6(){
512+
FDFS_IPADDR_SIZE = FDFS_IPADDR_V6_SIZE;
513+
514+
TRACKER_QUERY_STORAGE_FETCH_BODY_LEN = FDFS_GROUP_NAME_MAX_LEN
515+
+ FDFS_IPADDR_SIZE - 1 + FDFS_PROTO_PKG_LEN_SIZE;
516+
TRACKER_QUERY_STORAGE_STORE_BODY_LEN = FDFS_GROUP_NAME_MAX_LEN
517+
+ FDFS_IPADDR_SIZE + FDFS_PROTO_PKG_LEN_SIZE;
518+
}
505519
}

src/main/resources/fastdfs-client.properties.sample

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ fastdfs.http_anti_steal_token = false
99
fastdfs.http_secret_key = FastDFS1234567890
1010
fastdfs.http_tracker_http_port = 80
1111

12+
#tracker_server
13+
# IPv4:
14+
# for example: 192.168.2.100,122.244.141.46:22122
15+
#
16+
# IPv6:
17+
# for example: [2409:8a20:42d:2f40:587a:4c47:72c0:ad8e]:22122
18+
#
1219
fastdfs.tracker_servers = 185.245.40.70:22122
1320

1421
## Whether to open the connection pool, if not, create a new connection every time
@@ -21,4 +28,6 @@ fastdfs.connection_pool.max_count_per_entry = 500
2128
fastdfs.connection_pool.max_idle_time = 3600
2229

2330
## Maximum waiting time when the maximum number of connections is reached, unit: millisecond, default value is 1000
24-
fastdfs.connection_pool.max_wait_time_in_ms = 1000
31+
fastdfs.connection_pool.max_wait_time_in_ms = 1000
32+
33+
server_ipv6.enabled = false

src/main/resources/fdfs_client.conf.sample

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,19 @@ http.tracker_http_port = 8080
55
http.anti_steal_token = no
66
http.secret_key = FastDFS1234567890
77

8+
#tracker_server
9+
# IPv4:
10+
# for example: 192.168.2.100,122.244.141.46:22122
11+
#
12+
# IPv6:
13+
# for example: [2409:8a20:42d:2f40:587a:4c47:72c0:ad8e]:22122
14+
#
815
tracker_server = 10.0.11.243:22122
916
tracker_server = 10.0.11.244:22122
1017

1118
connection_pool.enabled = true
1219
connection_pool.max_count_per_entry = 500
1320
connection_pool.max_idle_time = 3600
14-
connection_pool.max_wait_time_in_ms = 1000
21+
connection_pool.max_wait_time_in_ms = 1000
22+
23+
server_ipv6.enabled = false

0 commit comments

Comments
 (0)