Home / Class/ LocalUnsafe Class — netty Architecture

LocalUnsafe Class — netty Architecture

Architecture documentation for the LocalUnsafe class in LocalChannel.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  84d2a0d7_132c_c9dd_926c_de7389277737["LocalUnsafe"]
  e832f9b0_66fa_273d_c41a_059a1e7dde03["LocalChannel.java"]
  84d2a0d7_132c_c9dd_926c_de7389277737 -->|defined in| e832f9b0_66fa_273d_c41a_059a1e7dde03
  19db0fc8_1524_a49b_2c8e_8b165b94bc06["close()"]
  84d2a0d7_132c_c9dd_926c_de7389277737 -->|method| 19db0fc8_1524_a49b_2c8e_8b165b94bc06
  5e4b8515_7c84_2505_d715_bcc0a157c907["handle()"]
  84d2a0d7_132c_c9dd_926c_de7389277737 -->|method| 5e4b8515_7c84_2505_d715_bcc0a157c907
  562d5c11_6ff6_9b17_d006_101ff1054c95["registered()"]
  84d2a0d7_132c_c9dd_926c_de7389277737 -->|method| 562d5c11_6ff6_9b17_d006_101ff1054c95
  f17369bf_bb1a_afdd_d027_3f73c8c9ff30["unregistered()"]
  84d2a0d7_132c_c9dd_926c_de7389277737 -->|method| f17369bf_bb1a_afdd_d027_3f73c8c9ff30
  04129b37_a824_b638_974d_221516db2b86["closeNow()"]
  84d2a0d7_132c_c9dd_926c_de7389277737 -->|method| 04129b37_a824_b638_974d_221516db2b86
  138997d1_ae4b_899e_69a4_f393e357ad1f["connect()"]
  84d2a0d7_132c_c9dd_926c_de7389277737 -->|method| 138997d1_ae4b_899e_69a4_f393e357ad1f

Relationship Graph

Source Code

transport/src/main/java/io/netty/channel/local/LocalChannel.java lines 479–585

    private class LocalUnsafe extends AbstractUnsafe implements LocalIoHandle {

        @Override
        public void close() {
            close(voidPromise());
        }

        @Override
        public void handle(IoRegistration registration, IoEvent event) {
            // NOOP
        }

        @Override
        public void registered() {
            // 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.
                // See https://github.com/netty/netty/issues/2144
                final LocalChannel peer = LocalChannel.this.peer;
                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() {
                        ChannelPromise promise = peer.connectPromise;

                        // Only trigger fireChannelActive() if the promise was not null and was not completed yet.
                        // connectPromise may be set to null if doClose() was called in the meantime.
                        if (promise != null && promise.trySuccess()) {
                            peer.pipeline().fireChannelActive();
                        }
                    }
                });
            }
            ((SingleThreadEventExecutor) eventLoop()).addShutdownHook(shutdownHook);
        }

        @Override
        public void unregistered() {
            // Just remove the shutdownHook as this Channel may be closed later or registered to another EventLoop
            ((SingleThreadEventExecutor) eventLoop()).removeShutdownHook(shutdownHook);
        }

        @Override
        public void closeNow() {
            close(voidPromise());
        }

        @Override
        public void connect(final SocketAddress remoteAddress,
                SocketAddress localAddress, final ChannelPromise promise) {
            if (!promise.setUncancellable() || !ensureOpen(promise)) {
                return;
            }

            if (state == State.CONNECTED) {
                Exception cause = new AlreadyConnectedException();
                safeSetFailure(promise, cause);
                return;
            }

            if (connectPromise != null) {
                throw new ConnectionPendingException();
            }

            connectPromise = promise;

            if (state != State.BOUND) {
                // Not bound yet and no localAddress specified - get one.
                if (localAddress == null) {

Frequently Asked Questions

What is the LocalUnsafe class?
LocalUnsafe is a class in the netty codebase, defined in transport/src/main/java/io/netty/channel/local/LocalChannel.java.
Where is LocalUnsafe defined?
LocalUnsafe is defined in transport/src/main/java/io/netty/channel/local/LocalChannel.java at line 479.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free