GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — AbstractChannel: Move connect logical to AbstractChannel and so r… · netty/netty@3eaaa58

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-classes-quic/src/main/java/io/netty/handler/codec/quic
    • QuicheQuicChannel.java
  • transport-classes-epoll/src/main/java/io/netty/channel/epoll
    • AbstractEpollChannel.java
    • AbstractEpollServerChannel.java
  • transport-classes-io_uring/src/main/java/io/netty/channel/uring
    • AbstractIoUringChannel.java
    • AbstractIoUringServerChannel.java
    • IoUringDomainSocketChannel.java
  • transport-classes-kqueue/src/main/java/io/netty/channel/kqueue
    • AbstractKQueueChannel.java
  • transport-sctp/src/main/java/io/netty/channel/sctp/nio
    • NioSctpChannel.java
    • NioSctpServerChannel.java
  • transport/src
    • main/java/io/netty
      • bootstrap
        • FailedChannel.java
      • channel
        • AbstractChannel.java
        • AbstractServerChannel.java
        • embedded
          • EmbeddedChannel.java
        • local
          • LocalChannel.java
          • LocalServerChannel.java
        • nio
          • AbstractNioChannel.java
        • socket/nio
          • NioDatagramChannel.java
          • NioServerSocketChannel.java
          • NioSocketChannel.java
    • test/java/io/netty/channel
      • AbstractChannelTest.java
      • ChannelOutboundBufferTest.java
      • DefaultChannelPipelineTailTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-classes-quic/src/main/java/io/netty/handler/codec/quic/QuicheQuicChannel.java‎

Copy file name to clipboardExpand all lines: codec-classes-quic/src/main/java/io/netty/handler/codec/quic/QuicheQuicChannel.java
+38-67Lines changed: 38 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import io.netty.channel.ChannelOutboundBuffer;
2929
import io.netty.channel.ChannelPipeline;
3030
import io.netty.channel.ChannelPromise;
31-
import io.netty.channel.ConnectTimeoutException;
3231
import io.netty.channel.DefaultChannelPipeline;
3332
import io.netty.channel.RecvByteBufAllocator;
3433
import io.netty.channel.socket.DatagramPacket;
@@ -143,7 +142,6 @@ public void operationComplete(ChannelFuture future) {
143142
private ByteBuf finBuffer;
144143
private ByteBuf outErrorCodeBuffer;
145144
private ChannelPromise connectPromise;
146-
private ScheduledFuture<?> connectTimeoutFuture;
147145
private QuicConnectionAddress connectAddress;
148146
private CloseData closeData;
149147
private QuicConnectionCloseEvent connectionCloseEvent;
@@ -1489,6 +1487,44 @@ public void run() {
14891487
}
14901488
}
14911489

1490+
@Override
1491+
protected void doConnect(SocketAddress remote, SocketAddress local, ChannelPromise channelPromise) {
1492+
assert executor().inEventLoop();
1493+
if (server) {
1494+
channelPromise.setFailure(new UnsupportedOperationException());
1495+
return;
1496+
}
1497+
1498+
if (connectPromise != null) {
1499+
channelPromise.setFailure(new ConnectionPendingException());
1500+
return;
1501+
}
1502+
1503+
if (remote instanceof QuicConnectionAddress) {
1504+
if (!sourceConnectionIds.isEmpty()) {
1505+
// If a key is assigned we know this channel was already connected.
1506+
channelPromise.setFailure(new AlreadyConnectedException());
1507+
return;
1508+
}
1509+
1510+
connectAddress = (QuicConnectionAddress) remote;
1511+
connectPromise = channelPromise;
1512+
1513+
parent().connect(new QuicheQuicChannelAddress(QuicheQuicChannel.this))
1514+
.addListener(f -> {
1515+
ChannelPromise connectPromise = QuicheQuicChannel.this.connectPromise;
1516+
if (connectPromise != null && !f.isSuccess()) {
1517+
connectPromise.tryFailure(f.cause());
1518+
// close everything after notify about failure.
1519+
unsafe().close(newPromise());
1520+
}
1521+
});
1522+
return;
1523+
}
1524+
1525+
channelPromise.setFailure(new UnsupportedOperationException());
1526+
}
1527+
14921528
private final class QuicChannelUnsafe extends AbstractChannel.AbstractUnsafe {
14931529

14941530
void connectStream(QuicStreamType type, @Nullable ChannelHandler handler,
@@ -1526,70 +1562,6 @@ void connectStream(QuicStreamType type, @Nullable ChannelHandler handler,
15261562
});
15271563
}
15281564

