1313
1414import java .io .IOException ;
1515import java .io .InputStream ;
16+ import java .net .InetAddress ;
1617import java .net .InetSocketAddress ;
1718import java .net .Socket ;
1819import 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
0 commit comments