/** * Creates a builder instance. * * @param socketSettings existing SocketSettings to default the builder settings on. * @return a builder * @since 3.7 */ public static Builder builder(final SocketSettings socketSettings) { return builder().applySettings(socketSettings); }
/** * Build an instance of {@code SocketSettings}. * @return the socket settings for this builder */ public SocketSettings build() { return new SocketSettings(this); } }
@SuppressWarnings("deprecation") static void initialize(final Socket socket, final InetSocketAddress inetSocketAddress, final SocketSettings settings, final SslSettings sslSettings) throws IOException { socket.setTcpNoDelay(true); socket.setSoTimeout(settings.getReadTimeout(MILLISECONDS)); socket.setKeepAlive(settings.isKeepAlive()); if (settings.getReceiveBufferSize() > 0) { socket.setReceiveBufferSize(settings.getReceiveBufferSize()); } if (settings.getSendBufferSize() > 0) { socket.setSendBufferSize(settings.getSendBufferSize()); } if (sslSettings.isEnabled() || socket instanceof SSLSocket) { if (!(socket instanceof SSLSocket)) { throw new MongoInternalException("SSL is enabled but the socket is not an instance of javax.net.ssl.SSLSocket"); } SSLSocket sslSocket = (SSLSocket) socket; SSLParameters sslParameters = sslSocket.getSSLParameters(); if (sslParameters == null) { sslParameters = new SSLParameters(); } enableSni(inetSocketAddress.getHostName(), sslParameters); if (!sslSettings.isInvalidHostNameAllowed()) { enableHostNameVerification(sslParameters); } sslSocket.setSSLParameters(sslParameters); } socket.connect(inetSocketAddress, settings.getConnectTimeout(MILLISECONDS)); }
@SuppressWarnings("deprecation") private void initializeSocketChannel(final AsyncCompletionHandler<Void> handler, final Queue<SocketAddress> socketAddressQueue) { if (socketAddressQueue.isEmpty()) { handler.failed(new MongoSocketException("Exception opening socket", serverAddress)); } else { SocketAddress socketAddress = socketAddressQueue.poll(); try { AsynchronousSocketChannel attemptConnectionChannel = AsynchronousSocketChannel.open(group); attemptConnectionChannel.setOption(StandardSocketOptions.TCP_NODELAY, true); attemptConnectionChannel.setOption(StandardSocketOptions.SO_KEEPALIVE, settings.isKeepAlive()); if (settings.getReceiveBufferSize() > 0) { attemptConnectionChannel.setOption(StandardSocketOptions.SO_RCVBUF, settings.getReceiveBufferSize()); } if (settings.getSendBufferSize() > 0) { attemptConnectionChannel.setOption(StandardSocketOptions.SO_SNDBUF, settings.getSendBufferSize()); } attemptConnectionChannel.connect(socketAddress, null, new OpenCompletionHandler(handler, socketAddressQueue, attemptConnectionChannel)); } catch (IOException e) { handler.failed(new MongoSocketOpenException("Exception opening socket", serverAddress, e)); } catch (Throwable t) { handler.failed(t); } } }
private MongoClientSettings(final Builder builder) { readPreference = builder.readPreference; writeConcern = builder.writeConcern; retryWrites = builder.retryWrites; readConcern = builder.readConcern; credential = builder.credential; streamFactoryFactory = builder.streamFactoryFactory; codecRegistry = builder.codecRegistry; commandListeners = builder.commandListeners; applicationName = builder.applicationName; clusterSettings = builder.clusterSettingsBuilder.build(); serverSettings = builder.serverSettingsBuilder.build(); socketSettings = builder.socketSettingsBuilder.build(); connectionPoolSettings = builder.connectionPoolSettingsBuilder.build(); sslSettings = builder.sslSettingsBuilder.build(); compressorList = builder.compressorList; SocketSettings.Builder heartbeatSocketSettingsBuilder = SocketSettings.builder() .readTimeout(socketSettings.getConnectTimeout(MILLISECONDS), MILLISECONDS) .connectTimeout(socketSettings.getConnectTimeout(MILLISECONDS), MILLISECONDS); heartbeatSocketSettings = heartbeatSocketSettingsBuilder.build(); } }
if (getSettings().getReceiveBufferSize() > 0) { socketChannel.setOption(StandardSocketOptions.SO_RCVBUF, getSettings().getReceiveBufferSize()); if (getSettings().getSendBufferSize() > 0) { socketChannel.setOption(StandardSocketOptions.SO_SNDBUF, getSettings().getSendBufferSize());
@Override public void readAsync(final int numBytes, final AsyncCompletionHandler<ByteBuf> handler) { ByteBuf buffer = bufferProvider.getBuffer(numBytes); channel.read(buffer.asNIO(), settings.getReadTimeout(MILLISECONDS), MILLISECONDS, null, new BasicCompletionHandler(buffer, handler)); }
private MongoClientSettings(final Builder builder) { readPreference = builder.readPreference; writeConcern = builder.writeConcern; retryWrites = builder.retryWrites; readConcern = builder.readConcern; credential = builder.credential; streamFactoryFactory = builder.streamFactoryFactory; codecRegistry = builder.codecRegistry; commandListeners = builder.commandListeners; applicationName = builder.applicationName; clusterSettings = builder.clusterSettingsBuilder.build(); serverSettings = builder.serverSettingsBuilder.build(); socketSettings = builder.socketSettingsBuilder.build(); connectionPoolSettings = builder.connectionPoolSettingsBuilder.build(); sslSettings = builder.sslSettingsBuilder.build(); compressorList = builder.compressorList; SocketSettings.Builder heartbeatSocketSettingsBuilder = SocketSettings.builder() .readTimeout(socketSettings.getConnectTimeout(MILLISECONDS), MILLISECONDS) .connectTimeout(socketSettings.getConnectTimeout(MILLISECONDS), MILLISECONDS); heartbeatSocketSettings = heartbeatSocketSettingsBuilder.build(); } }
@SuppressWarnings("deprecation") private void initializeSocketChannel(final AsyncCompletionHandler<Void> handler, final Queue<SocketAddress> socketAddressQueue) { if (socketAddressQueue.isEmpty()) { handler.failed(new MongoSocketException("Exception opening socket", serverAddress)); } else { SocketAddress socketAddress = socketAddressQueue.poll(); try { AsynchronousSocketChannel attemptConnectionChannel = AsynchronousSocketChannel.open(group); attemptConnectionChannel.setOption(StandardSocketOptions.TCP_NODELAY, true); attemptConnectionChannel.setOption(StandardSocketOptions.SO_KEEPALIVE, settings.isKeepAlive()); if (settings.getReceiveBufferSize() > 0) { attemptConnectionChannel.setOption(StandardSocketOptions.SO_RCVBUF, settings.getReceiveBufferSize()); } if (settings.getSendBufferSize() > 0) { attemptConnectionChannel.setOption(StandardSocketOptions.SO_SNDBUF, settings.getSendBufferSize()); } attemptConnectionChannel.connect(socketAddress, null, new OpenCompletionHandler(handler, socketAddressQueue, attemptConnectionChannel)); } catch (IOException e) { handler.failed(new MongoSocketOpenException("Exception opening socket", serverAddress, e)); } catch (Throwable t) { handler.failed(t); } } }
if (getSettings().getReceiveBufferSize() > 0) { socketChannel.setOption(StandardSocketOptions.SO_RCVBUF, getSettings().getReceiveBufferSize()); if (getSettings().getSendBufferSize() > 0) { socketChannel.setOption(StandardSocketOptions.SO_SNDBUF, getSettings().getSendBufferSize());
@Override public void initChannel(final SocketChannel ch) { if (sslSettings.isEnabled()) { SSLEngine engine = getSslContext().createSSLEngine(address.getHost(), address.getPort()); engine.setUseClientMode(true); SSLParameters sslParameters = engine.getSSLParameters(); enableSni(address.getHost(), sslParameters); if (!sslSettings.isInvalidHostNameAllowed()) { enableHostNameVerification(sslParameters); } engine.setSSLParameters(sslParameters); ch.pipeline().addFirst("ssl", new SslHandler(engine, false)); } int readTimeout = settings.getReadTimeout(MILLISECONDS); if (readTimeout > 0) { ch.pipeline().addLast(READ_HANDLER_NAME, new ReadTimeoutHandler(readTimeout)); } ch.pipeline().addLast(new InboundBufferHandler()); } });
bootstrap.channel(socketChannelClass); bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, settings.getConnectTimeout(MILLISECONDS)); bootstrap.option(ChannelOption.TCP_NODELAY, true); bootstrap.option(ChannelOption.SO_KEEPALIVE, settings.isKeepAlive()); if (settings.getReceiveBufferSize() > 0) { bootstrap.option(ChannelOption.SO_RCVBUF, settings.getReceiveBufferSize()); if (settings.getSendBufferSize() > 0) { bootstrap.option(ChannelOption.SO_SNDBUF, settings.getSendBufferSize());
static SocketSettings socket(final String path, final ConnectionString cstr, final Config dbconf) { SocketSettings.Builder settings = SocketSettings.builder().applyConnectionString(cstr); withConf(path, dbconf, c -> { withMs("connectTimeout", c, s -> settings.connectTimeout(s.intValue(), TimeUnit.MILLISECONDS)); withBool("keepAlive", c, settings::keepAlive); withMs("readTimeout", c, s -> settings.readTimeout(s.intValue(), TimeUnit.MILLISECONDS)); withInt("receiveBufferSize", c, settings::receiveBufferSize); withInt("sendBufferSize", c, settings::sendBufferSize); }); return settings.build(); }
@Override public void completed(final Integer result, final Void attachment) { AsyncCompletionHandler<ByteBuf> localHandler = getHandlerAndClear(); ByteBuf localByteBuf = byteBufReference.getAndSet(null); if (result == -1) { localByteBuf.release(); localHandler.failed(new MongoSocketReadException("Prematurely reached end of stream", serverAddress)); } else if (!localByteBuf.hasRemaining()) { localByteBuf.flip(); localHandler.completed(localByteBuf); } else { channel.read(localByteBuf.asNIO(), settings.getReadTimeout(MILLISECONDS), MILLISECONDS, null, new BasicCompletionHandler(localByteBuf, localHandler)); } }
/** * Build an instance of {@code SocketSettings}. * @return the socket settings for this builder */ public SocketSettings build() { return new SocketSettings(this); } }
@SuppressWarnings("deprecation") static void initialize(final Socket socket, final InetSocketAddress inetSocketAddress, final SocketSettings settings, final SslSettings sslSettings) throws IOException { socket.setTcpNoDelay(true); socket.setSoTimeout(settings.getReadTimeout(MILLISECONDS)); socket.setKeepAlive(settings.isKeepAlive()); if (settings.getReceiveBufferSize() > 0) { socket.setReceiveBufferSize(settings.getReceiveBufferSize()); } if (settings.getSendBufferSize() > 0) { socket.setSendBufferSize(settings.getSendBufferSize()); } if (sslSettings.isEnabled() || socket instanceof SSLSocket) { if (!(socket instanceof SSLSocket)) { throw new MongoInternalException("SSL is enabled but the socket is not an instance of javax.net.ssl.SSLSocket"); } SSLSocket sslSocket = (SSLSocket) socket; SSLParameters sslParameters = sslSocket.getSSLParameters(); if (sslParameters == null) { sslParameters = new SSLParameters(); } enableSni(inetSocketAddress.getHostName(), sslParameters); if (!sslSettings.isInvalidHostNameAllowed()) { enableHostNameVerification(sslParameters); } sslSocket.setSSLParameters(sslParameters); } socket.connect(inetSocketAddress, settings.getConnectTimeout(MILLISECONDS)); }
socketSettings = SocketSettings.builder() .connectTimeout(getConnectTimeout(), MILLISECONDS) .readTimeout(getSocketTimeout(), MILLISECONDS) .keepAlive(isSocketKeepAlive()) .build(); heartbeatSocketSettings = SocketSettings.builder() .connectTimeout(getHeartbeatConnectTimeout(), MILLISECONDS) .readTimeout(getHeartbeatSocketTimeout(), MILLISECONDS)
@Override public void readAsync(final int numBytes, final AsyncCompletionHandler<ByteBuf> handler) { ByteBuf buffer = bufferProvider.getBuffer(numBytes); channel.read(buffer.asNIO(), settings.getReadTimeout(MILLISECONDS), MILLISECONDS, null, new BasicCompletionHandler(buffer, handler)); }
bootstrap.channel(socketChannelClass); bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, settings.getConnectTimeout(MILLISECONDS)); bootstrap.option(ChannelOption.TCP_NODELAY, true); bootstrap.option(ChannelOption.SO_KEEPALIVE, settings.isKeepAlive()); if (settings.getReceiveBufferSize() > 0) { bootstrap.option(ChannelOption.SO_RCVBUF, settings.getReceiveBufferSize()); if (settings.getSendBufferSize() > 0) { bootstrap.option(ChannelOption.SO_SNDBUF, settings.getSendBufferSize());
/** * Creates a builder instance. * * @param socketSettings existing SocketSettings to default the builder settings on. * @return a builder * @since 3.7 */ public static Builder builder(final SocketSettings socketSettings) { return builder().applySettings(socketSettings); }