author | alanb |
Thu, 08 Feb 2018 10:55:21 +0000 | |
changeset 48761 | 74c1fa26435a |
parent 48750 | ffbb784a8873 |
child 49001 | ce06058197a4 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
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 |
||
28 |
import java.io.FileDescriptor; |
|
29 |
import java.io.IOException; |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
30 |
import java.net.InetAddress; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
31 |
import java.net.InetSocketAddress; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
32 |
import java.net.ProtocolFamily; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
33 |
import java.net.Socket; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
34 |
import java.net.SocketAddress; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
35 |
import java.net.SocketOption; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
36 |
import java.net.StandardProtocolFamily; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
37 |
import java.net.StandardSocketOptions; |
2 | 38 |
import java.nio.ByteBuffer; |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
39 |
import java.nio.channels.AlreadyBoundException; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
40 |
import java.nio.channels.AlreadyConnectedException; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
41 |
import java.nio.channels.AsynchronousCloseException; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
42 |
import java.nio.channels.ClosedChannelException; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
43 |
import java.nio.channels.ConnectionPendingException; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
44 |
import java.nio.channels.NoConnectionPendingException; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
45 |
import java.nio.channels.NotYetConnectedException; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
46 |
import java.nio.channels.SelectionKey; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
47 |
import java.nio.channels.SocketChannel; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
48 |
import java.nio.channels.spi.SelectorProvider; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
49 |
import java.util.Collections; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
50 |
import java.util.HashSet; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
51 |
import java.util.Set; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
52 |
import java.util.concurrent.locks.ReentrantLock; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
53 |
|
2446 | 54 |
import sun.net.NetHooks; |
37676
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
36115
diff
changeset
|
55 |
import sun.net.ext.ExtendedSocketOptions; |
2 | 56 |
|
57 |
/** |
|
58 |
* An implementation of SocketChannels |
|
59 |
*/ |
|
60 |
||
61 |
class SocketChannelImpl |
|
62 |
extends SocketChannel |
|
63 |
implements SelChImpl |
|
64 |
{ |
|
65 |
||
66 |
// Used to make native read and write calls |
|
67 |
private static NativeDispatcher nd; |
|
68 |
||
69 |
// Our file descriptor object |
|
70 |
private final FileDescriptor fd; |
|
71 |
private final int fdVal; |
|
72 |
||
73 |
// IDs of native threads doing reads and writes, for signalling |
|
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
29986
diff
changeset
|
74 |
private volatile long readerThread; |
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
29986
diff
changeset
|
75 |
private volatile long writerThread; |
2 | 76 |
|
77 |
// Lock held by current reading or connecting thread |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
78 |
private final ReentrantLock readLock = new ReentrantLock(); |
2 | 79 |
|
80 |
// Lock held by current writing or connecting thread |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
81 |
private final ReentrantLock writeLock = new ReentrantLock(); |
2 | 82 |
|
83 |
// Lock held by any thread that modifies the state fields declared below |
|
84 |
// DO NOT invoke a blocking I/O operation while holding this lock! |
|
85 |
private final Object stateLock = new Object(); |
|
86 |
||
87 |
// -- The following fields are protected by stateLock |
|
88 |
||
18192 | 89 |
// set true when exclusive binding is on and SO_REUSEADDR is emulated |
90 |
private boolean isReuseAddress; |
|
91 |
||
2 | 92 |
// State, increases monotonically |
93 |
private static final int ST_UNINITIALIZED = -1; |
|
94 |
private static final int ST_UNCONNECTED = 0; |
|
95 |
private static final int ST_PENDING = 1; |
|
96 |
private static final int ST_CONNECTED = 2; |
|
97 |
private static final int ST_KILLPENDING = 3; |
|
98 |
private static final int ST_KILLED = 4; |
|
99 |
private int state = ST_UNINITIALIZED; |
|
100 |
||
101 |
// Binding |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
102 |
private InetSocketAddress localAddress; |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
103 |
private InetSocketAddress remoteAddress; |
2 | 104 |
|
105 |
// Input/Output open |
|
106 |
private boolean isInputOpen = true; |
|
107 |
private boolean isOutputOpen = true; |
|
108 |
||
109 |
// Socket adaptor, created on demand |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
110 |
private Socket socket; |
2 | 111 |
|
112 |
// -- End of fields protected by stateLock |
|
113 |
||
114 |
||
115 |
// Constructor for normal connecting sockets |
|
116 |
// |
|
117 |
SocketChannelImpl(SelectorProvider sp) throws IOException { |
|
118 |
super(sp); |
|
119 |
this.fd = Net.socket(true); |
|
120 |
this.fdVal = IOUtil.fdVal(fd); |
|
121 |
this.state = ST_UNCONNECTED; |
|
122 |
} |
|
123 |
||
6525 | 124 |
SocketChannelImpl(SelectorProvider sp, |
125 |
FileDescriptor fd, |
|
126 |
boolean bound) |
|
127 |
throws IOException |
|
128 |
{ |
|
129 |
super(sp); |
|
130 |
this.fd = fd; |
|
131 |
this.fdVal = IOUtil.fdVal(fd); |
|
132 |
this.state = ST_UNCONNECTED; |
|
133 |
if (bound) |
|
134 |
this.localAddress = Net.localAddress(fd); |
|
135 |
} |
|
136 |
||
2 | 137 |
// Constructor for sockets obtained from server sockets |
138 |
// |
|
139 |
SocketChannelImpl(SelectorProvider sp, |
|
140 |
FileDescriptor fd, InetSocketAddress remote) |
|
141 |
throws IOException |
|
142 |
{ |
|
143 |
super(sp); |
|
144 |
this.fd = fd; |
|
145 |
this.fdVal = IOUtil.fdVal(fd); |
|
146 |
this.state = ST_CONNECTED; |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
147 |
this.localAddress = Net.localAddress(fd); |
2 | 148 |
this.remoteAddress = remote; |
149 |
} |
|
150 |
||
151 |
public Socket socket() { |
|
152 |
synchronized (stateLock) { |
|
153 |
if (socket == null) |
|
154 |
socket = SocketAdaptor.create(this); |
|
155 |
return socket; |
|
156 |
} |
|
157 |
} |
|
158 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
159 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
160 |
public SocketAddress getLocalAddress() throws IOException { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
161 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
162 |
if (!isOpen()) |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
163 |
throw new ClosedChannelException(); |
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
164 |
return Net.getRevealedLocalAddress(localAddress); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
165 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
166 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
167 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
168 |
@Override |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
169 |
public SocketAddress getRemoteAddress() throws IOException { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
170 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
171 |
if (!isOpen()) |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
172 |
throw new ClosedChannelException(); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
173 |
return remoteAddress; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
174 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
175 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
176 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
177 |
@Override |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
178 |
public <T> SocketChannel setOption(SocketOption<T> name, T value) |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
179 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
180 |
{ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
181 |
if (name == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
182 |
throw new NullPointerException(); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
183 |
if (!supportedOptions().contains(name)) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
184 |
throw new UnsupportedOperationException("'" + name + "' not supported"); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
185 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
186 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
187 |
if (!isOpen()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
188 |
throw new ClosedChannelException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
189 |
|
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
190 |
if (name == StandardSocketOptions.IP_TOS) { |
25170
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
191 |
ProtocolFamily family = Net.isIPv6Available() ? |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
192 |
StandardProtocolFamily.INET6 : StandardProtocolFamily.INET; |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
193 |
Net.setSocketOption(fd, family, name, value); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
194 |
return this; |
25170
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
195 |
} |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
196 |
|
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
197 |
if (name == StandardSocketOptions.SO_REUSEADDR && Net.useExclusiveBind()) { |
18192 | 198 |
// SO_REUSEADDR emulated when using exclusive bind |
199 |
isReuseAddress = (Boolean)value; |
|
200 |
return this; |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
201 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
202 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
203 |
// no options that require special handling |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
204 |
Net.setSocketOption(fd, Net.UNSPEC, name, value); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
205 |
return this; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
206 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
207 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
208 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
209 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
210 |
@SuppressWarnings("unchecked") |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
211 |
public <T> T getOption(SocketOption<T> name) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
212 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
213 |
{ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
214 |
if (name == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
215 |
throw new NullPointerException(); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
216 |
if (!supportedOptions().contains(name)) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
217 |
throw new UnsupportedOperationException("'" + name + "' not supported"); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
218 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
219 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
220 |
if (!isOpen()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
221 |
throw new ClosedChannelException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
222 |
|
18192 | 223 |
if (name == StandardSocketOptions.SO_REUSEADDR && |
224 |
Net.useExclusiveBind()) |
|
225 |
{ |
|
226 |
// SO_REUSEADDR emulated when using exclusive bind |
|
227 |
return (T)Boolean.valueOf(isReuseAddress); |
|
228 |
} |
|
229 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
230 |
// special handling for IP_TOS: always return 0 when IPv6 |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
231 |
if (name == StandardSocketOptions.IP_TOS) { |
25170
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
232 |
ProtocolFamily family = Net.isIPv6Available() ? |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
233 |
StandardProtocolFamily.INET6 : StandardProtocolFamily.INET; |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
234 |
return (T) Net.getSocketOption(fd, family, name); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
235 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
236 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
237 |
// no options that require special handling |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
238 |
return (T) Net.getSocketOption(fd, Net.UNSPEC, name); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
239 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
240 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
241 |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
242 |
private static class DefaultOptionsHolder { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
243 |
static final Set<SocketOption<?>> defaultOptions = defaultOptions(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
244 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
245 |
private static Set<SocketOption<?>> defaultOptions() { |
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
25859
diff
changeset
|
246 |
HashSet<SocketOption<?>> set = new HashSet<>(8); |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
247 |
set.add(StandardSocketOptions.SO_SNDBUF); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
248 |
set.add(StandardSocketOptions.SO_RCVBUF); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
249 |
set.add(StandardSocketOptions.SO_KEEPALIVE); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
250 |
set.add(StandardSocketOptions.SO_REUSEADDR); |
36115 | 251 |
if (Net.isReusePortAvailable()) { |
252 |
set.add(StandardSocketOptions.SO_REUSEPORT); |
|
253 |
} |
|
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
254 |
set.add(StandardSocketOptions.SO_LINGER); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
255 |
set.add(StandardSocketOptions.TCP_NODELAY); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
256 |
// additional options required by socket adaptor |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
257 |
set.add(StandardSocketOptions.IP_TOS); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
258 |
set.add(ExtendedSocketOption.SO_OOBINLINE); |
37676
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
36115
diff
changeset
|
259 |
ExtendedSocketOptions extendedOptions = |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
36115
diff
changeset
|
260 |
ExtendedSocketOptions.getInstance(); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
36115
diff
changeset
|
261 |
set.addAll(extendedOptions.options()); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
262 |
return Collections.unmodifiableSet(set); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
263 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
264 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
265 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
266 |
@Override |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
267 |
public final Set<SocketOption<?>> supportedOptions() { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
268 |
return DefaultOptionsHolder.defaultOptions; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
269 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
270 |
|
2 | 271 |
private boolean ensureReadOpen() throws ClosedChannelException { |
272 |
synchronized (stateLock) { |
|
273 |
if (!isOpen()) |
|
274 |
throw new ClosedChannelException(); |
|
275 |
if (!isConnected()) |
|
276 |
throw new NotYetConnectedException(); |
|
277 |
if (!isInputOpen) |
|
278 |
return false; |
|
279 |
else |
|
280 |
return true; |
|
281 |
} |
|
282 |
} |
|
283 |
||
284 |
private void ensureWriteOpen() throws ClosedChannelException { |
|
285 |
synchronized (stateLock) { |
|
286 |
if (!isOpen()) |
|
287 |
throw new ClosedChannelException(); |
|
288 |
if (!isOutputOpen) |
|
289 |
throw new ClosedChannelException(); |
|
290 |
if (!isConnected()) |
|
291 |
throw new NotYetConnectedException(); |
|
292 |
} |
|
293 |
} |
|
294 |
||
295 |
private void readerCleanup() throws IOException { |
|
296 |
synchronized (stateLock) { |
|
297 |
readerThread = 0; |
|
298 |
if (state == ST_KILLPENDING) |
|
299 |
kill(); |
|
300 |
} |
|
301 |
} |
|
302 |
||
303 |
private void writerCleanup() throws IOException { |
|
304 |
synchronized (stateLock) { |
|
305 |
writerThread = 0; |
|
306 |
if (state == ST_KILLPENDING) |
|
307 |
kill(); |
|
308 |
} |
|
309 |
} |
|
310 |
||
311 |
public int read(ByteBuffer buf) throws IOException { |
|
312 |
||
313 |
if (buf == null) |
|
314 |
throw new NullPointerException(); |
|
315 |
||
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
316 |
readLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
317 |
try { |
2 | 318 |
if (!ensureReadOpen()) |
319 |
return -1; |
|
320 |
int n = 0; |
|
321 |
try { |
|
322 |
||
323 |
// Set up the interruption machinery; see |
|
324 |
// AbstractInterruptibleChannel for details |
|
325 |
// |
|
326 |
begin(); |
|
327 |
||
328 |
synchronized (stateLock) { |
|
329 |
if (!isOpen()) { |
|
330 |
// Either the current thread is already interrupted, so |
|
331 |
// begin() closed the channel, or another thread closed the |
|
332 |
// channel since we checked it a few bytecodes ago. In |
|
333 |
// either case the value returned here is irrelevant since |
|
334 |
// the invocation of end() in the finally block will throw |
|
335 |
// an appropriate exception. |
|
336 |
// |
|
337 |
return 0; |
|
338 |
||
339 |
} |
|
340 |
||
341 |
// Save this thread so that it can be signalled on those |
|
342 |
// platforms that require it |
|
343 |
// |
|
344 |
readerThread = NativeThread.current(); |
|
345 |
} |
|
346 |
||
347 |
// Between the previous test of isOpen() and the return of the |
|
348 |
// IOUtil.read invocation below, this channel might be closed |
|
349 |
// or this thread might be interrupted. We rely upon the |
|
350 |
// implicit synchronization point in the kernel read() call to |
|
351 |
// make sure that the right thing happens. In either case the |
|
352 |
// implCloseSelectableChannel method is ultimately invoked in |
|
353 |
// some other thread, so there are three possibilities: |
|
354 |
// |
|
355 |
// - implCloseSelectableChannel() invokes nd.preClose() |
|
356 |
// before this thread invokes read(), in which case the |
|
357 |
// read returns immediately with either EOF or an error, |
|
358 |
// the latter of which will cause an IOException to be |
|
359 |
// thrown. |
|
360 |
// |
|
361 |
// - implCloseSelectableChannel() invokes nd.preClose() after |
|
362 |
// this thread is blocked in read(). On some operating |
|
363 |
// systems (e.g., Solaris and Windows) this causes the read |
|
364 |
// to return immediately with either EOF or an error |
|
365 |
// indication. |
|
366 |
// |
|
367 |
// - implCloseSelectableChannel() invokes nd.preClose() after |
|
368 |
// this thread is blocked in read() but the operating |
|
369 |
// system (e.g., Linux) doesn't support preemptive close, |
|
370 |
// so implCloseSelectableChannel() proceeds to signal this |
|
371 |
// thread, thereby causing the read to return immediately |
|
372 |
// with IOStatus.INTERRUPTED. |
|
373 |
// |
|
374 |
// In all three cases the invocation of end() in the finally |
|
375 |
// clause will notice that the channel has been closed and |
|
376 |
// throw an appropriate exception (AsynchronousCloseException |
|
377 |
// or ClosedByInterruptException) if necessary. |
|
378 |
// |
|
379 |
// *There is A fourth possibility. implCloseSelectableChannel() |
|
380 |
// invokes nd.preClose(), signals reader/writer thred and quickly |
|
381 |
// moves on to nd.close() in kill(), which does a real close. |
|
382 |
// Then a third thread accepts a new connection, opens file or |
|
383 |
// whatever that causes the released "fd" to be recycled. All |
|
384 |
// above happens just between our last isOpen() check and the |
|
385 |
// next kernel read reached, with the recycled "fd". The solution |
|
386 |
// is to postpone the real kill() if there is a reader or/and |
|
387 |
// writer thread(s) over there "waiting", leave the cleanup/kill |
|
388 |
// to the reader or writer thread. (the preClose() still happens |
|
389 |
// so the connection gets cut off as usual). |
|
390 |
// |
|
391 |
// For socket channels there is the additional wrinkle that |
|
392 |
// asynchronous shutdown works much like asynchronous close, |
|
393 |
// except that the channel is shutdown rather than completely |
|
394 |
// closed. This is analogous to the first two cases above, |
|
395 |
// except that the shutdown operation plays the role of |
|
396 |
// nd.preClose(). |
|
397 |
for (;;) { |
|
16921
e70261f11307
8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
14025
diff
changeset
|
398 |
n = IOUtil.read(fd, buf, -1, nd); |
2 | 399 |
if ((n == IOStatus.INTERRUPTED) && isOpen()) { |
400 |
// The system call was interrupted but the channel |
|
401 |
// is still open, so retry |
|
402 |
continue; |
|
403 |
} |
|
404 |
return IOStatus.normalize(n); |
|
405 |
} |
|
406 |
||
407 |
} finally { |
|
408 |
readerCleanup(); // Clear reader thread |
|
409 |
// The end method, which is defined in our superclass |
|
410 |
// AbstractInterruptibleChannel, resets the interruption |
|
411 |
// machinery. If its argument is true then it returns |
|
412 |
// normally; otherwise it checks the interrupt and open state |
|
413 |
// of this channel and throws an appropriate exception if |
|
414 |
// necessary. |
|
415 |
// |
|
416 |
// So, if we actually managed to do any I/O in the above try |
|
417 |
// block then we pass true to the end method. We also pass |
|
418 |
// true if the channel was in non-blocking mode when the I/O |
|
419 |
// operation was initiated but no data could be transferred; |
|
420 |
// this prevents spurious exceptions from being thrown in the |
|
421 |
// rare event that a channel is closed or a thread is |
|
422 |
// interrupted at the exact moment that a non-blocking I/O |
|
423 |
// request is made. |
|
424 |
// |
|
425 |
end(n > 0 || (n == IOStatus.UNAVAILABLE)); |
|
426 |
||
427 |
// Extra case for socket channels: Asynchronous shutdown |
|
428 |
// |
|
429 |
synchronized (stateLock) { |
|
430 |
if ((n <= 0) && (!isInputOpen)) |
|
431 |
return IOStatus.EOF; |
|
432 |
} |
|
433 |
||
434 |
assert IOStatus.check(n); |
|
435 |
||
436 |
} |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
437 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
438 |
readLock.unlock(); |
2 | 439 |
} |
440 |
} |
|
441 |
||
6301 | 442 |
public long read(ByteBuffer[] dsts, int offset, int length) |
443 |
throws IOException |
|
444 |
{ |
|
445 |
if ((offset < 0) || (length < 0) || (offset > dsts.length - length)) |
|
446 |
throw new IndexOutOfBoundsException(); |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
447 |
readLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
448 |
try { |
2 | 449 |
if (!ensureReadOpen()) |
450 |
return -1; |
|
451 |
long n = 0; |
|
452 |
try { |
|
453 |
begin(); |
|
454 |
synchronized (stateLock) { |
|
455 |
if (!isOpen()) |
|
456 |
return 0; |
|
457 |
readerThread = NativeThread.current(); |
|
458 |
} |
|
459 |
||
460 |
for (;;) { |
|
6301 | 461 |
n = IOUtil.read(fd, dsts, offset, length, nd); |
2 | 462 |
if ((n == IOStatus.INTERRUPTED) && isOpen()) |
463 |
continue; |
|
464 |
return IOStatus.normalize(n); |
|
465 |
} |
|
466 |
} finally { |
|
467 |
readerCleanup(); |
|
468 |
end(n > 0 || (n == IOStatus.UNAVAILABLE)); |
|
469 |
synchronized (stateLock) { |
|
470 |
if ((n <= 0) && (!isInputOpen)) |
|
471 |
return IOStatus.EOF; |
|
472 |
} |
|
473 |
assert IOStatus.check(n); |
|
474 |
} |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
475 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
476 |
readLock.unlock(); |
2 | 477 |
} |
478 |
} |
|
479 |
||
480 |
public int write(ByteBuffer buf) throws IOException { |
|
481 |
if (buf == null) |
|
482 |
throw new NullPointerException(); |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
483 |
writeLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
484 |
try { |
2 | 485 |
ensureWriteOpen(); |
486 |
int n = 0; |
|
487 |
try { |
|
488 |
begin(); |
|
489 |
synchronized (stateLock) { |
|
490 |
if (!isOpen()) |
|
491 |
return 0; |
|
492 |
writerThread = NativeThread.current(); |
|
493 |
} |
|
494 |
for (;;) { |
|
16921
e70261f11307
8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
14025
diff
changeset
|
495 |
n = IOUtil.write(fd, buf, -1, nd); |
2 | 496 |
if ((n == IOStatus.INTERRUPTED) && isOpen()) |
497 |
continue; |
|
498 |
return IOStatus.normalize(n); |
|
499 |
} |
|
500 |
} finally { |
|
501 |
writerCleanup(); |
|
502 |
end(n > 0 || (n == IOStatus.UNAVAILABLE)); |
|
503 |
synchronized (stateLock) { |
|
504 |
if ((n <= 0) && (!isOutputOpen)) |
|
505 |
throw new AsynchronousCloseException(); |
|
506 |
} |
|
507 |
assert IOStatus.check(n); |
|
508 |
} |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
509 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
510 |
writeLock.unlock(); |
2 | 511 |
} |
512 |
} |
|
513 |
||
6301 | 514 |
public long write(ByteBuffer[] srcs, int offset, int length) |
515 |
throws IOException |
|
516 |
{ |
|
517 |
if ((offset < 0) || (length < 0) || (offset > srcs.length - length)) |
|
518 |
throw new IndexOutOfBoundsException(); |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
519 |
writeLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
520 |
try { |
2 | 521 |
ensureWriteOpen(); |
522 |
long n = 0; |
|
523 |
try { |
|
524 |
begin(); |
|
525 |
synchronized (stateLock) { |
|
526 |
if (!isOpen()) |
|
527 |
return 0; |
|
528 |
writerThread = NativeThread.current(); |
|
529 |
} |
|
530 |
for (;;) { |
|
6301 | 531 |
n = IOUtil.write(fd, srcs, offset, length, nd); |
2 | 532 |
if ((n == IOStatus.INTERRUPTED) && isOpen()) |
533 |
continue; |
|
534 |
return IOStatus.normalize(n); |
|
535 |
} |
|
536 |
} finally { |
|
537 |
writerCleanup(); |
|
538 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
539 |
synchronized (stateLock) { |
|
540 |
if ((n <= 0) && (!isOutputOpen)) |
|
541 |
throw new AsynchronousCloseException(); |
|
542 |
} |
|
543 |
assert IOStatus.check(n); |
|
544 |
} |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
545 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
546 |
writeLock.unlock(); |
2 | 547 |
} |
548 |
} |
|
549 |
||
6117
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
550 |
// package-private |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
551 |
int sendOutOfBandData(byte b) throws IOException { |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
552 |
writeLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
553 |
try { |
6117
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
554 |
ensureWriteOpen(); |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
555 |
int n = 0; |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
556 |
try { |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
557 |
begin(); |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
558 |
synchronized (stateLock) { |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
559 |
if (!isOpen()) |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
560 |
return 0; |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
561 |
writerThread = NativeThread.current(); |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
562 |
} |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
563 |
for (;;) { |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
564 |
n = sendOutOfBandData(fd, b); |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
565 |
if ((n == IOStatus.INTERRUPTED) && isOpen()) |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
566 |
continue; |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
567 |
return IOStatus.normalize(n); |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
568 |
} |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
569 |
} finally { |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
570 |
writerCleanup(); |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
571 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
572 |
synchronized (stateLock) { |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
573 |
if ((n <= 0) && (!isOutputOpen)) |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
574 |
throw new AsynchronousCloseException(); |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
575 |
} |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
576 |
assert IOStatus.check(n); |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
577 |
} |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
578 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
579 |
writeLock.unlock(); |
6117
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
580 |
} |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
581 |
} |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
582 |
|
2 | 583 |
protected void implConfigureBlocking(boolean block) throws IOException { |
584 |
IOUtil.configureBlocking(fd, block); |
|
585 |
} |
|
586 |
||
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
587 |
public InetSocketAddress localAddress() { |
2 | 588 |
synchronized (stateLock) { |
589 |
return localAddress; |
|
590 |
} |
|
591 |
} |
|
592 |
||
593 |
public SocketAddress remoteAddress() { |
|
594 |
synchronized (stateLock) { |
|
595 |
return remoteAddress; |
|
596 |
} |
|
597 |
} |
|
598 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
599 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
600 |
public SocketChannel bind(SocketAddress local) throws IOException { |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
601 |
readLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
602 |
try { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
603 |
writeLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
604 |
try { |
2 | 605 |
synchronized (stateLock) { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
606 |
if (!isOpen()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
607 |
throw new ClosedChannelException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
608 |
if (state == ST_PENDING) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
609 |
throw new ConnectionPendingException(); |
2 | 610 |
if (localAddress != null) |
611 |
throw new AlreadyBoundException(); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
612 |
InetSocketAddress isa = (local == null) ? |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
613 |
new InetSocketAddress(0) : Net.checkAddress(local); |
22339 | 614 |
SecurityManager sm = System.getSecurityManager(); |
615 |
if (sm != null) { |
|
616 |
sm.checkListen(isa.getPort()); |
|
617 |
} |
|
2446 | 618 |
NetHooks.beforeTcpBind(fd, isa.getAddress(), isa.getPort()); |
2 | 619 |
Net.bind(fd, isa.getAddress(), isa.getPort()); |
620 |
localAddress = Net.localAddress(fd); |
|
621 |
} |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
622 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
623 |
writeLock.unlock(); |
2 | 624 |
} |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
625 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
626 |
readLock.unlock(); |
2 | 627 |
} |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
628 |
return this; |
2 | 629 |
} |
630 |
||
631 |
public boolean isConnected() { |
|
632 |
synchronized (stateLock) { |
|
633 |
return (state == ST_CONNECTED); |
|
634 |
} |
|
635 |
} |
|
636 |
||
637 |
public boolean isConnectionPending() { |
|
638 |
synchronized (stateLock) { |
|
639 |
return (state == ST_PENDING); |
|
640 |
} |
|
641 |
} |
|
642 |
||
643 |
void ensureOpenAndUnconnected() throws IOException { // package-private |
|
644 |
synchronized (stateLock) { |
|
645 |
if (!isOpen()) |
|
646 |
throw new ClosedChannelException(); |
|
647 |
if (state == ST_CONNECTED) |
|
648 |
throw new AlreadyConnectedException(); |
|
649 |
if (state == ST_PENDING) |
|
650 |
throw new ConnectionPendingException(); |
|
651 |
} |
|
652 |
} |
|
653 |
||
654 |
public boolean connect(SocketAddress sa) throws IOException { |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
655 |
readLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
656 |
try { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
657 |
writeLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
658 |
try { |
2 | 659 |
ensureOpenAndUnconnected(); |
660 |
InetSocketAddress isa = Net.checkAddress(sa); |
|
661 |
SecurityManager sm = System.getSecurityManager(); |
|
662 |
if (sm != null) |
|
663 |
sm.checkConnect(isa.getAddress().getHostAddress(), |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
664 |
isa.getPort()); |
2 | 665 |
synchronized (blockingLock()) { |
666 |
int n = 0; |
|
667 |
try { |
|
668 |
try { |
|
669 |
begin(); |
|
670 |
synchronized (stateLock) { |
|
671 |
if (!isOpen()) { |
|
672 |
return false; |
|
673 |
} |
|
2446 | 674 |
// notify hook only if unbound |
675 |
if (localAddress == null) { |
|
676 |
NetHooks.beforeTcpConnect(fd, |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
677 |
isa.getAddress(), |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
678 |
isa.getPort()); |
2446 | 679 |
} |
2 | 680 |
readerThread = NativeThread.current(); |
681 |
} |
|
682 |
for (;;) { |
|
683 |
InetAddress ia = isa.getAddress(); |
|
684 |
if (ia.isAnyLocalAddress()) |
|
685 |
ia = InetAddress.getLocalHost(); |
|
686 |
n = Net.connect(fd, |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
687 |
ia, |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
688 |
isa.getPort()); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
689 |
if ((n == IOStatus.INTERRUPTED) && isOpen()) |
2 | 690 |
continue; |
691 |
break; |
|
692 |
} |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
693 |
|
2 | 694 |
} finally { |
695 |
readerCleanup(); |
|
696 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
697 |
assert IOStatus.check(n); |
|
698 |
} |
|
699 |
} catch (IOException x) { |
|
700 |
// If an exception was thrown, close the channel after |
|
701 |
// invoking end() so as to avoid bogus |
|
702 |
// AsynchronousCloseExceptions |
|
703 |
close(); |
|
704 |
throw x; |
|
705 |
} |
|
706 |
synchronized (stateLock) { |
|
707 |
remoteAddress = isa; |
|
708 |
if (n > 0) { |
|
709 |
||
710 |
// Connection succeeded; disallow further |
|
711 |
// invocation |
|
712 |
state = ST_CONNECTED; |
|
12683
5c743840dd8a
7096436: (sc) SocketChannel.connect fails on Windows 8 when channel configured non-blocking
khazra
parents:
12440
diff
changeset
|
713 |
if (isOpen()) |
5c743840dd8a
7096436: (sc) SocketChannel.connect fails on Windows 8 when channel configured non-blocking
khazra
parents:
12440
diff
changeset
|
714 |
localAddress = Net.localAddress(fd); |
2 | 715 |
return true; |
716 |
} |
|
717 |
// If nonblocking and no exception then connection |
|
718 |
// pending; disallow another invocation |
|
719 |
if (!isBlocking()) |
|
720 |
state = ST_PENDING; |
|
721 |
else |
|
722 |
assert false; |
|
723 |
} |
|
724 |
} |
|
725 |
return false; |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
726 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
727 |
writeLock.unlock(); |
2 | 728 |
} |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
729 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
730 |
readLock.unlock(); |
2 | 731 |
} |
732 |
} |
|
733 |
||
734 |
public boolean finishConnect() throws IOException { |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
735 |
readLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
736 |
try { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
737 |
writeLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
738 |
try { |
2 | 739 |
synchronized (stateLock) { |
740 |
if (!isOpen()) |
|
741 |
throw new ClosedChannelException(); |
|
742 |
if (state == ST_CONNECTED) |
|
743 |
return true; |
|
744 |
if (state != ST_PENDING) |
|
745 |
throw new NoConnectionPendingException(); |
|
746 |
} |
|
747 |
int n = 0; |
|
748 |
try { |
|
749 |
try { |
|
750 |
begin(); |
|
751 |
synchronized (blockingLock()) { |
|
752 |
synchronized (stateLock) { |
|
753 |
if (!isOpen()) { |
|
754 |
return false; |
|
755 |
} |
|
756 |
readerThread = NativeThread.current(); |
|
757 |
} |
|
758 |
if (!isBlocking()) { |
|
759 |
for (;;) { |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
760 |
n = checkConnect(fd, false); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
761 |
if ((n == IOStatus.INTERRUPTED) && isOpen()) |
2 | 762 |
continue; |
763 |
break; |
|
764 |
} |
|
765 |
} else { |
|
766 |
for (;;) { |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
767 |
n = checkConnect(fd, true); |
2 | 768 |
if (n == 0) { |
769 |
// Loop in case of |
|
770 |
// spurious notifications |
|
771 |
continue; |
|
772 |
} |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
773 |
if ((n == IOStatus.INTERRUPTED) && isOpen()) |
2 | 774 |
continue; |
775 |
break; |
|
776 |
} |
|
777 |
} |
|
778 |
} |
|
779 |
} finally { |
|
780 |
synchronized (stateLock) { |
|
781 |
readerThread = 0; |
|
782 |
if (state == ST_KILLPENDING) { |
|
783 |
kill(); |
|
784 |
// poll()/getsockopt() does not report |
|
785 |
// error (throws exception, with n = 0) |
|
786 |
// on Linux platform after dup2 and |
|
787 |
// signal-wakeup. Force n to 0 so the |
|
788 |
// end() can throw appropriate exception |
|
789 |
n = 0; |
|
790 |
} |
|
791 |
} |
|
792 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
793 |
assert IOStatus.check(n); |
|
794 |
} |
|
795 |
} catch (IOException x) { |
|
796 |
// If an exception was thrown, close the channel after |
|
797 |
// invoking end() so as to avoid bogus |
|
798 |
// AsynchronousCloseExceptions |
|
799 |
close(); |
|
800 |
throw x; |
|
801 |
} |
|
802 |
if (n > 0) { |
|
803 |
synchronized (stateLock) { |
|
804 |
state = ST_CONNECTED; |
|
12683
5c743840dd8a
7096436: (sc) SocketChannel.connect fails on Windows 8 when channel configured non-blocking
khazra
parents:
12440
diff
changeset
|
805 |
if (isOpen()) |
5c743840dd8a
7096436: (sc) SocketChannel.connect fails on Windows 8 when channel configured non-blocking
khazra
parents:
12440
diff
changeset
|
806 |
localAddress = Net.localAddress(fd); |
2 | 807 |
} |
808 |
return true; |
|
809 |
} |
|
810 |
return false; |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
811 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
812 |
writeLock.unlock(); |
2 | 813 |
} |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
814 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
815 |
readLock.unlock(); |
2 | 816 |
} |
817 |
} |
|
818 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
819 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
820 |
public SocketChannel shutdownInput() throws IOException { |
2 | 821 |
synchronized (stateLock) { |
822 |
if (!isOpen()) |
|
823 |
throw new ClosedChannelException(); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
824 |
if (!isConnected()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
825 |
throw new NotYetConnectedException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
826 |
if (isInputOpen) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
827 |
Net.shutdown(fd, Net.SHUT_RD); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
828 |
if (readerThread != 0) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
829 |
NativeThread.signal(readerThread); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
830 |
isInputOpen = false; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
831 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
832 |
return this; |
2 | 833 |
} |
834 |
} |
|
835 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
836 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
837 |
public SocketChannel shutdownOutput() throws IOException { |
2 | 838 |
synchronized (stateLock) { |
839 |
if (!isOpen()) |
|
840 |
throw new ClosedChannelException(); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
841 |
if (!isConnected()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
842 |
throw new NotYetConnectedException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
843 |
if (isOutputOpen) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
844 |
Net.shutdown(fd, Net.SHUT_WR); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
845 |
if (writerThread != 0) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
846 |
NativeThread.signal(writerThread); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
847 |
isOutputOpen = false; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
848 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
849 |
return this; |
2 | 850 |
} |
851 |
} |
|
852 |
||
853 |
public boolean isInputOpen() { |
|
854 |
synchronized (stateLock) { |
|
855 |
return isInputOpen; |
|
856 |
} |
|
857 |
} |
|
858 |
||
859 |
public boolean isOutputOpen() { |
|
860 |
synchronized (stateLock) { |
|
861 |
return isOutputOpen; |
|
862 |
} |
|
863 |
} |
|
864 |
||
865 |
// AbstractInterruptibleChannel synchronizes invocations of this method |
|
866 |
// using AbstractInterruptibleChannel.closeLock, and also ensures that this |
|
867 |
// method is only ever invoked once. Before we get to this method, isOpen |
|
868 |
// (which is volatile) will have been set to false. |
|
869 |
// |
|
870 |
protected void implCloseSelectableChannel() throws IOException { |
|
871 |
synchronized (stateLock) { |
|
872 |
isInputOpen = false; |
|
873 |
isOutputOpen = false; |
|
874 |
||
875 |
// Close the underlying file descriptor and dup it to a known fd |
|
876 |
// that's already closed. This prevents other operations on this |
|
877 |
// channel from using the old fd, which might be recycled in the |
|
878 |
// meantime and allocated to an entirely different channel. |
|
879 |
// |
|
12440
2689ca179e22
7118373: (se) Potential leak file descriptor when deregistrating at around the same time as an async close
robm
parents:
9679
diff
changeset
|
880 |
if (state != ST_KILLED) |
2689ca179e22
7118373: (se) Potential leak file descriptor when deregistrating at around the same time as an async close
robm
parents:
9679
diff
changeset
|
881 |
nd.preClose(fd); |
2 | 882 |
|
883 |
// Signal native threads, if needed. If a target thread is not |
|
884 |
// currently blocked in an I/O operation then no harm is done since |
|
885 |
// the signal handler doesn't actually do anything. |
|
886 |
// |
|
887 |
if (readerThread != 0) |
|
888 |
NativeThread.signal(readerThread); |
|
889 |
||
890 |
if (writerThread != 0) |
|
891 |
NativeThread.signal(writerThread); |
|
892 |
||
893 |
// If this channel is not registered then it's safe to close the fd |
|
894 |
// immediately since we know at this point that no thread is |
|
895 |
// blocked in an I/O operation upon the channel and, since the |
|
896 |
// channel is marked closed, no thread will start another such |
|
897 |
// operation. If this channel is registered then we don't close |
|
898 |
// the fd since it might be in use by a selector. In that case |
|
899 |
// closing this channel caused its keys to be cancelled, so the |
|
900 |
// last selector to deregister a key for this channel will invoke |
|
901 |
// kill() to close the fd. |
|
902 |
// |
|
903 |
if (!isRegistered()) |
|
904 |
kill(); |
|
905 |
} |
|
906 |
} |
|
907 |
||
908 |
public void kill() throws IOException { |
|
909 |
synchronized (stateLock) { |
|
910 |
if (state == ST_KILLED) |
|
911 |
return; |
|
912 |
if (state == ST_UNINITIALIZED) { |
|
913 |
state = ST_KILLED; |
|
914 |
return; |
|
915 |
} |
|
916 |
assert !isOpen() && !isRegistered(); |
|
917 |
||
918 |
// Postpone the kill if there is a waiting reader |
|
919 |
// or writer thread. See the comments in read() for |
|
920 |
// more detailed explanation. |
|
921 |
if (readerThread == 0 && writerThread == 0) { |
|
922 |
nd.close(fd); |
|
923 |
state = ST_KILLED; |
|
924 |
} else { |
|
925 |
state = ST_KILLPENDING; |
|
926 |
} |
|
927 |
} |
|
928 |
} |
|
929 |
||
930 |
/** |
|
931 |
* Translates native poll revent ops into a ready operation ops |
|
932 |
*/ |
|
933 |
public boolean translateReadyOps(int ops, int initialOps, |
|
934 |
SelectionKeyImpl sk) { |
|
935 |
int intOps = sk.nioInterestOps(); // Do this just once, it synchronizes |
|
936 |
int oldOps = sk.nioReadyOps(); |
|
937 |
int newOps = initialOps; |
|
938 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
939 |
if ((ops & Net.POLLNVAL) != 0) { |
2 | 940 |
// This should only happen if this channel is pre-closed while a |
941 |
// selection operation is in progress |
|
942 |
// ## Throw an error if this channel has not been pre-closed |
|
943 |
return false; |
|
944 |
} |
|
945 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
946 |
if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) { |
2 | 947 |
newOps = intOps; |
948 |
sk.nioReadyOps(newOps); |
|
949 |
return (newOps & ~oldOps) != 0; |
|
950 |
} |
|
951 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
952 |
if (((ops & Net.POLLIN) != 0) && |
2 | 953 |
((intOps & SelectionKey.OP_READ) != 0) && |
954 |
(state == ST_CONNECTED)) |
|
955 |
newOps |= SelectionKey.OP_READ; |
|
956 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
957 |
if (((ops & Net.POLLCONN) != 0) && |
2 | 958 |
((intOps & SelectionKey.OP_CONNECT) != 0) && |
959 |
((state == ST_UNCONNECTED) || (state == ST_PENDING))) { |
|
960 |
newOps |= SelectionKey.OP_CONNECT; |
|
961 |
} |
|
962 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
963 |
if (((ops & Net.POLLOUT) != 0) && |
2 | 964 |
((intOps & SelectionKey.OP_WRITE) != 0) && |
965 |
(state == ST_CONNECTED)) |
|
966 |
newOps |= SelectionKey.OP_WRITE; |
|
967 |
||
968 |
sk.nioReadyOps(newOps); |
|
969 |
return (newOps & ~oldOps) != 0; |
|
970 |
} |
|
971 |
||
972 |
public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) { |
|
973 |
return translateReadyOps(ops, sk.nioReadyOps(), sk); |
|
974 |
} |
|
975 |
||
976 |
public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) { |
|
977 |
return translateReadyOps(ops, 0, sk); |
|
978 |
} |
|
979 |
||
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
980 |
// package-private |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
981 |
int poll(int events, long timeout) throws IOException { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
982 |
assert Thread.holdsLock(blockingLock()) && !isBlocking(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
983 |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
984 |
readLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
985 |
try { |
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
986 |
int n = 0; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
987 |
try { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
988 |
begin(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
989 |
synchronized (stateLock) { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
990 |
if (!isOpen()) |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
991 |
return 0; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
992 |
readerThread = NativeThread.current(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
993 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
994 |
n = Net.poll(fd, events, timeout); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
995 |
} finally { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
996 |
readerCleanup(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
997 |
end(n > 0); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
998 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
999 |
return n; |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
1000 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
1001 |
readLock.unlock(); |
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
1002 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
1003 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
1004 |
|
2 | 1005 |
/** |
1006 |
* Translates an interest operation set into a native poll event set |
|
1007 |
*/ |
|
1008 |
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) { |
|
1009 |
int newOps = 0; |
|
1010 |
if ((ops & SelectionKey.OP_READ) != 0) |
|
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1011 |
newOps |= Net.POLLIN; |
2 | 1012 |
if ((ops & SelectionKey.OP_WRITE) != 0) |
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1013 |
newOps |= Net.POLLOUT; |
2 | 1014 |
if ((ops & SelectionKey.OP_CONNECT) != 0) |
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1015 |
newOps |= Net.POLLCONN; |
2 | 1016 |
sk.selector.putEventOps(sk, newOps); |
1017 |
} |
|
1018 |
||
1019 |
public FileDescriptor getFD() { |
|
1020 |
return fd; |
|
1021 |
} |
|
1022 |
||
1023 |
public int getFDVal() { |
|
1024 |
return fdVal; |
|
1025 |
} |
|
1026 |
||
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
1027 |
@Override |
2 | 1028 |
public String toString() { |
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
23879
diff
changeset
|
1029 |
StringBuilder sb = new StringBuilder(); |
2 | 1030 |
sb.append(this.getClass().getSuperclass().getName()); |
1031 |
sb.append('['); |
|
1032 |
if (!isOpen()) |
|
1033 |
sb.append("closed"); |
|
1034 |
else { |
|
1035 |
synchronized (stateLock) { |
|
1036 |
switch (state) { |
|
1037 |
case ST_UNCONNECTED: |
|
1038 |
sb.append("unconnected"); |
|
1039 |
break; |
|
1040 |
case ST_PENDING: |
|
1041 |
sb.append("connection-pending"); |
|
1042 |
break; |
|
1043 |
case ST_CONNECTED: |
|
1044 |
sb.append("connected"); |
|
1045 |
if (!isInputOpen) |
|
1046 |
sb.append(" ishut"); |
|
1047 |
if (!isOutputOpen) |
|
1048 |
sb.append(" oshut"); |
|
1049 |
break; |
|
1050 |
} |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
1051 |
InetSocketAddress addr = localAddress(); |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
1052 |
if (addr != null) { |
2 | 1053 |
sb.append(" local="); |
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
1054 |
sb.append(Net.getRevealedLocalAddressAsString(addr)); |
2 | 1055 |
} |
1056 |
if (remoteAddress() != null) { |
|
1057 |
sb.append(" remote="); |
|
1058 |
sb.append(remoteAddress().toString()); |
|
1059 |
} |
|
1060 |
} |
|
1061 |
} |
|
1062 |
sb.append(']'); |
|
1063 |
return sb.toString(); |
|
1064 |
} |
|
1065 |
||
1066 |
||
1067 |
// -- Native methods -- |
|
1068 |
||
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
1069 |
private static native int checkConnect(FileDescriptor fd, boolean block) |
2 | 1070 |
throws IOException; |
1071 |
||
6117
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
1072 |
private static native int sendOutOfBandData(FileDescriptor fd, byte data) |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
1073 |
throws IOException; |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
1074 |
|
2 | 1075 |
static { |
19607
bee007586d06
8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents:
18212
diff
changeset
|
1076 |
IOUtil.load(); |
2 | 1077 |
nd = new SocketDispatcher(); |
1078 |
} |
|
1079 |
||
1080 |
} |