io.netty.channel.socket.SocketChannel类的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(10.4k)|赞(0)|评价(0)|浏览(182)

本文整理了Java中io.netty.channel.socket.SocketChannel类的一些代码示例,展示了SocketChannel类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。SocketChannel类的具体详情如下:
包路径:io.netty.channel.socket.SocketChannel
类名称:SocketChannel

SocketChannel介绍

[英]A TCP/IP socket Channel.
[中]TCP/IP套接字通道。

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Override
  protected void initChannel(SocketChannel channel) throws Exception {
    configureChannel(channel.config());
    ChannelPipeline pipeline = channel.pipeline();
    if (isSecure) {
      Assert.notNull(sslContext, "sslContext should not be null");
      pipeline.addLast(sslContext.newHandler(channel.alloc(), uri.getHost(), uri.getPort()));
    }
    pipeline.addLast(new HttpClientCodec());
    pipeline.addLast(new HttpObjectAggregator(maxResponseSize));
    if (readTimeout > 0) {
      pipeline.addLast(new ReadTimeoutHandler(readTimeout,
          TimeUnit.MILLISECONDS));
    }
  }
});

代码示例来源:origin: kaaproject/kaa

@Override
protected void initChannel(SocketChannel ch) throws Exception {
 final ChannelPipeline p = ch.pipeline();
 final UUID uuid = UUID.randomUUID();
 LOG.debug("KaaTcpServerInitializer Initializing Channel {} connection from {}:{}",
     uuid, ch.remoteAddress().getAddress().toString(), ch.remoteAddress().getPort());
 Attribute<UUID> uuidAttr = ch.attr(AbstractNettyServer.UUID_KEY);
 uuidAttr.set(uuid);
 p.addLast("binaryDecoder", new ByteArrayDecoder());
 p.addLast("kaaTcpDecoder", getDecoder());
 p.addLast("binaryEncoder", new ByteArrayEncoder());
 p.addLast("kaaTcpEncoder", new KaaTcpEncoder());
 p.addLast("mainHandler", getMainHandler(uuid));
 p.addLast("kaaTcpExceptionHandler", new KaaTcpExceptionHandler());
}

代码示例来源:origin: apache/drill

@Override
protected CC initRemoteConnection(SocketChannel channel){
 local=channel.localAddress();
 remote=channel.remoteAddress();
 return null;
}

代码示例来源:origin: andsel/moquette

private ChannelHandler createSslHandler(SocketChannel channel, SslContext sslContext, boolean needsClientAuth) {
    SSLEngine sslEngine = sslContext.newEngine(
        channel.alloc(),
        channel.remoteAddress().getHostString(),
        channel.remoteAddress().getPort());
    sslEngine.setUseClientMode(false);
    if (needsClientAuth) {
      sslEngine.setNeedClientAuth(true);
    }
    return new SslHandler(sslEngine);
  }
}

代码示例来源:origin: alibaba/fescar

@Override
  public void initChannel(SocketChannel ch) {
    ch.pipeline().addLast(new IdleStateHandler(nettyServerConfig.getChannelMaxReadIdleSeconds(), 0, 0))
      .addLast(new MessageCodecHandler());
    if (null != channelHandlers) {
      addChannelPipelineLast(ch, channelHandlers);
    }
  }
});

代码示例来源:origin: dreamhead/moco

@Override
  protected void initChannel(final SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    if (serverSetting.isSecure()) {
      pipeline.addFirst("ssl", sslHandler().get());
    }
    pipeline.addLast("codec", new HttpServerCodec(MAX_INITIAL_LINE_LENGTH, MAX_HEADER_SIZE,
        MAX_CHUNK_SIZE, false));
    pipeline.addLast("aggregator", new HttpObjectAggregator(MAX_CONTENT_LENGTH));
    pipeline.addLast("handler", new MocoHandler(serverSetting));
  }
};

代码示例来源:origin: weibocom/motan

@Override
  public void initChannel(SocketChannel ch) throws Exception {
    ch.pipeline().addLast("http-decoder", new HttpRequestDecoder());
    ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(maxContentLength));
    ch.pipeline().addLast("http-encoder", new HttpResponseEncoder());
    ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
    ch.pipeline().addLast("serverHandler", handler);
  }
}).option(ChannelOption.SO_BACKLOG, 1024).childOption(ChannelOption.SO_KEEPALIVE, false);

代码示例来源:origin: apache/zookeeper

@Override
  protected void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    if (secure) {
      initSSL(pipeline);
    }
    pipeline.addLast("servercnxnfactory", channelHandler);
  }
});

代码示例来源:origin: rakam-io/rakam

@Override
  protected void initChannel(SocketChannel ch) throws Exception {
    ch.pipeline().addLast(new HttpClientCodec())
        .addLast(new HttpContentDecompressor())
        .addLast(new HttpObjectAggregator(10048576))
        .addLast(new ProxyChannelInboundHandler());
  }
});

代码示例来源:origin: testcontainers/testcontainers-java

@Override
  protected void initChannel(final SocketChannel channel) throws Exception {
    channel.pipeline().addLast(new HttpClientCodec());
    channel.pipeline().addLast(new HttpContentDecompressor());
  }
});

代码示例来源:origin: jersey/jersey

