1 /*
2 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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
256 x = new AsynchronousCloseException();
257 e = x;
258 } finally {
259 end();
260 }
261 if (e != null) {
262 if (e instanceof IOException) {
263 var isa = (InetSocketAddress)pendingRemote;
264 e = SocketExceptions.of((IOException)e, isa);
265 }
266 // close channel if connection cannot be established
267 try {
268 close();
269 } catch (Throwable suppressed) {
270 e.addSuppressed(suppressed);
271 }
272 }
273
274 // invoke handler and set result
275 CompletionHandler<Void,Object> handler = connectHandler;
276 Object att = connectAttachment;
277 PendingFuture<Void,Object> future = connectFuture;
278 if (handler == null) {
279 future.setResult(null, e);
280 } else {
281 if (mayInvokeDirect) {
282 Invoker.invokeUnchecked(handler, att, null, e);
283 } else {
284 Invoker.invokeIndirectly(this, handler, att, null, e);
285 }
286 }
287 }
288
289 @Override
290 @SuppressWarnings("unchecked")
291 <A> Future<Void> implConnect(SocketAddress remote,
292 A attachment,
293 CompletionHandler<Void,? super A> handler)
294 {
295 if (!isOpen()) {
388 try {
389 begin();
390
391 if (scattering) {
392 n = (int)IOUtil.read(fd, readBuffers, nd);
393 } else {
394 n = IOUtil.read(fd, readBuffer, -1, nd);
395 }
396 if (n == IOStatus.UNAVAILABLE) {
397 // spurious wakeup, is this possible?
398 synchronized (updateLock) {
399 readPending = true;
400 }
401 return;
402 }
403
404 // allow objects to be GC'ed.
405 this.readBuffer = null;
406 this.readBuffers = null;
407 this.readAttachment = null;
408
409 // allow another read to be initiated
410 enableReading();
411
412 } catch (Throwable x) {
413 enableReading();
414 if (x instanceof ClosedChannelException)
415 x = new AsynchronousCloseException();
416 exc = x;
417 } finally {
418 // restart poll in case of concurrent write
419 if (!(exc instanceof AsynchronousCloseException))
420 lockAndUpdateEvents();
421 end();
422 }
423
424 // cancel the associated timer
425 if (timeout != null)
426 timeout.cancel(false);
427
583 try {
584 begin();
585
586 if (gathering) {
587 n = (int)IOUtil.write(fd, writeBuffers, nd);
588 } else {
589 n = IOUtil.write(fd, writeBuffer, -1, nd);
590 }
591 if (n == IOStatus.UNAVAILABLE) {
592 // spurious wakeup, is this possible?
593 synchronized (updateLock) {
594 writePending = true;
595 }
596 return;
597 }
598
599 // allow objects to be GC'ed.
600 this.writeBuffer = null;
601 this.writeBuffers = null;
602 this.writeAttachment = null;
603
604 // allow another write to be initiated
605 enableWriting();
606
607 } catch (Throwable x) {
608 enableWriting();
609 if (x instanceof ClosedChannelException)
610 x = new AsynchronousCloseException();
611 exc = x;
612 } finally {
613 // restart poll in case of concurrent write
614 if (!(exc instanceof AsynchronousCloseException))
615 lockAndUpdateEvents();
616 end();
617 }
618
619 // cancel the associated timer
620 if (timer != null)
621 timer.cancel(false);
622
|
1 /*
2 * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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
256 x = new AsynchronousCloseException();
257 e = x;
258 } finally {
259 end();
260 }
261 if (e != null) {
262 if (e instanceof IOException) {
263 var isa = (InetSocketAddress)pendingRemote;
264 e = SocketExceptions.of((IOException)e, isa);
265 }
266 // close channel if connection cannot be established
267 try {
268 close();
269 } catch (Throwable suppressed) {
270 e.addSuppressed(suppressed);
271 }
272 }
273
274 // invoke handler and set result
275 CompletionHandler<Void,Object> handler = connectHandler;
276 connectHandler = null;
277 Object att = connectAttachment;
278 PendingFuture<Void,Object> future = connectFuture;
279 if (handler == null) {
280 future.setResult(null, e);
281 } else {
282 if (mayInvokeDirect) {
283 Invoker.invokeUnchecked(handler, att, null, e);
284 } else {
285 Invoker.invokeIndirectly(this, handler, att, null, e);
286 }
287 }
288 }
289
290 @Override
291 @SuppressWarnings("unchecked")
292 <A> Future<Void> implConnect(SocketAddress remote,
293 A attachment,
294 CompletionHandler<Void,? super A> handler)
295 {
296 if (!isOpen()) {
389 try {
390 begin();
391
392 if (scattering) {
393 n = (int)IOUtil.read(fd, readBuffers, nd);
394 } else {
395 n = IOUtil.read(fd, readBuffer, -1, nd);
396 }
397 if (n == IOStatus.UNAVAILABLE) {
398 // spurious wakeup, is this possible?
399 synchronized (updateLock) {
400 readPending = true;
401 }
402 return;
403 }
404
405 // allow objects to be GC'ed.
406 this.readBuffer = null;
407 this.readBuffers = null;
408 this.readAttachment = null;
409 this.readHandler = null;
410
411 // allow another read to be initiated
412 enableReading();
413
414 } catch (Throwable x) {
415 enableReading();
416 if (x instanceof ClosedChannelException)
417 x = new AsynchronousCloseException();
418 exc = x;
419 } finally {
420 // restart poll in case of concurrent write
421 if (!(exc instanceof AsynchronousCloseException))
422 lockAndUpdateEvents();
423 end();
424 }
425
426 // cancel the associated timer
427 if (timeout != null)
428 timeout.cancel(false);
429
585 try {
586 begin();
587
588 if (gathering) {
589 n = (int)IOUtil.write(fd, writeBuffers, nd);
590 } else {
591 n = IOUtil.write(fd, writeBuffer, -1, nd);
592 }
593 if (n == IOStatus.UNAVAILABLE) {
594 // spurious wakeup, is this possible?
595 synchronized (updateLock) {
596 writePending = true;
597 }
598 return;
599 }
600
601 // allow objects to be GC'ed.
602 this.writeBuffer = null;
603 this.writeBuffers = null;
604 this.writeAttachment = null;
605 this.writeHandler = null;
606
607 // allow another write to be initiated
608 enableWriting();
609
610 } catch (Throwable x) {
611 enableWriting();
612 if (x instanceof ClosedChannelException)
613 x = new AsynchronousCloseException();
614 exc = x;
615 } finally {
616 // restart poll in case of concurrent write
617 if (!(exc instanceof AsynchronousCloseException))
618 lockAndUpdateEvents();
619 end();
620 }
621
622 // cancel the associated timer
623 if (timer != null)
624 timer.cancel(false);
625
|