查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2016 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.kqueue;
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.util.internal.UnstableApi;
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.kqueue.BsdSocket.newSocketDomain;
29  
30  @UnstableApi
31  public final class KQueueServerDomainSocketChannel extends AbstractKQueueServerChannel
32                                                    implements ServerDomainSocketChannel {
33      private static final InternalLogger logger = InternalLoggerFactory.getInstance(
34              KQueueServerDomainSocketChannel.class);
35  
36      private final KQueueServerChannelConfig config = new KQueueServerChannelConfig(this);
37      private volatile DomainSocketAddress local;
38  
39      public KQueueServerDomainSocketChannel() {
40          super(newSocketDomain(), false);
41      }
42  
43      public KQueueServerDomainSocketChannel(int fd) {
44          this(new BsdSocket(fd), false);
45      }
46  
47      KQueueServerDomainSocketChannel(BsdSocket socket, boolean active) {
48          super(socket, active);
49      }
50  
51      @Override
52      protected Channel newChildChannel(int fd, byte[] addr, int offset, int len) throws Exception {
53          return new KQueueDomainSocketChannel(this, new BsdSocket(fd));
54      }
55  
56      @Override
57      protected DomainSocketAddress localAddress0() {
58          return local;
59      }
60  
61      @Override
62      protected void doBind(SocketAddress localAddress) throws Exception {
63          socket.bind(localAddress);
64          socket.listen(config.getBacklog());
65          local = (DomainSocketAddress) localAddress;
66          active = true;
67      }
68  
69      @Override
70      protected void doClose() throws Exception {
71          try {
72              super.doClose();
73          } finally {
74              DomainSocketAddress local = this.local;
75              if (local != null) {
76                  // Delete the socket file if possible.
77                  File socketFile = new File(local.path());
78                  boolean success = socketFile.delete();
79                  if (!success && logger.isDebugEnabled()) {
80                      logger.debug("Failed to delete a domain socket file: {}", local.path());
81                  }
82              }
83          }
84      }
85  
86      @Override
87      public KQueueServerChannelConfig config() {
88          return config;
89      }
90  
91      @Override
92      public DomainSocketAddress remoteAddress() {
93          return (DomainSocketAddress) super.remoteAddress();
94      }
95  
96      @Override
97      public DomainSocketAddress localAddress() {
98          return (DomainSocketAddress) super.localAddress();
99      }
100 }