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