Search in sources :

Example 6 with SocketOption

use of java.net.SocketOption in project OpenSearch by opensearch-project.

the class NetUtils method getExtendedSocketOptionOrNull.

@SuppressWarnings("unchecked")
private static <T> SocketOption<T> getExtendedSocketOptionOrNull(String fieldName) {
    try {
        final Class<?> extendedSocketOptionsClass = Class.forName("jdk.net.ExtendedSocketOptions");
        final Field field = extendedSocketOptionsClass.getField(fieldName);
        return (SocketOption<T>) field.get(null);
    } catch (Exception t) {
        // ignore
        return null;
    }
}
Also used : Field(java.lang.reflect.Field) SocketOption(java.net.SocketOption) IOException(java.io.IOException)

Example 7 with SocketOption

use of java.net.SocketOption in project servicetalk by apple.

the class TcpFastOpenTest method newClient.

private static BlockingHttpClient newClient(final ServerContext serverContext, final Collection<HttpProtocol> protocols, final boolean secure, @SuppressWarnings("rawtypes") final Map<SocketOption, Object> clientOptions) {
    SingleAddressHttpClientBuilder<HostAndPort, InetSocketAddress> builder = HttpClients.forSingleAddress(serverHostAndPort(serverContext)).protocols(toConfigs(protocols));
    if (secure) {
        builder.sslConfig(new ClientSslConfigBuilder(DefaultTestCerts::loadServerCAPem).sniHostname(serverPemHostname()).build());
    }
    for (@SuppressWarnings("rawtypes") Entry<SocketOption, Object> entry : clientOptions.entrySet()) {
        @SuppressWarnings("unchecked") SocketOption<Object> option = entry.getKey();
        builder.socketOption(option, entry.getValue());
    }
    return builder.buildBlocking();
}
Also used : AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) HostAndPort(io.servicetalk.transport.api.HostAndPort) SocketOption(java.net.SocketOption) InetSocketAddress(java.net.InetSocketAddress) ClientSslConfigBuilder(io.servicetalk.transport.api.ClientSslConfigBuilder)

Example 8 with SocketOption

use of java.net.SocketOption in project servicetalk by apple.

the class TcpFastOpenTest method requestSucceedsEvenIfTcpFastOpenNotEnabledOrSupported.

@ParameterizedTest(name = "{displayName} [{index}] protocols={0}, secure={1}, serverListenOptions={2}, clientOptions={3}")
@MethodSource("sslProviders")
void requestSucceedsEvenIfTcpFastOpenNotEnabledOrSupported(final Collection<HttpProtocol> protocols, final boolean secure, @SuppressWarnings("rawtypes") final Map<SocketOption, Object> serverListenOptions, @SuppressWarnings("rawtypes") final Map<SocketOption, Object> clientOptions) throws Exception {
    assumeTcpFastOpen(clientOptions);
    HttpServerBuilder serverBuilder = HttpServers.forAddress(localAddress(0)).protocols(toConfigs(protocols));
    if (secure) {
        serverBuilder.sslConfig(new ServerSslConfigBuilder(DefaultTestCerts::loadServerPem, DefaultTestCerts::loadServerKey).build());
    }
    for (@SuppressWarnings("rawtypes") Entry<SocketOption, Object> entry : serverListenOptions.entrySet()) {
        @SuppressWarnings("unchecked") SocketOption<Object> option = entry.getKey();
        serverBuilder.listenSocketOption(option, entry.getValue());
    }
    try (ServerContext serverContext = serverBuilder.listenBlockingAndAwait((ctx, request, responseFactory) -> responseFactory.ok());
        BlockingHttpClient client = newClient(serverContext, protocols, secure, clientOptions)) {
        assertEquals(HttpResponseStatus.OK, client.request(client.get("/")).status());
    }
}
Also used : SocketOption(java.net.SocketOption) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) HttpServerBuilder(io.servicetalk.http.api.HttpServerBuilder) DefaultTestCerts(io.servicetalk.test.resources.DefaultTestCerts) ServerSslConfigBuilder(io.servicetalk.transport.api.ServerSslConfigBuilder) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 9 with SocketOption

use of java.net.SocketOption in project j2objc by google.

the class AsynchronousSocketChannelTest method test_supportedOptions.

public void test_supportedOptions() throws Throwable {
    AsynchronousSocketChannel assc = AsynchronousSocketChannel.open();
    Set<SocketOption<?>> supportedOptions = assc.supportedOptions();
    assertEquals(5, supportedOptions.size());
    assertTrue(supportedOptions.contains(StandardSocketOptions.SO_REUSEADDR));
    assertTrue(supportedOptions.contains(StandardSocketOptions.SO_RCVBUF));
    assertTrue(supportedOptions.contains(StandardSocketOptions.SO_SNDBUF));
    assertTrue(supportedOptions.contains(StandardSocketOptions.SO_KEEPALIVE));
    assertTrue(supportedOptions.contains(StandardSocketOptions.TCP_NODELAY));
    // supportedOptions should work after close according to spec
    assc.close();
    supportedOptions = assc.supportedOptions();
    assertEquals(5, supportedOptions.size());
}
Also used : AsynchronousSocketChannel(java.nio.channels.AsynchronousSocketChannel) SocketOption(java.net.SocketOption)

Example 10 with SocketOption

use of java.net.SocketOption in project j2objc by google.

the class AsynchronousServerSocketChannelTest method test_supportedOptions.

public void test_supportedOptions() throws Throwable {
    AsynchronousServerSocketChannel assc = AsynchronousServerSocketChannel.open();
    Set<SocketOption<?>> supportedOptions = assc.supportedOptions();
    assertEquals(2, supportedOptions.size());
    assertTrue(supportedOptions.contains(StandardSocketOptions.SO_REUSEADDR));
    assertTrue(supportedOptions.contains(StandardSocketOptions.SO_RCVBUF));
    // supportedOptions should work after close according to spec
    assc.close();
    supportedOptions = assc.supportedOptions();
    assertEquals(2, supportedOptions.size());
}
Also used : SocketOption(java.net.SocketOption) AsynchronousServerSocketChannel(java.nio.channels.AsynchronousServerSocketChannel)

Aggregations

SocketOption (java.net.SocketOption)13 IOException (java.io.IOException)6 Map (java.util.Map)4 InetSocketAddress (java.net.InetSocketAddress)3 AsynchronousSocketChannel (java.nio.channels.AsynchronousSocketChannel)3 BlockingHttpClient (io.servicetalk.http.api.BlockingHttpClient)2 HttpServerBuilder (io.servicetalk.http.api.HttpServerBuilder)2 DefaultTestCerts (io.servicetalk.test.resources.DefaultTestCerts)2 ClientSslConfigBuilder (io.servicetalk.transport.api.ClientSslConfigBuilder)2 ServerContext (io.servicetalk.transport.api.ServerContext)2 ServerSslConfigBuilder (io.servicetalk.transport.api.ServerSslConfigBuilder)2 Field (java.lang.reflect.Field)2 LinkedHashMap (java.util.LinkedHashMap)2 Semaphore (java.util.concurrent.Semaphore)2 SSLException (javax.net.ssl.SSLException)2 Property (org.apache.sshd.common.Property)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)2 HostAndPort (io.servicetalk.transport.api.HostAndPort)1 AddressUtils.serverHostAndPort (io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort)1