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