View Javadoc
1   /*
2    * Copyright 2016 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.kqueue;
17  
18  import io.netty.channel.Channel;
19  import io.netty.channel.unix.DomainSocketAddress;
20  import io.netty.channel.unix.ServerDomainSocketChannel;
21  import io.netty.util.internal.logging.InternalLogger;
22  import io.netty.util.internal.logging.InternalLoggerFactory;
23  
24  import java.io.File;
25  import java.net.SocketAddress;
26  
27  import static io.netty.channel.kqueue.BsdSocket.newSocketDomain;
28  
29  public final class KQueueServerDomainSocketChannel extends AbstractKQueueServerChannel
30                                                    implements ServerDomainSocketChannel {
31      private static final InternalLogger logger = InternalLoggerFactory.getInstance(
32              KQueueServerDomainSocketChannel.class);
33  
34      private final KQueueServerChannelConfig config = new KQueueServerChannelConfig(this);
35      private volatile DomainSocketAddress local;
36  
37      public KQueueServerDomainSocketChannel() {
38          super(newSocketDomain(), false);
39      }
40  
41      public KQueueServerDomainSocketChannel(int fd) {
42          this(new BsdSocket(fd), false);
43      }
44  
45      KQueueServerDomainSocketChannel(BsdSocket socket, boolean active) {
46          super(socket, active);
47      }
48  
49      @Override
50      protected Channel newChildChannel(int fd, byte[] addr, int offset, int len) throws Exception {
51          return new KQueueDomainSocketChannel(this, new BsdSocket(fd));
52      }
53  
54      @Override
55      protected DomainSocketAddress localAddress0() {
56          return local;
57      }
58  
59      @Override
60      protected void doBind(SocketAddress localAddress) throws Exception {
61          socket.bind(localAddress);
62          socket.listen(config.getBacklog());
63          local = (DomainSocketAddress) localAddress;
64          active = true;
65      }
66  
67      @Override
68      protected void doClose() throws Exception {
69          try {
70              super.doClose();
71          } finally {
72              DomainSocketAddress local = this.local;
73              if (local != null) {
74                  // Delete the socket file if possible.
75                  File socketFile = new File(local.path());
76                  boolean success = socketFile.delete();
77                  if (!success && logger.isDebugEnabled()) {
78                      logger.debug("Failed to delete a domain socket file: {}", local.path());
79                  }
80              }
81          }
82      }
83  
84      @Override
85      public KQueueServerChannelConfig config() {
86          return config;
87      }
88  
89      @Override
90      public DomainSocketAddress remoteAddress() {
91          return (DomainSocketAddress) super.remoteAddress();
92      }
93  
94      @Override
95      public DomainSocketAddress localAddress() {
96          return (DomainSocketAddress) super.localAddress();
97      }
98  }