View Javadoc
1   /*
2    * Copyright 2025 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.channel.uring;
17  
18  import io.netty.channel.Channel;
19  import io.netty.channel.ChannelConfig;
20  import io.netty.channel.ChannelFuture;
21  import io.netty.channel.ChannelFutureListener;
22  import io.netty.channel.unix.DomainSocketAddress;
23  import io.netty.channel.unix.ServerDomainSocketChannel;
24  import io.netty.channel.unix.UnixChannelUtil;
25  import io.netty.util.internal.logging.InternalLogger;
26  import io.netty.util.internal.logging.InternalLoggerFactory;
27  
28  import java.net.SocketAddress;
29  import java.nio.ByteBuffer;
30  
31  public final class IoUringServerDomainSocketChannel extends AbstractIoUringServerChannel
32          implements ServerDomainSocketChannel {
33  
34      private static final InternalLogger logger = InternalLoggerFactory.getInstance(
35              IoUringServerDomainSocketChannel.class);
36  
37      private final IoUringServerSocketChannelConfig config;
38  
39      private volatile DomainSocketAddress local;
40  
41      public IoUringServerDomainSocketChannel() {
42          super(LinuxSocket.newSocketDomain(), false);
43          this.config = new IoUringServerSocketChannelConfig(this);
44          this.closeFuture().addListener(new ChannelFutureListener() {
45              @Override
46              public void operationComplete(ChannelFuture future) throws Exception {
47                  if (local != null) {
48                      // Delete the socket file if possible (only if it is not a regular file, directory or symlink).
49                      boolean success = UnixChannelUtil.deleteDomainSocketFile(local.path());
50                      if (!success && logger.isDebugEnabled()) {
51                          logger.debug("Failed to delete a domain socket file: {}", local.path());
52                      }
53                  }
54              }
55          });
56      }
57  
58      @Override
59      Channel newChildChannel(int fd, ByteBuffer acceptedAddressMemory) throws Exception {
60          return new IoUringDomainSocketChannel(this, new LinuxSocket(fd));
61      }
62  
63      @Override
64      public ChannelConfig config() {
65          return config;
66      }
67  
68      @Override
69      public DomainSocketAddress localAddress() {
70          return local;
71      }
72  
73      @Override
74      public DomainSocketAddress remoteAddress() {
75          return (DomainSocketAddress) super.remoteAddress();
76      }
77  
78      @Override
79      protected void doBind(SocketAddress localAddress) throws Exception {
80          socket.bind(localAddress);
81          socket.listen(config.getBacklog());
82          local = (DomainSocketAddress) localAddress;
83          active = true;
84      }
85  
86  }