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.util.internal.logging.InternalLogger;
25  import io.netty.util.internal.logging.InternalLoggerFactory;
26  
27  import java.io.File;
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.
49                      File socketFile = new File(local.path());
50                      boolean success = socketFile.delete();
51                      if (!success && logger.isDebugEnabled()) {
52                          logger.debug("Failed to delete a domain socket file: {}", local.path());
53                      }
54                  }
55              }
56          });
57      }
58  
59      @Override
60      Channel newChildChannel(int fd, ByteBuffer acceptedAddressMemory) throws Exception {
61          return new IoUringDomainSocketChannel(this, new LinuxSocket(fd));
62      }
63  
64      @Override
65      public ChannelConfig config() {
66          return config;
67      }
68  
69      @Override
70      public DomainSocketAddress localAddress() {
71          return local;
72      }
73  
74      @Override
75      public DomainSocketAddress remoteAddress() {
76          return (DomainSocketAddress) super.remoteAddress();
77      }
78  
79      @Override
80      protected void doBind(SocketAddress localAddress) throws Exception {
81          socket.bind(localAddress);
82          socket.listen(config.getBacklog());
83          local = (DomainSocketAddress) localAddress;
84          active = true;
85      }
86  
87  }