1529-
@Override
1530-
public void connect(SocketAddress remote, SocketAddress local, ChannelPromise channelPromise) {
1531-
assert executor().inEventLoop();
1532-
if (!channelPromise.setUncancellable()) {
1533-
return;
1534-
}
1535-
if (server) {
1536-
channelPromise.setFailure(new UnsupportedOperationException());
1537-
return;
1538-
}
1539-
1540-
if (connectPromise != null) {
1541-
channelPromise.setFailure(new ConnectionPendingException());
1542-
return;
1543-
}
1544-
1545-
if (remote instanceof QuicConnectionAddress) {
1546-
if (!sourceConnectionIds.isEmpty()) {
1547-
// If a key is assigned we know this channel was already connected.
1548-
channelPromise.setFailure(new AlreadyConnectedException());
1549-
return;
1550-
}
1551-
1552-
connectAddress = (QuicConnectionAddress) remote;
1553-
connectPromise = channelPromise;
1554-
1555-
// Schedule connect timeout.
1556-
int connectTimeoutMillis = config().getConnectTimeoutMillis();
1557-
if (connectTimeoutMillis > 0) {
1558-
connectTimeoutFuture = executor().schedule(() -> {
1559-
ChannelPromise connectPromise = QuicheQuicChannel.this.connectPromise;
1560-
if (connectPromise != null && !connectPromise.isDone()
1561-
&& connectPromise.tryFailure(new ConnectTimeoutException(
1562-
"connection timed out: " + remote))) {
1563-
close(newPromise());
1564-
}
1565-
}, connectTimeoutMillis, TimeUnit.MILLISECONDS);
1566-
}
1567-
1568-
connectPromise.addListener((ChannelFuture future) -> {
1569-
if (future.isCancelled()) {
1570-
if (connectTimeoutFuture != null) {
1571-
connectTimeoutFuture.cancel(false);
1572-
}
1573-
connectPromise = null;
1574-
close(newPromise());
1575-
}
1576-
});
1577-
1578-
parent().connect(new QuicheQuicChannelAddress(QuicheQuicChannel.this))
1579-
.addListener(f -> {
1580-
ChannelPromise connectPromise = QuicheQuicChannel.this.connectPromise;
1581-
if (connectPromise != null && !f.isSuccess()) {
1582-
connectPromise.tryFailure(f.cause());
1583-
// close everything after notify about failure.
1584-
unsafe().close(newPromise());
1585-
}
1586-
});
1587-
return;
1588-
}
1589-
1590-
channelPromise.setFailure(new UnsupportedOperationException());
1591-
}
1592-
15931565
private void fireConnectCloseEventIfNeeded(QuicheQuicConnection conn) {
15941566
if (connectionCloseEvent == null && !conn.isFreed()) {
15951567
connectionCloseEvent = Quiche.quiche_conn_peer_error(conn.address());
@@ -1941,7 +1913,6 @@ private boolean handlePendingChannelActive(QuicheQuicConnection conn) {
19411913
state = ChannelState.ACTIVE;
19421914

19431915
boolean promiseSet = promise.trySuccess();
1944-
pipeline().fireChannelActive();
19451916
notifyAboutHandshakeCompletionIfNeeded(conn, null);
19461917
fireDatagramExtensionEvent(conn);
19471918
if (!promiseSet) {
Collapse file

‎transport-classes-epoll/src/main/java/io/netty/channel/epoll/AbstractEpollChannel.java‎

Copy file name to clipboardExpand all lines: transport-classes-epoll/src/main/java/io/netty/channel/epoll/AbstractEpollChannel.java
+32-132Lines changed: 32 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,9 @@
2323
import io.netty.channel.Channel;
2424
import io.netty.channel.ChannelConfig;
2525
import io.netty.channel.ChannelException;
26-
import io.netty.channel.ChannelFuture;
27-
import io.netty.channel.ChannelFutureListener;
2826
import io.netty.channel.ChannelMetadata;
2927
import io.netty.channel.ChannelOutboundBuffer;
3028
import io.netty.channel.ChannelPromise;
31-
import io.netty.channel.ConnectTimeoutException;
3229
import io.netty.channel.EventLoop;
3330
import io.netty.channel.IoEvent;
3431
import io.netty.channel.IoRegistration;
@@ -41,7 +38,6 @@
4138
import io.netty.channel.unix.Socket;
4239
import io.netty.channel.unix.UnixChannel;
4340
import io.netty.util.ReferenceCountUtil;
44-
import io.netty.util.concurrent.Future;
4541

4642
import java.io.IOException;
4743
import java.io.UncheckedIOException;
@@ -50,10 +46,8 @@
5046
import java.nio.ByteBuffer;
5147
import java.nio.channels.AlreadyConnectedException;
5248
import java.nio.channels.ClosedChannelException;
53-
import java.nio.channels.ConnectionPendingException;
5449
import java.nio.channels.NotYetConnectedException;
5550
import java.nio.channels.UnresolvedAddressException;
56-
import java.util.concurrent.TimeUnit;
5751

5852
import static io.netty.channel.epoll.EpollIoOps.EPOLL_ERR_IN_MASK;
5953
import static io.netty.channel.epoll.EpollIoOps.EPOLL_ERR_OUT_MASK;
@@ -65,12 +59,7 @@
6559
abstract class AbstractEpollChannel extends AbstractChannel implements UnixChannel {
6660
private static final ChannelMetadata METADATA = new ChannelMetadata(false);
6761
protected final LinuxSocket socket;
68-
/**
69-
* The future of the current connection attempt. If not null, subsequent
70-
* connection attempts will fail.
71-
*/
7262
private ChannelPromise connectPromise;
73-
private Future<?> connectTimeoutFuture;
7463
private SocketAddress requestedRemoteAddress;
7564
private volatile SocketAddress local;
7665
private volatile SocketAddress remote;
@@ -175,12 +164,6 @@ protected void doClose(ChannelPromise promise) {
175164
this.connectPromise = null;
176165
}
177166

178-
Future<?> future = connectTimeoutFuture;
179-
if (future != null) {
180-
future.cancel(false);
181-
connectTimeoutFuture = null;
182-
}
183-
184167
if (isRegistered()) {
185168
// Need to check if we are on the EventLoop as doClose() may be triggered by the GlobalEventExecutor
186169
// if SO_LINGER is used.
@@ -440,6 +423,24 @@ final long doWriteOrSendBytes(ByteBuf data, InetSocketAddress remoteAddress, boo
440423
remoteAddress.getAddress(), remoteAddress.getPort(), fastOpen);
441424
}
442425

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+
443444
protected abstract class AbstractEpollUnsafe extends AbstractUnsafe implements EpollIoHandle {
444445
boolean readPending;
445446
private EpollRecvByteAllocatorHandle allocHandle;
@@ -642,127 +643,24 @@ protected final void clearEpollIn0() {
642643
}
643644
}
644645

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-
741646
private void finishConnect() {
742647
// Note this method is invoked by the event loop only if the connection attempt was
743648
// neither cancelled nor timed out.
744649

745650
assert executor().inEventLoop();
746-
747-
boolean connectStillInProgress = false;
651+
assert connectPromise != null;
652+
ChannelPromise promise = connectPromise;
653+
final boolean connected;
748654
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();
766664
}
767665
}
768666

@@ -776,6 +674,7 @@ private boolean doFinishConnect() throws Exception {
776674
remote = computeRemoteAddr((InetSocketAddress) requestedRemoteAddress, socket.remoteAddress());
777675
}
778676
requestedRemoteAddress = null;
677+
active = true;
779678

780679
return true;
781680
}
@@ -828,6 +727,7 @@ protected boolean doConnect(SocketAddress remoteAddress, SocketAddress localAddr
828727
if (connected) {
829728
remote = remoteSocketAddr == null ?
830729
remoteAddress : computeRemoteAddr(remoteSocketAddr, socket.remoteAddress());
730+
active = true;
831731
}
832732
// We always need to set the localAddress even if not connected yet as the bind already took place.
833733
//

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.