author | alanb |
Thu, 25 Apr 2019 14:36:51 +0100 | |
branch | niosocketimpl-branch |
changeset 57338 | 8684e6479b20 |
parent 49001 | ce06058197a4 |
child 54620 | 13b67c1420b8 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
48761 | 2 |
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.nio.ch; |
|
27 |
||
48761 | 28 |
import java.io.IOException; |
29 |
import java.net.InetAddress; |
|
30 |
import java.net.InetSocketAddress; |
|
31 |
import java.net.ServerSocket; |
|
32 |
import java.net.Socket; |
|
33 |
import java.net.SocketAddress; |
|
34 |
import java.net.SocketException; |
|
35 |
import java.net.SocketTimeoutException; |
|
36 |
import java.net.StandardSocketOptions; |
|
37 |
import java.nio.channels.IllegalBlockingModeException; |
|
38 |
import java.nio.channels.NotYetBoundException; |
|
39 |
import java.nio.channels.ServerSocketChannel; |
|
40 |
import java.nio.channels.SocketChannel; |
|
2 | 41 |
|
42 |
||
43 |
// Make a server-socket channel look like a server socket. |
|
44 |
// |
|
45 |
// The methods in this class are defined in exactly the same order as in |
|
46 |
// java.net.ServerSocket so as to simplify tracking future changes to that |
|
47 |
// class. |
|
48 |
// |
|
49 |
||
48761 | 50 |
class ServerSocketAdaptor // package-private |
2 | 51 |
extends ServerSocket |
52 |
{ |
|
53 |
// The channel being adapted |
|
54 |
private final ServerSocketChannelImpl ssc; |
|
55 |
||
56 |
// Timeout "option" value for accepts |
|
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
25859
diff
changeset
|
57 |
private volatile int timeout; |
2 | 58 |
|
59 |
public static ServerSocket create(ServerSocketChannelImpl ssc) { |
|
60 |
try { |
|
61 |
return new ServerSocketAdaptor(ssc); |
|
62 |
} catch (IOException x) { |
|
63 |
throw new Error(x); |
|
64 |
} |
|
65 |
} |
|
66 |
||
67 |
// ## super will create a useless impl |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
68 |
private ServerSocketAdaptor(ServerSocketChannelImpl ssc) throws IOException { |
2 | 69 |
this.ssc = ssc; |
70 |
} |
|
71 |
||
72 |
public void bind(SocketAddress local) throws IOException { |
|
73 |
bind(local, 50); |
|
74 |
} |
|
75 |
||
76 |
public void bind(SocketAddress local, int backlog) throws IOException { |
|
77 |
if (local == null) |
|
78 |
local = new InetSocketAddress(0); |
|
79 |
try { |
|
80 |
ssc.bind(local, backlog); |
|
81 |
} catch (Exception x) { |
|
82 |
Net.translateException(x); |
|
83 |
} |
|
84 |
} |
|
85 |
||
86 |
public InetAddress getInetAddress() { |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
87 |
InetSocketAddress local = ssc.localAddress(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
88 |
if (local == null) { |
2 | 89 |
return null; |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
90 |
} else { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
91 |
return Net.getRevealedLocalAddress(local).getAddress(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
92 |
} |
2 | 93 |
} |
94 |
||
95 |
public int getLocalPort() { |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
96 |
InetSocketAddress local = ssc.localAddress(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
97 |
if (local == null) { |
2 | 98 |
return -1; |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
99 |
} else { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
100 |
return local.getPort(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
101 |
} |
2 | 102 |
} |
103 |
||
104 |
public Socket accept() throws IOException { |
|
105 |
synchronized (ssc.blockingLock()) { |
|
106 |
try { |
|
24623
c094ffea7aee
8024832: ServerSocketChannel.socket().accept() throws IllegalBlockingModeException if not bound
prappo
parents:
23010
diff
changeset
|
107 |
if (!ssc.isBound()) |
c094ffea7aee
8024832: ServerSocketChannel.socket().accept() throws IllegalBlockingModeException if not bound
prappo
parents:
23010
diff
changeset
|
108 |
throw new NotYetBoundException(); |
48761 | 109 |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
110 |
long to = this.timeout; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
111 |
if (to == 0) { |
48761 | 112 |
// for compatibility reasons: accept connection if available |
113 |
// when configured non-blocking |
|
2 | 114 |
SocketChannel sc = ssc.accept(); |
115 |
if (sc == null && !ssc.isBlocking()) |
|
116 |
throw new IllegalBlockingModeException(); |
|
117 |
return sc.socket(); |
|
118 |
} |
|
119 |
||
48761 | 120 |
if (!ssc.isBlocking()) |
121 |
throw new IllegalBlockingModeException(); |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
122 |
for (;;) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
123 |
long st = System.currentTimeMillis(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
124 |
if (ssc.pollAccept(to)) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
125 |
return ssc.accept().socket(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
126 |
to -= System.currentTimeMillis() - st; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
127 |
if (to <= 0) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
128 |
throw new SocketTimeoutException(); |
2 | 129 |
} |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
130 |
|
2 | 131 |
} catch (Exception x) { |
132 |
Net.translateException(x); |
|
133 |
assert false; |
|
134 |
return null; // Never happens |
|
135 |
} |
|
136 |
} |
|
137 |
} |
|
138 |
||
139 |
public void close() throws IOException { |
|
5784
e565c553e9fc
6938230: (so) SocketAdaptor.close() does not translate IOException resulting in Error
alanb
parents:
5506
diff
changeset
|
140 |
ssc.close(); |
2 | 141 |
} |
142 |
||
143 |
public ServerSocketChannel getChannel() { |
|
144 |
return ssc; |
|
145 |
} |
|
146 |
||
147 |
public boolean isBound() { |
|
148 |
return ssc.isBound(); |
|
149 |
} |
|
150 |
||
151 |
public boolean isClosed() { |
|
152 |
return !ssc.isOpen(); |
|
153 |
} |
|
154 |
||
155 |
public void setSoTimeout(int timeout) throws SocketException { |
|
156 |
this.timeout = timeout; |
|
157 |
} |
|
158 |
||
159 |
public int getSoTimeout() throws SocketException { |
|
160 |
return timeout; |
|
161 |
} |
|
162 |
||
163 |
public void setReuseAddress(boolean on) throws SocketException { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
164 |
try { |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
165 |
ssc.setOption(StandardSocketOptions.SO_REUSEADDR, on); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
166 |
} catch (IOException x) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
167 |
Net.translateToSocketException(x); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
168 |
} |
2 | 169 |
} |
170 |
||
171 |
public boolean getReuseAddress() throws SocketException { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
172 |
try { |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
173 |
return ssc.getOption(StandardSocketOptions.SO_REUSEADDR).booleanValue(); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
174 |
} catch (IOException x) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
175 |
Net.translateToSocketException(x); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
176 |
return false; // Never happens |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
177 |
} |
2 | 178 |
} |
179 |
||
180 |
public String toString() { |
|
181 |
if (!isBound()) |
|
182 |
return "ServerSocket[unbound]"; |
|
183 |
return "ServerSocket[addr=" + getInetAddress() + |
|
48761 | 184 |
",localport=" + getLocalPort() + "]"; |
2 | 185 |
} |
186 |
||
187 |
public void setReceiveBufferSize(int size) throws SocketException { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
188 |
// size 0 valid for ServerSocketChannel, invalid for ServerSocket |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
189 |
if (size <= 0) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
190 |
throw new IllegalArgumentException("size cannot be 0 or negative"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
191 |
try { |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
192 |
ssc.setOption(StandardSocketOptions.SO_RCVBUF, size); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
193 |
} catch (IOException x) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
194 |
Net.translateToSocketException(x); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
195 |
} |
2 | 196 |
} |
197 |
||
198 |
public int getReceiveBufferSize() throws SocketException { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
199 |
try { |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
200 |
return ssc.getOption(StandardSocketOptions.SO_RCVBUF).intValue(); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
201 |
} catch (IOException x) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
202 |
Net.translateToSocketException(x); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
203 |
return -1; // Never happens |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
204 |
} |
2 | 205 |
} |
206 |
} |