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 remov… · netty/netty@19b4de6

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-126Lines changed: 32 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,7 @@
6565
abstract class AbstractEpollChannel extends AbstractChannel implements UnixChannel {
6666
private static final ChannelMetadata METADATA = new ChannelMetadata(false);
6767
protected final LinuxSocket socket;
68-
/**
69-
* The future of the current connection attempt. If not null, subsequent
70-
* connection attempts will fail.
71-
*/
7268
private ChannelPromise connectPromise;
73-
private Future<?> connectTimeoutFuture;
7469
private SocketAddress requestedRemoteAddress;
7570
private volatile SocketAddress local;
7671
private volatile SocketAddress remote;
@@ -175,12 +170,6 @@ protected void doClose(ChannelPromise promise) {
175170
this.connectPromise = null;
176171
}
177172

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

432+
@Override
433+
protected void doConnect(
434+
final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) {
435+
final boolean connected;
436+
requestedRemoteAddress = remoteAddress;
437+
try {
438+
connected = doConnect(remoteAddress, localAddress);
439+
} catch (Throwable cause) {
440+
promise.setFailure(cause);
441+
return;
442+
}
443+
if (connected) {
444+
promise.setSuccess();
445+
} else {
446+
connectPromise = promise;
447+
}
448+
}
449+
443450
protected abstract class AbstractEpollUnsafe extends AbstractUnsafe implements EpollIoHandle {
444451
boolean readPending;
445452
private EpollRecvByteAllocatorHandle allocHandle;
@@ -642,127 +649,24 @@ protected final void clearEpollIn0() {
642649
}
643650
}
644651

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

745656
assert executor().inEventLoop();
746-
747-
boolean connectStillInProgress = false;
657+
assert connectPromise != null;
658+
ChannelPromise promise = connectPromise;
659+
final boolean connected;
748660
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-
}
661+
connected = doFinishConnect();
662+
} catch (Throwable cause) {
663+
connectPromise = null;
664+
promise.setFailure(cause);
665+
return;
666+
}
667+
if (connected) {
668+
connectPromise = null;
669+
promise.setSuccess();
766670
}
767671
}
768672

@@ -776,6 +680,7 @@ private boolean doFinishConnect() throws Exception {
776680
remote = computeRemoteAddr((InetSocketAddress) requestedRemoteAddress, socket.remoteAddress());
777681
}
778682
requestedRemoteAddress = null;
683+
active = true;
779684

780685
return true;
781686
}
@@ -828,6 +733,7 @@ protected boolean doConnect(SocketAddress remoteAddress, SocketAddress localAddr
828733
if (connected) {
829734
remote = remoteSocketAddr == null ?
830735
remoteAddress : computeRemoteAddr(remoteSocketAddr, socket.remoteAddress());
736+
active = true;
831737
}
832738
// We always need to set the localAddress even if not connected yet as the bind already took place.
833739
//
Collapse file

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

Copy file name to clipboardExpand all lines: transport-classes-epoll/src/main/java/io/netty/channel/epoll/AbstractEpollServerChannel.java
+6-6Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,18 @@ protected Object filterOutboundMessage(Object msg) throws Exception {
8181
protected abstract Channel newChildChannel(EventLoop eventLoop, int fd, byte[] remote, int offset, int len)
8282
throws Exception;
8383

84+
@Override
85+
protected void doConnect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) {
86+
// Connect not supported by ServerChannel implementations
87+
promise.setFailure(new UnsupportedOperationException());
88+
}
89+
8490
final class EpollServerSocketUnsafe extends AbstractEpollUnsafe {
8591
// Will hold the remote address after accept(...) was successful.
8692
// We need 24 bytes for the address as maximum + 1 byte for storing the length.
8793
// So use 26 bytes as it's a power of two.
8894
private final byte[] acceptedAddress = new byte[26];
8995

90-
@Override
91-
public void connect(SocketAddress socketAddress, SocketAddress socketAddress2, ChannelPromise channelPromise) {
92-
// Connect not supported by ServerChannel implementations
93-
channelPromise.setFailure(new UnsupportedOperationException());
94-
}
95-
9696
@Override
9797
void epollInReady() {
9898
assert executor().inEventLoop();

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.