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    *   http://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  
63          // TODO(scott): tcp fast open here!
64          socket.listen(config.getBacklog());
65          active = true;
66      }
67  
68      @Override
69      public InetSocketAddress remoteAddress() {
70          return (InetSocketAddress) super.remoteAddress();
71      }
72  
73      @Override
74      public InetSocketAddress localAddress() {
75          return (InetSocketAddress) super.localAddress();
76      }
77  
78      @Override
79      public KQueueServerSocketChannelConfig config() {
80          return config;
81      }
82  
83      @Override
84      protected Channel newChildChannel(int fd, byte[] address, int offset, int len) throws Exception {
85          return new KQueueSocketChannel(this, new BsdSocket(fd), address(address, offset, len));
86      }
87  }