예제 #1
0
  @Override
  protected void doRegister() throws Exception {
    // Check if both peer and parent are non-null because this channel was created by a
    // LocalServerChannel.
    // This is needed as a peer may not be null also if a LocalChannel was connected before and
    // deregistered / registered later again.
    //
    // See https://github.com/netty/netty/issues/2400
    if (peer != null && parent() != null) {
      // Store the peer in a local variable as it may be set to null if doClose() is called.
      // Because of this we also set registerInProgress to true as we check for this in doClose()
      // and make sure
      // we delay the fireChannelInactive() to be fired after the fireChannelActive() and so keep
      // the correct
      // order of events.
      //
      // See https://github.com/netty/netty/issues/2144
      final LocalChannel peer = this.peer;
      registerInProgress = true;
      state = State.CONNECTED;

      peer.remoteAddress = parent() == null ? null : parent().localAddress();
      peer.state = State.CONNECTED;

      // Always call peer.eventLoop().execute() even if peer.eventLoop().inEventLoop() is true.
      // This ensures that if both channels are on the same event loop, the peer's channelActive
      // event is triggered *after* this channel's channelRegistered event, so that this channel's
      // pipeline is fully initialized by ChannelInitializer before any channelRead events.
      peer.eventLoop()
          .execute(
              new Runnable() {
                @Override
                public void run() {
                  registerInProgress = false;
                  peer.pipeline().fireChannelActive();
                  peer.connectPromise.setSuccess();
                }
              });
    }
    ((SingleThreadEventExecutor) eventLoop()).addShutdownHook(shutdownHook);
  }
예제 #2
0
  @Override
  protected Runnable doRegister() throws Exception {
    final LocalChannel peer = this.peer;
    Runnable postRegisterTask;

    if (peer != null) {
      state = 2;

      peer.remoteAddress = parent().localAddress();
      peer.state = 2;

      // Ensure the peer's channelActive event is triggered *after* this channel's
      // channelRegistered event is triggered, so that this channel's pipeline is fully
      // initialized by ChannelInitializer.
      final EventLoop peerEventLoop = peer.eventLoop();
      postRegisterTask =
          new Runnable() {
            @Override
            public void run() {
              peerEventLoop.execute(
                  new Runnable() {
                    @Override
                    public void run() {
                      peer.connectFuture.setSuccess();
                      peer.pipeline().fireChannelActive();
                    }
                  });
            }
          };
    } else {
      postRegisterTask = null;
    }

    ((SingleThreadEventLoop) eventLoop()).addShutdownHook(shutdownHook);

    return postRegisterTask;
  }