|
23 | 23 | import io.netty.channel.Channel; |
24 | 24 | import io.netty.channel.ChannelConfig; |
25 | 25 | import io.netty.channel.ChannelException; |
26 | | -import io.netty.channel.ChannelFuture; |
27 | | -import io.netty.channel.ChannelFutureListener; |
28 | 26 | import io.netty.channel.ChannelMetadata; |
29 | 27 | import io.netty.channel.ChannelOutboundBuffer; |
30 | 28 | import io.netty.channel.ChannelPromise; |
31 | | -import io.netty.channel.ConnectTimeoutException; |
32 | 29 | import io.netty.channel.EventLoop; |
33 | 30 | import io.netty.channel.IoEvent; |
34 | 31 | import io.netty.channel.IoRegistration; |
|
41 | 38 | import io.netty.channel.unix.Socket; |
42 | 39 | import io.netty.channel.unix.UnixChannel; |
43 | 40 | import io.netty.util.ReferenceCountUtil; |
44 | | -import io.netty.util.concurrent.Future; |
45 | 41 |
|
46 | 42 | import java.io.IOException; |
47 | 43 | import java.io.UncheckedIOException; |
|
50 | 46 | import java.nio.ByteBuffer; |
51 | 47 | import java.nio.channels.AlreadyConnectedException; |
52 | 48 | import java.nio.channels.ClosedChannelException; |
53 | | -import java.nio.channels.ConnectionPendingException; |
54 | 49 | import java.nio.channels.NotYetConnectedException; |
55 | 50 | import java.nio.channels.UnresolvedAddressException; |
56 | | -import java.util.concurrent.TimeUnit; |
57 | 51 |
|
58 | 52 | import static io.netty.channel.epoll.EpollIoOps.EPOLL_ERR_IN_MASK; |
59 | 53 | import static io.netty.channel.epoll.EpollIoOps.EPOLL_ERR_OUT_MASK; |
|
65 | 59 | abstract class AbstractEpollChannel extends AbstractChannel implements UnixChannel { |
66 | 60 | private static final ChannelMetadata METADATA = new ChannelMetadata(false); |
67 | 61 | protected final LinuxSocket socket; |
68 | | - /** |
69 | | - * The future of the current connection attempt. If not null, subsequent |
70 | | - * connection attempts will fail. |
71 | | - */ |
72 | 62 | private ChannelPromise connectPromise; |
73 | | - private Future<?> connectTimeoutFuture; |
74 | 63 | private SocketAddress requestedRemoteAddress; |
75 | 64 | private volatile SocketAddress local; |
76 | 65 | private volatile SocketAddress remote; |
@@ -175,12 +164,6 @@ protected void doClose(ChannelPromise promise) { |
175 | 164 | this.connectPromise = null; |
176 | 165 | } |
177 | 166 |
|
178 | | - Future<?> future = connectTimeoutFuture; |
179 | | - if (future != null) { |
180 | | - future.cancel(false); |
181 | | - connectTimeoutFuture = null; |
182 | | - } |
183 | | - |
184 | 167 | if (isRegistered()) { |
185 | 168 | // Need to check if we are on the EventLoop as doClose() may be triggered by the GlobalEventExecutor |
186 | 169 | // if SO_LINGER is used. |
@@ -440,6 +423,24 @@ final long doWriteOrSendBytes(ByteBuf data, InetSocketAddress remoteAddress, boo |
440 | 423 | remoteAddress.getAddress(), remoteAddress.getPort(), fastOpen); |
441 | 424 | } |
442 | 425 |
|
| 426 | + @Override |
| 427 | + protected void doConnect( |
| 428 | + final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) { |
| 429 | + final boolean connected; |
| 430 | + requestedRemoteAddress = remoteAddress; |
| 431 | + try { |
| 432 | + connected = doConnect(remoteAddress, localAddress); |
| 433 | + } catch (Throwable cause) { |
| 434 | + promise.setFailure(cause); |
| 435 | + return; |
| 436 | + } |
| 437 | + if (connected) { |
| 438 | + promise.setSuccess(); |
| 439 | + } else { |
| 440 | + connectPromise = promise; |
| 441 | + } |
| 442 | + } |
| 443 | + |
443 | 444 | protected abstract class AbstractEpollUnsafe extends AbstractUnsafe implements EpollIoHandle { |
444 | 445 | boolean readPending; |
445 | 446 | private EpollRecvByteAllocatorHandle allocHandle; |
@@ -642,127 +643,24 @@ protected final void clearEpollIn0() { |
642 | 643 | } |
643 | 644 | } |
644 | 645 |
|
645 | | - @Override |
646 | | - public void connect( |
647 | | - final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) { |
648 | | - // Don't mark the connect promise as uncancellable as in fact we can cancel it as it is using |
649 | | - // non-blocking io. |
650 | | - if (promise.isDone() || !ensureOpen(promise)) { |
651 | | - return; |
652 | | - } |
653 | | - |
654 | | - try { |
655 | | - if (connectPromise != null) { |
656 | | - throw new ConnectionPendingException(); |
657 | | - } |
658 | | - |
659 | | - boolean wasActive = isActive(); |
660 | | - if (doConnect(remoteAddress, localAddress)) { |
661 | | - fulfillConnectPromise(promise, wasActive); |
662 | | - } else { |
663 | | - connectPromise = promise; |
664 | | - requestedRemoteAddress = remoteAddress; |
665 | | - |
666 | | - // Schedule connect timeout. |
667 | | - final int connectTimeoutMillis = config().getConnectTimeoutMillis(); |
668 | | - if (connectTimeoutMillis > 0) { |
669 | | - connectTimeoutFuture = executor().schedule(new Runnable() { |
670 | | - @Override |
671 | | - public void run() { |
672 | | - ChannelPromise connectPromise = AbstractEpollChannel.this.connectPromise; |
673 | | - if (connectPromise != null && !connectPromise.isDone() |
674 | | - && connectPromise.tryFailure(new ConnectTimeoutException( |
675 | | - "connection timed out after " + connectTimeoutMillis + " ms: " + |
676 | | - remoteAddress))) { |
677 | | - close(newPromise()); |
678 | | - } |
679 | | - } |
680 | | - }, connectTimeoutMillis, TimeUnit.MILLISECONDS); |
681 | | - } |
682 | | - |
683 | | - promise.addListener(new ChannelFutureListener() { |
684 | | - @Override |
685 | | - public void operationComplete(ChannelFuture future) { |
686 | | - // If the connect future is cancelled we also cancel the timeout and close the |
687 | | - // underlying socket. |
688 | | - if (future.isCancelled()) { |
689 | | - if (connectTimeoutFuture != null) { |
690 | | - connectTimeoutFuture.cancel(false); |
691 | | - } |
692 | | - connectPromise = null; |
693 | | - close(newPromise()); |
694 | | - } |
695 | | - } |
696 | | - }); |
697 | | - } |
698 | | - } catch (Throwable t) { |
699 | | - closeIfClosed(); |
700 | | - promise.tryFailure(annotateConnectException(t, remoteAddress)); |
701 | | - } |
702 | | - } |
703 | | - |
704 | | - private void fulfillConnectPromise(ChannelPromise promise, boolean wasActive) { |
705 | | - if (promise == null) { |
706 | | - // Closed via cancellation and the promise has been notified already. |
707 | | - return; |
708 | | - } |
709 | | - active = true; |
710 | | - |
711 | | - // Get the state as trySuccess() may trigger an ChannelFutureListener that will close the Channel. |
712 | | - // We still need to ensure we call fireChannelActive() in this case. |
713 | | - boolean active = isActive(); |
714 | | - |
715 | | - // trySuccess() will return false if a user cancelled the connection attempt. |
716 | | - boolean promiseSet = promise.trySuccess(); |
717 | | - |
718 | | - // Regardless if the connection attempt was cancelled, channelActive() event should be triggered, |
719 | | - // because what happened is what happened. |
720 | | - if (!wasActive && active) { |
721 | | - pipeline().fireChannelActive(); |
722 | | - } |
723 | | - |
724 | | - // If a user cancelled the connection attempt, close the channel, which is followed by channelInactive(). |
725 | | - if (!promiseSet) { |
726 | | - close(newPromise()); |
727 | | - } |
728 | | - } |
729 | | - |
730 | | - private void fulfillConnectPromise(ChannelPromise promise, Throwable cause) { |
731 | | - if (promise == null) { |
732 | | - // Closed via cancellation and the promise has been notified already. |
733 | | - return; |
734 | | - } |
735 | | - |
736 | | - // Use tryFailure() instead of setFailure() to avoid the race against cancel(). |
737 | | - promise.tryFailure(cause); |
738 | | - closeIfClosed(); |
739 | | - } |
740 | | - |
741 | 646 | private void finishConnect() { |
742 | 647 | // Note this method is invoked by the event loop only if the connection attempt was |
743 | 648 | // neither cancelled nor timed out. |
744 | 649 |
|
745 | 650 | assert executor().inEventLoop(); |
746 | | - |
747 | | - boolean connectStillInProgress = false; |
| 651 | + assert connectPromise != null; |
| 652 | + ChannelPromise promise = connectPromise; |
| 653 | + final boolean connected; |
748 | 654 | try { |
749 | | - boolean wasActive = isActive(); |
750 | | - if (!doFinishConnect()) { |
751 | | - connectStillInProgress = true; |
752 | | - return; |
753 | | - } |
754 | | - fulfillConnectPromise(connectPromise, wasActive); |
755 | | - } catch (Throwable t) { |
756 | | - fulfillConnectPromise(connectPromise, annotateConnectException(t, requestedRemoteAddress)); |
757 | | - } finally { |
758 | | - if (!connectStillInProgress) { |
759 | | - // Check for null as the connectTimeoutFuture is only created if a connectTimeoutMillis > 0 is used |
760 | | - // See https://github.com/netty/netty/issues/1770 |
761 | | - if (connectTimeoutFuture != null) { |
762 | | - connectTimeoutFuture.cancel(false); |
763 | | - } |
764 | | - connectPromise = null; |
765 | | - } |
| 655 | + connected = doFinishConnect(); |
| 656 | + } catch (Throwable cause) { |
| 657 | + connectPromise = null; |
| 658 | + promise.setFailure(cause); |
| 659 | + return; |
| 660 | + } |
| 661 | + if (connected) { |
| 662 | + connectPromise = null; |
| 663 | + promise.setSuccess(); |
766 | 664 | } |
767 | 665 | } |
768 | 666 |
|
@@ -776,6 +674,7 @@ private boolean doFinishConnect() throws Exception { |
776 | 674 | remote = computeRemoteAddr((InetSocketAddress) requestedRemoteAddress, socket.remoteAddress()); |
777 | 675 | } |
778 | 676 | requestedRemoteAddress = null; |
| 677 | + active = true; |
779 | 678 |
|
780 | 679 | return true; |
781 | 680 | } |
@@ -828,6 +727,7 @@ protected boolean doConnect(SocketAddress remoteAddress, SocketAddress localAddr |
828 | 727 | if (connected) { |
829 | 728 | remote = remoteSocketAddr == null ? |
830 | 729 | remoteAddress : computeRemoteAddr(remoteSocketAddr, socket.remoteAddress()); |
| 730 | + active = true; |
831 | 731 | } |
832 | 732 | // We always need to set the localAddress even if not connected yet as the bind already took place. |
833 | 733 | // |
|