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