@Override protected void serverShutdown() throws RemotingException { server.shutdownGracefully(); }
@Override protected void clientShutdown() { client.shutdownGracefully(); }
public NioServer(NioServerConfig serverConfig, NioHandler eventHandler, ChannelInitializer channelInitializer) { this.serverConfig = serverConfig; setEventHandler(eventHandler); this.processor = new NioServerProcessor(serverConfig, this.eventHandler, channelInitializer); }
@Override protected void serverStart() throws RemotingException { NioServerConfig serverConfig = new NioServerConfig(); serverConfig.setBacklog(65536); serverConfig.setReuseAddress(true); serverConfig.setTcpNoDelay(true); serverConfig.setIdleTimeBoth(remotingServerConfig.getServerChannelMaxIdleTimeSeconds()); serverConfig.setIdleTimeWrite(remotingServerConfig.getWriterIdleTimeSeconds()); serverConfig.setIdleTimeRead(remotingServerConfig.getReaderIdleTimeSeconds()); final LtsCodecFactory codecFactory = new LtsCodecFactory(getCodec()); server = new NioServer(serverConfig, new LtsEventHandler(this, "SERVER"), new ChannelInitializer() { @Override protected Decoder getDecoder() { return codecFactory.getDecoder(); } @Override protected Encoder getEncoder() { return codecFactory.getEncoder(); } }); server.bind(new InetSocketAddress(this.remotingServerConfig.getListenPort())); }
@Override protected void clientStart() throws RemotingException { NioClientConfig clientConfig = new NioClientConfig(); clientConfig.setTcpNoDelay(true); clientConfig.setIdleTimeBoth(remotingClientConfig.getClientChannelMaxIdleTimeSeconds()); clientConfig.setIdleTimeRead(remotingClientConfig.getReaderIdleTimeSeconds()); clientConfig.setIdleTimeWrite(remotingClientConfig.getWriterIdleTimeSeconds()); final LtsCodecFactory codecFactory = new LtsCodecFactory(getCodec()); this.client = new NioClient(clientConfig, new LtsEventHandler(this, "CLIENT"), new ChannelInitializer() { @Override protected Decoder getDecoder() { return codecFactory.getDecoder(); } @Override protected Encoder getEncoder() { return codecFactory.getEncoder(); } }); }
public void bind(InetSocketAddress localAddress) { // 初始化 init(); processor.start(); // 注册 try { processor.register(); } catch (ClosedChannelException e) { throw new NioException("register channel error:" + e.getMessage(), e); } // 绑定 try { processor.bind(localAddress, serverConfig); } catch (IOException e) { throw new NioException("bind channel error:" + e.getMessage(), e); } }
private void init() { ServerSocketChannel socketChannel = processor.javaChannel(); ServerSocket javaSocket = socketChannel.socket(); try { if (serverConfig.getReceiveBufferSize() != null) { javaSocket.setReceiveBufferSize(serverConfig.getReceiveBufferSize()); } if (serverConfig.getReuseAddress() != null) { javaSocket.setReuseAddress(serverConfig.getReuseAddress()); } } catch (SocketException e) { throw new NioException("config channel error:" + e.getMessage(), e); } }
private Selector openSelector() { Selector result = null; // 在linux平台,尽量启用epoll实现 if (isLinuxPlatform()) { try { final Class<?> providerClazz = Class.forName("sun.nio.ch.EPollSelectorProvider"); if (providerClazz != null) { final Method method = providerClazz.getMethod("provider"); if (method != null) { final SelectorProvider selectorProvider = (SelectorProvider) method.invoke(null); if (selectorProvider != null) { result = selectorProvider.openSelector(); } } } } catch (final Exception ignored) { } } if (result == null) { try { result = SelectorProvider.provider().openSelector(); } catch (IOException e) { throw new NioException("open selector error:" + e.getMessage(), e); } } return result; }
private static SocketChannel newSocket() { try { return SocketChannel.open(); } catch (IOException e) { throw new NioException("can't create a new socket, out of file descriptors ?", e); } }
@Override protected ChannelFuture connect(SocketAddress socketAddress) { Futures.ConnectFuture connectFuture = client.connect(socketAddress); return new LtsChannelFuture(connectFuture); }
@Override protected void clientStart() throws RemotingException { NioClientConfig clientConfig = new NioClientConfig(); clientConfig.setTcpNoDelay(true); clientConfig.setIdleTimeBoth(remotingClientConfig.getClientChannelMaxIdleTimeSeconds()); clientConfig.setIdleTimeRead(remotingClientConfig.getReaderIdleTimeSeconds()); clientConfig.setIdleTimeWrite(remotingClientConfig.getWriterIdleTimeSeconds()); final LtsCodecFactory codecFactory = new LtsCodecFactory(getCodec()); this.client = new NioClient(clientConfig, new LtsEventHandler(this, "CLIENT"), new ChannelInitializer() { @Override protected Decoder getDecoder() { return codecFactory.getDecoder(); } @Override protected Encoder getEncoder() { return codecFactory.getEncoder(); } }); }
public void bind(InetSocketAddress localAddress) { // 初始化 init(); processor.start(); // 注册 try { processor.register(); } catch (ClosedChannelException e) { throw new NioException("register channel error:" + e.getMessage(), e); } // 绑定 try { processor.bind(localAddress, serverConfig); } catch (IOException e) { throw new NioException("bind channel error:" + e.getMessage(), e); } }
private void init() { ServerSocketChannel socketChannel = processor.javaChannel(); ServerSocket javaSocket = socketChannel.socket(); try { if (serverConfig.getReceiveBufferSize() != null) { javaSocket.setReceiveBufferSize(serverConfig.getReceiveBufferSize()); } if (serverConfig.getReuseAddress() != null) { javaSocket.setReuseAddress(serverConfig.getReuseAddress()); } } catch (SocketException e) { throw new NioException("config channel error:" + e.getMessage(), e); } }
public NioServer(NioServerConfig serverConfig, NioHandler eventHandler, ChannelInitializer channelInitializer) { this.serverConfig = serverConfig; setEventHandler(eventHandler); this.processor = new NioServerProcessor(serverConfig, this.eventHandler, channelInitializer); }
private static SocketChannel newSocket() { try { return SocketChannel.open(); } catch (IOException e) { throw new NioException("can't create a new socket, out of file descriptors ?", e); } }
@Override protected void serverShutdown() throws RemotingException { server.shutdownGracefully(); }
@Override protected void clientShutdown() { client.shutdownGracefully(); }
@Override protected ChannelFuture connect(SocketAddress socketAddress) { Futures.ConnectFuture connectFuture = client.connect(socketAddress); return new LtsChannelFuture(connectFuture); }
private static ServerSocketChannel newSocket() { try { ServerSocketChannel ch = ServerSocketChannel.open(); ch.configureBlocking(false); return ch; } catch (IOException e) { throw new NioException("Open a server socket error:" + e.getMessage(), e); } }
private static ServerSocketChannel newSocket() { try { ServerSocketChannel ch = ServerSocketChannel.open(); ch.configureBlocking(false); return ch; } catch (IOException e) { throw new NioException("Open a server socket error:" + e.getMessage(), e); } }