1
0
mirror of https://github.com/square/okhttp.git synced 2026-01-24 04:02:07 +03:00

Platform.getMtu is unused.

This was a failed experiment.
This commit is contained in:
jwilson
2014-01-06 23:28:04 -05:00
parent e0b64b7d10
commit 3eda83ab14

View File

@@ -25,7 +25,6 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.SocketException;
import java.net.URI;
@@ -135,27 +134,8 @@ public class Platform {
}
}
/**
* Returns the maximum transmission unit of the network interface used by
* {@code socket}, or a reasonable default if this platform doesn't expose the
* MTU to the application layer.
*
* <p>The returned value should only be used as an optimization; such as to
* size buffers efficiently.
*/
public int getMtu(Socket socket) throws IOException {
return 1400; // Smaller than 1500 to leave room for headers on interfaces like PPPoE.
}
/** Attempt to match the host runtime to a capable Platform implementation. */
private static Platform findPlatform() {
Method getMtu;
try {
getMtu = NetworkInterface.class.getMethod("getMTU");
} catch (NoSuchMethodException e) {
return new Platform(); // No Java 1.6 APIs. It's either Java 1.5, Android 2.2 or earlier.
}
// Attempt to find Android 2.3+ APIs.
Class<?> openSslSocketClass;
Method setUseSessionTickets;
@@ -176,10 +156,10 @@ public class Platform {
try {
Method setNpnProtocols = openSslSocketClass.getMethod("setNpnProtocols", byte[].class);
Method getNpnSelectedProtocol = openSslSocketClass.getMethod("getNpnSelectedProtocol");
return new Android41(getMtu, openSslSocketClass, setUseSessionTickets, setHostname,
return new Android41(openSslSocketClass, setUseSessionTickets, setHostname,
setNpnProtocols, getNpnSelectedProtocol);
} catch (NoSuchMethodException ignored) {
return new Android23(getMtu, openSslSocketClass, setUseSessionTickets, setHostname);
return new Android23(openSslSocketClass, setUseSessionTickets, setHostname);
}
} catch (ClassNotFoundException ignored) {
// This isn't an Android runtime.
@@ -196,56 +176,25 @@ public class Platform {
Class<?> serverProviderClass = Class.forName(npnClassName + "$ServerProvider");
Method putMethod = nextProtoNegoClass.getMethod("put", SSLSocket.class, providerClass);
Method getMethod = nextProtoNegoClass.getMethod("get", SSLSocket.class);
return new JdkWithJettyNpnPlatform(getMtu, putMethod, getMethod, clientProviderClass,
serverProviderClass);
return new JdkWithJettyNpnPlatform(
putMethod, getMethod, clientProviderClass, serverProviderClass);
} catch (ClassNotFoundException ignored) {
// NPN isn't on the classpath.
} catch (NoSuchMethodException ignored) {
// The NPN version isn't what we expect.
}
return new Java6(getMtu);
}
private static class Java6 extends Platform {
private final Method getMtu;
private Java6(Method getMtu) {
this.getMtu = getMtu;
}
@Override public int getMtu(Socket socket) throws IOException {
try {
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(
socket.getLocalAddress());
if (networkInterface == null) {
return super.getMtu(socket); // There's no longer an interface with this local address.
}
return (Integer) getMtu.invoke(networkInterface);
} catch (NullPointerException e) {
// Certain Alcatel devices throw on getByInetAddress. Return default.
return super.getMtu(socket);
} catch (SocketException e) {
// Certain Motorola devices always throw on getByInetAddress. Return the default for those.
return super.getMtu(socket);
} catch (IllegalAccessException e) {
throw new AssertionError(e);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof IOException) throw (IOException) e.getCause();
throw new RuntimeException(e.getCause());
}
}
return new Platform();
}
/** Android version 2.3 and newer support TLS session tickets and server name indication (SNI). */
private static class Android23 extends Java6 {
private static class Android23 extends Platform {
protected final Class<?> openSslSocketClass;
private final Method setUseSessionTickets;
private final Method setHostname;
private Android23(Method getMtu, Class<?> openSslSocketClass, Method setUseSessionTickets,
Method setHostname) {
super(getMtu);
private Android23(
Class<?> openSslSocketClass, Method setUseSessionTickets, Method setHostname) {
this.openSslSocketClass = openSslSocketClass;
this.setUseSessionTickets = setUseSessionTickets;
this.setHostname = setHostname;
@@ -285,9 +234,9 @@ public class Platform {
private final Method setNpnProtocols;
private final Method getNpnSelectedProtocol;
private Android41(Method getMtu, Class<?> openSslSocketClass, Method setUseSessionTickets,
Method setHostname, Method setNpnProtocols, Method getNpnSelectedProtocol) {
super(getMtu, openSslSocketClass, setUseSessionTickets, setHostname);
private Android41(Class<?> openSslSocketClass, Method setUseSessionTickets, Method setHostname,
Method setNpnProtocols, Method getNpnSelectedProtocol) {
super(openSslSocketClass, setUseSessionTickets, setHostname);
this.setNpnProtocols = setNpnProtocols;
this.getNpnSelectedProtocol = getNpnSelectedProtocol;
}
@@ -320,15 +269,14 @@ public class Platform {
}
/** OpenJDK 7 plus {@code org.mortbay.jetty.npn/npn-boot} on the boot class path. */
private static class JdkWithJettyNpnPlatform extends Java6 {
private static class JdkWithJettyNpnPlatform extends Platform {
private final Method getMethod;
private final Method putMethod;
private final Class<?> clientProviderClass;
private final Class<?> serverProviderClass;
public JdkWithJettyNpnPlatform(Method getMtu, Method putMethod, Method getMethod,
Class<?> clientProviderClass, Class<?> serverProviderClass) {
super(getMtu);
public JdkWithJettyNpnPlatform(Method putMethod, Method getMethod, Class<?> clientProviderClass,
Class<?> serverProviderClass) {
this.putMethod = putMethod;
this.getMethod = getMethod;
this.clientProviderClass = clientProviderClass;