@Override protected void initChannel(SocketChannel c) { MessageHandler handler = new MessageHandler(connectionManager); CodecsHandler codecs = new CodecsHandler(ProtocolType.HANDSHAKE.getProtocol()); FramingHandler framing = new FramingHandler(); try { c.config().setOption(ChannelOption.IP_TOS, 0x18); } catch (ChannelException e) { // Not supported on all OSs, like Windows XP and lesser GlowServer.logger.warning("Your OS does not support type of service."); } c.pipeline() .addLast("idle_timeout", new IdleStateHandler(READ_IDLE_TIMEOUT, WRITE_IDLE_TIMEOUT, 0)) .addLast("legacy_ping", new LegacyPingHandler(connectionManager)) .addLast("encryption", NoopHandler.INSTANCE) .addLast("framing", framing) .addLast("compression", NoopHandler.INSTANCE) .addLast("codecs", codecs) .addLast("handler", handler); } }
@Override public void initChannel(NioSocketChannel ch) throws Exception { try { if (!peerDiscoveryMode) { logger.debug("Open {} connection, channel: {}", isInbound() ? "inbound" : "outbound", ch.toString()); } if (notEligibleForIncomingConnection(ch)) { ch.disconnect(); return; } final Channel channel = ctx.getBean(Channel.class); channel.setInetSocketAddress(ch.remoteAddress()); channel.init(ch.pipeline(), remoteId, peerDiscoveryMode, channelManager); if(!peerDiscoveryMode) { channelManager.add(channel); } // limit the size of receiving buffer to 1024 ch.config().setRecvByteBufAllocator(new FixedRecvByteBufAllocator(256 * 1024)); ch.config().setOption(ChannelOption.SO_RCVBUF, 256 * 1024); ch.config().setOption(ChannelOption.SO_BACKLOG, 1024); // be aware of channel closing ch.closeFuture().addListener((ChannelFutureListener) future -> { if (!peerDiscoveryMode) { channelManager.notifyDisconnect(channel); } }); } catch (Exception e) { logger.error("Unexpected error: ", e); } }
@Override public void setSoLinger(boolean on, int linger) throws SocketException { ch.config().setOption(ChannelOption.SO_LINGER, linger); }
@Override public void setReuseAddress(boolean on) throws SocketException { ch.config().setOption(ChannelOption.SO_REUSEADDR, on); }
@Override public void setKeepAlive(boolean on) throws SocketException { ch.config().setOption(ChannelOption.SO_KEEPALIVE, on); }
@Override public synchronized void setReceiveBufferSize(int size) throws SocketException { ch.config().setOption(ChannelOption.SO_RCVBUF, size); }
@Override public synchronized void setSendBufferSize(int size) throws SocketException { ch.config().setOption(ChannelOption.SO_SNDBUF, size); }
@Override public void setTcpNoDelay(boolean on) throws SocketException { ch.config().setOption(ChannelOption.TCP_NODELAY, on); }
@Override public void setTrafficClass(int tc) throws SocketException { ch.config().setOption(ChannelOption.IP_TOS, tc); }
@Override public void initChannel( SocketChannel ch ) throws Exception { try { ch.config().setOption( ChannelOption.IP_TOS, 0x18 ); } catch ( ChannelException e ) { // Ignore } ch.config().setAllocator( PooledByteBufAllocator.DEFAULT ); ch.config().setWriteBufferWaterMark( MARK ); Pipeline.prepare( ch.pipeline(), connectionHandler ); } }
@Override public void initChannel( SocketChannel ch ) throws Exception { final ConnectionHandler connectionHandler = new ConnectionHandler(); try { ch.config().setOption( ChannelOption.IP_TOS, 0x18 ); } catch ( ChannelException ex ) { // Ignored } ch.config().setAllocator( PooledByteBufAllocator.DEFAULT ); ch.config().setWriteBufferWaterMark( MARK ); Pipeline.prepare( ch.pipeline(), connectionHandler ); connectionHandler.whenConnected( aVoid -> newConnection.accept( connectionHandler ) ); } }