查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
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.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      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      }
75  
76      @Override
77      protected void doClose() throws Exception {
78          try {
79              super.doClose();
80          } finally {
81              DomainSocketAddress local = this.local;
82              if (local != null) {
83                  // Delete the socket file if possible.
84                  File socketFile = new File(local.path());
85                  boolean success = socketFile.delete();
86                  if (!success && logger.isDebugEnabled()) {
87                      logger.debug("Failed to delete a domain socket file: {}", local.path());
88                  }
89              }
90          }
91      }
92  
93      @Override
94      public EpollServerChannelConfig config() {
95          return config;
96      }
97  
98      @Override
99      public DomainSocketAddress remoteAddress() {
100         return (DomainSocketAddress) super.remoteAddress();
101     }
102 
103     @Override
104     public DomainSocketAddress localAddress() {
105         return (DomainSocketAddress) super.localAddress();
106     }
107 }