@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); } } }
@Override public <T> AsyncFiberSocketChannel setOption(SocketOption<T> name, T value) throws IOException { ac.setOption(name, value); return this; }
@Override public <T> FiberSocketChannel setOption(SocketOption<T> name, T value) throws IOException { ac.setOption(name, value); return this; }
@Override public void setTcpNoDelay(boolean on) throws IOException { this.socketChannel.setOption(StandardSocketOptions.TCP_NODELAY, on); }
@Override public void setKeepAlive(boolean on) throws IOException { this.socketChannel.setOption(StandardSocketOptions.SO_KEEPALIVE, on); }
public void setTcpNoDelay(boolean on) throws IOException { channel.setOption(StandardSocketOptions.TCP_NODELAY, on); }
public void setProperties(AsynchronousSocketChannel socket) throws IOException { if (rxBufSize != null) socket.setOption(StandardSocketOptions.SO_RCVBUF, rxBufSize); if (txBufSize != null) socket.setOption(StandardSocketOptions.SO_SNDBUF, txBufSize); if (soKeepAlive != null) socket.setOption(StandardSocketOptions.SO_KEEPALIVE, soKeepAlive); if (soReuseAddress != null) socket.setOption(StandardSocketOptions.SO_REUSEADDR, soReuseAddress); if (soLingerOn != null && soLingerOn.booleanValue() && soLingerTime != null) socket.setOption(StandardSocketOptions.SO_LINGER, soLingerTime); if (tcpNoDelay != null) socket.setOption(StandardSocketOptions.TCP_NODELAY, tcpNoDelay); }
/** * Sets the value of a socket option. * * @param name * The socket option name * @param value * The value of the socket option. A value of {@code null} may be * a valid value for some socket options. * * @return This channel * * @throws UnsupportedOperationException * If the socket option is not supported by this channel * @throws IllegalArgumentException * If the value is not a valid value for this socket option * @throws ClosedChannelException * If this channel is closed * @throws IOException * If an I/O error occurs * * @see java.net.StandardSocketOptions */ public <T> NioChannel setOption(SocketOption<T> name, T value) throws IOException { this.channel.setOption(name, value); return this; }
@Override public void setup() { try { socket.setOption(StandardSocketOptions.TCP_NODELAY, false); } catch (IOException e) { throw new SocketInfoException(e); } }
@Override public void registerChannel(Channel channel, int sessionId) { try { AsynchronousSocketChannel socketChannel = (AsynchronousSocketChannel) channel; socketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true); socketChannel.setOption(StandardSocketOptions.SO_KEEPALIVE, true); socketChannel.setOption(StandardSocketOptions.TCP_NODELAY, false); AsynchronousTcpSession session = new AsynchronousTcpSession(sessionId, config, sessionMetric, netEvent, socketChannel); netEvent.notifySessionOpened(session); session._read(); } catch (IOException e) { log.error("socketChannel register error", e); } }
@Override public void registerChannel(Channel channel, int sessionId) { try { AsynchronousSocketChannel socketChannel = (AsynchronousSocketChannel) channel; socketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true); socketChannel.setOption(StandardSocketOptions.SO_KEEPALIVE, true); socketChannel.setOption(StandardSocketOptions.TCP_NODELAY, false); AsynchronousTcpSession session = new AsynchronousTcpSession(sessionId, config, sessionMetric, netEvent, socketChannel); netEvent.notifySessionOpened(session); session._read(); } catch (IOException e) { log.error("socketChannel register error", e); } }
@Override public void registerChannel(Channel channel, int sessionId) { try { AsynchronousSocketChannel socketChannel = (AsynchronousSocketChannel) channel; socketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true); socketChannel.setOption(StandardSocketOptions.SO_KEEPALIVE, true); socketChannel.setOption(StandardSocketOptions.TCP_NODELAY, false); AsynchronousTcpSession session = new AsynchronousTcpSession(sessionId, config, eventManager, socketChannel); eventManager.executeOpenTask(session); session._read(); } catch (IOException e) { log.error("socketChannel register error", e); } }
@Override public void connect(String host, int port, int id) { init(); try { final AsynchronousSocketChannel socketChannel = AsynchronousSocketChannel.open(group); socketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true); socketChannel.setOption(StandardSocketOptions.SO_KEEPALIVE, true); socketChannel.setOption(StandardSocketOptions.TCP_NODELAY, false); socketChannel.connect(new InetSocketAddress(host, port), id, new CompletionHandler<Void, Integer>(){ @Override public void completed(Void result, Integer sessionId) { worker.registerChannel(socketChannel, sessionId); } @Override public void failed(Throwable t, Integer sessionId) { log.error("session {} connect error", t, sessionId); }}); } catch (IOException e) { log.error("client connect error", e); } }
void ready() { try { channel = AsynchronousSocketChannel.open(); channel.setOption(StandardSocketOptions.SO_KEEPALIVE, true); Future<Void> future = channel.connect(remote.getAddress()); if (future.get() != null) { log.log(Level.WARNING, "Can't connect to {0}", remote); } else { log.log(Level.INFO, "Connected to server {0}", remote); } } catch (Exception e) { e.printStackTrace(); } }
private void connect() throws BindException{ String errorMessage = ""; if (socketChannel == null || !socketChannel.isOpen() || closed) { try { socketChannel = AsynchronousSocketChannel.open(); socketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true); socketChannel.setOption(StandardSocketOptions.SO_KEEPALIVE, true); socketChannel.connect(new InetSocketAddress(ip, port)).get(timeout, TimeUnit.MILLISECONDS); closed = false; //when connect to the server, keep receiving data either server response or server call receive(); } catch (Exception e) { log.error("Connection error: " + e.getMessage()); errorMessage = e.getMessage(); } } if (socketChannel == null) { throw new BindException(errorMessage); } }
Timer.Context context = timer.time(); final AsynchronousSocketChannel socketChannel = AsynchronousSocketChannel.open(group); socketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true); socketChannel.setOption(StandardSocketOptions.SO_KEEPALIVE, true); socketChannel.setOption(StandardSocketOptions.TCP_NODELAY, false); socketChannel.connect(new InetSocketAddress(host, port), id, new CompletionHandler<Void, Integer>() {
Timer.Context context = timer.time(); final AsynchronousSocketChannel socketChannel = AsynchronousSocketChannel.open(group); socketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true); socketChannel.setOption(StandardSocketOptions.SO_KEEPALIVE, true); socketChannel.setOption(StandardSocketOptions.TCP_NODELAY, false); socketChannel.connect(new InetSocketAddress(host, port), id, new CompletionHandler<Void, Integer>() {
@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); } } }
/** * 启动客户端。 * <p> * 在与服务端建立连接期间,该方法处于阻塞状态。直至连接建立成功,或者发生异常。 * </p> * <p> * 该start方法支持外部指定AsynchronousChannelGroup,实现多个客户端共享一组线程池资源,有效提升资源利用率。 * </p> * * @param asynchronousChannelGroup IO事件处理线程组 * @see AsynchronousSocketChannel#connect(SocketAddress) */ public AioSession<T> start(AsynchronousChannelGroup asynchronousChannelGroup) throws IOException, ExecutionException, InterruptedException { AsynchronousSocketChannel socketChannel = AsynchronousSocketChannel.open(asynchronousChannelGroup); //set socket options if (config.getSocketOptions() != null) { for (Map.Entry<SocketOption<Object>, Object> entry : config.getSocketOptions().entrySet()) { socketChannel.setOption(entry.getKey(), entry.getValue()); } } //bind host socketChannel.connect(new InetSocketAddress(config.getHost(), config.getPort())).get(); //连接成功则构造AIOSession对象 session = new AioSession<T>(socketChannel, config, new ReadCompletionHandler<T>(), new WriteCompletionHandler<T>(), false); session.initSession(); return session; }