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