View Javadoc
1   /*
2    * Copyright 2014 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.epoll;
17  
18  import io.netty.channel.Channel;
19  import io.netty.channel.EventLoop;
20  import io.netty.channel.socket.ServerSocketChannel;
21  
22  import java.io.IOException;
23  import java.net.InetAddress;
24  import java.net.InetSocketAddress;
25  import java.net.SocketAddress;
26  import java.util.Collection;
27  import java.util.Collections;
28  import java.util.Map;
29  
30  import static io.netty.channel.epoll.LinuxSocket.newSocketStream;
31  import static io.netty.channel.unix.NativeInetAddress.address;
32  
33  /**
34   * {@link ServerSocketChannel} implementation that uses linux EPOLL Edge-Triggered Mode for
35   * maximal performance.
36   */
37  public final class EpollServerSocketChannel extends AbstractEpollServerChannel implements ServerSocketChannel {
38  
39      private final EpollServerSocketChannelConfig config;
40      private volatile Collection<InetAddress> tcpMd5SigAddresses = Collections.emptyList();
41  
42      public EpollServerSocketChannel() {
43          super(newSocketStream(), false);
44          config = new EpollServerSocketChannelConfig(this);
45      }
46  
47      public EpollServerSocketChannel(int fd) {
48          // Must call this constructor to ensure this object's local address is configured correctly.
49          // The local address can only be obtained from a Socket object.
50          this(new LinuxSocket(fd));
51      }
52  
53      EpollServerSocketChannel(LinuxSocket fd) {
54          super(fd);
55          config = new EpollServerSocketChannelConfig(this);
56      }
57  
58      EpollServerSocketChannel(LinuxSocket fd, boolean active) {
59          super(fd, active);
60          config = new EpollServerSocketChannelConfig(this);
61      }
62  
63      @Override
64      protected boolean isCompatible(EventLoop loop) {
65          return loop instanceof EpollEventLoop;
66      }
67  
68      @Override
69      protected void doBind(SocketAddress localAddress) throws Exception {
70          super.doBind(localAddress);
71          if (Native.IS_SUPPORTING_TCP_FASTOPEN && config.getTcpFastopen() > 0) {
72              socket.setTcpFastOpen(config.getTcpFastopen());
73          }
74          socket.listen(config.getBacklog());
75          active = true;
76      }
77  
78      @Override
79      public InetSocketAddress remoteAddress() {
80          return (InetSocketAddress) super.remoteAddress();
81      }
82  
83      @Override
84      public InetSocketAddress localAddress() {
85          return (InetSocketAddress) super.localAddress();
86      }
87  
88      @Override
89      public EpollServerSocketChannelConfig config() {
90          return config;
91      }
92  
93      @Override
94      protected Channel newChildChannel(int fd, byte[] address, int offset, int len) throws Exception {
95          return new EpollSocketChannel(this, new LinuxSocket(fd), address(address, offset, len));
96      }
97  
98      Collection<InetAddress> tcpMd5SigAddresses() {
99          return tcpMd5SigAddresses;
100     }
101 
102     void setTcpMd5Sig(Map<InetAddress, byte[]> keys) throws IOException {
103         tcpMd5SigAddresses = TcpMd5Util.newTcpMd5Sigs(this, tcpMd5SigAddresses, keys);
104     }
105 }