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