Search in sources :

Example 1 with SocketOption

use of java.net.SocketOption in project HolandaCatalinaFw by javaito.

the class NetService method connect.

/**
 * This method finalize the connection process when start a client connection.
 *
 * @param keyChannel Key associated to the connection channel.
 * @param client     Net client asociated to the connectable key.
 */
private void connect(SelectableChannel keyChannel, NetClient client) {
    if (!isShuttingDown()) {
        try {
            SocketChannel channel = (SocketChannel) keyChannel;
            channel.configureBlocking(false);
            channel.socket().setKeepAlive(true);
            channel.socket().setSoTimeout(100);
            channel.finishConnect();
            Map<SocketOption, Object> socketOptions = client.getSocketOptions();
            if (socketOptions != null) {
                for (SocketOption socketOption : socketOptions.keySet()) {
                    channel.setOption(socketOption, socketOptions.get(socketOption));
                }
            }
            NetSession session = getSession(client, createPackage(channel, null, NetPackage.ActionEvent.CONNECT), (SocketChannel) keyChannel);
            if (session != null) {
                sessions.add(session);
                sessionsByChannel.put(channel, session);
                channels.put(session, channel);
                outputQueue.put(channel, new LinkedBlockingQueue<>());
                lastWrite.put(channel, System.currentTimeMillis());
                if (client.getProtocol().equals(TransportLayerProtocol.TCP_SSL)) {
                    SSLHelper sslHelper = new SSLHelper(client.getSSLEngine(), channel, client, session);
                    sslHelpers.put(session, sslHelper);
                } else {
                    NetPackage connectionPackage = createPackage(keyChannel, new byte[] {}, NetPackage.ActionEvent.CONNECT);
                    onAction(connectionPackage, client);
                }
            } else {
                Log.w(SystemProperties.get(SystemProperties.Net.LOG_TAG), "Rejected connection, session null");
                channel.close();
                client.onConnectFail();
            }
        } catch (Exception ex) {
            Log.w(SystemProperties.get(SystemProperties.Net.LOG_TAG), "Error creating new client connection, %s:%d", ex, client.getHost(), client.getPort());
            client.onConnectFail();
        }
    }
}
Also used : SocketOption(java.net.SocketOption) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException)

Example 2 with SocketOption

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

the class MutualSslTest method mutualSsl.

@ParameterizedTest
@MethodSource("params")
void mutualSsl(SslProvider serverSslProvider, SslProvider clientSslProvider, @SuppressWarnings("rawtypes") Map<SocketOption, Object> serverListenOptions, @SuppressWarnings("rawtypes") Map<SocketOption, Object> clientOptions) throws Exception {
    assumeTcpFastOpen(clientOptions);
    HttpServerBuilder serverBuilder = HttpServers.forAddress(localAddress(0)).sslConfig(new ServerSslConfigBuilder(DefaultTestCerts::loadServerPem, DefaultTestCerts::loadServerKey).trustManager(DefaultTestCerts::loadClientCAPem).clientAuthMode(REQUIRE).provider(serverSslProvider).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 = newClientBuilder(serverContext, clientOptions).sslConfig(new ClientSslConfigBuilder(DefaultTestCerts::loadServerCAPem).provider(clientSslProvider).peerHost(serverPemHostname()).keyManager(DefaultTestCerts::loadClientPem, DefaultTestCerts::loadClientKey).build()).buildBlocking()) {
        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) ClientSslConfigBuilder(io.servicetalk.transport.api.ClientSslConfigBuilder) ServerSslConfigBuilder(io.servicetalk.transport.api.ServerSslConfigBuilder) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 3 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 4 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)

Example 5 with SocketOption

use of java.net.SocketOption in project HolandaCatalinaFw by javaito.

the class NetService method accept.

/**
 * This internal method is colled for the main thread when the selector accept
 * an acceptable key to create a new socket with a remote host.
 * This method only will create a socket but without session because the session
 * depends of the communication payload
 *
 * @param keyChannel Select's key.
 */
private void accept(SelectableChannel keyChannel, NetServer server) {
    if (!isShuttingDown()) {
        try {
            ServerSocketChannel serverSocketChannel = (ServerSocketChannel) keyChannel;
            SocketChannel socketChannel = serverSocketChannel.accept();
            socketChannel.configureBlocking(false);
            Map<SocketOption, Object> socketOptions = server.getSocketOptions();
            if (socketOptions != null) {
                for (SocketOption socketOption : socketOptions.keySet()) {
                    socketChannel.setOption(socketOption, socketOptions.get(socketOption));
                }
            }
            NetSession session = getSession(server, createPackage(socketChannel, null, NetPackage.ActionEvent.CONNECT), socketChannel);
            if (session != null) {
                if (channels.containsKey(session)) {
                    updateChannel((SocketChannel) channels.remove(session), socketChannel);
                } else {
                    sessionsByChannel.put(socketChannel, session);
                    outputQueue.put(socketChannel, new LinkedBlockingQueue<>());
                    lastWrite.put(socketChannel, System.currentTimeMillis());
                    channels.put(session, socketChannel);
                }
                if (server.getProtocol().equals(TransportLayerProtocol.TCP_SSL)) {
                    SSLHelper sslHelper = new SSLHelper(server.getSSLEngine(), socketChannel, server, session);
                    sslHelpers.put(session, sslHelper);
                }
                // A new readable key is created associated to the channel.
                socketChannel.register(getSelector(), SelectionKey.OP_READ, server);
                if (isCreationTimeoutAvailable()) {
                    getTimer().schedule(new ConnectionTimeout(socketChannel), getCreationTimeout());
                }
            } else {
                Log.w(SystemProperties.get(SystemProperties.Net.LOG_TAG), "Rejected connection, session null");
                socketChannel.close();
            }
        } catch (Exception ex) {
            Log.w(SystemProperties.get(SystemProperties.Net.LOG_TAG), "Error accepting a new connection.", ex);
        }
    }
}
Also used : SocketOption(java.net.SocketOption) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException)

Aggregations

SocketOption (java.net.SocketOption)8 IOException (java.io.IOException)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 SSLException (javax.net.ssl.SSLException)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 Field (java.lang.reflect.Field)1 InetSocketAddress (java.net.InetSocketAddress)1 AsynchronousServerSocketChannel (java.nio.channels.AsynchronousServerSocketChannel)1 AsynchronousSocketChannel (java.nio.channels.AsynchronousSocketChannel)1