View Javadoc
1   /*
2    * Copyright 2012 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.socket.nio;
17  
18  import io.netty.channel.ChannelException;
19  import io.netty.channel.ChannelMetadata;
20  import io.netty.channel.ChannelOption;
21  import io.netty.channel.ChannelOutboundBuffer;
22  import io.netty.util.internal.SocketUtils;
23  import io.netty.channel.nio.AbstractNioMessageChannel;
24  import io.netty.channel.socket.DefaultServerSocketChannelConfig;
25  import io.netty.channel.socket.ServerSocketChannelConfig;
26  import io.netty.util.internal.PlatformDependent;
27  import io.netty.util.internal.logging.InternalLogger;
28  import io.netty.util.internal.logging.InternalLoggerFactory;
29  
30  import java.io.IOException;
31  import java.net.InetSocketAddress;
32  import java.net.ServerSocket;
33  import java.net.SocketAddress;
34  import java.nio.channels.SelectionKey;
35  import java.nio.channels.ServerSocketChannel;
36  import java.nio.channels.SocketChannel;
37  import java.nio.channels.spi.SelectorProvider;
38  import java.util.List;
39  import java.util.Map;
40  
41  /**
42   * A {@link io.netty.channel.socket.ServerSocketChannel} implementation which uses
43   * NIO selector based implementation to accept new connections.
44   */
45  public class NioServerSocketChannel extends AbstractNioMessageChannel
46                               implements io.netty.channel.socket.ServerSocketChannel {
47  
48      private static final ChannelMetadata METADATA = new ChannelMetadata(false, 16);
49      private static final SelectorProvider DEFAULT_SELECTOR_PROVIDER = SelectorProvider.provider();
50  
51      private static final InternalLogger logger = InternalLoggerFactory.getInstance(NioServerSocketChannel.class);
52  
53      private static ServerSocketChannel newSocket(SelectorProvider provider) {
54          try {
55              /**
56               *  Use the {@link SelectorProvider} to open {@link SocketChannel} and so remove condition in
57               *  {@link SelectorProvider#provider()} which is called by each ServerSocketChannel.open() otherwise.
58               *
59               *  See <a href="https://github.com/netty/netty/issues/2308">#2308</a>.
60               */
61              return provider.openServerSocketChannel();
62          } catch (IOException e) {
63              throw new ChannelException(
64                      "Failed to open a server socket.", e);
65          }
66      }
67  
68      private final ServerSocketChannelConfig config;
69  
70      /**
71       * Create a new instance
72       */
73      public NioServerSocketChannel() {
74          this(newSocket(DEFAULT_SELECTOR_PROVIDER));
75      }
76  
77      /**
78       * Create a new instance using the given {@link SelectorProvider}.
79       */
80      public NioServerSocketChannel(SelectorProvider provider) {
81          this(newSocket(provider));
82      }
83  
84      /**
85       * Create a new instance using the given {@link ServerSocketChannel}.
86       */
87      public NioServerSocketChannel(ServerSocketChannel channel) {
88          super(null, channel, SelectionKey.OP_ACCEPT);
89          config = new NioServerSocketChannelConfig(this, javaChannel().socket());
90      }
91  
92      @Override
93      public InetSocketAddress localAddress() {
94          return (InetSocketAddress) super.localAddress();
95      }
96  
97      @Override
98      public ChannelMetadata metadata() {
99          return METADATA;
100     }
101 
102     @Override
103     public ServerSocketChannelConfig config() {
104         return config;
105     }
106 
107     @Override
108     public boolean isActive() {
109         return javaChannel().socket().isBound();
110     }
111 
112     @Override
113     public InetSocketAddress remoteAddress() {
114         return null;
115     }
116 
117     @Override
118     protected ServerSocketChannel javaChannel() {
119         return (ServerSocketChannel) super.javaChannel();
120     }
121 
122     @Override
123     protected SocketAddress localAddress0() {
124         return SocketUtils.localSocketAddress(javaChannel().socket());
125     }
126 
127     @Override
128     protected void doBind(SocketAddress localAddress) throws Exception {
129         if (PlatformDependent.javaVersion() >= 7) {
130             javaChannel().bind(localAddress, config.getBacklog());
131         } else {
132             javaChannel().socket().bind(localAddress, config.getBacklog());
133         }
134     }
135 
136     @Override
137     protected void doClose() throws Exception {
138         javaChannel().close();
139     }
140 
141     @Override
142     protected int doReadMessages(List<Object> buf) throws Exception {
143         SocketChannel ch = SocketUtils.accept(javaChannel());
144 
145         try {
146             if (ch != null) {
147                 buf.add(new NioSocketChannel(this, ch));
148                 return 1;
149             }
150         } catch (Throwable t) {
151             logger.warn("Failed to create a new channel from an accepted socket.", t);
152 
153             try {
154                 ch.close();
155             } catch (Throwable t2) {
156                 logger.warn("Failed to close a socket.", t2);
157             }
158         }
159 
160         return 0;
161     }
162 
163     // Unnecessary stuff
164     @Override
165     protected boolean doConnect(
166             SocketAddress remoteAddress, SocketAddress localAddress) throws Exception {
167         throw new UnsupportedOperationException();
168     }
169 
170     @Override
171     protected void doFinishConnect() throws Exception {
172         throw new UnsupportedOperationException();
173     }
174 
175     @Override
176     protected SocketAddress remoteAddress0() {
177         return null;
178     }
179 
180     @Override
181     protected void doDisconnect() throws Exception {
182         throw new UnsupportedOperationException();
183     }
184 
185     @Override
186     protected boolean doWriteMessage(Object msg, ChannelOutboundBuffer in) throws Exception {
187         throw new UnsupportedOperationException();
188     }
189 
190     @Override
191     protected final Object filterOutboundMessage(Object msg) throws Exception {
192         throw new UnsupportedOperationException();
193     }
194 
195     private final class NioServerSocketChannelConfig extends DefaultServerSocketChannelConfig {
196         private NioServerSocketChannelConfig(NioServerSocketChannel channel, ServerSocket javaSocket) {
197             super(channel, javaSocket);
198         }
199 
200         @Override
201         protected void autoReadCleared() {
202             clearReadPending();
203         }
204 
205         @Override
206         public <T> boolean setOption(ChannelOption<T> option, T value) {
207             if (PlatformDependent.javaVersion() >= 7 && option instanceof NioChannelOption) {
208                 return NioChannelOption.setOption(jdkChannel(), (NioChannelOption<T>) option, value);
209             }
210             return super.setOption(option, value);
211         }
212 
213         @Override
214         public <T> T getOption(ChannelOption<T> option) {
215             if (PlatformDependent.javaVersion() >= 7 && option instanceof NioChannelOption) {
216                 return NioChannelOption.getOption(jdkChannel(), (NioChannelOption<T>) option);
217             }
218             return super.getOption(option);
219         }
220 
221         @SuppressWarnings("unchecked")
222         @Override
223         public Map<ChannelOption<?>, Object> getOptions() {
224             if (PlatformDependent.javaVersion() >= 7) {
225                 return getOptions(super.getOptions(), NioChannelOption.getOptions(jdkChannel()));
226             }
227             return super.getOptions();
228         }
229 
230         private ServerSocketChannel jdkChannel() {
231             return ((NioServerSocketChannel) channel).javaChannel();
232         }
233     }
234 
235     // Override just to to be able to call directly via unit tests.
236     @Override
237     protected boolean closeOnReadError(Throwable cause) {
238         return super.closeOnReadError(cause);
239     }
240 }