author | simonis |
Mon, 20 Jan 2014 09:24:25 +0100 | |
changeset 22604 | 9b394795e216 |
parent 19607 | bee007586d06 |
child 22607 | ba232b417248 |
permissions | -rw-r--r-- |
2542 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
12559
diff
changeset
|
2 |
* Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
2542 | 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 |
2542 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2542 | 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. |
|
2542 | 24 |
*/ |
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10137
diff
changeset
|
25 |
package sun.nio.ch.sctp; |
2542 | 26 |
|
27 |
import java.net.InetAddress; |
|
28 |
import java.net.SocketAddress; |
|
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
29 |
import java.net.SocketException; |
2542 | 30 |
import java.net.InetSocketAddress; |
31 |
import java.io.FileDescriptor; |
|
32 |
import java.io.IOException; |
|
33 |
import java.util.Collections; |
|
34 |
import java.util.Set; |
|
35 |
import java.util.HashSet; |
|
36 |
import java.nio.ByteBuffer; |
|
37 |
import java.nio.channels.SelectionKey; |
|
38 |
import java.nio.channels.ClosedChannelException; |
|
39 |
import java.nio.channels.ConnectionPendingException; |
|
40 |
import java.nio.channels.NoConnectionPendingException; |
|
41 |
import java.nio.channels.AlreadyConnectedException; |
|
42 |
import java.nio.channels.NotYetBoundException; |
|
43 |
import java.nio.channels.NotYetConnectedException; |
|
44 |
import java.nio.channels.spi.SelectorProvider; |
|
45 |
import com.sun.nio.sctp.AbstractNotificationHandler; |
|
46 |
import com.sun.nio.sctp.Association; |
|
47 |
import com.sun.nio.sctp.AssociationChangeNotification; |
|
48 |
import com.sun.nio.sctp.HandlerResult; |
|
49 |
import com.sun.nio.sctp.IllegalReceiveException; |
|
50 |
import com.sun.nio.sctp.InvalidStreamException; |
|
51 |
import com.sun.nio.sctp.IllegalUnbindException; |
|
52 |
import com.sun.nio.sctp.MessageInfo; |
|
53 |
import com.sun.nio.sctp.NotificationHandler; |
|
54 |
import com.sun.nio.sctp.SctpChannel; |
|
55 |
import com.sun.nio.sctp.SctpSocketOption; |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10137
diff
changeset
|
56 |
import sun.nio.ch.DirectBuffer; |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10137
diff
changeset
|
57 |
import sun.nio.ch.IOStatus; |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10137
diff
changeset
|
58 |
import sun.nio.ch.IOUtil; |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10137
diff
changeset
|
59 |
import sun.nio.ch.NativeThread; |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10137
diff
changeset
|
60 |
import sun.nio.ch.Net; |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10137
diff
changeset
|
61 |
import sun.nio.ch.PollArrayWrapper; |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10137
diff
changeset
|
62 |
import sun.nio.ch.SelChImpl; |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10137
diff
changeset
|
63 |
import sun.nio.ch.SelectionKeyImpl; |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10137
diff
changeset
|
64 |
import sun.nio.ch.Util; |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
65 |
import static com.sun.nio.sctp.SctpStandardSocketOptions.*; |
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10137
diff
changeset
|
66 |
import static sun.nio.ch.sctp.ResultContainer.SEND_FAILED; |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10137
diff
changeset
|
67 |
import static sun.nio.ch.sctp.ResultContainer.ASSOCIATION_CHANGED; |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10137
diff
changeset
|
68 |
import static sun.nio.ch.sctp.ResultContainer.PEER_ADDRESS_CHANGED; |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10137
diff
changeset
|
69 |
import static sun.nio.ch.sctp.ResultContainer.SHUTDOWN; |
2542 | 70 |
|
71 |
/** |
|
72 |
* An implementation of an SctpChannel |
|
73 |
*/ |
|
74 |
public class SctpChannelImpl extends SctpChannel |
|
75 |
implements SelChImpl |
|
76 |
{ |
|
77 |
private final FileDescriptor fd; |
|
78 |
||
79 |
private final int fdVal; |
|
80 |
||
81 |
/* IDs of native threads doing send and receivess, for signalling */ |
|
82 |
private volatile long receiverThread = 0; |
|
83 |
private volatile long senderThread = 0; |
|
84 |
||
85 |
/* Lock held by current receiving or connecting thread */ |
|
86 |
private final Object receiveLock = new Object(); |
|
87 |
||
88 |
/* Lock held by current sending or connecting thread */ |
|
89 |
private final Object sendLock = new Object(); |
|
90 |
||
91 |
private final ThreadLocal<Boolean> receiveInvoked = |
|
92 |
new ThreadLocal<Boolean>() { |
|
93 |
@Override protected Boolean initialValue() { |
|
94 |
return Boolean.FALSE; |
|
95 |
} |
|
96 |
}; |
|
97 |
||
98 |
/* Lock held by any thread that modifies the state fields declared below |
|
99 |
DO NOT invoke a blocking I/O operation while holding this lock! */ |
|
100 |
private final Object stateLock = new Object(); |
|
101 |
||
102 |
private enum ChannelState { |
|
103 |
UNINITIALIZED, |
|
104 |
UNCONNECTED, |
|
105 |
PENDING, |
|
106 |
CONNECTED, |
|
107 |
KILLPENDING, |
|
108 |
KILLED, |
|
109 |
} |
|
110 |
/* -- The following fields are protected by stateLock -- */ |
|
111 |
private ChannelState state = ChannelState.UNINITIALIZED; |
|
112 |
||
113 |
/* Binding; Once bound the port will remain constant. */ |
|
114 |
int port = -1; |
|
115 |
private HashSet<InetSocketAddress> localAddresses = new HashSet<InetSocketAddress>(); |
|
116 |
/* Has the channel been bound to the wildcard address */ |
|
117 |
private boolean wildcard; /* false */ |
|
118 |
//private InetSocketAddress remoteAddress = null; |
|
119 |
||
120 |
/* Input/Output open */ |
|
121 |
private boolean readyToConnect; |
|
122 |
||
123 |
/* Shutdown */ |
|
124 |
private boolean isShutdown; |
|
125 |
||
126 |
private Association association; |
|
127 |
||
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9679
diff
changeset
|
128 |
private Set<SocketAddress> remoteAddresses = Collections.emptySet(); |
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
129 |
|
2542 | 130 |
/* -- End of fields protected by stateLock -- */ |
131 |
||
132 |
/** |
|
133 |
* Constructor for normal connecting sockets |
|
134 |
*/ |
|
135 |
public SctpChannelImpl(SelectorProvider provider) throws IOException { |
|
136 |
//TODO: update provider remove public modifier |
|
137 |
super(provider); |
|
138 |
this.fd = SctpNet.socket(true); |
|
139 |
this.fdVal = IOUtil.fdVal(fd); |
|
140 |
this.state = ChannelState.UNCONNECTED; |
|
141 |
} |
|
142 |
||
143 |
/** |
|
144 |
* Constructor for sockets obtained from server sockets |
|
145 |
*/ |
|
146 |
public SctpChannelImpl(SelectorProvider provider, FileDescriptor fd) |
|
147 |
throws IOException { |
|
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
148 |
this(provider, fd, null); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
149 |
} |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
150 |
|
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
151 |
/** |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
152 |
* Constructor for sockets obtained from branching |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
153 |
*/ |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
154 |
public SctpChannelImpl(SelectorProvider provider, |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
155 |
FileDescriptor fd, |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
156 |
Association association) |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
157 |
throws IOException { |
2542 | 158 |
super(provider); |
159 |
this.fd = fd; |
|
160 |
this.fdVal = IOUtil.fdVal(fd); |
|
161 |
this.state = ChannelState.CONNECTED; |
|
162 |
port = (Net.localAddress(fd)).getPort(); |
|
163 |
||
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
164 |
if (association != null) { /* branched */ |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
165 |
this.association = association; |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
166 |
} else { /* obtained from server channel */ |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
167 |
/* Receive COMM_UP */ |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
168 |
ByteBuffer buf = Util.getTemporaryDirectBuffer(50); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
169 |
try { |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
170 |
receive(buf, null, null, true); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
171 |
} finally { |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
172 |
Util.releaseTemporaryDirectBuffer(buf); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
173 |
} |
2542 | 174 |
} |
175 |
} |
|
176 |
||
177 |
/** |
|
178 |
* Binds the channel's socket to a local address. |
|
179 |
*/ |
|
180 |
@Override |
|
181 |
public SctpChannel bind(SocketAddress local) throws IOException { |
|
182 |
synchronized (receiveLock) { |
|
183 |
synchronized (sendLock) { |
|
184 |
synchronized (stateLock) { |
|
185 |
ensureOpenAndUnconnected(); |
|
186 |
if (isBound()) |
|
4669
11d1dbd3598d
6915313: Reorganize implementation to make it more feasible to port to JDK6
chegar
parents:
3320
diff
changeset
|
187 |
SctpNet.throwAlreadyBoundException(); |
2542 | 188 |
InetSocketAddress isa = (local == null) ? |
189 |
new InetSocketAddress(0) : Net.checkAddress(local); |
|
190 |
Net.bind(fd, isa.getAddress(), isa.getPort()); |
|
191 |
InetSocketAddress boundIsa = Net.localAddress(fd); |
|
192 |
port = boundIsa.getPort(); |
|
193 |
localAddresses.add(isa); |
|
194 |
if (isa.getAddress().isAnyLocalAddress()) |
|
195 |
wildcard = true; |
|
196 |
} |
|
197 |
} |
|
198 |
} |
|
199 |
return this; |
|
200 |
} |
|
201 |
||
202 |
@Override |
|
203 |
public SctpChannel bindAddress(InetAddress address) |
|
204 |
throws IOException { |
|
205 |
bindUnbindAddress(address, true); |
|
206 |
localAddresses.add(new InetSocketAddress(address, port)); |
|
207 |
return this; |
|
208 |
} |
|
209 |
||
210 |
@Override |
|
211 |
public SctpChannel unbindAddress(InetAddress address) |
|
212 |
throws IOException { |
|
213 |
bindUnbindAddress(address, false); |
|
214 |
localAddresses.remove(new InetSocketAddress(address, port)); |
|
215 |
return this; |
|
216 |
} |
|
217 |
||
218 |
private SctpChannel bindUnbindAddress(InetAddress address, boolean add) |
|
219 |
throws IOException { |
|
220 |
if (address == null) |
|
221 |
throw new IllegalArgumentException(); |
|
222 |
||
223 |
synchronized (receiveLock) { |
|
224 |
synchronized (sendLock) { |
|
225 |
synchronized (stateLock) { |
|
226 |
if (!isOpen()) |
|
227 |
throw new ClosedChannelException(); |
|
228 |
if (!isBound()) |
|
229 |
throw new NotYetBoundException(); |
|
230 |
if (wildcard) |
|
231 |
throw new IllegalStateException( |
|
232 |
"Cannot add or remove addresses from a channel that is bound to the wildcard address"); |
|
233 |
if (address.isAnyLocalAddress()) |
|
234 |
throw new IllegalArgumentException( |
|
235 |
"Cannot add or remove the wildcard address"); |
|
236 |
if (add) { |
|
237 |
for (InetSocketAddress addr : localAddresses) { |
|
238 |
if (addr.getAddress().equals(address)) { |
|
4669
11d1dbd3598d
6915313: Reorganize implementation to make it more feasible to port to JDK6
chegar
parents:
3320
diff
changeset
|
239 |
SctpNet.throwAlreadyBoundException(); |
2542 | 240 |
} |
241 |
} |
|
242 |
} else { /*removing */ |
|
243 |
/* Verify that there is more than one address |
|
244 |
* and that address is already bound */ |
|
245 |
if (localAddresses.size() <= 1) |
|
246 |
throw new IllegalUnbindException("Cannot remove address from a channel with only one address bound"); |
|
247 |
boolean foundAddress = false; |
|
248 |
for (InetSocketAddress addr : localAddresses) { |
|
249 |
if (addr.getAddress().equals(address)) { |
|
250 |
foundAddress = true; |
|
251 |
break; |
|
252 |
} |
|
253 |
} |
|
254 |
if (!foundAddress ) |
|
255 |
throw new IllegalUnbindException("Cannot remove address from a channel that is not bound to that address"); |
|
256 |
} |
|
257 |
||
258 |
SctpNet.bindx(fdVal, new InetAddress[]{address}, port, add); |
|
259 |
||
260 |
/* Update our internal Set to reflect the addition/removal */ |
|
261 |
if (add) |
|
262 |
localAddresses.add(new InetSocketAddress(address, port)); |
|
263 |
else { |
|
264 |
for (InetSocketAddress addr : localAddresses) { |
|
265 |
if (addr.getAddress().equals(address)) { |
|
266 |
localAddresses.remove(addr); |
|
267 |
break; |
|
268 |
} |
|
269 |
} |
|
270 |
} |
|
271 |
} |
|
272 |
} |
|
273 |
} |
|
274 |
return this; |
|
275 |
} |
|
276 |
||
277 |
private boolean isBound() { |
|
278 |
synchronized (stateLock) { |
|
279 |
return port == -1 ? false : true; |
|
280 |
} |
|
281 |
} |
|
282 |
||
283 |
private boolean isConnected() { |
|
284 |
synchronized (stateLock) { |
|
285 |
return (state == ChannelState.CONNECTED); |
|
286 |
} |
|
287 |
} |
|
288 |
||
289 |
private void ensureOpenAndUnconnected() throws IOException { |
|
290 |
synchronized (stateLock) { |
|
291 |
if (!isOpen()) |
|
292 |
throw new ClosedChannelException(); |
|
293 |
if (isConnected()) |
|
294 |
throw new AlreadyConnectedException(); |
|
295 |
if (state == ChannelState.PENDING) |
|
296 |
throw new ConnectionPendingException(); |
|
297 |
} |
|
298 |
} |
|
299 |
||
300 |
private boolean ensureReceiveOpen() throws ClosedChannelException { |
|
301 |
synchronized (stateLock) { |
|
302 |
if (!isOpen()) |
|
303 |
throw new ClosedChannelException(); |
|
304 |
if (!isConnected()) |
|
305 |
throw new NotYetConnectedException(); |
|
306 |
else |
|
307 |
return true; |
|
308 |
} |
|
309 |
} |
|
310 |
||
311 |
private void ensureSendOpen() throws ClosedChannelException { |
|
312 |
synchronized (stateLock) { |
|
313 |
if (!isOpen()) |
|
314 |
throw new ClosedChannelException(); |
|
315 |
if (isShutdown) |
|
316 |
throw new ClosedChannelException(); |
|
317 |
if (!isConnected()) |
|
318 |
throw new NotYetConnectedException(); |
|
319 |
} |
|
320 |
} |
|
321 |
||
322 |
private void receiverCleanup() throws IOException { |
|
323 |
synchronized (stateLock) { |
|
324 |
receiverThread = 0; |
|
325 |
if (state == ChannelState.KILLPENDING) |
|
326 |
kill(); |
|
327 |
} |
|
328 |
} |
|
329 |
||
330 |
private void senderCleanup() throws IOException { |
|
331 |
synchronized (stateLock) { |
|
332 |
senderThread = 0; |
|
333 |
if (state == ChannelState.KILLPENDING) |
|
334 |
kill(); |
|
335 |
} |
|
336 |
} |
|
337 |
||
338 |
@Override |
|
339 |
public Association association() throws ClosedChannelException { |
|
340 |
synchronized (stateLock) { |
|
341 |
if (!isOpen()) |
|
342 |
throw new ClosedChannelException(); |
|
343 |
if (!isConnected()) |
|
344 |
return null; |
|
345 |
||
346 |
return association; |
|
347 |
} |
|
348 |
} |
|
349 |
||
350 |
@Override |
|
351 |
public boolean connect(SocketAddress endpoint) throws IOException { |
|
352 |
synchronized (receiveLock) { |
|
353 |
synchronized (sendLock) { |
|
354 |
ensureOpenAndUnconnected(); |
|
355 |
InetSocketAddress isa = Net.checkAddress(endpoint); |
|
356 |
SecurityManager sm = System.getSecurityManager(); |
|
357 |
if (sm != null) |
|
358 |
sm.checkConnect(isa.getAddress().getHostAddress(), |
|
359 |
isa.getPort()); |
|
360 |
synchronized (blockingLock()) { |
|
361 |
int n = 0; |
|
362 |
try { |
|
363 |
try { |
|
364 |
begin(); |
|
365 |
synchronized (stateLock) { |
|
366 |
if (!isOpen()) { |
|
367 |
return false; |
|
368 |
} |
|
369 |
receiverThread = NativeThread.current(); |
|
370 |
} |
|
371 |
for (;;) { |
|
372 |
InetAddress ia = isa.getAddress(); |
|
373 |
if (ia.isAnyLocalAddress()) |
|
374 |
ia = InetAddress.getLocalHost(); |
|
4669
11d1dbd3598d
6915313: Reorganize implementation to make it more feasible to port to JDK6
chegar
parents:
3320
diff
changeset
|
375 |
n = SctpNet.connect(fdVal, ia, isa.getPort()); |
2542 | 376 |
if ( (n == IOStatus.INTERRUPTED) |
377 |
&& isOpen()) |
|
378 |
continue; |
|
379 |
break; |
|
380 |
} |
|
381 |
} finally { |
|
382 |
receiverCleanup(); |
|
383 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
384 |
assert IOStatus.check(n); |
|
385 |
} |
|
386 |
} catch (IOException x) { |
|
387 |
/* If an exception was thrown, close the channel after |
|
388 |
* invoking end() so as to avoid bogus |
|
389 |
* AsynchronousCloseExceptions */ |
|
390 |
close(); |
|
391 |
throw x; |
|
392 |
} |
|
393 |
||
394 |
if (n > 0) { |
|
395 |
synchronized (stateLock) { |
|
396 |
/* Connection succeeded */ |
|
397 |
state = ChannelState.CONNECTED; |
|
398 |
if (!isBound()) { |
|
399 |
InetSocketAddress boundIsa = |
|
400 |
Net.localAddress(fd); |
|
401 |
port = boundIsa.getPort(); |
|
402 |
} |
|
403 |
||
404 |
/* Receive COMM_UP */ |
|
405 |
ByteBuffer buf = Util.getTemporaryDirectBuffer(50); |
|
406 |
try { |
|
407 |
receive(buf, null, null, true); |
|
408 |
} finally { |
|
409 |
Util.releaseTemporaryDirectBuffer(buf); |
|
410 |
} |
|
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
411 |
|
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
412 |
/* cache remote addresses */ |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
413 |
try { |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
414 |
remoteAddresses = getRemoteAddresses(); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
415 |
} catch (IOException unused) { /* swallow exception */ } |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
416 |
|
2542 | 417 |
return true; |
418 |
} |
|
419 |
} else { |
|
420 |
synchronized (stateLock) { |
|
421 |
/* If nonblocking and no exception then connection |
|
422 |
* pending; disallow another invocation */ |
|
423 |
if (!isBlocking()) |
|
424 |
state = ChannelState.PENDING; |
|
425 |
else |
|
426 |
assert false; |
|
427 |
} |
|
428 |
} |
|
429 |
} |
|
430 |
return false; |
|
431 |
} |
|
432 |
} |
|
433 |
} |
|
434 |
||
435 |
@Override |
|
436 |
public boolean connect(SocketAddress endpoint, |
|
437 |
int maxOutStreams, |
|
438 |
int maxInStreams) |
|
439 |
throws IOException { |
|
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
440 |
ensureOpenAndUnconnected(); |
2542 | 441 |
return setOption(SCTP_INIT_MAXSTREAMS, InitMaxStreams. |
442 |
create(maxInStreams, maxOutStreams)).connect(endpoint); |
|
443 |
||
444 |
} |
|
445 |
||
446 |
@Override |
|
447 |
public boolean isConnectionPending() { |
|
448 |
synchronized (stateLock) { |
|
449 |
return (state == ChannelState.PENDING); |
|
450 |
} |
|
451 |
} |
|
452 |
||
453 |
@Override |
|
454 |
public boolean finishConnect() throws IOException { |
|
455 |
synchronized (receiveLock) { |
|
456 |
synchronized (sendLock) { |
|
457 |
synchronized (stateLock) { |
|
458 |
if (!isOpen()) |
|
459 |
throw new ClosedChannelException(); |
|
460 |
if (isConnected()) |
|
461 |
return true; |
|
462 |
if (state != ChannelState.PENDING) |
|
463 |
throw new NoConnectionPendingException(); |
|
464 |
} |
|
465 |
int n = 0; |
|
466 |
try { |
|
467 |
try { |
|
468 |
begin(); |
|
469 |
synchronized (blockingLock()) { |
|
470 |
synchronized (stateLock) { |
|
471 |
if (!isOpen()) { |
|
472 |
return false; |
|
473 |
} |
|
474 |
receiverThread = NativeThread.current(); |
|
475 |
} |
|
476 |
if (!isBlocking()) { |
|
477 |
for (;;) { |
|
478 |
n = checkConnect(fd, false, readyToConnect); |
|
479 |
if ( (n == IOStatus.INTERRUPTED) |
|
480 |
&& isOpen()) |
|
481 |
continue; |
|
482 |
break; |
|
483 |
} |
|
484 |
} else { |
|
485 |
for (;;) { |
|
486 |
n = checkConnect(fd, true, readyToConnect); |
|
487 |
if (n == 0) { |
|
488 |
// Loop in case of |
|
489 |
// spurious notifications |
|
490 |
continue; |
|
491 |
} |
|
492 |
if ( (n == IOStatus.INTERRUPTED) |
|
493 |
&& isOpen()) |
|
494 |
continue; |
|
495 |
break; |
|
496 |
} |
|
497 |
} |
|
498 |
} |
|
499 |
} finally { |
|
500 |
synchronized (stateLock) { |
|
501 |
receiverThread = 0; |
|
502 |
if (state == ChannelState.KILLPENDING) { |
|
503 |
kill(); |
|
504 |
/* poll()/getsockopt() does not report |
|
505 |
* error (throws exception, with n = 0) |
|
506 |
* on Linux platform after dup2 and |
|
507 |
* signal-wakeup. Force n to 0 so the |
|
508 |
* end() can throw appropriate exception */ |
|
509 |
n = 0; |
|
510 |
} |
|
511 |
} |
|
512 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
513 |
assert IOStatus.check(n); |
|
514 |
} |
|
515 |
} catch (IOException x) { |
|
516 |
/* If an exception was thrown, close the channel after |
|
517 |
* invoking end() so as to avoid bogus |
|
518 |
* AsynchronousCloseExceptions */ |
|
519 |
close(); |
|
520 |
throw x; |
|
521 |
} |
|
522 |
||
523 |
if (n > 0) { |
|
524 |
synchronized (stateLock) { |
|
525 |
state = ChannelState.CONNECTED; |
|
526 |
if (!isBound()) { |
|
527 |
InetSocketAddress boundIsa = |
|
528 |
Net.localAddress(fd); |
|
529 |
port = boundIsa.getPort(); |
|
530 |
} |
|
531 |
||
532 |
/* Receive COMM_UP */ |
|
533 |
ByteBuffer buf = Util.getTemporaryDirectBuffer(50); |
|
534 |
try { |
|
535 |
receive(buf, null, null, true); |
|
536 |
} finally { |
|
537 |
Util.releaseTemporaryDirectBuffer(buf); |
|
538 |
} |
|
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
539 |
|
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
540 |
/* cache remote addresses */ |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
541 |
try { |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
542 |
remoteAddresses = getRemoteAddresses(); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
543 |
} catch (IOException unused) { /* swallow exception */ } |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
544 |
|
2542 | 545 |
return true; |
546 |
} |
|
547 |
} |
|
548 |
} |
|
549 |
} |
|
550 |
return false; |
|
551 |
} |
|
552 |
||
553 |
@Override |
|
554 |
protected void implConfigureBlocking(boolean block) throws IOException { |
|
555 |
IOUtil.configureBlocking(fd, block); |
|
556 |
} |
|
557 |
||
558 |
@Override |
|
559 |
public void implCloseSelectableChannel() throws IOException { |
|
560 |
synchronized (stateLock) { |
|
4669
11d1dbd3598d
6915313: Reorganize implementation to make it more feasible to port to JDK6
chegar
parents:
3320
diff
changeset
|
561 |
SctpNet.preClose(fdVal); |
2542 | 562 |
|
563 |
if (receiverThread != 0) |
|
564 |
NativeThread.signal(receiverThread); |
|
565 |
||
566 |
if (senderThread != 0) |
|
567 |
NativeThread.signal(senderThread); |
|
568 |
||
569 |
if (!isRegistered()) |
|
570 |
kill(); |
|
571 |
} |
|
572 |
} |
|
573 |
||
574 |
@Override |
|
575 |
public FileDescriptor getFD() { |
|
576 |
return fd; |
|
577 |
} |
|
578 |
||
579 |
@Override |
|
580 |
public int getFDVal() { |
|
581 |
return fdVal; |
|
582 |
} |
|
583 |
||
584 |
/** |
|
585 |
* Translates native poll revent ops into a ready operation ops |
|
586 |
*/ |
|
587 |
private boolean translateReadyOps(int ops, int initialOps, SelectionKeyImpl sk) { |
|
588 |
int intOps = sk.nioInterestOps(); |
|
589 |
int oldOps = sk.nioReadyOps(); |
|
590 |
int newOps = initialOps; |
|
591 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
592 |
if ((ops & Net.POLLNVAL) != 0) { |
2542 | 593 |
/* This should only happen if this channel is pre-closed while a |
594 |
* selection operation is in progress |
|
595 |
* ## Throw an error if this channel has not been pre-closed */ |
|
596 |
return false; |
|
597 |
} |
|
598 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
599 |
if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) { |
2542 | 600 |
newOps = intOps; |
601 |
sk.nioReadyOps(newOps); |
|
602 |
/* No need to poll again in checkConnect, |
|
603 |
* the error will be detected there */ |
|
604 |
readyToConnect = true; |
|
605 |
return (newOps & ~oldOps) != 0; |
|
606 |
} |
|
607 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
608 |
if (((ops & Net.POLLIN) != 0) && |
2542 | 609 |
((intOps & SelectionKey.OP_READ) != 0) && |
610 |
isConnected()) |
|
611 |
newOps |= SelectionKey.OP_READ; |
|
612 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
613 |
if (((ops & Net.POLLCONN) != 0) && |
2542 | 614 |
((intOps & SelectionKey.OP_CONNECT) != 0) && |
615 |
((state == ChannelState.UNCONNECTED) || (state == ChannelState.PENDING))) { |
|
616 |
newOps |= SelectionKey.OP_CONNECT; |
|
617 |
readyToConnect = true; |
|
618 |
} |
|
619 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
620 |
if (((ops & Net.POLLOUT) != 0) && |
2542 | 621 |
((intOps & SelectionKey.OP_WRITE) != 0) && |
622 |
isConnected()) |
|
623 |
newOps |= SelectionKey.OP_WRITE; |
|
624 |
||
625 |
sk.nioReadyOps(newOps); |
|
626 |
return (newOps & ~oldOps) != 0; |
|
627 |
} |
|
628 |
||
629 |
@Override |
|
630 |
public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) { |
|
631 |
return translateReadyOps(ops, sk.nioReadyOps(), sk); |
|
632 |
} |
|
633 |
||
634 |
@Override |
|
635 |
@SuppressWarnings("all") |
|
636 |
public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) { |
|
637 |
return translateReadyOps(ops, 0, sk); |
|
638 |
} |
|
639 |
||
640 |
@Override |
|
641 |
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) { |
|
642 |
int newOps = 0; |
|
643 |
if ((ops & SelectionKey.OP_READ) != 0) |
|
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
644 |
newOps |= Net.POLLIN; |
2542 | 645 |
if ((ops & SelectionKey.OP_WRITE) != 0) |
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
646 |
newOps |= Net.POLLOUT; |
2542 | 647 |
if ((ops & SelectionKey.OP_CONNECT) != 0) |
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
648 |
newOps |= Net.POLLCONN; |
2542 | 649 |
sk.selector.putEventOps(sk, newOps); |
650 |
} |
|
651 |
||
652 |
@Override |
|
653 |
public void kill() throws IOException { |
|
654 |
synchronized (stateLock) { |
|
655 |
if (state == ChannelState.KILLED) |
|
656 |
return; |
|
657 |
if (state == ChannelState.UNINITIALIZED) { |
|
658 |
state = ChannelState.KILLED; |
|
659 |
return; |
|
660 |
} |
|
661 |
assert !isOpen() && !isRegistered(); |
|
662 |
||
663 |
/* Postpone the kill if there is a waiting reader |
|
664 |
* or writer thread. */ |
|
665 |
if (receiverThread == 0 && senderThread == 0) { |
|
4669
11d1dbd3598d
6915313: Reorganize implementation to make it more feasible to port to JDK6
chegar
parents:
3320
diff
changeset
|
666 |
SctpNet.close(fdVal); |
2542 | 667 |
state = ChannelState.KILLED; |
668 |
} else { |
|
669 |
state = ChannelState.KILLPENDING; |
|
670 |
} |
|
671 |
} |
|
672 |
} |
|
673 |
||
674 |
@Override |
|
675 |
public <T> SctpChannel setOption(SctpSocketOption<T> name, T value) |
|
676 |
throws IOException { |
|
677 |
if (name == null) |
|
678 |
throw new NullPointerException(); |
|
679 |
if (!supportedOptions().contains(name)) |
|
680 |
throw new UnsupportedOperationException("'" + name + "' not supported"); |
|
681 |
||
682 |
synchronized (stateLock) { |
|
683 |
if (!isOpen()) |
|
684 |
throw new ClosedChannelException(); |
|
685 |
||
686 |
SctpNet.setSocketOption(fdVal, name, value, 0 /*oneToOne*/); |
|
687 |
} |
|
688 |
return this; |
|
689 |
} |
|
690 |
||
691 |
@Override |
|
692 |
@SuppressWarnings("unchecked") |
|
693 |
public <T> T getOption(SctpSocketOption<T> name) throws IOException { |
|
694 |
if (name == null) |
|
695 |
throw new NullPointerException(); |
|
696 |
if (!supportedOptions().contains(name)) |
|
697 |
throw new UnsupportedOperationException("'" + name + "' not supported"); |
|
698 |
||
699 |
synchronized (stateLock) { |
|
700 |
if (!isOpen()) |
|
701 |
throw new ClosedChannelException(); |
|
702 |
||
703 |
return (T)SctpNet.getSocketOption(fdVal, name, 0 /*oneToOne*/); |
|
704 |
} |
|
705 |
} |
|
706 |
||
707 |
private static class DefaultOptionsHolder { |
|
708 |
static final Set<SctpSocketOption<?>> defaultOptions = defaultOptions(); |
|
709 |
||
710 |
private static Set<SctpSocketOption<?>> defaultOptions() { |
|
711 |
HashSet<SctpSocketOption<?>> set = new HashSet<SctpSocketOption<?>>(10); |
|
712 |
set.add(SCTP_DISABLE_FRAGMENTS); |
|
713 |
set.add(SCTP_EXPLICIT_COMPLETE); |
|
714 |
set.add(SCTP_FRAGMENT_INTERLEAVE); |
|
715 |
set.add(SCTP_INIT_MAXSTREAMS); |
|
716 |
set.add(SCTP_NODELAY); |
|
717 |
set.add(SCTP_PRIMARY_ADDR); |
|
718 |
set.add(SCTP_SET_PEER_PRIMARY_ADDR); |
|
719 |
set.add(SO_SNDBUF); |
|
720 |
set.add(SO_RCVBUF); |
|
721 |
set.add(SO_LINGER); |
|
722 |
return Collections.unmodifiableSet(set); |
|
723 |
} |
|
724 |
} |
|
725 |
||
726 |
@Override |
|
727 |
public final Set<SctpSocketOption<?>> supportedOptions() { |
|
728 |
return DefaultOptionsHolder.defaultOptions; |
|
729 |
} |
|
730 |
||
731 |
@Override |
|
732 |
public <T> MessageInfo receive(ByteBuffer buffer, |
|
733 |
T attachment, |
|
734 |
NotificationHandler<T> handler) |
|
735 |
throws IOException { |
|
736 |
return receive(buffer, attachment, handler, false); |
|
737 |
} |
|
738 |
||
739 |
private <T> MessageInfo receive(ByteBuffer buffer, |
|
740 |
T attachment, |
|
741 |
NotificationHandler<T> handler, |
|
742 |
boolean fromConnect) |
|
743 |
throws IOException { |
|
744 |
if (buffer == null) |
|
745 |
throw new IllegalArgumentException("buffer cannot be null"); |
|
746 |
||
747 |
if (buffer.isReadOnly()) |
|
748 |
throw new IllegalArgumentException("Read-only buffer"); |
|
749 |
||
750 |
if (receiveInvoked.get()) |
|
751 |
throw new IllegalReceiveException( |
|
752 |
"cannot invoke receive from handler"); |
|
753 |
receiveInvoked.set(Boolean.TRUE); |
|
754 |
||
755 |
try { |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10137
diff
changeset
|
756 |
ResultContainer resultContainer = new ResultContainer(); |
2542 | 757 |
do { |
758 |
resultContainer.clear(); |
|
759 |
synchronized (receiveLock) { |
|
760 |
if (!ensureReceiveOpen()) |
|
761 |
return null; |
|
762 |
||
763 |
int n = 0; |
|
764 |
try { |
|
765 |
begin(); |
|
766 |
||
767 |
synchronized (stateLock) { |
|
768 |
if(!isOpen()) |
|
769 |
return null; |
|
770 |
receiverThread = NativeThread.current(); |
|
771 |
} |
|
772 |
||
773 |
do { |
|
3320
a7c037dd2e14
6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents:
3072
diff
changeset
|
774 |
n = receive(fdVal, buffer, resultContainer, fromConnect); |
2542 | 775 |
} while ((n == IOStatus.INTERRUPTED) && isOpen()); |
776 |
} finally { |
|
777 |
receiverCleanup(); |
|
778 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
779 |
assert IOStatus.check(n); |
|
780 |
} |
|
781 |
||
782 |
if (!resultContainer.isNotification()) { |
|
783 |
/* message or nothing */ |
|
784 |
if (resultContainer.hasSomething()) { |
|
785 |
/* Set the association before returning */ |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10137
diff
changeset
|
786 |
MessageInfoImpl info = |
2542 | 787 |
resultContainer.getMessageInfo(); |
788 |
synchronized (stateLock) { |
|
789 |
assert association != null; |
|
790 |
info.setAssociation(association); |
|
791 |
} |
|
792 |
return info; |
|
793 |
} else |
|
794 |
/* Non-blocking may return null if nothing available*/ |
|
795 |
return null; |
|
796 |
} else { /* notification */ |
|
797 |
synchronized (stateLock) { |
|
798 |
handleNotificationInternal( |
|
799 |
resultContainer); |
|
800 |
} |
|
801 |
} |
|
802 |
||
803 |
if (fromConnect) { |
|
804 |
/* If we reach here, then it was connect that invoked |
|
3320
a7c037dd2e14
6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents:
3072
diff
changeset
|
805 |
* receive and received the COMM_UP. We have already |
a7c037dd2e14
6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents:
3072
diff
changeset
|
806 |
* handled the COMM_UP with the internal notification |
a7c037dd2e14
6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents:
3072
diff
changeset
|
807 |
* handler. Simply return. */ |
2542 | 808 |
return null; |
809 |
} |
|
810 |
} /* receiveLock */ |
|
811 |
} while (handler == null ? true : |
|
812 |
(invokeNotificationHandler(resultContainer, handler, attachment) |
|
813 |
== HandlerResult.CONTINUE)); |
|
814 |
||
815 |
return null; |
|
816 |
} finally { |
|
817 |
receiveInvoked.set(Boolean.FALSE); |
|
818 |
} |
|
819 |
} |
|
820 |
||
821 |
private int receive(int fd, |
|
822 |
ByteBuffer dst, |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10137
diff
changeset
|
823 |
ResultContainer resultContainer, |
3320
a7c037dd2e14
6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents:
3072
diff
changeset
|
824 |
boolean peek) |
2542 | 825 |
throws IOException { |
826 |
int pos = dst.position(); |
|
827 |
int lim = dst.limit(); |
|
828 |
assert (pos <= lim); |
|
829 |
int rem = (pos <= lim ? lim - pos : 0); |
|
830 |
if (dst instanceof DirectBuffer && rem > 0) |
|
3320
a7c037dd2e14
6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents:
3072
diff
changeset
|
831 |
return receiveIntoNativeBuffer(fd, resultContainer, dst, rem, pos, peek); |
2542 | 832 |
|
833 |
/* Substitute a native buffer */ |
|
834 |
int newSize = Math.max(rem, 1); |
|
835 |
ByteBuffer bb = Util.getTemporaryDirectBuffer(newSize); |
|
836 |
try { |
|
3320
a7c037dd2e14
6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents:
3072
diff
changeset
|
837 |
int n = receiveIntoNativeBuffer(fd, resultContainer, bb, newSize, 0, peek); |
2542 | 838 |
bb.flip(); |
839 |
if (n > 0 && rem > 0) |
|
840 |
dst.put(bb); |
|
841 |
return n; |
|
842 |
} finally { |
|
843 |
Util.releaseTemporaryDirectBuffer(bb); |
|
844 |
} |
|
845 |
} |
|
846 |
||
847 |
private int receiveIntoNativeBuffer(int fd, |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10137
diff
changeset
|
848 |
ResultContainer resultContainer, |
2542 | 849 |
ByteBuffer bb, |
850 |
int rem, |
|
3320
a7c037dd2e14
6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents:
3072
diff
changeset
|
851 |
int pos, |
a7c037dd2e14
6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents:
3072
diff
changeset
|
852 |
boolean peek) |
2542 | 853 |
throws IOException |
854 |
{ |
|
3320
a7c037dd2e14
6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents:
3072
diff
changeset
|
855 |
int n = receive0(fd, resultContainer, ((DirectBuffer)bb).address() + pos, rem, peek); |
2542 | 856 |
|
857 |
if (n > 0) |
|
858 |
bb.position(pos + n); |
|
859 |
return n; |
|
860 |
} |
|
861 |
||
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9679
diff
changeset
|
862 |
private InternalNotificationHandler internalNotificationHandler = |
2542 | 863 |
new InternalNotificationHandler(); |
864 |
||
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10137
diff
changeset
|
865 |
private void handleNotificationInternal(ResultContainer resultContainer) |
2542 | 866 |
{ |
867 |
invokeNotificationHandler(resultContainer, |
|
868 |
internalNotificationHandler, null); |
|
869 |
} |
|
870 |
||
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9679
diff
changeset
|
871 |
private class InternalNotificationHandler |
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9679
diff
changeset
|
872 |
extends AbstractNotificationHandler<Object> |
2542 | 873 |
{ |
874 |
@Override |
|
875 |
public HandlerResult handleNotification( |
|
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9679
diff
changeset
|
876 |
AssociationChangeNotification not, Object unused) { |
2542 | 877 |
if (not.event().equals( |
4677
1b6ce3fbc01b
6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents:
4669
diff
changeset
|
878 |
AssociationChangeNotification.AssocChangeEvent.COMM_UP) && |
1b6ce3fbc01b
6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents:
4669
diff
changeset
|
879 |
association == null) { |
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10137
diff
changeset
|
880 |
AssociationChange sac = (AssociationChange) not; |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10137
diff
changeset
|
881 |
association = new AssociationImpl |
2542 | 882 |
(sac.assocId(), sac.maxInStreams(), sac.maxOutStreams()); |
883 |
} |
|
884 |
return HandlerResult.CONTINUE; |
|
885 |
} |
|
886 |
} |
|
887 |
||
888 |
private <T> HandlerResult invokeNotificationHandler |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10137
diff
changeset
|
889 |
(ResultContainer resultContainer, |
2542 | 890 |
NotificationHandler<T> handler, |
891 |
T attachment) { |
|
892 |
SctpNotification notification = resultContainer.notification(); |
|
893 |
synchronized (stateLock) { |
|
894 |
notification.setAssociation(association); |
|
895 |
} |
|
896 |
||
897 |
if (!(handler instanceof AbstractNotificationHandler)) { |
|
898 |
return handler.handleNotification(notification, attachment); |
|
899 |
} |
|
900 |
||
901 |
/* AbstractNotificationHandler */ |
|
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9679
diff
changeset
|
902 |
AbstractNotificationHandler<T> absHandler = |
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9679
diff
changeset
|
903 |
(AbstractNotificationHandler<T>)handler; |
2542 | 904 |
switch(resultContainer.type()) { |
905 |
case ASSOCIATION_CHANGED : |
|
906 |
return absHandler.handleNotification( |
|
907 |
resultContainer.getAssociationChanged(), attachment); |
|
908 |
case PEER_ADDRESS_CHANGED : |
|
909 |
return absHandler.handleNotification( |
|
910 |
resultContainer.getPeerAddressChanged(), attachment); |
|
911 |
case SEND_FAILED : |
|
912 |
return absHandler.handleNotification( |
|
913 |
resultContainer.getSendFailed(), attachment); |
|
914 |
case SHUTDOWN : |
|
915 |
return absHandler.handleNotification( |
|
916 |
resultContainer.getShutdown(), attachment); |
|
917 |
default : |
|
918 |
/* implementation specific handlers */ |
|
919 |
return absHandler.handleNotification( |
|
920 |
resultContainer.notification(), attachment); |
|
921 |
} |
|
922 |
} |
|
923 |
||
924 |
private void checkAssociation(Association sendAssociation) { |
|
925 |
synchronized (stateLock) { |
|
926 |
if (sendAssociation != null && !sendAssociation.equals(association)) { |
|
927 |
throw new IllegalArgumentException( |
|
928 |
"Cannot send to another association"); |
|
929 |
} |
|
930 |
} |
|
931 |
} |
|
932 |
||
933 |
private void checkStreamNumber(int streamNumber) { |
|
934 |
synchronized (stateLock) { |
|
935 |
if (association != null) { |
|
936 |
if (streamNumber < 0 || |
|
937 |
streamNumber >= association.maxOutboundStreams()) |
|
938 |
throw new InvalidStreamException(); |
|
939 |
} |
|
940 |
} |
|
941 |
} |
|
942 |
||
943 |
/* TODO: Add support for ttl and isComplete to both 121 12M |
|
944 |
* SCTP_EOR not yet supported on reference platforms |
|
945 |
* TTL support limited... |
|
946 |
*/ |
|
947 |
@Override |
|
948 |
public int send(ByteBuffer buffer, MessageInfo messageInfo) |
|
949 |
throws IOException { |
|
950 |
if (buffer == null) |
|
951 |
throw new IllegalArgumentException("buffer cannot be null"); |
|
952 |
||
953 |
if (messageInfo == null) |
|
954 |
throw new IllegalArgumentException("messageInfo cannot be null"); |
|
955 |
||
956 |
checkAssociation(messageInfo.association()); |
|
957 |
checkStreamNumber(messageInfo.streamNumber()); |
|
958 |
||
959 |
synchronized (sendLock) { |
|
960 |
ensureSendOpen(); |
|
961 |
||
962 |
int n = 0; |
|
963 |
try { |
|
964 |
begin(); |
|
965 |
||
966 |
synchronized (stateLock) { |
|
967 |
if(!isOpen()) |
|
968 |
return 0; |
|
969 |
senderThread = NativeThread.current(); |
|
970 |
} |
|
971 |
||
972 |
do { |
|
973 |
n = send(fdVal, buffer, messageInfo); |
|
974 |
} while ((n == IOStatus.INTERRUPTED) && isOpen()); |
|
975 |
||
976 |
return IOStatus.normalize(n); |
|
977 |
} finally { |
|
978 |
senderCleanup(); |
|
979 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
980 |
assert IOStatus.check(n); |
|
981 |
} |
|
982 |
} |
|
983 |
} |
|
984 |
||
985 |
private int send(int fd, ByteBuffer src, MessageInfo messageInfo) |
|
986 |
throws IOException { |
|
987 |
int streamNumber = messageInfo.streamNumber(); |
|
988 |
SocketAddress target = messageInfo.address(); |
|
989 |
boolean unordered = messageInfo.isUnordered(); |
|
990 |
int ppid = messageInfo.payloadProtocolID(); |
|
991 |
||
992 |
if (src instanceof DirectBuffer) |
|
4677
1b6ce3fbc01b
6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents:
4669
diff
changeset
|
993 |
return sendFromNativeBuffer(fd, src, target, streamNumber, |
2542 | 994 |
unordered, ppid); |
995 |
||
996 |
/* Substitute a native buffer */ |
|
4677
1b6ce3fbc01b
6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents:
4669
diff
changeset
|
997 |
int pos = src.position(); |
1b6ce3fbc01b
6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents:
4669
diff
changeset
|
998 |
int lim = src.limit(); |
1b6ce3fbc01b
6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents:
4669
diff
changeset
|
999 |
assert (pos <= lim && streamNumber >= 0); |
1b6ce3fbc01b
6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents:
4669
diff
changeset
|
1000 |
|
1b6ce3fbc01b
6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents:
4669
diff
changeset
|
1001 |
int rem = (pos <= lim ? lim - pos : 0); |
2542 | 1002 |
ByteBuffer bb = Util.getTemporaryDirectBuffer(rem); |
1003 |
try { |
|
1004 |
bb.put(src); |
|
1005 |
bb.flip(); |
|
1006 |
/* Do not update src until we see how many bytes were written */ |
|
1007 |
src.position(pos); |
|
1008 |
||
4677
1b6ce3fbc01b
6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents:
4669
diff
changeset
|
1009 |
int n = sendFromNativeBuffer(fd, bb, target, streamNumber, |
2542 | 1010 |
unordered, ppid); |
1011 |
if (n > 0) { |
|
1012 |
/* now update src */ |
|
1013 |
src.position(pos + n); |
|
1014 |
} |
|
1015 |
return n; |
|
1016 |
} finally { |
|
1017 |
Util.releaseTemporaryDirectBuffer(bb); |
|
1018 |
} |
|
1019 |
} |
|
1020 |
||
1021 |
private int sendFromNativeBuffer(int fd, |
|
1022 |
ByteBuffer bb, |
|
1023 |
SocketAddress target, |
|
1024 |
int streamNumber, |
|
1025 |
boolean unordered, |
|
1026 |
int ppid) |
|
1027 |
throws IOException { |
|
16089 | 1028 |
InetAddress addr = null; // no preferred address |
1029 |
int port = 0; |
|
1030 |
if (target != null) { |
|
1031 |
InetSocketAddress isa = Net.checkAddress(target); |
|
1032 |
addr = isa.getAddress(); |
|
1033 |
port = isa.getPort(); |
|
1034 |
} |
|
1035 |
||
4677
1b6ce3fbc01b
6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents:
4669
diff
changeset
|
1036 |
int pos = bb.position(); |
1b6ce3fbc01b
6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents:
4669
diff
changeset
|
1037 |
int lim = bb.limit(); |
1b6ce3fbc01b
6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents:
4669
diff
changeset
|
1038 |
assert (pos <= lim); |
1b6ce3fbc01b
6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents:
4669
diff
changeset
|
1039 |
int rem = (pos <= lim ? lim - pos : 0); |
1b6ce3fbc01b
6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents:
4669
diff
changeset
|
1040 |
|
16089 | 1041 |
int written = send0(fd, ((DirectBuffer)bb).address() + pos, rem, addr, |
1042 |
port, -1 /*121*/, streamNumber, unordered, ppid); |
|
2542 | 1043 |
if (written > 0) |
1044 |
bb.position(pos + written); |
|
1045 |
return written; |
|
1046 |
} |
|
1047 |
||
1048 |
@Override |
|
1049 |
public SctpChannel shutdown() throws IOException { |
|
1050 |
synchronized(stateLock) { |
|
1051 |
if (isShutdown) |
|
1052 |
return this; |
|
1053 |
||
1054 |
ensureSendOpen(); |
|
1055 |
SctpNet.shutdown(fdVal, -1); |
|
1056 |
if (senderThread != 0) |
|
1057 |
NativeThread.signal(senderThread); |
|
1058 |
isShutdown = true; |
|
1059 |
} |
|
1060 |
return this; |
|
1061 |
} |
|
1062 |
||
1063 |
@Override |
|
1064 |
public Set<SocketAddress> getAllLocalAddresses() |
|
1065 |
throws IOException { |
|
1066 |
synchronized (stateLock) { |
|
1067 |
if (!isOpen()) |
|
1068 |
throw new ClosedChannelException(); |
|
1069 |
if (!isBound()) |
|
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9679
diff
changeset
|
1070 |
return Collections.emptySet(); |
2542 | 1071 |
|
1072 |
return SctpNet.getLocalAddresses(fdVal); |
|
1073 |
} |
|
1074 |
} |
|
1075 |
||
1076 |
@Override |
|
1077 |
public Set<SocketAddress> getRemoteAddresses() |
|
1078 |
throws IOException { |
|
1079 |
synchronized (stateLock) { |
|
1080 |
if (!isOpen()) |
|
1081 |
throw new ClosedChannelException(); |
|
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
1082 |
if (!isConnected() || isShutdown) |
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9679
diff
changeset
|
1083 |
return Collections.emptySet(); |
2542 | 1084 |
|
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
1085 |
try { |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
1086 |
return SctpNet.getRemoteAddresses(fdVal, 0/*unused*/); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
1087 |
} catch (SocketException unused) { |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
1088 |
/* an open connected channel should always have remote addresses */ |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
1089 |
return remoteAddresses; |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
1090 |
} |
2542 | 1091 |
} |
1092 |
} |
|
1093 |
||
1094 |
/* Native */ |
|
1095 |
private static native void initIDs(); |
|
1096 |
||
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10137
diff
changeset
|
1097 |
static native int receive0(int fd, ResultContainer resultContainer, |
3320
a7c037dd2e14
6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents:
3072
diff
changeset
|
1098 |
long address, int length, boolean peek) throws IOException; |
2542 | 1099 |
|
1100 |
static native int send0(int fd, long address, int length, |
|
16089 | 1101 |
InetAddress addr, int port, int assocId, int streamNumber, |
2542 | 1102 |
boolean unordered, int ppid) throws IOException; |
1103 |
||
1104 |
private static native int checkConnect(FileDescriptor fd, boolean block, |
|
1105 |
boolean ready) throws IOException; |
|
1106 |
||
1107 |
static { |
|
19607
bee007586d06
8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents:
16100
diff
changeset
|
1108 |
IOUtil.load(); /* loads nio & net native libraries */ |
2542 | 1109 |
java.security.AccessController.doPrivileged( |
12559
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
11823
diff
changeset
|
1110 |
new java.security.PrivilegedAction<Void>() { |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
11823
diff
changeset
|
1111 |
public Void run() { |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
11823
diff
changeset
|
1112 |
System.loadLibrary("sctp"); |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
11823
diff
changeset
|
1113 |
return null; |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
11823
diff
changeset
|
1114 |
} |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
11823
diff
changeset
|
1115 |
}); |
2542 | 1116 |
initIDs(); |
1117 |
} |
|
1118 |
} |