1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package io.netty.channel.socket.oio;
17  
18  import io.netty.channel.ChannelException;
19  import io.netty.channel.ChannelMetadata;
20  import io.netty.channel.ChannelOutboundBuffer;
21  import io.netty.channel.oio.AbstractOioMessageChannel;
22  import io.netty.channel.socket.ServerSocketChannel;
23  import io.netty.util.internal.ObjectUtil;
24  import io.netty.util.internal.SocketUtils;
25  import io.netty.util.internal.logging.InternalLogger;
26  import io.netty.util.internal.logging.InternalLoggerFactory;
27  
28  import java.io.IOException;
29  import java.net.InetSocketAddress;
30  import java.net.ServerSocket;
31  import java.net.Socket;
32  import java.net.SocketAddress;
33  import java.net.SocketTimeoutException;
34  import java.util.List;
35  
36  
37  
38  
39  
40  
41  
42  
43  @Deprecated
44  public class OioServerSocketChannel extends AbstractOioMessageChannel
45                                      implements ServerSocketChannel {
46  
47      private static final InternalLogger logger =
48          InternalLoggerFactory.getInstance(OioServerSocketChannel.class);
49  
50      private static final ChannelMetadata METADATA = new ChannelMetadata(false, 1);
51  
52      private static ServerSocket newServerSocket() {
53          try {
54              return new ServerSocket();
55          } catch (IOException e) {
56              throw new ChannelException("failed to create a server socket", e);
57          }
58      }
59  
60      final ServerSocket socket;
61      private final OioServerSocketChannelConfig config;
62  
63      
64  
65  
66      public OioServerSocketChannel() {
67          this(newServerSocket());
68      }
69  
70      
71  
72  
73  
74  
75      public OioServerSocketChannel(ServerSocket socket) {
76          super(null);
77          ObjectUtil.checkNotNull(socket, "socket");
78  
79          boolean success = false;
80          try {
81              socket.setSoTimeout(SO_TIMEOUT);
82              success = true;
83          } catch (IOException e) {
84              throw new ChannelException(
85                      "Failed to set the server socket timeout.", e);
86          } finally {
87              if (!success) {
88                  try {
89                      socket.close();
90                  } catch (IOException e) {
91                      if (logger.isWarnEnabled()) {
92                          logger.warn(
93                                  "Failed to close a partially initialized socket.", e);
94                      }
95                  }
96              }
97          }
98          this.socket = socket;
99          config = new DefaultOioServerSocketChannelConfig(this, socket);
100     }
101 
102     @Override
103     public InetSocketAddress localAddress() {
104         return (InetSocketAddress) super.localAddress();
105     }
106 
107     @Override
108     public ChannelMetadata metadata() {
109         return METADATA;
110     }
111 
112     @Override
113     public OioServerSocketChannelConfig config() {
114         return config;
115     }
116 
117     @Override
118     public InetSocketAddress remoteAddress() {
119         return null;
120     }
121 
122     @Override
123     public boolean isOpen() {
124         return !socket.isClosed();
125     }
126 
127     @Override
128     public boolean isActive() {
129         return isOpen() && socket.isBound();
130     }
131 
132     @Override
133     protected SocketAddress localAddress0() {
134         return SocketUtils.localSocketAddress(socket);
135     }
136 
137     @Override
138     protected void doBind(SocketAddress localAddress) throws Exception {
139         socket.bind(localAddress, config.getBacklog());
140     }
141 
142     @Override
143     protected void doClose() throws Exception {
144         socket.close();
145     }
146 
147     @Override
148     protected int doReadMessages(List<Object> buf) throws Exception {
149         if (socket.isClosed()) {
150             return -1;
151         }
152 
153         try {
154             Socket s = socket.accept();
155             try {
156                 buf.add(new OioSocketChannel(this, s));
157                 return 1;
158             } catch (Throwable t) {
159                 logger.warn("Failed to create a new channel from an accepted socket.", t);
160                 try {
161                     s.close();
162                 } catch (Throwable t2) {
163                     logger.warn("Failed to close a socket.", t2);
164                 }
165             }
166         } catch (SocketTimeoutException e) {
167             
168         }
169         return 0;
170     }
171 
172     @Override
173     protected void doWrite(ChannelOutboundBuffer in) throws Exception {
174         throw new UnsupportedOperationException();
175     }
176 
177     @Override
178     protected Object filterOutboundMessage(Object msg) throws Exception {
179         throw new UnsupportedOperationException();
180     }
181 
182     @Override
183     protected void doConnect(
184             SocketAddress remoteAddress, SocketAddress localAddress) throws Exception {
185         throw new UnsupportedOperationException();
186     }
187 
188     @Override
189     protected SocketAddress remoteAddress0() {
190         return null;
191     }
192 
193     @Override
194     protected void doDisconnect() throws Exception {
195         throw new UnsupportedOperationException();
196     }
197 
198     @Deprecated
199     @Override
200     protected void setReadPending(boolean readPending) {
201         super.setReadPending(readPending);
202     }
203 
204     final void clearReadPending0() {
205         super.clearReadPending();
206     }
207 }