View Javadoc
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.EventLoop;
20  import io.netty.channel.socket.ServerSocketChannel;
21  import io.netty.util.internal.UnstableApi;
22  
23  import java.net.InetSocketAddress;
24  import java.net.SocketAddress;
25  
26  import static io.netty.channel.kqueue.BsdSocket.newSocketStream;
27  import static io.netty.channel.unix.NativeInetAddress.address;
28  
29  @UnstableApi
30  public final class KQueueServerSocketChannel extends AbstractKQueueServerChannel implements ServerSocketChannel {
31      private final KQueueServerSocketChannelConfig config;
32  
33      public KQueueServerSocketChannel() {
34          super(newSocketStream(), false);
35          config = new KQueueServerSocketChannelConfig(this);
36      }
37  
38      public KQueueServerSocketChannel(int fd) {
39          // Must call this constructor to ensure this object's local address is configured correctly.
40          // The local address can only be obtained from a Socket object.
41          this(new BsdSocket(fd));
42      }
43  
44      KQueueServerSocketChannel(BsdSocket fd) {
45          super(fd);
46          config = new KQueueServerSocketChannelConfig(this);
47      }
48  
49      KQueueServerSocketChannel(BsdSocket fd, boolean active) {
50          super(fd, active);
51          config = new KQueueServerSocketChannelConfig(this);
52      }
53  
54      @Override
55      protected boolean isCompatible(EventLoop loop) {
56          return loop instanceof KQueueEventLoop;
57      }
58  
59      @Override
60      protected void doBind(SocketAddress localAddress) throws Exception {
61          super.doBind(localAddress);
62          socket.listen(config.getBacklog());
63          if (config.isTcpFastOpen()) {
64              socket.setTcpFastOpen(true);
65          }
66          active = true;
67      }
68  
69      @Override
70      public InetSocketAddress remoteAddress() {
71          return (InetSocketAddress) super.remoteAddress();
72      }
73  
74      @Override
75      public InetSocketAddress localAddress() {
76          return (InetSocketAddress) super.localAddress();
77      }
78  
79      @Override
80      public KQueueServerSocketChannelConfig config() {
81          return config;
82      }
83  
84      @Override
85      protected Channel newChildChannel(int fd, byte[] address, int offset, int len) throws Exception {
86          return new KQueueSocketChannel(this, new BsdSocket(fd), address(address, offset, len));
87      }
88  }