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    *   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.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.UnixChannelUtil;
22  import io.netty.channel.unix.Socket;
23  import io.netty.util.internal.logging.InternalLogger;
24  import io.netty.util.internal.logging.InternalLoggerFactory;
25  
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      public EpollServerDomainSocketChannel(int fd, boolean active) {
47          super(new LinuxSocket(fd), active);
48      }
49  
50      EpollServerDomainSocketChannel(LinuxSocket fd) {
51          super(fd);
52      }
53  
54      EpollServerDomainSocketChannel(LinuxSocket fd, boolean active) {
55          super(fd, active);
56      }
57  
58      @Override
59      protected Channel newChildChannel(int fd, byte[] addr, int offset, int len) throws Exception {
60          return new EpollDomainSocketChannel(this, new Socket(fd));
61      }
62  
63      @Override
64      protected DomainSocketAddress localAddress0() {
65          return local;
66      }
67  
68      @Override
69      protected void doBind(SocketAddress localAddress) throws Exception {
70          socket.bind(localAddress);
71          socket.listen(config.getBacklog());
72          local = (DomainSocketAddress) localAddress;
73          active = true;
74          // We now listen for new connections, submit the ops so we receive events.
75          submitCurrentOps();
76      }
77  
78      @Override
79      protected void doClose() throws Exception {
80          try {
81              super.doClose();
82          } finally {
83              DomainSocketAddress local = this.local;
84              if (local != null) {
85                  // Delete the socket file if possible (only if it is not a regular file, directory or symlink).
86                  boolean success = UnixChannelUtil.deleteDomainSocketFile(local.path());
87                  if (!success && logger.isDebugEnabled()) {
88                      logger.debug("Failed to delete a domain socket file: {}", local.path());
89                  }
90              }
91          }
92      }
93  
94      @Override
95      public EpollServerChannelConfig config() {
96          return config;
97      }
98  
99      @Override
100     public DomainSocketAddress remoteAddress() {
101         return (DomainSocketAddress) super.remoteAddress();
102     }
103 
104     @Override
105     public DomainSocketAddress localAddress() {
106         return (DomainSocketAddress) super.localAddress();
107     }
108 }