final ChannelPipeline p = ch.pipeline();
final HttpServerCodec sourceCodec = new HttpServerCodec();
p.addLast(sourceCodec);
p.addLast(new HttpServerUpgradeHandler(sourceCodec, new HttpServerUpgradeHandler.UpgradeCodecFactory() {
  @Override
  public HttpServerUpgradeHandler.UpgradeCodec newUpgradeCodec(CharSequence protocol) {
p.addLast(new SimpleChannelInboundHandler<HttpMessage>() {
  @Override
  protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws Exception {

代码示例来源:origin: apache/rocketmq

@Override
  public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    if (nettyClientConfig.isUseTLS()) {
      if (null != sslContext) {
        pipeline.addFirst(defaultEventExecutorGroup, "sslHandler", sslContext.newHandler(ch.alloc()));
        log.info("Prepend SSL handler");
      } else {
        log.warn("Connections are insecure as SSLContext is null!");
      }
    }
    pipeline.addLast(
      defaultEventExecutorGroup,
      new NettyEncoder(),
      new NettyDecoder(),
      new IdleStateHandler(0, 0, nettyClientConfig.getClientChannelMaxIdleTimeSeconds()),
      new NettyConnectManageHandler(),
      new NettyClientHandler());
  }
});

代码示例来源:origin: apache/geode

@Override
 public void initChannel(SocketChannel ch) throws Exception {
  if (logger.fineEnabled())
   logger.fine("GeodeRedisServer-Connection established with " + ch.remoteAddress());
  ChannelPipeline p = ch.pipeline();
  p.addLast(ByteToCommandDecoder.class.getSimpleName(), new ByteToCommandDecoder());
  p.addLast(ExecutionHandlerContext.class.getSimpleName(),
    new ExecutionHandlerContext(ch, cache, regionCache, GeodeRedisServer.this, pwdB));
 }
}).option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.SO_RCVBUF, getBufferSize())

代码示例来源:origin: jersey/jersey

@Override
  protected void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline p = ch.pipeline();
    // Enable HTTPS if necessary.
    if ("https".equals(requestUri.getScheme())) {
      // making client authentication optional for now; it could be extracted to configurable property
      JdkSslContext jdkSslContext = new JdkSslContext(client.getSslContext(), true, ClientAuth.NONE);
      p.addLast(jdkSslContext.newHandler(ch.alloc()));
    }
    // http proxy
    Configuration config = jerseyRequest.getConfiguration();
    final Object proxyUri = config.getProperties().get(ClientProperties.PROXY_URI);
    if (proxyUri != null) {
      final URI u = getProxyUri(proxyUri);
      final String userName = ClientProperties.getValue(
          config.getProperties(), ClientProperties.PROXY_USERNAME, String.class);
      final String password = ClientProperties.getValue(
          config.getProperties(), ClientProperties.PROXY_PASSWORD, String.class);
      p.addLast(new HttpProxyHandler(new InetSocketAddress(u.getHost(),
                                u.getPort() == -1 ? 8080 : u.getPort()),
                     userName, password));
    }
    p.addLast(new HttpClientCodec());
    p.addLast(new ChunkedWriteHandler());
    p.addLast(new HttpContentDecompressor());
    p.addLast(new JerseyClientHandler(NettyConnector.this, jerseyRequest, jerseyCallback, settableFuture));
  }
});

代码示例来源:origin: GlowstoneMC/Glowstone

@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);
  }
}

代码示例来源:origin: reactor/reactor-netty

/**
 * Retrieve the connection information from the current connection directly
 * @param c the current channel
 * @return the connection information
 */
static ConnectionInfo newConnectionInfo(Channel c) {
  SocketChannel channel = (SocketChannel) c; 
  InetSocketAddress hostAddress = channel.localAddress();
  InetSocketAddress remoteAddress = channel.remoteAddress();
  String scheme = channel.pipeline().get(SslHandler.class) != null ? "https" : "http";
  return new ConnectionInfo(hostAddress, remoteAddress, scheme);
}

代码示例来源:origin: alipay/sofa-rpc

@Override
  protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws Exception {
    // If this handler is hit then no upgrade has been attempted and the client is just talking HTTP.
    if (LOGGER.isWarnEnabled()) {
      LOGGER.warn("Directly talking: {} (no upgrade was attempted) from {}", msg.protocolVersion(),
        NetUtil.toSocketAddressString(ch.remoteAddress()));
    }
    ChannelPipeline pipeline = ctx.pipeline();
    ChannelHandlerContext thisCtx = pipeline.context(this);
    // 不需要了
    pipeline.addAfter(bizGroup, thisCtx.name(), "Http1ChannelHandler",
      new Http1ServerChannelHandler(serverHandler));
    pipeline.replace(this, "HttpObjectAggregator",
      new HttpObjectAggregator(maxHttpContentLength));
    // HttpServerUpgradeHandler -> HttpServerCodec ->  HttpObjectAggregator -> Http1ChannelHandler, 
    ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
  }
});

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

@Override
 public int compare(SocketChannel t0, SocketChannel t1) {
  // Should not be null as these are filtered previously in matchingChannels.
  assert t0 != null && t0.remoteAddress() != null;
  assert t1 != null && t1.remoteAddress() != null;
  return t0.remoteAddress().toString().compareTo(t1.remoteAddress().toString());
 }
};

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

@Override
 public boolean apply(SocketChannel input) {
  return input.isOpen()
    && input.remoteAddress() != null
    && addresses.contains(input.remoteAddress());
 }
}));

代码示例来源:origin: alipay/sofa-bolt

@Override
protected void initChannel(SocketChannel channel) {
  ChannelPipeline pipeline = channel.pipeline();
  pipeline.addLast("decoder", codec.newDecoder());
  pipeline.addLast("encoder", codec.newEncoder());
  if (idleSwitch) {
    pipeline.addLast("idleStateHandler", new IdleStateHandler(0, 0, idleTime,
      TimeUnit.MILLISECONDS));
    pipeline.addLast("serverIdleHandler", serverIdleHandler);
  }
  pipeline.addLast("connectionEventHandler", connectionEventHandler);
  pipeline.addLast("handler", rpcHandler);
  createConnection(channel);
}

相关文章