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  import io.netty.channel.unix.FileDescriptor;
22  
23  import java.net.InetSocketAddress;
24  import java.net.SocketAddress;
25  
26  /**
27   * {@link ServerSocketChannel} implementation that uses linux EPOLL Edge-Triggered Mode for
28   * maximal performance.
29   */
30  public final class EpollServerSocketChannel extends AbstractEpollServerChannel implements ServerSocketChannel {
31  
32      private final EpollServerSocketChannelConfig config;
33      private volatile InetSocketAddress local;
34  
35      public EpollServerSocketChannel() {
36          super(Native.socketStreamFd());
37          config = new EpollServerSocketChannelConfig(this);
38      }
39  
40      /**
41       * Creates a new {@link EpollServerSocketChannel} from an existing {@link FileDescriptor}.
42       */
43      public EpollServerSocketChannel(FileDescriptor fd) {
44          super(fd);
45          config = new EpollServerSocketChannelConfig(this);
46  
47          // As we create an EpollServerSocketChannel from a FileDescriptor we should try to obtain the remote and local
48          // address from it. This is needed as the FileDescriptor may be bound already.
49          local = Native.localAddress(fd.intValue());
50      }
51  
52      @Override
53      protected boolean isCompatible(EventLoop loop) {
54          return loop instanceof EpollEventLoop;
55      }
56  
57      @Override
58      protected void doBind(SocketAddress localAddress) throws Exception {
59          InetSocketAddress addr = (InetSocketAddress) localAddress;
60          checkResolvable(addr);
61          int fd = fd().intValue();
62          Native.bind(fd, addr);
63          local = Native.localAddress(fd);
64          Native.listen(fd, 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 EpollServerSocketChannelConfig config() {
80          return config;
81      }
82  
83      @Override
84      protected InetSocketAddress localAddress0() {
85          return local;
86      }
87  
88      @Override
89      protected Channel newChildChannel(int fd, byte[] address, int offset, int len) throws Exception {
90          return new EpollSocketChannel(this, fd, Native.address(address, offset, len));
91      }
92  }