public void start(ServerTransportListener listener) {
    Preconditions.checkState(this.listener == null, "Handler already registered");
    this.listener = listener;

    // Create the Netty handler for the pipeline.
    final NettyServerHandler grpcHandler = createHandler(listener);

    // Notify when the channel closes.
    channel
        .closeFuture()
        .addListener(
            new ChannelFutureListener() {
              @Override
              public void operationComplete(ChannelFuture future) throws Exception {
                notifyTerminated(grpcHandler.connectionError());
              }
            });

    ChannelHandler handler = grpcHandler;
    if (sslContext != null) {
      SSLEngine sslEngine = sslContext.newEngine(channel.alloc());
      handler = ProtocolNegotiators.serverTls(sslEngine, grpcHandler);
    }
    channel.pipeline().addLast(handler);
  }
  /**
   * 쓰레드 덤프를 요청하는 전문을 보낸다<br>
   * <br>
   *
   * @param threadDumpData
   * @param agentId
   * @param channel
   * @author Kim Ji Hye
   * @since 2015. 11. 26.
   */
  public ThreadDumpData sendThreadDumpRequest(ThreadDumpData td) {

    // Assembler를 사용하여 socket에 write한다.
    byte[] sendData = Assembler.getSendData(td, true);
    Channel channel = AgentClientSocketHandler.getClient(td.getAgentId());
    if (channel != null) {
      ByteBufAllocator alloc = channel.alloc();
      ByteBuf buf = alloc.buffer(sendData.length);
      buf.writeBytes(sendData);
      channel.writeAndFlush(buf);
    }
    return td;
  }
Esempio n. 3
0
  public void sendMsg(Channel channel) {
    if (!channel.isOpen()) return;

    for (int i = 0; i < size(); i++) {
      int msgId = idBufList.get(bufCurIndex);
      Chunk msgbuf = chunkBufList.get(bufCurIndex);
      removeMsg();

      // 构造头文件数据
      ByteBuf head = channel.alloc().buffer(8);
      head.writeInt(msgbuf.length + 8);
      head.writeInt(msgId);

      // 写入头数据
      channel.write(head);
      // Chunk类型的msgbuf肯定是protobuf直接生成的 所以buffer属性中不会有多余数据 才能这么用
      // 其余地方Chunk类不建议直接使用内部的buffer
      channel.write(msgbuf.buffer);
    }
    channel.flush();
  }
Esempio n. 4
0
 @Override
 protected void write(ByteBuffer data, Deferred<Void, Promise<Void>> onComplete, boolean flush) {
   ByteBuf buf = ioChannel.alloc().buffer(data.remaining());
   buf.writeBytes(data);
   write(buf, onComplete, flush);
 }
Esempio n. 5
0
    @Override
    protected void initChannel(Channel channel) throws Exception {
      SslContext sslContext;

      SSLParameters sslParams = new SSLParameters();

      if (redisURI.isVerifyPeer()) {
        sslContext = SslContext.newClientContext(SslProvider.JDK);
        if (JavaRuntime.AT_LEAST_JDK_7) {
          sslParams.setEndpointIdentificationAlgorithm("HTTPS");
        }
      } else {
        sslContext =
            SslContext.newClientContext(SslProvider.JDK, InsecureTrustManagerFactory.INSTANCE);
      }

      SSLEngine sslEngine =
          sslContext.newEngine(channel.alloc(), redisURI.getHost(), redisURI.getPort());
      sslEngine.setSSLParameters(sslParams);

      removeIfExists(channel.pipeline(), SslHandler.class);

      if (channel.pipeline().get("first") == null) {
        channel
            .pipeline()
            .addFirst(
                "first",
                new ChannelDuplexHandler() {

                  @Override
                  public void channelActive(ChannelHandlerContext ctx) throws Exception {
                    eventBus.publish(new ConnectedEvent(local(ctx), remote(ctx)));
                    super.channelActive(ctx);
                  }

                  @Override
                  public void channelInactive(ChannelHandlerContext ctx) throws Exception {
                    eventBus.publish(new DisconnectedEvent(local(ctx), remote(ctx)));
                    super.channelInactive(ctx);
                  }
                });
      }

      SslHandler sslHandler = new SslHandler(sslEngine, redisURI.isStartTls());
      channel.pipeline().addLast(sslHandler);
      if (channel.pipeline().get("channelActivator") == null) {
        channel
            .pipeline()
            .addLast(
                "channelActivator",
                new RedisChannelInitializerImpl() {

                  private Command<?, ?, ?> pingCommand;

                  @Override
                  public Future<Boolean> channelInitialized() {
                    return initializedFuture;
                  }

                  @Override
                  public void channelInactive(ChannelHandlerContext ctx) throws Exception {
                    initializedFuture = SettableFuture.create();
                    pingCommand = null;
                    super.channelInactive(ctx);
                  }

                  @Override
                  public void channelActive(ChannelHandlerContext ctx) throws Exception {
                    if (initializedFuture.isDone()) {
                      super.channelActive(ctx);
                    }
                  }

                  @Override
                  public void userEventTriggered(ChannelHandlerContext ctx, Object evt)
                      throws Exception {
                    if (evt instanceof SslHandshakeCompletionEvent && !initializedFuture.isDone()) {

                      SslHandshakeCompletionEvent event = (SslHandshakeCompletionEvent) evt;
                      if (event.isSuccess()) {
                        if (pingBeforeActivate) {
                          pingCommand = INITIALIZING_CMD_BUILDER.ping();
                          pingBeforeActivate(pingCommand, initializedFuture, ctx, handlers);
                        } else {
                          ctx.fireChannelActive();
                        }
                      } else {
                        initializedFuture.setException(event.cause());
                      }
                    }

                    if (evt instanceof ConnectionEvents.Close) {
                      if (ctx.channel().isOpen()) {
                        ctx.channel().close();
                      }
                    }

                    if (evt instanceof ConnectionEvents.Activated) {
                      if (!initializedFuture.isDone()) {
                        initializedFuture.set(true);
                        eventBus.publish(new ConnectionActivatedEvent(local(ctx), remote(ctx)));
                      }
                    }

                    super.userEventTriggered(ctx, evt);
                  }

                  @Override
                  public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
                      throws Exception {

                    if (!initializedFuture.isDone()) {
                      initializedFuture.setException(cause);
                    }
                    super.exceptionCaught(ctx, cause);
                  }
                });
      }

      for (ChannelHandler handler : handlers) {
        removeIfExists(channel.pipeline(), handler.getClass());
        channel.pipeline().addLast(handler);
      }
    }
 @NotNull
 public final ByteBufAllocator getByteBufAllocator() {
   return channel.alloc();
 }