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();
}
}
}
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());
}
}
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());
}
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());
}
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);
}
}
}
Aggregations